blob: 44dfa07794d26497d3c82eef0b4579f07c6f9360 [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale30e441d2017-11-09 08:28:45 -080019import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Wale Ogunwale61911492017-10-11 08:50:50 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale687b4272017-07-27 02:56:23 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070027import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
28import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070031import static android.view.Display.DEFAULT_DISPLAY;
32import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070033import static android.view.Surface.ROTATION_0;
34import static android.view.Surface.ROTATION_180;
35import static android.view.Surface.ROTATION_270;
36import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070037import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_INVALID;
40import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
43import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070045import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070046import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
47import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070052import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070053import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070054import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
55import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070056import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
58import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070059import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070060import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Adrian Roose99bc052017-11-20 17:55:31 +010061import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
62import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
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;
Robert Carrb1579c82017-09-05 14:54:47 -0700145import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700146import android.view.Surface;
147import android.view.SurfaceControl;
Robert Carrb1579c82017-09-05 14:54:47 -0700148import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700149
Bryce Lee48f4b572017-04-10 10:54:15 -0700150import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800151import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700152import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100153import com.android.server.policy.WindowManagerPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700154
Robert Carr3b716242016-08-16 16:02:21 -0700155import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700156import java.io.PrintWriter;
157import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700158import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700159import java.util.HashMap;
160import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700161import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700162import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800163import java.util.function.Consumer;
164import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700165
Craig Mautner59c00972012-07-30 12:10:24 -0700166/**
167 * Utility class for keeping track of the WindowStates and other pertinent contents of a
168 * particular Display.
169 *
170 * IMPORTANT: No method from this class should ever be used without holding
171 * WindowManagerService.mWindowMap.
172 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700173class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700174 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700175
176 /** Unique identifier of this stack. */
177 private final int mDisplayId;
178
Wale Ogunwale3a931692016-11-02 16:49:48 -0700179 /** The containers below are the only child containers the display can have. */
180 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700181 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700182 // Contains all non-app window containers that should be displayed above the app containers
183 // (e.g. Status bar)
184 private final NonAppWindowContainers mAboveAppWindowsContainers =
185 new NonAppWindowContainers("mAboveAppWindowsContainers");
186 // Contains all non-app window containers that should be displayed below the app containers
187 // (e.g. Wallpaper).
188 private final NonAppWindowContainers mBelowAppWindowsContainers =
189 new NonAppWindowContainers("mBelowAppWindowsContainers");
190 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
191 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
192 // window containers together and move them in-sync if/when needed.
193 private final NonAppWindowContainers mImeWindowsContainers =
194 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700195
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000196 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800197 private WindowState mTmpWindow2;
198 private WindowAnimator mTmpWindowAnimator;
199 private boolean mTmpRecoveringMemory;
200 private boolean mUpdateImeTarget;
201 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700202 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700203
Wale Ogunwale02319a62016-09-26 15:21:22 -0700204 // Mapping from a token IBinder to a WindowToken object on this display.
205 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
206
Andrii Kuliancd097992017-03-23 18:31:59 -0700207 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700208 int mInitialDisplayWidth = 0;
209 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700210 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700211
212 /**
213 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
214 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
215 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
216 */
Craig Mautner59c00972012-07-30 12:10:24 -0700217 int mBaseDisplayWidth = 0;
218 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700219 /**
220 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
221 * but can be set from Settings or via shell command "adb shell wm density".
222 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
223 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700224 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700225 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700226 private final DisplayInfo mDisplayInfo = new DisplayInfo();
227 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700228 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000229 DisplayFrames mDisplayFrames;
230
Andrii Kulian06d07d62017-03-14 11:11:47 -0700231 /**
232 * For default display it contains real metrics, empty for others.
233 * @see WindowManagerService#createWatermarkInTransaction()
234 */
235 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
236 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
237 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700238
Andrii Kulian06d07d62017-03-14 11:11:47 -0700239 /**
240 * Compat metrics computed based on {@link #mDisplayMetrics}.
241 * @see #updateDisplayAndOrientation(int)
242 */
243 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
244
245 /** The desired scaling factor for compatible apps. */
246 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700247
Andrii Kulian8ee72852017-03-10 10:36:45 -0800248 /**
249 * Current rotation of the display.
250 * Constants as per {@link android.view.Surface.Rotation}.
251 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700252 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800253 */
254 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700255
Andrii Kulian8ee72852017-03-10 10:36:45 -0800256 /**
257 * Last applied orientation of the display.
258 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
259 *
260 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
261 */
262 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700263
Andrii Kulian8ee72852017-03-10 10:36:45 -0800264 /**
265 * Flag indicating that the application is receiving an orientation that has different metrics
266 * than it expected. E.g. Portrait instead of Landscape.
267 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700268 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800269 */
270 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700271
Andrii Kulian8ee72852017-03-10 10:36:45 -0800272 /**
273 * Orientation forced by some window. If there is no visible window that specifies orientation
274 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
275 *
276 * @see NonAppWindowContainers#getOrientation()
277 */
278 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700279
Andrii Kulian8ee72852017-03-10 10:36:45 -0800280 /**
281 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
282 * occluded.
283 *
284 * @see NonAppWindowContainers#getOrientation()
285 */
286 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
287
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700288 /**
289 * Keep track of wallpaper visibility to notify changes.
290 */
291 private boolean mLastWallpaperVisible = false;
292
Andrii Kulian06d07d62017-03-14 11:11:47 -0700293 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700294
Craig Mautner39834192012-09-02 07:47:24 -0700295 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700296 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700297 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700298 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800299 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700300
Craig Mautnerdc548482014-02-05 13:35:24 -0800301 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700302 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800303
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700304 /** Detect user tapping outside of current focused task bounds .*/
305 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700306
Craig Mautner6601b7b2013-04-29 10:29:11 -0700307 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700308 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700309
Craig Mautner6601b7b2013-04-29 10:29:11 -0700310 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800311 private final Rect mTmpRect = new Rect();
312 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700313 private final RectF mTmpRectF = new RectF();
314 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800315 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700316
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800317 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700318
Craig Mautner95da1082014-02-24 17:54:35 -0800319 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700320 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800321
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700322 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700323 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700324
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800325 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
326
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000327 private boolean mHaveBootMsg = false;
328 private boolean mHaveApp = false;
329 private boolean mHaveWallpaper = false;
330 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700331
332 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
333
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700334 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
335 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000336 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
337 new ApplySurfaceChangesTransactionState();
338 private final ScreenshotApplicationState mScreenshotApplicationState =
339 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700340
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700341 // True if this display is in the process of being removed. Used to determine if the removal of
342 // the display's direct children should be allowed.
343 private boolean mRemovingDisplay = false;
344
Bryce Leed1871262017-06-12 14:12:29 -0700345 // {@code false} if this display is in the processing of being created.
346 private boolean mDisplayReady = false;
347
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800348 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700349 int mInputMethodAnimLayerAdjustment;
350
Robert Carrb1579c82017-09-05 14:54:47 -0700351 private final SurfaceSession mSession = new SurfaceSession();
352
353 /**
354 * We organize all top-level Surfaces in to the following layers.
355 * mOverlayLayer contains a few Surfaces which are always on top of others
356 * and omitted from Screen-Magnification ({@link WindowState#isScreenOverlay})
357 * {@link #mWindowingLayer} contains everything else.
358 */
359 private SurfaceControl mOverlayLayer;
360
361 /**
362 * See {@link #mOverlayLayer}
363 */
364 private SurfaceControl mWindowingLayer;
365
366 /**
367 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
368 * <p>
369 * For these surfaces currently we use a surface based on the larger of width or height so we
370 * don't have to resize when rotating the display.
371 */
372 private int mSurfaceSize;
373
374 /**
375 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
376 */
377 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
378
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800379 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
380 WindowStateAnimator winAnimator = w.mWinAnimator;
381 if (winAnimator.hasSurface()) {
382 final boolean wasAnimating = winAnimator.mWasAnimating;
383 final boolean nowAnimating = winAnimator.stepAnimationLocked(
384 mTmpWindowAnimator.mCurrentTime);
385 winAnimator.mWasAnimating = nowAnimating;
386 mTmpWindowAnimator.orAnimating(nowAnimating);
387
388 if (DEBUG_WALLPAPER) Slog.v(TAG,
389 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
390
391 if (wasAnimating && !winAnimator.mAnimating
392 && mWallpaperController.isWallpaperTarget(w)) {
393 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
394 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
395 if (DEBUG_LAYOUT_REPEATS) {
396 mService.mWindowPlacerLocked.debugLayoutRepeats(
397 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
398 }
399 }
400 }
401
402 final AppWindowToken atoken = w.mAppToken;
403 if (winAnimator.mDrawState == READY_TO_SHOW) {
404 if (atoken == null || atoken.allDrawn) {
405 if (w.performShowLocked()) {
406 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
407 if (DEBUG_LAYOUT_REPEATS) {
408 mService.mWindowPlacerLocked.debugLayoutRepeats(
409 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
410 }
411 }
412 }
413 }
414 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
415 if (appAnimator != null && appAnimator.thumbnail != null) {
416 if (appAnimator.thumbnailTransactionSeq
417 != mTmpWindowAnimator.mAnimTransactionSequence) {
418 appAnimator.thumbnailTransactionSeq =
419 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800420 }
421 }
422 };
423
424 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
425 final WindowStateAnimator winAnimator = w.mWinAnimator;
426 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
427 return;
428 }
429
430 final int flags = w.mAttrs.flags;
431
432 // If this window is animating, make a note that we have an animating window and take
433 // care of a request to run a detached wallpaper animation.
434 if (winAnimator.mAnimating) {
435 if (winAnimator.mAnimation != null) {
436 if ((flags & FLAG_SHOW_WALLPAPER) != 0
437 && winAnimator.mAnimation.getDetachWallpaper()) {
438 mTmpWindow = w;
439 }
440 final int color = winAnimator.mAnimation.getBackgroundColor();
441 if (color != 0) {
442 final TaskStack stack = w.getStack();
443 if (stack != null) {
444 stack.setAnimationBackground(winAnimator, color);
445 }
446 }
447 }
448 mTmpWindowAnimator.setAnimating(true);
449 }
450
451 // If this window's app token is running a detached wallpaper animation, make a note so
452 // we can ensure the wallpaper is displayed behind it.
453 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
454 if (appAnimator != null && appAnimator.animation != null
455 && appAnimator.animating) {
456 if ((flags & FLAG_SHOW_WALLPAPER) != 0
457 && appAnimator.animation.getDetachWallpaper()) {
458 mTmpWindow = w;
459 }
460
461 final int color = appAnimator.animation.getBackgroundColor();
462 if (color != 0) {
463 final TaskStack stack = w.getStack();
464 if (stack != null) {
465 stack.setAnimationBackground(winAnimator, color);
466 }
467 }
468 }
469 };
470
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800471 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
472 final int lostFocusUid = mTmpWindow.mOwnerUid;
473 final Handler handler = mService.mH;
474 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
475 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
476 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
477 w.mAttrs.hideTimeoutMilliseconds);
478 }
479 }
480 };
481
482 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
483 final AppWindowToken focusedApp = mService.mFocusedApp;
484 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
485 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
486
487 if (!w.canReceiveKeys()) {
488 return false;
489 }
490
491 final AppWindowToken wtoken = w.mAppToken;
492
493 // If this window's application has been removed, just skip it.
494 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
495 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
496 + (wtoken.removed ? "removed" : "sendingToBottom"));
497 return false;
498 }
499
500 if (focusedApp == null) {
501 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
502 + " using new focus @ " + w);
503 mTmpWindow = w;
504 return true;
505 }
506
507 if (!focusedApp.windowsAreFocusable()) {
508 // Current focused app windows aren't focusable...
509 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
510 + " focusable using new focus @ " + w);
511 mTmpWindow = w;
512 return true;
513 }
514
515 // Descend through all of the app tokens and find the first that either matches
516 // win.mAppToken (return win) or mFocusedApp (return null).
517 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
518 if (focusedApp.compareTo(wtoken) > 0) {
519 // App stack below focused app stack. No focus for you!!!
520 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
521 "findFocusedWindow: Reached focused app=" + focusedApp);
522 mTmpWindow = null;
523 return true;
524 }
525 }
526
527 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
528 mTmpWindow = w;
529 return true;
530 };
531
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800532 private final Consumer<WindowState> mPerformLayout = w -> {
533 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
534 // wasting time and funky changes while a window is animating away.
535 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
536 || w.isGoneForLayoutLw();
537
538 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
539 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
540 + " mLayoutAttached=" + w.mLayoutAttached
541 + " screen changed=" + w.isConfigChanged());
542 final AppWindowToken atoken = w.mAppToken;
543 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
544 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
545 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
546 + " parentHidden=" + w.isParentWindowHidden());
547 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
548 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
549 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
550 + " parentHidden=" + w.isParentWindowHidden());
551 }
552
553 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
554 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
555 // since that means "perform layout as normal, just don't display").
556 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
557 || ((w.isConfigChanged() || w.setReportResizeHints())
558 && !w.isGoneForLayoutLw() &&
559 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
560 (w.mHasSurface && w.mAppToken != null &&
561 w.mAppToken.layoutConfigChanges)))) {
562 if (!w.mLayoutAttached) {
563 if (mTmpInitial) {
564 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
565 w.mContentChanged = false;
566 }
567 if (w.mAttrs.type == TYPE_DREAM) {
568 // Don't layout windows behind a dream, so that if it does stuff like hide
569 // the status bar we won't get a bad transition when it goes away.
570 mTmpWindow = w;
571 }
572 w.mLayoutNeeded = false;
573 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200574 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000575 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800576 w.mLayoutSeq = mService.mLayoutSeq;
577
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200578 // If this is the first layout, we need to initialize the last inset values as
579 // otherwise we'd immediately cause an unnecessary resize.
580 if (firstLayout) {
581 w.updateLastInsetValues();
582 }
583
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800584 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
585 + " mContainingFrame=" + w.mContainingFrame
586 + " mDisplayFrame=" + w.mDisplayFrame);
587 }
588 }
589 };
590
591 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
592 if (w.mLayoutAttached) {
593 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
594 + " mViewVisibility=" + w.mViewVisibility
595 + " mRelayoutCalled=" + w.mRelayoutCalled);
596 // If this view is GONE, then skip it -- keep the current frame, and let the caller
597 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
598 // windows, since that means "perform layout as normal, just don't display").
599 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
600 return;
601 }
602 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
603 || w.mLayoutNeeded) {
604 if (mTmpInitial) {
605 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
606 w.mContentChanged = false;
607 }
608 w.mLayoutNeeded = false;
609 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000610 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800611 w.mLayoutSeq = mService.mLayoutSeq;
612 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
613 + " mContainingFrame=" + w.mContainingFrame
614 + " mDisplayFrame=" + w.mDisplayFrame);
615 }
616 } else if (w.mAttrs.type == TYPE_DREAM) {
617 // Don't layout windows behind a dream, so that if it does stuff like hide the
618 // status bar we won't get a bad transition when it goes away.
619 mTmpWindow = mTmpWindow2;
620 }
621 };
622
623 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
624 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
625 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
626 return w.canBeImeTarget();
627 };
628
629 private final Consumer<WindowState> mApplyPostLayoutPolicy =
630 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
631 mService.mInputMethodTarget);
632
633 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
634 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
635 final boolean obscuredChanged = w.mObscured !=
636 mTmpApplySurfaceChangesTransactionState.obscured;
637 final RootWindowContainer root = mService.mRoot;
638 // Only used if default window
639 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
640
641 // Update effect.
642 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
643 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
644 final boolean isDisplayed = w.isDisplayedLw();
645
646 if (isDisplayed && w.isObscuringDisplay()) {
647 // This window completely covers everything behind it, so we want to leave all
648 // of them as undimmed (for performance reasons).
649 root.mObscuringWindow = w;
650 mTmpApplySurfaceChangesTransactionState.obscured = true;
651 }
652
653 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
654 root.handleNotObscuredLocked(w,
655 mTmpApplySurfaceChangesTransactionState.obscured,
656 mTmpApplySurfaceChangesTransactionState.syswin);
657
658 if (w.mHasSurface && isDisplayed) {
659 final int type = w.mAttrs.type;
660 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
661 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
662 mTmpApplySurfaceChangesTransactionState.syswin = true;
663 }
664 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
665 && w.mAttrs.preferredRefreshRate != 0) {
666 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
667 = w.mAttrs.preferredRefreshRate;
668 }
669 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
670 && w.mAttrs.preferredDisplayModeId != 0) {
671 mTmpApplySurfaceChangesTransactionState.preferredModeId
672 = w.mAttrs.preferredDisplayModeId;
673 }
674 }
675 }
676
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800677 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
678 && mWallpaperController.isWallpaperTarget(w)) {
679 // This is the wallpaper target and its obscured state changed... make sure the
680 // current wallpaper's visibility has been updated accordingly.
681 mWallpaperController.updateWallpaperVisibility();
682 }
683
684 w.handleWindowMovedIfNeeded();
685
686 final WindowStateAnimator winAnimator = w.mWinAnimator;
687
688 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
689 w.mContentChanged = false;
690
691 // Moved from updateWindowsAndWallpaperLocked().
692 if (w.mHasSurface) {
693 // Take care of the window being ready to display.
694 final boolean committed = winAnimator.commitFinishDrawingLocked();
695 if (isDefaultDisplay && committed) {
696 if (w.mAttrs.type == TYPE_DREAM) {
697 // HACK: When a dream is shown, it may at that point hide the lock screen.
698 // So we need to redo the layout to let the phone window manager make this
699 // happen.
700 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
701 if (DEBUG_LAYOUT_REPEATS) {
702 surfacePlacer.debugLayoutRepeats(
703 "dream and commitFinishDrawingLocked true",
704 pendingLayoutChanges);
705 }
706 }
707 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
708 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
709 "First draw done in potential wallpaper target " + w);
710 root.mWallpaperMayChange = true;
711 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
712 if (DEBUG_LAYOUT_REPEATS) {
713 surfacePlacer.debugLayoutRepeats(
714 "wallpaper and commitFinishDrawingLocked true",
715 pendingLayoutChanges);
716 }
717 }
718 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700719 final TaskStack stack = w.getStack();
720 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
721 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800722 // Updates the shown frame before we set up the surface. This is needed
723 // because the resizing could change the top-left position (in addition to
724 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
725 // position the surface.
726 //
727 // If an animation is being started, we can't call this method because the
728 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700729 // we do want to update the position if the window is animating. We make an exception
730 // for the bounds animating state, where an application may have been waiting
731 // for an exit animation to start, but instead enters PiP. We need to ensure
732 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800733 winAnimator.computeShownFrameLocked();
734 }
735 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
736 }
737
738 final AppWindowToken atoken = w.mAppToken;
739 if (atoken != null) {
740 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
741 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
742 mTmpUpdateAllDrawn.add(atoken);
743 }
744 }
745
746 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
747 && w.isDisplayedLw()) {
748 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
749 }
750
751 w.updateResizingWindowIfNeeded();
752 };
753
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800754 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800755 * Create new {@link DisplayContent} instance, add itself to the root window container and
756 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700757 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800758 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700759 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
760 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700761 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700762 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700763 WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800764 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
765 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
766 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
767 + " new=" + display);
768 }
769
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700770 mDisplay = display;
771 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700772 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700773 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700774 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700775 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700776 mService = service;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000777 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700778 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800779 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700780 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700781
Robert Carrb1579c82017-09-05 14:54:47 -0700782 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
783
784 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
785 .setSize(mSurfaceSize, mSurfaceSize)
786 .setOpaque(true);
787 mWindowingLayer = b.setName("Display Root").build();
788 mOverlayLayer = b.setName("Display Overlays").build();
789
Robert Carrf59b8dd2017-10-02 18:58:36 -0700790 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700791 .setLayerStack(mWindowingLayer, mDisplayId)
792 .show(mWindowingLayer)
793 .setLayer(mOverlayLayer, 1)
794 .setLayerStack(mOverlayLayer, mDisplayId)
795 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700796 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700797
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700798 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700799 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700800 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700801 super.addChild(mAboveAppWindowsContainers, null);
802 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800803
804 // Add itself as a child to the root container.
805 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700806
807 // TODO(b/62541591): evaluate whether this is the best spot to declare the
808 // {@link DisplayContent} ready for use.
809 mDisplayReady = true;
810 }
811
812 boolean isReady() {
813 // The display is ready when the system and the individual display are both ready.
814 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700815 }
816
817 int getDisplayId() {
818 return mDisplayId;
819 }
820
Wale Ogunwale02319a62016-09-26 15:21:22 -0700821 WindowToken getWindowToken(IBinder binder) {
822 return mTokenMap.get(binder);
823 }
824
825 AppWindowToken getAppWindowToken(IBinder binder) {
826 final WindowToken token = getWindowToken(binder);
827 if (token == null) {
828 return null;
829 }
830 return token.asAppWindowToken();
831 }
832
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700833 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700834 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
835 if (dc != null) {
836 // We currently don't support adding a window token to the display if the display
837 // already has the binder mapped to another token. If there is a use case for supporting
838 // this moving forward we will either need to merge the WindowTokens some how or have
839 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700840 throw new IllegalArgumentException("Can't map token=" + token + " to display="
841 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700842 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700843 if (binder == null) {
844 throw new IllegalArgumentException("Can't map token=" + token + " to display="
845 + getName() + " binder is null");
846 }
847 if (token == null) {
848 throw new IllegalArgumentException("Can't map null token to display="
849 + getName() + " binder=" + binder);
850 }
851
Wale Ogunwale02319a62016-09-26 15:21:22 -0700852 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700853
854 if (token.asAppWindowToken() == null) {
855 // Add non-app token to container hierarchy on the display. App tokens are added through
856 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700857 switch (token.windowType) {
858 case TYPE_WALLPAPER:
859 mBelowAppWindowsContainers.addChild(token);
860 break;
861 case TYPE_INPUT_METHOD:
862 case TYPE_INPUT_METHOD_DIALOG:
863 mImeWindowsContainers.addChild(token);
864 break;
865 default:
866 mAboveAppWindowsContainers.addChild(token);
867 break;
868 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700869 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700870 }
871
872 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700873 final WindowToken token = mTokenMap.remove(binder);
874 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800875 token.setExiting();
876 }
877 return token;
878 }
879
880 /** Changes the display the input window token is housed on to this one. */
881 void reParentWindowToken(WindowToken token) {
882 final DisplayContent prevDc = token.getDisplayContent();
883 if (prevDc == this) {
884 return;
885 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800886 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
887 && token.asAppWindowToken() == null) {
888 // Removed the token from the map, but made sure it's not an app token before removing
889 // from parent.
890 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700891 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800892
893 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700894 }
895
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700896 void removeAppToken(IBinder binder) {
897 final WindowToken token = removeWindowToken(binder);
898 if (token == null) {
899 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
900 return;
901 }
902
903 final AppWindowToken appToken = token.asAppWindowToken();
904
905 if (appToken == null) {
906 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
907 return;
908 }
909
910 appToken.onRemovedFromDisplay();
911 }
912
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700913 Display getDisplay() {
914 return mDisplay;
915 }
916
Craig Mautner59c00972012-07-30 12:10:24 -0700917 DisplayInfo getDisplayInfo() {
918 return mDisplayInfo;
919 }
920
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700921 DisplayMetrics getDisplayMetrics() {
922 return mDisplayMetrics;
923 }
924
Andrii Kulian8ee72852017-03-10 10:36:45 -0800925 int getRotation() {
926 return mRotation;
927 }
928
929 void setRotation(int newRotation) {
930 mRotation = newRotation;
931 }
932
933 int getLastOrientation() {
934 return mLastOrientation;
935 }
936
937 void setLastOrientation(int orientation) {
938 mLastOrientation = orientation;
939 }
940
941 boolean getAltOrientation() {
942 return mAltOrientation;
943 }
944
945 void setAltOrientation(boolean altOrientation) {
946 mAltOrientation = altOrientation;
947 }
948
949 int getLastWindowForcedOrientation() {
950 return mLastWindowForcedOrientation;
951 }
952
Andrii Kulian06d07d62017-03-14 11:11:47 -0700953 /**
954 * Update rotation of the display.
955 *
956 * Returns true if the rotation has been changed. In this case YOU MUST CALL
957 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
958 */
959 boolean updateRotationUnchecked(boolean inTransaction) {
960 if (mService.mDeferredRotationPauseCount > 0) {
961 // Rotation updates have been paused temporarily. Defer the update until
962 // updates have been resumed.
963 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
964 return false;
965 }
966
967 ScreenRotationAnimation screenRotationAnimation =
968 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
969 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
970 // Rotation updates cannot be performed while the previous rotation change
971 // animation is still in progress. Skip this update. We will try updating
972 // again after the animation is finished and the display is unfrozen.
973 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
974 return false;
975 }
976 if (mService.mDisplayFrozen) {
977 // Even if the screen rotation animation has finished (e.g. isAnimating
978 // returns false), there is still some time where we haven't yet unfrozen
979 // the display. We also need to abort rotation here.
980 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
981 "Deferring rotation, still finishing previous rotation");
982 return false;
983 }
984
985 if (!mService.mDisplayEnabled) {
986 // No point choosing a rotation if the display is not enabled.
987 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
988 return false;
989 }
990
991 final int oldRotation = mRotation;
992 final int lastOrientation = mLastOrientation;
993 final boolean oldAltOrientation = mAltOrientation;
994 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700995 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700996 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700997
Robert Carr0e007272017-10-02 13:00:55 -0700998 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700999 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
1000 if (seamlessRotated != null) {
1001 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1002 // to complete (that is, waiting for windows to redraw). It's tempting to check
1003 // w.mSeamlessRotationCount but that could be incorrect in the case of
1004 // window-removal.
1005 return false;
1006 }
Robert Carr0e007272017-10-02 13:00:55 -07001007
1008 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001009 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001010 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001011 mayRotateSeamlessly = false;
1012 }
1013 for (int i = 0; i < mService.mSessions.size(); i++) {
1014 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1015 mayRotateSeamlessly = false;
1016 break;
1017 }
1018 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001019 }
Robert Carr0e007272017-10-02 13:00:55 -07001020 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001021
1022 // TODO: Implement forced rotation changes.
1023 // Set mAltOrientation to indicate that the application is receiving
1024 // an orientation that has different metrics than it expected.
1025 // eg. Portrait instead of Landscape.
1026
1027 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1028 lastOrientation, rotation);
1029
1030 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1031 + ", got rotation " + rotation + " which has "
1032 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1033
1034 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1035 // No change.
1036 return false;
1037 }
1038
1039 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1040 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1041 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1042
1043 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1044 mService.mWaitingForConfig = true;
1045 }
1046
1047 mRotation = rotation;
1048 mAltOrientation = altOrientation;
1049 if (isDefaultDisplay) {
1050 mService.mPolicy.setRotationLw(rotation);
1051 }
1052
1053 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1054 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1055 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1056 WINDOW_FREEZE_TIMEOUT_DURATION);
1057
1058 setLayoutNeeded();
1059 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001060 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001061
1062 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001063 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001064 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1065 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1066 mDisplayId);
1067 } else {
1068 // The screen rotation animation uses a screenshot to freeze the screen
1069 // while windows resize underneath.
1070 // When we are rotating seamlessly, we allow the elements to transition
1071 // to their rotated state independently and without a freeze required.
1072 screenRotationAnimation = null;
1073
1074 // We have to reset this in case a window was removed before it
1075 // finished seamless rotation.
1076 mService.mSeamlessRotationCount = 0;
1077 }
1078
1079 // We need to update our screen size information to match the new rotation. If the rotation
1080 // has actually changed then this method will return true and, according to the comment at
1081 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1082 // By updating the Display info here it will be available to
1083 // #computeScreenConfiguration() later.
1084 updateDisplayAndOrientation(getConfiguration().uiMode);
1085
1086 if (!inTransaction) {
1087 if (SHOW_TRANSACTIONS) {
1088 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1089 }
1090 mService.openSurfaceTransaction();
1091 }
1092 try {
1093 // NOTE: We disable the rotation in the emulator because
1094 // it doesn't support hardware OpenGL emulation yet.
1095 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1096 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001097 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001098 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1099 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1100 mService.scheduleAnimationLocked();
1101 }
1102 }
1103
1104 if (rotateSeamlessly) {
1105 forAllWindows(w -> {
1106 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1107 }, true /* traverseTopToBottom */);
1108 }
1109
1110 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1111 } finally {
1112 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001113 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001114 if (SHOW_LIGHT_TRANSACTIONS) {
1115 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1116 }
1117 }
1118 }
1119
1120 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001121 if (w.mHasSurface && !rotateSeamlessly) {
1122 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001123 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001124 mService.mRoot.mOrientationChangeComplete = false;
1125 w.mLastFreezeDuration = 0;
1126 }
1127 w.mReportOrientationChanged = true;
1128 }, true /* traverseTopToBottom */);
1129
1130 if (rotateSeamlessly) {
1131 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1132 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1133 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1134 }
1135
1136 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1137 final WindowManagerService.RotationWatcher rotationWatcher
1138 = mService.mRotationWatchers.get(i);
1139 if (rotationWatcher.mDisplayId == mDisplayId) {
1140 try {
1141 rotationWatcher.mWatcher.onRotationChanged(rotation);
1142 } catch (RemoteException e) {
1143 // Ignore
1144 }
1145 }
1146 }
1147
1148 // TODO (multi-display): Magnification is supported only for the default display.
1149 // Announce rotation only if we will not animate as we already have the
1150 // windows in final state. Otherwise, we make this call at the rotation end.
1151 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1152 && isDefaultDisplay) {
1153 mService.mAccessibilityController.onRotationChangedLocked(this);
1154 }
1155
1156 return true;
1157 }
1158
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001159 void configureDisplayPolicy() {
1160 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1161 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1162
1163 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1164 }
1165
Andrii Kulian06d07d62017-03-14 11:11:47 -07001166 /**
1167 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1168 * changed.
1169 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1170 */
1171 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1172 // Use the effective "visual" dimensions based on current rotation
1173 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1174 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1175 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1176 int dw = realdw;
1177 int dh = realdh;
1178
1179 if (mAltOrientation) {
1180 if (realdw > realdh) {
1181 // Turn landscape into portrait.
1182 int maxw = (int)(realdh/1.3f);
1183 if (maxw < realdw) {
1184 dw = maxw;
1185 }
1186 } else {
1187 // Turn portrait into landscape.
1188 int maxh = (int)(realdw/1.3f);
1189 if (maxh < realdh) {
1190 dh = maxh;
1191 }
1192 }
1193 }
1194
1195 // Update application display metrics.
1196 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1197 mDisplayId);
1198 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1199 mDisplayId);
1200 mDisplayInfo.rotation = mRotation;
1201 mDisplayInfo.logicalWidth = dw;
1202 mDisplayInfo.logicalHeight = dh;
1203 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1204 mDisplayInfo.appWidth = appWidth;
1205 mDisplayInfo.appHeight = appHeight;
1206 if (isDefaultDisplay) {
1207 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1208 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1209 }
1210 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1211 if (mDisplayScalingDisabled) {
1212 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1213 } else {
1214 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1215 }
1216
1217 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1218 mDisplayInfo);
1219
1220 mBaseDisplayRect.set(0, 0, dw, dh);
1221
1222 if (isDefaultDisplay) {
1223 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1224 mCompatDisplayMetrics);
1225 }
1226 return mDisplayInfo;
1227 }
1228
1229 /**
1230 * Compute display configuration based on display properties and policy settings.
1231 * Do not call if mDisplayReady == false.
1232 */
1233 void computeScreenConfiguration(Configuration config) {
1234 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1235
1236 final int dw = displayInfo.logicalWidth;
1237 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001238 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1239 // TODO: Probably best to set this based on some setting in the display content object,
1240 // so the display can be configured for things like fullscreen.
1241 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001242
Andrii Kulian06d07d62017-03-14 11:11:47 -07001243 config.screenWidthDp =
1244 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1245 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1246 config.screenHeightDp =
1247 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1248 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001249
1250 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1251 final int leftInset = mTmpRect.left;
1252 final int topInset = mTmpRect.top;
1253 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001254 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1255 leftInset + displayInfo.appWidth /* right */,
1256 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001257 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1258 || displayInfo.rotation == Surface.ROTATION_270);
1259
1260 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1261 mDisplayMetrics.density, config);
1262
1263 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1264 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1265 ? Configuration.SCREENLAYOUT_ROUND_YES
1266 : Configuration.SCREENLAYOUT_ROUND_NO);
1267
1268 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1269 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1270 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1271 dh, mDisplayId);
1272 config.densityDpi = displayInfo.logicalDensityDpi;
1273
1274 config.colorMode =
1275 (displayInfo.isHdr()
1276 ? Configuration.COLOR_MODE_HDR_YES
1277 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001278 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001279 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1280 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1281
1282 // Update the configuration based on available input devices, lid switch,
1283 // and platform configuration.
1284 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1285 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1286 config.navigation = Configuration.NAVIGATION_NONAV;
1287
1288 int keyboardPresence = 0;
1289 int navigationPresence = 0;
1290 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1291 final int len = devices != null ? devices.length : 0;
1292 for (int i = 0; i < len; i++) {
1293 InputDevice device = devices[i];
1294 if (!device.isVirtual()) {
1295 final int sources = device.getSources();
1296 final int presenceFlag = device.isExternal() ?
1297 WindowManagerPolicy.PRESENCE_EXTERNAL :
1298 WindowManagerPolicy.PRESENCE_INTERNAL;
1299
1300 // TODO(multi-display): Configure on per-display basis.
1301 if (mService.mIsTouchDevice) {
1302 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1303 InputDevice.SOURCE_TOUCHSCREEN) {
1304 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1305 }
1306 } else {
1307 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1308 }
1309
1310 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1311 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1312 navigationPresence |= presenceFlag;
1313 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1314 && config.navigation == Configuration.NAVIGATION_NONAV) {
1315 config.navigation = Configuration.NAVIGATION_DPAD;
1316 navigationPresence |= presenceFlag;
1317 }
1318
1319 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1320 config.keyboard = Configuration.KEYBOARD_QWERTY;
1321 keyboardPresence |= presenceFlag;
1322 }
1323 }
1324 }
1325
1326 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1327 config.navigation = Configuration.NAVIGATION_DPAD;
1328 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1329 }
1330
1331 // Determine whether a hard keyboard is available and enabled.
1332 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1333 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1334 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1335 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1336 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1337 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1338 }
1339
1340 // Let the policy update hidden states.
1341 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1342 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1343 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1344 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1345 }
1346
1347 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1348 int displayId) {
1349 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1350 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1351 final int unrotDw, unrotDh;
1352 if (rotated) {
1353 unrotDw = dh;
1354 unrotDh = dw;
1355 } else {
1356 unrotDw = dw;
1357 unrotDh = dh;
1358 }
1359 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1360 displayId);
1361 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1362 displayId);
1363 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1364 displayId);
1365 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1366 displayId);
1367 return sw;
1368 }
1369
1370 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1371 DisplayMetrics dm, int dw, int dh, int displayId) {
1372 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1373 displayId);
1374 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1375 uiMode, displayId);
1376 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1377 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1378 if (curSize == 0 || size < curSize) {
1379 curSize = size;
1380 }
1381 return curSize;
1382 }
1383
1384 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1385 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1386
1387 // We need to determine the smallest width that will occur under normal
1388 // operation. To this, start with the base screen size and compute the
1389 // width under the different possible rotations. We need to un-rotate
1390 // the current screen dimensions before doing this.
1391 int unrotDw, unrotDh;
1392 if (rotated) {
1393 unrotDw = dh;
1394 unrotDh = dw;
1395 } else {
1396 unrotDw = dw;
1397 unrotDh = dh;
1398 }
1399 displayInfo.smallestNominalAppWidth = 1<<30;
1400 displayInfo.smallestNominalAppHeight = 1<<30;
1401 displayInfo.largestNominalAppWidth = 0;
1402 displayInfo.largestNominalAppHeight = 0;
1403 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1404 unrotDh);
1405 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1406 unrotDw);
1407 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1408 unrotDh);
1409 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1410 unrotDw);
1411 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1412 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1413 displayId);
1414 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1415 displayId);
1416 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1417 displayId);
1418 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1419 displayId);
1420 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1421 outConfig.screenLayout = sl;
1422 }
1423
1424 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1425 int uiMode, int displayId) {
1426 // Get the app screen size at this rotation.
1427 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1428 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1429
1430 // Compute the screen layout size class for this rotation.
1431 int longSize = w;
1432 int shortSize = h;
1433 if (longSize < shortSize) {
1434 int tmp = longSize;
1435 longSize = shortSize;
1436 shortSize = tmp;
1437 }
1438 longSize = (int)(longSize/density);
1439 shortSize = (int)(shortSize/density);
1440 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1441 }
1442
1443 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1444 int uiMode, int dw, int dh) {
1445 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1446 displayId);
1447 if (width < displayInfo.smallestNominalAppWidth) {
1448 displayInfo.smallestNominalAppWidth = width;
1449 }
1450 if (width > displayInfo.largestNominalAppWidth) {
1451 displayInfo.largestNominalAppWidth = width;
1452 }
1453 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1454 displayId);
1455 if (height < displayInfo.smallestNominalAppHeight) {
1456 displayInfo.smallestNominalAppHeight = height;
1457 }
1458 if (height > displayInfo.largestNominalAppHeight) {
1459 displayInfo.largestNominalAppHeight = height;
1460 }
1461 }
1462
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001463 DockedStackDividerController getDockedDividerController() {
1464 return mDividerControllerLocked;
1465 }
1466
Winson Chung655332c2016-10-31 13:14:28 -07001467 PinnedStackController getPinnedStackController() {
1468 return mPinnedStackControllerLocked;
1469 }
1470
Jeff Browna506a6e2013-06-04 00:02:38 -07001471 /**
1472 * Returns true if the specified UID has access to this display.
1473 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001474 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001475 return mDisplay.hasAccess(uid);
1476 }
1477
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001478 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001479 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001480 }
1481
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001482 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001483 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001484 }
1485
Wale Ogunwale61911492017-10-11 08:50:50 -07001486 /**
1487 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1488 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001489 TaskStack getSplitScreenPrimaryStack() {
1490 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001491 return (stack != null && stack.isVisible()) ? stack : null;
1492 }
1493
1494 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001495 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001496 * not visible.
1497 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001498 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1499 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001500 }
1501
1502 TaskStack getPinnedStack() {
1503 return mTaskStackContainers.getPinnedStack();
1504 }
1505
1506 private boolean hasPinnedStack() {
1507 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001508 }
1509
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001510 /**
1511 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1512 * Null is no compatible stack on the display.
1513 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001514 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001515 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1516 }
1517
1518 /**
1519 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1520 * activity type. Null is no compatible stack on the display.
1521 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001522 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001523 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001524 }
1525
Bryce Lee48f4b572017-04-10 10:54:15 -07001526 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001527 TaskStack getTopStack() {
1528 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001529 }
1530
Wale Ogunwale61911492017-10-11 08:50:50 -07001531 void onStackWindowingModeChanged(TaskStack stack) {
1532 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001533 }
1534
Andrii Kulian441e4492016-09-29 15:25:00 -07001535 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001536 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001537 super.onConfigurationChanged(newParentConfig);
1538
Andrii Kulian3a507b52016-09-19 18:14:12 -07001539 // The display size information is heavily dependent on the resources in the current
1540 // configuration, so we need to reconfigure it every time the configuration changes.
1541 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1542 mService.reconfigureDisplayLocked(this);
1543
1544 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001545 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001546 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001547
Andrii Kulian441e4492016-09-29 15:25:00 -07001548 /**
1549 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1550 * bounds were updated.
1551 */
1552 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001553 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1554 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001555 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001556 changedStackList.add(stack.mStackId);
1557 }
1558 }
Winson Chung32c566f2017-04-11 18:31:21 -07001559
1560 // If there was no pinned stack, we still need to notify the controller of the display info
1561 // update as a result of the config change. We do this here to consolidate the flow between
1562 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001563 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001564 mPinnedStackControllerLocked.onDisplayInfoChanged();
1565 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001566 }
1567
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001568 @Override
1569 boolean fillsParent() {
1570 return true;
1571 }
1572
1573 @Override
1574 boolean isVisible() {
1575 return true;
1576 }
1577
1578 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001579 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001580 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001581 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001582 }
1583
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001584 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001585 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1586 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1587 // target, or here in order if there isn't an IME target.
1588 if (traverseTopToBottom) {
1589 for (int i = mChildren.size() - 1; i >= 0; --i) {
1590 final DisplayChildWindowContainer child = mChildren.get(i);
1591 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1592 // In this case the Ime windows will be processed above their target so we skip
1593 // here.
1594 continue;
1595 }
1596 if (child.forAllWindows(callback, traverseTopToBottom)) {
1597 return true;
1598 }
1599 }
1600 } else {
1601 final int count = mChildren.size();
1602 for (int i = 0; i < count; i++) {
1603 final DisplayChildWindowContainer child = mChildren.get(i);
1604 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1605 // In this case the Ime windows will be processed above their target so we skip
1606 // here.
1607 continue;
1608 }
1609 if (child.forAllWindows(callback, traverseTopToBottom)) {
1610 return true;
1611 }
1612 }
1613 }
1614 return false;
1615 }
1616
1617 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1618 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1619 }
1620
Wale Ogunwale399c8692017-05-08 14:22:42 -07001621 @Override
1622 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001623 final WindowManagerPolicy policy = mService.mPolicy;
1624
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001625 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001626 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001627 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001628 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001629 // If the display is frozen, some activities may be in the middle of restarting, and
1630 // thus have removed their old window. If the window has the flag to hide the lock
1631 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1632 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001633 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001634 } else if (policy.isKeyguardLocked()) {
1635 // Use the last orientation the while the display is frozen with the keyguard
1636 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1637 // window. We don't want to check the show when locked window directly though as
1638 // things aren't stable while the display is frozen, for example the window could be
1639 // momentarily unavailable due to activity relaunch.
1640 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001641 + "return " + mLastOrientation);
1642 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001643 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001644 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001645 final int orientation = mAboveAppWindowsContainers.getOrientation();
1646 if (orientation != SCREEN_ORIENTATION_UNSET) {
1647 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001648 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001649 }
1650
Wale Ogunwale399c8692017-05-08 14:22:42 -07001651 // Top system windows are not requesting an orientation. Start searching from apps.
1652 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001653 }
1654
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001655 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001656 // Check if display metrics changed and update base values if needed.
1657 updateBaseDisplayMetricsIfNeeded();
1658
Craig Mautner722285e2012-09-07 13:55:58 -07001659 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001660 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001661
Wale Ogunwale61911492017-10-11 08:50:50 -07001662 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1663 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001664 }
Craig Mautner722285e2012-09-07 13:55:58 -07001665 }
1666
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001667 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001668 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1669 if (displayManagerInternal != null) {
1670 // Bootstrap the default logical display from the display manager.
1671 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1672 if (newDisplayInfo != null) {
1673 mDisplayInfo.copyFrom(newDisplayInfo);
1674 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001675 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001676
Andrii Kuliancd097992017-03-23 18:31:59 -07001677 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1678 mDisplayInfo.logicalDensityDpi);
1679 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1680 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1681 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001682 }
1683
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001684 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001685 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001686 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001687 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001688 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1689 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001690 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001691 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001692 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001693 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001694 out.set(left, top, left + width, top + height);
1695 }
1696
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001697 private void getLogicalDisplayRect(Rect out, int orientation) {
1698 getLogicalDisplayRect(out);
1699
1700 // Rotate the Rect if needed.
1701 final int currentRotation = mDisplayInfo.rotation;
1702 final int rotationDelta = deltaRotation(currentRotation, orientation);
1703 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1704 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1705 mTmpRectF.set(out);
1706 mTmpMatrix.mapRect(mTmpRectF);
1707 mTmpRectF.round(out);
1708 }
1709 }
1710
Andrii Kuliancd097992017-03-23 18:31:59 -07001711 /**
1712 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1713 * values.
1714 */
1715 private void updateBaseDisplayMetricsIfNeeded() {
1716 // Get real display metrics without overrides from WM.
1717 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1718 final int orientation = mDisplayInfo.rotation;
1719 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1720 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1721 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1722 final int newDensity = mDisplayInfo.logicalDensityDpi;
1723
1724 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1725 || mInitialDisplayHeight != newHeight
1726 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1727
1728 if (displayMetricsChanged) {
1729 // Check if display size or density is forced.
1730 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1731 || mBaseDisplayHeight != mInitialDisplayHeight;
1732 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1733
1734 // If there is an override set for base values - use it, otherwise use new values.
1735 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1736 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1737 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1738
1739 // Real display metrics changed, so we should also update initial values.
1740 mInitialDisplayWidth = newWidth;
1741 mInitialDisplayHeight = newHeight;
1742 mInitialDisplayDensity = newDensity;
1743 mService.reconfigureDisplayLocked(this);
1744 }
1745 }
1746
Bryce Lee27cec322017-03-21 09:41:37 -07001747 /** Sets the maximum width the screen resolution can be */
1748 void setMaxUiWidth(int width) {
1749 if (DEBUG_DISPLAY) {
1750 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1751 }
1752
1753 mMaxUiWidth = width;
1754
1755 // Update existing metrics.
1756 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1757 }
1758
1759 /** Update base (override) display metrics. */
1760 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1761 mBaseDisplayWidth = baseWidth;
1762 mBaseDisplayHeight = baseHeight;
1763 mBaseDisplayDensity = baseDensity;
1764
1765 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1766 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1767 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1768 mBaseDisplayWidth = mMaxUiWidth;
1769
1770 if (DEBUG_DISPLAY) {
1771 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1772 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1773 + " on display:" + getDisplayId());
1774 }
1775 }
1776
1777 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1778 }
1779
Chong Zhangf66db432016-01-13 10:39:51 -08001780 void getContentRect(Rect out) {
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001781 out.set(mDisplayFrames.mContent);
Chong Zhangf66db432016-01-13 10:39:51 -08001782 }
1783
Wale Ogunwale61911492017-10-11 08:50:50 -07001784 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001785 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1786 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001787
Wale Ogunwale61911492017-10-11 08:50:50 -07001788 final TaskStack stack = new TaskStack(mService, stackId, controller);
1789 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001790 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001791 }
1792
Andrii Kulian51c1b672017-04-07 18:39:32 -07001793 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001794 final DisplayContent prevDc = stack.getDisplayContent();
1795 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001796 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1797 + " which is not currently attached to any display");
1798 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001799 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001800 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1801 + " to its current displayId=" + mDisplayId);
1802 }
1803
Wale Ogunwale61911492017-10-11 08:50:50 -07001804 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001805 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001806 }
1807
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001808 @Override
1809 protected void addChild(DisplayChildWindowContainer child,
1810 Comparator<DisplayChildWindowContainer> comparator) {
1811 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1812 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001813
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001814 @Override
1815 protected void addChild(DisplayChildWindowContainer child, int index) {
1816 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1817 }
1818
1819 @Override
1820 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001821 // Only allow removal of direct children from this display if the display is in the process
1822 // of been removed.
1823 if (mRemovingDisplay) {
1824 super.removeChild(child);
1825 return;
1826 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001827 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001828 }
1829
Andrii Kuliand2765632016-12-12 22:26:34 -08001830 @Override
1831 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1832 // Children of the display are statically ordered, so the real intention here is to perform
1833 // the operation on the display and not the static direct children.
1834 getParent().positionChildAt(position, this, includingParents);
1835 }
1836
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001837 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001838 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1839 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001840 final int taskId = stack.taskIdFromPoint(x, y);
1841 if (taskId != -1) {
1842 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001843 }
1844 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001845 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001846 }
1847
Chong Zhang8e89b312015-09-09 15:09:30 -07001848 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001849 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001850 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001851 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001852 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001853 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001854 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001855 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1856 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001857 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001858 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001859 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001860
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001861 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1862 if (mTmpTaskForResizePointSearchResult.searchDone) {
1863 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001864 }
1865 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001866 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001867 }
1868
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001869 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001870 // The provided task is the task on this display with focus, so if WindowManagerService's
1871 // focused app is not on this display, focusedTask will be null.
1872 if (focusedTask == null) {
1873 mTouchExcludeRegion.setEmpty();
1874 } else {
1875 mTouchExcludeRegion.set(mBaseDisplayRect);
1876 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1877 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001878 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1879 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001880 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1881 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001882 }
1883 // If we removed the focused task above, add it back and only leave its
1884 // outside touch area in the exclusion. TapDectector is not interested in
1885 // any touch inside the focused task itself.
1886 if (!mTmpRect2.isEmpty()) {
1887 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1888 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001889 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001890 final WindowState inputMethod = mService.mInputMethodWindow;
1891 if (inputMethod != null && inputMethod.isVisibleLw()) {
1892 // If the input method is visible and the user is typing, we don't want these touch
1893 // events to be intercepted and used to change focus. This would likely cause a
1894 // disappearance of the input method.
1895 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001896 if (inputMethod.getDisplayId() == mDisplayId) {
1897 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1898 } else {
1899 // IME is on a different display, so we need to update its tap detector.
1900 // TODO(multidisplay): Remove when IME will always appear on same display.
1901 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1902 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001903 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001904 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1905 WindowState win = mTapExcludedWindows.get(i);
1906 win.getTouchableRegion(mTmpRegion);
1907 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1908 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001909 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001910 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001911 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001912 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001913 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1914 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001915 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001916 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001917 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001918 }
1919
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001920 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001921 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001922 super.switchUser();
1923 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001924 }
1925
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001926 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001927 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1928 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001929 }
1930 }
1931
Wale Ogunwale10124582016-09-15 20:25:50 -07001932 @Override
1933 void removeIfPossible() {
1934 if (isAnimating()) {
1935 mDeferredRemoval = true;
1936 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001937 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001938 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001939 }
1940
Wale Ogunwale10124582016-09-15 20:25:50 -07001941 @Override
1942 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001943 mRemovingDisplay = true;
1944 try {
1945 super.removeImmediately();
1946 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001947 if (mService.canDispatchPointerEvents()) {
1948 if (mTapDetector != null) {
1949 mService.unregisterPointerEventListener(mTapDetector);
1950 }
1951 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1952 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1953 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001954 }
Robert Carrb1579c82017-09-05 14:54:47 -07001955 // The pending transaction won't be applied so we should
1956 // just clean up any surfaces pending destruction.
1957 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001958 } finally {
1959 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001960 }
Craig Mautner95da1082014-02-24 17:54:35 -08001961 }
1962
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001963 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001964 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001965 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001966 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1967
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001968 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001969 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001970 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001971 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001972 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001973 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001974 }
1975
Andrii Kulian0214ed92017-05-16 13:44:05 -07001976 /** @return 'true' if removal of this display content is deferred due to active animation. */
1977 boolean isRemovalDeferred() {
1978 return mDeferredRemoval;
1979 }
1980
Wale Ogunwale10124582016-09-15 20:25:50 -07001981 boolean animateForIme(float interpolatedValue, float animationTarget,
1982 float dividerAnimationTarget) {
1983 boolean updated = false;
1984
Wale Ogunwale61911492017-10-11 08:50:50 -07001985 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1986 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001987 if (stack == null || !stack.isAdjustedForIme()) {
1988 continue;
1989 }
1990
1991 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1992 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1993 updated = true;
1994 } else {
1995 mDividerControllerLocked.mLastAnimationProgress =
1996 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1997 mDividerControllerLocked.mLastDividerProgress =
1998 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1999 updated |= stack.updateAdjustForIme(
2000 mDividerControllerLocked.mLastAnimationProgress,
2001 mDividerControllerLocked.mLastDividerProgress,
2002 false /* force */);
2003 }
2004 if (interpolatedValue >= 1f) {
2005 stack.endImeAdjustAnimation();
2006 }
2007 }
2008
2009 return updated;
2010 }
2011
2012 boolean clearImeAdjustAnimation() {
2013 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002014 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2015 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002016 if (stack != null && stack.isAdjustedForIme()) {
2017 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2018 changed = true;
2019 }
2020 }
2021 return changed;
2022 }
2023
2024 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002025 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2026 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002027 if (stack.isVisible() && stack.isAdjustedForIme()) {
2028 stack.beginImeAdjustAnimation();
2029 }
2030 }
2031 }
2032
2033 void adjustForImeIfNeeded() {
2034 final WindowState imeWin = mService.mInputMethodWindow;
2035 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2036 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002037 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002038 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2039 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2040 imeTargetStack.getDockSide() : DOCKED_INVALID;
2041 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2042 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2043 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002044 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002045 final boolean imeHeightChanged = imeVisible &&
2046 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2047
2048 // The divider could be adjusted for IME position, or be thinner than usual,
2049 // or both. There are three possible cases:
2050 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2051 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2052 // - If IME is not visible, divider is not moved and is normal width.
2053
2054 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002055 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2056 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002057 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002058 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2059 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002060 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2061 } else {
2062 stack.resetAdjustedForIme(false);
2063 }
2064 }
2065 mDividerControllerLocked.setAdjustedForIme(
2066 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2067 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002068 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2069 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002070 stack.resetAdjustedForIme(!dockVisible);
2071 }
2072 mDividerControllerLocked.setAdjustedForIme(
2073 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2074 }
Winson Chung655332c2016-10-31 13:14:28 -07002075 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002076 }
2077
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002078 void setInputMethodAnimLayerAdjustment(int adj) {
2079 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2080 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002081 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002082 }
2083
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002084 /**
2085 * If a window that has an animation specifying a colored background and the current wallpaper
2086 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2087 * suddenly disappear.
2088 */
2089 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002090 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2091 w -> w.mIsWallpaper && w.isVisibleNow());
2092
2093 if (visibleWallpaper != null) {
2094 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002095 }
2096 return winAnimator.mAnimLayer;
2097 }
2098
Wale Ogunwale10124582016-09-15 20:25:50 -07002099 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002100 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2101 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002102 stack.prepareFreezingTaskBounds();
2103 }
2104 }
2105
Wale Ogunwale94744212015-09-21 19:01:47 -07002106 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002107 getLogicalDisplayRect(mTmpRect, newRotation);
2108
2109 // Compute a transform matrix to undo the coordinate space transformation,
2110 // and present the window at the same physical position it previously occupied.
2111 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2112 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2113
2114 mTmpRectF.set(bounds);
2115 mTmpMatrix.mapRect(mTmpRectF);
2116 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002117 }
2118
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002119 static int deltaRotation(int oldRotation, int newRotation) {
2120 int delta = newRotation - oldRotation;
2121 if (delta < 0) delta += 4;
2122 return delta;
2123 }
2124
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002125 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002126 Matrix outMatrix) {
2127 // For rotations without Z-ordering we don't need the target rectangle's position.
2128 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2129 displayHeight, outMatrix);
2130 }
2131
2132 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2133 float displayWidth, float displayHeight, Matrix outMatrix) {
2134 switch (rotation) {
2135 case ROTATION_0:
2136 outMatrix.reset();
2137 break;
2138 case ROTATION_270:
2139 outMatrix.setRotate(270, 0, 0);
2140 outMatrix.postTranslate(0, displayHeight);
2141 outMatrix.postTranslate(rectTop, 0);
2142 break;
2143 case ROTATION_180:
2144 outMatrix.reset();
2145 break;
2146 case ROTATION_90:
2147 outMatrix.setRotate(90, 0, 0);
2148 outMatrix.postTranslate(displayWidth, 0);
2149 outMatrix.postTranslate(-rectTop, rectLeft);
2150 break;
2151 }
2152 }
2153
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002154 @CallSuper
2155 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002156 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002157 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002158 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002159 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002160 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2161 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002162 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002163 }
2164 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2165 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002166 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2167 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002168 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002169 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002170 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2171 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002172 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002173 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002174 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2175 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002176 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002177 }
2178 proto.write(DPI, mBaseDisplayDensity);
2179 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002180 proto.write(ROTATION, mRotation);
2181 final ScreenRotationAnimation screenRotationAnimation =
2182 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2183 if (screenRotationAnimation != null) {
2184 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2185 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002186 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002187 proto.end(token);
2188 }
2189
Craig Mautnera91f9e22012-09-14 16:22:08 -07002190 public void dump(String prefix, PrintWriter pw) {
2191 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2192 final String subPrefix = " " + prefix;
2193 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2194 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2195 pw.print("dpi");
2196 if (mInitialDisplayWidth != mBaseDisplayWidth
2197 || mInitialDisplayHeight != mBaseDisplayHeight
2198 || mInitialDisplayDensity != mBaseDisplayDensity) {
2199 pw.print(" base=");
2200 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2201 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2202 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002203 if (mDisplayScalingDisabled) {
2204 pw.println(" noscale");
2205 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002206 pw.print(" cur=");
2207 pw.print(mDisplayInfo.logicalWidth);
2208 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2209 pw.print(" app=");
2210 pw.print(mDisplayInfo.appWidth);
2211 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2212 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2213 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2214 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2215 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002216 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002217 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002218 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002219
Craig Mautnerdc548482014-02-05 13:35:24 -08002220 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002221 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002222 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2223 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002224 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002225 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002226
Craig Mautnerdc548482014-02-05 13:35:24 -08002227 pw.println();
2228 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002229 pw.println();
2230 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002231 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002232 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002233 pw.print(" Exiting #"); pw.print(i);
2234 pw.print(' '); pw.print(token);
2235 pw.println(':');
2236 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002237 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002238 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002239
Jorim Jaggi31f71702016-05-04 16:43:04 -07002240 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002241
2242 // Dump stack references
2243 final TaskStack homeStack = getHomeStack();
2244 if (homeStack != null) {
2245 pw.println(prefix + "homeStack=" + homeStack.getName());
2246 }
2247 final TaskStack pinnedStack = getPinnedStack();
2248 if (pinnedStack != null) {
2249 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2250 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002251 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002252 if (splitScreenPrimaryStack != null) {
2253 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2254 }
2255
2256 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002257 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002258 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002259 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002260
2261 if (mInputMethodAnimLayerAdjustment != 0) {
2262 pw.println(subPrefix
2263 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2264 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002265
2266 pw.println();
2267 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002268 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002269
2270 @Override
2271 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002272 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002273 }
2274
2275 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002276 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002277 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002278
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002279 /** Returns true if the stack in the windowing mode is visible. */
2280 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002281 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002282 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002283 }
2284
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002285 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002286 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002287 final int x = (int) xf;
2288 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002289 final WindowState touchedWin = getWindow(w -> {
2290 final int flags = w.mAttrs.flags;
2291 if (!w.isVisibleLw()) {
2292 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002293 }
2294 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002295 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002296 }
2297
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002298 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002299 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002300 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002301 }
2302
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002303 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002304
2305 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002306 return mTmpRegion.contains(x, y) || touchFlags == 0;
2307 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002308
2309 return touchedWin;
2310 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002311
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002312 boolean canAddToastWindowForUid(int uid) {
2313 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002314 // Also if the app is focused adding more than one toast at
2315 // a time for better backwards compatibility.
2316 final WindowState focusedWindowForUid = getWindow(w ->
2317 w.mOwnerUid == uid && w.isFocused());
2318 if (focusedWindowForUid != null) {
2319 return true;
2320 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002321 final WindowState win = getWindow(w ->
2322 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2323 && !w.mWindowRemovalAllowed);
2324 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002325 }
2326
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002327 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002328 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2329 return;
2330 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002331
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002332 // Used to communicate the old focus to the callback method.
2333 mTmpWindow = oldFocus;
2334
2335 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002336 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002337
2338 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002339 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002340
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002341 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002342
2343 if (mTmpWindow == null) {
2344 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2345 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002346 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002347 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002348 }
2349
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002350 /** Updates the layer assignment of windows on this display. */
2351 void assignWindowLayers(boolean setLayoutNeeded) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07002352 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002353 if (setLayoutNeeded) {
2354 setLayoutNeeded();
2355 }
Robert Carrb1579c82017-09-05 14:54:47 -07002356
Robert Carrf59b8dd2017-10-02 18:58:36 -07002357 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002358 // the application of this transaction until the animation pass triggers
2359 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2360 // the hiding and showing of surfaces.
2361 scheduleAnimation();
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002362 }
2363
Wale Ogunwale1666e312016-12-16 11:27:18 -08002364 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2365 // moving containers or resizing them. Need to investigate the best way to have it automatically
2366 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002367 void layoutAndAssignWindowLayersIfNeeded() {
2368 mService.mWindowsChanged = true;
2369 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002370
2371 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2372 false /*updateInputWindows*/)) {
2373 assignWindowLayers(false /* setLayoutNeeded */);
2374 }
2375
2376 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2377 mService.mWindowPlacerLocked.performSurfacePlacement();
2378 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2379 }
2380
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002381 /** Returns true if a leaked surface was destroyed */
2382 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002383 // Used to indicate that a surface was leaked.
2384 mTmpWindow = null;
2385 forAllWindows(w -> {
2386 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002387 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002388 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002389 }
2390 if (!mService.mSessions.contains(wsa.mSession)) {
2391 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 + w + " surface=" + wsa.mSurfaceController
2393 + " token=" + w.mToken
2394 + " pid=" + w.mSession.mPid
2395 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002396 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002397 mService.mForceRemoves.add(w);
2398 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002399 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002400 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002401 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002402 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002403 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002404 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002405 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002406 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002407 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002408
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002409 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002410 }
2411
2412 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002413 * Determine and return the window that should be the IME target.
2414 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2415 * @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 +00002416 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002417 WindowState computeImeTarget(boolean updateImeTarget) {
2418 if (mService.mInputMethodWindow == null) {
2419 // There isn't an IME so there shouldn't be a target...That was easy!
2420 if (updateImeTarget) {
2421 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2422 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2423 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2424 }
2425 return null;
2426 }
2427
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002428 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2429 // same display. Or even when the current IME/target are not on the same screen as the next
2430 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002431 mUpdateImeTarget = updateImeTarget;
2432 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002433
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002434
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002435 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2436 // to be on top of it, but it is not -really- where input will go. So look down below
2437 // for a real window to target...
2438 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2439 final AppWindowToken token = target.mAppToken;
2440 if (token != null) {
2441 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2442 if (betterTarget != null) {
2443 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002444 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002445 }
2446 }
2447
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002448 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2449 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002450
2451 // Now, a special case -- if the last target's window is in the process of exiting, and is
2452 // above the new target, keep on the last target to avoid flicker. Consider for example a
2453 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2454 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2455 // scrim.
2456 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002457 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2458 && (target == null
2459 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002460 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002461 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002462 }
2463
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002464 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2465 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002466
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002467 if (target == null) {
2468 if (updateImeTarget) {
2469 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2470 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2471 + Debug.getCallers(4) : ""));
2472 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2473 }
2474
2475 return null;
2476 }
2477
2478 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002479 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2480 if (token != null) {
2481
2482 // Now some fun for dealing with window animations that modify the Z order. We need
2483 // to look at all windows below the current target that are in this app, finding the
2484 // highest visible one in layering.
2485 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002486 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002488 }
2489
2490 if (highestTarget != null) {
2491 final AppTransition appTransition = mService.mAppTransition;
2492 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2493 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2494 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002495 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002496
2497 if (appTransition.isTransitionSet()) {
2498 // If we are currently setting up for an animation, hold everything until we
2499 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002500 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2501 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002502 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002503 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002504 // If the window we are currently targeting is involved with an animation,
2505 // and it is on top of the next target we will be over, then hold off on
2506 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002507 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2508 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002509 }
2510 }
2511 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002512
2513 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2514 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2515 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002516 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002517 }
2518
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002519 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002520 }
2521
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002522 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2523 if (target == mService.mInputMethodTarget
2524 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2525 && mInputMethodAnimLayerAdjustment == layerAdj) {
2526 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002527 }
2528
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002529 mService.mInputMethodTarget = target;
2530 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2531 setInputMethodAnimLayerAdjustment(layerAdj);
2532 assignWindowLayers(false /* setLayoutNeeded */);
2533 }
2534
2535 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2536 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2537 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2538 }
2539
2540 // Used to indicate we have reached the first window in the range we are interested in.
2541 mTmpWindow = null;
2542
2543 // TODO: Figure-out a more efficient way to do this.
2544 final WindowState candidate = getWindow(w -> {
2545 if (w == top) {
2546 // Reached the first window in the range we are interested in.
2547 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002548 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002549 if (mTmpWindow == null) {
2550 return false;
2551 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002552
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002553 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2554 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002555 }
2556 // If we reached the bottom of the range of windows we are considering,
2557 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002558 if (w == bottom) {
2559 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002560 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002561 return false;
2562 });
2563
2564 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002565 }
2566
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002567 void setLayoutNeeded() {
2568 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2569 mLayoutNeeded = true;
2570 }
2571
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002572 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002573 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2574 mLayoutNeeded = false;
2575 }
2576
2577 boolean isLayoutNeeded() {
2578 return mLayoutNeeded;
2579 }
2580
Wale Ogunwale02319a62016-09-26 15:21:22 -07002581 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2582 if (mTokenMap.isEmpty()) {
2583 return;
2584 }
2585 pw.println(" Display #" + mDisplayId);
2586 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2587 while (it.hasNext()) {
2588 final WindowToken token = it.next();
2589 pw.print(" ");
2590 pw.print(token);
2591 if (dumpAll) {
2592 pw.println(':');
2593 token.dump(pw, " ");
2594 } else {
2595 pw.println();
2596 }
2597 }
2598 }
2599
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002600 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002601 final int[] index = new int[1];
2602 forAllWindows(w -> {
2603 final WindowStateAnimator wAnim = w.mWinAnimator;
2604 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2605 index[0] = index[0] + 1;
2606 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002607 }
2608
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002609 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002610 forAllWindows(w -> {
2611 w.mWinAnimator.enableSurfaceTrace(fd);
2612 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002613 }
2614
2615 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 forAllWindows(w -> {
2617 w.mWinAnimator.disableSurfaceTrace();
2618 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002619 }
2620
Jorim Jaggife762342016-10-13 14:33:27 +02002621 /**
2622 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2623 */
2624 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2625 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002626 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002627 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2628 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002629 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002630 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2631 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002633 }
2634
Wale Ogunwale494009b82016-10-21 09:01:38 -07002635 boolean checkWaitingForWindows() {
2636
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002637 mHaveBootMsg = false;
2638 mHaveApp = false;
2639 mHaveWallpaper = false;
2640 mHaveKeyguard = true;
2641
2642 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002643 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2644 return true;
2645 }
2646 if (w.isDrawnLw()) {
2647 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002648 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002649 } else if (w.mAttrs.type == TYPE_APPLICATION
2650 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002651 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002652 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002653 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002654 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002655 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002656 }
2657 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002658 return false;
2659 });
2660
2661 if (visibleWindow != null) {
2662 // We have a visible window.
2663 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002664 }
2665
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 // if the wallpaper service is disabled on the device, we're never going to have
2667 // wallpaper, don't bother waiting for it
2668 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2669 com.android.internal.R.bool.config_enableWallpaperService)
2670 && !mService.mOnlyCore;
2671
Wale Ogunwale494009b82016-10-21 09:01:38 -07002672 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2673 "******** booted=" + mService.mSystemBooted
2674 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002675 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2676 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2677 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002678
2679 // If we are turning on the screen to show the boot message, don't do it until the boot
2680 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002681 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002682 return true;
2683 }
2684
2685 // If we are turning on the screen after the boot is completed normally, don't do so until
2686 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002687 if (mService.mSystemBooted
2688 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002689 return true;
2690 }
2691
2692 return false;
2693 }
2694
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002695 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002696 mTmpWindowAnimator = animator;
2697 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002698 }
2699
2700 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002701 resetAnimationBackgroundAnimator();
2702
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002703 // Used to indicate a detached wallpaper.
2704 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002705 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002706
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002707 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002708
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002709 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002710 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002711 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2712 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002713 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2714 }
2715 }
2716
Wale Ogunwale494009b82016-10-21 09:01:38 -07002717 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002718 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002719 if (imFocus == null) {
2720 return false;
2721 }
2722
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002723 if (DEBUG_INPUT_METHOD) {
2724 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2725 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2726 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002727 }
2728
Wale Ogunwale494009b82016-10-21 09:01:38 -07002729 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2730
2731 if (DEBUG_INPUT_METHOD) {
2732 Slog.i(TAG_WM, "IM target client: " + imeClient);
2733 if (imeClient != null) {
2734 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2735 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2736 }
2737 }
2738
2739 return imeClient != null && imeClient.asBinder() == client.asBinder();
2740 }
2741
2742 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002743 final WindowState win = getWindow(
2744 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2745 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002746 }
2747
2748 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002749 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002750 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002751 final int curValue = w.mSystemUiVisibility;
2752 final int diff = (curValue ^ visibility) & globalDiff;
2753 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002754 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002755 w.mSeq++;
2756 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002757 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002758 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2759 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002760 visibility, newValue, diff);
2761 }
2762 } catch (RemoteException e) {
2763 // so sorry
2764 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002765 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002766 }
2767
2768 void onWindowFreezeTimeout() {
2769 Slog.w(TAG_WM, "Window freeze timeout expired.");
2770 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002771
2772 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002773 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002774 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002775 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002776 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002777 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2778 - mService.mDisplayFreezeTime);
2779 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002780 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002781 mService.mWindowPlacerLocked.performSurfacePlacement();
2782 }
2783
2784 void waitForAllWindowsDrawn() {
2785 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002786 forAllWindows(w -> {
2787 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2788 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2789 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002790 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002791 w.mLastContentInsets.set(-1, -1, -1, -1);
2792 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002793 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002794 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002795 }
2796
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002797 // TODO: Super crazy long method that should be broken down...
2798 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2799
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002800 final int dw = mDisplayInfo.logicalWidth;
2801 final int dh = mDisplayInfo.logicalHeight;
2802 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2803
2804 mTmpUpdateAllDrawn.clear();
2805
2806 int repeats = 0;
2807 do {
2808 repeats++;
2809 if (repeats > 6) {
2810 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2811 clearLayoutNeeded();
2812 break;
2813 }
2814
2815 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2816 pendingLayoutChanges);
2817
Andrii Kulian839def92016-11-02 10:58:58 -07002818 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2819 // the wallpaper window jumping across displays.
2820 // Remove check for default display when there will be support for multiple wallpaper
2821 // targets (on different displays).
2822 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002823 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002824 }
2825
2826 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2827 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2828 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2829 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002830 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002831 }
2832 }
2833
2834 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2835 setLayoutNeeded();
2836 }
2837
2838 // FIRST LOOP: Perform a layout, if needed.
2839 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2840 performLayout(repeats == 1, false /* updateInputWindows */);
2841 } else {
2842 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2843 }
2844
2845 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2846 pendingLayoutChanges = 0;
2847
2848 if (isDefaultDisplay) {
2849 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002850 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002851 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2852 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2853 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2854 }
2855 } while (pendingLayoutChanges != 0);
2856
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002857 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002858
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002859 mTmpRecoveringMemory = recoveringMemory;
2860 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002861
2862 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002863 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2864 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2865 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002866 true /* inTraversal, must call performTraversalInTrans... below */);
2867
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002868 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2869 if (wallpaperVisible != mLastWallpaperVisible) {
2870 mLastWallpaperVisible = wallpaperVisible;
2871 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2872 }
2873
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002874 while (!mTmpUpdateAllDrawn.isEmpty()) {
2875 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2876 // See if any windows have been drawn, so they (and others associated with them)
2877 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002878 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002879 }
2880
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002881 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002882 }
2883
2884 void performLayout(boolean initial, boolean updateInputWindows) {
2885 if (!isLayoutNeeded()) {
2886 return;
2887 }
2888 clearLayoutNeeded();
2889
2890 final int dw = mDisplayInfo.logicalWidth;
2891 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002892 if (DEBUG_LAYOUT) {
2893 Slog.v(TAG, "-------------------------------------");
2894 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2895 }
2896
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002897 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2898 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2899 // display info above...
2900 mDisplayFrames.mRotation = mRotation;
2901 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002902 if (isDefaultDisplay) {
2903 // Not needed on non-default displays.
2904 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2905 mService.mScreenRect.set(0, 0, dw, dh);
2906 }
2907
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002908 int seq = mService.mLayoutSeq + 1;
2909 if (seq < 0) seq = 0;
2910 mService.mLayoutSeq = seq;
2911
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002912 // Used to indicate that we have processed the dream window and all additional windows are
2913 // behind it.
2914 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002915 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002916
2917 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002918 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002919
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002920 // Used to indicate that we have processed the dream window and all additional attached
2921 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002922 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002923 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002924
2925 // Now perform layout of attached windows, which usually depend on the position of the
2926 // window they are attached to. XXX does not deal with windows that are attached to windows
2927 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002928 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002929
2930 // Window frames may have changed. Tell the input dispatcher about it.
2931 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2932 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2933 if (updateInputWindows) {
2934 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2935 }
2936
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002937 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2938 }
2939
2940 /**
2941 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2942 * In portrait mode, it grabs the full screenshot.
2943 *
2944 * @param width the width of the target bitmap
2945 * @param height the height of the target bitmap
2946 * @param includeFullDisplay true if the screen should not be cropped before capture
2947 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2948 * @param config of the output bitmap
2949 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002950 * @param includeDecor whether to include window decors, like the status or navigation bar
2951 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002952 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002953 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002954 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002955 boolean wallpaperOnly, boolean includeDecor) {
2956 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2957 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002958 if (bitmap == null) {
2959 return null;
2960 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002961
2962 if (DEBUG_SCREENSHOT) {
2963 // TEST IF IT's ALL BLACK
2964 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2965 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2966 bitmap.getHeight());
2967 boolean allBlack = true;
2968 final int firstColor = buffer[0];
2969 for (int i = 0; i < buffer.length; i++) {
2970 if (buffer[i] != firstColor) {
2971 allBlack = false;
2972 break;
2973 }
2974 }
2975 if (allBlack) {
2976 final WindowState appWin = mScreenshotApplicationState.appWin;
2977 final int maxLayer = mScreenshotApplicationState.maxLayer;
2978 final int minLayer = mScreenshotApplicationState.minLayer;
2979 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2980 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2981 (appWin != null ?
2982 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2983 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2984 }
2985 }
2986
2987 // Create a copy of the screenshot that is immutable and backed in ashmem.
2988 // This greatly reduces the overhead of passing the bitmap between processes.
2989 Bitmap ret = bitmap.createAshmemBitmap(config);
2990 bitmap.recycle();
2991 return ret;
2992 }
2993
2994 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2995 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2996 boolean includeDecor) {
2997 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2998 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2999 }
3000
3001 private <E> E screenshotApplications(IBinder appToken, int width, int height,
3002 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
3003 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003004 int dw = mDisplayInfo.logicalWidth;
3005 int dh = mDisplayInfo.logicalHeight;
3006 if (dw == 0 || dh == 0) {
3007 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3008 + ": returning null. logical widthxheight=" + dw + "x" + dh);
3009 return null;
3010 }
3011
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003012 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003013
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003014 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003015 final Rect frame = new Rect();
3016 final Rect stackBounds = new Rect();
3017
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003018 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003019 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08003020 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003021 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02003022 if (!mService.mPolicy.isScreenOn()) {
3023 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
3024 + " was off.");
3025 return null;
3026 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003027 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 mScreenshotApplicationState.appWin = null;
3029 forAllWindows(w -> {
3030 if (!w.mHasSurface) {
3031 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003032 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003033 if (w.mLayer >= aboveAppLayer) {
3034 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003035 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003036 if (wallpaperOnly && !w.mIsWallpaper) {
3037 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003038 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003039 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003040 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003041 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003042 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3043 // then the target window state is this one.
3044 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003045 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003046 }
3047
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003048 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003049 // We have not ran across the target window yet, so it is probably behind
3050 // the wallpaper. This can happen when the keyguard is up and all windows
3051 // are moved behind the wallpaper. We don't want to include the wallpaper
3052 // layer in the screenshot as it will cover-up the layer of the target
3053 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003054 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003055 }
3056 // Fall through. The target window is in front of the wallpaper. For this
3057 // case we want to include the wallpaper layer in the screenshot because
3058 // the target window might have some transparent areas.
3059 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003060 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003061 // This app window is of no interest if it is not associated with the
3062 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003063 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003064 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003065 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003066 }
3067
3068 // Include this window.
3069
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003070 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003071
3072 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003073 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3074 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003075 final Task task = w.getTask();
3076 if (task != null) {
3077 task.getBounds(frame);
3078 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003079
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003080 // No task bounds? Too bad! Ain't no screenshot then.
3081 return true;
3082 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003083 } else {
3084 final Rect wf = w.mFrame;
3085 final Rect cr = w.mContentInsets;
3086 int left = wf.left + cr.left;
3087 int top = wf.top + cr.top;
3088 int right = wf.right - cr.right;
3089 int bottom = wf.bottom - cr.bottom;
3090 frame.union(left, top, right, bottom);
3091 w.getVisibleBounds(stackBounds);
3092 if (!Rect.intersects(frame, stackBounds)) {
3093 // Set frame empty if there's no intersection.
3094 frame.setEmpty();
3095 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003096 }
3097 }
3098
3099 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003100 (w.mAppToken != null && w.mAppToken.token == appToken)
3101 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi406386a82017-09-15 14:17:40 +02003102 if (foundTargetWs && winAnim.getShown() && winAnim.mLastAlpha > 0f) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003103 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003104 }
3105
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003106 if (w.isObscuringDisplay()){
3107 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003108 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003109 return false;
3110 }, true /* traverseTopToBottom */);
3111
3112 final WindowState appWin = mScreenshotApplicationState.appWin;
3113 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003114
3115 if (appToken != null && appWin == null) {
3116 // Can't find a window to snapshot.
3117 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3118 "Screenshot: Couldn't find a surface matching " + appToken);
3119 return null;
3120 }
3121
3122 if (!screenshotReady) {
3123 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3124 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3125 appWin.mWinAnimator.mDrawState)));
3126 return null;
3127 }
3128
3129 // Screenshot is ready to be taken. Everything from here below will continue
3130 // through the bottom of the loop and return a value. We only stay in the loop
3131 // because we don't want to release the mWindowMap lock until the screenshot is
3132 // taken.
3133
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003134
Jorim Jaggi02886a82016-12-06 09:10:06 -08003135 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003136 // Constrain frame to the screen size.
3137 if (!frame.intersect(0, 0, dw, dh)) {
3138 frame.setEmpty();
3139 }
3140 } else {
3141 // Caller just wants entire display.
3142 frame.set(0, 0, dw, dh);
3143 }
3144 if (frame.isEmpty()) {
3145 return null;
3146 }
3147
3148 if (width < 0) {
3149 width = (int) (frame.width() * frameScale);
3150 }
3151 if (height < 0) {
3152 height = (int) (frame.height() * frameScale);
3153 }
3154
3155 // Tell surface flinger what part of the image to crop. Take the top
3156 // right part of the application, and crop the larger dimension to fit.
3157 Rect crop = new Rect(frame);
3158 if (width / (float) frame.width() < height / (float) frame.height()) {
3159 int cropWidth = (int)((float)width / (float)height * frame.height());
3160 crop.right = crop.left + cropWidth;
3161 } else {
3162 int cropHeight = (int)((float)height / (float)width * frame.width());
3163 crop.bottom = crop.top + cropHeight;
3164 }
3165
3166 // The screenshot API does not apply the current screen rotation.
3167 int rot = mDisplay.getRotation();
3168
3169 if (rot == ROTATION_90 || rot == ROTATION_270) {
3170 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3171 }
3172
3173 // Surfaceflinger is not aware of orientation, so convert our logical
3174 // crop to surfaceflinger's portrait orientation.
3175 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3176
3177 if (DEBUG_SCREENSHOT) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003178 forAllWindows(w -> {
3179 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3180 Slog.i(TAG_WM, w + ": " + w.mLayer
3181 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003182 + " surfaceLayer=" + ((controller == null)
3183 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003184 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003185 }
3186
3187 final ScreenRotationAnimation screenRotationAnimation =
3188 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3189 final boolean inRotation = screenRotationAnimation != null &&
3190 screenRotationAnimation.isAnimating();
3191 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3192 "Taking screenshot while rotating");
3193
3194 // We force pending transactions to flush before taking
3195 // the screenshot by pushing an empty synchronous transaction.
3196 SurfaceControl.openTransaction();
3197 SurfaceControl.closeTransactionSync();
3198
Robert Carrb1579c82017-09-05 14:54:47 -07003199 // TODO(b/68392460): We should screenshot Task controls directly
3200 // but it's difficult at the moment as the Task doesn't have the
3201 // correct size set.
3202 bitmap = screenshoter.screenshot(crop, width, height, 0, 1,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003203 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003204 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07003205 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003206 return null;
3207 }
3208 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003209 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003210 }
3211
3212 // TODO: Can this use createRotationMatrix()?
3213 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3214 if (rot == Surface.ROTATION_90) {
3215 final int tmp = crop.top;
3216 crop.top = dw - crop.right;
3217 crop.right = crop.bottom;
3218 crop.bottom = dw - crop.left;
3219 crop.left = tmp;
3220 } else if (rot == Surface.ROTATION_180) {
3221 int tmp = crop.top;
3222 crop.top = dh - crop.bottom;
3223 crop.bottom = dh - tmp;
3224 tmp = crop.right;
3225 crop.right = dw - crop.left;
3226 crop.left = dw - tmp;
3227 } else if (rot == Surface.ROTATION_270) {
3228 final int tmp = crop.top;
3229 crop.top = crop.left;
3230 crop.left = dh - crop.bottom;
3231 crop.bottom = crop.right;
3232 crop.right = dh - tmp;
3233 }
3234 }
3235
3236 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003237 // Used to indicate the layout is needed.
3238 mTmpWindow = null;
3239
3240 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003241 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003242 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003243 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003244 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003245 w.setDisplayLayoutNeeded();
3246 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003247 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003248
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003249 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003250 mService.mWindowPlacerLocked.performSurfacePlacement();
3251 }
3252 }
3253
Wale Ogunwale1666e312016-12-16 11:27:18 -08003254 void setExitingTokensHasVisible(boolean hasVisible) {
3255 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3256 mExitingTokens.get(i).hasVisible = hasVisible;
3257 }
3258
3259 // Initialize state of exiting applications.
3260 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3261 }
3262
3263 void removeExistingTokensIfPossible() {
3264 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3265 final WindowToken token = mExitingTokens.get(i);
3266 if (!token.hasVisible) {
3267 mExitingTokens.remove(i);
3268 }
3269 }
3270
3271 // Time to remove any exiting applications?
3272 mTaskStackContainers.removeExistingAppTokensIfPossible();
3273 }
3274
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003275 @Override
3276 void onDescendantOverrideConfigurationChanged() {
3277 setLayoutNeeded();
3278 mService.requestTraversal();
3279 }
3280
David Stevens9440dc82017-03-16 19:00:20 -07003281 boolean okToDisplay() {
3282 if (mDisplayId == DEFAULT_DISPLAY) {
3283 return !mService.mDisplayFrozen
3284 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3285 }
3286 return mDisplayInfo.state == Display.STATE_ON;
3287 }
3288
3289 boolean okToAnimate() {
3290 return okToDisplay() &&
3291 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3292 }
3293
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003294 static final class TaskForResizePointSearchResult {
3295 boolean searchDone;
3296 Task taskForResize;
3297
3298 void reset() {
3299 searchDone = false;
3300 taskForResize = null;
3301 }
3302 }
Robert Carr3b716242016-08-16 16:02:21 -07003303
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003304 private static final class ApplySurfaceChangesTransactionState {
3305 boolean displayHasContent;
3306 boolean obscured;
3307 boolean syswin;
3308 boolean focusDisplayed;
3309 float preferredRefreshRate;
3310 int preferredModeId;
3311
3312 void reset() {
3313 displayHasContent = false;
3314 obscured = false;
3315 syswin = false;
3316 focusDisplayed = false;
3317 preferredRefreshRate = 0;
3318 preferredModeId = 0;
3319 }
3320 }
3321
3322 private static final class ScreenshotApplicationState {
3323 WindowState appWin;
3324 int maxLayer;
3325 int minLayer;
3326 boolean screenshotReady;
3327
3328 void reset(boolean screenshotReady) {
3329 appWin = null;
3330 maxLayer = 0;
3331 minLayer = 0;
3332 this.screenshotReady = screenshotReady;
3333 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3334 }
3335 }
3336
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003337 /**
3338 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3339 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3340 * homogeneous children type which is currently required by sub-classes of
3341 * {@link WindowContainer} class.
3342 */
3343 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3344
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003345 @Override
3346 boolean fillsParent() {
3347 return true;
3348 }
3349
3350 @Override
3351 boolean isVisible() {
3352 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003353 }
3354 }
3355
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003356 /**
3357 * Window container class that contains all containers on this display relating to Apps.
3358 * I.e Activities.
3359 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003360 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003361 /**
3362 * A control placed at the appropriate level for transitions to occur.
3363 */
3364 SurfaceControl mAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003365
Wale Ogunwale61911492017-10-11 08:50:50 -07003366 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3367 // through the list to find them.
3368 private TaskStack mHomeStack = null;
3369 private TaskStack mPinnedStack = null;
3370 private TaskStack mSplitScreenPrimaryStack = null;
3371
3372 /**
3373 * Returns the topmost stack on the display that is compatible with the input windowing mode
3374 * and activity type. Null is no compatible stack on the display.
3375 */
3376 TaskStack getStack(int windowingMode, int activityType) {
3377 if (activityType == ACTIVITY_TYPE_HOME) {
3378 return mHomeStack;
3379 }
3380 if (windowingMode == WINDOWING_MODE_PINNED) {
3381 return mPinnedStack;
3382 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3383 return mSplitScreenPrimaryStack;
3384 }
3385 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3386 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003387 if (activityType == ACTIVITY_TYPE_UNDEFINED
3388 && windowingMode == stack.getWindowingMode()) {
3389 // Passing in undefined type means we want to match the topmost stack with the
3390 // windowing mode.
3391 return stack;
3392 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003393 if (stack.isCompatible(windowingMode, activityType)) {
3394 return stack;
3395 }
3396 }
3397 return null;
3398 }
3399
3400 @VisibleForTesting
3401 TaskStack getTopStack() {
3402 return mTaskStackContainers.getChildCount() > 0
3403 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3404 }
3405
3406 TaskStack getHomeStack() {
3407 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3408 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3409 }
3410 return mHomeStack;
3411 }
3412
3413 TaskStack getPinnedStack() {
3414 return mPinnedStack;
3415 }
3416
Matthew Ng64e77cf2017-10-31 14:01:31 -07003417 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003418 return mSplitScreenPrimaryStack;
3419 }
3420
Andrii Kulian839def92016-11-02 10:58:58 -07003421 /**
3422 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003423 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003424 */
3425 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003426 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003427 addChild(stack, onTop);
3428 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003429 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003430
Wale Ogunwale61911492017-10-11 08:50:50 -07003431 void onStackWindowingModeChanged(TaskStack stack) {
3432 removeStackReferenceIfNeeded(stack);
3433 addStackReferenceIfNeeded(stack);
3434 if (stack == mPinnedStack && getTopStack() != stack) {
3435 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3436 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3437 }
3438 }
3439
3440 private void addStackReferenceIfNeeded(TaskStack stack) {
3441 if (stack.isActivityTypeHome()) {
3442 if (mHomeStack != null) {
3443 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3444 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3445 }
3446 mHomeStack = stack;
3447 }
3448 final int windowingMode = stack.getWindowingMode();
3449 if (windowingMode == WINDOWING_MODE_PINNED) {
3450 if (mPinnedStack != null) {
3451 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3452 + mPinnedStack + " already exist on display=" + this
3453 + " stack=" + stack);
3454 }
3455 mPinnedStack = stack;
3456 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3457 if (mSplitScreenPrimaryStack != null) {
3458 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3459 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3460 + " already exist on display=" + this + " stack=" + stack);
3461 }
3462 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003463 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003464 }
3465 }
3466
3467 private void removeStackReferenceIfNeeded(TaskStack stack) {
3468 if (stack == mHomeStack) {
3469 mHomeStack = null;
3470 } else if (stack == mPinnedStack) {
3471 mPinnedStack = null;
3472 } else if (stack == mSplitScreenPrimaryStack) {
3473 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003474 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3475 mService.setDockedStackCreateStateLocked(
3476 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3477 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003478 }
Andrii Kulian839def92016-11-02 10:58:58 -07003479 }
3480
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003481 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003482 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3483 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003484 addChild(stack, addIndex);
3485 setLayoutNeeded();
3486 }
3487
Wale Ogunwale61911492017-10-11 08:50:50 -07003488 @Override
3489 protected void removeChild(TaskStack stack) {
3490 super.removeChild(stack);
3491 removeStackReferenceIfNeeded(stack);
3492 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003493
3494 @Override
3495 boolean isOnTop() {
3496 // Considered always on top
3497 return true;
3498 }
3499
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003500 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003501 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003502 if (child.getWindowConfiguration().isAlwaysOnTop()
3503 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003504 // This stack is always-on-top, override the default behavior.
3505 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3506
3507 // Moving to its current position, as we must call super but we don't want to
3508 // perform any meaningful action.
3509 final int currentPosition = mChildren.indexOf(child);
3510 super.positionChildAt(currentPosition, child, false /* includingParents */);
3511 return;
3512 }
3513
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003514 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3515 super.positionChildAt(targetPosition, child, includingParents);
3516
3517 setLayoutNeeded();
3518 }
3519
3520 /**
3521 * When stack is added or repositioned, find a proper position for it.
3522 * This will make sure that pinned stack always stays on top.
3523 * @param requestedPosition Position requested by caller.
3524 * @param stack Stack to be added or positioned.
3525 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3526 * @return The proper position for the stack.
3527 */
3528 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3529 final int topChildPosition = mChildren.size() - 1;
3530 boolean toTop = requestedPosition == POSITION_TOP;
3531 toTop |= adding ? requestedPosition >= topChildPosition + 1
3532 : requestedPosition >= topChildPosition;
3533 int targetPosition = requestedPosition;
3534
Wale Ogunwale61911492017-10-11 08:50:50 -07003535 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003536 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003537 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003538 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003539 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3540 }
3541
3542 // So, stack is moved just below the pinned stack.
3543 // When we're adding a new stack the target is the current pinned stack position.
3544 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003545 // stack, because WindowContainer#positionAt() first removes element and then adds
3546 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003547 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3548 }
3549
3550 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003551 }
3552
3553 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003554 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3555 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003556 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003557 if (super.forAllWindows(callback, traverseTopToBottom)) {
3558 return true;
3559 }
3560 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3561 return true;
3562 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003563 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003564 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3565 return true;
3566 }
3567 if (super.forAllWindows(callback, traverseTopToBottom)) {
3568 return true;
3569 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003570 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003571 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003572 }
3573
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003574 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003575 boolean traverseTopToBottom) {
3576 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3577 // app tokens.
3578 // TODO: Investigate if we need to continue to do this or if we can just process them
3579 // in-order.
3580 if (traverseTopToBottom) {
3581 for (int i = mChildren.size() - 1; i >= 0; --i) {
3582 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3583 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003584 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3585 traverseTopToBottom)) {
3586 return true;
3587 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003588 }
3589 }
3590 } else {
3591 final int count = mChildren.size();
3592 for (int i = 0; i < count; ++i) {
3593 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3594 final int appTokensCount = appTokens.size();
3595 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003596 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3597 traverseTopToBottom)) {
3598 return true;
3599 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003600 }
3601 }
3602 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003603 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003604 }
3605
Wale Ogunwale1666e312016-12-16 11:27:18 -08003606 void setExitingTokensHasVisible(boolean hasVisible) {
3607 for (int i = mChildren.size() - 1; i >= 0; --i) {
3608 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3609 for (int j = appTokens.size() - 1; j >= 0; --j) {
3610 appTokens.get(j).hasVisible = hasVisible;
3611 }
3612 }
3613 }
3614
3615 void removeExistingAppTokensIfPossible() {
3616 for (int i = mChildren.size() - 1; i >= 0; --i) {
3617 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3618 for (int j = appTokens.size() - 1; j >= 0; --j) {
3619 final AppWindowToken token = appTokens.get(j);
3620 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3621 && (!token.mIsExiting || token.isEmpty())) {
3622 // Make sure there is no animation running on this token, so any windows
3623 // associated with it will be removed as soon as their animations are
3624 // complete.
3625 token.mAppAnimator.clearAnimation();
3626 token.mAppAnimator.animating = false;
3627 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3628 "performLayout: App token exiting now removed" + token);
3629 token.removeIfPossible();
3630 }
3631 }
3632 }
3633 }
3634
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003635 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003636 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003637 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3638 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003639 // Apps and their containers are not allowed to specify an orientation while the
3640 // docked or freeform stack is visible...except for the home stack/task if the
3641 // docked stack is minimized and it actually set something.
3642 if (mHomeStack != null && mHomeStack.isVisible()
3643 && mDividerControllerLocked.isMinimizedDock()) {
3644 final int orientation = mHomeStack.getOrientation();
3645 if (orientation != SCREEN_ORIENTATION_UNSET) {
3646 return orientation;
3647 }
3648 }
3649 return SCREEN_ORIENTATION_UNSPECIFIED;
3650 }
3651
3652 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003653 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3654 PackageManager.FEATURE_AUTOMOTIVE);
3655 if (isCar) {
3656 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3657 // allow anything but the default orientation.
3658 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3659 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3660 return SCREEN_ORIENTATION_UNSPECIFIED;
3661 }
3662
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003663 if (orientation != SCREEN_ORIENTATION_UNSET
3664 && orientation != SCREEN_ORIENTATION_BEHIND) {
3665 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3666 "App is requesting an orientation, return " + orientation);
3667 return orientation;
3668 }
3669
3670 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003671 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003672 // The next app has not been requested to be visible, so we keep the current orientation
3673 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003674 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003675 }
Robert Carrb1579c82017-09-05 14:54:47 -07003676
3677 @Override
3678 void assignChildLayers(SurfaceControl.Transaction t) {
3679 final int NORMAL_STACK_STATE = 0;
3680 final int BOOSTED_STATE = 1;
3681 final int ALWAYS_ON_TOP_STATE = 2;
3682
3683 // We allow stacks to change visual order from the AM specified order due to
3684 // Z-boosting during animations. However we must take care to ensure TaskStacks
3685 // which are marked as alwaysOnTop remain that way.
3686 int layer = 0;
3687 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3688 for (int i = 0; i < mChildren.size(); i++) {
3689 final TaskStack s = mChildren.get(i);
3690 layer++;
3691 if (state == NORMAL_STACK_STATE) {
3692 s.assignLayer(t, layer);
3693 } else if (state == BOOSTED_STATE && s.needsZBoost()) {
3694 s.assignLayer(t, layer);
3695 } else if (state == ALWAYS_ON_TOP_STATE &&
3696 s.isAlwaysOnTop()) {
3697 s.assignLayer(t, layer);
3698 }
3699 s.assignChildLayers(t);
3700 }
3701 // The appropriate place for App-Transitions to occur is right
3702 // above all other animations but still below things in the Picture-and-Picture
3703 // windowing mode.
3704 if (state == BOOSTED_STATE && mAnimationLayer != null) {
3705 t.setLayer(mAnimationLayer, layer + 1);
3706 }
3707 }
3708 }
3709
3710 @Override
3711 void onParentSet() {
3712 super.onParentSet();
3713 if (getParent() != null) {
3714 mAnimationLayer = makeSurface().build();
3715 } else {
3716 mAnimationLayer.destroy();
3717 mAnimationLayer = null;
3718 }
3719 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003720 }
3721
3722 /**
3723 * Window container class that contains all containers on this display that are not related to
3724 * Apps. E.g. status bar.
3725 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003726 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3727 /**
3728 * Compares two child window tokens returns -1 if the first is lesser than the second in
3729 * terms of z-order and 1 otherwise.
3730 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003731 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003732 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003733 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3734 token1.mOwnerCanManageAppTokens)
3735 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3736 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003737
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003738 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3739 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3740 return false;
3741 }
3742 final int req = w.mAttrs.screenOrientation;
3743 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3744 || req == SCREEN_ORIENTATION_UNSET) {
3745 return false;
3746 }
3747 return true;
3748 };
3749
Wale Ogunwale3a931692016-11-02 16:49:48 -07003750 private final String mName;
3751 NonAppWindowContainers(String name) {
3752 mName = name;
3753 }
3754
3755 void addChild(WindowToken token) {
3756 addChild(token, mWindowComparator);
3757 }
3758
3759 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003760 int getOrientation() {
3761 final WindowManagerPolicy policy = mService.mPolicy;
3762 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003763 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003764
3765 if (win != null) {
3766 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003767 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003768 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003769 if (mService.mKeyguardGoingAway) {
3770 // Keyguard can't affect the orientation if it is going away...
3771 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3772 return SCREEN_ORIENTATION_UNSET;
3773 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003774 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003775 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003776 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003777 }
3778
Andrii Kulian8ee72852017-03-10 10:36:45 -08003779 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003780
Jorim Jaggi75520d52017-08-24 17:23:19 +02003781 if (policy.isKeyguardShowingAndNotOccluded()
3782 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003783 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003784 }
3785
3786 return SCREEN_ORIENTATION_UNSET;
3787 }
3788
3789 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003790 String getName() {
3791 return mName;
3792 }
Robert Carr3b716242016-08-16 16:02:21 -07003793 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003794
3795 /**
3796 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3797 */
3798 @FunctionalInterface
3799 private interface Screenshoter<E> {
3800 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3801 boolean useIdentityTransform, int rotation);
3802 }
Robert Carrb1579c82017-09-05 14:54:47 -07003803
3804 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3805 return mService.makeSurfaceBuilder(s)
3806 .setParent(mWindowingLayer);
3807 }
3808
3809 @Override
3810 SurfaceSession getSession() {
3811 return mSession;
3812 }
3813
3814 @Override
3815 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003816 SurfaceSession s = child != null ? child.getSession() : getSession();
3817 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003818 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003819
3820 if (child == null) {
3821 return b;
3822 }
3823
3824 b.setName(child.getName());
Robert Carrb1579c82017-09-05 14:54:47 -07003825 if (child.isScreenOverlay()) {
3826 return b.setParent(mOverlayLayer);
3827 } else {
3828 return b.setParent(mWindowingLayer);
3829 }
3830 }
3831
3832 /**
3833 * The makeSurface variants are for use by the window-container
3834 * hierarchy. makeOverlay here is a function for various non windowing
3835 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3836 * and other potpourii.
3837 */
3838 SurfaceControl.Builder makeOverlay() {
3839 return mService.makeSurfaceBuilder(mSession)
3840 .setParent(mOverlayLayer);
3841 }
3842
3843 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003844 applyMagnificationSpec(getPendingTransaction(), spec);
3845 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003846 }
3847
3848 @Override
3849 void onParentSet() {
3850 // Since we are the top of the SurfaceControl hierarchy here
3851 // we create the root surfaces explicitly rather than chaining
3852 // up as the default implementation in onParentSet does. So we
3853 // explicitly do NOT call super here.
3854 }
3855
3856 @Override
3857 void assignChildLayers(SurfaceControl.Transaction t) {
3858 t.setLayer(mOverlayLayer, 1)
3859 .setLayer(mWindowingLayer, 0);
3860
3861 // These are layers as children of "mWindowingLayer"
3862 mBelowAppWindowsContainers.assignLayer(t, 0);
3863 mTaskStackContainers.assignLayer(t, 1);
3864 mAboveAppWindowsContainers.assignLayer(t, 2);
3865
3866 WindowState imeTarget = mService.mInputMethodTarget;
3867 if (imeTarget == null || imeTarget.inSplitScreenWindowingMode()) {
3868 // In split-screen windowing mode we can't layer the
3869 // IME relative to the IME target because it needs to
3870 // go over the docked divider, so instead we place it on top
3871 // of everything and use relative layering of windows which need
3872 // to go above it (see special logic in WindowState#assignLayer)
3873 mImeWindowsContainers.assignLayer(t, 3);
3874 } else {
3875 t.setRelativeLayer(mImeWindowsContainers.getSurfaceControl(),
3876 imeTarget.getSurfaceControl(),
3877 // TODO: We need to use an extra level on the app surface to ensure
3878 // this is always above SurfaceView but always below attached window.
3879 1);
3880 }
3881
3882 // Above we have assigned layers to our children, now we ask them to assign
3883 // layers to their children.
3884 mBelowAppWindowsContainers.assignChildLayers(t);
3885 mTaskStackContainers.assignChildLayers(t);
3886 mAboveAppWindowsContainers.assignChildLayers(t);
3887 mImeWindowsContainers.assignChildLayers(t);
3888 }
3889
3890 /**
3891 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3892 * that the IME target is one of the docked applications. We'd like the docked divider to be
3893 * above both of the applications, and we'd like the IME to be above the docked divider.
3894 * However we need child windows of the applications to be above the IME (Text drag handles).
3895 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3896 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3897 * with {@link #WindowState#assignLayer}
3898 */
3899 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3900 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3901 }
3902
3903 @Override
3904 void destroyAfterPendingTransaction(SurfaceControl surface) {
3905 mPendingDestroyingSurfaces.add(surface);
3906 }
3907
3908 /**
3909 * Destroys any surfaces that have been put into the pending list with
3910 * {@link #destroyAfterTransaction}.
3911 */
3912 void onPendingTransactionApplied() {
3913 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3914 mPendingDestroyingSurfaces.get(i).destroy();
3915 }
3916 mPendingDestroyingSurfaces.clear();
3917 }
Craig Mautner59c00972012-07-30 12:10:24 -07003918}