blob: 4d839d009c56634fac55746f96787c3ec56f47bd [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;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070061import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070062import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070064import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi75520d52017-08-24 17:23:19 +020065import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
70import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
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.TYPE_LAYER_MULTIPLIER;
95import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
96import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070097import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070098import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070099import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700100import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700101import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700102import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700103import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700104import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700105import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700106import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
107import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000108import static com.android.server.wm.proto.DisplayProto.DISPLAY_FRAMES;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700109import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
110import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
111import static com.android.server.wm.proto.DisplayProto.DPI;
112import static com.android.server.wm.proto.DisplayProto.ID;
113import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
114import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700115import static com.android.server.wm.proto.DisplayProto.ROTATION;
116import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700117import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700118import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700119
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700120import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700121import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700122import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700123import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700124import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700125import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100126import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700127import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800128import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700129import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700130import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100131import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700132import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700133import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700134import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700135import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700136import android.os.RemoteException;
137import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700138import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800139import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700140import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700141import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700142import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700143import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700144import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700145import android.view.Surface;
146import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700147import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700148
Bryce Lee48f4b572017-04-10 10:54:15 -0700149import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800150import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700151import com.android.internal.view.IInputMethodClient;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000152import android.view.DisplayFrames;
Craig Mautner59c00972012-07-30 12:10:24 -0700153
Robert Carr3b716242016-08-16 16:02:21 -0700154import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700155import java.io.PrintWriter;
156import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700157import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700158import java.util.HashMap;
159import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700160import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700161import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800162import java.util.function.Consumer;
163import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700164
Craig Mautner59c00972012-07-30 12:10:24 -0700165/**
166 * Utility class for keeping track of the WindowStates and other pertinent contents of a
167 * particular Display.
168 *
169 * IMPORTANT: No method from this class should ever be used without holding
170 * WindowManagerService.mWindowMap.
171 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700172class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700173 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700174
175 /** Unique identifier of this stack. */
176 private final int mDisplayId;
177
Wale Ogunwale3a931692016-11-02 16:49:48 -0700178 /** The containers below are the only child containers the display can have. */
179 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700180 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700181 // Contains all non-app window containers that should be displayed above the app containers
182 // (e.g. Status bar)
183 private final NonAppWindowContainers mAboveAppWindowsContainers =
184 new NonAppWindowContainers("mAboveAppWindowsContainers");
185 // Contains all non-app window containers that should be displayed below the app containers
186 // (e.g. Wallpaper).
187 private final NonAppWindowContainers mBelowAppWindowsContainers =
188 new NonAppWindowContainers("mBelowAppWindowsContainers");
189 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
190 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
191 // window containers together and move them in-sync if/when needed.
192 private final NonAppWindowContainers mImeWindowsContainers =
193 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700194
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000195 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800196 private WindowState mTmpWindow2;
197 private WindowAnimator mTmpWindowAnimator;
198 private boolean mTmpRecoveringMemory;
199 private boolean mUpdateImeTarget;
200 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700201 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700202
Wale Ogunwale02319a62016-09-26 15:21:22 -0700203 // Mapping from a token IBinder to a WindowToken object on this display.
204 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
205
Andrii Kuliancd097992017-03-23 18:31:59 -0700206 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700207 int mInitialDisplayWidth = 0;
208 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700209 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700210
211 /**
212 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
213 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
214 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
215 */
Craig Mautner59c00972012-07-30 12:10:24 -0700216 int mBaseDisplayWidth = 0;
217 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700218 /**
219 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
220 * but can be set from Settings or via shell command "adb shell wm density".
221 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
222 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700223 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700224 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700225 private final DisplayInfo mDisplayInfo = new DisplayInfo();
226 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700227 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000228 DisplayFrames mDisplayFrames;
229
Andrii Kulian06d07d62017-03-14 11:11:47 -0700230 /**
231 * For default display it contains real metrics, empty for others.
232 * @see WindowManagerService#createWatermarkInTransaction()
233 */
234 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
235 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
236 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700237
Andrii Kulian06d07d62017-03-14 11:11:47 -0700238 /**
239 * Compat metrics computed based on {@link #mDisplayMetrics}.
240 * @see #updateDisplayAndOrientation(int)
241 */
242 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
243
244 /** The desired scaling factor for compatible apps. */
245 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700246
Andrii Kulian8ee72852017-03-10 10:36:45 -0800247 /**
248 * Current rotation of the display.
249 * Constants as per {@link android.view.Surface.Rotation}.
250 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700251 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800252 */
253 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700254
Andrii Kulian8ee72852017-03-10 10:36:45 -0800255 /**
256 * Last applied orientation of the display.
257 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
258 *
259 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
260 */
261 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700262
Andrii Kulian8ee72852017-03-10 10:36:45 -0800263 /**
264 * Flag indicating that the application is receiving an orientation that has different metrics
265 * than it expected. E.g. Portrait instead of Landscape.
266 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700267 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800268 */
269 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700270
Andrii Kulian8ee72852017-03-10 10:36:45 -0800271 /**
272 * Orientation forced by some window. If there is no visible window that specifies orientation
273 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
274 *
275 * @see NonAppWindowContainers#getOrientation()
276 */
277 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700278
Andrii Kulian8ee72852017-03-10 10:36:45 -0800279 /**
280 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
281 * occluded.
282 *
283 * @see NonAppWindowContainers#getOrientation()
284 */
285 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
286
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700287 /**
288 * Keep track of wallpaper visibility to notify changes.
289 */
290 private boolean mLastWallpaperVisible = false;
291
Andrii Kulian06d07d62017-03-14 11:11:47 -0700292 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700293
Craig Mautner39834192012-09-02 07:47:24 -0700294 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700295 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700296 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700297 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800298 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700299
Craig Mautnerdc548482014-02-05 13:35:24 -0800300 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700301 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800302
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700303 /** Detect user tapping outside of current focused task bounds .*/
304 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700305
Craig Mautner6601b7b2013-04-29 10:29:11 -0700306 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700307 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700308
Craig Mautner6601b7b2013-04-29 10:29:11 -0700309 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800310 private final Rect mTmpRect = new Rect();
311 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700312 private final RectF mTmpRectF = new RectF();
313 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800314 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700315
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800316 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700317
Craig Mautner95da1082014-02-24 17:54:35 -0800318 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700319 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800320
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700321 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700322 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700323
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800324 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700325
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800326 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
327
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000328 private boolean mHaveBootMsg = false;
329 private boolean mHaveApp = false;
330 private boolean mHaveWallpaper = false;
331 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700332
333 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
334
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700335 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
336 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000337 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
338 new ApplySurfaceChangesTransactionState();
339 private final ScreenshotApplicationState mScreenshotApplicationState =
340 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700341
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700342 // True if this display is in the process of being removed. Used to determine if the removal of
343 // the display's direct children should be allowed.
344 private boolean mRemovingDisplay = false;
345
Bryce Leed1871262017-06-12 14:12:29 -0700346 // {@code false} if this display is in the processing of being created.
347 private boolean mDisplayReady = false;
348
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700349 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800350 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700351 int mInputMethodAnimLayerAdjustment;
352
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800353 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
354 WindowStateAnimator winAnimator = w.mWinAnimator;
355 if (winAnimator.hasSurface()) {
356 final boolean wasAnimating = winAnimator.mWasAnimating;
357 final boolean nowAnimating = winAnimator.stepAnimationLocked(
358 mTmpWindowAnimator.mCurrentTime);
359 winAnimator.mWasAnimating = nowAnimating;
360 mTmpWindowAnimator.orAnimating(nowAnimating);
361
362 if (DEBUG_WALLPAPER) Slog.v(TAG,
363 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
364
365 if (wasAnimating && !winAnimator.mAnimating
366 && mWallpaperController.isWallpaperTarget(w)) {
367 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
368 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
369 if (DEBUG_LAYOUT_REPEATS) {
370 mService.mWindowPlacerLocked.debugLayoutRepeats(
371 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
372 }
373 }
374 }
375
376 final AppWindowToken atoken = w.mAppToken;
377 if (winAnimator.mDrawState == READY_TO_SHOW) {
378 if (atoken == null || atoken.allDrawn) {
379 if (w.performShowLocked()) {
380 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
381 if (DEBUG_LAYOUT_REPEATS) {
382 mService.mWindowPlacerLocked.debugLayoutRepeats(
383 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
384 }
385 }
386 }
387 }
388 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
389 if (appAnimator != null && appAnimator.thumbnail != null) {
390 if (appAnimator.thumbnailTransactionSeq
391 != mTmpWindowAnimator.mAnimTransactionSequence) {
392 appAnimator.thumbnailTransactionSeq =
393 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800394 }
395 }
396 };
397
398 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
399 final WindowStateAnimator winAnimator = w.mWinAnimator;
400 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
401 return;
402 }
403
404 final int flags = w.mAttrs.flags;
405
406 // If this window is animating, make a note that we have an animating window and take
407 // care of a request to run a detached wallpaper animation.
408 if (winAnimator.mAnimating) {
409 if (winAnimator.mAnimation != null) {
410 if ((flags & FLAG_SHOW_WALLPAPER) != 0
411 && winAnimator.mAnimation.getDetachWallpaper()) {
412 mTmpWindow = w;
413 }
414 final int color = winAnimator.mAnimation.getBackgroundColor();
415 if (color != 0) {
416 final TaskStack stack = w.getStack();
417 if (stack != null) {
418 stack.setAnimationBackground(winAnimator, color);
419 }
420 }
421 }
422 mTmpWindowAnimator.setAnimating(true);
423 }
424
425 // If this window's app token is running a detached wallpaper animation, make a note so
426 // we can ensure the wallpaper is displayed behind it.
427 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
428 if (appAnimator != null && appAnimator.animation != null
429 && appAnimator.animating) {
430 if ((flags & FLAG_SHOW_WALLPAPER) != 0
431 && appAnimator.animation.getDetachWallpaper()) {
432 mTmpWindow = w;
433 }
434
435 final int color = appAnimator.animation.getBackgroundColor();
436 if (color != 0) {
437 final TaskStack stack = w.getStack();
438 if (stack != null) {
439 stack.setAnimationBackground(winAnimator, color);
440 }
441 }
442 }
443 };
444
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800445 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
446 final int lostFocusUid = mTmpWindow.mOwnerUid;
447 final Handler handler = mService.mH;
448 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
449 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
450 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
451 w.mAttrs.hideTimeoutMilliseconds);
452 }
453 }
454 };
455
456 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
457 final AppWindowToken focusedApp = mService.mFocusedApp;
458 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
459 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
460
461 if (!w.canReceiveKeys()) {
462 return false;
463 }
464
465 final AppWindowToken wtoken = w.mAppToken;
466
467 // If this window's application has been removed, just skip it.
468 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
469 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
470 + (wtoken.removed ? "removed" : "sendingToBottom"));
471 return false;
472 }
473
474 if (focusedApp == null) {
475 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
476 + " using new focus @ " + w);
477 mTmpWindow = w;
478 return true;
479 }
480
481 if (!focusedApp.windowsAreFocusable()) {
482 // Current focused app windows aren't focusable...
483 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
484 + " focusable using new focus @ " + w);
485 mTmpWindow = w;
486 return true;
487 }
488
489 // Descend through all of the app tokens and find the first that either matches
490 // win.mAppToken (return win) or mFocusedApp (return null).
491 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
492 if (focusedApp.compareTo(wtoken) > 0) {
493 // App stack below focused app stack. No focus for you!!!
494 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
495 "findFocusedWindow: Reached focused app=" + focusedApp);
496 mTmpWindow = null;
497 return true;
498 }
499 }
500
501 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
502 mTmpWindow = w;
503 return true;
504 };
505
506 private final Consumer<WindowState> mPrepareWindowSurfaces =
507 w -> w.mWinAnimator.prepareSurfaceLocked(true);
508
509 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
521 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
522 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
523 + " parentHidden=" + w.isParentWindowHidden());
524 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
525 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
526 + " 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 // Window frames may have changed. Update dim layer with the new bounds.
562 final Task task = w.getTask();
563 if (task != null) {
564 mDimLayerController.updateDimLayer(task);
565 }
566
567 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
568 + " mContainingFrame=" + w.mContainingFrame
569 + " mDisplayFrame=" + w.mDisplayFrame);
570 }
571 }
572 };
573
574 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
575 if (w.mLayoutAttached) {
576 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
577 + " mViewVisibility=" + w.mViewVisibility
578 + " mRelayoutCalled=" + w.mRelayoutCalled);
579 // If this view is GONE, then skip it -- keep the current frame, and let the caller
580 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
581 // windows, since that means "perform layout as normal, just don't display").
582 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
583 return;
584 }
585 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
586 || w.mLayoutNeeded) {
587 if (mTmpInitial) {
588 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
589 w.mContentChanged = false;
590 }
591 w.mLayoutNeeded = false;
592 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000593 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800594 w.mLayoutSeq = mService.mLayoutSeq;
595 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
596 + " mContainingFrame=" + w.mContainingFrame
597 + " mDisplayFrame=" + w.mDisplayFrame);
598 }
599 } else if (w.mAttrs.type == TYPE_DREAM) {
600 // Don't layout windows behind a dream, so that if it does stuff like hide the
601 // status bar we won't get a bad transition when it goes away.
602 mTmpWindow = mTmpWindow2;
603 }
604 };
605
606 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
607 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
608 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
609 return w.canBeImeTarget();
610 };
611
612 private final Consumer<WindowState> mApplyPostLayoutPolicy =
613 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
614 mService.mInputMethodTarget);
615
616 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
617 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
618 final boolean obscuredChanged = w.mObscured !=
619 mTmpApplySurfaceChangesTransactionState.obscured;
620 final RootWindowContainer root = mService.mRoot;
621 // Only used if default window
622 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
623
624 // Update effect.
625 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
626 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
627 final boolean isDisplayed = w.isDisplayedLw();
628
629 if (isDisplayed && w.isObscuringDisplay()) {
630 // This window completely covers everything behind it, so we want to leave all
631 // of them as undimmed (for performance reasons).
632 root.mObscuringWindow = w;
633 mTmpApplySurfaceChangesTransactionState.obscured = true;
634 }
635
636 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
637 root.handleNotObscuredLocked(w,
638 mTmpApplySurfaceChangesTransactionState.obscured,
639 mTmpApplySurfaceChangesTransactionState.syswin);
640
641 if (w.mHasSurface && isDisplayed) {
642 final int type = w.mAttrs.type;
643 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
644 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
645 mTmpApplySurfaceChangesTransactionState.syswin = true;
646 }
647 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
648 && w.mAttrs.preferredRefreshRate != 0) {
649 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
650 = w.mAttrs.preferredRefreshRate;
651 }
652 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
653 && w.mAttrs.preferredDisplayModeId != 0) {
654 mTmpApplySurfaceChangesTransactionState.preferredModeId
655 = w.mAttrs.preferredDisplayModeId;
656 }
657 }
658 }
659
660 w.applyDimLayerIfNeeded();
661
662 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
663 && mWallpaperController.isWallpaperTarget(w)) {
664 // This is the wallpaper target and its obscured state changed... make sure the
665 // current wallpaper's visibility has been updated accordingly.
666 mWallpaperController.updateWallpaperVisibility();
667 }
668
669 w.handleWindowMovedIfNeeded();
670
671 final WindowStateAnimator winAnimator = w.mWinAnimator;
672
673 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
674 w.mContentChanged = false;
675
676 // Moved from updateWindowsAndWallpaperLocked().
677 if (w.mHasSurface) {
678 // Take care of the window being ready to display.
679 final boolean committed = winAnimator.commitFinishDrawingLocked();
680 if (isDefaultDisplay && committed) {
681 if (w.mAttrs.type == TYPE_DREAM) {
682 // HACK: When a dream is shown, it may at that point hide the lock screen.
683 // So we need to redo the layout to let the phone window manager make this
684 // happen.
685 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
686 if (DEBUG_LAYOUT_REPEATS) {
687 surfacePlacer.debugLayoutRepeats(
688 "dream and commitFinishDrawingLocked true",
689 pendingLayoutChanges);
690 }
691 }
692 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
693 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
694 "First draw done in potential wallpaper target " + w);
695 root.mWallpaperMayChange = true;
696 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
697 if (DEBUG_LAYOUT_REPEATS) {
698 surfacePlacer.debugLayoutRepeats(
699 "wallpaper and commitFinishDrawingLocked true",
700 pendingLayoutChanges);
701 }
702 }
703 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700704 final TaskStack stack = w.getStack();
705 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
706 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800707 // Updates the shown frame before we set up the surface. This is needed
708 // because the resizing could change the top-left position (in addition to
709 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
710 // position the surface.
711 //
712 // If an animation is being started, we can't call this method because the
713 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700714 // we do want to update the position if the window is animating. We make an exception
715 // for the bounds animating state, where an application may have been waiting
716 // for an exit animation to start, but instead enters PiP. We need to ensure
717 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800718 winAnimator.computeShownFrameLocked();
719 }
720 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
721 }
722
723 final AppWindowToken atoken = w.mAppToken;
724 if (atoken != null) {
725 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
726 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
727 mTmpUpdateAllDrawn.add(atoken);
728 }
729 }
730
731 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
732 && w.isDisplayedLw()) {
733 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
734 }
735
736 w.updateResizingWindowIfNeeded();
737 };
738
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800739 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800740 * Create new {@link DisplayContent} instance, add itself to the root window container and
741 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700742 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800743 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700744 * @param layersController window layer controller used to assign layer to the windows on this
745 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700746 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
747 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700748 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700749 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700750 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800751 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
752 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
753 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
754 + " new=" + display);
755 }
756
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700757 mDisplay = display;
758 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700759 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700760 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700761 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700762 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700763 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700764 mService = service;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000765 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700766 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800767 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700768 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800769 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700770
771 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700772 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700773 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700774 super.addChild(mAboveAppWindowsContainers, null);
775 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800776
777 // Add itself as a child to the root container.
778 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700779
780 // TODO(b/62541591): evaluate whether this is the best spot to declare the
781 // {@link DisplayContent} ready for use.
782 mDisplayReady = true;
783 }
784
785 boolean isReady() {
786 // The display is ready when the system and the individual display are both ready.
787 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700788 }
789
790 int getDisplayId() {
791 return mDisplayId;
792 }
793
Wale Ogunwale02319a62016-09-26 15:21:22 -0700794 WindowToken getWindowToken(IBinder binder) {
795 return mTokenMap.get(binder);
796 }
797
798 AppWindowToken getAppWindowToken(IBinder binder) {
799 final WindowToken token = getWindowToken(binder);
800 if (token == null) {
801 return null;
802 }
803 return token.asAppWindowToken();
804 }
805
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700806 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700807 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
808 if (dc != null) {
809 // We currently don't support adding a window token to the display if the display
810 // already has the binder mapped to another token. If there is a use case for supporting
811 // this moving forward we will either need to merge the WindowTokens some how or have
812 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700813 throw new IllegalArgumentException("Can't map token=" + token + " to display="
814 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700815 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700816 if (binder == null) {
817 throw new IllegalArgumentException("Can't map token=" + token + " to display="
818 + getName() + " binder is null");
819 }
820 if (token == null) {
821 throw new IllegalArgumentException("Can't map null token to display="
822 + getName() + " binder=" + binder);
823 }
824
Wale Ogunwale02319a62016-09-26 15:21:22 -0700825 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700826
827 if (token.asAppWindowToken() == null) {
828 // Add non-app token to container hierarchy on the display. App tokens are added through
829 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700830 switch (token.windowType) {
831 case TYPE_WALLPAPER:
832 mBelowAppWindowsContainers.addChild(token);
833 break;
834 case TYPE_INPUT_METHOD:
835 case TYPE_INPUT_METHOD_DIALOG:
836 mImeWindowsContainers.addChild(token);
837 break;
838 default:
839 mAboveAppWindowsContainers.addChild(token);
840 break;
841 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700842 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700843 }
844
845 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700846 final WindowToken token = mTokenMap.remove(binder);
847 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800848 token.setExiting();
849 }
850 return token;
851 }
852
853 /** Changes the display the input window token is housed on to this one. */
854 void reParentWindowToken(WindowToken token) {
855 final DisplayContent prevDc = token.getDisplayContent();
856 if (prevDc == this) {
857 return;
858 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800859 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
860 && token.asAppWindowToken() == null) {
861 // Removed the token from the map, but made sure it's not an app token before removing
862 // from parent.
863 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700864 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800865
866 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700867 }
868
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700869 void removeAppToken(IBinder binder) {
870 final WindowToken token = removeWindowToken(binder);
871 if (token == null) {
872 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
873 return;
874 }
875
876 final AppWindowToken appToken = token.asAppWindowToken();
877
878 if (appToken == null) {
879 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
880 return;
881 }
882
883 appToken.onRemovedFromDisplay();
884 }
885
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700886 Display getDisplay() {
887 return mDisplay;
888 }
889
Craig Mautner59c00972012-07-30 12:10:24 -0700890 DisplayInfo getDisplayInfo() {
891 return mDisplayInfo;
892 }
893
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700894 DisplayMetrics getDisplayMetrics() {
895 return mDisplayMetrics;
896 }
897
Andrii Kulian8ee72852017-03-10 10:36:45 -0800898 int getRotation() {
899 return mRotation;
900 }
901
902 void setRotation(int newRotation) {
903 mRotation = newRotation;
904 }
905
906 int getLastOrientation() {
907 return mLastOrientation;
908 }
909
910 void setLastOrientation(int orientation) {
911 mLastOrientation = orientation;
912 }
913
914 boolean getAltOrientation() {
915 return mAltOrientation;
916 }
917
918 void setAltOrientation(boolean altOrientation) {
919 mAltOrientation = altOrientation;
920 }
921
922 int getLastWindowForcedOrientation() {
923 return mLastWindowForcedOrientation;
924 }
925
Andrii Kulian06d07d62017-03-14 11:11:47 -0700926 /**
927 * Update rotation of the display.
928 *
929 * Returns true if the rotation has been changed. In this case YOU MUST CALL
930 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
931 */
932 boolean updateRotationUnchecked(boolean inTransaction) {
933 if (mService.mDeferredRotationPauseCount > 0) {
934 // Rotation updates have been paused temporarily. Defer the update until
935 // updates have been resumed.
936 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
937 return false;
938 }
939
940 ScreenRotationAnimation screenRotationAnimation =
941 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
942 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
943 // Rotation updates cannot be performed while the previous rotation change
944 // animation is still in progress. Skip this update. We will try updating
945 // again after the animation is finished and the display is unfrozen.
946 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
947 return false;
948 }
949 if (mService.mDisplayFrozen) {
950 // Even if the screen rotation animation has finished (e.g. isAnimating
951 // returns false), there is still some time where we haven't yet unfrozen
952 // the display. We also need to abort rotation here.
953 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
954 "Deferring rotation, still finishing previous rotation");
955 return false;
956 }
957
958 if (!mService.mDisplayEnabled) {
959 // No point choosing a rotation if the display is not enabled.
960 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
961 return false;
962 }
963
964 final int oldRotation = mRotation;
965 final int lastOrientation = mLastOrientation;
966 final boolean oldAltOrientation = mAltOrientation;
967 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700968 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700969 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700970
Robert Carr0e007272017-10-02 13:00:55 -0700971 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700972 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
973 if (seamlessRotated != null) {
974 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
975 // to complete (that is, waiting for windows to redraw). It's tempting to check
976 // w.mSeamlessRotationCount but that could be incorrect in the case of
977 // window-removal.
978 return false;
979 }
Robert Carr0e007272017-10-02 13:00:55 -0700980
981 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700982 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -0700983 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -0700984 mayRotateSeamlessly = false;
985 }
986 for (int i = 0; i < mService.mSessions.size(); i++) {
987 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
988 mayRotateSeamlessly = false;
989 break;
990 }
991 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700992 }
Robert Carr0e007272017-10-02 13:00:55 -0700993 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700994
995 // TODO: Implement forced rotation changes.
996 // Set mAltOrientation to indicate that the application is receiving
997 // an orientation that has different metrics than it expected.
998 // eg. Portrait instead of Landscape.
999
1000 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1001 lastOrientation, rotation);
1002
1003 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1004 + ", got rotation " + rotation + " which has "
1005 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1006
1007 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1008 // No change.
1009 return false;
1010 }
1011
1012 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1013 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1014 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1015
1016 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1017 mService.mWaitingForConfig = true;
1018 }
1019
1020 mRotation = rotation;
1021 mAltOrientation = altOrientation;
1022 if (isDefaultDisplay) {
1023 mService.mPolicy.setRotationLw(rotation);
1024 }
1025
1026 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1027 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1028 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1029 WINDOW_FREEZE_TIMEOUT_DURATION);
1030
1031 setLayoutNeeded();
1032 final int[] anim = new int[2];
1033 if (isDimming()) {
1034 anim[0] = anim[1] = 0;
1035 } else {
1036 mService.mPolicy.selectRotationAnimationLw(anim);
1037 }
1038
1039 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001040 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001041 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1042 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1043 mDisplayId);
1044 } else {
1045 // The screen rotation animation uses a screenshot to freeze the screen
1046 // while windows resize underneath.
1047 // When we are rotating seamlessly, we allow the elements to transition
1048 // to their rotated state independently and without a freeze required.
1049 screenRotationAnimation = null;
1050
1051 // We have to reset this in case a window was removed before it
1052 // finished seamless rotation.
1053 mService.mSeamlessRotationCount = 0;
1054 }
1055
1056 // We need to update our screen size information to match the new rotation. If the rotation
1057 // has actually changed then this method will return true and, according to the comment at
1058 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1059 // By updating the Display info here it will be available to
1060 // #computeScreenConfiguration() later.
1061 updateDisplayAndOrientation(getConfiguration().uiMode);
1062
1063 if (!inTransaction) {
1064 if (SHOW_TRANSACTIONS) {
1065 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1066 }
1067 mService.openSurfaceTransaction();
1068 }
1069 try {
1070 // NOTE: We disable the rotation in the emulator because
1071 // it doesn't support hardware OpenGL emulation yet.
1072 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1073 && screenRotationAnimation.hasScreenshot()) {
1074 if (screenRotationAnimation.setRotationInTransaction(
1075 rotation, mService.mFxSession,
1076 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1077 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1078 mService.scheduleAnimationLocked();
1079 }
1080 }
1081
1082 if (rotateSeamlessly) {
1083 forAllWindows(w -> {
1084 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1085 }, true /* traverseTopToBottom */);
1086 }
1087
1088 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1089 } finally {
1090 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001091 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001092 if (SHOW_LIGHT_TRANSACTIONS) {
1093 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1094 }
1095 }
1096 }
1097
1098 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001099 if (w.mHasSurface && !rotateSeamlessly) {
1100 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001101 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001102 mService.mRoot.mOrientationChangeComplete = false;
1103 w.mLastFreezeDuration = 0;
1104 }
1105 w.mReportOrientationChanged = true;
1106 }, true /* traverseTopToBottom */);
1107
1108 if (rotateSeamlessly) {
1109 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1110 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1111 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1112 }
1113
1114 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1115 final WindowManagerService.RotationWatcher rotationWatcher
1116 = mService.mRotationWatchers.get(i);
1117 if (rotationWatcher.mDisplayId == mDisplayId) {
1118 try {
1119 rotationWatcher.mWatcher.onRotationChanged(rotation);
1120 } catch (RemoteException e) {
1121 // Ignore
1122 }
1123 }
1124 }
1125
1126 // TODO (multi-display): Magnification is supported only for the default display.
1127 // Announce rotation only if we will not animate as we already have the
1128 // windows in final state. Otherwise, we make this call at the rotation end.
1129 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1130 && isDefaultDisplay) {
1131 mService.mAccessibilityController.onRotationChangedLocked(this);
1132 }
1133
1134 return true;
1135 }
1136
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001137 void configureDisplayPolicy() {
1138 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1139 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1140
1141 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1142 }
1143
Andrii Kulian06d07d62017-03-14 11:11:47 -07001144 /**
1145 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1146 * changed.
1147 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1148 */
1149 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1150 // Use the effective "visual" dimensions based on current rotation
1151 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1152 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1153 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1154 int dw = realdw;
1155 int dh = realdh;
1156
1157 if (mAltOrientation) {
1158 if (realdw > realdh) {
1159 // Turn landscape into portrait.
1160 int maxw = (int)(realdh/1.3f);
1161 if (maxw < realdw) {
1162 dw = maxw;
1163 }
1164 } else {
1165 // Turn portrait into landscape.
1166 int maxh = (int)(realdw/1.3f);
1167 if (maxh < realdh) {
1168 dh = maxh;
1169 }
1170 }
1171 }
1172
1173 // Update application display metrics.
1174 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1175 mDisplayId);
1176 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1177 mDisplayId);
1178 mDisplayInfo.rotation = mRotation;
1179 mDisplayInfo.logicalWidth = dw;
1180 mDisplayInfo.logicalHeight = dh;
1181 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1182 mDisplayInfo.appWidth = appWidth;
1183 mDisplayInfo.appHeight = appHeight;
1184 if (isDefaultDisplay) {
1185 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1186 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1187 }
1188 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1189 if (mDisplayScalingDisabled) {
1190 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1191 } else {
1192 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1193 }
1194
1195 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1196 mDisplayInfo);
1197
1198 mBaseDisplayRect.set(0, 0, dw, dh);
1199
1200 if (isDefaultDisplay) {
1201 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1202 mCompatDisplayMetrics);
1203 }
1204 return mDisplayInfo;
1205 }
1206
1207 /**
1208 * Compute display configuration based on display properties and policy settings.
1209 * Do not call if mDisplayReady == false.
1210 */
1211 void computeScreenConfiguration(Configuration config) {
1212 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1213
1214 final int dw = displayInfo.logicalWidth;
1215 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001216 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1217 // TODO: Probably best to set this based on some setting in the display content object,
1218 // so the display can be configured for things like fullscreen.
1219 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001220
Andrii Kulian06d07d62017-03-14 11:11:47 -07001221 config.screenWidthDp =
1222 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1223 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1224 config.screenHeightDp =
1225 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1226 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001227
1228 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1229 final int leftInset = mTmpRect.left;
1230 final int topInset = mTmpRect.top;
1231 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001232 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1233 leftInset + displayInfo.appWidth /* right */,
1234 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001235 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1236 || displayInfo.rotation == Surface.ROTATION_270);
1237
1238 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1239 mDisplayMetrics.density, config);
1240
1241 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1242 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1243 ? Configuration.SCREENLAYOUT_ROUND_YES
1244 : Configuration.SCREENLAYOUT_ROUND_NO);
1245
1246 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1247 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1248 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1249 dh, mDisplayId);
1250 config.densityDpi = displayInfo.logicalDensityDpi;
1251
1252 config.colorMode =
1253 (displayInfo.isHdr()
1254 ? Configuration.COLOR_MODE_HDR_YES
1255 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001256 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001257 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1258 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1259
1260 // Update the configuration based on available input devices, lid switch,
1261 // and platform configuration.
1262 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1263 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1264 config.navigation = Configuration.NAVIGATION_NONAV;
1265
1266 int keyboardPresence = 0;
1267 int navigationPresence = 0;
1268 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1269 final int len = devices != null ? devices.length : 0;
1270 for (int i = 0; i < len; i++) {
1271 InputDevice device = devices[i];
1272 if (!device.isVirtual()) {
1273 final int sources = device.getSources();
1274 final int presenceFlag = device.isExternal() ?
1275 WindowManagerPolicy.PRESENCE_EXTERNAL :
1276 WindowManagerPolicy.PRESENCE_INTERNAL;
1277
1278 // TODO(multi-display): Configure on per-display basis.
1279 if (mService.mIsTouchDevice) {
1280 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1281 InputDevice.SOURCE_TOUCHSCREEN) {
1282 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1283 }
1284 } else {
1285 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1286 }
1287
1288 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1289 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1290 navigationPresence |= presenceFlag;
1291 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1292 && config.navigation == Configuration.NAVIGATION_NONAV) {
1293 config.navigation = Configuration.NAVIGATION_DPAD;
1294 navigationPresence |= presenceFlag;
1295 }
1296
1297 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1298 config.keyboard = Configuration.KEYBOARD_QWERTY;
1299 keyboardPresence |= presenceFlag;
1300 }
1301 }
1302 }
1303
1304 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1305 config.navigation = Configuration.NAVIGATION_DPAD;
1306 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1307 }
1308
1309 // Determine whether a hard keyboard is available and enabled.
1310 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1311 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1312 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1313 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1314 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1315 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1316 }
1317
1318 // Let the policy update hidden states.
1319 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1320 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1321 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1322 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1323 }
1324
1325 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1326 int displayId) {
1327 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1328 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1329 final int unrotDw, unrotDh;
1330 if (rotated) {
1331 unrotDw = dh;
1332 unrotDh = dw;
1333 } else {
1334 unrotDw = dw;
1335 unrotDh = dh;
1336 }
1337 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1338 displayId);
1339 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1340 displayId);
1341 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1342 displayId);
1343 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1344 displayId);
1345 return sw;
1346 }
1347
1348 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1349 DisplayMetrics dm, int dw, int dh, int displayId) {
1350 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1351 displayId);
1352 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1353 uiMode, displayId);
1354 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1355 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1356 if (curSize == 0 || size < curSize) {
1357 curSize = size;
1358 }
1359 return curSize;
1360 }
1361
1362 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1363 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1364
1365 // We need to determine the smallest width that will occur under normal
1366 // operation. To this, start with the base screen size and compute the
1367 // width under the different possible rotations. We need to un-rotate
1368 // the current screen dimensions before doing this.
1369 int unrotDw, unrotDh;
1370 if (rotated) {
1371 unrotDw = dh;
1372 unrotDh = dw;
1373 } else {
1374 unrotDw = dw;
1375 unrotDh = dh;
1376 }
1377 displayInfo.smallestNominalAppWidth = 1<<30;
1378 displayInfo.smallestNominalAppHeight = 1<<30;
1379 displayInfo.largestNominalAppWidth = 0;
1380 displayInfo.largestNominalAppHeight = 0;
1381 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1382 unrotDh);
1383 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1384 unrotDw);
1385 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1386 unrotDh);
1387 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1388 unrotDw);
1389 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1390 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1391 displayId);
1392 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1393 displayId);
1394 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1395 displayId);
1396 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1397 displayId);
1398 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1399 outConfig.screenLayout = sl;
1400 }
1401
1402 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1403 int uiMode, int displayId) {
1404 // Get the app screen size at this rotation.
1405 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1406 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1407
1408 // Compute the screen layout size class for this rotation.
1409 int longSize = w;
1410 int shortSize = h;
1411 if (longSize < shortSize) {
1412 int tmp = longSize;
1413 longSize = shortSize;
1414 shortSize = tmp;
1415 }
1416 longSize = (int)(longSize/density);
1417 shortSize = (int)(shortSize/density);
1418 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1419 }
1420
1421 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1422 int uiMode, int dw, int dh) {
1423 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1424 displayId);
1425 if (width < displayInfo.smallestNominalAppWidth) {
1426 displayInfo.smallestNominalAppWidth = width;
1427 }
1428 if (width > displayInfo.largestNominalAppWidth) {
1429 displayInfo.largestNominalAppWidth = width;
1430 }
1431 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1432 displayId);
1433 if (height < displayInfo.smallestNominalAppHeight) {
1434 displayInfo.smallestNominalAppHeight = height;
1435 }
1436 if (height > displayInfo.largestNominalAppHeight) {
1437 displayInfo.largestNominalAppHeight = height;
1438 }
1439 }
1440
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001441 DockedStackDividerController getDockedDividerController() {
1442 return mDividerControllerLocked;
1443 }
1444
Winson Chung655332c2016-10-31 13:14:28 -07001445 PinnedStackController getPinnedStackController() {
1446 return mPinnedStackControllerLocked;
1447 }
1448
Jeff Browna506a6e2013-06-04 00:02:38 -07001449 /**
1450 * Returns true if the specified UID has access to this display.
1451 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001452 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001453 return mDisplay.hasAccess(uid);
1454 }
1455
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001456 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001457 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001458 }
1459
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001460 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001461 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001462 }
1463
Wale Ogunwale61911492017-10-11 08:50:50 -07001464 /**
1465 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1466 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001467 TaskStack getSplitScreenPrimaryStack() {
1468 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001469 return (stack != null && stack.isVisible()) ? stack : null;
1470 }
1471
1472 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001473 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001474 * not visible.
1475 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001476 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1477 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001478 }
1479
1480 TaskStack getPinnedStack() {
1481 return mTaskStackContainers.getPinnedStack();
1482 }
1483
1484 private boolean hasPinnedStack() {
1485 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001486 }
1487
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001488 /**
1489 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1490 * Null is no compatible stack on the display.
1491 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001492 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001493 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1494 }
1495
1496 /**
1497 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1498 * activity type. Null is no compatible stack on the display.
1499 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001500 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001501 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001502 }
1503
Bryce Lee48f4b572017-04-10 10:54:15 -07001504 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001505 TaskStack getTopStack() {
1506 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001507 }
1508
Wale Ogunwale61911492017-10-11 08:50:50 -07001509 void onStackWindowingModeChanged(TaskStack stack) {
1510 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001511 }
1512
Andrii Kulian441e4492016-09-29 15:25:00 -07001513 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001514 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001515 super.onConfigurationChanged(newParentConfig);
1516
Andrii Kulian3a507b52016-09-19 18:14:12 -07001517 // The display size information is heavily dependent on the resources in the current
1518 // configuration, so we need to reconfigure it every time the configuration changes.
1519 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1520 mService.reconfigureDisplayLocked(this);
1521
1522 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001523 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001524 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001525
Andrii Kulian441e4492016-09-29 15:25:00 -07001526 /**
1527 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1528 * bounds were updated.
1529 */
1530 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001531 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1532 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001533 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001534 changedStackList.add(stack.mStackId);
1535 }
1536 }
Winson Chung32c566f2017-04-11 18:31:21 -07001537
1538 // If there was no pinned stack, we still need to notify the controller of the display info
1539 // update as a result of the config change. We do this here to consolidate the flow between
1540 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001541 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001542 mPinnedStackControllerLocked.onDisplayInfoChanged();
1543 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001544 }
1545
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001546 @Override
1547 boolean fillsParent() {
1548 return true;
1549 }
1550
1551 @Override
1552 boolean isVisible() {
1553 return true;
1554 }
1555
1556 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001557 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001558 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001559 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001560 }
1561
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001562 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001563 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1564 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1565 // target, or here in order if there isn't an IME target.
1566 if (traverseTopToBottom) {
1567 for (int i = mChildren.size() - 1; i >= 0; --i) {
1568 final DisplayChildWindowContainer child = mChildren.get(i);
1569 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1570 // In this case the Ime windows will be processed above their target so we skip
1571 // here.
1572 continue;
1573 }
1574 if (child.forAllWindows(callback, traverseTopToBottom)) {
1575 return true;
1576 }
1577 }
1578 } else {
1579 final int count = mChildren.size();
1580 for (int i = 0; i < count; i++) {
1581 final DisplayChildWindowContainer child = mChildren.get(i);
1582 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1583 // In this case the Ime windows will be processed above their target so we skip
1584 // here.
1585 continue;
1586 }
1587 if (child.forAllWindows(callback, traverseTopToBottom)) {
1588 return true;
1589 }
1590 }
1591 }
1592 return false;
1593 }
1594
1595 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1596 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1597 }
1598
Wale Ogunwale399c8692017-05-08 14:22:42 -07001599 @Override
1600 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001601 final WindowManagerPolicy policy = mService.mPolicy;
1602
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001603 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001604 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001605 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001606 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001607 // If the display is frozen, some activities may be in the middle of restarting, and
1608 // thus have removed their old window. If the window has the flag to hide the lock
1609 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1610 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001611 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001612 } else if (policy.isKeyguardLocked()) {
1613 // Use the last orientation the while the display is frozen with the keyguard
1614 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1615 // window. We don't want to check the show when locked window directly though as
1616 // things aren't stable while the display is frozen, for example the window could be
1617 // momentarily unavailable due to activity relaunch.
1618 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001619 + "return " + mLastOrientation);
1620 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001621 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001622 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001623 final int orientation = mAboveAppWindowsContainers.getOrientation();
1624 if (orientation != SCREEN_ORIENTATION_UNSET) {
1625 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001626 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001627 }
1628
Wale Ogunwale399c8692017-05-08 14:22:42 -07001629 // Top system windows are not requesting an orientation. Start searching from apps.
1630 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001631 }
1632
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001633 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001634 // Check if display metrics changed and update base values if needed.
1635 updateBaseDisplayMetricsIfNeeded();
1636
Craig Mautner722285e2012-09-07 13:55:58 -07001637 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001638 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001639
Wale Ogunwale61911492017-10-11 08:50:50 -07001640 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1641 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001642 }
Craig Mautner722285e2012-09-07 13:55:58 -07001643 }
1644
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001645 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001646 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1647 if (displayManagerInternal != null) {
1648 // Bootstrap the default logical display from the display manager.
1649 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1650 if (newDisplayInfo != null) {
1651 mDisplayInfo.copyFrom(newDisplayInfo);
1652 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001653 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001654
Andrii Kuliancd097992017-03-23 18:31:59 -07001655 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1656 mDisplayInfo.logicalDensityDpi);
1657 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1658 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1659 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001660 }
1661
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001662 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001663 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001664 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001665 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001666 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1667 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001668 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001669 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001670 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001671 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001672 out.set(left, top, left + width, top + height);
1673 }
1674
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001675 private void getLogicalDisplayRect(Rect out, int orientation) {
1676 getLogicalDisplayRect(out);
1677
1678 // Rotate the Rect if needed.
1679 final int currentRotation = mDisplayInfo.rotation;
1680 final int rotationDelta = deltaRotation(currentRotation, orientation);
1681 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1682 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1683 mTmpRectF.set(out);
1684 mTmpMatrix.mapRect(mTmpRectF);
1685 mTmpRectF.round(out);
1686 }
1687 }
1688
Andrii Kuliancd097992017-03-23 18:31:59 -07001689 /**
1690 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1691 * values.
1692 */
1693 private void updateBaseDisplayMetricsIfNeeded() {
1694 // Get real display metrics without overrides from WM.
1695 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1696 final int orientation = mDisplayInfo.rotation;
1697 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1698 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1699 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1700 final int newDensity = mDisplayInfo.logicalDensityDpi;
1701
1702 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1703 || mInitialDisplayHeight != newHeight
1704 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1705
1706 if (displayMetricsChanged) {
1707 // Check if display size or density is forced.
1708 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1709 || mBaseDisplayHeight != mInitialDisplayHeight;
1710 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1711
1712 // If there is an override set for base values - use it, otherwise use new values.
1713 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1714 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1715 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1716
1717 // Real display metrics changed, so we should also update initial values.
1718 mInitialDisplayWidth = newWidth;
1719 mInitialDisplayHeight = newHeight;
1720 mInitialDisplayDensity = newDensity;
1721 mService.reconfigureDisplayLocked(this);
1722 }
1723 }
1724
Bryce Lee27cec322017-03-21 09:41:37 -07001725 /** Sets the maximum width the screen resolution can be */
1726 void setMaxUiWidth(int width) {
1727 if (DEBUG_DISPLAY) {
1728 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1729 }
1730
1731 mMaxUiWidth = width;
1732
1733 // Update existing metrics.
1734 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1735 }
1736
1737 /** Update base (override) display metrics. */
1738 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1739 mBaseDisplayWidth = baseWidth;
1740 mBaseDisplayHeight = baseHeight;
1741 mBaseDisplayDensity = baseDensity;
1742
1743 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1744 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1745 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1746 mBaseDisplayWidth = mMaxUiWidth;
1747
1748 if (DEBUG_DISPLAY) {
1749 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1750 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1751 + " on display:" + getDisplayId());
1752 }
1753 }
1754
1755 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1756 }
1757
Chong Zhangf66db432016-01-13 10:39:51 -08001758 void getContentRect(Rect out) {
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001759 out.set(mDisplayFrames.mContent);
Chong Zhangf66db432016-01-13 10:39:51 -08001760 }
1761
Wale Ogunwale61911492017-10-11 08:50:50 -07001762 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001763 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1764 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001765
Wale Ogunwale61911492017-10-11 08:50:50 -07001766 final TaskStack stack = new TaskStack(mService, stackId, controller);
1767 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001768 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001769 }
1770
Andrii Kulian51c1b672017-04-07 18:39:32 -07001771 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001772 final DisplayContent prevDc = stack.getDisplayContent();
1773 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001774 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1775 + " which is not currently attached to any display");
1776 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001777 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001778 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1779 + " to its current displayId=" + mDisplayId);
1780 }
1781
Wale Ogunwale61911492017-10-11 08:50:50 -07001782 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001783 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001784 }
1785
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001786 @Override
1787 protected void addChild(DisplayChildWindowContainer child,
1788 Comparator<DisplayChildWindowContainer> comparator) {
1789 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1790 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001791
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001792 @Override
1793 protected void addChild(DisplayChildWindowContainer child, int index) {
1794 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1795 }
1796
1797 @Override
1798 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001799 // Only allow removal of direct children from this display if the display is in the process
1800 // of been removed.
1801 if (mRemovingDisplay) {
1802 super.removeChild(child);
1803 return;
1804 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001805 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001806 }
1807
Andrii Kuliand2765632016-12-12 22:26:34 -08001808 @Override
1809 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1810 // Children of the display are statically ordered, so the real intention here is to perform
1811 // the operation on the display and not the static direct children.
1812 getParent().positionChildAt(position, this, includingParents);
1813 }
1814
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001815 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001816 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1817 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001818 final int taskId = stack.taskIdFromPoint(x, y);
1819 if (taskId != -1) {
1820 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001821 }
1822 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001823 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001824 }
1825
Chong Zhang8e89b312015-09-09 15:09:30 -07001826 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001827 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001828 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001829 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001830 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001831 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001832 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001833 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1834 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001835 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001836 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001837 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001838
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001839 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1840 if (mTmpTaskForResizePointSearchResult.searchDone) {
1841 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001842 }
1843 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001844 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001845 }
1846
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001847 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001848 // The provided task is the task on this display with focus, so if WindowManagerService's
1849 // focused app is not on this display, focusedTask will be null.
1850 if (focusedTask == null) {
1851 mTouchExcludeRegion.setEmpty();
1852 } else {
1853 mTouchExcludeRegion.set(mBaseDisplayRect);
1854 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1855 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001856 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1857 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001858 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1859 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001860 }
1861 // If we removed the focused task above, add it back and only leave its
1862 // outside touch area in the exclusion. TapDectector is not interested in
1863 // any touch inside the focused task itself.
1864 if (!mTmpRect2.isEmpty()) {
1865 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1866 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001867 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001868 final WindowState inputMethod = mService.mInputMethodWindow;
1869 if (inputMethod != null && inputMethod.isVisibleLw()) {
1870 // If the input method is visible and the user is typing, we don't want these touch
1871 // events to be intercepted and used to change focus. This would likely cause a
1872 // disappearance of the input method.
1873 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001874 if (inputMethod.getDisplayId() == mDisplayId) {
1875 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1876 } else {
1877 // IME is on a different display, so we need to update its tap detector.
1878 // TODO(multidisplay): Remove when IME will always appear on same display.
1879 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1880 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001881 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001882 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1883 WindowState win = mTapExcludedWindows.get(i);
1884 win.getTouchableRegion(mTmpRegion);
1885 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1886 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001887 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001888 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001889 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001890 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001891 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1892 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001893 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001894 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001895 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001896 }
1897
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001898 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001899 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001900 super.switchUser();
1901 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001902 }
1903
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001904 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001905 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1906 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001907 }
1908 }
1909
1910 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001911 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001912 }
1913
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001914 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001915 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001916 }
1917
1918 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001919 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001920 }
1921
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001922 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001923 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001924 }
1925
Wale Ogunwale10124582016-09-15 20:25:50 -07001926 @Override
1927 void removeIfPossible() {
1928 if (isAnimating()) {
1929 mDeferredRemoval = true;
1930 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001931 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001932 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001933 }
1934
Wale Ogunwale10124582016-09-15 20:25:50 -07001935 @Override
1936 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001937 mRemovingDisplay = true;
1938 try {
1939 super.removeImmediately();
1940 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1941 mDimLayerController.close();
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001942 if (mService.canDispatchPointerEvents()) {
1943 if (mTapDetector != null) {
1944 mService.unregisterPointerEventListener(mTapDetector);
1945 }
1946 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1947 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1948 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001949 }
1950 } finally {
1951 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001952 }
Craig Mautner95da1082014-02-24 17:54:35 -08001953 }
1954
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001955 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001956 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001957 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001958 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1959
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001960 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001961 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001962 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001963 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001964 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001965 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001966 }
1967
Andrii Kulian0214ed92017-05-16 13:44:05 -07001968 /** @return 'true' if removal of this display content is deferred due to active animation. */
1969 boolean isRemovalDeferred() {
1970 return mDeferredRemoval;
1971 }
1972
Wale Ogunwale10124582016-09-15 20:25:50 -07001973 boolean animateForIme(float interpolatedValue, float animationTarget,
1974 float dividerAnimationTarget) {
1975 boolean updated = false;
1976
Wale Ogunwale61911492017-10-11 08:50:50 -07001977 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1978 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001979 if (stack == null || !stack.isAdjustedForIme()) {
1980 continue;
1981 }
1982
1983 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1984 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1985 updated = true;
1986 } else {
1987 mDividerControllerLocked.mLastAnimationProgress =
1988 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1989 mDividerControllerLocked.mLastDividerProgress =
1990 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1991 updated |= stack.updateAdjustForIme(
1992 mDividerControllerLocked.mLastAnimationProgress,
1993 mDividerControllerLocked.mLastDividerProgress,
1994 false /* force */);
1995 }
1996 if (interpolatedValue >= 1f) {
1997 stack.endImeAdjustAnimation();
1998 }
1999 }
2000
2001 return updated;
2002 }
2003
2004 boolean clearImeAdjustAnimation() {
2005 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002006 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2007 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002008 if (stack != null && stack.isAdjustedForIme()) {
2009 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2010 changed = true;
2011 }
2012 }
2013 return changed;
2014 }
2015
2016 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002017 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2018 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002019 if (stack.isVisible() && stack.isAdjustedForIme()) {
2020 stack.beginImeAdjustAnimation();
2021 }
2022 }
2023 }
2024
2025 void adjustForImeIfNeeded() {
2026 final WindowState imeWin = mService.mInputMethodWindow;
2027 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2028 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002029 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002030 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2031 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2032 imeTargetStack.getDockSide() : DOCKED_INVALID;
2033 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2034 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2035 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002036 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002037 final boolean imeHeightChanged = imeVisible &&
2038 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2039
2040 // The divider could be adjusted for IME position, or be thinner than usual,
2041 // or both. There are three possible cases:
2042 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2043 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2044 // - If IME is not visible, divider is not moved and is normal width.
2045
2046 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002047 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2048 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002049 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002050 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2051 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002052 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2053 } else {
2054 stack.resetAdjustedForIme(false);
2055 }
2056 }
2057 mDividerControllerLocked.setAdjustedForIme(
2058 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2059 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002060 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2061 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002062 stack.resetAdjustedForIme(!dockVisible);
2063 }
2064 mDividerControllerLocked.setAdjustedForIme(
2065 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2066 }
Winson Chung655332c2016-10-31 13:14:28 -07002067 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002068 }
2069
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002070 void setInputMethodAnimLayerAdjustment(int adj) {
2071 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2072 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002073 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002074 }
2075
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002076 /**
2077 * If a window that has an animation specifying a colored background and the current wallpaper
2078 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2079 * suddenly disappear.
2080 */
2081 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002082 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2083 w -> w.mIsWallpaper && w.isVisibleNow());
2084
2085 if (visibleWallpaper != null) {
2086 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002087 }
2088 return winAnimator.mAnimLayer;
2089 }
2090
Wale Ogunwale10124582016-09-15 20:25:50 -07002091 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002092 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2093 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002094 stack.prepareFreezingTaskBounds();
2095 }
2096 }
2097
Wale Ogunwale94744212015-09-21 19:01:47 -07002098 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002099 getLogicalDisplayRect(mTmpRect, newRotation);
2100
2101 // Compute a transform matrix to undo the coordinate space transformation,
2102 // and present the window at the same physical position it previously occupied.
2103 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2104 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2105
2106 mTmpRectF.set(bounds);
2107 mTmpMatrix.mapRect(mTmpRectF);
2108 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002109 }
2110
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002111 static int deltaRotation(int oldRotation, int newRotation) {
2112 int delta = newRotation - oldRotation;
2113 if (delta < 0) delta += 4;
2114 return delta;
2115 }
2116
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002117 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002118 Matrix outMatrix) {
2119 // For rotations without Z-ordering we don't need the target rectangle's position.
2120 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2121 displayHeight, outMatrix);
2122 }
2123
2124 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2125 float displayWidth, float displayHeight, Matrix outMatrix) {
2126 switch (rotation) {
2127 case ROTATION_0:
2128 outMatrix.reset();
2129 break;
2130 case ROTATION_270:
2131 outMatrix.setRotate(270, 0, 0);
2132 outMatrix.postTranslate(0, displayHeight);
2133 outMatrix.postTranslate(rectTop, 0);
2134 break;
2135 case ROTATION_180:
2136 outMatrix.reset();
2137 break;
2138 case ROTATION_90:
2139 outMatrix.setRotate(90, 0, 0);
2140 outMatrix.postTranslate(displayWidth, 0);
2141 outMatrix.postTranslate(-rectTop, rectLeft);
2142 break;
2143 }
2144 }
2145
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002146 @CallSuper
2147 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002148 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002149 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002150 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002151 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002152 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2153 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002154 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002155 }
2156 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2157 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002158 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2159 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002160 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002161 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002162 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2163 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002164 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002165 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002166 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2167 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002168 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002169 }
2170 proto.write(DPI, mBaseDisplayDensity);
2171 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002172 proto.write(ROTATION, mRotation);
2173 final ScreenRotationAnimation screenRotationAnimation =
2174 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2175 if (screenRotationAnimation != null) {
2176 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2177 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002178 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002179 proto.end(token);
2180 }
2181
Craig Mautnera91f9e22012-09-14 16:22:08 -07002182 public void dump(String prefix, PrintWriter pw) {
2183 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2184 final String subPrefix = " " + prefix;
2185 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2186 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2187 pw.print("dpi");
2188 if (mInitialDisplayWidth != mBaseDisplayWidth
2189 || mInitialDisplayHeight != mBaseDisplayHeight
2190 || mInitialDisplayDensity != mBaseDisplayDensity) {
2191 pw.print(" base=");
2192 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2193 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2194 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002195 if (mDisplayScalingDisabled) {
2196 pw.println(" noscale");
2197 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002198 pw.print(" cur=");
2199 pw.print(mDisplayInfo.logicalWidth);
2200 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2201 pw.print(" app=");
2202 pw.print(mDisplayInfo.appWidth);
2203 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2204 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2205 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2206 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2207 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002208 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002209 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002210 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002211
Craig Mautnerdc548482014-02-05 13:35:24 -08002212 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002213 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002214 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2215 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002216 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002217 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002218
Craig Mautnerdc548482014-02-05 13:35:24 -08002219 pw.println();
2220 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002221 pw.println();
2222 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002223 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002224 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002225 pw.print(" Exiting #"); pw.print(i);
2226 pw.print(' '); pw.print(token);
2227 pw.println(':');
2228 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002229 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002230 }
Craig Mautner59c00972012-07-30 12:10:24 -07002231 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002232 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002233 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002234
2235 // Dump stack references
2236 final TaskStack homeStack = getHomeStack();
2237 if (homeStack != null) {
2238 pw.println(prefix + "homeStack=" + homeStack.getName());
2239 }
2240 final TaskStack pinnedStack = getPinnedStack();
2241 if (pinnedStack != null) {
2242 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2243 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002244 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002245 if (splitScreenPrimaryStack != null) {
2246 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2247 }
2248
2249 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002250 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002251 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002252 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002253
2254 if (mInputMethodAnimLayerAdjustment != 0) {
2255 pw.println(subPrefix
2256 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2257 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002258
2259 pw.println();
2260 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002261 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002262
2263 @Override
2264 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002265 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002266 }
2267
2268 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002269 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002270 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002271
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002272 /** Returns true if the stack in the windowing mode is visible. */
2273 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002274 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002275 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002276 }
2277
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002278 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002279 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002280 final int x = (int) xf;
2281 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002282 final WindowState touchedWin = getWindow(w -> {
2283 final int flags = w.mAttrs.flags;
2284 if (!w.isVisibleLw()) {
2285 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002286 }
2287 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002288 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002289 }
2290
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002291 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002292 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002293 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002294 }
2295
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002297
2298 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002299 return mTmpRegion.contains(x, y) || touchFlags == 0;
2300 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002301
2302 return touchedWin;
2303 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002304
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002305 boolean canAddToastWindowForUid(int uid) {
2306 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002307 // Also if the app is focused adding more than one toast at
2308 // a time for better backwards compatibility.
2309 final WindowState focusedWindowForUid = getWindow(w ->
2310 w.mOwnerUid == uid && w.isFocused());
2311 if (focusedWindowForUid != null) {
2312 return true;
2313 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002314 final WindowState win = getWindow(w ->
2315 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2316 && !w.mWindowRemovalAllowed);
2317 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002318 }
2319
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002320 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002321 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2322 return;
2323 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002324
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002325 // Used to communicate the old focus to the callback method.
2326 mTmpWindow = oldFocus;
2327
2328 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002329 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002330
2331 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002333
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002334 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002335
2336 if (mTmpWindow == null) {
2337 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2338 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002339 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002340 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002341 }
2342
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002343 /** Updates the layer assignment of windows on this display. */
2344 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002345 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002346 if (setLayoutNeeded) {
2347 setLayoutNeeded();
2348 }
2349 }
2350
Wale Ogunwale1666e312016-12-16 11:27:18 -08002351 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2352 // moving containers or resizing them. Need to investigate the best way to have it automatically
2353 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002354 void layoutAndAssignWindowLayersIfNeeded() {
2355 mService.mWindowsChanged = true;
2356 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002357
2358 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2359 false /*updateInputWindows*/)) {
2360 assignWindowLayers(false /* setLayoutNeeded */);
2361 }
2362
2363 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2364 mService.mWindowPlacerLocked.performSurfacePlacement();
2365 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2366 }
2367
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002368 /** Returns true if a leaked surface was destroyed */
2369 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002370 // Used to indicate that a surface was leaked.
2371 mTmpWindow = null;
2372 forAllWindows(w -> {
2373 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002374 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002375 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002376 }
2377 if (!mService.mSessions.contains(wsa.mSession)) {
2378 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002379 + w + " surface=" + wsa.mSurfaceController
2380 + " token=" + w.mToken
2381 + " pid=" + w.mSession.mPid
2382 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002383 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002384 mService.mForceRemoves.add(w);
2385 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002386 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002387 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002388 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002389 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002390 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002391 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002393 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002394 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002395
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002396 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002397 }
2398
2399 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002400 * Determine and return the window that should be the IME target.
2401 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2402 * @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 +00002403 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002404 WindowState computeImeTarget(boolean updateImeTarget) {
2405 if (mService.mInputMethodWindow == null) {
2406 // There isn't an IME so there shouldn't be a target...That was easy!
2407 if (updateImeTarget) {
2408 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2409 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2410 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2411 }
2412 return null;
2413 }
2414
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002415 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2416 // same display. Or even when the current IME/target are not on the same screen as the next
2417 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002418 mUpdateImeTarget = updateImeTarget;
2419 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002420
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002421
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002422 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2423 // to be on top of it, but it is not -really- where input will go. So look down below
2424 // for a real window to target...
2425 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2426 final AppWindowToken token = target.mAppToken;
2427 if (token != null) {
2428 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2429 if (betterTarget != null) {
2430 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002431 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002432 }
2433 }
2434
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002435 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2436 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002437
2438 // Now, a special case -- if the last target's window is in the process of exiting, and is
2439 // above the new target, keep on the last target to avoid flicker. Consider for example a
2440 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2441 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2442 // scrim.
2443 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002444 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2445 && (target == null
2446 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002447 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002448 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002449 }
2450
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002451 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2452 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002453
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002454 if (target == null) {
2455 if (updateImeTarget) {
2456 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2457 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2458 + Debug.getCallers(4) : ""));
2459 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2460 }
2461
2462 return null;
2463 }
2464
2465 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002466 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2467 if (token != null) {
2468
2469 // Now some fun for dealing with window animations that modify the Z order. We need
2470 // to look at all windows below the current target that are in this app, finding the
2471 // highest visible one in layering.
2472 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002473 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002474 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002475 }
2476
2477 if (highestTarget != null) {
2478 final AppTransition appTransition = mService.mAppTransition;
2479 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2480 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2481 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002482 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002483
2484 if (appTransition.isTransitionSet()) {
2485 // If we are currently setting up for an animation, hold everything until we
2486 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2488 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002489 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002490 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002491 // If the window we are currently targeting is involved with an animation,
2492 // and it is on top of the next target we will be over, then hold off on
2493 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002494 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2495 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002496 }
2497 }
2498 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002499
2500 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2501 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2502 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002503 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002504 }
2505
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002506 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002507 }
2508
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002509 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2510 if (target == mService.mInputMethodTarget
2511 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2512 && mInputMethodAnimLayerAdjustment == layerAdj) {
2513 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002514 }
2515
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002516 mService.mInputMethodTarget = target;
2517 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2518 setInputMethodAnimLayerAdjustment(layerAdj);
2519 assignWindowLayers(false /* setLayoutNeeded */);
2520 }
2521
2522 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2523 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2524 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2525 }
2526
2527 // Used to indicate we have reached the first window in the range we are interested in.
2528 mTmpWindow = null;
2529
2530 // TODO: Figure-out a more efficient way to do this.
2531 final WindowState candidate = getWindow(w -> {
2532 if (w == top) {
2533 // Reached the first window in the range we are interested in.
2534 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002535 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002536 if (mTmpWindow == null) {
2537 return false;
2538 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002539
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002540 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2541 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002542 }
2543 // If we reached the bottom of the range of windows we are considering,
2544 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002545 if (w == bottom) {
2546 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002547 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002548 return false;
2549 });
2550
2551 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002552 }
2553
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002554 void setLayoutNeeded() {
2555 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2556 mLayoutNeeded = true;
2557 }
2558
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002559 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002560 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2561 mLayoutNeeded = false;
2562 }
2563
2564 boolean isLayoutNeeded() {
2565 return mLayoutNeeded;
2566 }
2567
Wale Ogunwale02319a62016-09-26 15:21:22 -07002568 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2569 if (mTokenMap.isEmpty()) {
2570 return;
2571 }
2572 pw.println(" Display #" + mDisplayId);
2573 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2574 while (it.hasNext()) {
2575 final WindowToken token = it.next();
2576 pw.print(" ");
2577 pw.print(token);
2578 if (dumpAll) {
2579 pw.println(':');
2580 token.dump(pw, " ");
2581 } else {
2582 pw.println();
2583 }
2584 }
2585 }
2586
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002587 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002588 final int[] index = new int[1];
2589 forAllWindows(w -> {
2590 final WindowStateAnimator wAnim = w.mWinAnimator;
2591 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2592 index[0] = index[0] + 1;
2593 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002594 }
2595
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002596 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002597 forAllWindows(w -> {
2598 w.mWinAnimator.enableSurfaceTrace(fd);
2599 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002600 }
2601
2602 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002603 forAllWindows(w -> {
2604 w.mWinAnimator.disableSurfaceTrace();
2605 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002606 }
2607
Jorim Jaggife762342016-10-13 14:33:27 +02002608 /**
2609 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2610 */
2611 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2612 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002613 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002614 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2615 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002617 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2618 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002619 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002620 }
2621
Wale Ogunwale494009b82016-10-21 09:01:38 -07002622 boolean checkWaitingForWindows() {
2623
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002624 mHaveBootMsg = false;
2625 mHaveApp = false;
2626 mHaveWallpaper = false;
2627 mHaveKeyguard = true;
2628
2629 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002630 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2631 return true;
2632 }
2633 if (w.isDrawnLw()) {
2634 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002635 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002636 } else if (w.mAttrs.type == TYPE_APPLICATION
2637 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002638 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002639 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002640 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002641 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002643 }
2644 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002645 return false;
2646 });
2647
2648 if (visibleWindow != null) {
2649 // We have a visible window.
2650 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002651 }
2652
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002653 // if the wallpaper service is disabled on the device, we're never going to have
2654 // wallpaper, don't bother waiting for it
2655 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2656 com.android.internal.R.bool.config_enableWallpaperService)
2657 && !mService.mOnlyCore;
2658
Wale Ogunwale494009b82016-10-21 09:01:38 -07002659 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2660 "******** booted=" + mService.mSystemBooted
2661 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002662 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2663 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2664 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002665
2666 // If we are turning on the screen to show the boot message, don't do it until the boot
2667 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002668 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002669 return true;
2670 }
2671
2672 // If we are turning on the screen after the boot is completed normally, don't do so until
2673 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002674 if (mService.mSystemBooted
2675 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002676 return true;
2677 }
2678
2679 return false;
2680 }
2681
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002682 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002683 mTmpWindowAnimator = animator;
2684 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002685 }
2686
2687 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002688 resetAnimationBackgroundAnimator();
2689
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002690 // Used to indicate a detached wallpaper.
2691 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002692 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002693
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002694 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002695
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002696 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002697 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002698 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2699 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002700 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2701 }
2702 }
2703
2704 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002705 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002706 }
2707
Wale Ogunwale494009b82016-10-21 09:01:38 -07002708 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002709 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002710 if (imFocus == null) {
2711 return false;
2712 }
2713
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002714 if (DEBUG_INPUT_METHOD) {
2715 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2716 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2717 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002718 }
2719
Wale Ogunwale494009b82016-10-21 09:01:38 -07002720 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2721
2722 if (DEBUG_INPUT_METHOD) {
2723 Slog.i(TAG_WM, "IM target client: " + imeClient);
2724 if (imeClient != null) {
2725 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2726 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2727 }
2728 }
2729
2730 return imeClient != null && imeClient.asBinder() == client.asBinder();
2731 }
2732
2733 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002734 final WindowState win = getWindow(
2735 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2736 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002737 }
2738
2739 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002740 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002741 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002742 final int curValue = w.mSystemUiVisibility;
2743 final int diff = (curValue ^ visibility) & globalDiff;
2744 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002745 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002746 w.mSeq++;
2747 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002748 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002749 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2750 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002751 visibility, newValue, diff);
2752 }
2753 } catch (RemoteException e) {
2754 // so sorry
2755 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002756 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002757 }
2758
2759 void onWindowFreezeTimeout() {
2760 Slog.w(TAG_WM, "Window freeze timeout expired.");
2761 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002762
2763 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002764 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002765 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002766 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002767 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002768 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2769 - mService.mDisplayFreezeTime);
2770 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002771 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002772 mService.mWindowPlacerLocked.performSurfacePlacement();
2773 }
2774
2775 void waitForAllWindowsDrawn() {
2776 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002777 forAllWindows(w -> {
2778 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2779 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2780 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002781 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002782 w.mLastContentInsets.set(-1, -1, -1, -1);
2783 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002784 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002785 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002786 }
2787
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002788 // TODO: Super crazy long method that should be broken down...
2789 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2790
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002791 final int dw = mDisplayInfo.logicalWidth;
2792 final int dh = mDisplayInfo.logicalHeight;
2793 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2794
2795 mTmpUpdateAllDrawn.clear();
2796
2797 int repeats = 0;
2798 do {
2799 repeats++;
2800 if (repeats > 6) {
2801 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2802 clearLayoutNeeded();
2803 break;
2804 }
2805
2806 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2807 pendingLayoutChanges);
2808
Andrii Kulian839def92016-11-02 10:58:58 -07002809 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2810 // the wallpaper window jumping across displays.
2811 // Remove check for default display when there will be support for multiple wallpaper
2812 // targets (on different displays).
2813 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002814 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002815 }
2816
2817 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2818 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2819 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2820 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002821 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002822 }
2823 }
2824
2825 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2826 setLayoutNeeded();
2827 }
2828
2829 // FIRST LOOP: Perform a layout, if needed.
2830 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2831 performLayout(repeats == 1, false /* updateInputWindows */);
2832 } else {
2833 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2834 }
2835
2836 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2837 pendingLayoutChanges = 0;
2838
2839 if (isDefaultDisplay) {
2840 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002841 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002842 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2843 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2844 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2845 }
2846 } while (pendingLayoutChanges != 0);
2847
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002848 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002849 resetDimming();
2850
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002851 mTmpRecoveringMemory = recoveringMemory;
2852 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002853
2854 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002855 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2856 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2857 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002858 true /* inTraversal, must call performTraversalInTrans... below */);
2859
2860 stopDimmingIfNeeded();
2861
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002862 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2863 if (wallpaperVisible != mLastWallpaperVisible) {
2864 mLastWallpaperVisible = wallpaperVisible;
2865 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2866 }
2867
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002868 while (!mTmpUpdateAllDrawn.isEmpty()) {
2869 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2870 // See if any windows have been drawn, so they (and others associated with them)
2871 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002872 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002873 }
2874
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002875 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002876 }
2877
2878 void performLayout(boolean initial, boolean updateInputWindows) {
2879 if (!isLayoutNeeded()) {
2880 return;
2881 }
2882 clearLayoutNeeded();
2883
2884 final int dw = mDisplayInfo.logicalWidth;
2885 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002886 if (DEBUG_LAYOUT) {
2887 Slog.v(TAG, "-------------------------------------");
2888 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2889 }
2890
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002891 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2892 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2893 // display info above...
2894 mDisplayFrames.mRotation = mRotation;
2895 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002896 if (isDefaultDisplay) {
2897 // Not needed on non-default displays.
2898 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2899 mService.mScreenRect.set(0, 0, dw, dh);
2900 }
2901
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002902 int seq = mService.mLayoutSeq + 1;
2903 if (seq < 0) seq = 0;
2904 mService.mLayoutSeq = seq;
2905
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002906 // Used to indicate that we have processed the dream window and all additional windows are
2907 // behind it.
2908 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002909 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002910
2911 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002912 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002913
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002914 // Used to indicate that we have processed the dream window and all additional attached
2915 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002916 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002917 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002918
2919 // Now perform layout of attached windows, which usually depend on the position of the
2920 // window they are attached to. XXX does not deal with windows that are attached to windows
2921 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002922 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002923
2924 // Window frames may have changed. Tell the input dispatcher about it.
2925 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2926 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2927 if (updateInputWindows) {
2928 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2929 }
2930
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002931 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2932 }
2933
2934 /**
2935 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2936 * In portrait mode, it grabs the full screenshot.
2937 *
2938 * @param width the width of the target bitmap
2939 * @param height the height of the target bitmap
2940 * @param includeFullDisplay true if the screen should not be cropped before capture
2941 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2942 * @param config of the output bitmap
2943 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002944 * @param includeDecor whether to include window decors, like the status or navigation bar
2945 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002946 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002947 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002948 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002949 boolean wallpaperOnly, boolean includeDecor) {
2950 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2951 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002952 if (bitmap == null) {
2953 return null;
2954 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002955
2956 if (DEBUG_SCREENSHOT) {
2957 // TEST IF IT's ALL BLACK
2958 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2959 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2960 bitmap.getHeight());
2961 boolean allBlack = true;
2962 final int firstColor = buffer[0];
2963 for (int i = 0; i < buffer.length; i++) {
2964 if (buffer[i] != firstColor) {
2965 allBlack = false;
2966 break;
2967 }
2968 }
2969 if (allBlack) {
2970 final WindowState appWin = mScreenshotApplicationState.appWin;
2971 final int maxLayer = mScreenshotApplicationState.maxLayer;
2972 final int minLayer = mScreenshotApplicationState.minLayer;
2973 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2974 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2975 (appWin != null ?
2976 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2977 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2978 }
2979 }
2980
2981 // Create a copy of the screenshot that is immutable and backed in ashmem.
2982 // This greatly reduces the overhead of passing the bitmap between processes.
2983 Bitmap ret = bitmap.createAshmemBitmap(config);
2984 bitmap.recycle();
2985 return ret;
2986 }
2987
2988 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2989 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2990 boolean includeDecor) {
2991 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2992 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2993 }
2994
2995 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2996 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2997 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002998 int dw = mDisplayInfo.logicalWidth;
2999 int dh = mDisplayInfo.logicalHeight;
3000 if (dw == 0 || dh == 0) {
3001 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3002 + ": returning null. logical widthxheight=" + dw + "x" + dh);
3003 return null;
3004 }
3005
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003006 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003007
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003008 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003009 final Rect frame = new Rect();
3010 final Rect stackBounds = new Rect();
3011
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003012 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003013 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08003014 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003015 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02003016 if (!mService.mPolicy.isScreenOn()) {
3017 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
3018 + " was off.");
3019 return null;
3020 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003021 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003022 mScreenshotApplicationState.appWin = null;
3023 forAllWindows(w -> {
3024 if (!w.mHasSurface) {
3025 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003026 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003027 if (w.mLayer >= aboveAppLayer) {
3028 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003029 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003030 if (wallpaperOnly && !w.mIsWallpaper) {
3031 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003032 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003033 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003034 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003035 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003036 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3037 // then the target window state is this one.
3038 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003039 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003040 }
3041
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003042 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003043 // We have not ran across the target window yet, so it is probably behind
3044 // the wallpaper. This can happen when the keyguard is up and all windows
3045 // are moved behind the wallpaper. We don't want to include the wallpaper
3046 // layer in the screenshot as it will cover-up the layer of the target
3047 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003048 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003049 }
3050 // Fall through. The target window is in front of the wallpaper. For this
3051 // case we want to include the wallpaper layer in the screenshot because
3052 // the target window might have some transparent areas.
3053 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003054 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003055 // This app window is of no interest if it is not associated with the
3056 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003057 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003058 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003059 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003060 }
3061
3062 // Include this window.
3063
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003064 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003065 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003066 if (mScreenshotApplicationState.maxLayer < layer) {
3067 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003068 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003069 if (mScreenshotApplicationState.minLayer > layer) {
3070 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003071 }
3072
3073 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003074 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3075 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003076 final Task task = w.getTask();
3077 if (task != null) {
3078 task.getBounds(frame);
3079 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003080
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003081 // No task bounds? Too bad! Ain't no screenshot then.
3082 return true;
3083 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003084 } else {
3085 final Rect wf = w.mFrame;
3086 final Rect cr = w.mContentInsets;
3087 int left = wf.left + cr.left;
3088 int top = wf.top + cr.top;
3089 int right = wf.right - cr.right;
3090 int bottom = wf.bottom - cr.bottom;
3091 frame.union(left, top, right, bottom);
3092 w.getVisibleBounds(stackBounds);
3093 if (!Rect.intersects(frame, stackBounds)) {
3094 // Set frame empty if there's no intersection.
3095 frame.setEmpty();
3096 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003097 }
3098 }
3099
3100 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003101 (w.mAppToken != null && w.mAppToken.token == appToken)
3102 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi406386a82017-09-15 14:17:40 +02003103 if (foundTargetWs && winAnim.getShown() && winAnim.mLastAlpha > 0f) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003104 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003105 }
3106
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003107 if (w.isObscuringDisplay()){
3108 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003109 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003110 return false;
3111 }, true /* traverseTopToBottom */);
3112
3113 final WindowState appWin = mScreenshotApplicationState.appWin;
3114 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3115 final int maxLayer = mScreenshotApplicationState.maxLayer;
3116 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003117
3118 if (appToken != null && appWin == null) {
3119 // Can't find a window to snapshot.
3120 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3121 "Screenshot: Couldn't find a surface matching " + appToken);
3122 return null;
3123 }
3124
3125 if (!screenshotReady) {
3126 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3127 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3128 appWin.mWinAnimator.mDrawState)));
3129 return null;
3130 }
3131
3132 // Screenshot is ready to be taken. Everything from here below will continue
3133 // through the bottom of the loop and return a value. We only stay in the loop
3134 // because we don't want to release the mWindowMap lock until the screenshot is
3135 // taken.
3136
3137 if (maxLayer == 0) {
3138 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3139 + ": returning null maxLayer=" + maxLayer);
3140 return null;
3141 }
3142
Jorim Jaggi02886a82016-12-06 09:10:06 -08003143 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003144 // Constrain frame to the screen size.
3145 if (!frame.intersect(0, 0, dw, dh)) {
3146 frame.setEmpty();
3147 }
3148 } else {
3149 // Caller just wants entire display.
3150 frame.set(0, 0, dw, dh);
3151 }
3152 if (frame.isEmpty()) {
3153 return null;
3154 }
3155
3156 if (width < 0) {
3157 width = (int) (frame.width() * frameScale);
3158 }
3159 if (height < 0) {
3160 height = (int) (frame.height() * frameScale);
3161 }
3162
3163 // Tell surface flinger what part of the image to crop. Take the top
3164 // right part of the application, and crop the larger dimension to fit.
3165 Rect crop = new Rect(frame);
3166 if (width / (float) frame.width() < height / (float) frame.height()) {
3167 int cropWidth = (int)((float)width / (float)height * frame.height());
3168 crop.right = crop.left + cropWidth;
3169 } else {
3170 int cropHeight = (int)((float)height / (float)width * frame.width());
3171 crop.bottom = crop.top + cropHeight;
3172 }
3173
3174 // The screenshot API does not apply the current screen rotation.
3175 int rot = mDisplay.getRotation();
3176
3177 if (rot == ROTATION_90 || rot == ROTATION_270) {
3178 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3179 }
3180
3181 // Surfaceflinger is not aware of orientation, so convert our logical
3182 // crop to surfaceflinger's portrait orientation.
3183 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3184
3185 if (DEBUG_SCREENSHOT) {
3186 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3187 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003188 forAllWindows(w -> {
3189 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3190 Slog.i(TAG_WM, w + ": " + w.mLayer
3191 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003192 + " surfaceLayer=" + ((controller == null)
3193 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003194 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003195 }
3196
3197 final ScreenRotationAnimation screenRotationAnimation =
3198 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3199 final boolean inRotation = screenRotationAnimation != null &&
3200 screenRotationAnimation.isAnimating();
3201 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3202 "Taking screenshot while rotating");
3203
3204 // We force pending transactions to flush before taking
3205 // the screenshot by pushing an empty synchronous transaction.
3206 SurfaceControl.openTransaction();
3207 SurfaceControl.closeTransactionSync();
3208
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003209 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003210 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003211 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003212 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3213 + ") to layer " + maxLayer);
3214 return null;
3215 }
3216 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003217 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003218 }
3219
3220 // TODO: Can this use createRotationMatrix()?
3221 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3222 if (rot == Surface.ROTATION_90) {
3223 final int tmp = crop.top;
3224 crop.top = dw - crop.right;
3225 crop.right = crop.bottom;
3226 crop.bottom = dw - crop.left;
3227 crop.left = tmp;
3228 } else if (rot == Surface.ROTATION_180) {
3229 int tmp = crop.top;
3230 crop.top = dh - crop.bottom;
3231 crop.bottom = dh - tmp;
3232 tmp = crop.right;
3233 crop.right = dw - crop.left;
3234 crop.left = dw - tmp;
3235 } else if (rot == Surface.ROTATION_270) {
3236 final int tmp = crop.top;
3237 crop.top = crop.left;
3238 crop.left = dh - crop.bottom;
3239 crop.bottom = crop.right;
3240 crop.right = dh - tmp;
3241 }
3242 }
3243
3244 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003245 // Used to indicate the layout is needed.
3246 mTmpWindow = null;
3247
3248 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003249 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003250 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003251 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003252 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003253 w.setDisplayLayoutNeeded();
3254 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003255 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003256
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003257 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003258 mService.mWindowPlacerLocked.performSurfacePlacement();
3259 }
3260 }
3261
Wale Ogunwale1666e312016-12-16 11:27:18 -08003262 void setExitingTokensHasVisible(boolean hasVisible) {
3263 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3264 mExitingTokens.get(i).hasVisible = hasVisible;
3265 }
3266
3267 // Initialize state of exiting applications.
3268 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3269 }
3270
3271 void removeExistingTokensIfPossible() {
3272 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3273 final WindowToken token = mExitingTokens.get(i);
3274 if (!token.hasVisible) {
3275 mExitingTokens.remove(i);
3276 }
3277 }
3278
3279 // Time to remove any exiting applications?
3280 mTaskStackContainers.removeExistingAppTokensIfPossible();
3281 }
3282
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003283 @Override
3284 void onDescendantOverrideConfigurationChanged() {
3285 setLayoutNeeded();
3286 mService.requestTraversal();
3287 }
3288
David Stevens9440dc82017-03-16 19:00:20 -07003289 boolean okToDisplay() {
3290 if (mDisplayId == DEFAULT_DISPLAY) {
3291 return !mService.mDisplayFrozen
3292 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3293 }
3294 return mDisplayInfo.state == Display.STATE_ON;
3295 }
3296
3297 boolean okToAnimate() {
3298 return okToDisplay() &&
3299 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3300 }
3301
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003302 static final class TaskForResizePointSearchResult {
3303 boolean searchDone;
3304 Task taskForResize;
3305
3306 void reset() {
3307 searchDone = false;
3308 taskForResize = null;
3309 }
3310 }
Robert Carr3b716242016-08-16 16:02:21 -07003311
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003312 private static final class ApplySurfaceChangesTransactionState {
3313 boolean displayHasContent;
3314 boolean obscured;
3315 boolean syswin;
3316 boolean focusDisplayed;
3317 float preferredRefreshRate;
3318 int preferredModeId;
3319
3320 void reset() {
3321 displayHasContent = false;
3322 obscured = false;
3323 syswin = false;
3324 focusDisplayed = false;
3325 preferredRefreshRate = 0;
3326 preferredModeId = 0;
3327 }
3328 }
3329
3330 private static final class ScreenshotApplicationState {
3331 WindowState appWin;
3332 int maxLayer;
3333 int minLayer;
3334 boolean screenshotReady;
3335
3336 void reset(boolean screenshotReady) {
3337 appWin = null;
3338 maxLayer = 0;
3339 minLayer = 0;
3340 this.screenshotReady = screenshotReady;
3341 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3342 }
3343 }
3344
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003345 /**
3346 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3347 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3348 * homogeneous children type which is currently required by sub-classes of
3349 * {@link WindowContainer} class.
3350 */
3351 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3352
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003353 @Override
3354 boolean fillsParent() {
3355 return true;
3356 }
3357
3358 @Override
3359 boolean isVisible() {
3360 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003361 }
3362 }
3363
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003364 /**
3365 * Window container class that contains all containers on this display relating to Apps.
3366 * I.e Activities.
3367 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003368 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003369
Wale Ogunwale61911492017-10-11 08:50:50 -07003370 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3371 // through the list to find them.
3372 private TaskStack mHomeStack = null;
3373 private TaskStack mPinnedStack = null;
3374 private TaskStack mSplitScreenPrimaryStack = null;
3375
3376 /**
3377 * Returns the topmost stack on the display that is compatible with the input windowing mode
3378 * and activity type. Null is no compatible stack on the display.
3379 */
3380 TaskStack getStack(int windowingMode, int activityType) {
3381 if (activityType == ACTIVITY_TYPE_HOME) {
3382 return mHomeStack;
3383 }
3384 if (windowingMode == WINDOWING_MODE_PINNED) {
3385 return mPinnedStack;
3386 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3387 return mSplitScreenPrimaryStack;
3388 }
3389 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3390 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003391 if (activityType == ACTIVITY_TYPE_UNDEFINED
3392 && windowingMode == stack.getWindowingMode()) {
3393 // Passing in undefined type means we want to match the topmost stack with the
3394 // windowing mode.
3395 return stack;
3396 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003397 if (stack.isCompatible(windowingMode, activityType)) {
3398 return stack;
3399 }
3400 }
3401 return null;
3402 }
3403
3404 @VisibleForTesting
3405 TaskStack getTopStack() {
3406 return mTaskStackContainers.getChildCount() > 0
3407 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3408 }
3409
3410 TaskStack getHomeStack() {
3411 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3412 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3413 }
3414 return mHomeStack;
3415 }
3416
3417 TaskStack getPinnedStack() {
3418 return mPinnedStack;
3419 }
3420
Matthew Ng64e77cf2017-10-31 14:01:31 -07003421 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003422 return mSplitScreenPrimaryStack;
3423 }
3424
Andrii Kulian839def92016-11-02 10:58:58 -07003425 /**
3426 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003427 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003428 */
3429 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003430 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003431 addChild(stack, onTop);
3432 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003433 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003434
Wale Ogunwale61911492017-10-11 08:50:50 -07003435 void onStackWindowingModeChanged(TaskStack stack) {
3436 removeStackReferenceIfNeeded(stack);
3437 addStackReferenceIfNeeded(stack);
3438 if (stack == mPinnedStack && getTopStack() != stack) {
3439 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3440 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3441 }
3442 }
3443
3444 private void addStackReferenceIfNeeded(TaskStack stack) {
3445 if (stack.isActivityTypeHome()) {
3446 if (mHomeStack != null) {
3447 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3448 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3449 }
3450 mHomeStack = stack;
3451 }
3452 final int windowingMode = stack.getWindowingMode();
3453 if (windowingMode == WINDOWING_MODE_PINNED) {
3454 if (mPinnedStack != null) {
3455 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3456 + mPinnedStack + " already exist on display=" + this
3457 + " stack=" + stack);
3458 }
3459 mPinnedStack = stack;
3460 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3461 if (mSplitScreenPrimaryStack != null) {
3462 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3463 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3464 + " already exist on display=" + this + " stack=" + stack);
3465 }
3466 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003467 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003468 }
3469 }
3470
3471 private void removeStackReferenceIfNeeded(TaskStack stack) {
3472 if (stack == mHomeStack) {
3473 mHomeStack = null;
3474 } else if (stack == mPinnedStack) {
3475 mPinnedStack = null;
3476 } else if (stack == mSplitScreenPrimaryStack) {
3477 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003478 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3479 mService.setDockedStackCreateStateLocked(
3480 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3481 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003482 }
Andrii Kulian839def92016-11-02 10:58:58 -07003483 }
3484
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003485 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003486 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3487 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003488 addChild(stack, addIndex);
3489 setLayoutNeeded();
3490 }
3491
Wale Ogunwale61911492017-10-11 08:50:50 -07003492 @Override
3493 protected void removeChild(TaskStack stack) {
3494 super.removeChild(stack);
3495 removeStackReferenceIfNeeded(stack);
3496 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003497
3498 @Override
3499 boolean isOnTop() {
3500 // Considered always on top
3501 return true;
3502 }
3503
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003504 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003505 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003506 if (child.getWindowConfiguration().isAlwaysOnTop()
3507 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003508 // This stack is always-on-top, override the default behavior.
3509 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3510
3511 // Moving to its current position, as we must call super but we don't want to
3512 // perform any meaningful action.
3513 final int currentPosition = mChildren.indexOf(child);
3514 super.positionChildAt(currentPosition, child, false /* includingParents */);
3515 return;
3516 }
3517
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003518 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3519 super.positionChildAt(targetPosition, child, includingParents);
3520
3521 setLayoutNeeded();
3522 }
3523
3524 /**
3525 * When stack is added or repositioned, find a proper position for it.
3526 * This will make sure that pinned stack always stays on top.
3527 * @param requestedPosition Position requested by caller.
3528 * @param stack Stack to be added or positioned.
3529 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3530 * @return The proper position for the stack.
3531 */
3532 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3533 final int topChildPosition = mChildren.size() - 1;
3534 boolean toTop = requestedPosition == POSITION_TOP;
3535 toTop |= adding ? requestedPosition >= topChildPosition + 1
3536 : requestedPosition >= topChildPosition;
3537 int targetPosition = requestedPosition;
3538
Wale Ogunwale61911492017-10-11 08:50:50 -07003539 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003540 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003541 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003542 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003543 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3544 }
3545
3546 // So, stack is moved just below the pinned stack.
3547 // When we're adding a new stack the target is the current pinned stack position.
3548 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003549 // stack, because WindowContainer#positionAt() first removes element and then adds
3550 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003551 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3552 }
3553
3554 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003555 }
3556
3557 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003558 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3559 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003560 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003561 if (super.forAllWindows(callback, traverseTopToBottom)) {
3562 return true;
3563 }
3564 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3565 return true;
3566 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003567 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003568 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3569 return true;
3570 }
3571 if (super.forAllWindows(callback, traverseTopToBottom)) {
3572 return true;
3573 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003574 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003575 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003576 }
3577
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003578 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003579 boolean traverseTopToBottom) {
3580 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3581 // app tokens.
3582 // TODO: Investigate if we need to continue to do this or if we can just process them
3583 // in-order.
3584 if (traverseTopToBottom) {
3585 for (int i = mChildren.size() - 1; i >= 0; --i) {
3586 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3587 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003588 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3589 traverseTopToBottom)) {
3590 return true;
3591 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003592 }
3593 }
3594 } else {
3595 final int count = mChildren.size();
3596 for (int i = 0; i < count; ++i) {
3597 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3598 final int appTokensCount = appTokens.size();
3599 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003600 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3601 traverseTopToBottom)) {
3602 return true;
3603 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003604 }
3605 }
3606 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003607 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003608 }
3609
Wale Ogunwale1666e312016-12-16 11:27:18 -08003610 void setExitingTokensHasVisible(boolean hasVisible) {
3611 for (int i = mChildren.size() - 1; i >= 0; --i) {
3612 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3613 for (int j = appTokens.size() - 1; j >= 0; --j) {
3614 appTokens.get(j).hasVisible = hasVisible;
3615 }
3616 }
3617 }
3618
3619 void removeExistingAppTokensIfPossible() {
3620 for (int i = mChildren.size() - 1; i >= 0; --i) {
3621 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3622 for (int j = appTokens.size() - 1; j >= 0; --j) {
3623 final AppWindowToken token = appTokens.get(j);
3624 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3625 && (!token.mIsExiting || token.isEmpty())) {
3626 // Make sure there is no animation running on this token, so any windows
3627 // associated with it will be removed as soon as their animations are
3628 // complete.
3629 token.mAppAnimator.clearAnimation();
3630 token.mAppAnimator.animating = false;
3631 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3632 "performLayout: App token exiting now removed" + token);
3633 token.removeIfPossible();
3634 }
3635 }
3636 }
3637 }
3638
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003639 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003640 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003641 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3642 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003643 // Apps and their containers are not allowed to specify an orientation while the
3644 // docked or freeform stack is visible...except for the home stack/task if the
3645 // docked stack is minimized and it actually set something.
3646 if (mHomeStack != null && mHomeStack.isVisible()
3647 && mDividerControllerLocked.isMinimizedDock()) {
3648 final int orientation = mHomeStack.getOrientation();
3649 if (orientation != SCREEN_ORIENTATION_UNSET) {
3650 return orientation;
3651 }
3652 }
3653 return SCREEN_ORIENTATION_UNSPECIFIED;
3654 }
3655
3656 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003657 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3658 PackageManager.FEATURE_AUTOMOTIVE);
3659 if (isCar) {
3660 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3661 // allow anything but the default orientation.
3662 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3663 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3664 return SCREEN_ORIENTATION_UNSPECIFIED;
3665 }
3666
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003667 if (orientation != SCREEN_ORIENTATION_UNSET
3668 && orientation != SCREEN_ORIENTATION_BEHIND) {
3669 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3670 "App is requesting an orientation, return " + orientation);
3671 return orientation;
3672 }
3673
3674 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003675 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003676 // The next app has not been requested to be visible, so we keep the current orientation
3677 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003678 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003679 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003680 }
3681
3682 /**
3683 * Window container class that contains all containers on this display that are not related to
3684 * Apps. E.g. status bar.
3685 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003686 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3687 /**
3688 * Compares two child window tokens returns -1 if the first is lesser than the second in
3689 * terms of z-order and 1 otherwise.
3690 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003691 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003692 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003693 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3694 token1.mOwnerCanManageAppTokens)
3695 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3696 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003697
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003698 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3699 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3700 return false;
3701 }
3702 final int req = w.mAttrs.screenOrientation;
3703 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3704 || req == SCREEN_ORIENTATION_UNSET) {
3705 return false;
3706 }
3707 return true;
3708 };
3709
Wale Ogunwale3a931692016-11-02 16:49:48 -07003710 private final String mName;
3711 NonAppWindowContainers(String name) {
3712 mName = name;
3713 }
3714
3715 void addChild(WindowToken token) {
3716 addChild(token, mWindowComparator);
3717 }
3718
3719 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003720 int getOrientation() {
3721 final WindowManagerPolicy policy = mService.mPolicy;
3722 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003723 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003724
3725 if (win != null) {
3726 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003727 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003728 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003729 if (mService.mKeyguardGoingAway) {
3730 // Keyguard can't affect the orientation if it is going away...
3731 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3732 return SCREEN_ORIENTATION_UNSET;
3733 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003734 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003735 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003736 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003737 }
3738
Andrii Kulian8ee72852017-03-10 10:36:45 -08003739 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003740
Jorim Jaggi75520d52017-08-24 17:23:19 +02003741 if (policy.isKeyguardShowingAndNotOccluded()
3742 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003743 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003744 }
3745
3746 return SCREEN_ORIENTATION_UNSET;
3747 }
3748
3749 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003750 String getName() {
3751 return mName;
3752 }
Robert Carr3b716242016-08-16 16:02:21 -07003753 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003754
3755 /**
3756 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3757 */
3758 @FunctionalInterface
3759 private interface Screenshoter<E> {
3760 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3761 boolean useIdentityTransform, int rotation);
3762 }
Craig Mautner59c00972012-07-30 12:10:24 -07003763}