blob: 77367db94e3562ff06c9381c25f2133c8d97c7e4 [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale30e441d2017-11-09 08:28:45 -080019import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Wale Ogunwale61911492017-10-11 08:50:50 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale687b4272017-07-27 02:56:23 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070027import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
28import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070031import static android.view.Display.DEFAULT_DISPLAY;
32import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070033import static android.view.Surface.ROTATION_0;
34import static android.view.Surface.ROTATION_180;
35import static android.view.Surface.ROTATION_270;
36import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070037import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_INVALID;
40import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
43import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070045import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070046import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
47import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
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;
Adrian Roose99bc052017-11-20 17:55:31 +010061import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
62import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Adrian Roos1cf585052018-01-03 18:43:27 +010065import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates;
Jorim Jaggi75520d52017-08-24 17:23:19 +020066import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
71import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070084import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080086import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070087import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
89import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070090import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070092import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
93import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070094import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070096import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070097import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070098import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070099import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700100import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700101import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700103import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700104import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
105import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000106import static com.android.server.wm.proto.DisplayProto.DISPLAY_FRAMES;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700107import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
108import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
109import static com.android.server.wm.proto.DisplayProto.DPI;
110import static com.android.server.wm.proto.DisplayProto.ID;
111import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
112import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700113import static com.android.server.wm.proto.DisplayProto.ROTATION;
114import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700115import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700116import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700117
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700118import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700119import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700120import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700121import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700122import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700123import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700124import android.graphics.Matrix;
Adrian Roos1cf585052018-01-03 18:43:27 +0100125import android.graphics.Path;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800126import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700127import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700128import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100129import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700130import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700131import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700132import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700133import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700134import android.os.RemoteException;
135import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100136import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -0700137import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800138import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700139import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700140import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700141import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100142import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700143import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700144import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700145import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700146import android.view.Surface;
147import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100148import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700149import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700150
Bryce Lee48f4b572017-04-10 10:54:15 -0700151import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800152import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700153import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100154import com.android.server.policy.WindowManagerPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700155
Robert Carr3b716242016-08-16 16:02:21 -0700156import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700157import java.io.PrintWriter;
158import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700159import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700160import java.util.HashMap;
161import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700162import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700163import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100164import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800165import java.util.function.Consumer;
166import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700167
Craig Mautner59c00972012-07-30 12:10:24 -0700168/**
169 * Utility class for keeping track of the WindowStates and other pertinent contents of a
170 * particular Display.
171 *
172 * IMPORTANT: No method from this class should ever be used without holding
173 * WindowManagerService.mWindowMap.
174 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700175class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700176 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700177
178 /** Unique identifier of this stack. */
179 private final int mDisplayId;
180
Wale Ogunwale3a931692016-11-02 16:49:48 -0700181 /** The containers below are the only child containers the display can have. */
182 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100183 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700184 // Contains all non-app window containers that should be displayed above the app containers
185 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800186 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100187 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700188 // Contains all non-app window containers that should be displayed below the app containers
189 // (e.g. Wallpaper).
190 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100191 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700192 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
193 // 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 -0800194 // window containers together and move them in-sync if/when needed. We use a subclass of
195 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
196 private final NonMagnifiableWindowContainers mImeWindowsContainers =
197 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700198
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000199 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800200 private WindowState mTmpWindow2;
201 private WindowAnimator mTmpWindowAnimator;
202 private boolean mTmpRecoveringMemory;
203 private boolean mUpdateImeTarget;
204 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700205 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700206
Wale Ogunwale02319a62016-09-26 15:21:22 -0700207 // Mapping from a token IBinder to a WindowToken object on this display.
208 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
209
Andrii Kuliancd097992017-03-23 18:31:59 -0700210 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700211 int mInitialDisplayWidth = 0;
212 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700213 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700214
Adrian Roos1cf585052018-01-03 18:43:27 +0100215 DisplayCutout mInitialDisplayCutout;
216 DisplayCutout mDisplayCutoutOverride;
217
Andrii Kuliancd097992017-03-23 18:31:59 -0700218 /**
219 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
220 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
221 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
222 */
Craig Mautner59c00972012-07-30 12:10:24 -0700223 int mBaseDisplayWidth = 0;
224 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700225 /**
226 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
227 * but can be set from Settings or via shell command "adb shell wm density".
228 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
229 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700230 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700231 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700232 private final DisplayInfo mDisplayInfo = new DisplayInfo();
233 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700234 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000235 DisplayFrames mDisplayFrames;
236
Andrii Kulian06d07d62017-03-14 11:11:47 -0700237 /**
238 * For default display it contains real metrics, empty for others.
239 * @see WindowManagerService#createWatermarkInTransaction()
240 */
241 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
242 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
243 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700244
Andrii Kulian06d07d62017-03-14 11:11:47 -0700245 /**
246 * Compat metrics computed based on {@link #mDisplayMetrics}.
247 * @see #updateDisplayAndOrientation(int)
248 */
249 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
250
251 /** The desired scaling factor for compatible apps. */
252 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700253
Andrii Kulian8ee72852017-03-10 10:36:45 -0800254 /**
255 * Current rotation of the display.
256 * Constants as per {@link android.view.Surface.Rotation}.
257 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700258 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800259 */
260 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700261
Andrii Kulian8ee72852017-03-10 10:36:45 -0800262 /**
263 * Last applied orientation of the display.
264 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
265 *
266 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
267 */
268 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700269
Andrii Kulian8ee72852017-03-10 10:36:45 -0800270 /**
271 * Flag indicating that the application is receiving an orientation that has different metrics
272 * than it expected. E.g. Portrait instead of Landscape.
273 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700274 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800275 */
276 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700277
Andrii Kulian8ee72852017-03-10 10:36:45 -0800278 /**
279 * Orientation forced by some window. If there is no visible window that specifies orientation
280 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
281 *
282 * @see NonAppWindowContainers#getOrientation()
283 */
284 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700285
Andrii Kulian8ee72852017-03-10 10:36:45 -0800286 /**
287 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
288 * occluded.
289 *
290 * @see NonAppWindowContainers#getOrientation()
291 */
292 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
293
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700294 /**
295 * Keep track of wallpaper visibility to notify changes.
296 */
297 private boolean mLastWallpaperVisible = false;
298
Andrii Kulian06d07d62017-03-14 11:11:47 -0700299 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700300
Craig Mautner39834192012-09-02 07:47:24 -0700301 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700302 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700303 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700304 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800305 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700306
Craig Mautnerdc548482014-02-05 13:35:24 -0800307 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700308 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800309
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700310 /** Detect user tapping outside of current focused task bounds .*/
311 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700312
Craig Mautner6601b7b2013-04-29 10:29:11 -0700313 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700314 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700315
Craig Mautner6601b7b2013-04-29 10:29:11 -0700316 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800317 private final Rect mTmpRect = new Rect();
318 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700319 private final RectF mTmpRectF = new RectF();
320 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800321 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700322
Bryce Leef3c6a472017-11-14 14:53:06 -0800323 /** Used for handing back size of display */
324 private final Rect mTmpBounds = new Rect();
325
Craig Mautner95da1082014-02-24 17:54:35 -0800326 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700327 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800328
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700329 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700330 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700331
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800332 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
333
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000334 private boolean mHaveBootMsg = false;
335 private boolean mHaveApp = false;
336 private boolean mHaveWallpaper = false;
337 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700338
339 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
340
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700341 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
342 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000343 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
344 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700345
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700346 // True if this display is in the process of being removed. Used to determine if the removal of
347 // the display's direct children should be allowed.
348 private boolean mRemovingDisplay = false;
349
Bryce Leed1871262017-06-12 14:12:29 -0700350 // {@code false} if this display is in the processing of being created.
351 private boolean mDisplayReady = false;
352
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800353 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700354
Robert Carrb1579c82017-09-05 14:54:47 -0700355 private final SurfaceSession mSession = new SurfaceSession();
356
357 /**
358 * We organize all top-level Surfaces in to the following layers.
359 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800360 * and omitted from Screen-Magnification, for example the strict mode flash or
361 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700362 * {@link #mWindowingLayer} contains everything else.
363 */
364 private SurfaceControl mOverlayLayer;
365
366 /**
367 * See {@link #mOverlayLayer}
368 */
369 private SurfaceControl mWindowingLayer;
370
371 /**
372 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
373 * <p>
374 * For these surfaces currently we use a surface based on the larger of width or height so we
375 * don't have to resize when rotating the display.
376 */
377 private int mSurfaceSize;
378
379 /**
380 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
381 */
382 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
383
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100384 /** Temporary float array to retrieve 3x3 matrix values. */
385 private final float[] mTmpFloats = new float[9];
386
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800387 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
388 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800389 final AppWindowToken atoken = w.mAppToken;
390 if (winAnimator.mDrawState == READY_TO_SHOW) {
391 if (atoken == null || atoken.allDrawn) {
392 if (w.performShowLocked()) {
393 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
394 if (DEBUG_LAYOUT_REPEATS) {
395 mService.mWindowPlacerLocked.debugLayoutRepeats(
396 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
397 }
398 }
399 }
400 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800401 };
402
403 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
404 final WindowStateAnimator winAnimator = w.mWinAnimator;
405 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
406 return;
407 }
408
409 final int flags = w.mAttrs.flags;
410
411 // If this window is animating, make a note that we have an animating window and take
412 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100413 if (winAnimator.isAnimationSet()) {
414 final AnimationAdapter anim = w.getAnimation();
415 if (anim != null) {
416 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800417 mTmpWindow = w;
418 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100419 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800420 if (color != 0) {
421 final TaskStack stack = w.getStack();
422 if (stack != null) {
423 stack.setAnimationBackground(winAnimator, color);
424 }
425 }
426 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800427 }
428
429 // If this window's app token is running a detached wallpaper animation, make a note so
430 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200431 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
432 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
433 if (animation != null) {
434 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800435 mTmpWindow = w;
436 }
437
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200438 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800439 if (color != 0) {
440 final TaskStack stack = w.getStack();
441 if (stack != null) {
442 stack.setAnimationBackground(winAnimator, color);
443 }
444 }
445 }
446 };
447
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800448 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
449 final int lostFocusUid = mTmpWindow.mOwnerUid;
450 final Handler handler = mService.mH;
451 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
452 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
453 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
454 w.mAttrs.hideTimeoutMilliseconds);
455 }
456 }
457 };
458
459 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
460 final AppWindowToken focusedApp = mService.mFocusedApp;
461 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
462 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
463
464 if (!w.canReceiveKeys()) {
465 return false;
466 }
467
468 final AppWindowToken wtoken = w.mAppToken;
469
470 // If this window's application has been removed, just skip it.
471 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
472 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
473 + (wtoken.removed ? "removed" : "sendingToBottom"));
474 return false;
475 }
476
477 if (focusedApp == null) {
478 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
479 + " using new focus @ " + w);
480 mTmpWindow = w;
481 return true;
482 }
483
484 if (!focusedApp.windowsAreFocusable()) {
485 // Current focused app windows aren't focusable...
486 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
487 + " focusable using new focus @ " + w);
488 mTmpWindow = w;
489 return true;
490 }
491
492 // Descend through all of the app tokens and find the first that either matches
493 // win.mAppToken (return win) or mFocusedApp (return null).
494 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
495 if (focusedApp.compareTo(wtoken) > 0) {
496 // App stack below focused app stack. No focus for you!!!
497 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
498 "findFocusedWindow: Reached focused app=" + focusedApp);
499 mTmpWindow = null;
500 return true;
501 }
502 }
503
504 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
505 mTmpWindow = w;
506 return true;
507 };
508
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800509 private final Consumer<WindowState> mPerformLayout = w -> {
510 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
511 // wasting time and funky changes while a window is animating away.
512 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
513 || w.isGoneForLayoutLw();
514
515 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
516 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
517 + " mLayoutAttached=" + w.mLayoutAttached
518 + " screen changed=" + w.isConfigChanged());
519 final AppWindowToken atoken = w.mAppToken;
520 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200521 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800522 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
523 + " parentHidden=" + w.isParentWindowHidden());
524 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200525 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800526 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
527 + " parentHidden=" + w.isParentWindowHidden());
528 }
529
530 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
531 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
532 // since that means "perform layout as normal, just don't display").
533 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
534 || ((w.isConfigChanged() || w.setReportResizeHints())
535 && !w.isGoneForLayoutLw() &&
536 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
537 (w.mHasSurface && w.mAppToken != null &&
538 w.mAppToken.layoutConfigChanges)))) {
539 if (!w.mLayoutAttached) {
540 if (mTmpInitial) {
541 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
542 w.mContentChanged = false;
543 }
544 if (w.mAttrs.type == TYPE_DREAM) {
545 // Don't layout windows behind a dream, so that if it does stuff like hide
546 // the status bar we won't get a bad transition when it goes away.
547 mTmpWindow = w;
548 }
549 w.mLayoutNeeded = false;
550 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200551 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000552 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800553 w.mLayoutSeq = mService.mLayoutSeq;
554
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200555 // If this is the first layout, we need to initialize the last inset values as
556 // otherwise we'd immediately cause an unnecessary resize.
557 if (firstLayout) {
558 w.updateLastInsetValues();
559 }
560
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800561 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
562 + " mContainingFrame=" + w.mContainingFrame
563 + " mDisplayFrame=" + w.mDisplayFrame);
564 }
565 }
566 };
567
568 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
569 if (w.mLayoutAttached) {
570 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
571 + " mViewVisibility=" + w.mViewVisibility
572 + " mRelayoutCalled=" + w.mRelayoutCalled);
573 // If this view is GONE, then skip it -- keep the current frame, and let the caller
574 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
575 // windows, since that means "perform layout as normal, just don't display").
576 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
577 return;
578 }
579 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
580 || w.mLayoutNeeded) {
581 if (mTmpInitial) {
582 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
583 w.mContentChanged = false;
584 }
585 w.mLayoutNeeded = false;
586 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000587 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800588 w.mLayoutSeq = mService.mLayoutSeq;
589 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
590 + " mContainingFrame=" + w.mContainingFrame
591 + " mDisplayFrame=" + w.mDisplayFrame);
592 }
593 } else if (w.mAttrs.type == TYPE_DREAM) {
594 // Don't layout windows behind a dream, so that if it does stuff like hide the
595 // status bar we won't get a bad transition when it goes away.
596 mTmpWindow = mTmpWindow2;
597 }
598 };
599
600 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
601 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
602 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
603 return w.canBeImeTarget();
604 };
605
606 private final Consumer<WindowState> mApplyPostLayoutPolicy =
607 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
608 mService.mInputMethodTarget);
609
610 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
611 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
612 final boolean obscuredChanged = w.mObscured !=
613 mTmpApplySurfaceChangesTransactionState.obscured;
614 final RootWindowContainer root = mService.mRoot;
615 // Only used if default window
616 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
617
618 // Update effect.
619 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
620 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
621 final boolean isDisplayed = w.isDisplayedLw();
622
623 if (isDisplayed && w.isObscuringDisplay()) {
624 // This window completely covers everything behind it, so we want to leave all
625 // of them as undimmed (for performance reasons).
626 root.mObscuringWindow = w;
627 mTmpApplySurfaceChangesTransactionState.obscured = true;
628 }
629
630 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
631 root.handleNotObscuredLocked(w,
632 mTmpApplySurfaceChangesTransactionState.obscured,
633 mTmpApplySurfaceChangesTransactionState.syswin);
634
635 if (w.mHasSurface && isDisplayed) {
636 final int type = w.mAttrs.type;
637 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
638 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
639 mTmpApplySurfaceChangesTransactionState.syswin = true;
640 }
641 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
642 && w.mAttrs.preferredRefreshRate != 0) {
643 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
644 = w.mAttrs.preferredRefreshRate;
645 }
646 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
647 && w.mAttrs.preferredDisplayModeId != 0) {
648 mTmpApplySurfaceChangesTransactionState.preferredModeId
649 = w.mAttrs.preferredDisplayModeId;
650 }
651 }
652 }
653
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800654 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
655 && mWallpaperController.isWallpaperTarget(w)) {
656 // This is the wallpaper target and its obscured state changed... make sure the
657 // current wallpaper's visibility has been updated accordingly.
658 mWallpaperController.updateWallpaperVisibility();
659 }
660
Jorim Jaggi6b0f8462018-01-05 17:23:47 +0100661 w.handleWindowMovedIfNeeded(mPendingTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800662
663 final WindowStateAnimator winAnimator = w.mWinAnimator;
664
665 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
666 w.mContentChanged = false;
667
668 // Moved from updateWindowsAndWallpaperLocked().
669 if (w.mHasSurface) {
670 // Take care of the window being ready to display.
671 final boolean committed = winAnimator.commitFinishDrawingLocked();
672 if (isDefaultDisplay && committed) {
673 if (w.mAttrs.type == TYPE_DREAM) {
674 // HACK: When a dream is shown, it may at that point hide the lock screen.
675 // So we need to redo the layout to let the phone window manager make this
676 // happen.
677 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
678 if (DEBUG_LAYOUT_REPEATS) {
679 surfacePlacer.debugLayoutRepeats(
680 "dream and commitFinishDrawingLocked true",
681 pendingLayoutChanges);
682 }
683 }
684 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
685 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
686 "First draw done in potential wallpaper target " + w);
687 root.mWallpaperMayChange = true;
688 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
689 if (DEBUG_LAYOUT_REPEATS) {
690 surfacePlacer.debugLayoutRepeats(
691 "wallpaper and commitFinishDrawingLocked true",
692 pendingLayoutChanges);
693 }
694 }
695 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800696 }
697
698 final AppWindowToken atoken = w.mAppToken;
699 if (atoken != null) {
Adrian Roos4d18a2e2017-12-19 19:08:05 +0100700 atoken.updateLetterbox(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800701 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
702 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
703 mTmpUpdateAllDrawn.add(atoken);
704 }
705 }
706
707 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
708 && w.isDisplayedLw()) {
709 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
710 }
711
712 w.updateResizingWindowIfNeeded();
713 };
714
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800715 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800716 * Create new {@link DisplayContent} instance, add itself to the root window container and
717 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700718 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800719 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700720 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
721 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700722 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700723 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700724 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100725 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800726 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
727 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
728 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
729 + " new=" + display);
730 }
731
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700732 mDisplay = display;
733 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700734 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700735 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700736 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700737 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000738 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700739 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800740 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700741 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700742
Robert Carrb1579c82017-09-05 14:54:47 -0700743 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
744
745 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
746 .setSize(mSurfaceSize, mSurfaceSize)
747 .setOpaque(true);
748 mWindowingLayer = b.setName("Display Root").build();
749 mOverlayLayer = b.setName("Display Overlays").build();
750
Robert Carrf59b8dd2017-10-02 18:58:36 -0700751 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700752 .setLayerStack(mWindowingLayer, mDisplayId)
753 .show(mWindowingLayer)
754 .setLayer(mOverlayLayer, 1)
755 .setLayerStack(mOverlayLayer, mDisplayId)
756 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700757 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700758
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700759 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700760 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700761 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700762 super.addChild(mAboveAppWindowsContainers, null);
763 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800764
765 // Add itself as a child to the root container.
766 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700767
768 // TODO(b/62541591): evaluate whether this is the best spot to declare the
769 // {@link DisplayContent} ready for use.
770 mDisplayReady = true;
771 }
772
773 boolean isReady() {
774 // The display is ready when the system and the individual display are both ready.
775 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700776 }
777
778 int getDisplayId() {
779 return mDisplayId;
780 }
781
Wale Ogunwale02319a62016-09-26 15:21:22 -0700782 WindowToken getWindowToken(IBinder binder) {
783 return mTokenMap.get(binder);
784 }
785
786 AppWindowToken getAppWindowToken(IBinder binder) {
787 final WindowToken token = getWindowToken(binder);
788 if (token == null) {
789 return null;
790 }
791 return token.asAppWindowToken();
792 }
793
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700794 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700795 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
796 if (dc != null) {
797 // We currently don't support adding a window token to the display if the display
798 // already has the binder mapped to another token. If there is a use case for supporting
799 // this moving forward we will either need to merge the WindowTokens some how or have
800 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700801 throw new IllegalArgumentException("Can't map token=" + token + " to display="
802 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700803 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700804 if (binder == null) {
805 throw new IllegalArgumentException("Can't map token=" + token + " to display="
806 + getName() + " binder is null");
807 }
808 if (token == null) {
809 throw new IllegalArgumentException("Can't map null token to display="
810 + getName() + " binder=" + binder);
811 }
812
Wale Ogunwale02319a62016-09-26 15:21:22 -0700813 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700814
815 if (token.asAppWindowToken() == null) {
816 // Add non-app token to container hierarchy on the display. App tokens are added through
817 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700818 switch (token.windowType) {
819 case TYPE_WALLPAPER:
820 mBelowAppWindowsContainers.addChild(token);
821 break;
822 case TYPE_INPUT_METHOD:
823 case TYPE_INPUT_METHOD_DIALOG:
824 mImeWindowsContainers.addChild(token);
825 break;
826 default:
827 mAboveAppWindowsContainers.addChild(token);
828 break;
829 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700830 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700831 }
832
833 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700834 final WindowToken token = mTokenMap.remove(binder);
835 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800836 token.setExiting();
837 }
838 return token;
839 }
840
841 /** Changes the display the input window token is housed on to this one. */
842 void reParentWindowToken(WindowToken token) {
843 final DisplayContent prevDc = token.getDisplayContent();
844 if (prevDc == this) {
845 return;
846 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800847 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
848 && token.asAppWindowToken() == null) {
849 // Removed the token from the map, but made sure it's not an app token before removing
850 // from parent.
851 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700852 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800853
854 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700855 }
856
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700857 void removeAppToken(IBinder binder) {
858 final WindowToken token = removeWindowToken(binder);
859 if (token == null) {
860 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
861 return;
862 }
863
864 final AppWindowToken appToken = token.asAppWindowToken();
865
866 if (appToken == null) {
867 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
868 return;
869 }
870
871 appToken.onRemovedFromDisplay();
872 }
873
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700874 Display getDisplay() {
875 return mDisplay;
876 }
877
Craig Mautner59c00972012-07-30 12:10:24 -0700878 DisplayInfo getDisplayInfo() {
879 return mDisplayInfo;
880 }
881
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700882 DisplayMetrics getDisplayMetrics() {
883 return mDisplayMetrics;
884 }
885
Andrii Kulian8ee72852017-03-10 10:36:45 -0800886 int getRotation() {
887 return mRotation;
888 }
889
890 void setRotation(int newRotation) {
891 mRotation = newRotation;
892 }
893
894 int getLastOrientation() {
895 return mLastOrientation;
896 }
897
898 void setLastOrientation(int orientation) {
899 mLastOrientation = orientation;
900 }
901
902 boolean getAltOrientation() {
903 return mAltOrientation;
904 }
905
906 void setAltOrientation(boolean altOrientation) {
907 mAltOrientation = altOrientation;
908 }
909
910 int getLastWindowForcedOrientation() {
911 return mLastWindowForcedOrientation;
912 }
913
Andrii Kulian06d07d62017-03-14 11:11:47 -0700914 /**
915 * Update rotation of the display.
916 *
917 * Returns true if the rotation has been changed. In this case YOU MUST CALL
918 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
919 */
920 boolean updateRotationUnchecked(boolean inTransaction) {
921 if (mService.mDeferredRotationPauseCount > 0) {
922 // Rotation updates have been paused temporarily. Defer the update until
923 // updates have been resumed.
924 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
925 return false;
926 }
927
928 ScreenRotationAnimation screenRotationAnimation =
929 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
930 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
931 // Rotation updates cannot be performed while the previous rotation change
932 // animation is still in progress. Skip this update. We will try updating
933 // again after the animation is finished and the display is unfrozen.
934 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
935 return false;
936 }
937 if (mService.mDisplayFrozen) {
938 // Even if the screen rotation animation has finished (e.g. isAnimating
939 // returns false), there is still some time where we haven't yet unfrozen
940 // the display. We also need to abort rotation here.
941 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
942 "Deferring rotation, still finishing previous rotation");
943 return false;
944 }
945
946 if (!mService.mDisplayEnabled) {
947 // No point choosing a rotation if the display is not enabled.
948 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
949 return false;
950 }
951
952 final int oldRotation = mRotation;
953 final int lastOrientation = mLastOrientation;
954 final boolean oldAltOrientation = mAltOrientation;
955 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700956 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700957 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700958
Robert Carr0e007272017-10-02 13:00:55 -0700959 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700960 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
961 if (seamlessRotated != null) {
962 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
963 // to complete (that is, waiting for windows to redraw). It's tempting to check
964 // w.mSeamlessRotationCount but that could be incorrect in the case of
965 // window-removal.
966 return false;
967 }
Robert Carr0e007272017-10-02 13:00:55 -0700968
969 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700970 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -0700971 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -0700972 mayRotateSeamlessly = false;
973 }
974 for (int i = 0; i < mService.mSessions.size(); i++) {
975 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
976 mayRotateSeamlessly = false;
977 break;
978 }
979 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700980 }
Robert Carr0e007272017-10-02 13:00:55 -0700981 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700982
983 // TODO: Implement forced rotation changes.
984 // Set mAltOrientation to indicate that the application is receiving
985 // an orientation that has different metrics than it expected.
986 // eg. Portrait instead of Landscape.
987
988 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
989 lastOrientation, rotation);
990
991 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
992 + ", got rotation " + rotation + " which has "
993 + (altOrientation ? "incompatible" : "compatible") + " metrics");
994
995 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
996 // No change.
997 return false;
998 }
999
1000 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1001 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1002 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1003
1004 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1005 mService.mWaitingForConfig = true;
1006 }
1007
1008 mRotation = rotation;
1009 mAltOrientation = altOrientation;
1010 if (isDefaultDisplay) {
1011 mService.mPolicy.setRotationLw(rotation);
1012 }
1013
1014 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1015 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1016 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1017 WINDOW_FREEZE_TIMEOUT_DURATION);
1018
1019 setLayoutNeeded();
1020 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001021 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001022
1023 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001024 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001025 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1026 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1027 mDisplayId);
1028 } else {
1029 // The screen rotation animation uses a screenshot to freeze the screen
1030 // while windows resize underneath.
1031 // When we are rotating seamlessly, we allow the elements to transition
1032 // to their rotated state independently and without a freeze required.
1033 screenRotationAnimation = null;
1034
1035 // We have to reset this in case a window was removed before it
1036 // finished seamless rotation.
1037 mService.mSeamlessRotationCount = 0;
1038 }
1039
1040 // We need to update our screen size information to match the new rotation. If the rotation
1041 // has actually changed then this method will return true and, according to the comment at
1042 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1043 // By updating the Display info here it will be available to
1044 // #computeScreenConfiguration() later.
1045 updateDisplayAndOrientation(getConfiguration().uiMode);
1046
1047 if (!inTransaction) {
1048 if (SHOW_TRANSACTIONS) {
1049 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1050 }
1051 mService.openSurfaceTransaction();
1052 }
1053 try {
1054 // NOTE: We disable the rotation in the emulator because
1055 // it doesn't support hardware OpenGL emulation yet.
1056 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1057 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001058 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001059 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1060 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1061 mService.scheduleAnimationLocked();
1062 }
1063 }
1064
1065 if (rotateSeamlessly) {
1066 forAllWindows(w -> {
1067 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1068 }, true /* traverseTopToBottom */);
1069 }
1070
1071 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1072 } finally {
1073 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001074 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001075 if (SHOW_LIGHT_TRANSACTIONS) {
1076 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1077 }
1078 }
1079 }
1080
1081 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001082 if (w.mHasSurface && !rotateSeamlessly) {
1083 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001084 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001085 mService.mRoot.mOrientationChangeComplete = false;
1086 w.mLastFreezeDuration = 0;
1087 }
1088 w.mReportOrientationChanged = true;
1089 }, true /* traverseTopToBottom */);
1090
1091 if (rotateSeamlessly) {
1092 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1093 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1094 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1095 }
1096
1097 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1098 final WindowManagerService.RotationWatcher rotationWatcher
1099 = mService.mRotationWatchers.get(i);
1100 if (rotationWatcher.mDisplayId == mDisplayId) {
1101 try {
1102 rotationWatcher.mWatcher.onRotationChanged(rotation);
1103 } catch (RemoteException e) {
1104 // Ignore
1105 }
1106 }
1107 }
1108
1109 // TODO (multi-display): Magnification is supported only for the default display.
1110 // Announce rotation only if we will not animate as we already have the
1111 // windows in final state. Otherwise, we make this call at the rotation end.
1112 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1113 && isDefaultDisplay) {
1114 mService.mAccessibilityController.onRotationChangedLocked(this);
1115 }
1116
1117 return true;
1118 }
1119
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001120 void configureDisplayPolicy() {
1121 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1122 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1123
1124 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1125 }
1126
Andrii Kulian06d07d62017-03-14 11:11:47 -07001127 /**
1128 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1129 * changed.
1130 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1131 */
1132 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1133 // Use the effective "visual" dimensions based on current rotation
1134 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1135 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1136 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1137 int dw = realdw;
1138 int dh = realdh;
1139
1140 if (mAltOrientation) {
1141 if (realdw > realdh) {
1142 // Turn landscape into portrait.
1143 int maxw = (int)(realdh/1.3f);
1144 if (maxw < realdw) {
1145 dw = maxw;
1146 }
1147 } else {
1148 // Turn portrait into landscape.
1149 int maxh = (int)(realdw/1.3f);
1150 if (maxh < realdh) {
1151 dh = maxh;
1152 }
1153 }
1154 }
1155
1156 // Update application display metrics.
1157 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1158 mDisplayId);
1159 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1160 mDisplayId);
1161 mDisplayInfo.rotation = mRotation;
1162 mDisplayInfo.logicalWidth = dw;
1163 mDisplayInfo.logicalHeight = dh;
1164 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1165 mDisplayInfo.appWidth = appWidth;
1166 mDisplayInfo.appHeight = appHeight;
1167 if (isDefaultDisplay) {
1168 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1169 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1170 }
Adrian Roos1cf585052018-01-03 18:43:27 +01001171 mDisplayInfo.displayCutout = calculateDisplayCutoutForCurrentRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001172 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1173 if (mDisplayScalingDisabled) {
1174 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1175 } else {
1176 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1177 }
1178
1179 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1180 mDisplayInfo);
1181
1182 mBaseDisplayRect.set(0, 0, dw, dh);
1183
1184 if (isDefaultDisplay) {
1185 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1186 mCompatDisplayMetrics);
1187 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001188
1189 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001190 return mDisplayInfo;
1191 }
1192
Adrian Roos1cf585052018-01-03 18:43:27 +01001193 DisplayCutout calculateDisplayCutoutForCurrentRotation() {
1194 final DisplayCutout cutout = mInitialDisplayCutout;
1195 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT || mRotation == ROTATION_0) {
1196 return cutout;
1197 }
1198 final Path bounds = cutout.getBounds().getBoundaryPath();
1199 transformPhysicalToLogicalCoordinates(mRotation, mInitialDisplayWidth,
1200 mInitialDisplayHeight, mTmpMatrix);
1201 bounds.transform(mTmpMatrix);
1202 return DisplayCutout.fromBounds(bounds);
1203 }
1204
Andrii Kulian06d07d62017-03-14 11:11:47 -07001205 /**
1206 * Compute display configuration based on display properties and policy settings.
1207 * Do not call if mDisplayReady == false.
1208 */
1209 void computeScreenConfiguration(Configuration config) {
1210 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1211
1212 final int dw = displayInfo.logicalWidth;
1213 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001214 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1215 // TODO: Probably best to set this based on some setting in the display content object,
1216 // so the display can be configured for things like fullscreen.
1217 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001218
Andrii Kulian06d07d62017-03-14 11:11:47 -07001219 config.screenWidthDp =
1220 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1221 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1222 config.screenHeightDp =
1223 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1224 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001225
1226 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1227 final int leftInset = mTmpRect.left;
1228 final int topInset = mTmpRect.top;
1229 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001230 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1231 leftInset + displayInfo.appWidth /* right */,
1232 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001233 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1234 || displayInfo.rotation == Surface.ROTATION_270);
1235
1236 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1237 mDisplayMetrics.density, config);
1238
1239 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1240 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1241 ? Configuration.SCREENLAYOUT_ROUND_YES
1242 : Configuration.SCREENLAYOUT_ROUND_NO);
1243
1244 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1245 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1246 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1247 dh, mDisplayId);
1248 config.densityDpi = displayInfo.logicalDensityDpi;
1249
1250 config.colorMode =
1251 (displayInfo.isHdr()
1252 ? Configuration.COLOR_MODE_HDR_YES
1253 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001254 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001255 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1256 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1257
1258 // Update the configuration based on available input devices, lid switch,
1259 // and platform configuration.
1260 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1261 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1262 config.navigation = Configuration.NAVIGATION_NONAV;
1263
1264 int keyboardPresence = 0;
1265 int navigationPresence = 0;
1266 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1267 final int len = devices != null ? devices.length : 0;
1268 for (int i = 0; i < len; i++) {
1269 InputDevice device = devices[i];
1270 if (!device.isVirtual()) {
1271 final int sources = device.getSources();
1272 final int presenceFlag = device.isExternal() ?
1273 WindowManagerPolicy.PRESENCE_EXTERNAL :
1274 WindowManagerPolicy.PRESENCE_INTERNAL;
1275
1276 // TODO(multi-display): Configure on per-display basis.
1277 if (mService.mIsTouchDevice) {
1278 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1279 InputDevice.SOURCE_TOUCHSCREEN) {
1280 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1281 }
1282 } else {
1283 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1284 }
1285
1286 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1287 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1288 navigationPresence |= presenceFlag;
1289 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1290 && config.navigation == Configuration.NAVIGATION_NONAV) {
1291 config.navigation = Configuration.NAVIGATION_DPAD;
1292 navigationPresence |= presenceFlag;
1293 }
1294
1295 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1296 config.keyboard = Configuration.KEYBOARD_QWERTY;
1297 keyboardPresence |= presenceFlag;
1298 }
1299 }
1300 }
1301
1302 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1303 config.navigation = Configuration.NAVIGATION_DPAD;
1304 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1305 }
1306
1307 // Determine whether a hard keyboard is available and enabled.
1308 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1309 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1310 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1311 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1312 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1313 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1314 }
1315
1316 // Let the policy update hidden states.
1317 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1318 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1319 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1320 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1321 }
1322
1323 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1324 int displayId) {
1325 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1326 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1327 final int unrotDw, unrotDh;
1328 if (rotated) {
1329 unrotDw = dh;
1330 unrotDh = dw;
1331 } else {
1332 unrotDw = dw;
1333 unrotDh = dh;
1334 }
1335 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1336 displayId);
1337 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1338 displayId);
1339 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1340 displayId);
1341 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1342 displayId);
1343 return sw;
1344 }
1345
1346 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1347 DisplayMetrics dm, int dw, int dh, int displayId) {
1348 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1349 displayId);
1350 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1351 uiMode, displayId);
1352 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1353 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1354 if (curSize == 0 || size < curSize) {
1355 curSize = size;
1356 }
1357 return curSize;
1358 }
1359
1360 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1361 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1362
1363 // We need to determine the smallest width that will occur under normal
1364 // operation. To this, start with the base screen size and compute the
1365 // width under the different possible rotations. We need to un-rotate
1366 // the current screen dimensions before doing this.
1367 int unrotDw, unrotDh;
1368 if (rotated) {
1369 unrotDw = dh;
1370 unrotDh = dw;
1371 } else {
1372 unrotDw = dw;
1373 unrotDh = dh;
1374 }
1375 displayInfo.smallestNominalAppWidth = 1<<30;
1376 displayInfo.smallestNominalAppHeight = 1<<30;
1377 displayInfo.largestNominalAppWidth = 0;
1378 displayInfo.largestNominalAppHeight = 0;
1379 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1380 unrotDh);
1381 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1382 unrotDw);
1383 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1384 unrotDh);
1385 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1386 unrotDw);
1387 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1388 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1389 displayId);
1390 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1391 displayId);
1392 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1393 displayId);
1394 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1395 displayId);
1396 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1397 outConfig.screenLayout = sl;
1398 }
1399
1400 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1401 int uiMode, int displayId) {
1402 // Get the app screen size at this rotation.
1403 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1404 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1405
1406 // Compute the screen layout size class for this rotation.
1407 int longSize = w;
1408 int shortSize = h;
1409 if (longSize < shortSize) {
1410 int tmp = longSize;
1411 longSize = shortSize;
1412 shortSize = tmp;
1413 }
1414 longSize = (int)(longSize/density);
1415 shortSize = (int)(shortSize/density);
1416 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1417 }
1418
1419 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1420 int uiMode, int dw, int dh) {
1421 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1422 displayId);
1423 if (width < displayInfo.smallestNominalAppWidth) {
1424 displayInfo.smallestNominalAppWidth = width;
1425 }
1426 if (width > displayInfo.largestNominalAppWidth) {
1427 displayInfo.largestNominalAppWidth = width;
1428 }
1429 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1430 displayId);
1431 if (height < displayInfo.smallestNominalAppHeight) {
1432 displayInfo.smallestNominalAppHeight = height;
1433 }
1434 if (height > displayInfo.largestNominalAppHeight) {
1435 displayInfo.largestNominalAppHeight = height;
1436 }
1437 }
1438
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001439 DockedStackDividerController getDockedDividerController() {
1440 return mDividerControllerLocked;
1441 }
1442
Winson Chung655332c2016-10-31 13:14:28 -07001443 PinnedStackController getPinnedStackController() {
1444 return mPinnedStackControllerLocked;
1445 }
1446
Jeff Browna506a6e2013-06-04 00:02:38 -07001447 /**
1448 * Returns true if the specified UID has access to this display.
1449 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001450 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001451 return mDisplay.hasAccess(uid);
1452 }
1453
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001454 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001455 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001456 }
1457
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001458 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001459 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001460 }
1461
Wale Ogunwale61911492017-10-11 08:50:50 -07001462 /**
1463 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1464 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001465 TaskStack getSplitScreenPrimaryStack() {
1466 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001467 return (stack != null && stack.isVisible()) ? stack : null;
1468 }
1469
1470 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001471 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001472 * not visible.
1473 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001474 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1475 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001476 }
1477
1478 TaskStack getPinnedStack() {
1479 return mTaskStackContainers.getPinnedStack();
1480 }
1481
1482 private boolean hasPinnedStack() {
1483 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001484 }
1485
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001486 /**
1487 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1488 * Null is no compatible stack on the display.
1489 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001490 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001491 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1492 }
1493
1494 /**
1495 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1496 * activity type. Null is no compatible stack on the display.
1497 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001498 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001499 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001500 }
1501
Bryce Lee48f4b572017-04-10 10:54:15 -07001502 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001503 TaskStack getTopStack() {
1504 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001505 }
1506
Wale Ogunwale61911492017-10-11 08:50:50 -07001507 void onStackWindowingModeChanged(TaskStack stack) {
1508 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001509 }
1510
Andrii Kulian441e4492016-09-29 15:25:00 -07001511 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001512 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001513 super.onConfigurationChanged(newParentConfig);
1514
Andrii Kulian3a507b52016-09-19 18:14:12 -07001515 // The display size information is heavily dependent on the resources in the current
1516 // configuration, so we need to reconfigure it every time the configuration changes.
1517 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1518 mService.reconfigureDisplayLocked(this);
1519
Bryce Leef3c6a472017-11-14 14:53:06 -08001520 final DockedStackDividerController dividerController = getDockedDividerController();
1521
1522 if (dividerController != null) {
1523 getDockedDividerController().onConfigurationChanged();
1524 }
1525
1526 final PinnedStackController pinnedStackController = getPinnedStackController();
1527
1528 if (pinnedStackController != null) {
1529 getPinnedStackController().onConfigurationChanged();
1530 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001531 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001532
Andrii Kulian441e4492016-09-29 15:25:00 -07001533 /**
1534 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1535 * bounds were updated.
1536 */
1537 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001538 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1539 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001540 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001541 changedStackList.add(stack.mStackId);
1542 }
1543 }
Winson Chung32c566f2017-04-11 18:31:21 -07001544
1545 // If there was no pinned stack, we still need to notify the controller of the display info
1546 // update as a result of the config change. We do this here to consolidate the flow between
1547 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001548 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001549 mPinnedStackControllerLocked.onDisplayInfoChanged();
1550 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001551 }
1552
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001553 @Override
1554 boolean fillsParent() {
1555 return true;
1556 }
1557
1558 @Override
1559 boolean isVisible() {
1560 return true;
1561 }
1562
1563 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001564 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001565 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001566 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001567 }
1568
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001569 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001570 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1571 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1572 // target, or here in order if there isn't an IME target.
1573 if (traverseTopToBottom) {
1574 for (int i = mChildren.size() - 1; i >= 0; --i) {
1575 final DisplayChildWindowContainer child = mChildren.get(i);
1576 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1577 // In this case the Ime windows will be processed above their target so we skip
1578 // here.
1579 continue;
1580 }
1581 if (child.forAllWindows(callback, traverseTopToBottom)) {
1582 return true;
1583 }
1584 }
1585 } else {
1586 final int count = mChildren.size();
1587 for (int i = 0; i < count; i++) {
1588 final DisplayChildWindowContainer child = mChildren.get(i);
1589 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1590 // In this case the Ime windows will be processed above their target so we skip
1591 // here.
1592 continue;
1593 }
1594 if (child.forAllWindows(callback, traverseTopToBottom)) {
1595 return true;
1596 }
1597 }
1598 }
1599 return false;
1600 }
1601
1602 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1603 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1604 }
1605
Wale Ogunwale399c8692017-05-08 14:22:42 -07001606 @Override
1607 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001608 final WindowManagerPolicy policy = mService.mPolicy;
1609
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001610 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001611 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001612 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001613 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001614 // If the display is frozen, some activities may be in the middle of restarting, and
1615 // thus have removed their old window. If the window has the flag to hide the lock
1616 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1617 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001618 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001619 } else if (policy.isKeyguardLocked()) {
1620 // Use the last orientation the while the display is frozen with the keyguard
1621 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1622 // window. We don't want to check the show when locked window directly though as
1623 // things aren't stable while the display is frozen, for example the window could be
1624 // momentarily unavailable due to activity relaunch.
1625 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001626 + "return " + mLastOrientation);
1627 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001628 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001629 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001630 final int orientation = mAboveAppWindowsContainers.getOrientation();
1631 if (orientation != SCREEN_ORIENTATION_UNSET) {
1632 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001633 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001634 }
1635
Wale Ogunwale399c8692017-05-08 14:22:42 -07001636 // Top system windows are not requesting an orientation. Start searching from apps.
1637 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001638 }
1639
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001640 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001641 // Check if display metrics changed and update base values if needed.
1642 updateBaseDisplayMetricsIfNeeded();
1643
Craig Mautner722285e2012-09-07 13:55:58 -07001644 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001645 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001646
Wale Ogunwale61911492017-10-11 08:50:50 -07001647 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1648 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001649 }
Craig Mautner722285e2012-09-07 13:55:58 -07001650 }
1651
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001652 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001653 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1654 if (displayManagerInternal != null) {
1655 // Bootstrap the default logical display from the display manager.
1656 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1657 if (newDisplayInfo != null) {
1658 mDisplayInfo.copyFrom(newDisplayInfo);
1659 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001660 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001661
Andrii Kuliancd097992017-03-23 18:31:59 -07001662 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1663 mDisplayInfo.logicalDensityDpi);
1664 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1665 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1666 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001667 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001668 }
1669
Andrii Kuliancd097992017-03-23 18:31:59 -07001670 /**
1671 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1672 * values.
1673 */
1674 private void updateBaseDisplayMetricsIfNeeded() {
1675 // Get real display metrics without overrides from WM.
1676 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1677 final int orientation = mDisplayInfo.rotation;
1678 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1679 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1680 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1681 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001682 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001683
1684 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1685 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001686 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1687 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001688
1689 if (displayMetricsChanged) {
1690 // Check if display size or density is forced.
1691 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1692 || mBaseDisplayHeight != mInitialDisplayHeight;
1693 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1694
1695 // If there is an override set for base values - use it, otherwise use new values.
1696 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1697 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1698 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1699
1700 // Real display metrics changed, so we should also update initial values.
1701 mInitialDisplayWidth = newWidth;
1702 mInitialDisplayHeight = newHeight;
1703 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001704 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001705 mService.reconfigureDisplayLocked(this);
1706 }
1707 }
1708
Bryce Lee27cec322017-03-21 09:41:37 -07001709 /** Sets the maximum width the screen resolution can be */
1710 void setMaxUiWidth(int width) {
1711 if (DEBUG_DISPLAY) {
1712 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1713 }
1714
1715 mMaxUiWidth = width;
1716
1717 // Update existing metrics.
1718 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1719 }
1720
1721 /** Update base (override) display metrics. */
1722 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1723 mBaseDisplayWidth = baseWidth;
1724 mBaseDisplayHeight = baseHeight;
1725 mBaseDisplayDensity = baseDensity;
1726
1727 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1728 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1729 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1730 mBaseDisplayWidth = mMaxUiWidth;
1731
1732 if (DEBUG_DISPLAY) {
1733 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1734 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1735 + " on display:" + getDisplayId());
1736 }
1737 }
1738
1739 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001740
1741 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001742 }
1743
Wale Ogunwaledb506192017-12-08 10:57:32 -08001744 void getStableRect(Rect out) {
1745 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001746 }
1747
Wale Ogunwale61911492017-10-11 08:50:50 -07001748 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001749 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1750 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001751
Wale Ogunwale61911492017-10-11 08:50:50 -07001752 final TaskStack stack = new TaskStack(mService, stackId, controller);
1753 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001754 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001755 }
1756
Andrii Kulian51c1b672017-04-07 18:39:32 -07001757 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001758 final DisplayContent prevDc = stack.getDisplayContent();
1759 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001760 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1761 + " which is not currently attached to any display");
1762 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001763 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001764 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1765 + " to its current displayId=" + mDisplayId);
1766 }
1767
Wale Ogunwale61911492017-10-11 08:50:50 -07001768 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001769 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001770 }
1771
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001772 @Override
1773 protected void addChild(DisplayChildWindowContainer child,
1774 Comparator<DisplayChildWindowContainer> comparator) {
1775 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1776 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001777
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001778 @Override
1779 protected void addChild(DisplayChildWindowContainer child, int index) {
1780 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1781 }
1782
1783 @Override
1784 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001785 // Only allow removal of direct children from this display if the display is in the process
1786 // of been removed.
1787 if (mRemovingDisplay) {
1788 super.removeChild(child);
1789 return;
1790 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001791 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001792 }
1793
Andrii Kuliand2765632016-12-12 22:26:34 -08001794 @Override
1795 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1796 // Children of the display are statically ordered, so the real intention here is to perform
1797 // the operation on the display and not the static direct children.
1798 getParent().positionChildAt(position, this, includingParents);
1799 }
1800
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001801 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001802 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1803 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001804 final int taskId = stack.taskIdFromPoint(x, y);
1805 if (taskId != -1) {
1806 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001807 }
1808 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001809 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001810 }
1811
Chong Zhang8e89b312015-09-09 15:09:30 -07001812 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001813 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001814 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001815 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001816 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001817 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001818 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001819 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1820 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001821 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001822 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001823 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001824
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001825 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1826 if (mTmpTaskForResizePointSearchResult.searchDone) {
1827 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001828 }
1829 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001830 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001831 }
1832
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001833 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001834 // The provided task is the task on this display with focus, so if WindowManagerService's
1835 // focused app is not on this display, focusedTask will be null.
1836 if (focusedTask == null) {
1837 mTouchExcludeRegion.setEmpty();
1838 } else {
1839 mTouchExcludeRegion.set(mBaseDisplayRect);
1840 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1841 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001842 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1843 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001844 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1845 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001846 }
1847 // If we removed the focused task above, add it back and only leave its
1848 // outside touch area in the exclusion. TapDectector is not interested in
1849 // any touch inside the focused task itself.
1850 if (!mTmpRect2.isEmpty()) {
1851 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1852 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001853 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001854 final WindowState inputMethod = mService.mInputMethodWindow;
1855 if (inputMethod != null && inputMethod.isVisibleLw()) {
1856 // If the input method is visible and the user is typing, we don't want these touch
1857 // events to be intercepted and used to change focus. This would likely cause a
1858 // disappearance of the input method.
1859 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001860 if (inputMethod.getDisplayId() == mDisplayId) {
1861 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1862 } else {
1863 // IME is on a different display, so we need to update its tap detector.
1864 // TODO(multidisplay): Remove when IME will always appear on same display.
1865 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1866 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001867 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001868 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1869 WindowState win = mTapExcludedWindows.get(i);
1870 win.getTouchableRegion(mTmpRegion);
1871 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1872 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001873 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001874 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001875 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001876 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001877 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1878 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001879 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001880 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001881 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001882 }
1883
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001884 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001885 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001886 super.switchUser();
1887 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001888 }
1889
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001890 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001891 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1892 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001893 }
1894 }
1895
Wale Ogunwale10124582016-09-15 20:25:50 -07001896 @Override
1897 void removeIfPossible() {
1898 if (isAnimating()) {
1899 mDeferredRemoval = true;
1900 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001901 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001902 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001903 }
1904
Wale Ogunwale10124582016-09-15 20:25:50 -07001905 @Override
1906 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001907 mRemovingDisplay = true;
1908 try {
1909 super.removeImmediately();
1910 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001911 if (mService.canDispatchPointerEvents()) {
1912 if (mTapDetector != null) {
1913 mService.unregisterPointerEventListener(mTapDetector);
1914 }
1915 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1916 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1917 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001918 }
Jorim Jaggia3844032018-01-03 15:54:43 +01001919 mService.mAnimator.removeDisplayLocked(mDisplayId);
1920
Robert Carrb1579c82017-09-05 14:54:47 -07001921 // The pending transaction won't be applied so we should
1922 // just clean up any surfaces pending destruction.
1923 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001924 } finally {
1925 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001926 }
Craig Mautner95da1082014-02-24 17:54:35 -08001927 }
1928
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001929 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001930 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001931 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001932 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1933
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001934 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001935 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001936 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001937 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001938 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001939 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001940 }
1941
Andrii Kulian0214ed92017-05-16 13:44:05 -07001942 /** @return 'true' if removal of this display content is deferred due to active animation. */
1943 boolean isRemovalDeferred() {
1944 return mDeferredRemoval;
1945 }
1946
Wale Ogunwale10124582016-09-15 20:25:50 -07001947 boolean animateForIme(float interpolatedValue, float animationTarget,
1948 float dividerAnimationTarget) {
1949 boolean updated = false;
1950
Wale Ogunwale61911492017-10-11 08:50:50 -07001951 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1952 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001953 if (stack == null || !stack.isAdjustedForIme()) {
1954 continue;
1955 }
1956
1957 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1958 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1959 updated = true;
1960 } else {
1961 mDividerControllerLocked.mLastAnimationProgress =
1962 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1963 mDividerControllerLocked.mLastDividerProgress =
1964 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1965 updated |= stack.updateAdjustForIme(
1966 mDividerControllerLocked.mLastAnimationProgress,
1967 mDividerControllerLocked.mLastDividerProgress,
1968 false /* force */);
1969 }
1970 if (interpolatedValue >= 1f) {
1971 stack.endImeAdjustAnimation();
1972 }
1973 }
1974
1975 return updated;
1976 }
1977
1978 boolean clearImeAdjustAnimation() {
1979 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001980 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1981 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001982 if (stack != null && stack.isAdjustedForIme()) {
1983 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1984 changed = true;
1985 }
1986 }
1987 return changed;
1988 }
1989
1990 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001991 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1992 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001993 if (stack.isVisible() && stack.isAdjustedForIme()) {
1994 stack.beginImeAdjustAnimation();
1995 }
1996 }
1997 }
1998
1999 void adjustForImeIfNeeded() {
2000 final WindowState imeWin = mService.mInputMethodWindow;
2001 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2002 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002003 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002004 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2005 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2006 imeTargetStack.getDockSide() : DOCKED_INVALID;
2007 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2008 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2009 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002010 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002011 final boolean imeHeightChanged = imeVisible &&
2012 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2013
2014 // The divider could be adjusted for IME position, or be thinner than usual,
2015 // or both. There are three possible cases:
2016 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2017 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2018 // - If IME is not visible, divider is not moved and is normal width.
2019
2020 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002021 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2022 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002023 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002024 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2025 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002026 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2027 } else {
2028 stack.resetAdjustedForIme(false);
2029 }
2030 }
2031 mDividerControllerLocked.setAdjustedForIme(
2032 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2033 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002034 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2035 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002036 stack.resetAdjustedForIme(!dockVisible);
2037 }
2038 mDividerControllerLocked.setAdjustedForIme(
2039 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2040 }
Winson Chung655332c2016-10-31 13:14:28 -07002041 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002042 }
2043
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002044 /**
2045 * If a window that has an animation specifying a colored background and the current wallpaper
2046 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2047 * suddenly disappear.
2048 */
2049 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002050 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2051 w -> w.mIsWallpaper && w.isVisibleNow());
2052
2053 if (visibleWallpaper != null) {
2054 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002055 }
2056 return winAnimator.mAnimLayer;
2057 }
2058
Wale Ogunwale10124582016-09-15 20:25:50 -07002059 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002060 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2061 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002062 stack.prepareFreezingTaskBounds();
2063 }
2064 }
2065
Wale Ogunwale94744212015-09-21 19:01:47 -07002066 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002067 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002068
2069 // Compute a transform matrix to undo the coordinate space transformation,
2070 // and present the window at the same physical position it previously occupied.
2071 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2072 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2073
2074 mTmpRectF.set(bounds);
2075 mTmpMatrix.mapRect(mTmpRectF);
2076 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002077 }
2078
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002079 static int deltaRotation(int oldRotation, int newRotation) {
2080 int delta = newRotation - oldRotation;
2081 if (delta < 0) delta += 4;
2082 return delta;
2083 }
2084
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002085 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002086 Matrix outMatrix) {
2087 // For rotations without Z-ordering we don't need the target rectangle's position.
2088 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2089 displayHeight, outMatrix);
2090 }
2091
2092 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2093 float displayWidth, float displayHeight, Matrix outMatrix) {
2094 switch (rotation) {
2095 case ROTATION_0:
2096 outMatrix.reset();
2097 break;
2098 case ROTATION_270:
2099 outMatrix.setRotate(270, 0, 0);
2100 outMatrix.postTranslate(0, displayHeight);
2101 outMatrix.postTranslate(rectTop, 0);
2102 break;
2103 case ROTATION_180:
2104 outMatrix.reset();
2105 break;
2106 case ROTATION_90:
2107 outMatrix.setRotate(90, 0, 0);
2108 outMatrix.postTranslate(displayWidth, 0);
2109 outMatrix.postTranslate(-rectTop, rectLeft);
2110 break;
2111 }
2112 }
2113
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002114 @CallSuper
2115 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002116 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002117 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002118 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002119 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002120 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2121 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002122 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002123 }
2124 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2125 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002126 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2127 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002128 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002129 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002130 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2131 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002132 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002133 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002134 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2135 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002136 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002137 }
2138 proto.write(DPI, mBaseDisplayDensity);
2139 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002140 proto.write(ROTATION, mRotation);
2141 final ScreenRotationAnimation screenRotationAnimation =
2142 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2143 if (screenRotationAnimation != null) {
2144 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2145 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002146 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002147 proto.end(token);
2148 }
2149
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002150 @Override
2151 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2152 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002153 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2154 final String subPrefix = " " + prefix;
2155 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2156 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2157 pw.print("dpi");
2158 if (mInitialDisplayWidth != mBaseDisplayWidth
2159 || mInitialDisplayHeight != mBaseDisplayHeight
2160 || mInitialDisplayDensity != mBaseDisplayDensity) {
2161 pw.print(" base=");
2162 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2163 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2164 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002165 if (mDisplayScalingDisabled) {
2166 pw.println(" noscale");
2167 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002168 pw.print(" cur=");
2169 pw.print(mDisplayInfo.logicalWidth);
2170 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2171 pw.print(" app=");
2172 pw.print(mDisplayInfo.appWidth);
2173 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2174 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2175 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2176 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2177 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002178 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002179 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002180 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002181
Craig Mautnerdc548482014-02-05 13:35:24 -08002182 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002183 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002184 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2185 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002186 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002187 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002188
Craig Mautnerdc548482014-02-05 13:35:24 -08002189 pw.println();
2190 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002191 pw.println();
2192 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002193 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002194 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002195 pw.print(" Exiting #"); pw.print(i);
2196 pw.print(' '); pw.print(token);
2197 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002198 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002199 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002200 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002201
Jorim Jaggi31f71702016-05-04 16:43:04 -07002202 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002203
2204 // Dump stack references
2205 final TaskStack homeStack = getHomeStack();
2206 if (homeStack != null) {
2207 pw.println(prefix + "homeStack=" + homeStack.getName());
2208 }
2209 final TaskStack pinnedStack = getPinnedStack();
2210 if (pinnedStack != null) {
2211 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2212 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002213 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002214 if (splitScreenPrimaryStack != null) {
2215 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2216 }
2217
2218 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002219 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002220 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002221 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002222
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002223 pw.println();
2224 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002225 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002226
2227 @Override
2228 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002229 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002230 }
2231
2232 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002233 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002234 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002235
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002236 /** Returns true if the stack in the windowing mode is visible. */
2237 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002238 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002239 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002240 }
2241
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002242 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002243 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002244 final int x = (int) xf;
2245 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002246 final WindowState touchedWin = getWindow(w -> {
2247 final int flags = w.mAttrs.flags;
2248 if (!w.isVisibleLw()) {
2249 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002250 }
2251 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002252 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002253 }
2254
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002255 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002256 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002257 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002258 }
2259
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002260 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002261
2262 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002263 return mTmpRegion.contains(x, y) || touchFlags == 0;
2264 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002265
2266 return touchedWin;
2267 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002268
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002269 boolean canAddToastWindowForUid(int uid) {
2270 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002271 // Also if the app is focused adding more than one toast at
2272 // a time for better backwards compatibility.
2273 final WindowState focusedWindowForUid = getWindow(w ->
2274 w.mOwnerUid == uid && w.isFocused());
2275 if (focusedWindowForUid != null) {
2276 return true;
2277 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002278 final WindowState win = getWindow(w ->
2279 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2280 && !w.mWindowRemovalAllowed);
2281 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002282 }
2283
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002284 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002285 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2286 return;
2287 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002288
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002289 // Used to communicate the old focus to the callback method.
2290 mTmpWindow = oldFocus;
2291
2292 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002293 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002294
2295 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002297
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002298 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002299
2300 if (mTmpWindow == null) {
2301 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2302 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002303 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002304 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002305 }
2306
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002307 /** Updates the layer assignment of windows on this display. */
2308 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002309 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002310 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002311 if (setLayoutNeeded) {
2312 setLayoutNeeded();
2313 }
Robert Carrb1579c82017-09-05 14:54:47 -07002314
Robert Carrf59b8dd2017-10-02 18:58:36 -07002315 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002316 // the application of this transaction until the animation pass triggers
2317 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2318 // the hiding and showing of surfaces.
2319 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002320 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002321 }
2322
Wale Ogunwale1666e312016-12-16 11:27:18 -08002323 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2324 // moving containers or resizing them. Need to investigate the best way to have it automatically
2325 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002326 void layoutAndAssignWindowLayersIfNeeded() {
2327 mService.mWindowsChanged = true;
2328 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002329
2330 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2331 false /*updateInputWindows*/)) {
2332 assignWindowLayers(false /* setLayoutNeeded */);
2333 }
2334
2335 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2336 mService.mWindowPlacerLocked.performSurfacePlacement();
2337 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2338 }
2339
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002340 /** Returns true if a leaked surface was destroyed */
2341 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002342 // Used to indicate that a surface was leaked.
2343 mTmpWindow = null;
2344 forAllWindows(w -> {
2345 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002346 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002347 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002348 }
2349 if (!mService.mSessions.contains(wsa.mSession)) {
2350 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002351 + w + " surface=" + wsa.mSurfaceController
2352 + " token=" + w.mToken
2353 + " pid=" + w.mSession.mPid
2354 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002355 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002356 mService.mForceRemoves.add(w);
2357 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002358 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002359 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002360 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002361 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002362 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002363 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002364 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002365 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002366 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002367
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002368 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002369 }
2370
2371 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002372 * Determine and return the window that should be the IME target.
2373 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2374 * @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 +00002375 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002376 WindowState computeImeTarget(boolean updateImeTarget) {
2377 if (mService.mInputMethodWindow == null) {
2378 // There isn't an IME so there shouldn't be a target...That was easy!
2379 if (updateImeTarget) {
2380 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2381 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002382 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002383 }
2384 return null;
2385 }
2386
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002387 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2388 // same display. Or even when the current IME/target are not on the same screen as the next
2389 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002390 mUpdateImeTarget = updateImeTarget;
2391 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002392
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002393
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002394 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2395 // to be on top of it, but it is not -really- where input will go. So look down below
2396 // for a real window to target...
2397 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2398 final AppWindowToken token = target.mAppToken;
2399 if (token != null) {
2400 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2401 if (betterTarget != null) {
2402 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002403 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002404 }
2405 }
2406
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002407 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2408 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002409
2410 // Now, a special case -- if the last target's window is in the process of exiting, and is
2411 // above the new target, keep on the last target to avoid flicker. Consider for example a
2412 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2413 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2414 // scrim.
2415 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002416 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2417 && (target == null
2418 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002419 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002420 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002421 }
2422
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002423 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2424 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002425
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002426 if (target == null) {
2427 if (updateImeTarget) {
2428 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2429 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2430 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002431 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002432 }
2433
2434 return null;
2435 }
2436
2437 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002438 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2439 if (token != null) {
2440
2441 // Now some fun for dealing with window animations that modify the Z order. We need
2442 // to look at all windows below the current target that are in this app, finding the
2443 // highest visible one in layering.
2444 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002445 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002446 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002447 }
2448
2449 if (highestTarget != null) {
2450 final AppTransition appTransition = mService.mAppTransition;
2451 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2452 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2453 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002454 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002455
2456 if (appTransition.isTransitionSet()) {
2457 // If we are currently setting up for an animation, hold everything until we
2458 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002459 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002460 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002461 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002462 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002463 // If the window we are currently targeting is involved with an animation,
2464 // and it is on top of the next target we will be over, then hold off on
2465 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002466 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002467 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002468 }
2469 }
2470 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002471
2472 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2473 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002474 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002475 }
2476
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002477 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002478 }
2479
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002480 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002481 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002482 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002483 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002484 }
2485
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002486 mService.mInputMethodTarget = target;
2487 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002488 assignWindowLayers(false /* setLayoutNeeded */);
2489 }
2490
2491 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2492 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2493 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2494 }
2495
2496 // Used to indicate we have reached the first window in the range we are interested in.
2497 mTmpWindow = null;
2498
2499 // TODO: Figure-out a more efficient way to do this.
2500 final WindowState candidate = getWindow(w -> {
2501 if (w == top) {
2502 // Reached the first window in the range we are interested in.
2503 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002504 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002505 if (mTmpWindow == null) {
2506 return false;
2507 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002508
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002509 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2510 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002511 }
2512 // If we reached the bottom of the range of windows we are considering,
2513 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002514 if (w == bottom) {
2515 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002516 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002517 return false;
2518 });
2519
2520 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002521 }
2522
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002523 void setLayoutNeeded() {
2524 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2525 mLayoutNeeded = true;
2526 }
2527
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002528 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002529 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2530 mLayoutNeeded = false;
2531 }
2532
2533 boolean isLayoutNeeded() {
2534 return mLayoutNeeded;
2535 }
2536
Wale Ogunwale02319a62016-09-26 15:21:22 -07002537 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2538 if (mTokenMap.isEmpty()) {
2539 return;
2540 }
2541 pw.println(" Display #" + mDisplayId);
2542 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2543 while (it.hasNext()) {
2544 final WindowToken token = it.next();
2545 pw.print(" ");
2546 pw.print(token);
2547 if (dumpAll) {
2548 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002549 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002550 } else {
2551 pw.println();
2552 }
2553 }
2554 }
2555
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002556 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002557 final int[] index = new int[1];
2558 forAllWindows(w -> {
2559 final WindowStateAnimator wAnim = w.mWinAnimator;
2560 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2561 index[0] = index[0] + 1;
2562 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002563 }
2564
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002565 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002566 forAllWindows(w -> {
2567 w.mWinAnimator.enableSurfaceTrace(fd);
2568 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002569 }
2570
2571 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002572 forAllWindows(w -> {
2573 w.mWinAnimator.disableSurfaceTrace();
2574 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002575 }
2576
Jorim Jaggife762342016-10-13 14:33:27 +02002577 /**
2578 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2579 */
2580 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2581 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002582 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002583 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2584 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002585 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002586 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002587 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002588 }
2589
Wale Ogunwale494009b82016-10-21 09:01:38 -07002590 boolean checkWaitingForWindows() {
2591
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002592 mHaveBootMsg = false;
2593 mHaveApp = false;
2594 mHaveWallpaper = false;
2595 mHaveKeyguard = true;
2596
2597 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002598 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2599 return true;
2600 }
2601 if (w.isDrawnLw()) {
2602 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002603 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002604 } else if (w.mAttrs.type == TYPE_APPLICATION
2605 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002606 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002607 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002608 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002609 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002610 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002611 }
2612 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002613 return false;
2614 });
2615
2616 if (visibleWindow != null) {
2617 // We have a visible window.
2618 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002619 }
2620
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002621 // if the wallpaper service is disabled on the device, we're never going to have
2622 // wallpaper, don't bother waiting for it
2623 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2624 com.android.internal.R.bool.config_enableWallpaperService)
2625 && !mService.mOnlyCore;
2626
Wale Ogunwale494009b82016-10-21 09:01:38 -07002627 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2628 "******** booted=" + mService.mSystemBooted
2629 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002630 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2631 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2632 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002633
2634 // If we are turning on the screen to show the boot message, don't do it until the boot
2635 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002636 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002637 return true;
2638 }
2639
2640 // If we are turning on the screen after the boot is completed normally, don't do so until
2641 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 if (mService.mSystemBooted
2643 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002644 return true;
2645 }
2646
2647 return false;
2648 }
2649
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002650 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002651 mTmpWindowAnimator = animator;
2652 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002653 }
2654
2655 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002656 resetAnimationBackgroundAnimator();
2657
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002658 // Used to indicate a detached wallpaper.
2659 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002660 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002661
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002662 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002663
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002664 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002665 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2667 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002668 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2669 }
2670 }
2671
Wale Ogunwale494009b82016-10-21 09:01:38 -07002672 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002673 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002674 if (imFocus == null) {
2675 return false;
2676 }
2677
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002678 if (DEBUG_INPUT_METHOD) {
2679 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2680 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2681 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002682 }
2683
Wale Ogunwale494009b82016-10-21 09:01:38 -07002684 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2685
2686 if (DEBUG_INPUT_METHOD) {
2687 Slog.i(TAG_WM, "IM target client: " + imeClient);
2688 if (imeClient != null) {
2689 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2690 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2691 }
2692 }
2693
2694 return imeClient != null && imeClient.asBinder() == client.asBinder();
2695 }
2696
2697 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002698 final WindowState win = getWindow(
2699 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2700 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002701 }
2702
2703 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002704 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002705 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002706 final int curValue = w.mSystemUiVisibility;
2707 final int diff = (curValue ^ visibility) & globalDiff;
2708 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002709 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002710 w.mSeq++;
2711 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002712 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002713 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2714 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002715 visibility, newValue, diff);
2716 }
2717 } catch (RemoteException e) {
2718 // so sorry
2719 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002720 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002721 }
2722
2723 void onWindowFreezeTimeout() {
2724 Slog.w(TAG_WM, "Window freeze timeout expired.");
2725 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002726
2727 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002728 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002729 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002730 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002731 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002732 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2733 - mService.mDisplayFreezeTime);
2734 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002735 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002736 mService.mWindowPlacerLocked.performSurfacePlacement();
2737 }
2738
2739 void waitForAllWindowsDrawn() {
2740 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002741 forAllWindows(w -> {
2742 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2743 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2744 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002745 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002746 w.mLastContentInsets.set(-1, -1, -1, -1);
2747 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002748 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002749 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002750 }
2751
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002752 // TODO: Super crazy long method that should be broken down...
2753 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2754
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002755 final int dw = mDisplayInfo.logicalWidth;
2756 final int dh = mDisplayInfo.logicalHeight;
2757 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2758
2759 mTmpUpdateAllDrawn.clear();
2760
2761 int repeats = 0;
2762 do {
2763 repeats++;
2764 if (repeats > 6) {
2765 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2766 clearLayoutNeeded();
2767 break;
2768 }
2769
2770 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2771 pendingLayoutChanges);
2772
Andrii Kulian839def92016-11-02 10:58:58 -07002773 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2774 // the wallpaper window jumping across displays.
2775 // Remove check for default display when there will be support for multiple wallpaper
2776 // targets (on different displays).
2777 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002778 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002779 }
2780
2781 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2782 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2783 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2784 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002785 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002786 }
2787 }
2788
2789 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2790 setLayoutNeeded();
2791 }
2792
2793 // FIRST LOOP: Perform a layout, if needed.
2794 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2795 performLayout(repeats == 1, false /* updateInputWindows */);
2796 } else {
2797 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2798 }
2799
2800 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2801 pendingLayoutChanges = 0;
2802
2803 if (isDefaultDisplay) {
2804 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002805 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002806 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2807 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2808 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2809 }
2810 } while (pendingLayoutChanges != 0);
2811
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002812 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002813
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002814 mTmpRecoveringMemory = recoveringMemory;
2815 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01002816 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002817
2818 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002819 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2820 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2821 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002822 true /* inTraversal, must call performTraversalInTrans... below */);
2823
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002824 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2825 if (wallpaperVisible != mLastWallpaperVisible) {
2826 mLastWallpaperVisible = wallpaperVisible;
2827 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2828 }
2829
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002830 while (!mTmpUpdateAllDrawn.isEmpty()) {
2831 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2832 // See if any windows have been drawn, so they (and others associated with them)
2833 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002834 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002835 }
2836
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002837 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002838 }
2839
Bryce Leef3c6a472017-11-14 14:53:06 -08002840 private void updateBounds() {
2841 calculateBounds(mTmpBounds);
2842 setBounds(mTmpBounds);
2843 }
2844
2845 // Determines the current display bounds based on the current state
2846 private void calculateBounds(Rect out) {
2847 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2848 final int orientation = mDisplayInfo.rotation;
2849 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2850 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2851 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2852 int width = mDisplayInfo.logicalWidth;
2853 int left = (physWidth - width) / 2;
2854 int height = mDisplayInfo.logicalHeight;
2855 int top = (physHeight - height) / 2;
2856 out.set(left, top, left + width, top + height);
2857 }
2858
2859 @Override
2860 public void getBounds(Rect out) {
2861 calculateBounds(out);
2862 }
2863
2864 private void getBounds(Rect out, int orientation) {
2865 getBounds(out);
2866
2867 // Rotate the Rect if needed.
2868 final int currentRotation = mDisplayInfo.rotation;
2869 final int rotationDelta = deltaRotation(currentRotation, orientation);
2870 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2871 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2872 mTmpRectF.set(out);
2873 mTmpMatrix.mapRect(mTmpRectF);
2874 mTmpRectF.round(out);
2875 }
2876 }
2877
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002878 void performLayout(boolean initial, boolean updateInputWindows) {
2879 if (!isLayoutNeeded()) {
2880 return;
2881 }
2882 clearLayoutNeeded();
2883
2884 final int dw = mDisplayInfo.logicalWidth;
2885 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002886 if (DEBUG_LAYOUT) {
2887 Slog.v(TAG, "-------------------------------------");
2888 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2889 }
2890
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002891 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2892 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2893 // display info above...
2894 mDisplayFrames.mRotation = mRotation;
2895 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002896 if (isDefaultDisplay) {
2897 // Not needed on non-default displays.
2898 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2899 mService.mScreenRect.set(0, 0, dw, dh);
2900 }
2901
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002902 int seq = mService.mLayoutSeq + 1;
2903 if (seq < 0) seq = 0;
2904 mService.mLayoutSeq = seq;
2905
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002906 // Used to indicate that we have processed the dream window and all additional windows are
2907 // behind it.
2908 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002909 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002910
2911 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002912 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002913
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002914 // Used to indicate that we have processed the dream window and all additional attached
2915 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002916 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002917 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002918
2919 // Now perform layout of attached windows, which usually depend on the position of the
2920 // window they are attached to. XXX does not deal with windows that are attached to windows
2921 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002922 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002923
2924 // Window frames may have changed. Tell the input dispatcher about it.
2925 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2926 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2927 if (updateInputWindows) {
2928 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2929 }
2930
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002931 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2932 }
2933
2934 /**
2935 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2936 * In portrait mode, it grabs the full screenshot.
2937 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002938 * @param config of the output bitmap
2939 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2940 */
chaviwfbe47df2017-11-10 16:14:49 -08002941 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2942 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002943 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002944 if (DEBUG_SCREENSHOT) {
2945 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002946 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002947 return null;
2948 }
2949
chaviwfbe47df2017-11-10 16:14:49 -08002950 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951 return null;
2952 }
2953
chaviwfbe47df2017-11-10 16:14:49 -08002954 int dw = mDisplayInfo.logicalWidth;
2955 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002956
chaviwfbe47df2017-11-10 16:14:49 -08002957 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002958 return null;
2959 }
2960
chaviwfbe47df2017-11-10 16:14:49 -08002961 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002962
2963 // The screenshot API does not apply the current screen rotation.
2964 int rot = mDisplay.getRotation();
2965
2966 if (rot == ROTATION_90 || rot == ROTATION_270) {
2967 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2968 }
2969
chaviwfbe47df2017-11-10 16:14:49 -08002970 // SurfaceFlinger is not aware of orientation, so convert our logical
2971 // crop to SurfaceFlinger's portrait orientation.
2972 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002973
2974 final ScreenRotationAnimation screenRotationAnimation =
2975 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2976 final boolean inRotation = screenRotationAnimation != null &&
2977 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08002978 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002979
Robert Carrb1579c82017-09-05 14:54:47 -07002980 // TODO(b/68392460): We should screenshot Task controls directly
2981 // but it's difficult at the moment as the Task doesn't have the
2982 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08002983 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002984 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07002985 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002986 return null;
2987 }
chaviwfbe47df2017-11-10 16:14:49 -08002988
2989 // Create a copy of the screenshot that is immutable and backed in ashmem.
2990 // This greatly reduces the overhead of passing the bitmap between processes.
2991 final Bitmap ret = bitmap.createAshmemBitmap(config);
2992 bitmap.recycle();
2993 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002994 }
chaviwfbe47df2017-11-10 16:14:49 -08002995 }
2996
2997 private boolean shouldScreenshotWallpaper() {
2998 MutableBoolean screenshotReady = new MutableBoolean(false);
2999
3000 forAllWindows(w -> {
3001 if (!w.mIsWallpaper) {
3002 return false;
3003 }
3004
3005 // Found the wallpaper window
3006 final WindowStateAnimator winAnim = w.mWinAnimator;
3007
3008 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
3009 screenshotReady.value = true;
3010 }
3011
3012 return true;
3013 }, true /* traverseTopToBottom */);
3014
3015 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003016 }
3017
3018 // TODO: Can this use createRotationMatrix()?
3019 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3020 if (rot == Surface.ROTATION_90) {
3021 final int tmp = crop.top;
3022 crop.top = dw - crop.right;
3023 crop.right = crop.bottom;
3024 crop.bottom = dw - crop.left;
3025 crop.left = tmp;
3026 } else if (rot == Surface.ROTATION_180) {
3027 int tmp = crop.top;
3028 crop.top = dh - crop.bottom;
3029 crop.bottom = dh - tmp;
3030 tmp = crop.right;
3031 crop.right = dw - crop.left;
3032 crop.left = dw - tmp;
3033 } else if (rot == Surface.ROTATION_270) {
3034 final int tmp = crop.top;
3035 crop.top = crop.left;
3036 crop.left = dh - crop.bottom;
3037 crop.bottom = crop.right;
3038 crop.right = dh - tmp;
3039 }
3040 }
3041
3042 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003043 // Used to indicate the layout is needed.
3044 mTmpWindow = null;
3045
3046 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003047 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003048 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003049 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003050 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003051 w.setDisplayLayoutNeeded();
3052 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003053 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003054
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003055 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003056 mService.mWindowPlacerLocked.performSurfacePlacement();
3057 }
3058 }
3059
Wale Ogunwale1666e312016-12-16 11:27:18 -08003060 void setExitingTokensHasVisible(boolean hasVisible) {
3061 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3062 mExitingTokens.get(i).hasVisible = hasVisible;
3063 }
3064
3065 // Initialize state of exiting applications.
3066 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3067 }
3068
3069 void removeExistingTokensIfPossible() {
3070 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3071 final WindowToken token = mExitingTokens.get(i);
3072 if (!token.hasVisible) {
3073 mExitingTokens.remove(i);
3074 }
3075 }
3076
3077 // Time to remove any exiting applications?
3078 mTaskStackContainers.removeExistingAppTokensIfPossible();
3079 }
3080
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003081 @Override
3082 void onDescendantOverrideConfigurationChanged() {
3083 setLayoutNeeded();
3084 mService.requestTraversal();
3085 }
3086
David Stevens9440dc82017-03-16 19:00:20 -07003087 boolean okToDisplay() {
3088 if (mDisplayId == DEFAULT_DISPLAY) {
3089 return !mService.mDisplayFrozen
3090 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3091 }
3092 return mDisplayInfo.state == Display.STATE_ON;
3093 }
3094
3095 boolean okToAnimate() {
3096 return okToDisplay() &&
3097 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3098 }
3099
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003100 static final class TaskForResizePointSearchResult {
3101 boolean searchDone;
3102 Task taskForResize;
3103
3104 void reset() {
3105 searchDone = false;
3106 taskForResize = null;
3107 }
3108 }
Robert Carr3b716242016-08-16 16:02:21 -07003109
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003110 private static final class ApplySurfaceChangesTransactionState {
3111 boolean displayHasContent;
3112 boolean obscured;
3113 boolean syswin;
3114 boolean focusDisplayed;
3115 float preferredRefreshRate;
3116 int preferredModeId;
3117
3118 void reset() {
3119 displayHasContent = false;
3120 obscured = false;
3121 syswin = false;
3122 focusDisplayed = false;
3123 preferredRefreshRate = 0;
3124 preferredModeId = 0;
3125 }
3126 }
3127
3128 private static final class ScreenshotApplicationState {
3129 WindowState appWin;
3130 int maxLayer;
3131 int minLayer;
3132 boolean screenshotReady;
3133
3134 void reset(boolean screenshotReady) {
3135 appWin = null;
3136 maxLayer = 0;
3137 minLayer = 0;
3138 this.screenshotReady = screenshotReady;
3139 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3140 }
3141 }
3142
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003143 /**
3144 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3145 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3146 * homogeneous children type which is currently required by sub-classes of
3147 * {@link WindowContainer} class.
3148 */
3149 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3150
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003151 DisplayChildWindowContainer(WindowManagerService service) {
3152 super(service);
3153 }
3154
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003155 @Override
3156 boolean fillsParent() {
3157 return true;
3158 }
3159
3160 @Override
3161 boolean isVisible() {
3162 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003163 }
3164 }
3165
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003166 /**
3167 * Window container class that contains all containers on this display relating to Apps.
3168 * I.e Activities.
3169 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003170 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003171 /**
3172 * A control placed at the appropriate level for transitions to occur.
3173 */
chaviw23ee71c2017-12-18 11:29:41 -08003174 SurfaceControl mAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003175
Wale Ogunwale61911492017-10-11 08:50:50 -07003176 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3177 // through the list to find them.
3178 private TaskStack mHomeStack = null;
3179 private TaskStack mPinnedStack = null;
3180 private TaskStack mSplitScreenPrimaryStack = null;
3181
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003182 TaskStackContainers(WindowManagerService service) {
3183 super(service);
3184 }
3185
Wale Ogunwale61911492017-10-11 08:50:50 -07003186 /**
3187 * Returns the topmost stack on the display that is compatible with the input windowing mode
3188 * and activity type. Null is no compatible stack on the display.
3189 */
3190 TaskStack getStack(int windowingMode, int activityType) {
3191 if (activityType == ACTIVITY_TYPE_HOME) {
3192 return mHomeStack;
3193 }
3194 if (windowingMode == WINDOWING_MODE_PINNED) {
3195 return mPinnedStack;
3196 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3197 return mSplitScreenPrimaryStack;
3198 }
3199 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3200 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003201 if (activityType == ACTIVITY_TYPE_UNDEFINED
3202 && windowingMode == stack.getWindowingMode()) {
3203 // Passing in undefined type means we want to match the topmost stack with the
3204 // windowing mode.
3205 return stack;
3206 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003207 if (stack.isCompatible(windowingMode, activityType)) {
3208 return stack;
3209 }
3210 }
3211 return null;
3212 }
3213
3214 @VisibleForTesting
3215 TaskStack getTopStack() {
3216 return mTaskStackContainers.getChildCount() > 0
3217 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3218 }
3219
3220 TaskStack getHomeStack() {
3221 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3222 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3223 }
3224 return mHomeStack;
3225 }
3226
3227 TaskStack getPinnedStack() {
3228 return mPinnedStack;
3229 }
3230
Matthew Ng64e77cf2017-10-31 14:01:31 -07003231 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003232 return mSplitScreenPrimaryStack;
3233 }
3234
Andrii Kulian839def92016-11-02 10:58:58 -07003235 /**
3236 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003237 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003238 */
3239 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003240 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003241 addChild(stack, onTop);
3242 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003243 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003244
Wale Ogunwale61911492017-10-11 08:50:50 -07003245 void onStackWindowingModeChanged(TaskStack stack) {
3246 removeStackReferenceIfNeeded(stack);
3247 addStackReferenceIfNeeded(stack);
3248 if (stack == mPinnedStack && getTopStack() != stack) {
3249 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3250 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3251 }
3252 }
3253
3254 private void addStackReferenceIfNeeded(TaskStack stack) {
3255 if (stack.isActivityTypeHome()) {
3256 if (mHomeStack != null) {
3257 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3258 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3259 }
3260 mHomeStack = stack;
3261 }
3262 final int windowingMode = stack.getWindowingMode();
3263 if (windowingMode == WINDOWING_MODE_PINNED) {
3264 if (mPinnedStack != null) {
3265 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3266 + mPinnedStack + " already exist on display=" + this
3267 + " stack=" + stack);
3268 }
3269 mPinnedStack = stack;
3270 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3271 if (mSplitScreenPrimaryStack != null) {
3272 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3273 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3274 + " already exist on display=" + this + " stack=" + stack);
3275 }
3276 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003277 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003278 }
3279 }
3280
3281 private void removeStackReferenceIfNeeded(TaskStack stack) {
3282 if (stack == mHomeStack) {
3283 mHomeStack = null;
3284 } else if (stack == mPinnedStack) {
3285 mPinnedStack = null;
3286 } else if (stack == mSplitScreenPrimaryStack) {
3287 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003288 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3289 mService.setDockedStackCreateStateLocked(
3290 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3291 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003292 }
Andrii Kulian839def92016-11-02 10:58:58 -07003293 }
3294
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003295 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003296 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3297 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003298 addChild(stack, addIndex);
3299 setLayoutNeeded();
3300 }
3301
Wale Ogunwale61911492017-10-11 08:50:50 -07003302 @Override
3303 protected void removeChild(TaskStack stack) {
3304 super.removeChild(stack);
3305 removeStackReferenceIfNeeded(stack);
3306 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003307
3308 @Override
3309 boolean isOnTop() {
3310 // Considered always on top
3311 return true;
3312 }
3313
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003314 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003315 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003316 if (child.getWindowConfiguration().isAlwaysOnTop()
3317 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003318 // This stack is always-on-top, override the default behavior.
3319 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3320
3321 // Moving to its current position, as we must call super but we don't want to
3322 // perform any meaningful action.
3323 final int currentPosition = mChildren.indexOf(child);
3324 super.positionChildAt(currentPosition, child, false /* includingParents */);
3325 return;
3326 }
3327
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003328 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3329 super.positionChildAt(targetPosition, child, includingParents);
3330
3331 setLayoutNeeded();
3332 }
3333
3334 /**
3335 * When stack is added or repositioned, find a proper position for it.
3336 * This will make sure that pinned stack always stays on top.
3337 * @param requestedPosition Position requested by caller.
3338 * @param stack Stack to be added or positioned.
3339 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3340 * @return The proper position for the stack.
3341 */
3342 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3343 final int topChildPosition = mChildren.size() - 1;
3344 boolean toTop = requestedPosition == POSITION_TOP;
3345 toTop |= adding ? requestedPosition >= topChildPosition + 1
3346 : requestedPosition >= topChildPosition;
3347 int targetPosition = requestedPosition;
3348
Wale Ogunwale61911492017-10-11 08:50:50 -07003349 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003350 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003351 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003352 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003353 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3354 }
3355
3356 // So, stack is moved just below the pinned stack.
3357 // When we're adding a new stack the target is the current pinned stack position.
3358 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003359 // stack, because WindowContainer#positionAt() first removes element and then adds
3360 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003361 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3362 }
3363
3364 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003365 }
3366
3367 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003368 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3369 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003370 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003371 if (super.forAllWindows(callback, traverseTopToBottom)) {
3372 return true;
3373 }
3374 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3375 return true;
3376 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003377 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003378 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3379 return true;
3380 }
3381 if (super.forAllWindows(callback, traverseTopToBottom)) {
3382 return true;
3383 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003384 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003385 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003386 }
3387
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003388 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003389 boolean traverseTopToBottom) {
3390 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3391 // app tokens.
3392 // TODO: Investigate if we need to continue to do this or if we can just process them
3393 // in-order.
3394 if (traverseTopToBottom) {
3395 for (int i = mChildren.size() - 1; i >= 0; --i) {
3396 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3397 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003398 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3399 traverseTopToBottom)) {
3400 return true;
3401 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003402 }
3403 }
3404 } else {
3405 final int count = mChildren.size();
3406 for (int i = 0; i < count; ++i) {
3407 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3408 final int appTokensCount = appTokens.size();
3409 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003410 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3411 traverseTopToBottom)) {
3412 return true;
3413 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003414 }
3415 }
3416 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003417 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003418 }
3419
Wale Ogunwale1666e312016-12-16 11:27:18 -08003420 void setExitingTokensHasVisible(boolean hasVisible) {
3421 for (int i = mChildren.size() - 1; i >= 0; --i) {
3422 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3423 for (int j = appTokens.size() - 1; j >= 0; --j) {
3424 appTokens.get(j).hasVisible = hasVisible;
3425 }
3426 }
3427 }
3428
3429 void removeExistingAppTokensIfPossible() {
3430 for (int i = mChildren.size() - 1; i >= 0; --i) {
3431 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3432 for (int j = appTokens.size() - 1; j >= 0; --j) {
3433 final AppWindowToken token = appTokens.get(j);
3434 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3435 && (!token.mIsExiting || token.isEmpty())) {
3436 // Make sure there is no animation running on this token, so any windows
3437 // associated with it will be removed as soon as their animations are
3438 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003439 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003440 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3441 "performLayout: App token exiting now removed" + token);
3442 token.removeIfPossible();
3443 }
3444 }
3445 }
3446 }
3447
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003448 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003449 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003450 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3451 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003452 // Apps and their containers are not allowed to specify an orientation while the
3453 // docked or freeform stack is visible...except for the home stack/task if the
3454 // docked stack is minimized and it actually set something.
3455 if (mHomeStack != null && mHomeStack.isVisible()
3456 && mDividerControllerLocked.isMinimizedDock()) {
3457 final int orientation = mHomeStack.getOrientation();
3458 if (orientation != SCREEN_ORIENTATION_UNSET) {
3459 return orientation;
3460 }
3461 }
3462 return SCREEN_ORIENTATION_UNSPECIFIED;
3463 }
3464
3465 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003466 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3467 PackageManager.FEATURE_AUTOMOTIVE);
3468 if (isCar) {
3469 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3470 // allow anything but the default orientation.
3471 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3472 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3473 return SCREEN_ORIENTATION_UNSPECIFIED;
3474 }
3475
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003476 if (orientation != SCREEN_ORIENTATION_UNSET
3477 && orientation != SCREEN_ORIENTATION_BEHIND) {
3478 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3479 "App is requesting an orientation, return " + orientation);
3480 return orientation;
3481 }
3482
3483 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003484 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003485 // The next app has not been requested to be visible, so we keep the current orientation
3486 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003487 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003488 }
Robert Carrb1579c82017-09-05 14:54:47 -07003489
3490 @Override
3491 void assignChildLayers(SurfaceControl.Transaction t) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003492 int layer = 0;
Rob Carr6693e062018-01-09 17:10:00 +00003493
3494 // We allow stacks to change visual order from the AM specified order due to
3495 // Z-boosting during animations. However we must take care to ensure TaskStacks
3496 // which are marked as alwaysOnTop remain that way.
3497 for (int i = 0; i < mChildren.size(); i++) {
3498 final TaskStack s = mChildren.get(i);
3499 s.assignChildLayers();
3500 if (!s.needsZBoost() && !s.isAlwaysOnTop()) {
3501 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003502 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003503 }
3504 for (int i = 0; i < mChildren.size(); i++) {
3505 final TaskStack s = mChildren.get(i);
Rob Carr6693e062018-01-09 17:10:00 +00003506 if (s.needsZBoost() && !s.isAlwaysOnTop()) {
3507 s.assignLayer(t, layer++);
3508 }
3509 }
3510 for (int i = 0; i < mChildren.size(); i++) {
3511 final TaskStack s = mChildren.get(i);
3512 if (s.isAlwaysOnTop()) {
3513 s.assignLayer(t, layer++);
3514 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003515 }
3516
Rob Carr6693e062018-01-09 17:10:00 +00003517 // The appropriate place for App-Transitions to occur is right
3518 // above all other animations but still below things in the Picture-and-Picture
3519 // windowing mode.
3520 if (mAppAnimationLayer != null) {
3521 t.setLayer(mAppAnimationLayer, layer++);
3522 }
Robert Carrb1579c82017-09-05 14:54:47 -07003523 }
3524
3525 @Override
chaviw23ee71c2017-12-18 11:29:41 -08003526 SurfaceControl getAppAnimationLayer() {
3527 return mAppAnimationLayer;
3528 }
3529
3530 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003531 void onParentSet() {
3532 super.onParentSet();
3533 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003534 mAppAnimationLayer = makeChildSurface(null)
3535 .setName("animationLayer")
3536 .build();
3537 getPendingTransaction().show(mAppAnimationLayer);
3538 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003539 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003540 mAppAnimationLayer.destroy();
3541 mAppAnimationLayer = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003542 }
3543 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003544 }
3545
Robert Carree4d4b92017-11-22 12:21:46 -08003546 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003547 AboveAppWindowContainers(String name, WindowManagerService service) {
3548 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003549 }
3550
3551 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3552 boolean needAssignIme = imeContainer != null
3553 && imeContainer.getSurfaceControl() != null;
3554 for (int j = 0; j < mChildren.size(); ++j) {
3555 final WindowToken wt = mChildren.get(j);
3556 wt.assignLayer(t, j);
3557 wt.assignChildLayers(t);
3558
3559 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3560 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003561
3562 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3563 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003564 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003565 needAssignIme = false;
3566 }
3567 }
3568 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003569 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003570 }
3571 }
3572 }
3573
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003574 /**
3575 * Window container class that contains all containers on this display that are not related to
3576 * Apps. E.g. status bar.
3577 */
Robert Carree4d4b92017-11-22 12:21:46 -08003578 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003579 /**
3580 * Compares two child window tokens returns -1 if the first is lesser than the second in
3581 * terms of z-order and 1 otherwise.
3582 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003583 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003584 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003585 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3586 token1.mOwnerCanManageAppTokens)
3587 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3588 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003589
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003590 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3591 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3592 return false;
3593 }
3594 final int req = w.mAttrs.screenOrientation;
3595 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3596 || req == SCREEN_ORIENTATION_UNSET) {
3597 return false;
3598 }
3599 return true;
3600 };
3601
Wale Ogunwale3a931692016-11-02 16:49:48 -07003602 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003603 NonAppWindowContainers(String name, WindowManagerService service) {
3604 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003605 mName = name;
3606 }
3607
3608 void addChild(WindowToken token) {
3609 addChild(token, mWindowComparator);
3610 }
3611
3612 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003613 int getOrientation() {
3614 final WindowManagerPolicy policy = mService.mPolicy;
3615 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003616 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003617
3618 if (win != null) {
3619 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003620 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003621 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003622 if (mService.mKeyguardGoingAway) {
3623 // Keyguard can't affect the orientation if it is going away...
3624 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3625 return SCREEN_ORIENTATION_UNSET;
3626 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003627 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003628 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003629 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003630 }
3631
Andrii Kulian8ee72852017-03-10 10:36:45 -08003632 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003633
Jorim Jaggi75520d52017-08-24 17:23:19 +02003634 if (policy.isKeyguardShowingAndNotOccluded()
3635 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003636 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003637 }
3638
3639 return SCREEN_ORIENTATION_UNSET;
3640 }
3641
3642 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003643 String getName() {
3644 return mName;
3645 }
Robert Carr3b716242016-08-16 16:02:21 -07003646 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003647
Robert Carr9034d962018-01-04 18:27:42 -08003648 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
3649 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
3650 super(name, service);
3651 }
3652
3653 @Override
3654 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
3655 }
3656 };
3657
Robert Carrb1579c82017-09-05 14:54:47 -07003658 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3659 return mService.makeSurfaceBuilder(s)
3660 .setParent(mWindowingLayer);
3661 }
3662
3663 @Override
3664 SurfaceSession getSession() {
3665 return mSession;
3666 }
3667
3668 @Override
3669 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003670 SurfaceSession s = child != null ? child.getSession() : getSession();
3671 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003672 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003673
3674 if (child == null) {
3675 return b;
3676 }
3677
Robert Carree4d4b92017-11-22 12:21:46 -08003678 return b.setName(child.getName())
3679 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003680 }
3681
3682 /**
3683 * The makeSurface variants are for use by the window-container
3684 * hierarchy. makeOverlay here is a function for various non windowing
3685 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3686 * and other potpourii.
3687 */
3688 SurfaceControl.Builder makeOverlay() {
3689 return mService.makeSurfaceBuilder(mSession)
3690 .setParent(mOverlayLayer);
3691 }
3692
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09003693 /**
3694 * Reparents the given surface to mOverlayLayer.
3695 */
3696 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
3697 transaction.reparent(surface, mOverlayLayer.getHandle());
3698 }
3699
Robert Carrb1579c82017-09-05 14:54:47 -07003700 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003701 applyMagnificationSpec(getPendingTransaction(), spec);
3702 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003703 }
3704
3705 @Override
3706 void onParentSet() {
3707 // Since we are the top of the SurfaceControl hierarchy here
3708 // we create the root surfaces explicitly rather than chaining
3709 // up as the default implementation in onParentSet does. So we
3710 // explicitly do NOT call super here.
3711 }
3712
3713 @Override
3714 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003715
3716 // These are layers as children of "mWindowingLayer"
3717 mBelowAppWindowsContainers.assignLayer(t, 0);
3718 mTaskStackContainers.assignLayer(t, 1);
3719 mAboveAppWindowsContainers.assignLayer(t, 2);
3720
3721 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003722 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003723
Robert Carree4d4b92017-11-22 12:21:46 -08003724 // In the case where we have an IME target that is not in split-screen
3725 // mode IME assignment is easy. We just need the IME to go directly above
3726 // the target. This way children of the target will naturally go above the IME
3727 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003728 //
Robert Carree4d4b92017-11-22 12:21:46 -08003729 // In the case of split-screen windowing mode, we need to elevate the IME above the
3730 // docked divider while keeping the app itself below the docked divider, so instead
3731 // we use relative layering of the IME targets child windows, and place the
3732 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003733 //
Robert Carree4d4b92017-11-22 12:21:46 -08003734 // In the case where we have no IME target we assign it where it's base layer would
3735 // place it in the AboveAppWindowContainers.
3736 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3737 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003738 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003739 // TODO: We need to use an extra level on the app surface to ensure
3740 // this is always above SurfaceView but always below attached window.
3741 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003742 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003743 }
3744
3745 // Above we have assigned layers to our children, now we ask them to assign
3746 // layers to their children.
3747 mBelowAppWindowsContainers.assignChildLayers(t);
3748 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003749 mAboveAppWindowsContainers.assignChildLayers(t,
3750 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003751 mImeWindowsContainers.assignChildLayers(t);
3752 }
3753
3754 /**
3755 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3756 * that the IME target is one of the docked applications. We'd like the docked divider to be
3757 * above both of the applications, and we'd like the IME to be above the docked divider.
3758 * However we need child windows of the applications to be above the IME (Text drag handles).
3759 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3760 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3761 * with {@link #WindowState#assignLayer}
3762 */
3763 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3764 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3765 }
3766
3767 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +01003768 public void destroyAfterPendingTransaction(SurfaceControl surface) {
Robert Carrb1579c82017-09-05 14:54:47 -07003769 mPendingDestroyingSurfaces.add(surface);
3770 }
3771
3772 /**
3773 * Destroys any surfaces that have been put into the pending list with
Jorim Jaggia5e10572017-11-15 14:36:26 +01003774 * {@link #destroyAfterPendingTransaction}.
Robert Carrb1579c82017-09-05 14:54:47 -07003775 */
3776 void onPendingTransactionApplied() {
3777 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3778 mPendingDestroyingSurfaces.get(i).destroy();
3779 }
3780 mPendingDestroyingSurfaces.clear();
3781 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01003782
3783 @Override
3784 void prepareSurfaces() {
3785 final ScreenRotationAnimation screenRotationAnimation =
3786 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
3787 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
3788 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
3789 mPendingTransaction.setMatrix(mWindowingLayer,
3790 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
3791 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
3792 mPendingTransaction.setPosition(mWindowingLayer,
3793 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
3794 mPendingTransaction.setAlpha(mWindowingLayer,
3795 screenRotationAnimation.getEnterTransformation().getAlpha());
3796 }
3797 super.prepareSurfaces();
3798 }
Craig Mautner59c00972012-07-30 12:10:24 -07003799}