blob: 4c5520fac2cb6d1bef3f1325c34af2a06b39b07a [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) {
700 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
701 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
702 mTmpUpdateAllDrawn.add(atoken);
703 }
704 }
705
706 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
707 && w.isDisplayedLw()) {
708 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
709 }
710
711 w.updateResizingWindowIfNeeded();
712 };
713
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800714 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800715 * Create new {@link DisplayContent} instance, add itself to the root window container and
716 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700717 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800718 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700719 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
720 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700721 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700722 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700723 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100724 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800725 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
726 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
727 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
728 + " new=" + display);
729 }
730
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700731 mDisplay = display;
732 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700733 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700734 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700735 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700736 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000737 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700738 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800739 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700740 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700741
Robert Carrb1579c82017-09-05 14:54:47 -0700742 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
743
744 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
745 .setSize(mSurfaceSize, mSurfaceSize)
746 .setOpaque(true);
747 mWindowingLayer = b.setName("Display Root").build();
748 mOverlayLayer = b.setName("Display Overlays").build();
749
Robert Carrf59b8dd2017-10-02 18:58:36 -0700750 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700751 .setLayerStack(mWindowingLayer, mDisplayId)
752 .show(mWindowingLayer)
753 .setLayer(mOverlayLayer, 1)
754 .setLayerStack(mOverlayLayer, mDisplayId)
755 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700756 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700757
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700758 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700759 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700760 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700761 super.addChild(mAboveAppWindowsContainers, null);
762 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800763
764 // Add itself as a child to the root container.
765 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700766
767 // TODO(b/62541591): evaluate whether this is the best spot to declare the
768 // {@link DisplayContent} ready for use.
769 mDisplayReady = true;
770 }
771
772 boolean isReady() {
773 // The display is ready when the system and the individual display are both ready.
774 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700775 }
776
777 int getDisplayId() {
778 return mDisplayId;
779 }
780
Wale Ogunwale02319a62016-09-26 15:21:22 -0700781 WindowToken getWindowToken(IBinder binder) {
782 return mTokenMap.get(binder);
783 }
784
785 AppWindowToken getAppWindowToken(IBinder binder) {
786 final WindowToken token = getWindowToken(binder);
787 if (token == null) {
788 return null;
789 }
790 return token.asAppWindowToken();
791 }
792
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700793 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700794 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
795 if (dc != null) {
796 // We currently don't support adding a window token to the display if the display
797 // already has the binder mapped to another token. If there is a use case for supporting
798 // this moving forward we will either need to merge the WindowTokens some how or have
799 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700800 throw new IllegalArgumentException("Can't map token=" + token + " to display="
801 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700802 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700803 if (binder == null) {
804 throw new IllegalArgumentException("Can't map token=" + token + " to display="
805 + getName() + " binder is null");
806 }
807 if (token == null) {
808 throw new IllegalArgumentException("Can't map null token to display="
809 + getName() + " binder=" + binder);
810 }
811
Wale Ogunwale02319a62016-09-26 15:21:22 -0700812 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700813
814 if (token.asAppWindowToken() == null) {
815 // Add non-app token to container hierarchy on the display. App tokens are added through
816 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700817 switch (token.windowType) {
818 case TYPE_WALLPAPER:
819 mBelowAppWindowsContainers.addChild(token);
820 break;
821 case TYPE_INPUT_METHOD:
822 case TYPE_INPUT_METHOD_DIALOG:
823 mImeWindowsContainers.addChild(token);
824 break;
825 default:
826 mAboveAppWindowsContainers.addChild(token);
827 break;
828 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700829 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700830 }
831
832 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700833 final WindowToken token = mTokenMap.remove(binder);
834 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800835 token.setExiting();
836 }
837 return token;
838 }
839
840 /** Changes the display the input window token is housed on to this one. */
841 void reParentWindowToken(WindowToken token) {
842 final DisplayContent prevDc = token.getDisplayContent();
843 if (prevDc == this) {
844 return;
845 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800846 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
847 && token.asAppWindowToken() == null) {
848 // Removed the token from the map, but made sure it's not an app token before removing
849 // from parent.
850 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700851 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800852
853 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700854 }
855
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700856 void removeAppToken(IBinder binder) {
857 final WindowToken token = removeWindowToken(binder);
858 if (token == null) {
859 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
860 return;
861 }
862
863 final AppWindowToken appToken = token.asAppWindowToken();
864
865 if (appToken == null) {
866 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
867 return;
868 }
869
870 appToken.onRemovedFromDisplay();
871 }
872
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700873 Display getDisplay() {
874 return mDisplay;
875 }
876
Craig Mautner59c00972012-07-30 12:10:24 -0700877 DisplayInfo getDisplayInfo() {
878 return mDisplayInfo;
879 }
880
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700881 DisplayMetrics getDisplayMetrics() {
882 return mDisplayMetrics;
883 }
884
Andrii Kulian8ee72852017-03-10 10:36:45 -0800885 int getRotation() {
886 return mRotation;
887 }
888
889 void setRotation(int newRotation) {
890 mRotation = newRotation;
891 }
892
893 int getLastOrientation() {
894 return mLastOrientation;
895 }
896
897 void setLastOrientation(int orientation) {
898 mLastOrientation = orientation;
899 }
900
901 boolean getAltOrientation() {
902 return mAltOrientation;
903 }
904
905 void setAltOrientation(boolean altOrientation) {
906 mAltOrientation = altOrientation;
907 }
908
909 int getLastWindowForcedOrientation() {
910 return mLastWindowForcedOrientation;
911 }
912
Andrii Kulian06d07d62017-03-14 11:11:47 -0700913 /**
914 * Update rotation of the display.
915 *
916 * Returns true if the rotation has been changed. In this case YOU MUST CALL
917 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
918 */
919 boolean updateRotationUnchecked(boolean inTransaction) {
920 if (mService.mDeferredRotationPauseCount > 0) {
921 // Rotation updates have been paused temporarily. Defer the update until
922 // updates have been resumed.
923 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
924 return false;
925 }
926
927 ScreenRotationAnimation screenRotationAnimation =
928 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
929 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
930 // Rotation updates cannot be performed while the previous rotation change
931 // animation is still in progress. Skip this update. We will try updating
932 // again after the animation is finished and the display is unfrozen.
933 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
934 return false;
935 }
936 if (mService.mDisplayFrozen) {
937 // Even if the screen rotation animation has finished (e.g. isAnimating
938 // returns false), there is still some time where we haven't yet unfrozen
939 // the display. We also need to abort rotation here.
940 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
941 "Deferring rotation, still finishing previous rotation");
942 return false;
943 }
944
945 if (!mService.mDisplayEnabled) {
946 // No point choosing a rotation if the display is not enabled.
947 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
948 return false;
949 }
950
951 final int oldRotation = mRotation;
952 final int lastOrientation = mLastOrientation;
953 final boolean oldAltOrientation = mAltOrientation;
954 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700955 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700956 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700957
Robert Carr0e007272017-10-02 13:00:55 -0700958 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700959 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
960 if (seamlessRotated != null) {
961 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
962 // to complete (that is, waiting for windows to redraw). It's tempting to check
963 // w.mSeamlessRotationCount but that could be incorrect in the case of
964 // window-removal.
965 return false;
966 }
Robert Carr0e007272017-10-02 13:00:55 -0700967
968 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700969 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -0700970 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -0700971 mayRotateSeamlessly = false;
972 }
973 for (int i = 0; i < mService.mSessions.size(); i++) {
974 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
975 mayRotateSeamlessly = false;
976 break;
977 }
978 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700979 }
Robert Carr0e007272017-10-02 13:00:55 -0700980 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700981
982 // TODO: Implement forced rotation changes.
983 // Set mAltOrientation to indicate that the application is receiving
984 // an orientation that has different metrics than it expected.
985 // eg. Portrait instead of Landscape.
986
987 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
988 lastOrientation, rotation);
989
990 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
991 + ", got rotation " + rotation + " which has "
992 + (altOrientation ? "incompatible" : "compatible") + " metrics");
993
994 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
995 // No change.
996 return false;
997 }
998
999 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1000 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1001 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1002
1003 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1004 mService.mWaitingForConfig = true;
1005 }
1006
1007 mRotation = rotation;
1008 mAltOrientation = altOrientation;
1009 if (isDefaultDisplay) {
1010 mService.mPolicy.setRotationLw(rotation);
1011 }
1012
1013 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1014 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1015 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1016 WINDOW_FREEZE_TIMEOUT_DURATION);
1017
1018 setLayoutNeeded();
1019 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001020 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001021
1022 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001023 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001024 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1025 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1026 mDisplayId);
1027 } else {
1028 // The screen rotation animation uses a screenshot to freeze the screen
1029 // while windows resize underneath.
1030 // When we are rotating seamlessly, we allow the elements to transition
1031 // to their rotated state independently and without a freeze required.
1032 screenRotationAnimation = null;
1033
1034 // We have to reset this in case a window was removed before it
1035 // finished seamless rotation.
1036 mService.mSeamlessRotationCount = 0;
1037 }
1038
1039 // We need to update our screen size information to match the new rotation. If the rotation
1040 // has actually changed then this method will return true and, according to the comment at
1041 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1042 // By updating the Display info here it will be available to
1043 // #computeScreenConfiguration() later.
1044 updateDisplayAndOrientation(getConfiguration().uiMode);
1045
1046 if (!inTransaction) {
1047 if (SHOW_TRANSACTIONS) {
1048 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1049 }
1050 mService.openSurfaceTransaction();
1051 }
1052 try {
1053 // NOTE: We disable the rotation in the emulator because
1054 // it doesn't support hardware OpenGL emulation yet.
1055 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1056 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001057 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001058 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1059 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1060 mService.scheduleAnimationLocked();
1061 }
1062 }
1063
1064 if (rotateSeamlessly) {
1065 forAllWindows(w -> {
1066 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1067 }, true /* traverseTopToBottom */);
1068 }
1069
1070 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1071 } finally {
1072 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001073 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001074 if (SHOW_LIGHT_TRANSACTIONS) {
1075 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1076 }
1077 }
1078 }
1079
1080 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001081 if (w.mHasSurface && !rotateSeamlessly) {
1082 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001083 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001084 mService.mRoot.mOrientationChangeComplete = false;
1085 w.mLastFreezeDuration = 0;
1086 }
1087 w.mReportOrientationChanged = true;
1088 }, true /* traverseTopToBottom */);
1089
1090 if (rotateSeamlessly) {
1091 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1092 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1093 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1094 }
1095
1096 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1097 final WindowManagerService.RotationWatcher rotationWatcher
1098 = mService.mRotationWatchers.get(i);
1099 if (rotationWatcher.mDisplayId == mDisplayId) {
1100 try {
1101 rotationWatcher.mWatcher.onRotationChanged(rotation);
1102 } catch (RemoteException e) {
1103 // Ignore
1104 }
1105 }
1106 }
1107
1108 // TODO (multi-display): Magnification is supported only for the default display.
1109 // Announce rotation only if we will not animate as we already have the
1110 // windows in final state. Otherwise, we make this call at the rotation end.
1111 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1112 && isDefaultDisplay) {
1113 mService.mAccessibilityController.onRotationChangedLocked(this);
1114 }
1115
1116 return true;
1117 }
1118
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001119 void configureDisplayPolicy() {
1120 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1121 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1122
1123 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1124 }
1125
Andrii Kulian06d07d62017-03-14 11:11:47 -07001126 /**
1127 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1128 * changed.
1129 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1130 */
1131 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1132 // Use the effective "visual" dimensions based on current rotation
1133 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1134 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1135 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1136 int dw = realdw;
1137 int dh = realdh;
1138
1139 if (mAltOrientation) {
1140 if (realdw > realdh) {
1141 // Turn landscape into portrait.
1142 int maxw = (int)(realdh/1.3f);
1143 if (maxw < realdw) {
1144 dw = maxw;
1145 }
1146 } else {
1147 // Turn portrait into landscape.
1148 int maxh = (int)(realdw/1.3f);
1149 if (maxh < realdh) {
1150 dh = maxh;
1151 }
1152 }
1153 }
1154
1155 // Update application display metrics.
1156 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1157 mDisplayId);
1158 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1159 mDisplayId);
1160 mDisplayInfo.rotation = mRotation;
1161 mDisplayInfo.logicalWidth = dw;
1162 mDisplayInfo.logicalHeight = dh;
1163 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1164 mDisplayInfo.appWidth = appWidth;
1165 mDisplayInfo.appHeight = appHeight;
1166 if (isDefaultDisplay) {
1167 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1168 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1169 }
Adrian Roos1cf585052018-01-03 18:43:27 +01001170 mDisplayInfo.displayCutout = calculateDisplayCutoutForCurrentRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001171 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1172 if (mDisplayScalingDisabled) {
1173 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1174 } else {
1175 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1176 }
1177
1178 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1179 mDisplayInfo);
1180
1181 mBaseDisplayRect.set(0, 0, dw, dh);
1182
1183 if (isDefaultDisplay) {
1184 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1185 mCompatDisplayMetrics);
1186 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001187
1188 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001189 return mDisplayInfo;
1190 }
1191
Adrian Roos1cf585052018-01-03 18:43:27 +01001192 DisplayCutout calculateDisplayCutoutForCurrentRotation() {
1193 final DisplayCutout cutout = mInitialDisplayCutout;
1194 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT || mRotation == ROTATION_0) {
1195 return cutout;
1196 }
1197 final Path bounds = cutout.getBounds().getBoundaryPath();
1198 transformPhysicalToLogicalCoordinates(mRotation, mInitialDisplayWidth,
1199 mInitialDisplayHeight, mTmpMatrix);
1200 bounds.transform(mTmpMatrix);
1201 return DisplayCutout.fromBounds(bounds);
1202 }
1203
Andrii Kulian06d07d62017-03-14 11:11:47 -07001204 /**
1205 * Compute display configuration based on display properties and policy settings.
1206 * Do not call if mDisplayReady == false.
1207 */
1208 void computeScreenConfiguration(Configuration config) {
1209 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1210
1211 final int dw = displayInfo.logicalWidth;
1212 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001213 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1214 // TODO: Probably best to set this based on some setting in the display content object,
1215 // so the display can be configured for things like fullscreen.
1216 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001217
Andrii Kulian06d07d62017-03-14 11:11:47 -07001218 config.screenWidthDp =
1219 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1220 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1221 config.screenHeightDp =
1222 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1223 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001224
1225 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1226 final int leftInset = mTmpRect.left;
1227 final int topInset = mTmpRect.top;
1228 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001229 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1230 leftInset + displayInfo.appWidth /* right */,
1231 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001232 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1233 || displayInfo.rotation == Surface.ROTATION_270);
1234
1235 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1236 mDisplayMetrics.density, config);
1237
1238 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1239 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1240 ? Configuration.SCREENLAYOUT_ROUND_YES
1241 : Configuration.SCREENLAYOUT_ROUND_NO);
1242
1243 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1244 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1245 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1246 dh, mDisplayId);
1247 config.densityDpi = displayInfo.logicalDensityDpi;
1248
1249 config.colorMode =
1250 (displayInfo.isHdr()
1251 ? Configuration.COLOR_MODE_HDR_YES
1252 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001253 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001254 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1255 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1256
1257 // Update the configuration based on available input devices, lid switch,
1258 // and platform configuration.
1259 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1260 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1261 config.navigation = Configuration.NAVIGATION_NONAV;
1262
1263 int keyboardPresence = 0;
1264 int navigationPresence = 0;
1265 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1266 final int len = devices != null ? devices.length : 0;
1267 for (int i = 0; i < len; i++) {
1268 InputDevice device = devices[i];
1269 if (!device.isVirtual()) {
1270 final int sources = device.getSources();
1271 final int presenceFlag = device.isExternal() ?
1272 WindowManagerPolicy.PRESENCE_EXTERNAL :
1273 WindowManagerPolicy.PRESENCE_INTERNAL;
1274
1275 // TODO(multi-display): Configure on per-display basis.
1276 if (mService.mIsTouchDevice) {
1277 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1278 InputDevice.SOURCE_TOUCHSCREEN) {
1279 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1280 }
1281 } else {
1282 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1283 }
1284
1285 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1286 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1287 navigationPresence |= presenceFlag;
1288 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1289 && config.navigation == Configuration.NAVIGATION_NONAV) {
1290 config.navigation = Configuration.NAVIGATION_DPAD;
1291 navigationPresence |= presenceFlag;
1292 }
1293
1294 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1295 config.keyboard = Configuration.KEYBOARD_QWERTY;
1296 keyboardPresence |= presenceFlag;
1297 }
1298 }
1299 }
1300
1301 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1302 config.navigation = Configuration.NAVIGATION_DPAD;
1303 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1304 }
1305
1306 // Determine whether a hard keyboard is available and enabled.
1307 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1308 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1309 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1310 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1311 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1312 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1313 }
1314
1315 // Let the policy update hidden states.
1316 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1317 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1318 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1319 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1320 }
1321
1322 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1323 int displayId) {
1324 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1325 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1326 final int unrotDw, unrotDh;
1327 if (rotated) {
1328 unrotDw = dh;
1329 unrotDh = dw;
1330 } else {
1331 unrotDw = dw;
1332 unrotDh = dh;
1333 }
1334 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1335 displayId);
1336 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1337 displayId);
1338 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1339 displayId);
1340 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1341 displayId);
1342 return sw;
1343 }
1344
1345 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1346 DisplayMetrics dm, int dw, int dh, int displayId) {
1347 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1348 displayId);
1349 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1350 uiMode, displayId);
1351 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1352 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1353 if (curSize == 0 || size < curSize) {
1354 curSize = size;
1355 }
1356 return curSize;
1357 }
1358
1359 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1360 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1361
1362 // We need to determine the smallest width that will occur under normal
1363 // operation. To this, start with the base screen size and compute the
1364 // width under the different possible rotations. We need to un-rotate
1365 // the current screen dimensions before doing this.
1366 int unrotDw, unrotDh;
1367 if (rotated) {
1368 unrotDw = dh;
1369 unrotDh = dw;
1370 } else {
1371 unrotDw = dw;
1372 unrotDh = dh;
1373 }
1374 displayInfo.smallestNominalAppWidth = 1<<30;
1375 displayInfo.smallestNominalAppHeight = 1<<30;
1376 displayInfo.largestNominalAppWidth = 0;
1377 displayInfo.largestNominalAppHeight = 0;
1378 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1379 unrotDh);
1380 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1381 unrotDw);
1382 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1383 unrotDh);
1384 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1385 unrotDw);
1386 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1387 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1388 displayId);
1389 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1390 displayId);
1391 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1392 displayId);
1393 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1394 displayId);
1395 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1396 outConfig.screenLayout = sl;
1397 }
1398
1399 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1400 int uiMode, int displayId) {
1401 // Get the app screen size at this rotation.
1402 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1403 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1404
1405 // Compute the screen layout size class for this rotation.
1406 int longSize = w;
1407 int shortSize = h;
1408 if (longSize < shortSize) {
1409 int tmp = longSize;
1410 longSize = shortSize;
1411 shortSize = tmp;
1412 }
1413 longSize = (int)(longSize/density);
1414 shortSize = (int)(shortSize/density);
1415 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1416 }
1417
1418 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1419 int uiMode, int dw, int dh) {
1420 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1421 displayId);
1422 if (width < displayInfo.smallestNominalAppWidth) {
1423 displayInfo.smallestNominalAppWidth = width;
1424 }
1425 if (width > displayInfo.largestNominalAppWidth) {
1426 displayInfo.largestNominalAppWidth = width;
1427 }
1428 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1429 displayId);
1430 if (height < displayInfo.smallestNominalAppHeight) {
1431 displayInfo.smallestNominalAppHeight = height;
1432 }
1433 if (height > displayInfo.largestNominalAppHeight) {
1434 displayInfo.largestNominalAppHeight = height;
1435 }
1436 }
1437
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001438 DockedStackDividerController getDockedDividerController() {
1439 return mDividerControllerLocked;
1440 }
1441
Winson Chung655332c2016-10-31 13:14:28 -07001442 PinnedStackController getPinnedStackController() {
1443 return mPinnedStackControllerLocked;
1444 }
1445
Jeff Browna506a6e2013-06-04 00:02:38 -07001446 /**
1447 * Returns true if the specified UID has access to this display.
1448 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001449 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001450 return mDisplay.hasAccess(uid);
1451 }
1452
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001453 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001454 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001455 }
1456
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001457 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001458 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001459 }
1460
Wale Ogunwale61911492017-10-11 08:50:50 -07001461 /**
1462 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1463 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001464 TaskStack getSplitScreenPrimaryStack() {
1465 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001466 return (stack != null && stack.isVisible()) ? stack : null;
1467 }
1468
1469 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001470 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001471 * not visible.
1472 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001473 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1474 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001475 }
1476
1477 TaskStack getPinnedStack() {
1478 return mTaskStackContainers.getPinnedStack();
1479 }
1480
1481 private boolean hasPinnedStack() {
1482 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001483 }
1484
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001485 /**
1486 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1487 * Null is no compatible stack on the display.
1488 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001489 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001490 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1491 }
1492
1493 /**
1494 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1495 * activity type. Null is no compatible stack on the display.
1496 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001497 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001498 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001499 }
1500
Bryce Lee48f4b572017-04-10 10:54:15 -07001501 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001502 TaskStack getTopStack() {
1503 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001504 }
1505
Wale Ogunwale61911492017-10-11 08:50:50 -07001506 void onStackWindowingModeChanged(TaskStack stack) {
1507 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001508 }
1509
Andrii Kulian441e4492016-09-29 15:25:00 -07001510 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001511 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001512 super.onConfigurationChanged(newParentConfig);
1513
Andrii Kulian3a507b52016-09-19 18:14:12 -07001514 // The display size information is heavily dependent on the resources in the current
1515 // configuration, so we need to reconfigure it every time the configuration changes.
1516 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1517 mService.reconfigureDisplayLocked(this);
1518
Bryce Leef3c6a472017-11-14 14:53:06 -08001519 final DockedStackDividerController dividerController = getDockedDividerController();
1520
1521 if (dividerController != null) {
1522 getDockedDividerController().onConfigurationChanged();
1523 }
1524
1525 final PinnedStackController pinnedStackController = getPinnedStackController();
1526
1527 if (pinnedStackController != null) {
1528 getPinnedStackController().onConfigurationChanged();
1529 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001530 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001531
Andrii Kulian441e4492016-09-29 15:25:00 -07001532 /**
1533 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1534 * bounds were updated.
1535 */
1536 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001537 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1538 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001539 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001540 changedStackList.add(stack.mStackId);
1541 }
1542 }
Winson Chung32c566f2017-04-11 18:31:21 -07001543
1544 // If there was no pinned stack, we still need to notify the controller of the display info
1545 // update as a result of the config change. We do this here to consolidate the flow between
1546 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001547 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001548 mPinnedStackControllerLocked.onDisplayInfoChanged();
1549 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001550 }
1551
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001552 @Override
1553 boolean fillsParent() {
1554 return true;
1555 }
1556
1557 @Override
1558 boolean isVisible() {
1559 return true;
1560 }
1561
1562 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001563 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001564 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001565 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001566 }
1567
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001568 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001569 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1570 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1571 // target, or here in order if there isn't an IME target.
1572 if (traverseTopToBottom) {
1573 for (int i = mChildren.size() - 1; i >= 0; --i) {
1574 final DisplayChildWindowContainer child = mChildren.get(i);
1575 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1576 // In this case the Ime windows will be processed above their target so we skip
1577 // here.
1578 continue;
1579 }
1580 if (child.forAllWindows(callback, traverseTopToBottom)) {
1581 return true;
1582 }
1583 }
1584 } else {
1585 final int count = mChildren.size();
1586 for (int i = 0; i < count; i++) {
1587 final DisplayChildWindowContainer child = mChildren.get(i);
1588 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1589 // In this case the Ime windows will be processed above their target so we skip
1590 // here.
1591 continue;
1592 }
1593 if (child.forAllWindows(callback, traverseTopToBottom)) {
1594 return true;
1595 }
1596 }
1597 }
1598 return false;
1599 }
1600
1601 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1602 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1603 }
1604
Wale Ogunwale399c8692017-05-08 14:22:42 -07001605 @Override
1606 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001607 final WindowManagerPolicy policy = mService.mPolicy;
1608
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001609 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001610 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001611 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001612 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001613 // If the display is frozen, some activities may be in the middle of restarting, and
1614 // thus have removed their old window. If the window has the flag to hide the lock
1615 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1616 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001617 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001618 } else if (policy.isKeyguardLocked()) {
1619 // Use the last orientation the while the display is frozen with the keyguard
1620 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1621 // window. We don't want to check the show when locked window directly though as
1622 // things aren't stable while the display is frozen, for example the window could be
1623 // momentarily unavailable due to activity relaunch.
1624 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001625 + "return " + mLastOrientation);
1626 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001627 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001628 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001629 final int orientation = mAboveAppWindowsContainers.getOrientation();
1630 if (orientation != SCREEN_ORIENTATION_UNSET) {
1631 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001632 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001633 }
1634
Wale Ogunwale399c8692017-05-08 14:22:42 -07001635 // Top system windows are not requesting an orientation. Start searching from apps.
1636 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001637 }
1638
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001639 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001640 // Check if display metrics changed and update base values if needed.
1641 updateBaseDisplayMetricsIfNeeded();
1642
Craig Mautner722285e2012-09-07 13:55:58 -07001643 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001644 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001645
Wale Ogunwale61911492017-10-11 08:50:50 -07001646 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1647 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001648 }
Craig Mautner722285e2012-09-07 13:55:58 -07001649 }
1650
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001651 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001652 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1653 if (displayManagerInternal != null) {
1654 // Bootstrap the default logical display from the display manager.
1655 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1656 if (newDisplayInfo != null) {
1657 mDisplayInfo.copyFrom(newDisplayInfo);
1658 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001659 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001660
Andrii Kuliancd097992017-03-23 18:31:59 -07001661 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1662 mDisplayInfo.logicalDensityDpi);
1663 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1664 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1665 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001666 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001667 }
1668
Andrii Kuliancd097992017-03-23 18:31:59 -07001669 /**
1670 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1671 * values.
1672 */
1673 private void updateBaseDisplayMetricsIfNeeded() {
1674 // Get real display metrics without overrides from WM.
1675 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1676 final int orientation = mDisplayInfo.rotation;
1677 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1678 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1679 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1680 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001681 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001682
1683 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1684 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001685 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1686 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001687
1688 if (displayMetricsChanged) {
1689 // Check if display size or density is forced.
1690 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1691 || mBaseDisplayHeight != mInitialDisplayHeight;
1692 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1693
1694 // If there is an override set for base values - use it, otherwise use new values.
1695 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1696 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1697 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1698
1699 // Real display metrics changed, so we should also update initial values.
1700 mInitialDisplayWidth = newWidth;
1701 mInitialDisplayHeight = newHeight;
1702 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001703 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001704 mService.reconfigureDisplayLocked(this);
1705 }
1706 }
1707
Bryce Lee27cec322017-03-21 09:41:37 -07001708 /** Sets the maximum width the screen resolution can be */
1709 void setMaxUiWidth(int width) {
1710 if (DEBUG_DISPLAY) {
1711 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1712 }
1713
1714 mMaxUiWidth = width;
1715
1716 // Update existing metrics.
1717 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1718 }
1719
1720 /** Update base (override) display metrics. */
1721 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1722 mBaseDisplayWidth = baseWidth;
1723 mBaseDisplayHeight = baseHeight;
1724 mBaseDisplayDensity = baseDensity;
1725
1726 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1727 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1728 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1729 mBaseDisplayWidth = mMaxUiWidth;
1730
1731 if (DEBUG_DISPLAY) {
1732 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1733 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1734 + " on display:" + getDisplayId());
1735 }
1736 }
1737
1738 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001739
1740 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001741 }
1742
Wale Ogunwaledb506192017-12-08 10:57:32 -08001743 void getStableRect(Rect out) {
1744 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001745 }
1746
Wale Ogunwale61911492017-10-11 08:50:50 -07001747 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001748 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1749 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001750
Wale Ogunwale61911492017-10-11 08:50:50 -07001751 final TaskStack stack = new TaskStack(mService, stackId, controller);
1752 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001753 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001754 }
1755
Andrii Kulian51c1b672017-04-07 18:39:32 -07001756 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001757 final DisplayContent prevDc = stack.getDisplayContent();
1758 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001759 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1760 + " which is not currently attached to any display");
1761 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001762 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001763 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1764 + " to its current displayId=" + mDisplayId);
1765 }
1766
Wale Ogunwale61911492017-10-11 08:50:50 -07001767 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001768 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001769 }
1770
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001771 @Override
1772 protected void addChild(DisplayChildWindowContainer child,
1773 Comparator<DisplayChildWindowContainer> comparator) {
1774 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1775 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001776
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001777 @Override
1778 protected void addChild(DisplayChildWindowContainer child, int index) {
1779 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1780 }
1781
1782 @Override
1783 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001784 // Only allow removal of direct children from this display if the display is in the process
1785 // of been removed.
1786 if (mRemovingDisplay) {
1787 super.removeChild(child);
1788 return;
1789 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001790 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001791 }
1792
Andrii Kuliand2765632016-12-12 22:26:34 -08001793 @Override
1794 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1795 // Children of the display are statically ordered, so the real intention here is to perform
1796 // the operation on the display and not the static direct children.
1797 getParent().positionChildAt(position, this, includingParents);
1798 }
1799
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001800 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001801 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1802 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001803 final int taskId = stack.taskIdFromPoint(x, y);
1804 if (taskId != -1) {
1805 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001806 }
1807 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001808 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001809 }
1810
Chong Zhang8e89b312015-09-09 15:09:30 -07001811 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001812 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001813 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001814 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001815 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001816 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001817 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001818 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1819 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001820 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001821 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001822 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001823
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001824 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1825 if (mTmpTaskForResizePointSearchResult.searchDone) {
1826 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001827 }
1828 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001829 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001830 }
1831
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001832 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001833 // The provided task is the task on this display with focus, so if WindowManagerService's
1834 // focused app is not on this display, focusedTask will be null.
1835 if (focusedTask == null) {
1836 mTouchExcludeRegion.setEmpty();
1837 } else {
1838 mTouchExcludeRegion.set(mBaseDisplayRect);
1839 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1840 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001841 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1842 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001843 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1844 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001845 }
1846 // If we removed the focused task above, add it back and only leave its
1847 // outside touch area in the exclusion. TapDectector is not interested in
1848 // any touch inside the focused task itself.
1849 if (!mTmpRect2.isEmpty()) {
1850 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1851 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001852 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001853 final WindowState inputMethod = mService.mInputMethodWindow;
1854 if (inputMethod != null && inputMethod.isVisibleLw()) {
1855 // If the input method is visible and the user is typing, we don't want these touch
1856 // events to be intercepted and used to change focus. This would likely cause a
1857 // disappearance of the input method.
1858 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001859 if (inputMethod.getDisplayId() == mDisplayId) {
1860 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1861 } else {
1862 // IME is on a different display, so we need to update its tap detector.
1863 // TODO(multidisplay): Remove when IME will always appear on same display.
1864 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1865 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001866 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001867 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1868 WindowState win = mTapExcludedWindows.get(i);
1869 win.getTouchableRegion(mTmpRegion);
1870 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1871 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001872 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001873 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001874 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001875 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001876 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1877 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001878 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001879 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001880 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001881 }
1882
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001883 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001884 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001885 super.switchUser();
1886 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001887 }
1888
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001889 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001890 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1891 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001892 }
1893 }
1894
Wale Ogunwale10124582016-09-15 20:25:50 -07001895 @Override
1896 void removeIfPossible() {
1897 if (isAnimating()) {
1898 mDeferredRemoval = true;
1899 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001900 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001901 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001902 }
1903
Wale Ogunwale10124582016-09-15 20:25:50 -07001904 @Override
1905 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001906 mRemovingDisplay = true;
1907 try {
1908 super.removeImmediately();
1909 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001910 if (mService.canDispatchPointerEvents()) {
1911 if (mTapDetector != null) {
1912 mService.unregisterPointerEventListener(mTapDetector);
1913 }
1914 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1915 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1916 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001917 }
Jorim Jaggia3844032018-01-03 15:54:43 +01001918 mService.mAnimator.removeDisplayLocked(mDisplayId);
1919
Robert Carrb1579c82017-09-05 14:54:47 -07001920 // The pending transaction won't be applied so we should
1921 // just clean up any surfaces pending destruction.
1922 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001923 } finally {
1924 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001925 }
Craig Mautner95da1082014-02-24 17:54:35 -08001926 }
1927
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001928 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001929 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001930 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001931 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1932
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001933 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001934 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001935 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001936 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001937 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001938 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001939 }
1940
Andrii Kulian0214ed92017-05-16 13:44:05 -07001941 /** @return 'true' if removal of this display content is deferred due to active animation. */
1942 boolean isRemovalDeferred() {
1943 return mDeferredRemoval;
1944 }
1945
Wale Ogunwale10124582016-09-15 20:25:50 -07001946 boolean animateForIme(float interpolatedValue, float animationTarget,
1947 float dividerAnimationTarget) {
1948 boolean updated = false;
1949
Wale Ogunwale61911492017-10-11 08:50:50 -07001950 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1951 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001952 if (stack == null || !stack.isAdjustedForIme()) {
1953 continue;
1954 }
1955
1956 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1957 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1958 updated = true;
1959 } else {
1960 mDividerControllerLocked.mLastAnimationProgress =
1961 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1962 mDividerControllerLocked.mLastDividerProgress =
1963 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1964 updated |= stack.updateAdjustForIme(
1965 mDividerControllerLocked.mLastAnimationProgress,
1966 mDividerControllerLocked.mLastDividerProgress,
1967 false /* force */);
1968 }
1969 if (interpolatedValue >= 1f) {
1970 stack.endImeAdjustAnimation();
1971 }
1972 }
1973
1974 return updated;
1975 }
1976
1977 boolean clearImeAdjustAnimation() {
1978 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001979 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1980 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001981 if (stack != null && stack.isAdjustedForIme()) {
1982 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1983 changed = true;
1984 }
1985 }
1986 return changed;
1987 }
1988
1989 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001990 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1991 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001992 if (stack.isVisible() && stack.isAdjustedForIme()) {
1993 stack.beginImeAdjustAnimation();
1994 }
1995 }
1996 }
1997
1998 void adjustForImeIfNeeded() {
1999 final WindowState imeWin = mService.mInputMethodWindow;
2000 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2001 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002002 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002003 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2004 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2005 imeTargetStack.getDockSide() : DOCKED_INVALID;
2006 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2007 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2008 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002009 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002010 final boolean imeHeightChanged = imeVisible &&
2011 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2012
2013 // The divider could be adjusted for IME position, or be thinner than usual,
2014 // or both. There are three possible cases:
2015 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2016 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2017 // - If IME is not visible, divider is not moved and is normal width.
2018
2019 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002020 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2021 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002022 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002023 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2024 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002025 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2026 } else {
2027 stack.resetAdjustedForIme(false);
2028 }
2029 }
2030 mDividerControllerLocked.setAdjustedForIme(
2031 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2032 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002033 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2034 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002035 stack.resetAdjustedForIme(!dockVisible);
2036 }
2037 mDividerControllerLocked.setAdjustedForIme(
2038 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2039 }
Winson Chung655332c2016-10-31 13:14:28 -07002040 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002041 }
2042
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002043 /**
2044 * If a window that has an animation specifying a colored background and the current wallpaper
2045 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2046 * suddenly disappear.
2047 */
2048 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002049 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2050 w -> w.mIsWallpaper && w.isVisibleNow());
2051
2052 if (visibleWallpaper != null) {
2053 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002054 }
2055 return winAnimator.mAnimLayer;
2056 }
2057
Wale Ogunwale10124582016-09-15 20:25:50 -07002058 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002059 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2060 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002061 stack.prepareFreezingTaskBounds();
2062 }
2063 }
2064
Wale Ogunwale94744212015-09-21 19:01:47 -07002065 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002066 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002067
2068 // Compute a transform matrix to undo the coordinate space transformation,
2069 // and present the window at the same physical position it previously occupied.
2070 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2071 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2072
2073 mTmpRectF.set(bounds);
2074 mTmpMatrix.mapRect(mTmpRectF);
2075 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002076 }
2077
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002078 static int deltaRotation(int oldRotation, int newRotation) {
2079 int delta = newRotation - oldRotation;
2080 if (delta < 0) delta += 4;
2081 return delta;
2082 }
2083
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002084 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002085 Matrix outMatrix) {
2086 // For rotations without Z-ordering we don't need the target rectangle's position.
2087 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2088 displayHeight, outMatrix);
2089 }
2090
2091 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2092 float displayWidth, float displayHeight, Matrix outMatrix) {
2093 switch (rotation) {
2094 case ROTATION_0:
2095 outMatrix.reset();
2096 break;
2097 case ROTATION_270:
2098 outMatrix.setRotate(270, 0, 0);
2099 outMatrix.postTranslate(0, displayHeight);
2100 outMatrix.postTranslate(rectTop, 0);
2101 break;
2102 case ROTATION_180:
2103 outMatrix.reset();
2104 break;
2105 case ROTATION_90:
2106 outMatrix.setRotate(90, 0, 0);
2107 outMatrix.postTranslate(displayWidth, 0);
2108 outMatrix.postTranslate(-rectTop, rectLeft);
2109 break;
2110 }
2111 }
2112
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002113 @CallSuper
2114 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002115 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002116 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002117 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002118 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002119 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2120 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002121 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002122 }
2123 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2124 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002125 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2126 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002127 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002128 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002129 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2130 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002131 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002132 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002133 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2134 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002135 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002136 }
2137 proto.write(DPI, mBaseDisplayDensity);
2138 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002139 proto.write(ROTATION, mRotation);
2140 final ScreenRotationAnimation screenRotationAnimation =
2141 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2142 if (screenRotationAnimation != null) {
2143 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2144 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002145 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002146 proto.end(token);
2147 }
2148
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002149 @Override
2150 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2151 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002152 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2153 final String subPrefix = " " + prefix;
2154 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2155 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2156 pw.print("dpi");
2157 if (mInitialDisplayWidth != mBaseDisplayWidth
2158 || mInitialDisplayHeight != mBaseDisplayHeight
2159 || mInitialDisplayDensity != mBaseDisplayDensity) {
2160 pw.print(" base=");
2161 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2162 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2163 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002164 if (mDisplayScalingDisabled) {
2165 pw.println(" noscale");
2166 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002167 pw.print(" cur=");
2168 pw.print(mDisplayInfo.logicalWidth);
2169 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2170 pw.print(" app=");
2171 pw.print(mDisplayInfo.appWidth);
2172 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2173 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2174 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2175 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2176 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002177 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002178 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002179 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002180
Craig Mautnerdc548482014-02-05 13:35:24 -08002181 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002182 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002183 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2184 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002185 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002186 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002187
Craig Mautnerdc548482014-02-05 13:35:24 -08002188 pw.println();
2189 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002190 pw.println();
2191 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002192 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002193 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002194 pw.print(" Exiting #"); pw.print(i);
2195 pw.print(' '); pw.print(token);
2196 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002197 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002198 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002199 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002200
Jorim Jaggi31f71702016-05-04 16:43:04 -07002201 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002202
2203 // Dump stack references
2204 final TaskStack homeStack = getHomeStack();
2205 if (homeStack != null) {
2206 pw.println(prefix + "homeStack=" + homeStack.getName());
2207 }
2208 final TaskStack pinnedStack = getPinnedStack();
2209 if (pinnedStack != null) {
2210 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2211 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002212 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002213 if (splitScreenPrimaryStack != null) {
2214 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2215 }
2216
2217 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002218 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002219 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002220 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002221
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002222 pw.println();
2223 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002224 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002225
2226 @Override
2227 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002228 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002229 }
2230
2231 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002232 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002233 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002234
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002235 /** Returns true if the stack in the windowing mode is visible. */
2236 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002237 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002238 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002239 }
2240
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002241 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002242 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002243 final int x = (int) xf;
2244 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002245 final WindowState touchedWin = getWindow(w -> {
2246 final int flags = w.mAttrs.flags;
2247 if (!w.isVisibleLw()) {
2248 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002249 }
2250 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002251 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002252 }
2253
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002254 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002255 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002256 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002257 }
2258
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002259 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002260
2261 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002262 return mTmpRegion.contains(x, y) || touchFlags == 0;
2263 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002264
2265 return touchedWin;
2266 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002267
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002268 boolean canAddToastWindowForUid(int uid) {
2269 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002270 // Also if the app is focused adding more than one toast at
2271 // a time for better backwards compatibility.
2272 final WindowState focusedWindowForUid = getWindow(w ->
2273 w.mOwnerUid == uid && w.isFocused());
2274 if (focusedWindowForUid != null) {
2275 return true;
2276 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002277 final WindowState win = getWindow(w ->
2278 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2279 && !w.mWindowRemovalAllowed);
2280 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002281 }
2282
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002283 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002284 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2285 return;
2286 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002287
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002288 // Used to communicate the old focus to the callback method.
2289 mTmpWindow = oldFocus;
2290
2291 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002292 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002293
2294 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002295 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002296
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002297 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002298
2299 if (mTmpWindow == null) {
2300 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2301 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002302 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002303 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002304 }
2305
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002306 /** Updates the layer assignment of windows on this display. */
2307 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002308 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002309 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002310 if (setLayoutNeeded) {
2311 setLayoutNeeded();
2312 }
Robert Carrb1579c82017-09-05 14:54:47 -07002313
Robert Carrf59b8dd2017-10-02 18:58:36 -07002314 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002315 // the application of this transaction until the animation pass triggers
2316 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2317 // the hiding and showing of surfaces.
2318 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002319 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002320 }
2321
Wale Ogunwale1666e312016-12-16 11:27:18 -08002322 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2323 // moving containers or resizing them. Need to investigate the best way to have it automatically
2324 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002325 void layoutAndAssignWindowLayersIfNeeded() {
2326 mService.mWindowsChanged = true;
2327 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002328
2329 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2330 false /*updateInputWindows*/)) {
2331 assignWindowLayers(false /* setLayoutNeeded */);
2332 }
2333
2334 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2335 mService.mWindowPlacerLocked.performSurfacePlacement();
2336 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2337 }
2338
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002339 /** Returns true if a leaked surface was destroyed */
2340 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002341 // Used to indicate that a surface was leaked.
2342 mTmpWindow = null;
2343 forAllWindows(w -> {
2344 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002345 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002346 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002347 }
2348 if (!mService.mSessions.contains(wsa.mSession)) {
2349 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002350 + w + " surface=" + wsa.mSurfaceController
2351 + " token=" + w.mToken
2352 + " pid=" + w.mSession.mPid
2353 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002354 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002355 mService.mForceRemoves.add(w);
2356 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002357 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002358 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002359 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002360 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002361 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002362 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002363 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002364 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002365 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002366
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002367 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002368 }
2369
2370 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002371 * Determine and return the window that should be the IME target.
2372 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2373 * @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 +00002374 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002375 WindowState computeImeTarget(boolean updateImeTarget) {
2376 if (mService.mInputMethodWindow == null) {
2377 // There isn't an IME so there shouldn't be a target...That was easy!
2378 if (updateImeTarget) {
2379 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2380 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002381 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 }
2383 return null;
2384 }
2385
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002386 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2387 // same display. Or even when the current IME/target are not on the same screen as the next
2388 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002389 mUpdateImeTarget = updateImeTarget;
2390 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002391
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002392
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002393 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2394 // to be on top of it, but it is not -really- where input will go. So look down below
2395 // for a real window to target...
2396 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2397 final AppWindowToken token = target.mAppToken;
2398 if (token != null) {
2399 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2400 if (betterTarget != null) {
2401 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002402 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002403 }
2404 }
2405
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002406 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2407 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002408
2409 // Now, a special case -- if the last target's window is in the process of exiting, and is
2410 // above the new target, keep on the last target to avoid flicker. Consider for example a
2411 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2412 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2413 // scrim.
2414 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002415 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2416 && (target == null
2417 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002418 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002419 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002420 }
2421
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002422 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2423 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002424
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002425 if (target == null) {
2426 if (updateImeTarget) {
2427 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2428 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2429 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002430 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002431 }
2432
2433 return null;
2434 }
2435
2436 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002437 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2438 if (token != null) {
2439
2440 // Now some fun for dealing with window animations that modify the Z order. We need
2441 // to look at all windows below the current target that are in this app, finding the
2442 // highest visible one in layering.
2443 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002444 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002445 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002446 }
2447
2448 if (highestTarget != null) {
2449 final AppTransition appTransition = mService.mAppTransition;
2450 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2451 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2452 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002453 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002454
2455 if (appTransition.isTransitionSet()) {
2456 // If we are currently setting up for an animation, hold everything until we
2457 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002458 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002460 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002461 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002462 // If the window we are currently targeting is involved with an animation,
2463 // and it is on top of the next target we will be over, then hold off on
2464 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002465 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002466 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002467 }
2468 }
2469 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002470
2471 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2472 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002473 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002474 }
2475
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002476 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002477 }
2478
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002479 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002480 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002481 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002482 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002483 }
2484
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002485 mService.mInputMethodTarget = target;
2486 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487 assignWindowLayers(false /* setLayoutNeeded */);
2488 }
2489
2490 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2491 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2492 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2493 }
2494
2495 // Used to indicate we have reached the first window in the range we are interested in.
2496 mTmpWindow = null;
2497
2498 // TODO: Figure-out a more efficient way to do this.
2499 final WindowState candidate = getWindow(w -> {
2500 if (w == top) {
2501 // Reached the first window in the range we are interested in.
2502 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002503 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002504 if (mTmpWindow == null) {
2505 return false;
2506 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002507
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002508 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2509 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002510 }
2511 // If we reached the bottom of the range of windows we are considering,
2512 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002513 if (w == bottom) {
2514 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002515 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002516 return false;
2517 });
2518
2519 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002520 }
2521
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002522 void setLayoutNeeded() {
2523 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2524 mLayoutNeeded = true;
2525 }
2526
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002527 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002528 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2529 mLayoutNeeded = false;
2530 }
2531
2532 boolean isLayoutNeeded() {
2533 return mLayoutNeeded;
2534 }
2535
Wale Ogunwale02319a62016-09-26 15:21:22 -07002536 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2537 if (mTokenMap.isEmpty()) {
2538 return;
2539 }
2540 pw.println(" Display #" + mDisplayId);
2541 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2542 while (it.hasNext()) {
2543 final WindowToken token = it.next();
2544 pw.print(" ");
2545 pw.print(token);
2546 if (dumpAll) {
2547 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002548 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002549 } else {
2550 pw.println();
2551 }
2552 }
2553 }
2554
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002555 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002556 final int[] index = new int[1];
2557 forAllWindows(w -> {
2558 final WindowStateAnimator wAnim = w.mWinAnimator;
2559 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2560 index[0] = index[0] + 1;
2561 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002562 }
2563
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002564 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002565 forAllWindows(w -> {
2566 w.mWinAnimator.enableSurfaceTrace(fd);
2567 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002568 }
2569
2570 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002571 forAllWindows(w -> {
2572 w.mWinAnimator.disableSurfaceTrace();
2573 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002574 }
2575
Jorim Jaggife762342016-10-13 14:33:27 +02002576 /**
2577 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2578 */
2579 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2580 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002581 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002582 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2583 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002584 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002585 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002586 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002587 }
2588
Wale Ogunwale494009b82016-10-21 09:01:38 -07002589 boolean checkWaitingForWindows() {
2590
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002591 mHaveBootMsg = false;
2592 mHaveApp = false;
2593 mHaveWallpaper = false;
2594 mHaveKeyguard = true;
2595
2596 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002597 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2598 return true;
2599 }
2600 if (w.isDrawnLw()) {
2601 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002602 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002603 } else if (w.mAttrs.type == TYPE_APPLICATION
2604 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002605 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002606 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002607 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002608 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002609 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002610 }
2611 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002612 return false;
2613 });
2614
2615 if (visibleWindow != null) {
2616 // We have a visible window.
2617 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002618 }
2619
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002620 // if the wallpaper service is disabled on the device, we're never going to have
2621 // wallpaper, don't bother waiting for it
2622 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2623 com.android.internal.R.bool.config_enableWallpaperService)
2624 && !mService.mOnlyCore;
2625
Wale Ogunwale494009b82016-10-21 09:01:38 -07002626 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2627 "******** booted=" + mService.mSystemBooted
2628 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002629 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2630 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2631 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002632
2633 // If we are turning on the screen to show the boot message, don't do it until the boot
2634 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002635 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002636 return true;
2637 }
2638
2639 // If we are turning on the screen after the boot is completed normally, don't do so until
2640 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002641 if (mService.mSystemBooted
2642 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002643 return true;
2644 }
2645
2646 return false;
2647 }
2648
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002649 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002650 mTmpWindowAnimator = animator;
2651 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002652 }
2653
2654 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002655 resetAnimationBackgroundAnimator();
2656
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002657 // Used to indicate a detached wallpaper.
2658 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002659 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002660
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002661 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002662
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002663 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002664 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002665 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2666 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002667 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2668 }
2669 }
2670
Wale Ogunwale494009b82016-10-21 09:01:38 -07002671 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002672 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002673 if (imFocus == null) {
2674 return false;
2675 }
2676
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002677 if (DEBUG_INPUT_METHOD) {
2678 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2679 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2680 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002681 }
2682
Wale Ogunwale494009b82016-10-21 09:01:38 -07002683 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2684
2685 if (DEBUG_INPUT_METHOD) {
2686 Slog.i(TAG_WM, "IM target client: " + imeClient);
2687 if (imeClient != null) {
2688 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2689 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2690 }
2691 }
2692
2693 return imeClient != null && imeClient.asBinder() == client.asBinder();
2694 }
2695
2696 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002697 final WindowState win = getWindow(
2698 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2699 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002700 }
2701
2702 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002703 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002704 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002705 final int curValue = w.mSystemUiVisibility;
2706 final int diff = (curValue ^ visibility) & globalDiff;
2707 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002708 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002709 w.mSeq++;
2710 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002711 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002712 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2713 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002714 visibility, newValue, diff);
2715 }
2716 } catch (RemoteException e) {
2717 // so sorry
2718 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002719 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002720 }
2721
2722 void onWindowFreezeTimeout() {
2723 Slog.w(TAG_WM, "Window freeze timeout expired.");
2724 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002725
2726 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002727 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002728 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002729 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002730 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002731 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2732 - mService.mDisplayFreezeTime);
2733 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002734 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002735 mService.mWindowPlacerLocked.performSurfacePlacement();
2736 }
2737
2738 void waitForAllWindowsDrawn() {
2739 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002740 forAllWindows(w -> {
2741 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2742 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2743 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002744 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 w.mLastContentInsets.set(-1, -1, -1, -1);
2746 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002747 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002748 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002749 }
2750
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002751 // TODO: Super crazy long method that should be broken down...
2752 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2753
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002754 final int dw = mDisplayInfo.logicalWidth;
2755 final int dh = mDisplayInfo.logicalHeight;
2756 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2757
2758 mTmpUpdateAllDrawn.clear();
2759
2760 int repeats = 0;
2761 do {
2762 repeats++;
2763 if (repeats > 6) {
2764 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2765 clearLayoutNeeded();
2766 break;
2767 }
2768
2769 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2770 pendingLayoutChanges);
2771
Andrii Kulian839def92016-11-02 10:58:58 -07002772 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2773 // the wallpaper window jumping across displays.
2774 // Remove check for default display when there will be support for multiple wallpaper
2775 // targets (on different displays).
2776 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002777 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002778 }
2779
2780 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2781 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2782 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2783 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002784 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002785 }
2786 }
2787
2788 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2789 setLayoutNeeded();
2790 }
2791
2792 // FIRST LOOP: Perform a layout, if needed.
2793 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2794 performLayout(repeats == 1, false /* updateInputWindows */);
2795 } else {
2796 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2797 }
2798
2799 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2800 pendingLayoutChanges = 0;
2801
2802 if (isDefaultDisplay) {
2803 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002804 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002805 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2806 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2807 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2808 }
2809 } while (pendingLayoutChanges != 0);
2810
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002811 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002812
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002813 mTmpRecoveringMemory = recoveringMemory;
2814 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01002815 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002816
2817 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002818 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2819 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2820 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002821 true /* inTraversal, must call performTraversalInTrans... below */);
2822
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002823 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2824 if (wallpaperVisible != mLastWallpaperVisible) {
2825 mLastWallpaperVisible = wallpaperVisible;
2826 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2827 }
2828
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002829 while (!mTmpUpdateAllDrawn.isEmpty()) {
2830 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2831 // See if any windows have been drawn, so they (and others associated with them)
2832 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002833 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002834 }
2835
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002836 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002837 }
2838
Bryce Leef3c6a472017-11-14 14:53:06 -08002839 private void updateBounds() {
2840 calculateBounds(mTmpBounds);
2841 setBounds(mTmpBounds);
2842 }
2843
2844 // Determines the current display bounds based on the current state
2845 private void calculateBounds(Rect out) {
2846 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2847 final int orientation = mDisplayInfo.rotation;
2848 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2849 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2850 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2851 int width = mDisplayInfo.logicalWidth;
2852 int left = (physWidth - width) / 2;
2853 int height = mDisplayInfo.logicalHeight;
2854 int top = (physHeight - height) / 2;
2855 out.set(left, top, left + width, top + height);
2856 }
2857
2858 @Override
2859 public void getBounds(Rect out) {
2860 calculateBounds(out);
2861 }
2862
2863 private void getBounds(Rect out, int orientation) {
2864 getBounds(out);
2865
2866 // Rotate the Rect if needed.
2867 final int currentRotation = mDisplayInfo.rotation;
2868 final int rotationDelta = deltaRotation(currentRotation, orientation);
2869 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2870 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2871 mTmpRectF.set(out);
2872 mTmpMatrix.mapRect(mTmpRectF);
2873 mTmpRectF.round(out);
2874 }
2875 }
2876
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002877 void performLayout(boolean initial, boolean updateInputWindows) {
2878 if (!isLayoutNeeded()) {
2879 return;
2880 }
2881 clearLayoutNeeded();
2882
2883 final int dw = mDisplayInfo.logicalWidth;
2884 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002885 if (DEBUG_LAYOUT) {
2886 Slog.v(TAG, "-------------------------------------");
2887 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2888 }
2889
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002890 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2891 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2892 // display info above...
2893 mDisplayFrames.mRotation = mRotation;
2894 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002895 if (isDefaultDisplay) {
2896 // Not needed on non-default displays.
2897 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2898 mService.mScreenRect.set(0, 0, dw, dh);
2899 }
2900
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002901 int seq = mService.mLayoutSeq + 1;
2902 if (seq < 0) seq = 0;
2903 mService.mLayoutSeq = seq;
2904
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002905 // Used to indicate that we have processed the dream window and all additional windows are
2906 // behind it.
2907 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002908 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002909
2910 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002911 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002912
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 // Used to indicate that we have processed the dream window and all additional attached
2914 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002915 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002916 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002917
2918 // Now perform layout of attached windows, which usually depend on the position of the
2919 // window they are attached to. XXX does not deal with windows that are attached to windows
2920 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002921 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002922
2923 // Window frames may have changed. Tell the input dispatcher about it.
2924 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2925 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2926 if (updateInputWindows) {
2927 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2928 }
2929
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002930 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2931 }
2932
2933 /**
2934 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2935 * In portrait mode, it grabs the full screenshot.
2936 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002937 * @param config of the output bitmap
2938 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2939 */
chaviwfbe47df2017-11-10 16:14:49 -08002940 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2941 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002942 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002943 if (DEBUG_SCREENSHOT) {
2944 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002945 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002946 return null;
2947 }
2948
chaviwfbe47df2017-11-10 16:14:49 -08002949 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002950 return null;
2951 }
2952
chaviwfbe47df2017-11-10 16:14:49 -08002953 int dw = mDisplayInfo.logicalWidth;
2954 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955
chaviwfbe47df2017-11-10 16:14:49 -08002956 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002957 return null;
2958 }
2959
chaviwfbe47df2017-11-10 16:14:49 -08002960 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002961
2962 // The screenshot API does not apply the current screen rotation.
2963 int rot = mDisplay.getRotation();
2964
2965 if (rot == ROTATION_90 || rot == ROTATION_270) {
2966 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2967 }
2968
chaviwfbe47df2017-11-10 16:14:49 -08002969 // SurfaceFlinger is not aware of orientation, so convert our logical
2970 // crop to SurfaceFlinger's portrait orientation.
2971 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972
2973 final ScreenRotationAnimation screenRotationAnimation =
2974 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2975 final boolean inRotation = screenRotationAnimation != null &&
2976 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08002977 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002978
Robert Carrb1579c82017-09-05 14:54:47 -07002979 // TODO(b/68392460): We should screenshot Task controls directly
2980 // but it's difficult at the moment as the Task doesn't have the
2981 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08002982 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002983 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07002984 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002985 return null;
2986 }
chaviwfbe47df2017-11-10 16:14:49 -08002987
2988 // Create a copy of the screenshot that is immutable and backed in ashmem.
2989 // This greatly reduces the overhead of passing the bitmap between processes.
2990 final Bitmap ret = bitmap.createAshmemBitmap(config);
2991 bitmap.recycle();
2992 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002993 }
chaviwfbe47df2017-11-10 16:14:49 -08002994 }
2995
2996 private boolean shouldScreenshotWallpaper() {
2997 MutableBoolean screenshotReady = new MutableBoolean(false);
2998
2999 forAllWindows(w -> {
3000 if (!w.mIsWallpaper) {
3001 return false;
3002 }
3003
3004 // Found the wallpaper window
3005 final WindowStateAnimator winAnim = w.mWinAnimator;
3006
3007 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
3008 screenshotReady.value = true;
3009 }
3010
3011 return true;
3012 }, true /* traverseTopToBottom */);
3013
3014 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003015 }
3016
3017 // TODO: Can this use createRotationMatrix()?
3018 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3019 if (rot == Surface.ROTATION_90) {
3020 final int tmp = crop.top;
3021 crop.top = dw - crop.right;
3022 crop.right = crop.bottom;
3023 crop.bottom = dw - crop.left;
3024 crop.left = tmp;
3025 } else if (rot == Surface.ROTATION_180) {
3026 int tmp = crop.top;
3027 crop.top = dh - crop.bottom;
3028 crop.bottom = dh - tmp;
3029 tmp = crop.right;
3030 crop.right = dw - crop.left;
3031 crop.left = dw - tmp;
3032 } else if (rot == Surface.ROTATION_270) {
3033 final int tmp = crop.top;
3034 crop.top = crop.left;
3035 crop.left = dh - crop.bottom;
3036 crop.bottom = crop.right;
3037 crop.right = dh - tmp;
3038 }
3039 }
3040
3041 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003042 // Used to indicate the layout is needed.
3043 mTmpWindow = null;
3044
3045 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003046 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003047 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003048 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003049 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003050 w.setDisplayLayoutNeeded();
3051 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003052 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003053
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003054 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003055 mService.mWindowPlacerLocked.performSurfacePlacement();
3056 }
3057 }
3058
Wale Ogunwale1666e312016-12-16 11:27:18 -08003059 void setExitingTokensHasVisible(boolean hasVisible) {
3060 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3061 mExitingTokens.get(i).hasVisible = hasVisible;
3062 }
3063
3064 // Initialize state of exiting applications.
3065 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3066 }
3067
3068 void removeExistingTokensIfPossible() {
3069 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3070 final WindowToken token = mExitingTokens.get(i);
3071 if (!token.hasVisible) {
3072 mExitingTokens.remove(i);
3073 }
3074 }
3075
3076 // Time to remove any exiting applications?
3077 mTaskStackContainers.removeExistingAppTokensIfPossible();
3078 }
3079
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003080 @Override
3081 void onDescendantOverrideConfigurationChanged() {
3082 setLayoutNeeded();
3083 mService.requestTraversal();
3084 }
3085
David Stevens9440dc82017-03-16 19:00:20 -07003086 boolean okToDisplay() {
3087 if (mDisplayId == DEFAULT_DISPLAY) {
3088 return !mService.mDisplayFrozen
3089 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3090 }
3091 return mDisplayInfo.state == Display.STATE_ON;
3092 }
3093
3094 boolean okToAnimate() {
3095 return okToDisplay() &&
3096 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3097 }
3098
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003099 static final class TaskForResizePointSearchResult {
3100 boolean searchDone;
3101 Task taskForResize;
3102
3103 void reset() {
3104 searchDone = false;
3105 taskForResize = null;
3106 }
3107 }
Robert Carr3b716242016-08-16 16:02:21 -07003108
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003109 private static final class ApplySurfaceChangesTransactionState {
3110 boolean displayHasContent;
3111 boolean obscured;
3112 boolean syswin;
3113 boolean focusDisplayed;
3114 float preferredRefreshRate;
3115 int preferredModeId;
3116
3117 void reset() {
3118 displayHasContent = false;
3119 obscured = false;
3120 syswin = false;
3121 focusDisplayed = false;
3122 preferredRefreshRate = 0;
3123 preferredModeId = 0;
3124 }
3125 }
3126
3127 private static final class ScreenshotApplicationState {
3128 WindowState appWin;
3129 int maxLayer;
3130 int minLayer;
3131 boolean screenshotReady;
3132
3133 void reset(boolean screenshotReady) {
3134 appWin = null;
3135 maxLayer = 0;
3136 minLayer = 0;
3137 this.screenshotReady = screenshotReady;
3138 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3139 }
3140 }
3141
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003142 /**
3143 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3144 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3145 * homogeneous children type which is currently required by sub-classes of
3146 * {@link WindowContainer} class.
3147 */
3148 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3149
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003150 DisplayChildWindowContainer(WindowManagerService service) {
3151 super(service);
3152 }
3153
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003154 @Override
3155 boolean fillsParent() {
3156 return true;
3157 }
3158
3159 @Override
3160 boolean isVisible() {
3161 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003162 }
3163 }
3164
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003165 /**
3166 * Window container class that contains all containers on this display relating to Apps.
3167 * I.e Activities.
3168 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003169 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003170 /**
3171 * A control placed at the appropriate level for transitions to occur.
3172 */
chaviw23ee71c2017-12-18 11:29:41 -08003173 SurfaceControl mAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003174
Wale Ogunwale61911492017-10-11 08:50:50 -07003175 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3176 // through the list to find them.
3177 private TaskStack mHomeStack = null;
3178 private TaskStack mPinnedStack = null;
3179 private TaskStack mSplitScreenPrimaryStack = null;
3180
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003181 TaskStackContainers(WindowManagerService service) {
3182 super(service);
3183 }
3184
Wale Ogunwale61911492017-10-11 08:50:50 -07003185 /**
3186 * Returns the topmost stack on the display that is compatible with the input windowing mode
3187 * and activity type. Null is no compatible stack on the display.
3188 */
3189 TaskStack getStack(int windowingMode, int activityType) {
3190 if (activityType == ACTIVITY_TYPE_HOME) {
3191 return mHomeStack;
3192 }
3193 if (windowingMode == WINDOWING_MODE_PINNED) {
3194 return mPinnedStack;
3195 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3196 return mSplitScreenPrimaryStack;
3197 }
3198 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3199 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003200 if (activityType == ACTIVITY_TYPE_UNDEFINED
3201 && windowingMode == stack.getWindowingMode()) {
3202 // Passing in undefined type means we want to match the topmost stack with the
3203 // windowing mode.
3204 return stack;
3205 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003206 if (stack.isCompatible(windowingMode, activityType)) {
3207 return stack;
3208 }
3209 }
3210 return null;
3211 }
3212
3213 @VisibleForTesting
3214 TaskStack getTopStack() {
3215 return mTaskStackContainers.getChildCount() > 0
3216 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3217 }
3218
3219 TaskStack getHomeStack() {
3220 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3221 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3222 }
3223 return mHomeStack;
3224 }
3225
3226 TaskStack getPinnedStack() {
3227 return mPinnedStack;
3228 }
3229
Matthew Ng64e77cf2017-10-31 14:01:31 -07003230 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003231 return mSplitScreenPrimaryStack;
3232 }
3233
Andrii Kulian839def92016-11-02 10:58:58 -07003234 /**
3235 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003236 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003237 */
3238 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003239 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003240 addChild(stack, onTop);
3241 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003242 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003243
Wale Ogunwale61911492017-10-11 08:50:50 -07003244 void onStackWindowingModeChanged(TaskStack stack) {
3245 removeStackReferenceIfNeeded(stack);
3246 addStackReferenceIfNeeded(stack);
3247 if (stack == mPinnedStack && getTopStack() != stack) {
3248 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3249 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3250 }
3251 }
3252
3253 private void addStackReferenceIfNeeded(TaskStack stack) {
3254 if (stack.isActivityTypeHome()) {
3255 if (mHomeStack != null) {
3256 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3257 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3258 }
3259 mHomeStack = stack;
3260 }
3261 final int windowingMode = stack.getWindowingMode();
3262 if (windowingMode == WINDOWING_MODE_PINNED) {
3263 if (mPinnedStack != null) {
3264 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3265 + mPinnedStack + " already exist on display=" + this
3266 + " stack=" + stack);
3267 }
3268 mPinnedStack = stack;
3269 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3270 if (mSplitScreenPrimaryStack != null) {
3271 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3272 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3273 + " already exist on display=" + this + " stack=" + stack);
3274 }
3275 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003276 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003277 }
3278 }
3279
3280 private void removeStackReferenceIfNeeded(TaskStack stack) {
3281 if (stack == mHomeStack) {
3282 mHomeStack = null;
3283 } else if (stack == mPinnedStack) {
3284 mPinnedStack = null;
3285 } else if (stack == mSplitScreenPrimaryStack) {
3286 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003287 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3288 mService.setDockedStackCreateStateLocked(
3289 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3290 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003291 }
Andrii Kulian839def92016-11-02 10:58:58 -07003292 }
3293
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003294 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003295 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3296 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003297 addChild(stack, addIndex);
3298 setLayoutNeeded();
3299 }
3300
Wale Ogunwale61911492017-10-11 08:50:50 -07003301 @Override
3302 protected void removeChild(TaskStack stack) {
3303 super.removeChild(stack);
3304 removeStackReferenceIfNeeded(stack);
3305 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003306
3307 @Override
3308 boolean isOnTop() {
3309 // Considered always on top
3310 return true;
3311 }
3312
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003313 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003314 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003315 if (child.getWindowConfiguration().isAlwaysOnTop()
3316 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003317 // This stack is always-on-top, override the default behavior.
3318 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3319
3320 // Moving to its current position, as we must call super but we don't want to
3321 // perform any meaningful action.
3322 final int currentPosition = mChildren.indexOf(child);
3323 super.positionChildAt(currentPosition, child, false /* includingParents */);
3324 return;
3325 }
3326
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003327 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3328 super.positionChildAt(targetPosition, child, includingParents);
3329
3330 setLayoutNeeded();
3331 }
3332
3333 /**
3334 * When stack is added or repositioned, find a proper position for it.
3335 * This will make sure that pinned stack always stays on top.
3336 * @param requestedPosition Position requested by caller.
3337 * @param stack Stack to be added or positioned.
3338 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3339 * @return The proper position for the stack.
3340 */
3341 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3342 final int topChildPosition = mChildren.size() - 1;
3343 boolean toTop = requestedPosition == POSITION_TOP;
3344 toTop |= adding ? requestedPosition >= topChildPosition + 1
3345 : requestedPosition >= topChildPosition;
3346 int targetPosition = requestedPosition;
3347
Wale Ogunwale61911492017-10-11 08:50:50 -07003348 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003349 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003350 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003351 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003352 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3353 }
3354
3355 // So, stack is moved just below the pinned stack.
3356 // When we're adding a new stack the target is the current pinned stack position.
3357 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003358 // stack, because WindowContainer#positionAt() first removes element and then adds
3359 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003360 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3361 }
3362
3363 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003364 }
3365
3366 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003367 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3368 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003369 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003370 if (super.forAllWindows(callback, traverseTopToBottom)) {
3371 return true;
3372 }
3373 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3374 return true;
3375 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003376 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003377 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3378 return true;
3379 }
3380 if (super.forAllWindows(callback, traverseTopToBottom)) {
3381 return true;
3382 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003383 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003384 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003385 }
3386
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003387 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003388 boolean traverseTopToBottom) {
3389 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3390 // app tokens.
3391 // TODO: Investigate if we need to continue to do this or if we can just process them
3392 // in-order.
3393 if (traverseTopToBottom) {
3394 for (int i = mChildren.size() - 1; i >= 0; --i) {
3395 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3396 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003397 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3398 traverseTopToBottom)) {
3399 return true;
3400 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003401 }
3402 }
3403 } else {
3404 final int count = mChildren.size();
3405 for (int i = 0; i < count; ++i) {
3406 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3407 final int appTokensCount = appTokens.size();
3408 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003409 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3410 traverseTopToBottom)) {
3411 return true;
3412 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003413 }
3414 }
3415 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003416 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003417 }
3418
Wale Ogunwale1666e312016-12-16 11:27:18 -08003419 void setExitingTokensHasVisible(boolean hasVisible) {
3420 for (int i = mChildren.size() - 1; i >= 0; --i) {
3421 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3422 for (int j = appTokens.size() - 1; j >= 0; --j) {
3423 appTokens.get(j).hasVisible = hasVisible;
3424 }
3425 }
3426 }
3427
3428 void removeExistingAppTokensIfPossible() {
3429 for (int i = mChildren.size() - 1; i >= 0; --i) {
3430 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3431 for (int j = appTokens.size() - 1; j >= 0; --j) {
3432 final AppWindowToken token = appTokens.get(j);
3433 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3434 && (!token.mIsExiting || token.isEmpty())) {
3435 // Make sure there is no animation running on this token, so any windows
3436 // associated with it will be removed as soon as their animations are
3437 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003438 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003439 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3440 "performLayout: App token exiting now removed" + token);
3441 token.removeIfPossible();
3442 }
3443 }
3444 }
3445 }
3446
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003447 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003448 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003449 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3450 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003451 // Apps and their containers are not allowed to specify an orientation while the
3452 // docked or freeform stack is visible...except for the home stack/task if the
3453 // docked stack is minimized and it actually set something.
3454 if (mHomeStack != null && mHomeStack.isVisible()
3455 && mDividerControllerLocked.isMinimizedDock()) {
3456 final int orientation = mHomeStack.getOrientation();
3457 if (orientation != SCREEN_ORIENTATION_UNSET) {
3458 return orientation;
3459 }
3460 }
3461 return SCREEN_ORIENTATION_UNSPECIFIED;
3462 }
3463
3464 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003465 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3466 PackageManager.FEATURE_AUTOMOTIVE);
3467 if (isCar) {
3468 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3469 // allow anything but the default orientation.
3470 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3471 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3472 return SCREEN_ORIENTATION_UNSPECIFIED;
3473 }
3474
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003475 if (orientation != SCREEN_ORIENTATION_UNSET
3476 && orientation != SCREEN_ORIENTATION_BEHIND) {
3477 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3478 "App is requesting an orientation, return " + orientation);
3479 return orientation;
3480 }
3481
3482 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003483 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003484 // The next app has not been requested to be visible, so we keep the current orientation
3485 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003486 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003487 }
Robert Carrb1579c82017-09-05 14:54:47 -07003488
3489 @Override
3490 void assignChildLayers(SurfaceControl.Transaction t) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003491 int layer = 0;
Rob Carr6693e062018-01-09 17:10:00 +00003492
3493 // We allow stacks to change visual order from the AM specified order due to
3494 // Z-boosting during animations. However we must take care to ensure TaskStacks
3495 // which are marked as alwaysOnTop remain that way.
3496 for (int i = 0; i < mChildren.size(); i++) {
3497 final TaskStack s = mChildren.get(i);
3498 s.assignChildLayers();
3499 if (!s.needsZBoost() && !s.isAlwaysOnTop()) {
3500 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003501 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003502 }
3503 for (int i = 0; i < mChildren.size(); i++) {
3504 final TaskStack s = mChildren.get(i);
Rob Carr6693e062018-01-09 17:10:00 +00003505 if (s.needsZBoost() && !s.isAlwaysOnTop()) {
3506 s.assignLayer(t, layer++);
3507 }
3508 }
3509 for (int i = 0; i < mChildren.size(); i++) {
3510 final TaskStack s = mChildren.get(i);
3511 if (s.isAlwaysOnTop()) {
3512 s.assignLayer(t, layer++);
3513 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003514 }
3515
Rob Carr6693e062018-01-09 17:10:00 +00003516 // The appropriate place for App-Transitions to occur is right
3517 // above all other animations but still below things in the Picture-and-Picture
3518 // windowing mode.
3519 if (mAppAnimationLayer != null) {
3520 t.setLayer(mAppAnimationLayer, layer++);
3521 }
Robert Carrb1579c82017-09-05 14:54:47 -07003522 }
3523
3524 @Override
chaviw23ee71c2017-12-18 11:29:41 -08003525 SurfaceControl getAppAnimationLayer() {
3526 return mAppAnimationLayer;
3527 }
3528
3529 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003530 void onParentSet() {
3531 super.onParentSet();
3532 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003533 mAppAnimationLayer = makeChildSurface(null)
3534 .setName("animationLayer")
3535 .build();
3536 getPendingTransaction().show(mAppAnimationLayer);
3537 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003538 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003539 mAppAnimationLayer.destroy();
3540 mAppAnimationLayer = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003541 }
3542 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003543 }
3544
Robert Carree4d4b92017-11-22 12:21:46 -08003545 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003546 AboveAppWindowContainers(String name, WindowManagerService service) {
3547 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003548 }
3549
3550 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3551 boolean needAssignIme = imeContainer != null
3552 && imeContainer.getSurfaceControl() != null;
3553 for (int j = 0; j < mChildren.size(); ++j) {
3554 final WindowToken wt = mChildren.get(j);
3555 wt.assignLayer(t, j);
3556 wt.assignChildLayers(t);
3557
3558 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3559 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003560
3561 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3562 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003563 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003564 needAssignIme = false;
3565 }
3566 }
3567 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003568 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003569 }
3570 }
3571 }
3572
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003573 /**
3574 * Window container class that contains all containers on this display that are not related to
3575 * Apps. E.g. status bar.
3576 */
Robert Carree4d4b92017-11-22 12:21:46 -08003577 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003578 /**
3579 * Compares two child window tokens returns -1 if the first is lesser than the second in
3580 * terms of z-order and 1 otherwise.
3581 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003582 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003583 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003584 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3585 token1.mOwnerCanManageAppTokens)
3586 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3587 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003588
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003589 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3590 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3591 return false;
3592 }
3593 final int req = w.mAttrs.screenOrientation;
3594 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3595 || req == SCREEN_ORIENTATION_UNSET) {
3596 return false;
3597 }
3598 return true;
3599 };
3600
Wale Ogunwale3a931692016-11-02 16:49:48 -07003601 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003602 NonAppWindowContainers(String name, WindowManagerService service) {
3603 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003604 mName = name;
3605 }
3606
3607 void addChild(WindowToken token) {
3608 addChild(token, mWindowComparator);
3609 }
3610
3611 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003612 int getOrientation() {
3613 final WindowManagerPolicy policy = mService.mPolicy;
3614 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003615 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003616
3617 if (win != null) {
3618 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003619 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003620 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003621 if (mService.mKeyguardGoingAway) {
3622 // Keyguard can't affect the orientation if it is going away...
3623 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3624 return SCREEN_ORIENTATION_UNSET;
3625 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003626 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003627 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003628 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003629 }
3630
Andrii Kulian8ee72852017-03-10 10:36:45 -08003631 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003632
Jorim Jaggi75520d52017-08-24 17:23:19 +02003633 if (policy.isKeyguardShowingAndNotOccluded()
3634 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003635 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003636 }
3637
3638 return SCREEN_ORIENTATION_UNSET;
3639 }
3640
3641 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003642 String getName() {
3643 return mName;
3644 }
Robert Carr3b716242016-08-16 16:02:21 -07003645 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003646
Robert Carr9034d962018-01-04 18:27:42 -08003647 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
3648 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
3649 super(name, service);
3650 }
3651
3652 @Override
3653 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
3654 }
3655 };
3656
Robert Carrb1579c82017-09-05 14:54:47 -07003657 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3658 return mService.makeSurfaceBuilder(s)
3659 .setParent(mWindowingLayer);
3660 }
3661
3662 @Override
3663 SurfaceSession getSession() {
3664 return mSession;
3665 }
3666
3667 @Override
3668 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003669 SurfaceSession s = child != null ? child.getSession() : getSession();
3670 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003671 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003672
3673 if (child == null) {
3674 return b;
3675 }
3676
Robert Carree4d4b92017-11-22 12:21:46 -08003677 return b.setName(child.getName())
3678 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003679 }
3680
3681 /**
3682 * The makeSurface variants are for use by the window-container
3683 * hierarchy. makeOverlay here is a function for various non windowing
3684 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3685 * and other potpourii.
3686 */
3687 SurfaceControl.Builder makeOverlay() {
3688 return mService.makeSurfaceBuilder(mSession)
3689 .setParent(mOverlayLayer);
3690 }
3691
3692 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003693 applyMagnificationSpec(getPendingTransaction(), spec);
3694 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003695 }
3696
3697 @Override
3698 void onParentSet() {
3699 // Since we are the top of the SurfaceControl hierarchy here
3700 // we create the root surfaces explicitly rather than chaining
3701 // up as the default implementation in onParentSet does. So we
3702 // explicitly do NOT call super here.
3703 }
3704
3705 @Override
3706 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003707
3708 // These are layers as children of "mWindowingLayer"
3709 mBelowAppWindowsContainers.assignLayer(t, 0);
3710 mTaskStackContainers.assignLayer(t, 1);
3711 mAboveAppWindowsContainers.assignLayer(t, 2);
3712
3713 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003714 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003715
Robert Carree4d4b92017-11-22 12:21:46 -08003716 // In the case where we have an IME target that is not in split-screen
3717 // mode IME assignment is easy. We just need the IME to go directly above
3718 // the target. This way children of the target will naturally go above the IME
3719 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003720 //
Robert Carree4d4b92017-11-22 12:21:46 -08003721 // In the case of split-screen windowing mode, we need to elevate the IME above the
3722 // docked divider while keeping the app itself below the docked divider, so instead
3723 // we use relative layering of the IME targets child windows, and place the
3724 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003725 //
Robert Carree4d4b92017-11-22 12:21:46 -08003726 // In the case where we have no IME target we assign it where it's base layer would
3727 // place it in the AboveAppWindowContainers.
3728 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3729 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003730 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003731 // TODO: We need to use an extra level on the app surface to ensure
3732 // this is always above SurfaceView but always below attached window.
3733 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003734 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003735 }
3736
3737 // Above we have assigned layers to our children, now we ask them to assign
3738 // layers to their children.
3739 mBelowAppWindowsContainers.assignChildLayers(t);
3740 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003741 mAboveAppWindowsContainers.assignChildLayers(t,
3742 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003743 mImeWindowsContainers.assignChildLayers(t);
3744 }
3745
3746 /**
3747 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3748 * that the IME target is one of the docked applications. We'd like the docked divider to be
3749 * above both of the applications, and we'd like the IME to be above the docked divider.
3750 * However we need child windows of the applications to be above the IME (Text drag handles).
3751 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3752 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3753 * with {@link #WindowState#assignLayer}
3754 */
3755 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3756 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3757 }
3758
3759 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +01003760 public void destroyAfterPendingTransaction(SurfaceControl surface) {
Robert Carrb1579c82017-09-05 14:54:47 -07003761 mPendingDestroyingSurfaces.add(surface);
3762 }
3763
3764 /**
3765 * Destroys any surfaces that have been put into the pending list with
Jorim Jaggia5e10572017-11-15 14:36:26 +01003766 * {@link #destroyAfterPendingTransaction}.
Robert Carrb1579c82017-09-05 14:54:47 -07003767 */
3768 void onPendingTransactionApplied() {
3769 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3770 mPendingDestroyingSurfaces.get(i).destroy();
3771 }
3772 mPendingDestroyingSurfaces.clear();
3773 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01003774
3775 @Override
3776 void prepareSurfaces() {
3777 final ScreenRotationAnimation screenRotationAnimation =
3778 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
3779 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
3780 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
3781 mPendingTransaction.setMatrix(mWindowingLayer,
3782 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
3783 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
3784 mPendingTransaction.setPosition(mWindowingLayer,
3785 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
3786 mPendingTransaction.setAlpha(mWindowingLayer,
3787 screenRotationAnimation.getEnterTransformation().getAlpha());
3788 }
3789 super.prepareSurfaces();
3790 }
Craig Mautner59c00972012-07-30 12:10:24 -07003791}