blob: 642f57814c618fd1b111eff86c2b2885d5c892bb [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 Ogunwalef7cab102016-10-25 15:25:14 -070095import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070096import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070097import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070098import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080099import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700100import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800101import static com.android.server.wm.WindowManagerService.H.REPORT_FOCUS_CHANGE;
102import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700103import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
104import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700105import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700106import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700107import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
108import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800109import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
110import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
111import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700112import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700113import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700114import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700115import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700116import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700117import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700118import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700119import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700120import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700121
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700122import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700123import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700124import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700125import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700126import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700127import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700128import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800129import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700130import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700131import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100132import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700133import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700134import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700135import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700136import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700137import android.os.RemoteException;
138import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100139import android.os.Trace;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800140import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700141import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700142import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700143import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700144import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100145import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700146import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800147import android.view.Gravity;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700148import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700149import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700150import android.view.Surface;
151import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100152import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700153import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700154
Bryce Lee48f4b572017-04-10 10:54:15 -0700155import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800156import com.android.internal.util.ToBooleanFunction;
Adrian Roose99bc052017-11-20 17:55:31 +0100157import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200158import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100159import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100160import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700161
162import java.io.PrintWriter;
163import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700164import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700165import java.util.HashMap;
166import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700167import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700168import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100169import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800170import java.util.function.Consumer;
171import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700172
Craig Mautner59c00972012-07-30 12:10:24 -0700173/**
174 * Utility class for keeping track of the WindowStates and other pertinent contents of a
175 * particular Display.
176 *
177 * IMPORTANT: No method from this class should ever be used without holding
178 * WindowManagerService.mWindowMap.
179 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800180class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
181 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700182 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700183
184 /** Unique identifier of this stack. */
185 private final int mDisplayId;
186
Wale Ogunwale3a931692016-11-02 16:49:48 -0700187 /** The containers below are the only child containers the display can have. */
188 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100189 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700190 // Contains all non-app window containers that should be displayed above the app containers
191 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800192 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100193 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700194 // Contains all non-app window containers that should be displayed below the app containers
195 // (e.g. Wallpaper).
196 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100197 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700198 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
199 // 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 -0800200 // window containers together and move them in-sync if/when needed. We use a subclass of
201 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
202 private final NonMagnifiableWindowContainers mImeWindowsContainers =
203 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700204
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000205 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800206 private WindowState mTmpWindow2;
207 private WindowAnimator mTmpWindowAnimator;
208 private boolean mTmpRecoveringMemory;
209 private boolean mUpdateImeTarget;
210 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700211 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700212
Wale Ogunwale02319a62016-09-26 15:21:22 -0700213 // Mapping from a token IBinder to a WindowToken object on this display.
214 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
215
Andrii Kuliancd097992017-03-23 18:31:59 -0700216 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700217 int mInitialDisplayWidth = 0;
218 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700219 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700220
Adrian Roos1cf585052018-01-03 18:43:27 +0100221 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100222 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100223 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100224
Andrii Kuliancd097992017-03-23 18:31:59 -0700225 /**
226 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
227 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
228 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
229 */
Craig Mautner59c00972012-07-30 12:10:24 -0700230 int mBaseDisplayWidth = 0;
231 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700232 /**
233 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
234 * but can be set from Settings or via shell command "adb shell wm density".
235 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
236 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700237 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700238 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700239 private final DisplayInfo mDisplayInfo = new DisplayInfo();
240 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700241 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800242 private final DisplayPolicy mDisplayPolicy;
243 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000244 DisplayFrames mDisplayFrames;
245
Andrii Kulian06d07d62017-03-14 11:11:47 -0700246 /**
247 * For default display it contains real metrics, empty for others.
248 * @see WindowManagerService#createWatermarkInTransaction()
249 */
250 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
251 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
252 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700253
Andrii Kulian06d07d62017-03-14 11:11:47 -0700254 /**
255 * Compat metrics computed based on {@link #mDisplayMetrics}.
256 * @see #updateDisplayAndOrientation(int)
257 */
258 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
259
260 /** The desired scaling factor for compatible apps. */
261 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700262
Andrii Kulian8ee72852017-03-10 10:36:45 -0800263 /**
264 * Current rotation of the display.
265 * Constants as per {@link android.view.Surface.Rotation}.
266 *
Robert Carrae606b42018-02-15 15:36:23 -0800267 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800268 */
269 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700270
Andrii Kulian8ee72852017-03-10 10:36:45 -0800271 /**
272 * Last applied orientation of the display.
273 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
274 *
275 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
276 */
277 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700278
Andrii Kulian8ee72852017-03-10 10:36:45 -0800279 /**
280 * Flag indicating that the application is receiving an orientation that has different metrics
281 * than it expected. E.g. Portrait instead of Landscape.
282 *
Robert Carrae606b42018-02-15 15:36:23 -0800283 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800284 */
285 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700286
Andrii Kulian8ee72852017-03-10 10:36:45 -0800287 /**
288 * Orientation forced by some window. If there is no visible window that specifies orientation
289 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
290 *
291 * @see NonAppWindowContainers#getOrientation()
292 */
293 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700294
Andrii Kulian8ee72852017-03-10 10:36:45 -0800295 /**
296 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
297 * occluded.
298 *
299 * @see NonAppWindowContainers#getOrientation()
300 */
301 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
302
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700303 /**
304 * Keep track of wallpaper visibility to notify changes.
305 */
306 private boolean mLastWallpaperVisible = false;
307
Andrii Kulian06d07d62017-03-14 11:11:47 -0700308 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700309
Craig Mautner39834192012-09-02 07:47:24 -0700310 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700311 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700312 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800313 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800314
Andrii Kulian839def92016-11-02 10:58:58 -0700315 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800316 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800317 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800318
Andrii Kulianf0379de2018-03-14 16:24:07 -0700319 /**
320 * Flag indicating whether WindowManager should override info for this display in
321 * DisplayManager.
322 */
323 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700324
Craig Mautnerdc548482014-02-05 13:35:24 -0800325 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700326 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800327
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700328 /** Detect user tapping outside of current focused task bounds .*/
329 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700330
Craig Mautner6601b7b2013-04-29 10:29:11 -0700331 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700332 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700333
Craig Mautner6601b7b2013-04-29 10:29:11 -0700334 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800335 private final Rect mTmpRect = new Rect();
336 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700337 private final RectF mTmpRectF = new RectF();
338 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800339 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700340
Issei Suzuki43190bd2018-08-20 17:28:41 +0200341
Bryce Leef3c6a472017-11-14 14:53:06 -0800342 /** Used for handing back size of display */
343 private final Rect mTmpBounds = new Rect();
344
Craig Mautner95da1082014-02-24 17:54:35 -0800345 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700346 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800347
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700348 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700349 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700350
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800351 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800352 /** A collection of windows that provide tap exclude regions inside of them. */
353 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800354
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000355 private boolean mHaveBootMsg = false;
356 private boolean mHaveApp = false;
357 private boolean mHaveWallpaper = false;
358 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700359
360 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
361
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700362 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
363 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000364 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
365 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700366
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700367 // True if this display is in the process of being removed. Used to determine if the removal of
368 // the display's direct children should be allowed.
369 private boolean mRemovingDisplay = false;
370
Bryce Leed1871262017-06-12 14:12:29 -0700371 // {@code false} if this display is in the processing of being created.
372 private boolean mDisplayReady = false;
373
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800374 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700375
Robert Carrb1579c82017-09-05 14:54:47 -0700376 private final SurfaceSession mSession = new SurfaceSession();
377
378 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800379 * Window that is currently interacting with the user. This window is responsible for receiving
380 * key events and pointer events from the user.
381 */
382 WindowState mCurrentFocus = null;
383
384 /**
385 * The last focused window that we've notified the client that the focus is changed.
386 */
387 WindowState mLastFocus = null;
388
389 /**
390 * Windows that have lost input focus and are waiting for the new focus window to be displayed
391 * before they are told about this.
392 */
393 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
394
395 /**
396 * The foreground app of this display. Windows below this app cannot be the focused window. If
397 * the user taps on the area outside of the task of the focused app, we will notify AM about the
398 * new task the user wants to interact with.
399 */
400 AppWindowToken mFocusedApp = null;
401
402 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
403 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
404
405 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
406 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
407
408 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700409 * We organize all top-level Surfaces in to the following layers.
410 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800411 * and omitted from Screen-Magnification, for example the strict mode flash or
412 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700413 * {@link #mWindowingLayer} contains everything else.
414 */
415 private SurfaceControl mOverlayLayer;
416
417 /**
418 * See {@link #mOverlayLayer}
419 */
420 private SurfaceControl mWindowingLayer;
421
422 /**
423 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
424 * <p>
425 * For these surfaces currently we use a surface based on the larger of width or height so we
426 * don't have to resize when rotating the display.
427 */
428 private int mSurfaceSize;
429
Adrian Roos5251b1d2018-03-23 18:57:43 +0100430 /**
431 * Sequence number for the current layout pass.
432 */
433 int mLayoutSeq = 0;
434
Chavi Weingarten3a748552018-05-14 17:32:42 +0000435 /**
436 * Specifies the count to determine whether to defer updating the IME target until ready.
437 */
438 private int mDeferUpdateImeTargetCount;
439
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100440 /** Temporary float array to retrieve 3x3 matrix values. */
441 private final float[] mTmpFloats = new float[9];
442
Robert Carr24be9ab2018-04-30 17:54:53 -0700443 private MagnificationSpec mMagnificationSpec;
444
Arthur Hung95b38a92018-07-20 18:56:12 +0800445 private InputMonitor mInputMonitor;
446
Jorim Jaggif1292892018-09-10 11:58:13 +0200447 /** Caches the value whether told display manager that we have content. */
448 private boolean mLastHasContent;
449
Issei Suzuki43190bd2018-08-20 17:28:41 +0200450 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
451
lumark90120a82018-08-15 00:33:03 +0800452 /**
453 * The input method window for this display.
454 */
455 WindowState mInputMethodWindow;
456
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800457 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
458 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800459 final AppWindowToken atoken = w.mAppToken;
460 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200461 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800462 if (w.performShowLocked()) {
463 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
464 if (DEBUG_LAYOUT_REPEATS) {
465 mService.mWindowPlacerLocked.debugLayoutRepeats(
466 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
467 }
468 }
469 }
470 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800471 };
472
473 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
474 final WindowStateAnimator winAnimator = w.mWinAnimator;
475 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
476 return;
477 }
478
Jorim Jaggi8f520872018-08-14 17:00:20 +0200479 // If this window is animating, ensure the animation background is set.
480 final AnimationAdapter anim = w.mAppToken != null
481 ? w.mAppToken.getAnimation()
482 : w.getAnimation();
483 if (anim != null) {
484 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800485 if (color != 0) {
486 final TaskStack stack = w.getStack();
487 if (stack != null) {
488 stack.setAnimationBackground(winAnimator, color);
489 }
490 }
491 }
492 };
493
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800494 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
495 final int lostFocusUid = mTmpWindow.mOwnerUid;
496 final Handler handler = mService.mH;
497 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
498 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
499 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
500 w.mAttrs.hideTimeoutMilliseconds);
501 }
502 }
503 };
504
505 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800506 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800507 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
508 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
509
510 if (!w.canReceiveKeys()) {
511 return false;
512 }
513
514 final AppWindowToken wtoken = w.mAppToken;
515
516 // If this window's application has been removed, just skip it.
517 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
518 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
519 + (wtoken.removed ? "removed" : "sendingToBottom"));
520 return false;
521 }
522
523 if (focusedApp == null) {
524 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
525 + " using new focus @ " + w);
526 mTmpWindow = w;
527 return true;
528 }
529
530 if (!focusedApp.windowsAreFocusable()) {
531 // Current focused app windows aren't focusable...
532 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
533 + " focusable using new focus @ " + w);
534 mTmpWindow = w;
535 return true;
536 }
537
538 // Descend through all of the app tokens and find the first that either matches
539 // win.mAppToken (return win) or mFocusedApp (return null).
540 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
541 if (focusedApp.compareTo(wtoken) > 0) {
542 // App stack below focused app stack. No focus for you!!!
543 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
544 "findFocusedWindow: Reached focused app=" + focusedApp);
545 mTmpWindow = null;
546 return true;
547 }
548 }
549
550 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
551 mTmpWindow = w;
552 return true;
553 };
554
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800555 private final Consumer<WindowState> mPerformLayout = w -> {
556 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
557 // wasting time and funky changes while a window is animating away.
558 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
559 || w.isGoneForLayoutLw();
560
561 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
562 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
563 + " mLayoutAttached=" + w.mLayoutAttached
564 + " screen changed=" + w.isConfigChanged());
565 final AppWindowToken atoken = w.mAppToken;
566 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200567 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800568 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
569 + " parentHidden=" + w.isParentWindowHidden());
570 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200571 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800572 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
573 + " parentHidden=" + w.isParentWindowHidden());
574 }
575
576 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
577 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
578 // since that means "perform layout as normal, just don't display").
579 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
580 || ((w.isConfigChanged() || w.setReportResizeHints())
581 && !w.isGoneForLayoutLw() &&
582 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
583 (w.mHasSurface && w.mAppToken != null &&
584 w.mAppToken.layoutConfigChanges)))) {
585 if (!w.mLayoutAttached) {
586 if (mTmpInitial) {
587 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700588 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800589 }
590 if (w.mAttrs.type == TYPE_DREAM) {
591 // Don't layout windows behind a dream, so that if it does stuff like hide
592 // the status bar we won't get a bad transition when it goes away.
593 mTmpWindow = w;
594 }
595 w.mLayoutNeeded = false;
596 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200597 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000598 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100599 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800600
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200601 // If this is the first layout, we need to initialize the last inset values as
602 // otherwise we'd immediately cause an unnecessary resize.
603 if (firstLayout) {
604 w.updateLastInsetValues();
605 }
606
Adrian Roos23df3a32018-03-15 15:41:13 +0100607 if (w.mAppToken != null) {
608 w.mAppToken.layoutLetterbox(w);
609 }
610
chaviw492139a2018-07-16 16:07:35 -0700611 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700612 + " mContainingFrame=" + w.getContainingFrame()
613 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800614 }
615 }
616 };
617
618 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
619 if (w.mLayoutAttached) {
620 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
621 + " mViewVisibility=" + w.mViewVisibility
622 + " mRelayoutCalled=" + w.mRelayoutCalled);
623 // If this view is GONE, then skip it -- keep the current frame, and let the caller
624 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
625 // windows, since that means "perform layout as normal, just don't display").
626 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
627 return;
628 }
629 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
630 || w.mLayoutNeeded) {
631 if (mTmpInitial) {
632 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700633 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800634 }
635 w.mLayoutNeeded = false;
636 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000637 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100638 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700639 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700640 + " mContainingFrame=" + w.getContainingFrame()
641 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800642 }
643 } else if (w.mAttrs.type == TYPE_DREAM) {
644 // Don't layout windows behind a dream, so that if it does stuff like hide the
645 // status bar we won't get a bad transition when it goes away.
646 mTmpWindow = mTmpWindow2;
647 }
648 };
649
650 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
651 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
652 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
653 return w.canBeImeTarget();
654 };
655
656 private final Consumer<WindowState> mApplyPostLayoutPolicy =
657 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
658 mService.mInputMethodTarget);
659
660 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
661 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
662 final boolean obscuredChanged = w.mObscured !=
663 mTmpApplySurfaceChangesTransactionState.obscured;
664 final RootWindowContainer root = mService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800665
666 // Update effect.
667 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
668 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
669 final boolean isDisplayed = w.isDisplayedLw();
670
671 if (isDisplayed && w.isObscuringDisplay()) {
672 // This window completely covers everything behind it, so we want to leave all
673 // of them as undimmed (for performance reasons).
674 root.mObscuringWindow = w;
675 mTmpApplySurfaceChangesTransactionState.obscured = true;
676 }
677
678 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
679 root.handleNotObscuredLocked(w,
680 mTmpApplySurfaceChangesTransactionState.obscured,
681 mTmpApplySurfaceChangesTransactionState.syswin);
682
683 if (w.mHasSurface && isDisplayed) {
684 final int type = w.mAttrs.type;
685 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
686 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
687 mTmpApplySurfaceChangesTransactionState.syswin = true;
688 }
689 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
690 && w.mAttrs.preferredRefreshRate != 0) {
691 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
692 = w.mAttrs.preferredRefreshRate;
693 }
694 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
695 && w.mAttrs.preferredDisplayModeId != 0) {
696 mTmpApplySurfaceChangesTransactionState.preferredModeId
697 = w.mAttrs.preferredDisplayModeId;
698 }
699 }
700 }
701
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800702 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
703 && mWallpaperController.isWallpaperTarget(w)) {
704 // This is the wallpaper target and its obscured state changed... make sure the
705 // current wallpaper's visibility has been updated accordingly.
706 mWallpaperController.updateWallpaperVisibility();
707 }
708
chaviw161ea3e2018-01-31 12:01:12 -0800709 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800710
711 final WindowStateAnimator winAnimator = w.mWinAnimator;
712
713 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700714 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800715
716 // Moved from updateWindowsAndWallpaperLocked().
717 if (w.mHasSurface) {
718 // Take care of the window being ready to display.
719 final boolean committed = winAnimator.commitFinishDrawingLocked();
720 if (isDefaultDisplay && committed) {
721 if (w.mAttrs.type == TYPE_DREAM) {
722 // HACK: When a dream is shown, it may at that point hide the lock screen.
723 // So we need to redo the layout to let the phone window manager make this
724 // happen.
725 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
726 if (DEBUG_LAYOUT_REPEATS) {
727 surfacePlacer.debugLayoutRepeats(
728 "dream and commitFinishDrawingLocked true",
729 pendingLayoutChanges);
730 }
731 }
732 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
733 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
734 "First draw done in potential wallpaper target " + w);
735 root.mWallpaperMayChange = true;
736 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
737 if (DEBUG_LAYOUT_REPEATS) {
738 surfacePlacer.debugLayoutRepeats(
739 "wallpaper and commitFinishDrawingLocked true",
740 pendingLayoutChanges);
741 }
742 }
743 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800744 }
745
746 final AppWindowToken atoken = w.mAppToken;
747 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100748 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800749 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
750 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
751 mTmpUpdateAllDrawn.add(atoken);
752 }
753 }
754
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800755 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
756 mService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800757 }
758
759 w.updateResizingWindowIfNeeded();
760 };
761
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800762 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800763 * Create new {@link DisplayContent} instance, add itself to the root window container and
764 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700765 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800766 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700767 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
768 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700769 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700770 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800771 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100772 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800773 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800774 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
775 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
776 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
777 + " new=" + display);
778 }
779
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700780 mDisplay = display;
781 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700782 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700783 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700784 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700785 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100786 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
787 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700788 initializeDisplayBaseInfo();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800789 mDisplayPolicy = new DisplayPolicy(service);
790 mDisplayRotation = new DisplayRotation(service, this);
791 if (isDefaultDisplay) {
792 // The policy may be invoked right after here, so it requires the necessary default
793 // fields of this display content.
794 mService.mPolicy.setDefaultDisplay(this);
795 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800796 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700797 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700798
Robert Carr74a66a22018-02-23 12:17:51 -0800799 // We use this as our arbitrary surface size for buffer-less parents
800 // that don't impose cropping on their children. It may need to be larger
801 // than the display size because fullscreen windows can be shifted offscreen
802 // due to surfaceInsets. 2 times the largest display dimension feels like an
803 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
804 // layers the ability to match their parent sizes and be able to skip
805 // such arbitrary size settings.
806 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700807
808 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
809 .setSize(mSurfaceSize, mSurfaceSize)
810 .setOpaque(true);
811 mWindowingLayer = b.setName("Display Root").build();
812 mOverlayLayer = b.setName("Display Overlays").build();
813
Robert Carrf59b8dd2017-10-02 18:58:36 -0700814 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700815 .setLayerStack(mWindowingLayer, mDisplayId)
816 .show(mWindowingLayer)
817 .setLayer(mOverlayLayer, 1)
818 .setLayerStack(mOverlayLayer, mDisplayId)
819 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700820 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700821
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700822 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700823 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700824 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700825 super.addChild(mAboveAppWindowsContainers, null);
826 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800827
828 // Add itself as a child to the root container.
829 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700830
831 // TODO(b/62541591): evaluate whether this is the best spot to declare the
832 // {@link DisplayContent} ready for use.
833 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800834
Arthur Hung39134b22018-08-14 11:58:28 +0800835 mInputMonitor = new InputMonitor(service, mDisplayId);
Bryce Leed1871262017-06-12 14:12:29 -0700836 }
837
838 boolean isReady() {
839 // The display is ready when the system and the individual display are both ready.
840 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700841 }
842
843 int getDisplayId() {
844 return mDisplayId;
845 }
846
Wale Ogunwale02319a62016-09-26 15:21:22 -0700847 WindowToken getWindowToken(IBinder binder) {
848 return mTokenMap.get(binder);
849 }
850
851 AppWindowToken getAppWindowToken(IBinder binder) {
852 final WindowToken token = getWindowToken(binder);
853 if (token == null) {
854 return null;
855 }
856 return token.asAppWindowToken();
857 }
858
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700859 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700860 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
861 if (dc != null) {
862 // We currently don't support adding a window token to the display if the display
863 // already has the binder mapped to another token. If there is a use case for supporting
864 // this moving forward we will either need to merge the WindowTokens some how or have
865 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700866 throw new IllegalArgumentException("Can't map token=" + token + " to display="
867 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700868 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700869 if (binder == null) {
870 throw new IllegalArgumentException("Can't map token=" + token + " to display="
871 + getName() + " binder is null");
872 }
873 if (token == null) {
874 throw new IllegalArgumentException("Can't map null token to display="
875 + getName() + " binder=" + binder);
876 }
877
Wale Ogunwale02319a62016-09-26 15:21:22 -0700878 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700879
880 if (token.asAppWindowToken() == null) {
881 // Add non-app token to container hierarchy on the display. App tokens are added through
882 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700883 switch (token.windowType) {
884 case TYPE_WALLPAPER:
885 mBelowAppWindowsContainers.addChild(token);
886 break;
887 case TYPE_INPUT_METHOD:
888 case TYPE_INPUT_METHOD_DIALOG:
889 mImeWindowsContainers.addChild(token);
890 break;
891 default:
892 mAboveAppWindowsContainers.addChild(token);
893 break;
894 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700895 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700896 }
897
898 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700899 final WindowToken token = mTokenMap.remove(binder);
900 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800901 token.setExiting();
902 }
903 return token;
904 }
905
906 /** Changes the display the input window token is housed on to this one. */
907 void reParentWindowToken(WindowToken token) {
908 final DisplayContent prevDc = token.getDisplayContent();
909 if (prevDc == this) {
910 return;
911 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800912 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
913 && token.asAppWindowToken() == null) {
914 // Removed the token from the map, but made sure it's not an app token before removing
915 // from parent.
916 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700917 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800918
919 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700920 }
921
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700922 void removeAppToken(IBinder binder) {
923 final WindowToken token = removeWindowToken(binder);
924 if (token == null) {
925 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
926 return;
927 }
928
929 final AppWindowToken appToken = token.asAppWindowToken();
930
931 if (appToken == null) {
932 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
933 return;
934 }
935
936 appToken.onRemovedFromDisplay();
937 }
938
Riddle Hsuad256a12018-07-18 16:11:30 +0800939 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800940 DisplayWindowController getController() {
941 return (DisplayWindowController) super.getController();
942 }
943
944 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +0800945 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700946 return mDisplay;
947 }
948
Craig Mautner59c00972012-07-30 12:10:24 -0700949 DisplayInfo getDisplayInfo() {
950 return mDisplayInfo;
951 }
952
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700953 DisplayMetrics getDisplayMetrics() {
954 return mDisplayMetrics;
955 }
956
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800957 DisplayPolicy getDisplayPolicy() {
958 return mDisplayPolicy;
959 }
960
Riddle Hsuad256a12018-07-18 16:11:30 +0800961 @Override
962 public DisplayRotation getDisplayRotation() {
963 return mDisplayRotation;
964 }
965
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800966 @VisibleForTesting
967 void setDisplayRotation(DisplayRotation displayRotation) {
968 mDisplayRotation = displayRotation;
969 }
970
Andrii Kulian8ee72852017-03-10 10:36:45 -0800971 int getRotation() {
972 return mRotation;
973 }
974
Andrii Kulianef5ce1c2018-03-20 19:27:24 -0700975 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -0800976 void setRotation(int newRotation) {
977 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +0800978 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -0800979 }
980
981 int getLastOrientation() {
982 return mLastOrientation;
983 }
984
985 void setLastOrientation(int orientation) {
986 mLastOrientation = orientation;
987 }
988
989 boolean getAltOrientation() {
990 return mAltOrientation;
991 }
992
993 void setAltOrientation(boolean altOrientation) {
994 mAltOrientation = altOrientation;
995 }
996
997 int getLastWindowForcedOrientation() {
998 return mLastWindowForcedOrientation;
999 }
1000
Andrii Kulian06d07d62017-03-14 11:11:47 -07001001 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001002 * Temporarily pauses rotation changes until resumed.
1003 *
1004 * This can be used to prevent rotation changes from occurring while the user is
1005 * performing certain operations, such as drag and drop.
1006 *
1007 * This call nests and must be matched by an equal number of calls to
1008 * {@link #resumeRotationLocked}.
1009 */
1010 void pauseRotationLocked() {
1011 mDeferredRotationPauseCount++;
1012 }
1013
1014 /**
1015 * Resumes normal rotation changes after being paused.
1016 */
1017 void resumeRotationLocked() {
1018 if (mDeferredRotationPauseCount <= 0) {
1019 return;
1020 }
1021
1022 mDeferredRotationPauseCount--;
1023 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001024 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001025 }
1026 }
1027
1028 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001029 * If this is true we have updated our desired orientation, but not yet changed the real
1030 * orientation our applied our screen rotation animation. For example, because a previous
1031 * screen rotation was in progress.
1032 *
1033 * @return {@code true} if the there is an ongoing rotation change.
1034 */
1035 boolean rotationNeedsUpdate() {
1036 final int lastOrientation = getLastOrientation();
1037 final int oldRotation = getRotation();
1038 final boolean oldAltOrientation = getAltOrientation();
1039
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001040 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001041 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1042 lastOrientation, rotation);
1043 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1044 return false;
1045 }
1046 return true;
1047 }
1048
1049 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001050 * Update rotation of the display and send configuration if the rotation is changed.
1051 *
1052 * @return {@code true} if the rotation has been changed and the new config is sent.
1053 */
1054 boolean updateRotationAndSendNewConfigIfNeeded() {
1055 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1056 if (changed) {
1057 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
1058 }
1059 return changed;
1060 }
1061
1062 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001063 * Update rotation of the display.
1064 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001065 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1066 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001067 */
Robert Carrae606b42018-02-15 15:36:23 -08001068 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001069 return updateRotationUnchecked(false /* forceUpdate */);
1070 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001071
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001072 /**
1073 * Update rotation of the display with an option to force the update.
1074 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1075 * orientation because we're waiting for some rotation to finish or display
1076 * to unfreeze, which results in configuration of the previously visible
1077 * activity being applied to a newly visible one. Forcing the rotation
1078 * update allows to workaround this issue.
1079 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1080 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
1081 */
1082 boolean updateRotationUnchecked(boolean forceUpdate) {
1083 ScreenRotationAnimation screenRotationAnimation;
1084 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001085 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001086 // Rotation updates have been paused temporarily. Defer the update until
1087 // updates have been resumed.
1088 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1089 return false;
1090 }
1091
1092 screenRotationAnimation =
1093 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1094 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1095 // Rotation updates cannot be performed while the previous rotation change
1096 // animation is still in progress. Skip this update. We will try updating
1097 // again after the animation is finished and the display is unfrozen.
1098 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1099 return false;
1100 }
1101 if (mService.mDisplayFrozen) {
1102 // Even if the screen rotation animation has finished (e.g. isAnimating
1103 // returns false), there is still some time where we haven't yet unfrozen
1104 // the display. We also need to abort rotation here.
1105 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1106 "Deferring rotation, still finishing previous rotation");
1107 return false;
1108 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001109 }
1110
1111 if (!mService.mDisplayEnabled) {
1112 // No point choosing a rotation if the display is not enabled.
1113 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1114 return false;
1115 }
1116
1117 final int oldRotation = mRotation;
1118 final int lastOrientation = mLastOrientation;
1119 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001120 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001121 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1122 + mDisplayId + " based on lastOrientation=" + lastOrientation
1123 + " and oldRotation=" + oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001124 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(mDisplayRotation,
1125 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001126
Robert Carr0e007272017-10-02 13:00:55 -07001127 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001128 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001129 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001130 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1131 // to complete (that is, waiting for windows to redraw). It's tempting to check
1132 // w.mSeamlessRotationCount but that could be incorrect in the case of
1133 // window-removal.
1134 return false;
1135 }
Robert Carr0e007272017-10-02 13:00:55 -07001136
1137 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001138 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001139 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001140 mayRotateSeamlessly = false;
1141 }
1142 for (int i = 0; i < mService.mSessions.size(); i++) {
1143 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1144 mayRotateSeamlessly = false;
1145 break;
1146 }
1147 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001148 }
Robert Carr0e007272017-10-02 13:00:55 -07001149 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001150
1151 // TODO: Implement forced rotation changes.
1152 // Set mAltOrientation to indicate that the application is receiving
1153 // an orientation that has different metrics than it expected.
1154 // eg. Portrait instead of Landscape.
1155
Riddle Hsuad256a12018-07-18 16:11:30 +08001156 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001157 lastOrientation, rotation);
1158
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001159 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1160 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001161 + ", got rotation " + rotation + " which has "
1162 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1163
1164 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1165 // No change.
1166 return false;
1167 }
1168
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001169 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1170 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001171 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1172 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1173
1174 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1175 mService.mWaitingForConfig = true;
1176 }
1177
Riddle Hsuad256a12018-07-18 16:11:30 +08001178 setRotation(rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001179 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001180
1181 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001182 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1183 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001184
1185 setLayoutNeeded();
1186 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001187 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001188
1189 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001190 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001191 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1192 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1193 mDisplayId);
1194 } else {
1195 // The screen rotation animation uses a screenshot to freeze the screen
1196 // while windows resize underneath.
1197 // When we are rotating seamlessly, we allow the elements to transition
1198 // to their rotated state independently and without a freeze required.
1199 screenRotationAnimation = null;
1200
Robert Carrefc75702018-02-16 11:46:16 -08001201 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001202 }
1203
1204 // We need to update our screen size information to match the new rotation. If the rotation
1205 // has actually changed then this method will return true and, according to the comment at
1206 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1207 // By updating the Display info here it will be available to
1208 // #computeScreenConfiguration() later.
1209 updateDisplayAndOrientation(getConfiguration().uiMode);
1210
Robert Carrae606b42018-02-15 15:36:23 -08001211 // NOTE: We disable the rotation in the emulator because
1212 // it doesn't support hardware OpenGL emulation yet.
1213 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1214 && screenRotationAnimation.hasScreenshot()) {
1215 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1216 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1217 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1218 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001219 }
1220 }
1221
Vishnu Nair83537a72018-07-19 21:27:48 -07001222 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001223 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1224 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001225 }, true /* traverseTopToBottom */);
1226
Robert Carrae606b42018-02-15 15:36:23 -08001227 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1228 scheduleAnimation();
1229
Andrii Kulian06d07d62017-03-14 11:11:47 -07001230 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001231 if (w.mHasSurface && !rotateSeamlessly) {
1232 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001233 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001234 mService.mRoot.mOrientationChangeComplete = false;
1235 w.mLastFreezeDuration = 0;
1236 }
1237 w.mReportOrientationChanged = true;
1238 }, true /* traverseTopToBottom */);
1239
1240 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001241 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1242 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001243 }
1244
1245 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1246 final WindowManagerService.RotationWatcher rotationWatcher
1247 = mService.mRotationWatchers.get(i);
1248 if (rotationWatcher.mDisplayId == mDisplayId) {
1249 try {
1250 rotationWatcher.mWatcher.onRotationChanged(rotation);
1251 } catch (RemoteException e) {
1252 // Ignore
1253 }
1254 }
1255 }
1256
1257 // TODO (multi-display): Magnification is supported only for the default display.
1258 // Announce rotation only if we will not animate as we already have the
1259 // windows in final state. Otherwise, we make this call at the rotation end.
1260 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1261 && isDefaultDisplay) {
1262 mService.mAccessibilityController.onRotationChangedLocked(this);
1263 }
1264
1265 return true;
1266 }
1267
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001268 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001269 final int width = mBaseDisplayWidth;
1270 final int height = mBaseDisplayHeight;
1271 final int shortSize;
1272 final int longSize;
1273 if (width > height) {
1274 shortSize = height;
1275 longSize = width;
1276 } else {
1277 shortSize = width;
1278 longSize = height;
1279 }
1280
1281 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1282 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1283
1284 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1285 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001286
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001287 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1288 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001289 }
1290
Andrii Kulian06d07d62017-03-14 11:11:47 -07001291 /**
1292 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1293 * changed.
1294 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1295 */
1296 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1297 // Use the effective "visual" dimensions based on current rotation
1298 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1299 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1300 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1301 int dw = realdw;
1302 int dh = realdh;
1303
1304 if (mAltOrientation) {
1305 if (realdw > realdh) {
1306 // Turn landscape into portrait.
1307 int maxw = (int)(realdh/1.3f);
1308 if (maxw < realdw) {
1309 dw = maxw;
1310 }
1311 } else {
1312 // Turn portrait into landscape.
1313 int maxh = (int)(realdw/1.3f);
1314 if (maxh < realdh) {
1315 dh = maxh;
1316 }
1317 }
1318 }
1319
1320 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001321 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1322 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1323
Andrii Kulian06d07d62017-03-14 11:11:47 -07001324 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001325 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001326 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001327 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001328 mDisplayInfo.rotation = mRotation;
1329 mDisplayInfo.logicalWidth = dw;
1330 mDisplayInfo.logicalHeight = dh;
1331 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1332 mDisplayInfo.appWidth = appWidth;
1333 mDisplayInfo.appHeight = appHeight;
1334 if (isDefaultDisplay) {
1335 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1336 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1337 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001338 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001339 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1340 if (mDisplayScalingDisabled) {
1341 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1342 } else {
1343 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1344 }
1345
Andrii Kulianf0379de2018-03-14 16:24:07 -07001346 // We usually set the override info in DisplayManager so that we get consistent display
1347 // metrics values when displays are changing and don't send out new values until WM is aware
1348 // of them. However, we don't do this for displays that serve as containers for ActivityView
1349 // because we don't want letter-/pillar-boxing during resize.
1350 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1351 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001352 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001353 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001354
1355 mBaseDisplayRect.set(0, 0, dw, dh);
1356
1357 if (isDefaultDisplay) {
1358 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1359 mCompatDisplayMetrics);
1360 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001361
1362 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001363 return mDisplayInfo;
1364 }
1365
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001366 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001367 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1368 }
1369
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001370 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001371 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001372 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001373 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001374 }
Adrian Roos11c25582018-02-19 18:06:36 +01001375 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001376 return WmDisplayCutout.computeSafeInsets(
1377 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001378 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001379 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001380 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1381 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001382 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001383 .getDisplayCutout().getBoundingRectsAll(),
1384 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1385 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001386 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1387 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001388 }
1389
Andrii Kulian06d07d62017-03-14 11:11:47 -07001390 /**
1391 * Compute display configuration based on display properties and policy settings.
1392 * Do not call if mDisplayReady == false.
1393 */
1394 void computeScreenConfiguration(Configuration config) {
1395 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1396
1397 final int dw = displayInfo.logicalWidth;
1398 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001399 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001400 config.windowConfiguration.setWindowingMode(getWindowingMode());
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001401
Adrian Roos11c25582018-02-19 18:06:36 +01001402 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001403 config.screenWidthDp =
1404 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001405 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001406 config.screenHeightDp =
1407 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001408 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001409
Adrian Roos11c25582018-02-19 18:06:36 +01001410 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1411 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001412 final int leftInset = mTmpRect.left;
1413 final int topInset = mTmpRect.top;
1414 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001415 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1416 leftInset + displayInfo.appWidth /* right */,
1417 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001418 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1419 || displayInfo.rotation == Surface.ROTATION_270);
1420
1421 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001422 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001423
1424 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1425 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1426 ? Configuration.SCREENLAYOUT_ROUND_YES
1427 : Configuration.SCREENLAYOUT_ROUND_NO);
1428
1429 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1430 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1431 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1432 dh, mDisplayId);
1433 config.densityDpi = displayInfo.logicalDensityDpi;
1434
1435 config.colorMode =
Chia-I Wuf739bad2018-05-09 12:36:57 -07001436 ((displayInfo.isHdr() && mService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001437 ? Configuration.COLOR_MODE_HDR_YES
1438 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001439 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001440 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1441 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1442
1443 // Update the configuration based on available input devices, lid switch,
1444 // and platform configuration.
1445 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1446 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1447 config.navigation = Configuration.NAVIGATION_NONAV;
1448
1449 int keyboardPresence = 0;
1450 int navigationPresence = 0;
1451 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1452 final int len = devices != null ? devices.length : 0;
1453 for (int i = 0; i < len; i++) {
1454 InputDevice device = devices[i];
1455 if (!device.isVirtual()) {
1456 final int sources = device.getSources();
1457 final int presenceFlag = device.isExternal() ?
1458 WindowManagerPolicy.PRESENCE_EXTERNAL :
1459 WindowManagerPolicy.PRESENCE_INTERNAL;
1460
1461 // TODO(multi-display): Configure on per-display basis.
1462 if (mService.mIsTouchDevice) {
1463 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1464 InputDevice.SOURCE_TOUCHSCREEN) {
1465 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1466 }
1467 } else {
1468 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1469 }
1470
1471 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1472 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1473 navigationPresence |= presenceFlag;
1474 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1475 && config.navigation == Configuration.NAVIGATION_NONAV) {
1476 config.navigation = Configuration.NAVIGATION_DPAD;
1477 navigationPresence |= presenceFlag;
1478 }
1479
1480 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1481 config.keyboard = Configuration.KEYBOARD_QWERTY;
1482 keyboardPresence |= presenceFlag;
1483 }
1484 }
1485 }
1486
1487 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1488 config.navigation = Configuration.NAVIGATION_DPAD;
1489 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1490 }
1491
1492 // Determine whether a hard keyboard is available and enabled.
1493 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1494 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1495 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1496 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1497 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1498 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1499 }
1500
1501 // Let the policy update hidden states.
1502 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1503 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1504 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1505 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1506 }
1507
1508 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1509 int displayId) {
1510 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1511 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1512 final int unrotDw, unrotDh;
1513 if (rotated) {
1514 unrotDw = dh;
1515 unrotDh = dw;
1516 } else {
1517 unrotDw = dw;
1518 unrotDh = dh;
1519 }
1520 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1521 displayId);
1522 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1523 displayId);
1524 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1525 displayId);
1526 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1527 displayId);
1528 return sw;
1529 }
1530
1531 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1532 DisplayMetrics dm, int dw, int dh, int displayId) {
1533 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001534 displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001535 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001536 uiMode, displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001537 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1538 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1539 if (curSize == 0 || size < curSize) {
1540 curSize = size;
1541 }
1542 return curSize;
1543 }
1544
1545 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1546 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1547
1548 // We need to determine the smallest width that will occur under normal
1549 // operation. To this, start with the base screen size and compute the
1550 // width under the different possible rotations. We need to un-rotate
1551 // the current screen dimensions before doing this.
1552 int unrotDw, unrotDh;
1553 if (rotated) {
1554 unrotDw = dh;
1555 unrotDh = dw;
1556 } else {
1557 unrotDw = dw;
1558 unrotDh = dh;
1559 }
1560 displayInfo.smallestNominalAppWidth = 1<<30;
1561 displayInfo.smallestNominalAppHeight = 1<<30;
1562 displayInfo.largestNominalAppWidth = 0;
1563 displayInfo.largestNominalAppHeight = 0;
1564 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1565 unrotDh);
1566 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1567 unrotDw);
1568 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1569 unrotDh);
1570 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1571 unrotDw);
1572 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1573 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1574 displayId);
1575 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1576 displayId);
1577 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1578 displayId);
1579 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1580 displayId);
1581 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1582 outConfig.screenLayout = sl;
1583 }
1584
1585 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1586 int uiMode, int displayId) {
1587 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001588 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
1589 mDisplayInfo.displayCutout);
1590 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
1591 mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001592
1593 // Compute the screen layout size class for this rotation.
1594 int longSize = w;
1595 int shortSize = h;
1596 if (longSize < shortSize) {
1597 int tmp = longSize;
1598 longSize = shortSize;
1599 shortSize = tmp;
1600 }
1601 longSize = (int)(longSize/density);
1602 shortSize = (int)(shortSize/density);
1603 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1604 }
1605
1606 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1607 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001608 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1609 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001610 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001611 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001612 if (width < displayInfo.smallestNominalAppWidth) {
1613 displayInfo.smallestNominalAppWidth = width;
1614 }
1615 if (width > displayInfo.largestNominalAppWidth) {
1616 displayInfo.largestNominalAppWidth = width;
1617 }
1618 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001619 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001620 if (height < displayInfo.smallestNominalAppHeight) {
1621 displayInfo.smallestNominalAppHeight = height;
1622 }
1623 if (height > displayInfo.largestNominalAppHeight) {
1624 displayInfo.largestNominalAppHeight = height;
1625 }
1626 }
1627
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001628 /**
1629 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1630 * theme attribute) on devices that feature a physical options menu key attempt to position
1631 * their menu panel window along the edge of the screen nearest the physical menu key.
1632 * This lowers the travel distance between invoking the menu panel and selecting
1633 * a menu option.
1634 *
1635 * This method helps control where that menu is placed. Its current implementation makes
1636 * assumptions about the menu key and its relationship to the screen based on whether
1637 * the device's natural orientation is portrait (width < height) or landscape.
1638 *
1639 * The menu key is assumed to be located along the bottom edge of natural-portrait
1640 * devices and along the right edge of natural-landscape devices. If these assumptions
1641 * do not hold for the target device, this method should be changed to reflect that.
1642 *
1643 * @return A {@link Gravity} value for placing the options menu window.
1644 */
1645 int getPreferredOptionsPanelGravity() {
1646 final int rotation = getRotation();
1647 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1648 // On devices with a natural orientation of portrait.
1649 switch (rotation) {
1650 default:
1651 case Surface.ROTATION_0:
1652 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1653 case Surface.ROTATION_90:
1654 return Gravity.RIGHT | Gravity.BOTTOM;
1655 case Surface.ROTATION_180:
1656 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1657 case Surface.ROTATION_270:
1658 return Gravity.START | Gravity.BOTTOM;
1659 }
1660 }
1661
1662 // On devices with a natural orientation of landscape.
1663 switch (rotation) {
1664 default:
1665 case Surface.ROTATION_0:
1666 return Gravity.RIGHT | Gravity.BOTTOM;
1667 case Surface.ROTATION_90:
1668 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1669 case Surface.ROTATION_180:
1670 return Gravity.START | Gravity.BOTTOM;
1671 case Surface.ROTATION_270:
1672 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1673 }
1674 }
1675
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001676 DockedStackDividerController getDockedDividerController() {
1677 return mDividerControllerLocked;
1678 }
1679
Winson Chung655332c2016-10-31 13:14:28 -07001680 PinnedStackController getPinnedStackController() {
1681 return mPinnedStackControllerLocked;
1682 }
1683
Jeff Browna506a6e2013-06-04 00:02:38 -07001684 /**
1685 * Returns true if the specified UID has access to this display.
1686 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001687 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001688 return mDisplay.hasAccess(uid);
1689 }
1690
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001691 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001692 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001693 }
1694
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001695 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001696 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001697 }
1698
Wale Ogunwale61911492017-10-11 08:50:50 -07001699 /**
1700 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1701 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001702 TaskStack getSplitScreenPrimaryStack() {
1703 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001704 return (stack != null && stack.isVisible()) ? stack : null;
1705 }
1706
Robert Carr9785cf32018-04-25 15:06:07 -07001707 boolean hasSplitScreenPrimaryStack() {
1708 return getSplitScreenPrimaryStack() != null;
1709 }
1710
Wale Ogunwale61911492017-10-11 08:50:50 -07001711 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001712 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001713 * not visible.
1714 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001715 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1716 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001717 }
1718
1719 TaskStack getPinnedStack() {
1720 return mTaskStackContainers.getPinnedStack();
1721 }
1722
1723 private boolean hasPinnedStack() {
1724 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001725 }
1726
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001727 /**
1728 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1729 * Null is no compatible stack on the display.
1730 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001731 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001732 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1733 }
1734
1735 /**
1736 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1737 * activity type. Null is no compatible stack on the display.
1738 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001739 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001740 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001741 }
1742
Bryce Lee48f4b572017-04-10 10:54:15 -07001743 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001744 WindowList<TaskStack> getStacks() {
1745 return mTaskStackContainers.mChildren;
1746 }
1747
1748 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001749 TaskStack getTopStack() {
1750 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001751 }
1752
Winson Chunge2d72172018-01-25 17:46:20 +00001753 ArrayList<Task> getVisibleTasks() {
1754 return mTaskStackContainers.getVisibleTasks();
1755 }
1756
Wale Ogunwale61911492017-10-11 08:50:50 -07001757 void onStackWindowingModeChanged(TaskStack stack) {
1758 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001759 }
1760
Andrii Kulian441e4492016-09-29 15:25:00 -07001761 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001762 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001763 super.onConfigurationChanged(newParentConfig);
1764
Andrii Kulian3a507b52016-09-19 18:14:12 -07001765 // The display size information is heavily dependent on the resources in the current
1766 // configuration, so we need to reconfigure it every time the configuration changes.
1767 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1768 mService.reconfigureDisplayLocked(this);
1769
Bryce Leef3c6a472017-11-14 14:53:06 -08001770 final DockedStackDividerController dividerController = getDockedDividerController();
1771
1772 if (dividerController != null) {
1773 getDockedDividerController().onConfigurationChanged();
1774 }
1775
1776 final PinnedStackController pinnedStackController = getPinnedStackController();
1777
1778 if (pinnedStackController != null) {
1779 getPinnedStackController().onConfigurationChanged();
1780 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001781 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001782
Andrii Kulian441e4492016-09-29 15:25:00 -07001783 /**
1784 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1785 * bounds were updated.
1786 */
Bryce Leeaea60d22018-01-31 14:42:15 -08001787 void updateStackBoundsAfterConfigChange(@NonNull List<TaskStack> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001788 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1789 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001790 if (stack.updateBoundsAfterConfigChange()) {
Bryce Leeaea60d22018-01-31 14:42:15 -08001791 changedStackList.add(stack);
Andrii Kulian3a507b52016-09-19 18:14:12 -07001792 }
1793 }
Winson Chung32c566f2017-04-11 18:31:21 -07001794
1795 // If there was no pinned stack, we still need to notify the controller of the display info
1796 // update as a result of the config change. We do this here to consolidate the flow between
1797 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001798 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001799 mPinnedStackControllerLocked.onDisplayInfoChanged();
1800 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001801 }
1802
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001803 @Override
1804 boolean fillsParent() {
1805 return true;
1806 }
1807
1808 @Override
1809 boolean isVisible() {
1810 return true;
1811 }
1812
1813 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001814 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001815 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001816 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001817 }
1818
Robert Carr9785cf32018-04-25 15:06:07 -07001819 /**
1820 * In split-screen mode we process the IME containers above the docked divider
1821 * rather than directly above their target.
1822 */
1823 private boolean skipTraverseChild(WindowContainer child) {
1824 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null
1825 && !hasSplitScreenPrimaryStack()) {
1826 return true;
1827 }
1828 return false;
1829 }
1830
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001831 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001832 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1833 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1834 // target, or here in order if there isn't an IME target.
1835 if (traverseTopToBottom) {
1836 for (int i = mChildren.size() - 1; i >= 0; --i) {
1837 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001838 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001839 continue;
1840 }
Robert Carr9785cf32018-04-25 15:06:07 -07001841
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001842 if (child.forAllWindows(callback, traverseTopToBottom)) {
1843 return true;
1844 }
1845 }
1846 } else {
1847 final int count = mChildren.size();
1848 for (int i = 0; i < count; i++) {
1849 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001850 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001851 continue;
1852 }
Robert Carr9785cf32018-04-25 15:06:07 -07001853
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001854 if (child.forAllWindows(callback, traverseTopToBottom)) {
1855 return true;
1856 }
1857 }
1858 }
1859 return false;
1860 }
1861
1862 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1863 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1864 }
1865
Wale Ogunwale399c8692017-05-08 14:22:42 -07001866 @Override
1867 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001868 final WindowManagerPolicy policy = mService.mPolicy;
1869
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001870 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001871 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001872 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1873 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001874 // If the display is frozen, some activities may be in the middle of restarting, and
1875 // thus have removed their old window. If the window has the flag to hide the lock
1876 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1877 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001878 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001879 } else if (policy.isKeyguardLocked()) {
1880 // Use the last orientation the while the display is frozen with the keyguard
1881 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1882 // window. We don't want to check the show when locked window directly though as
1883 // things aren't stable while the display is frozen, for example the window could be
1884 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001885 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1886 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001887 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001888 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001889 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001890 final int orientation = mAboveAppWindowsContainers.getOrientation();
1891 if (orientation != SCREEN_ORIENTATION_UNSET) {
1892 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001893 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001894 }
1895
Wale Ogunwale399c8692017-05-08 14:22:42 -07001896 // Top system windows are not requesting an orientation. Start searching from apps.
1897 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001898 }
1899
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001900 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001901 // Check if display metrics changed and update base values if needed.
1902 updateBaseDisplayMetricsIfNeeded();
1903
Craig Mautner722285e2012-09-07 13:55:58 -07001904 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001905 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001906
Wale Ogunwale61911492017-10-11 08:50:50 -07001907 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1908 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001909 }
Craig Mautner722285e2012-09-07 13:55:58 -07001910 }
1911
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001912 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001913 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1914 if (displayManagerInternal != null) {
1915 // Bootstrap the default logical display from the display manager.
1916 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1917 if (newDisplayInfo != null) {
1918 mDisplayInfo.copyFrom(newDisplayInfo);
1919 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001920 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001921
Andrii Kuliancd097992017-03-23 18:31:59 -07001922 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1923 mDisplayInfo.logicalDensityDpi);
1924 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1925 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1926 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001927 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001928 }
1929
Andrii Kuliancd097992017-03-23 18:31:59 -07001930 /**
1931 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1932 * values.
1933 */
1934 private void updateBaseDisplayMetricsIfNeeded() {
1935 // Get real display metrics without overrides from WM.
1936 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1937 final int orientation = mDisplayInfo.rotation;
1938 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1939 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1940 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1941 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001942 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001943
1944 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1945 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001946 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1947 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001948
1949 if (displayMetricsChanged) {
1950 // Check if display size or density is forced.
1951 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1952 || mBaseDisplayHeight != mInitialDisplayHeight;
1953 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1954
1955 // If there is an override set for base values - use it, otherwise use new values.
1956 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1957 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1958 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1959
1960 // Real display metrics changed, so we should also update initial values.
1961 mInitialDisplayWidth = newWidth;
1962 mInitialDisplayHeight = newHeight;
1963 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001964 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001965 mService.reconfigureDisplayLocked(this);
1966 }
1967 }
1968
Bryce Lee27cec322017-03-21 09:41:37 -07001969 /** Sets the maximum width the screen resolution can be */
1970 void setMaxUiWidth(int width) {
1971 if (DEBUG_DISPLAY) {
1972 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1973 }
1974
1975 mMaxUiWidth = width;
1976
1977 // Update existing metrics.
1978 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1979 }
1980
1981 /** Update base (override) display metrics. */
1982 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1983 mBaseDisplayWidth = baseWidth;
1984 mBaseDisplayHeight = baseHeight;
1985 mBaseDisplayDensity = baseDensity;
1986
1987 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1988 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1989 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1990 mBaseDisplayWidth = mMaxUiWidth;
1991
1992 if (DEBUG_DISPLAY) {
1993 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1994 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1995 + " on display:" + getDisplayId());
1996 }
1997 }
1998
1999 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002000
2001 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002002 }
2003
Wale Ogunwaledb506192017-12-08 10:57:32 -08002004 void getStableRect(Rect out) {
2005 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002006 }
2007
Wale Ogunwale61911492017-10-11 08:50:50 -07002008 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002009 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2010 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002011
Wale Ogunwale61911492017-10-11 08:50:50 -07002012 final TaskStack stack = new TaskStack(mService, stackId, controller);
2013 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002014 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002015 }
2016
Andrii Kulian51c1b672017-04-07 18:39:32 -07002017 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002018 final DisplayContent prevDc = stack.getDisplayContent();
2019 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002020 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2021 + " which is not currently attached to any display");
2022 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002023 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002024 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2025 + " to its current displayId=" + mDisplayId);
2026 }
2027
Wale Ogunwale61911492017-10-11 08:50:50 -07002028 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002029 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002030 }
2031
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002032 @Override
2033 protected void addChild(DisplayChildWindowContainer child,
2034 Comparator<DisplayChildWindowContainer> comparator) {
2035 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2036 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002037
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002038 @Override
2039 protected void addChild(DisplayChildWindowContainer child, int index) {
2040 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2041 }
2042
2043 @Override
2044 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002045 // Only allow removal of direct children from this display if the display is in the process
2046 // of been removed.
2047 if (mRemovingDisplay) {
2048 super.removeChild(child);
2049 return;
2050 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002051 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002052 }
2053
Andrii Kuliand2765632016-12-12 22:26:34 -08002054 @Override
2055 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2056 // Children of the display are statically ordered, so the real intention here is to perform
2057 // the operation on the display and not the static direct children.
2058 getParent().positionChildAt(position, this, includingParents);
2059 }
2060
Riddle Hsu57831b52018-07-27 00:31:48 +08002061 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2062 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002063 layoutAndAssignWindowLayersIfNeeded();
2064 }
2065
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002066 /**
2067 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2068 * current task in focus.
2069 *
2070 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2071 * returns -1.
2072 *
2073 * @param x horizontal coordinate of the tap position
2074 * @param y vertical coordinate of the tap position
2075 * @return the task ID if a non-home task is found; -1 if not
2076 */
2077 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002078 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2079 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002080 if (stack.isActivityTypeHome()) {
2081 // We skip not only home stack, but also everything behind home because user can't
2082 // see them.
2083 break;
2084 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002085 final int taskId = stack.taskIdFromPoint(x, y);
2086 if (taskId != -1) {
2087 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002088 }
2089 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002090 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002091 }
2092
Chong Zhang8e89b312015-09-09 15:09:30 -07002093 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002094 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002095 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002096 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002097 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002098 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002099 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002100 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2101 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002102 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002103 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002104 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002105
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002106 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2107 if (mTmpTaskForResizePointSearchResult.searchDone) {
2108 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002109 }
2110 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002111 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002112 }
2113
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002114 void updateTouchExcludeRegion() {
2115 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002116 if (focusedTask == null) {
2117 mTouchExcludeRegion.setEmpty();
2118 } else {
2119 mTouchExcludeRegion.set(mBaseDisplayRect);
2120 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2121 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002122 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2123 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002124 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002125 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2126 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002127 }
2128 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002129 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002130 // any touch inside the focused task itself.
2131 if (!mTmpRect2.isEmpty()) {
2132 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2133 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002134 }
lumark90120a82018-08-15 00:33:03 +08002135 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002136 // If the input method is visible and the user is typing, we don't want these touch
2137 // events to be intercepted and used to change focus. This would likely cause a
2138 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002139 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002140 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002141 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002142 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002143 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002144 win.getTouchableRegion(mTmpRegion);
2145 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2146 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002147 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2148 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2149 win.amendTapExcludeRegion(mTouchExcludeRegion);
2150 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002151 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002152 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002153 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002154 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002155 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2156 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002157 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002158 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002159 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002160 }
2161
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002162 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002163 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002164 super.switchUser();
2165 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002166 }
2167
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002168 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002169 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2170 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002171 }
2172 }
2173
Wale Ogunwale10124582016-09-15 20:25:50 -07002174 @Override
2175 void removeIfPossible() {
2176 if (isAnimating()) {
2177 mDeferredRemoval = true;
2178 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002179 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002180 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002181 }
2182
Wale Ogunwale10124582016-09-15 20:25:50 -07002183 @Override
2184 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002185 mRemovingDisplay = true;
2186 try {
2187 super.removeImmediately();
2188 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07002189 if (mService.canDispatchPointerEvents()) {
2190 if (mTapDetector != null) {
2191 mService.unregisterPointerEventListener(mTapDetector);
2192 }
2193 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
2194 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
2195 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002196 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002197 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002198 mWindowingLayer.release();
2199 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002200 } finally {
2201 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002202 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002203
Arthur Hung39134b22018-08-14 11:58:28 +08002204 mInputMonitor.onRemoved();
Bryce Leef19cbe22018-02-02 15:09:21 -08002205 mService.onDisplayRemoved(mDisplayId);
Craig Mautner95da1082014-02-24 17:54:35 -08002206 }
2207
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002208 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002209 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002210 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002211 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2212
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002213 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002214 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002215 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002216 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002217 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002218 }
2219
Andrii Kulian0214ed92017-05-16 13:44:05 -07002220 /** @return 'true' if removal of this display content is deferred due to active animation. */
2221 boolean isRemovalDeferred() {
2222 return mDeferredRemoval;
2223 }
2224
Wale Ogunwale10124582016-09-15 20:25:50 -07002225 boolean animateForIme(float interpolatedValue, float animationTarget,
2226 float dividerAnimationTarget) {
2227 boolean updated = false;
2228
Wale Ogunwale61911492017-10-11 08:50:50 -07002229 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2230 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002231 if (stack == null || !stack.isAdjustedForIme()) {
2232 continue;
2233 }
2234
2235 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2236 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2237 updated = true;
2238 } else {
2239 mDividerControllerLocked.mLastAnimationProgress =
2240 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2241 mDividerControllerLocked.mLastDividerProgress =
2242 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2243 updated |= stack.updateAdjustForIme(
2244 mDividerControllerLocked.mLastAnimationProgress,
2245 mDividerControllerLocked.mLastDividerProgress,
2246 false /* force */);
2247 }
2248 if (interpolatedValue >= 1f) {
2249 stack.endImeAdjustAnimation();
2250 }
2251 }
2252
2253 return updated;
2254 }
2255
2256 boolean clearImeAdjustAnimation() {
2257 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002258 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2259 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002260 if (stack != null && stack.isAdjustedForIme()) {
2261 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2262 changed = true;
2263 }
2264 }
2265 return changed;
2266 }
2267
2268 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002269 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2270 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002271 if (stack.isVisible() && stack.isAdjustedForIme()) {
2272 stack.beginImeAdjustAnimation();
2273 }
2274 }
2275 }
2276
2277 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002278 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002279 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2280 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002281 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002282 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2283 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2284 imeTargetStack.getDockSide() : DOCKED_INVALID;
2285 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2286 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2287 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002288 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002289 final boolean imeHeightChanged = imeVisible &&
2290 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2291
2292 // The divider could be adjusted for IME position, or be thinner than usual,
2293 // or both. There are three possible cases:
2294 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2295 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2296 // - If IME is not visible, divider is not moved and is normal width.
2297
2298 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002299 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2300 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002301 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002302 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2303 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002304 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2305 } else {
2306 stack.resetAdjustedForIme(false);
2307 }
2308 }
2309 mDividerControllerLocked.setAdjustedForIme(
2310 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2311 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002312 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2313 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002314 stack.resetAdjustedForIme(!dockVisible);
2315 }
2316 mDividerControllerLocked.setAdjustedForIme(
2317 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2318 }
Winson Chung655332c2016-10-31 13:14:28 -07002319 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002320 }
2321
2322 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002323 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2324 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002325 stack.prepareFreezingTaskBounds();
2326 }
2327 }
2328
Wale Ogunwale94744212015-09-21 19:01:47 -07002329 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002330 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002331
2332 // Compute a transform matrix to undo the coordinate space transformation,
2333 // and present the window at the same physical position it previously occupied.
2334 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2335 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2336
2337 mTmpRectF.set(bounds);
2338 mTmpMatrix.mapRect(mTmpRectF);
2339 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002340 }
2341
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002342 static int deltaRotation(int oldRotation, int newRotation) {
2343 int delta = newRotation - oldRotation;
2344 if (delta < 0) delta += 4;
2345 return delta;
2346 }
2347
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002348 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002349 Matrix outMatrix) {
2350 // For rotations without Z-ordering we don't need the target rectangle's position.
2351 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2352 displayHeight, outMatrix);
2353 }
2354
2355 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2356 float displayWidth, float displayHeight, Matrix outMatrix) {
2357 switch (rotation) {
2358 case ROTATION_0:
2359 outMatrix.reset();
2360 break;
2361 case ROTATION_270:
2362 outMatrix.setRotate(270, 0, 0);
2363 outMatrix.postTranslate(0, displayHeight);
2364 outMatrix.postTranslate(rectTop, 0);
2365 break;
2366 case ROTATION_180:
2367 outMatrix.reset();
2368 break;
2369 case ROTATION_90:
2370 outMatrix.setRotate(90, 0, 0);
2371 outMatrix.postTranslate(displayWidth, 0);
2372 outMatrix.postTranslate(-rectTop, rectLeft);
2373 break;
2374 }
2375 }
2376
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002377 @CallSuper
2378 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002379 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002380 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002381 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002382 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002383 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2384 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002385 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002386 }
2387 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2388 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002389 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2390 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002391 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002392 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002393 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2394 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002395 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002396 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002397 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2398 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002399 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002400 }
2401 proto.write(DPI, mBaseDisplayDensity);
2402 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002403 proto.write(ROTATION, mRotation);
2404 final ScreenRotationAnimation screenRotationAnimation =
2405 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2406 if (screenRotationAnimation != null) {
2407 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2408 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002409 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Louis Chang7501e332018-08-20 13:08:39 +08002410 proto.write(SURFACE_SIZE, mSurfaceSize);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002411 if (mFocusedApp != null) {
2412 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2413 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002414 proto.end(token);
2415 }
2416
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002417 @Override
2418 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2419 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002420 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2421 final String subPrefix = " " + prefix;
2422 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2423 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2424 pw.print("dpi");
2425 if (mInitialDisplayWidth != mBaseDisplayWidth
2426 || mInitialDisplayHeight != mBaseDisplayHeight
2427 || mInitialDisplayDensity != mBaseDisplayDensity) {
2428 pw.print(" base=");
2429 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2430 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2431 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002432 if (mDisplayScalingDisabled) {
2433 pw.println(" noscale");
2434 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002435 pw.print(" cur=");
2436 pw.print(mDisplayInfo.logicalWidth);
2437 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2438 pw.print(" app=");
2439 pw.print(mDisplayInfo.appWidth);
2440 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2441 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2442 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2443 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2444 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002445 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002446 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002447 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002448
Craig Mautnerdc548482014-02-05 13:35:24 -08002449 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002450 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002451 pw.print(prefix);
2452 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002453
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002454 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2455 if (mLastFocus != mCurrentFocus) {
2456 pw.print(" mLastFocus="); pw.println(mLastFocus);
2457 }
2458 if (mLosingFocus.size() > 0) {
2459 pw.println();
2460 pw.println(" Windows losing focus:");
2461 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2462 final WindowState w = mLosingFocus.get(i);
2463 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2464 pw.print(w);
2465 if (dumpAll) {
2466 pw.println(":");
2467 w.dump(pw, " ", true);
2468 } else {
2469 pw.println();
2470 }
2471 }
2472 }
2473 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
2474
Adrian Roos5251b1d2018-03-23 18:57:43 +01002475 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002476 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002477 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2478 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002479 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002480 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002481
Craig Mautnerdc548482014-02-05 13:35:24 -08002482 pw.println();
2483 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002484 pw.println();
2485 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002486 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002487 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002488 pw.print(" Exiting #"); pw.print(i);
2489 pw.print(' '); pw.print(token);
2490 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002491 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002492 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002493 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002494
Jorim Jaggi31f71702016-05-04 16:43:04 -07002495 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002496
2497 // Dump stack references
2498 final TaskStack homeStack = getHomeStack();
2499 if (homeStack != null) {
2500 pw.println(prefix + "homeStack=" + homeStack.getName());
2501 }
2502 final TaskStack pinnedStack = getPinnedStack();
2503 if (pinnedStack != null) {
2504 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2505 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002506 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002507 if (splitScreenPrimaryStack != null) {
2508 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2509 }
2510
2511 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002512 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002513 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002514 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002515
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002516 pw.println();
2517 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002518 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002519 mDisplayPolicy.dump(prefix, pw);
2520 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002521 mDisplayRotation.dump(prefix, pw);
2522 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002523 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002524 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002525
2526 @Override
2527 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002528 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002529 }
2530
2531 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002532 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002533 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002534
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002535 /** Returns true if the stack in the windowing mode is visible. */
2536 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002537 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002538 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002539 }
2540
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002541 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002542 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002543 final int x = (int) xf;
2544 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002545 final WindowState touchedWin = getWindow(w -> {
2546 final int flags = w.mAttrs.flags;
2547 if (!w.isVisibleLw()) {
2548 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002549 }
2550 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002551 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002552 }
2553
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002554 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002555 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002556 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002557 }
2558
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002559 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002560
2561 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002562 return mTmpRegion.contains(x, y) || touchFlags == 0;
2563 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002564
2565 return touchedWin;
2566 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002567
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002568 boolean canAddToastWindowForUid(int uid) {
2569 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002570 // Also if the app is focused adding more than one toast at
2571 // a time for better backwards compatibility.
2572 final WindowState focusedWindowForUid = getWindow(w ->
2573 w.mOwnerUid == uid && w.isFocused());
2574 if (focusedWindowForUid != null) {
2575 return true;
2576 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002577 final WindowState win = getWindow(w ->
2578 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2579 && !w.mWindowRemovalAllowed);
2580 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002581 }
2582
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002583 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002584 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2585 return;
2586 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002587
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002588 // Used to communicate the old focus to the callback method.
2589 mTmpWindow = oldFocus;
2590
2591 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002592 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002593
2594 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002595 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002596
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002597 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002598
2599 if (mTmpWindow == null) {
2600 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2601 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002602 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002603 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002604 }
2605
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002606
2607 /**
2608 * Update the focused window and make some adjustments if the focus has changed.
2609 *
2610 * @param mode Indicates the situation we are in. Possible modes are:
2611 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2612 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2613 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2614 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2615 * @param updateInputWindows Whether to sync the window information to the input module.
2616 * @return {@code true} if the focused window has changed.
2617 */
2618 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows, boolean focusFound) {
2619 final WindowState newFocus = findFocusedWindow();
2620 if (mCurrentFocus == newFocus) {
2621 return false;
2622 }
2623 mService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
2624 boolean imWindowChanged = false;
2625 // TODO (b/111080190): Multi-Session IME
2626 if (!focusFound) {
2627 final WindowState imWindow = mInputMethodWindow;
2628 if (imWindow != null) {
2629 final WindowState prevTarget = mService.mInputMethodTarget;
2630
2631 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2632 imWindowChanged = prevTarget != newTarget;
2633
2634 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2635 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2636 assignWindowLayers(false /* setLayoutNeeded */);
2637 }
2638 }
2639 }
2640
2641 if (imWindowChanged) {
2642 mService.mWindowsChanged = true;
2643 setLayoutNeeded();
2644 }
2645
2646 if (DEBUG_FOCUS_LIGHT || mService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
2647 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2648 + " Callers=" + Debug.getCallers(4));
2649 final WindowState oldFocus = mCurrentFocus;
2650 mCurrentFocus = newFocus;
2651 mLosingFocus.remove(newFocus);
2652
2653 if (newFocus != null) {
2654 mWinAddedSinceNullFocus.clear();
2655 mWinRemovedSinceNullFocus.clear();
2656
2657 if (newFocus.canReceiveKeys()) {
2658 // Displaying a window implicitly causes dispatching to be unpaused.
2659 // This is to protect against bugs if someone pauses dispatching but
2660 // forgets to resume.
2661 newFocus.mToken.paused = false;
2662 }
2663 }
2664
2665 // System UI is only shown on the default display.
2666 int focusChanged = isDefaultDisplay
2667 ? mService.mPolicy.focusChangedLw(oldFocus, newFocus) : 0;
2668
2669 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2670 // Focus of the input method window changed. Perform layout if needed.
2671 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2672 performLayout(true /*initial*/, updateInputWindows);
2673 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2674 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2675 // Client will do the layout, but we need to assign layers
2676 // for handleNewWindowLocked() below.
2677 assignWindowLayers(false /* setLayoutNeeded */);
2678 }
2679 }
2680
2681 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2682 // The change in focus caused us to need to do a layout. Okay.
2683 setLayoutNeeded();
2684 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2685 performLayout(true /*initial*/, updateInputWindows);
2686 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
2687 mService.mRoot.performSurfacePlacement(false);
2688 }
2689 }
2690
2691 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2692 // If we defer assigning layers, then the caller is responsible for doing this part.
2693 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2694 }
2695
2696 adjustForImeIfNeeded();
2697
2698 // We may need to schedule some toast windows to be removed. The toasts for an app that
2699 // does not have input focus are removed within a timeout to prevent apps to redress
2700 // other apps' UI.
2701 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2702
2703 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2704 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2705 }
2706 return true;
2707 }
2708
2709 /**
2710 * Set the new focused app to this display.
2711 *
2712 * @param newFocus the new focused AppWindowToken.
2713 * @return true if the focused app is changed.
2714 */
2715 boolean setFocusedApp(AppWindowToken newFocus) {
2716 if (newFocus != null) {
2717 final DisplayContent appDisplay = newFocus.getDisplayContent();
2718 if (appDisplay != this) {
2719 throw new IllegalStateException(newFocus + " is not on " + getName()
2720 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2721 }
2722 }
2723 if (mFocusedApp == newFocus) {
2724 return false;
2725 }
2726 mFocusedApp = newFocus;
2727 getInputMonitor().setFocusedAppLw(newFocus);
2728 updateTouchExcludeRegion();
2729 return true;
2730 }
2731
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002732 /** Updates the layer assignment of windows on this display. */
2733 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002734 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002735 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002736 if (setLayoutNeeded) {
2737 setLayoutNeeded();
2738 }
Robert Carrb1579c82017-09-05 14:54:47 -07002739
Robert Carrf59b8dd2017-10-02 18:58:36 -07002740 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002741 // the application of this transaction until the animation pass triggers
2742 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2743 // the hiding and showing of surfaces.
2744 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002745 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002746 }
2747
Wale Ogunwale1666e312016-12-16 11:27:18 -08002748 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2749 // moving containers or resizing them. Need to investigate the best way to have it automatically
2750 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002751 void layoutAndAssignWindowLayersIfNeeded() {
2752 mService.mWindowsChanged = true;
2753 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002754
2755 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2756 false /*updateInputWindows*/)) {
2757 assignWindowLayers(false /* setLayoutNeeded */);
2758 }
2759
Arthur Hung95b38a92018-07-20 18:56:12 +08002760 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002761 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002762 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002763 }
2764
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002765 /** Returns true if a leaked surface was destroyed */
2766 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002767 // Used to indicate that a surface was leaked.
2768 mTmpWindow = null;
2769 forAllWindows(w -> {
2770 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002771 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002772 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002773 }
2774 if (!mService.mSessions.contains(wsa.mSession)) {
2775 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002776 + w + " surface=" + wsa.mSurfaceController
2777 + " token=" + w.mToken
2778 + " pid=" + w.mSession.mPid
2779 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002780 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002781 mService.mForceRemoves.add(w);
2782 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002783 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002784 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002785 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002786 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002787 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002788 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002789 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002790 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002791 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002792
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002793 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002794 }
2795
2796 /**
lumark90120a82018-08-15 00:33:03 +08002797 * Set input method window for the display.
2798 * @param win Set when window added or Null when destroyed.
2799 */
2800 void setInputMethodWindowLocked(WindowState win) {
2801 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08002802 // Update display configuration for IME process.
2803 if (mInputMethodWindow != null) {
2804 final int imePid = mInputMethodWindow.mSession.mPid;
2805 mService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
2806 mInputMethodWindow.getDisplayId());
2807 }
lumark90120a82018-08-15 00:33:03 +08002808 computeImeTarget(true /* updateImeTarget */);
2809 }
2810
2811 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002812 * Determine and return the window that should be the IME target.
2813 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2814 * @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 +00002815 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002816 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08002817 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002818 // There isn't an IME so there shouldn't be a target...That was easy!
2819 if (updateImeTarget) {
2820 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2821 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002822 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002823 }
2824 return null;
2825 }
2826
Chavi Weingarten3a748552018-05-14 17:32:42 +00002827 final WindowState curTarget = mService.mInputMethodTarget;
2828 if (!canUpdateImeTarget()) {
2829 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
2830 return curTarget;
2831 }
2832
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002833 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2834 // same display. Or even when the current IME/target are not on the same screen as the next
2835 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002836 mUpdateImeTarget = updateImeTarget;
2837 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002838
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002839
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002840 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2841 // to be on top of it, but it is not -really- where input will go. So look down below
2842 // for a real window to target...
2843 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2844 final AppWindowToken token = target.mAppToken;
2845 if (token != null) {
2846 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2847 if (betterTarget != null) {
2848 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002849 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002850 }
2851 }
2852
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002853 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2854 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002855
chaviw1c13ad32018-06-12 16:44:23 -07002856 // Now, a special case -- if the last target's window is in the process of exiting, but
2857 // not removed, and the new target is home, keep on the last target to avoid flicker.
2858 // Home is a special case since its above other stacks in the ordering list, but layed
2859 // out below the others.
2860 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
2861 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07002862 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00002863 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002864 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002865 }
2866
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002867 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2868 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002869
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002870 if (target == null) {
2871 if (updateImeTarget) {
2872 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2873 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2874 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002875 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002876 }
2877
2878 return null;
2879 }
2880
2881 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002882 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2883 if (token != null) {
2884
2885 // Now some fun for dealing with window animations that modify the Z order. We need
2886 // to look at all windows below the current target that are in this app, finding the
2887 // highest visible one in layering.
2888 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002889 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002890 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002891 }
2892
2893 if (highestTarget != null) {
2894 final AppTransition appTransition = mService.mAppTransition;
2895 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02002896 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002897
2898 if (appTransition.isTransitionSet()) {
2899 // If we are currently setting up for an animation, hold everything until we
2900 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002901 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002902 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002903 }
2904 }
2905 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002906
2907 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2908 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002909 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002910 }
2911
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002912 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002913 }
2914
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002915 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002916 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002917 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002918 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002919 }
2920
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002921 mService.mInputMethodTarget = target;
2922 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002923 assignWindowLayers(false /* setLayoutNeeded */);
2924 }
2925
2926 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2927 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2928 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2929 }
2930
2931 // Used to indicate we have reached the first window in the range we are interested in.
2932 mTmpWindow = null;
2933
2934 // TODO: Figure-out a more efficient way to do this.
2935 final WindowState candidate = getWindow(w -> {
2936 if (w == top) {
2937 // Reached the first window in the range we are interested in.
2938 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002939 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002940 if (mTmpWindow == null) {
2941 return false;
2942 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002943
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002944 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2945 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002946 }
2947 // If we reached the bottom of the range of windows we are considering,
2948 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002949 if (w == bottom) {
2950 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002951 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002952 return false;
2953 });
2954
2955 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002956 }
2957
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002958 void setLayoutNeeded() {
2959 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2960 mLayoutNeeded = true;
2961 }
2962
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002963 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002964 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2965 mLayoutNeeded = false;
2966 }
2967
2968 boolean isLayoutNeeded() {
2969 return mLayoutNeeded;
2970 }
2971
Wale Ogunwale02319a62016-09-26 15:21:22 -07002972 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2973 if (mTokenMap.isEmpty()) {
2974 return;
2975 }
2976 pw.println(" Display #" + mDisplayId);
2977 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2978 while (it.hasNext()) {
2979 final WindowToken token = it.next();
2980 pw.print(" ");
2981 pw.print(token);
2982 if (dumpAll) {
2983 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002984 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002985 } else {
2986 pw.println();
2987 }
2988 }
2989 }
2990
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002991 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002992 final int[] index = new int[1];
2993 forAllWindows(w -> {
2994 final WindowStateAnimator wAnim = w.mWinAnimator;
2995 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2996 index[0] = index[0] + 1;
2997 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002998 }
2999
Jorim Jaggife762342016-10-13 14:33:27 +02003000 /**
3001 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3002 */
3003 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
3004 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003005 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003006 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3007 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003008 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003009 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003010 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003011 }
3012
Wale Ogunwale494009b82016-10-21 09:01:38 -07003013 boolean checkWaitingForWindows() {
3014
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003015 mHaveBootMsg = false;
3016 mHaveApp = false;
3017 mHaveWallpaper = false;
3018 mHaveKeyguard = true;
3019
3020 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003021 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3022 return true;
3023 }
3024 if (w.isDrawnLw()) {
3025 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003026 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003027 } else if (w.mAttrs.type == TYPE_APPLICATION
3028 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003029 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003030 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003031 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003032 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003033 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003034 }
3035 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003036 return false;
3037 });
3038
3039 if (visibleWindow != null) {
3040 // We have a visible window.
3041 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003042 }
3043
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003044 // if the wallpaper service is disabled on the device, we're never going to have
3045 // wallpaper, don't bother waiting for it
3046 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
3047 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09003048 && mService.mContext.getResources().getBoolean(
3049 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003050 && !mService.mOnlyCore;
3051
Wale Ogunwale494009b82016-10-21 09:01:38 -07003052 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
3053 "******** booted=" + mService.mSystemBooted
3054 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003055 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3056 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3057 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003058
3059 // If we are turning on the screen to show the boot message, don't do it until the boot
3060 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003061 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003062 return true;
3063 }
3064
3065 // If we are turning on the screen after the boot is completed normally, don't do so until
3066 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003067 if (mService.mSystemBooted
3068 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003069 return true;
3070 }
3071
3072 return false;
3073 }
3074
Jorim Jaggi8f520872018-08-14 17:00:20 +02003075 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003076 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003077 }
3078
Jorim Jaggi8f520872018-08-14 17:00:20 +02003079 /**
3080 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3081 */
3082 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003083 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003084 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003085 }
3086
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003087 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003088 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003089 if (imFocus == null) {
3090 return false;
3091 }
3092
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003093 if (DEBUG_INPUT_METHOD) {
3094 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003095 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3096 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003097 }
3098
Wale Ogunwale494009b82016-10-21 09:01:38 -07003099 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003100 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3101 + "/" + imFocus.mSession.mPid);
3102 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003103 }
3104
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003105 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003106 }
3107
3108 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003109 final WindowState win = getWindow(
3110 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3111 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003112 }
3113
3114 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003115 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003116 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003117 final int curValue = w.mSystemUiVisibility;
3118 final int diff = (curValue ^ visibility) & globalDiff;
3119 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003120 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003121 w.mSeq++;
3122 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003123 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003124 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3125 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003126 visibility, newValue, diff);
3127 }
3128 } catch (RemoteException e) {
3129 // so sorry
3130 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003131 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003132 }
3133
3134 void onWindowFreezeTimeout() {
3135 Slog.w(TAG_WM, "Window freeze timeout expired.");
3136 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003137
3138 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003139 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003140 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003141 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003142 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003143 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3144 - mService.mDisplayFreezeTime);
3145 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003146 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003147 mService.mWindowPlacerLocked.performSurfacePlacement();
3148 }
3149
3150 void waitForAllWindowsDrawn() {
3151 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003152 forAllWindows(w -> {
3153 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3154 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3155 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003156 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003157 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003158 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003159 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003160 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003161 }
3162
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003163 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003164 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003165
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003166 final int dw = mDisplayInfo.logicalWidth;
3167 final int dh = mDisplayInfo.logicalHeight;
3168 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3169
3170 mTmpUpdateAllDrawn.clear();
3171
3172 int repeats = 0;
3173 do {
3174 repeats++;
3175 if (repeats > 6) {
3176 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3177 clearLayoutNeeded();
3178 break;
3179 }
3180
3181 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3182 pendingLayoutChanges);
3183
Andrii Kulian839def92016-11-02 10:58:58 -07003184 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
3185 // the wallpaper window jumping across displays.
3186 // Remove check for default display when there will be support for multiple wallpaper
3187 // targets (on different displays).
3188 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003189 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003190 }
3191
Riddle Hsu654a6f92018-07-13 22:59:36 +08003192 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003193 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Robert Carrae606b42018-02-15 15:36:23 -08003194 if (mService.updateOrientationFromAppTokensLocked(mDisplayId)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003195 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07003196 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003197 }
3198 }
3199
3200 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3201 setLayoutNeeded();
3202 }
3203
3204 // FIRST LOOP: Perform a layout, if needed.
3205 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3206 performLayout(repeats == 1, false /* updateInputWindows */);
3207 } else {
3208 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3209 }
3210
3211 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3212 pendingLayoutChanges = 0;
3213
3214 if (isDefaultDisplay) {
3215 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003216 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003217 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
3218 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3219 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
3220 }
3221 } while (pendingLayoutChanges != 0);
3222
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003223 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003224
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003225 mTmpRecoveringMemory = recoveringMemory;
3226 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003227 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003228
Jorim Jaggif1292892018-09-10 11:58:13 +02003229 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003230 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003231 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003232 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3233 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003234 true /* inTraversal, must call performTraversalInTrans... below */);
3235
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003236 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3237 if (wallpaperVisible != mLastWallpaperVisible) {
3238 mLastWallpaperVisible = wallpaperVisible;
3239 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3240 }
3241
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003242 while (!mTmpUpdateAllDrawn.isEmpty()) {
3243 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3244 // See if any windows have been drawn, so they (and others associated with them)
3245 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003246 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003247 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003248 }
3249
Bryce Leef3c6a472017-11-14 14:53:06 -08003250 private void updateBounds() {
3251 calculateBounds(mTmpBounds);
3252 setBounds(mTmpBounds);
3253 }
3254
3255 // Determines the current display bounds based on the current state
3256 private void calculateBounds(Rect out) {
3257 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
3258 final int orientation = mDisplayInfo.rotation;
3259 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
3260 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3261 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
3262 int width = mDisplayInfo.logicalWidth;
3263 int left = (physWidth - width) / 2;
3264 int height = mDisplayInfo.logicalHeight;
3265 int top = (physHeight - height) / 2;
3266 out.set(left, top, left + width, top + height);
3267 }
3268
3269 @Override
3270 public void getBounds(Rect out) {
3271 calculateBounds(out);
3272 }
3273
3274 private void getBounds(Rect out, int orientation) {
3275 getBounds(out);
3276
3277 // Rotate the Rect if needed.
3278 final int currentRotation = mDisplayInfo.rotation;
3279 final int rotationDelta = deltaRotation(currentRotation, orientation);
3280 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3281 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3282 mTmpRectF.set(out);
3283 mTmpMatrix.mapRect(mTmpRectF);
3284 mTmpRectF.round(out);
3285 }
3286 }
3287
Louis Chang7501e332018-08-20 13:08:39 +08003288 int getSurfaceSize() {
3289 return mSurfaceSize;
3290 }
3291
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003292 void performLayout(boolean initial, boolean updateInputWindows) {
3293 if (!isLayoutNeeded()) {
3294 return;
3295 }
3296 clearLayoutNeeded();
3297
3298 final int dw = mDisplayInfo.logicalWidth;
3299 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003300 if (DEBUG_LAYOUT) {
3301 Slog.v(TAG, "-------------------------------------");
3302 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3303 }
3304
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003305 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3306 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003307 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3308 // display info above...
3309 mDisplayFrames.mRotation = mRotation;
3310 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003311 if (isDefaultDisplay) {
3312 // Not needed on non-default displays.
3313 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3314 mService.mScreenRect.set(0, 0, dw, dh);
3315 }
3316
Adrian Roos5251b1d2018-03-23 18:57:43 +01003317 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003318 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003319 mLayoutSeq = seq;
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 windows are
3322 // behind it.
3323 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003324 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003325
3326 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003327 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003328
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003329 // Used to indicate that we have processed the dream window and all additional attached
3330 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003331 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003332 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003333
3334 // Now perform layout of attached windows, which usually depend on the position of the
3335 // window they are attached to. XXX does not deal with windows that are attached to windows
3336 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003337 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003338
3339 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003340 mInputMonitor.layoutInputConsumers(dw, dh);
3341 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003342 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003343 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003344 }
3345
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003346 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3347 }
3348
3349 /**
3350 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3351 * In portrait mode, it grabs the full screenshot.
3352 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003353 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003354 */
chaviw0315a1a2018-03-05 15:28:35 -08003355 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3356 if (!mService.mPolicy.isScreenOn()) {
3357 if (DEBUG_SCREENSHOT) {
3358 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003359 }
chaviw0315a1a2018-03-05 15:28:35 -08003360 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003361 }
chaviwfbe47df2017-11-10 16:14:49 -08003362
chaviw0315a1a2018-03-05 15:28:35 -08003363 int dw = mDisplayInfo.logicalWidth;
3364 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003365
chaviw0315a1a2018-03-05 15:28:35 -08003366 if (dw <= 0 || dh <= 0) {
3367 return null;
3368 }
chaviwfbe47df2017-11-10 16:14:49 -08003369
chaviw0315a1a2018-03-05 15:28:35 -08003370 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003371
chaviw0315a1a2018-03-05 15:28:35 -08003372 // The screenshot API does not apply the current screen rotation.
3373 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003374
chaviw0315a1a2018-03-05 15:28:35 -08003375 if (rot == ROTATION_90 || rot == ROTATION_270) {
3376 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3377 }
chaviwfbe47df2017-11-10 16:14:49 -08003378
chaviw0315a1a2018-03-05 15:28:35 -08003379 // SurfaceFlinger is not aware of orientation, so convert our logical
3380 // crop to SurfaceFlinger's portrait orientation.
3381 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3382
3383 final ScreenRotationAnimation screenRotationAnimation =
3384 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3385 final boolean inRotation = screenRotationAnimation != null &&
3386 screenRotationAnimation.isAnimating();
3387 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3388
3389 // TODO(b/68392460): We should screenshot Task controls directly
3390 // but it's difficult at the moment as the Task doesn't have the
3391 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003392 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003393 if (bitmap == null) {
3394 Slog.w(TAG_WM, "Failed to take screenshot");
3395 return null;
3396 }
3397
3398 // Create a copy of the screenshot that is immutable and backed in ashmem.
3399 // This greatly reduces the overhead of passing the bitmap between processes.
3400 final Bitmap ret = bitmap.createAshmemBitmap(config);
3401 bitmap.recycle();
3402 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003403 }
3404
3405 // TODO: Can this use createRotationMatrix()?
3406 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3407 if (rot == Surface.ROTATION_90) {
3408 final int tmp = crop.top;
3409 crop.top = dw - crop.right;
3410 crop.right = crop.bottom;
3411 crop.bottom = dw - crop.left;
3412 crop.left = tmp;
3413 } else if (rot == Surface.ROTATION_180) {
3414 int tmp = crop.top;
3415 crop.top = dh - crop.bottom;
3416 crop.bottom = dh - tmp;
3417 tmp = crop.right;
3418 crop.right = dw - crop.left;
3419 crop.left = dw - tmp;
3420 } else if (rot == Surface.ROTATION_270) {
3421 final int tmp = crop.top;
3422 crop.top = crop.left;
3423 crop.left = dh - crop.bottom;
3424 crop.bottom = crop.right;
3425 crop.right = dh - tmp;
3426 }
3427 }
3428
3429 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003430 // Used to indicate the layout is needed.
3431 mTmpWindow = null;
3432
3433 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003434 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003435 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003436 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003437 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003438 w.setDisplayLayoutNeeded();
3439 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003440 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003441
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003442 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003443 mService.mWindowPlacerLocked.performSurfacePlacement();
3444 }
3445 }
3446
Wale Ogunwale1666e312016-12-16 11:27:18 -08003447 void setExitingTokensHasVisible(boolean hasVisible) {
3448 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3449 mExitingTokens.get(i).hasVisible = hasVisible;
3450 }
3451
3452 // Initialize state of exiting applications.
3453 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3454 }
3455
3456 void removeExistingTokensIfPossible() {
3457 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3458 final WindowToken token = mExitingTokens.get(i);
3459 if (!token.hasVisible) {
3460 mExitingTokens.remove(i);
3461 }
3462 }
3463
3464 // Time to remove any exiting applications?
3465 mTaskStackContainers.removeExistingAppTokensIfPossible();
3466 }
3467
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003468 @Override
3469 void onDescendantOverrideConfigurationChanged() {
3470 setLayoutNeeded();
3471 mService.requestTraversal();
3472 }
3473
David Stevens9440dc82017-03-16 19:00:20 -07003474 boolean okToDisplay() {
3475 if (mDisplayId == DEFAULT_DISPLAY) {
3476 return !mService.mDisplayFrozen
3477 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3478 }
3479 return mDisplayInfo.state == Display.STATE_ON;
3480 }
3481
3482 boolean okToAnimate() {
3483 return okToDisplay() &&
3484 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3485 }
3486
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003487 static final class TaskForResizePointSearchResult {
3488 boolean searchDone;
3489 Task taskForResize;
3490
3491 void reset() {
3492 searchDone = false;
3493 taskForResize = null;
3494 }
3495 }
Robert Carr3b716242016-08-16 16:02:21 -07003496
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003497 private static final class ApplySurfaceChangesTransactionState {
3498 boolean displayHasContent;
3499 boolean obscured;
3500 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003501 float preferredRefreshRate;
3502 int preferredModeId;
3503
3504 void reset() {
3505 displayHasContent = false;
3506 obscured = false;
3507 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003508 preferredRefreshRate = 0;
3509 preferredModeId = 0;
3510 }
3511 }
3512
3513 private static final class ScreenshotApplicationState {
3514 WindowState appWin;
3515 int maxLayer;
3516 int minLayer;
3517 boolean screenshotReady;
3518
3519 void reset(boolean screenshotReady) {
3520 appWin = null;
3521 maxLayer = 0;
3522 minLayer = 0;
3523 this.screenshotReady = screenshotReady;
3524 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3525 }
3526 }
3527
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003528 /**
3529 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3530 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3531 * homogeneous children type which is currently required by sub-classes of
3532 * {@link WindowContainer} class.
3533 */
3534 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3535
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003536 DisplayChildWindowContainer(WindowManagerService service) {
3537 super(service);
3538 }
3539
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003540 @Override
3541 boolean fillsParent() {
3542 return true;
3543 }
3544
3545 @Override
3546 boolean isVisible() {
3547 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003548 }
3549 }
3550
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003551 /**
3552 * Window container class that contains all containers on this display relating to Apps.
3553 * I.e Activities.
3554 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003555 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003556 /**
3557 * A control placed at the appropriate level for transitions to occur.
3558 */
chaviw23ee71c2017-12-18 11:29:41 -08003559 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003560 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003561 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003562
Robert Carrf7a7ca82017-12-06 13:17:07 -08003563 /**
3564 * Given that the split-screen divider does not have an AppWindowToken, it
3565 * will have to live inside of a "NonAppWindowContainer", in particular
3566 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3567 * it will need to be interleaved with some of our children, appearing on top of
3568 * both docked stacks but underneath any assistant stacks.
3569 *
3570 * To solve this problem we have this anchor control, which will always exist so
3571 * we can always assign it the correct value in our {@link #assignChildLayers}.
3572 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3573 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3574 * events between the two containers.
3575 */
3576 SurfaceControl mSplitScreenDividerAnchor = null;
3577
Wale Ogunwale61911492017-10-11 08:50:50 -07003578 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3579 // through the list to find them.
3580 private TaskStack mHomeStack = null;
3581 private TaskStack mPinnedStack = null;
3582 private TaskStack mSplitScreenPrimaryStack = null;
3583
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003584 TaskStackContainers(WindowManagerService service) {
3585 super(service);
3586 }
3587
Wale Ogunwale61911492017-10-11 08:50:50 -07003588 /**
3589 * Returns the topmost stack on the display that is compatible with the input windowing mode
3590 * and activity type. Null is no compatible stack on the display.
3591 */
3592 TaskStack getStack(int windowingMode, int activityType) {
3593 if (activityType == ACTIVITY_TYPE_HOME) {
3594 return mHomeStack;
3595 }
3596 if (windowingMode == WINDOWING_MODE_PINNED) {
3597 return mPinnedStack;
3598 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3599 return mSplitScreenPrimaryStack;
3600 }
3601 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3602 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003603 if (activityType == ACTIVITY_TYPE_UNDEFINED
3604 && windowingMode == stack.getWindowingMode()) {
3605 // Passing in undefined type means we want to match the topmost stack with the
3606 // windowing mode.
3607 return stack;
3608 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003609 if (stack.isCompatible(windowingMode, activityType)) {
3610 return stack;
3611 }
3612 }
3613 return null;
3614 }
3615
3616 @VisibleForTesting
3617 TaskStack getTopStack() {
3618 return mTaskStackContainers.getChildCount() > 0
3619 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3620 }
3621
3622 TaskStack getHomeStack() {
3623 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3624 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3625 }
3626 return mHomeStack;
3627 }
3628
3629 TaskStack getPinnedStack() {
3630 return mPinnedStack;
3631 }
3632
Matthew Ng64e77cf2017-10-31 14:01:31 -07003633 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003634 return mSplitScreenPrimaryStack;
3635 }
3636
Winson Chunge2d72172018-01-25 17:46:20 +00003637 ArrayList<Task> getVisibleTasks() {
3638 final ArrayList<Task> visibleTasks = new ArrayList<>();
3639 forAllTasks(task -> {
3640 if (task.isVisible()) {
3641 visibleTasks.add(task);
3642 }
3643 });
3644 return visibleTasks;
3645 }
3646
Andrii Kulian839def92016-11-02 10:58:58 -07003647 /**
3648 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003649 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003650 */
3651 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003652 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003653 addChild(stack, onTop);
3654 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003655 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003656
Wale Ogunwale61911492017-10-11 08:50:50 -07003657 void onStackWindowingModeChanged(TaskStack stack) {
3658 removeStackReferenceIfNeeded(stack);
3659 addStackReferenceIfNeeded(stack);
3660 if (stack == mPinnedStack && getTopStack() != stack) {
3661 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3662 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3663 }
3664 }
3665
3666 private void addStackReferenceIfNeeded(TaskStack stack) {
3667 if (stack.isActivityTypeHome()) {
3668 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003669 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003670 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003671
Wale Ogunwale61911492017-10-11 08:50:50 -07003672 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003673 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003674 }
3675 final int windowingMode = stack.getWindowingMode();
3676 if (windowingMode == WINDOWING_MODE_PINNED) {
3677 if (mPinnedStack != null) {
3678 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3679 + mPinnedStack + " already exist on display=" + this
3680 + " stack=" + stack);
3681 }
3682 mPinnedStack = stack;
3683 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3684 if (mSplitScreenPrimaryStack != null) {
3685 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3686 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3687 + " already exist on display=" + this + " stack=" + stack);
3688 }
3689 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003690 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003691 }
3692 }
3693
3694 private void removeStackReferenceIfNeeded(TaskStack stack) {
3695 if (stack == mHomeStack) {
3696 mHomeStack = null;
3697 } else if (stack == mPinnedStack) {
3698 mPinnedStack = null;
3699 } else if (stack == mSplitScreenPrimaryStack) {
3700 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003701 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3702 mService.setDockedStackCreateStateLocked(
3703 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3704 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003705 }
Andrii Kulian839def92016-11-02 10:58:58 -07003706 }
3707
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003708 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003709 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3710 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003711 addChild(stack, addIndex);
3712 setLayoutNeeded();
3713 }
3714
Wale Ogunwale61911492017-10-11 08:50:50 -07003715 @Override
3716 protected void removeChild(TaskStack stack) {
3717 super.removeChild(stack);
3718 removeStackReferenceIfNeeded(stack);
3719 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003720
3721 @Override
3722 boolean isOnTop() {
3723 // Considered always on top
3724 return true;
3725 }
3726
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003727 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003728 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003729 if (child.getWindowConfiguration().isAlwaysOnTop()
3730 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003731 // This stack is always-on-top, override the default behavior.
3732 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3733
3734 // Moving to its current position, as we must call super but we don't want to
3735 // perform any meaningful action.
3736 final int currentPosition = mChildren.indexOf(child);
3737 super.positionChildAt(currentPosition, child, false /* includingParents */);
3738 return;
3739 }
3740
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003741 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3742 super.positionChildAt(targetPosition, child, includingParents);
3743
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003744 if (includingParents) {
3745 // We still want to move the display of this stack container to top because even the
3746 // target position is adjusted to non-top, the intention of the condition is to have
3747 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3748 // in a non-top display which is using picture-in-picture mode).
3749 final int topChildPosition = getChildCount() - 1;
3750 if (targetPosition < topChildPosition && position >= topChildPosition) {
3751 getParent().positionChildAt(POSITION_TOP, this /* child */,
3752 true /* includingParents */);
3753 }
3754 }
3755
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003756 setLayoutNeeded();
3757 }
3758
3759 /**
3760 * When stack is added or repositioned, find a proper position for it.
3761 * This will make sure that pinned stack always stays on top.
3762 * @param requestedPosition Position requested by caller.
3763 * @param stack Stack to be added or positioned.
3764 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3765 * @return The proper position for the stack.
3766 */
3767 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003768 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003769 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003770 }
3771
Kazuki Takisef85197b2018-06-18 18:18:36 +09003772 final int topChildPosition = mChildren.size() - 1;
3773 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3774 for (int i = topChildPosition; i >= 0; --i) {
3775 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3776 belowAlwaysOnTopPosition = i;
3777 break;
3778 }
3779 }
3780
3781 // The max possible position we can insert the stack at.
3782 int maxPosition = POSITION_TOP;
3783 // The min possible position we can insert the stack at.
3784 int minPosition = POSITION_BOTTOM;
3785
3786 if (stack.isAlwaysOnTop()) {
3787 if (hasPinnedStack()) {
3788 // Always-on-top stacks go below the pinned stack.
3789 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
3790 }
3791 // Always-on-top stacks need to be above all other stacks.
3792 minPosition = belowAlwaysOnTopPosition !=
3793 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
3794 } else {
3795 // Other stacks need to be below the always-on-top stacks.
3796 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08003797 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09003798 }
3799
3800 int targetPosition = requestedPosition;
3801 targetPosition = Math.min(targetPosition, maxPosition);
3802 targetPosition = Math.max(targetPosition, minPosition);
3803
3804 int prevPosition = getStacks().indexOf(stack);
3805 // The positions we calculated above (maxPosition, minPosition) do not take into
3806 // consideration the following edge cases.
3807 // 1) We need to adjust the position depending on the value "adding".
3808 // 2) When we are moving a stack to another position, we also need to adjust the
3809 // position depending on whether the stack is moving to a higher or lower position.
3810 if ((targetPosition != requestedPosition) &&
3811 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003812 targetPosition++;
3813 }
3814
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003815 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003816 }
3817
3818 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003819 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3820 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003821 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003822 if (super.forAllWindows(callback, traverseTopToBottom)) {
3823 return true;
3824 }
3825 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3826 return true;
3827 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003828 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003829 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3830 return true;
3831 }
3832 if (super.forAllWindows(callback, traverseTopToBottom)) {
3833 return true;
3834 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003835 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003836 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003837 }
3838
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003839 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003840 boolean traverseTopToBottom) {
3841 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3842 // app tokens.
3843 // TODO: Investigate if we need to continue to do this or if we can just process them
3844 // in-order.
3845 if (traverseTopToBottom) {
3846 for (int i = mChildren.size() - 1; i >= 0; --i) {
3847 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3848 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003849 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3850 traverseTopToBottom)) {
3851 return true;
3852 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003853 }
3854 }
3855 } else {
3856 final int count = mChildren.size();
3857 for (int i = 0; i < count; ++i) {
3858 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3859 final int appTokensCount = appTokens.size();
3860 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003861 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3862 traverseTopToBottom)) {
3863 return true;
3864 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003865 }
3866 }
3867 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003868 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003869 }
3870
Wale Ogunwale1666e312016-12-16 11:27:18 -08003871 void setExitingTokensHasVisible(boolean hasVisible) {
3872 for (int i = mChildren.size() - 1; i >= 0; --i) {
3873 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3874 for (int j = appTokens.size() - 1; j >= 0; --j) {
3875 appTokens.get(j).hasVisible = hasVisible;
3876 }
3877 }
3878 }
3879
3880 void removeExistingAppTokensIfPossible() {
3881 for (int i = mChildren.size() - 1; i >= 0; --i) {
3882 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3883 for (int j = appTokens.size() - 1; j >= 0; --j) {
3884 final AppWindowToken token = appTokens.get(j);
3885 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3886 && (!token.mIsExiting || token.isEmpty())) {
3887 // Make sure there is no animation running on this token, so any windows
3888 // associated with it will be removed as soon as their animations are
3889 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003890 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003891 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3892 "performLayout: App token exiting now removed" + token);
3893 token.removeIfPossible();
3894 }
3895 }
3896 }
3897 }
3898
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003899 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003900 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003901 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3902 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003903 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07003904 // docked or freeform stack is visible...except for the home stack if the docked
3905 // stack is minimized and it actually set something and the bounds is different from
3906 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003907 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003908 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003909 && !(mDividerControllerLocked.isHomeStackResizable()
3910 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003911 final int orientation = mHomeStack.getOrientation();
3912 if (orientation != SCREEN_ORIENTATION_UNSET) {
3913 return orientation;
3914 }
3915 }
3916 return SCREEN_ORIENTATION_UNSPECIFIED;
3917 }
3918
3919 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003920 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3921 PackageManager.FEATURE_AUTOMOTIVE);
3922 if (isCar) {
3923 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3924 // allow anything but the default orientation.
3925 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003926 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
3927 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07003928 return SCREEN_ORIENTATION_UNSPECIFIED;
3929 }
3930
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003931 if (orientation != SCREEN_ORIENTATION_UNSET
3932 && orientation != SCREEN_ORIENTATION_BEHIND) {
3933 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003934 "App is requesting an orientation, return " + orientation
3935 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003936 return orientation;
3937 }
3938
3939 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003940 "No app is requesting an orientation, return " + mLastOrientation
3941 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003942 // The next app has not been requested to be visible, so we keep the current orientation
3943 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003944 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003945 }
Robert Carrb1579c82017-09-05 14:54:47 -07003946
3947 @Override
3948 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003949 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003950
Robert Carr2f8aa392018-01-31 14:46:51 -08003951 for (int i = 0; i < mChildren.size(); i++) {
3952 final TaskStack s = mChildren.get(i);
3953 s.assignChildLayers(t);
3954 }
3955 }
3956
3957 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02003958
Robert Carrf7a7ca82017-12-06 13:17:07 -08003959 final int HOME_STACK_STATE = 0;
3960 final int NORMAL_STACK_STATE = 1;
3961 final int ALWAYS_ON_TOP_STATE = 2;
3962
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003963 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003964 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02003965 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02003966 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003967
Robert Carrf7a7ca82017-12-06 13:17:07 -08003968 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3969 for (int i = 0; i < mChildren.size(); i++) {
3970 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08003971 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
3972 continue;
3973 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
3974 || s.isAlwaysOnTop())) {
3975 continue;
3976 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
3977 continue;
3978 }
3979 s.assignLayer(t, layer++);
3980 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
3981 t.setLayer(mSplitScreenDividerAnchor, layer++);
3982 }
Winson Chungd41f71d2018-03-16 15:26:07 -07003983 if ((s.isTaskAnimating() || s.isAppAnimating())
3984 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003985 // Ensure the animation layer ends up above the
3986 // highest animating stack and no higher.
3987 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003988 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003989 if (state != ALWAYS_ON_TOP_STATE) {
3990 layerForBoostedAnimationLayer = layer++;
3991 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08003992 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003993 if (state == HOME_STACK_STATE) {
3994 layerForHomeAnimationLayer = layer++;
3995 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003996 }
Robert Carr2f8aa392018-01-31 14:46:51 -08003997 if (mAppAnimationLayer != null) {
3998 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003999 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004000 if (mBoostedAppAnimationLayer != null) {
4001 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4002 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004003 if (mHomeAppAnimationLayer != null) {
4004 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4005 }
Robert Carrb1579c82017-09-05 14:54:47 -07004006 }
4007
4008 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004009 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4010 switch (animationLayer) {
4011 case ANIMATION_LAYER_BOOSTED:
4012 return mBoostedAppAnimationLayer;
4013 case ANIMATION_LAYER_HOME:
4014 return mHomeAppAnimationLayer;
4015 case ANIMATION_LAYER_STANDARD:
4016 default:
4017 return mAppAnimationLayer;
4018 }
chaviw23ee71c2017-12-18 11:29:41 -08004019 }
4020
Robert Carrf7a7ca82017-12-06 13:17:07 -08004021 SurfaceControl getSplitScreenDividerAnchor() {
4022 return mSplitScreenDividerAnchor;
4023 }
4024
chaviw23ee71c2017-12-18 11:29:41 -08004025 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004026 void onParentSet() {
4027 super.onParentSet();
4028 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004029 mAppAnimationLayer = makeChildSurface(null)
4030 .setName("animationLayer")
4031 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004032 mBoostedAppAnimationLayer = makeChildSurface(null)
4033 .setName("boostedAnimationLayer")
4034 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004035 mHomeAppAnimationLayer = makeChildSurface(null)
4036 .setName("homeAnimationLayer")
4037 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004038 mSplitScreenDividerAnchor = makeChildSurface(null)
4039 .setName("splitScreenDividerAnchor")
4040 .build();
4041 getPendingTransaction()
4042 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004043 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004044 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004045 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004046 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004047 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004048 mAppAnimationLayer.destroy();
4049 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004050 mBoostedAppAnimationLayer.destroy();
4051 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004052 mHomeAppAnimationLayer.destroy();
4053 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004054 mSplitScreenDividerAnchor.destroy();
4055 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004056 }
4057 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004058 }
4059
Robert Carree4d4b92017-11-22 12:21:46 -08004060 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004061 AboveAppWindowContainers(String name, WindowManagerService service) {
4062 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004063 }
4064
Robert Carrb9506032018-02-13 13:54:00 -08004065 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004066 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4067 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4068 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4069 // To draw above the ColorFade layer during the screen off transition, the
4070 // rounded corner overlays need to be at the root of the surface hierarchy.
4071 // TODO: move the ColorLayer into the display overlay layer such that this is not
4072 // necessary anymore.
4073 builder.setParent(null);
4074 }
4075 return builder;
4076 }
4077
4078 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004079 void assignChildLayers(SurfaceControl.Transaction t) {
4080 assignChildLayers(t, null /* imeContainer */);
4081 }
4082
Robert Carree4d4b92017-11-22 12:21:46 -08004083 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4084 boolean needAssignIme = imeContainer != null
4085 && imeContainer.getSurfaceControl() != null;
4086 for (int j = 0; j < mChildren.size(); ++j) {
4087 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004088
4089 // See {@link mSplitScreenDividerAnchor}
4090 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4091 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4092 continue;
4093 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004094 if (wt.mRoundedCornerOverlay) {
4095 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4096 continue;
4097 }
Robert Carree4d4b92017-11-22 12:21:46 -08004098 wt.assignLayer(t, j);
4099 wt.assignChildLayers(t);
4100
4101 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
4102 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004103
4104 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
4105 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004106 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004107 needAssignIme = false;
4108 }
4109 }
4110 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004111 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004112 }
4113 }
4114 }
4115
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004116 /**
4117 * Window container class that contains all containers on this display that are not related to
4118 * Apps. E.g. status bar.
4119 */
Robert Carree4d4b92017-11-22 12:21:46 -08004120 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004121 /**
4122 * Compares two child window tokens returns -1 if the first is lesser than the second in
4123 * terms of z-order and 1 otherwise.
4124 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004125 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004126 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004127 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
4128 token1.mOwnerCanManageAppTokens)
4129 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
4130 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004131
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004132 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4133 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4134 return false;
4135 }
4136 final int req = w.mAttrs.screenOrientation;
4137 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4138 || req == SCREEN_ORIENTATION_UNSET) {
4139 return false;
4140 }
4141 return true;
4142 };
4143
Wale Ogunwale3a931692016-11-02 16:49:48 -07004144 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004145 private final Dimmer mDimmer = new Dimmer(this);
4146 private final Rect mTmpDimBoundsRect = new Rect();
4147
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004148 NonAppWindowContainers(String name, WindowManagerService service) {
4149 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004150 mName = name;
4151 }
4152
4153 void addChild(WindowToken token) {
4154 addChild(token, mWindowComparator);
4155 }
4156
4157 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004158 int getOrientation() {
4159 final WindowManagerPolicy policy = mService.mPolicy;
4160 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004161 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004162
4163 if (win != null) {
4164 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004165 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004166 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004167 if (mService.mKeyguardGoingAway) {
4168 // Keyguard can't affect the orientation if it is going away...
4169 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4170 return SCREEN_ORIENTATION_UNSET;
4171 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004172 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004173 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4174 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004175 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004176 }
4177
Andrii Kulian8ee72852017-03-10 10:36:45 -08004178 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004179
Jorim Jaggi1c530592018-04-06 15:11:47 +02004180 // Only allow force setting the orientation when all unknown visibilities have been
4181 // resolved, as otherwise we just may be starting another occluding activity.
4182 final boolean isUnoccluding =
4183 mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4184 && mService.mUnknownAppVisibilityController.allResolved();
4185 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004186 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004187 }
4188
4189 return SCREEN_ORIENTATION_UNSET;
4190 }
4191
4192 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004193 String getName() {
4194 return mName;
4195 }
chaviwf29223e2018-01-31 13:23:51 -08004196
4197 @Override
4198 Dimmer getDimmer() {
4199 return mDimmer;
4200 }
4201
4202 @Override
4203 void prepareSurfaces() {
4204 mDimmer.resetDimStates();
4205 super.prepareSurfaces();
4206 getBounds(mTmpDimBoundsRect);
4207
4208 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4209 scheduleAnimation();
4210 }
4211 }
Robert Carr3b716242016-08-16 16:02:21 -07004212 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004213
Robert Carr9034d962018-01-04 18:27:42 -08004214 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4215 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4216 super(name, service);
4217 }
4218
4219 @Override
4220 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4221 }
4222 };
4223
Robert Carrb1579c82017-09-05 14:54:47 -07004224 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4225 return mService.makeSurfaceBuilder(s)
4226 .setParent(mWindowingLayer);
4227 }
4228
4229 @Override
4230 SurfaceSession getSession() {
4231 return mSession;
4232 }
4233
4234 @Override
4235 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004236 SurfaceSession s = child != null ? child.getSession() : getSession();
4237 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07004238 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004239
4240 if (child == null) {
4241 return b;
4242 }
4243
Robert Carree4d4b92017-11-22 12:21:46 -08004244 return b.setName(child.getName())
4245 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004246 }
4247
4248 /**
4249 * The makeSurface variants are for use by the window-container
4250 * hierarchy. makeOverlay here is a function for various non windowing
4251 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4252 * and other potpourii.
4253 */
4254 SurfaceControl.Builder makeOverlay() {
4255 return mService.makeSurfaceBuilder(mSession)
4256 .setParent(mOverlayLayer);
4257 }
4258
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004259 /**
4260 * Reparents the given surface to mOverlayLayer.
4261 */
4262 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4263 transaction.reparent(surface, mOverlayLayer.getHandle());
4264 }
4265
Robert Carrb1579c82017-09-05 14:54:47 -07004266 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004267 if (spec.scale != 1.0) {
4268 mMagnificationSpec = spec;
4269 } else {
4270 mMagnificationSpec = null;
4271 }
4272
Robert Carrf59b8dd2017-10-02 18:58:36 -07004273 applyMagnificationSpec(getPendingTransaction(), spec);
4274 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004275 }
4276
Robert Carr24be9ab2018-04-30 17:54:53 -07004277 void reapplyMagnificationSpec() {
4278 if (mMagnificationSpec != null) {
4279 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4280 }
4281 }
4282
Robert Carrb1579c82017-09-05 14:54:47 -07004283 @Override
4284 void onParentSet() {
4285 // Since we are the top of the SurfaceControl hierarchy here
4286 // we create the root surfaces explicitly rather than chaining
4287 // up as the default implementation in onParentSet does. So we
4288 // explicitly do NOT call super here.
4289 }
4290
4291 @Override
4292 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004293
4294 // These are layers as children of "mWindowingLayer"
4295 mBelowAppWindowsContainers.assignLayer(t, 0);
4296 mTaskStackContainers.assignLayer(t, 1);
4297 mAboveAppWindowsContainers.assignLayer(t, 2);
4298
4299 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004300 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004301
Robert Carree4d4b92017-11-22 12:21:46 -08004302 // In the case where we have an IME target that is not in split-screen
4303 // mode IME assignment is easy. We just need the IME to go directly above
4304 // the target. This way children of the target will naturally go above the IME
4305 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004306 //
Robert Carree4d4b92017-11-22 12:21:46 -08004307 // In the case of split-screen windowing mode, we need to elevate the IME above the
4308 // docked divider while keeping the app itself below the docked divider, so instead
4309 // we use relative layering of the IME targets child windows, and place the
4310 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004311 //
Robert Carr234b6332018-03-20 13:38:16 -07004312 // In the case the IME target is animating, the animation Z order may be different
4313 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4314 // IME target. In this case we just layer the IME over all transitions by placing it in the
4315 // above applications layer.
4316 //
Robert Carree4d4b92017-11-22 12:21:46 -08004317 // In the case where we have no IME target we assign it where it's base layer would
4318 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004319 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4320 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004321 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004322 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004323 // TODO: We need to use an extra level on the app surface to ensure
4324 // this is always above SurfaceView but always below attached window.
4325 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004326 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004327 }
4328
4329 // Above we have assigned layers to our children, now we ask them to assign
4330 // layers to their children.
4331 mBelowAppWindowsContainers.assignChildLayers(t);
4332 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004333 mAboveAppWindowsContainers.assignChildLayers(t,
4334 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004335 mImeWindowsContainers.assignChildLayers(t);
4336 }
4337
4338 /**
4339 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4340 * that the IME target is one of the docked applications. We'd like the docked divider to be
4341 * above both of the applications, and we'd like the IME to be above the docked divider.
4342 * However we need child windows of the applications to be above the IME (Text drag handles).
4343 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4344 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4345 * with {@link #WindowState#assignLayer}
4346 */
4347 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004348 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004349 }
4350
4351 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004352 void prepareSurfaces() {
4353 final ScreenRotationAnimation screenRotationAnimation =
4354 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4355 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4356 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4357 mPendingTransaction.setMatrix(mWindowingLayer,
4358 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4359 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4360 mPendingTransaction.setPosition(mWindowingLayer,
4361 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4362 mPendingTransaction.setAlpha(mWindowingLayer,
4363 screenRotationAnimation.getEnterTransformation().getAlpha());
4364 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004365
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004366 super.prepareSurfaces();
4367 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004368
Jorim Jaggibe418292018-03-26 16:14:12 +02004369 void assignStackOrdering() {
4370 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004371 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004372
4373 /**
4374 * Increment the deferral count to determine whether to update the IME target.
4375 */
4376 void deferUpdateImeTarget() {
4377 mDeferUpdateImeTargetCount++;
4378 }
4379
4380 /**
4381 * Decrement the deferral count to determine whether to update the IME target. If the count
4382 * reaches 0, a new ime target will get computed.
4383 */
4384 void continueUpdateImeTarget() {
4385 if (mDeferUpdateImeTargetCount == 0) {
4386 return;
4387 }
4388
4389 mDeferUpdateImeTargetCount--;
4390 if (mDeferUpdateImeTargetCount == 0) {
4391 computeImeTarget(true /* updateImeTarget */);
4392 }
4393 }
4394
4395 /**
4396 * @return Whether a new IME target should be computed.
4397 */
4398 private boolean canUpdateImeTarget() {
4399 return mDeferUpdateImeTargetCount == 0;
4400 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004401
4402 InputMonitor getInputMonitor() {
4403 return mInputMonitor;
4404 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004405
4406 /**
4407 * @return Cached value whether we told display manager that we have content.
4408 */
4409 boolean getLastHasContent() {
4410 return mLastHasContent;
4411 }
Craig Mautner59c00972012-07-30 12:10:24 -07004412}