blob: 3fb3773852202e1fb558afc33faba979a691dcfa [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
Bryce Leef3c6a472017-11-14 14:53:06 -0800317 /** Used for handing back size of display */
318 private final Rect mTmpBounds = new Rect();
319
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800320 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700321
Craig Mautner95da1082014-02-24 17:54:35 -0800322 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700323 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800324
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700325 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700326 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700327
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800328 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
329
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000330 private boolean mHaveBootMsg = false;
331 private boolean mHaveApp = false;
332 private boolean mHaveWallpaper = false;
333 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700334
335 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
336
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700337 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
338 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000339 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
340 new ApplySurfaceChangesTransactionState();
341 private final ScreenshotApplicationState mScreenshotApplicationState =
342 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700343
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700344 // True if this display is in the process of being removed. Used to determine if the removal of
345 // the display's direct children should be allowed.
346 private boolean mRemovingDisplay = false;
347
Bryce Leed1871262017-06-12 14:12:29 -0700348 // {@code false} if this display is in the processing of being created.
349 private boolean mDisplayReady = false;
350
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800351 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700352 int mInputMethodAnimLayerAdjustment;
353
Robert Carrb1579c82017-09-05 14:54:47 -0700354 private final SurfaceSession mSession = new SurfaceSession();
355
356 /**
357 * We organize all top-level Surfaces in to the following layers.
358 * mOverlayLayer contains a few Surfaces which are always on top of others
359 * and omitted from Screen-Magnification ({@link WindowState#isScreenOverlay})
360 * {@link #mWindowingLayer} contains everything else.
361 */
362 private SurfaceControl mOverlayLayer;
363
364 /**
365 * See {@link #mOverlayLayer}
366 */
367 private SurfaceControl mWindowingLayer;
368
369 /**
370 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
371 * <p>
372 * For these surfaces currently we use a surface based on the larger of width or height so we
373 * don't have to resize when rotating the display.
374 */
375 private int mSurfaceSize;
376
377 /**
378 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
379 */
380 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
381
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800382 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
383 WindowStateAnimator winAnimator = w.mWinAnimator;
384 if (winAnimator.hasSurface()) {
385 final boolean wasAnimating = winAnimator.mWasAnimating;
386 final boolean nowAnimating = winAnimator.stepAnimationLocked(
387 mTmpWindowAnimator.mCurrentTime);
388 winAnimator.mWasAnimating = nowAnimating;
389 mTmpWindowAnimator.orAnimating(nowAnimating);
390
391 if (DEBUG_WALLPAPER) Slog.v(TAG,
392 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
393
394 if (wasAnimating && !winAnimator.mAnimating
395 && mWallpaperController.isWallpaperTarget(w)) {
396 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
397 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
398 if (DEBUG_LAYOUT_REPEATS) {
399 mService.mWindowPlacerLocked.debugLayoutRepeats(
400 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
401 }
402 }
403 }
404
405 final AppWindowToken atoken = w.mAppToken;
406 if (winAnimator.mDrawState == READY_TO_SHOW) {
407 if (atoken == null || atoken.allDrawn) {
408 if (w.performShowLocked()) {
409 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
410 if (DEBUG_LAYOUT_REPEATS) {
411 mService.mWindowPlacerLocked.debugLayoutRepeats(
412 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
413 }
414 }
415 }
416 }
417 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
418 if (appAnimator != null && appAnimator.thumbnail != null) {
419 if (appAnimator.thumbnailTransactionSeq
420 != mTmpWindowAnimator.mAnimTransactionSequence) {
421 appAnimator.thumbnailTransactionSeq =
422 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800423 }
424 }
425 };
426
427 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
428 final WindowStateAnimator winAnimator = w.mWinAnimator;
429 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
430 return;
431 }
432
433 final int flags = w.mAttrs.flags;
434
435 // If this window is animating, make a note that we have an animating window and take
436 // care of a request to run a detached wallpaper animation.
437 if (winAnimator.mAnimating) {
438 if (winAnimator.mAnimation != null) {
439 if ((flags & FLAG_SHOW_WALLPAPER) != 0
440 && winAnimator.mAnimation.getDetachWallpaper()) {
441 mTmpWindow = w;
442 }
443 final int color = winAnimator.mAnimation.getBackgroundColor();
444 if (color != 0) {
445 final TaskStack stack = w.getStack();
446 if (stack != null) {
447 stack.setAnimationBackground(winAnimator, color);
448 }
449 }
450 }
451 mTmpWindowAnimator.setAnimating(true);
452 }
453
454 // If this window's app token is running a detached wallpaper animation, make a note so
455 // we can ensure the wallpaper is displayed behind it.
456 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
457 if (appAnimator != null && appAnimator.animation != null
458 && appAnimator.animating) {
459 if ((flags & FLAG_SHOW_WALLPAPER) != 0
460 && appAnimator.animation.getDetachWallpaper()) {
461 mTmpWindow = w;
462 }
463
464 final int color = appAnimator.animation.getBackgroundColor();
465 if (color != 0) {
466 final TaskStack stack = w.getStack();
467 if (stack != null) {
468 stack.setAnimationBackground(winAnimator, color);
469 }
470 }
471 }
472 };
473
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800474 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
475 final int lostFocusUid = mTmpWindow.mOwnerUid;
476 final Handler handler = mService.mH;
477 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
478 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
479 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
480 w.mAttrs.hideTimeoutMilliseconds);
481 }
482 }
483 };
484
485 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
486 final AppWindowToken focusedApp = mService.mFocusedApp;
487 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
488 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
489
490 if (!w.canReceiveKeys()) {
491 return false;
492 }
493
494 final AppWindowToken wtoken = w.mAppToken;
495
496 // If this window's application has been removed, just skip it.
497 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
498 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
499 + (wtoken.removed ? "removed" : "sendingToBottom"));
500 return false;
501 }
502
503 if (focusedApp == null) {
504 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
505 + " using new focus @ " + w);
506 mTmpWindow = w;
507 return true;
508 }
509
510 if (!focusedApp.windowsAreFocusable()) {
511 // Current focused app windows aren't focusable...
512 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
513 + " focusable using new focus @ " + w);
514 mTmpWindow = w;
515 return true;
516 }
517
518 // Descend through all of the app tokens and find the first that either matches
519 // win.mAppToken (return win) or mFocusedApp (return null).
520 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
521 if (focusedApp.compareTo(wtoken) > 0) {
522 // App stack below focused app stack. No focus for you!!!
523 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
524 "findFocusedWindow: Reached focused app=" + focusedApp);
525 mTmpWindow = null;
526 return true;
527 }
528 }
529
530 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
531 mTmpWindow = w;
532 return true;
533 };
534
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800535 private final Consumer<WindowState> mPerformLayout = w -> {
536 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
537 // wasting time and funky changes while a window is animating away.
538 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
539 || w.isGoneForLayoutLw();
540
541 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
542 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
543 + " mLayoutAttached=" + w.mLayoutAttached
544 + " screen changed=" + w.isConfigChanged());
545 final AppWindowToken atoken = w.mAppToken;
546 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
547 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
548 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
549 + " parentHidden=" + w.isParentWindowHidden());
550 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
551 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
552 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
553 + " parentHidden=" + w.isParentWindowHidden());
554 }
555
556 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
557 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
558 // since that means "perform layout as normal, just don't display").
559 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
560 || ((w.isConfigChanged() || w.setReportResizeHints())
561 && !w.isGoneForLayoutLw() &&
562 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
563 (w.mHasSurface && w.mAppToken != null &&
564 w.mAppToken.layoutConfigChanges)))) {
565 if (!w.mLayoutAttached) {
566 if (mTmpInitial) {
567 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
568 w.mContentChanged = false;
569 }
570 if (w.mAttrs.type == TYPE_DREAM) {
571 // Don't layout windows behind a dream, so that if it does stuff like hide
572 // the status bar we won't get a bad transition when it goes away.
573 mTmpWindow = w;
574 }
575 w.mLayoutNeeded = false;
576 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200577 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000578 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800579 w.mLayoutSeq = mService.mLayoutSeq;
580
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200581 // If this is the first layout, we need to initialize the last inset values as
582 // otherwise we'd immediately cause an unnecessary resize.
583 if (firstLayout) {
584 w.updateLastInsetValues();
585 }
586
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800587 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
588 + " mContainingFrame=" + w.mContainingFrame
589 + " mDisplayFrame=" + w.mDisplayFrame);
590 }
591 }
592 };
593
594 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
595 if (w.mLayoutAttached) {
596 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
597 + " mViewVisibility=" + w.mViewVisibility
598 + " mRelayoutCalled=" + w.mRelayoutCalled);
599 // If this view is GONE, then skip it -- keep the current frame, and let the caller
600 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
601 // windows, since that means "perform layout as normal, just don't display").
602 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
603 return;
604 }
605 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
606 || w.mLayoutNeeded) {
607 if (mTmpInitial) {
608 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
609 w.mContentChanged = false;
610 }
611 w.mLayoutNeeded = false;
612 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000613 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800614 w.mLayoutSeq = mService.mLayoutSeq;
615 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
616 + " mContainingFrame=" + w.mContainingFrame
617 + " mDisplayFrame=" + w.mDisplayFrame);
618 }
619 } else if (w.mAttrs.type == TYPE_DREAM) {
620 // Don't layout windows behind a dream, so that if it does stuff like hide the
621 // status bar we won't get a bad transition when it goes away.
622 mTmpWindow = mTmpWindow2;
623 }
624 };
625
626 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
627 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
628 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
629 return w.canBeImeTarget();
630 };
631
632 private final Consumer<WindowState> mApplyPostLayoutPolicy =
633 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
634 mService.mInputMethodTarget);
635
636 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
637 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
638 final boolean obscuredChanged = w.mObscured !=
639 mTmpApplySurfaceChangesTransactionState.obscured;
640 final RootWindowContainer root = mService.mRoot;
641 // Only used if default window
642 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
643
644 // Update effect.
645 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
646 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
647 final boolean isDisplayed = w.isDisplayedLw();
648
649 if (isDisplayed && w.isObscuringDisplay()) {
650 // This window completely covers everything behind it, so we want to leave all
651 // of them as undimmed (for performance reasons).
652 root.mObscuringWindow = w;
653 mTmpApplySurfaceChangesTransactionState.obscured = true;
654 }
655
656 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
657 root.handleNotObscuredLocked(w,
658 mTmpApplySurfaceChangesTransactionState.obscured,
659 mTmpApplySurfaceChangesTransactionState.syswin);
660
661 if (w.mHasSurface && isDisplayed) {
662 final int type = w.mAttrs.type;
663 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
664 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
665 mTmpApplySurfaceChangesTransactionState.syswin = true;
666 }
667 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
668 && w.mAttrs.preferredRefreshRate != 0) {
669 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
670 = w.mAttrs.preferredRefreshRate;
671 }
672 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
673 && w.mAttrs.preferredDisplayModeId != 0) {
674 mTmpApplySurfaceChangesTransactionState.preferredModeId
675 = w.mAttrs.preferredDisplayModeId;
676 }
677 }
678 }
679
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800680 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
681 && mWallpaperController.isWallpaperTarget(w)) {
682 // This is the wallpaper target and its obscured state changed... make sure the
683 // current wallpaper's visibility has been updated accordingly.
684 mWallpaperController.updateWallpaperVisibility();
685 }
686
687 w.handleWindowMovedIfNeeded();
688
689 final WindowStateAnimator winAnimator = w.mWinAnimator;
690
691 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
692 w.mContentChanged = false;
693
694 // Moved from updateWindowsAndWallpaperLocked().
695 if (w.mHasSurface) {
696 // Take care of the window being ready to display.
697 final boolean committed = winAnimator.commitFinishDrawingLocked();
698 if (isDefaultDisplay && committed) {
699 if (w.mAttrs.type == TYPE_DREAM) {
700 // HACK: When a dream is shown, it may at that point hide the lock screen.
701 // So we need to redo the layout to let the phone window manager make this
702 // happen.
703 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
704 if (DEBUG_LAYOUT_REPEATS) {
705 surfacePlacer.debugLayoutRepeats(
706 "dream and commitFinishDrawingLocked true",
707 pendingLayoutChanges);
708 }
709 }
710 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
711 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
712 "First draw done in potential wallpaper target " + w);
713 root.mWallpaperMayChange = true;
714 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
715 if (DEBUG_LAYOUT_REPEATS) {
716 surfacePlacer.debugLayoutRepeats(
717 "wallpaper and commitFinishDrawingLocked true",
718 pendingLayoutChanges);
719 }
720 }
721 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700722 final TaskStack stack = w.getStack();
723 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
724 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800725 // Updates the shown frame before we set up the surface. This is needed
726 // because the resizing could change the top-left position (in addition to
727 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
728 // position the surface.
729 //
730 // If an animation is being started, we can't call this method because the
731 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700732 // we do want to update the position if the window is animating. We make an exception
733 // for the bounds animating state, where an application may have been waiting
734 // for an exit animation to start, but instead enters PiP. We need to ensure
735 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800736 winAnimator.computeShownFrameLocked();
737 }
738 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
739 }
740
741 final AppWindowToken atoken = w.mAppToken;
742 if (atoken != null) {
743 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
744 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
745 mTmpUpdateAllDrawn.add(atoken);
746 }
747 }
748
749 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
750 && w.isDisplayedLw()) {
751 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
752 }
753
754 w.updateResizingWindowIfNeeded();
755 };
756
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800757 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800758 * Create new {@link DisplayContent} instance, add itself to the root window container and
759 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700760 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800761 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700762 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
763 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700764 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700765 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700766 WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800767 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
768 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
769 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
770 + " new=" + display);
771 }
772
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700773 mDisplay = display;
774 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700775 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700776 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700777 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700778 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700779 mService = service;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000780 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700781 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800782 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700783 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700784
Robert Carrb1579c82017-09-05 14:54:47 -0700785 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
786
787 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
788 .setSize(mSurfaceSize, mSurfaceSize)
789 .setOpaque(true);
790 mWindowingLayer = b.setName("Display Root").build();
791 mOverlayLayer = b.setName("Display Overlays").build();
792
Robert Carrf59b8dd2017-10-02 18:58:36 -0700793 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700794 .setLayerStack(mWindowingLayer, mDisplayId)
795 .show(mWindowingLayer)
796 .setLayer(mOverlayLayer, 1)
797 .setLayerStack(mOverlayLayer, mDisplayId)
798 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700799 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700800
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700801 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700802 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700803 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700804 super.addChild(mAboveAppWindowsContainers, null);
805 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800806
807 // Add itself as a child to the root container.
808 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700809
810 // TODO(b/62541591): evaluate whether this is the best spot to declare the
811 // {@link DisplayContent} ready for use.
812 mDisplayReady = true;
813 }
814
815 boolean isReady() {
816 // The display is ready when the system and the individual display are both ready.
817 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700818 }
819
820 int getDisplayId() {
821 return mDisplayId;
822 }
823
Wale Ogunwale02319a62016-09-26 15:21:22 -0700824 WindowToken getWindowToken(IBinder binder) {
825 return mTokenMap.get(binder);
826 }
827
828 AppWindowToken getAppWindowToken(IBinder binder) {
829 final WindowToken token = getWindowToken(binder);
830 if (token == null) {
831 return null;
832 }
833 return token.asAppWindowToken();
834 }
835
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700836 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700837 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
838 if (dc != null) {
839 // We currently don't support adding a window token to the display if the display
840 // already has the binder mapped to another token. If there is a use case for supporting
841 // this moving forward we will either need to merge the WindowTokens some how or have
842 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700843 throw new IllegalArgumentException("Can't map token=" + token + " to display="
844 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700845 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700846 if (binder == null) {
847 throw new IllegalArgumentException("Can't map token=" + token + " to display="
848 + getName() + " binder is null");
849 }
850 if (token == null) {
851 throw new IllegalArgumentException("Can't map null token to display="
852 + getName() + " binder=" + binder);
853 }
854
Wale Ogunwale02319a62016-09-26 15:21:22 -0700855 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700856
857 if (token.asAppWindowToken() == null) {
858 // Add non-app token to container hierarchy on the display. App tokens are added through
859 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700860 switch (token.windowType) {
861 case TYPE_WALLPAPER:
862 mBelowAppWindowsContainers.addChild(token);
863 break;
864 case TYPE_INPUT_METHOD:
865 case TYPE_INPUT_METHOD_DIALOG:
866 mImeWindowsContainers.addChild(token);
867 break;
868 default:
869 mAboveAppWindowsContainers.addChild(token);
870 break;
871 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700872 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700873 }
874
875 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700876 final WindowToken token = mTokenMap.remove(binder);
877 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800878 token.setExiting();
879 }
880 return token;
881 }
882
883 /** Changes the display the input window token is housed on to this one. */
884 void reParentWindowToken(WindowToken token) {
885 final DisplayContent prevDc = token.getDisplayContent();
886 if (prevDc == this) {
887 return;
888 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800889 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
890 && token.asAppWindowToken() == null) {
891 // Removed the token from the map, but made sure it's not an app token before removing
892 // from parent.
893 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700894 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800895
896 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700897 }
898
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700899 void removeAppToken(IBinder binder) {
900 final WindowToken token = removeWindowToken(binder);
901 if (token == null) {
902 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
903 return;
904 }
905
906 final AppWindowToken appToken = token.asAppWindowToken();
907
908 if (appToken == null) {
909 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
910 return;
911 }
912
913 appToken.onRemovedFromDisplay();
914 }
915
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700916 Display getDisplay() {
917 return mDisplay;
918 }
919
Craig Mautner59c00972012-07-30 12:10:24 -0700920 DisplayInfo getDisplayInfo() {
921 return mDisplayInfo;
922 }
923
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700924 DisplayMetrics getDisplayMetrics() {
925 return mDisplayMetrics;
926 }
927
Andrii Kulian8ee72852017-03-10 10:36:45 -0800928 int getRotation() {
929 return mRotation;
930 }
931
932 void setRotation(int newRotation) {
933 mRotation = newRotation;
934 }
935
936 int getLastOrientation() {
937 return mLastOrientation;
938 }
939
940 void setLastOrientation(int orientation) {
941 mLastOrientation = orientation;
942 }
943
944 boolean getAltOrientation() {
945 return mAltOrientation;
946 }
947
948 void setAltOrientation(boolean altOrientation) {
949 mAltOrientation = altOrientation;
950 }
951
952 int getLastWindowForcedOrientation() {
953 return mLastWindowForcedOrientation;
954 }
955
Andrii Kulian06d07d62017-03-14 11:11:47 -0700956 /**
957 * Update rotation of the display.
958 *
959 * Returns true if the rotation has been changed. In this case YOU MUST CALL
960 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
961 */
962 boolean updateRotationUnchecked(boolean inTransaction) {
963 if (mService.mDeferredRotationPauseCount > 0) {
964 // Rotation updates have been paused temporarily. Defer the update until
965 // updates have been resumed.
966 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
967 return false;
968 }
969
970 ScreenRotationAnimation screenRotationAnimation =
971 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
972 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
973 // Rotation updates cannot be performed while the previous rotation change
974 // animation is still in progress. Skip this update. We will try updating
975 // again after the animation is finished and the display is unfrozen.
976 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
977 return false;
978 }
979 if (mService.mDisplayFrozen) {
980 // Even if the screen rotation animation has finished (e.g. isAnimating
981 // returns false), there is still some time where we haven't yet unfrozen
982 // the display. We also need to abort rotation here.
983 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
984 "Deferring rotation, still finishing previous rotation");
985 return false;
986 }
987
988 if (!mService.mDisplayEnabled) {
989 // No point choosing a rotation if the display is not enabled.
990 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
991 return false;
992 }
993
994 final int oldRotation = mRotation;
995 final int lastOrientation = mLastOrientation;
996 final boolean oldAltOrientation = mAltOrientation;
997 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700998 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700999 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001000
Robert Carr0e007272017-10-02 13:00:55 -07001001 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001002 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
1003 if (seamlessRotated != null) {
1004 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1005 // to complete (that is, waiting for windows to redraw). It's tempting to check
1006 // w.mSeamlessRotationCount but that could be incorrect in the case of
1007 // window-removal.
1008 return false;
1009 }
Robert Carr0e007272017-10-02 13:00:55 -07001010
1011 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001012 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001013 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001014 mayRotateSeamlessly = false;
1015 }
1016 for (int i = 0; i < mService.mSessions.size(); i++) {
1017 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1018 mayRotateSeamlessly = false;
1019 break;
1020 }
1021 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001022 }
Robert Carr0e007272017-10-02 13:00:55 -07001023 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001024
1025 // TODO: Implement forced rotation changes.
1026 // Set mAltOrientation to indicate that the application is receiving
1027 // an orientation that has different metrics than it expected.
1028 // eg. Portrait instead of Landscape.
1029
1030 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1031 lastOrientation, rotation);
1032
1033 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1034 + ", got rotation " + rotation + " which has "
1035 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1036
1037 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1038 // No change.
1039 return false;
1040 }
1041
1042 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1043 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1044 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1045
1046 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1047 mService.mWaitingForConfig = true;
1048 }
1049
1050 mRotation = rotation;
1051 mAltOrientation = altOrientation;
1052 if (isDefaultDisplay) {
1053 mService.mPolicy.setRotationLw(rotation);
1054 }
1055
1056 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1057 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1058 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1059 WINDOW_FREEZE_TIMEOUT_DURATION);
1060
1061 setLayoutNeeded();
1062 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001063 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001064
1065 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001066 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001067 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1068 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1069 mDisplayId);
1070 } else {
1071 // The screen rotation animation uses a screenshot to freeze the screen
1072 // while windows resize underneath.
1073 // When we are rotating seamlessly, we allow the elements to transition
1074 // to their rotated state independently and without a freeze required.
1075 screenRotationAnimation = null;
1076
1077 // We have to reset this in case a window was removed before it
1078 // finished seamless rotation.
1079 mService.mSeamlessRotationCount = 0;
1080 }
1081
1082 // We need to update our screen size information to match the new rotation. If the rotation
1083 // has actually changed then this method will return true and, according to the comment at
1084 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1085 // By updating the Display info here it will be available to
1086 // #computeScreenConfiguration() later.
1087 updateDisplayAndOrientation(getConfiguration().uiMode);
1088
1089 if (!inTransaction) {
1090 if (SHOW_TRANSACTIONS) {
1091 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1092 }
1093 mService.openSurfaceTransaction();
1094 }
1095 try {
1096 // NOTE: We disable the rotation in the emulator because
1097 // it doesn't support hardware OpenGL emulation yet.
1098 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1099 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001100 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001101 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1102 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1103 mService.scheduleAnimationLocked();
1104 }
1105 }
1106
1107 if (rotateSeamlessly) {
1108 forAllWindows(w -> {
1109 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1110 }, true /* traverseTopToBottom */);
1111 }
1112
1113 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1114 } finally {
1115 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001116 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001117 if (SHOW_LIGHT_TRANSACTIONS) {
1118 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1119 }
1120 }
1121 }
1122
1123 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001124 if (w.mHasSurface && !rotateSeamlessly) {
1125 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001126 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001127 mService.mRoot.mOrientationChangeComplete = false;
1128 w.mLastFreezeDuration = 0;
1129 }
1130 w.mReportOrientationChanged = true;
1131 }, true /* traverseTopToBottom */);
1132
1133 if (rotateSeamlessly) {
1134 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1135 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1136 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1137 }
1138
1139 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1140 final WindowManagerService.RotationWatcher rotationWatcher
1141 = mService.mRotationWatchers.get(i);
1142 if (rotationWatcher.mDisplayId == mDisplayId) {
1143 try {
1144 rotationWatcher.mWatcher.onRotationChanged(rotation);
1145 } catch (RemoteException e) {
1146 // Ignore
1147 }
1148 }
1149 }
1150
1151 // TODO (multi-display): Magnification is supported only for the default display.
1152 // Announce rotation only if we will not animate as we already have the
1153 // windows in final state. Otherwise, we make this call at the rotation end.
1154 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1155 && isDefaultDisplay) {
1156 mService.mAccessibilityController.onRotationChangedLocked(this);
1157 }
1158
1159 return true;
1160 }
1161
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001162 void configureDisplayPolicy() {
1163 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1164 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1165
1166 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1167 }
1168
Andrii Kulian06d07d62017-03-14 11:11:47 -07001169 /**
1170 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1171 * changed.
1172 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1173 */
1174 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1175 // Use the effective "visual" dimensions based on current rotation
1176 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1177 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1178 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1179 int dw = realdw;
1180 int dh = realdh;
1181
1182 if (mAltOrientation) {
1183 if (realdw > realdh) {
1184 // Turn landscape into portrait.
1185 int maxw = (int)(realdh/1.3f);
1186 if (maxw < realdw) {
1187 dw = maxw;
1188 }
1189 } else {
1190 // Turn portrait into landscape.
1191 int maxh = (int)(realdw/1.3f);
1192 if (maxh < realdh) {
1193 dh = maxh;
1194 }
1195 }
1196 }
1197
1198 // Update application display metrics.
1199 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1200 mDisplayId);
1201 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1202 mDisplayId);
1203 mDisplayInfo.rotation = mRotation;
1204 mDisplayInfo.logicalWidth = dw;
1205 mDisplayInfo.logicalHeight = dh;
1206 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1207 mDisplayInfo.appWidth = appWidth;
1208 mDisplayInfo.appHeight = appHeight;
1209 if (isDefaultDisplay) {
1210 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1211 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1212 }
1213 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1214 if (mDisplayScalingDisabled) {
1215 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1216 } else {
1217 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1218 }
1219
1220 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1221 mDisplayInfo);
1222
1223 mBaseDisplayRect.set(0, 0, dw, dh);
1224
1225 if (isDefaultDisplay) {
1226 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1227 mCompatDisplayMetrics);
1228 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001229
1230 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001231 return mDisplayInfo;
1232 }
1233
1234 /**
1235 * Compute display configuration based on display properties and policy settings.
1236 * Do not call if mDisplayReady == false.
1237 */
1238 void computeScreenConfiguration(Configuration config) {
1239 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1240
1241 final int dw = displayInfo.logicalWidth;
1242 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001243 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1244 // TODO: Probably best to set this based on some setting in the display content object,
1245 // so the display can be configured for things like fullscreen.
1246 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001247
Andrii Kulian06d07d62017-03-14 11:11:47 -07001248 config.screenWidthDp =
1249 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1250 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1251 config.screenHeightDp =
1252 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1253 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001254
1255 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1256 final int leftInset = mTmpRect.left;
1257 final int topInset = mTmpRect.top;
1258 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001259 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1260 leftInset + displayInfo.appWidth /* right */,
1261 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001262 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1263 || displayInfo.rotation == Surface.ROTATION_270);
1264
1265 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1266 mDisplayMetrics.density, config);
1267
1268 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1269 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1270 ? Configuration.SCREENLAYOUT_ROUND_YES
1271 : Configuration.SCREENLAYOUT_ROUND_NO);
1272
1273 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1274 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1275 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1276 dh, mDisplayId);
1277 config.densityDpi = displayInfo.logicalDensityDpi;
1278
1279 config.colorMode =
1280 (displayInfo.isHdr()
1281 ? Configuration.COLOR_MODE_HDR_YES
1282 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001283 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001284 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1285 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1286
1287 // Update the configuration based on available input devices, lid switch,
1288 // and platform configuration.
1289 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1290 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1291 config.navigation = Configuration.NAVIGATION_NONAV;
1292
1293 int keyboardPresence = 0;
1294 int navigationPresence = 0;
1295 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1296 final int len = devices != null ? devices.length : 0;
1297 for (int i = 0; i < len; i++) {
1298 InputDevice device = devices[i];
1299 if (!device.isVirtual()) {
1300 final int sources = device.getSources();
1301 final int presenceFlag = device.isExternal() ?
1302 WindowManagerPolicy.PRESENCE_EXTERNAL :
1303 WindowManagerPolicy.PRESENCE_INTERNAL;
1304
1305 // TODO(multi-display): Configure on per-display basis.
1306 if (mService.mIsTouchDevice) {
1307 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1308 InputDevice.SOURCE_TOUCHSCREEN) {
1309 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1310 }
1311 } else {
1312 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1313 }
1314
1315 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1316 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1317 navigationPresence |= presenceFlag;
1318 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1319 && config.navigation == Configuration.NAVIGATION_NONAV) {
1320 config.navigation = Configuration.NAVIGATION_DPAD;
1321 navigationPresence |= presenceFlag;
1322 }
1323
1324 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1325 config.keyboard = Configuration.KEYBOARD_QWERTY;
1326 keyboardPresence |= presenceFlag;
1327 }
1328 }
1329 }
1330
1331 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1332 config.navigation = Configuration.NAVIGATION_DPAD;
1333 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1334 }
1335
1336 // Determine whether a hard keyboard is available and enabled.
1337 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1338 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1339 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1340 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1341 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1342 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1343 }
1344
1345 // Let the policy update hidden states.
1346 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1347 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1348 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1349 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1350 }
1351
1352 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1353 int displayId) {
1354 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1355 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1356 final int unrotDw, unrotDh;
1357 if (rotated) {
1358 unrotDw = dh;
1359 unrotDh = dw;
1360 } else {
1361 unrotDw = dw;
1362 unrotDh = dh;
1363 }
1364 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1365 displayId);
1366 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1367 displayId);
1368 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1369 displayId);
1370 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1371 displayId);
1372 return sw;
1373 }
1374
1375 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1376 DisplayMetrics dm, int dw, int dh, int displayId) {
1377 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1378 displayId);
1379 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1380 uiMode, displayId);
1381 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1382 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1383 if (curSize == 0 || size < curSize) {
1384 curSize = size;
1385 }
1386 return curSize;
1387 }
1388
1389 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1390 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1391
1392 // We need to determine the smallest width that will occur under normal
1393 // operation. To this, start with the base screen size and compute the
1394 // width under the different possible rotations. We need to un-rotate
1395 // the current screen dimensions before doing this.
1396 int unrotDw, unrotDh;
1397 if (rotated) {
1398 unrotDw = dh;
1399 unrotDh = dw;
1400 } else {
1401 unrotDw = dw;
1402 unrotDh = dh;
1403 }
1404 displayInfo.smallestNominalAppWidth = 1<<30;
1405 displayInfo.smallestNominalAppHeight = 1<<30;
1406 displayInfo.largestNominalAppWidth = 0;
1407 displayInfo.largestNominalAppHeight = 0;
1408 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1409 unrotDh);
1410 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1411 unrotDw);
1412 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1413 unrotDh);
1414 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1415 unrotDw);
1416 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1417 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1418 displayId);
1419 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1420 displayId);
1421 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1422 displayId);
1423 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1424 displayId);
1425 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1426 outConfig.screenLayout = sl;
1427 }
1428
1429 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1430 int uiMode, int displayId) {
1431 // Get the app screen size at this rotation.
1432 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1433 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1434
1435 // Compute the screen layout size class for this rotation.
1436 int longSize = w;
1437 int shortSize = h;
1438 if (longSize < shortSize) {
1439 int tmp = longSize;
1440 longSize = shortSize;
1441 shortSize = tmp;
1442 }
1443 longSize = (int)(longSize/density);
1444 shortSize = (int)(shortSize/density);
1445 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1446 }
1447
1448 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1449 int uiMode, int dw, int dh) {
1450 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1451 displayId);
1452 if (width < displayInfo.smallestNominalAppWidth) {
1453 displayInfo.smallestNominalAppWidth = width;
1454 }
1455 if (width > displayInfo.largestNominalAppWidth) {
1456 displayInfo.largestNominalAppWidth = width;
1457 }
1458 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1459 displayId);
1460 if (height < displayInfo.smallestNominalAppHeight) {
1461 displayInfo.smallestNominalAppHeight = height;
1462 }
1463 if (height > displayInfo.largestNominalAppHeight) {
1464 displayInfo.largestNominalAppHeight = height;
1465 }
1466 }
1467
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001468 DockedStackDividerController getDockedDividerController() {
1469 return mDividerControllerLocked;
1470 }
1471
Winson Chung655332c2016-10-31 13:14:28 -07001472 PinnedStackController getPinnedStackController() {
1473 return mPinnedStackControllerLocked;
1474 }
1475
Jeff Browna506a6e2013-06-04 00:02:38 -07001476 /**
1477 * Returns true if the specified UID has access to this display.
1478 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001479 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001480 return mDisplay.hasAccess(uid);
1481 }
1482
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001483 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001484 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001485 }
1486
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001487 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001488 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001489 }
1490
Wale Ogunwale61911492017-10-11 08:50:50 -07001491 /**
1492 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1493 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001494 TaskStack getSplitScreenPrimaryStack() {
1495 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001496 return (stack != null && stack.isVisible()) ? stack : null;
1497 }
1498
1499 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001500 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001501 * not visible.
1502 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001503 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1504 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001505 }
1506
1507 TaskStack getPinnedStack() {
1508 return mTaskStackContainers.getPinnedStack();
1509 }
1510
1511 private boolean hasPinnedStack() {
1512 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001513 }
1514
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001515 /**
1516 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1517 * Null is no compatible stack on the display.
1518 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001519 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001520 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1521 }
1522
1523 /**
1524 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1525 * activity type. Null is no compatible stack on the display.
1526 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001527 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001528 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001529 }
1530
Bryce Lee48f4b572017-04-10 10:54:15 -07001531 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001532 TaskStack getTopStack() {
1533 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001534 }
1535
Wale Ogunwale61911492017-10-11 08:50:50 -07001536 void onStackWindowingModeChanged(TaskStack stack) {
1537 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001538 }
1539
Andrii Kulian441e4492016-09-29 15:25:00 -07001540 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001541 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001542 super.onConfigurationChanged(newParentConfig);
1543
Andrii Kulian3a507b52016-09-19 18:14:12 -07001544 // The display size information is heavily dependent on the resources in the current
1545 // configuration, so we need to reconfigure it every time the configuration changes.
1546 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1547 mService.reconfigureDisplayLocked(this);
1548
Bryce Leef3c6a472017-11-14 14:53:06 -08001549 final DockedStackDividerController dividerController = getDockedDividerController();
1550
1551 if (dividerController != null) {
1552 getDockedDividerController().onConfigurationChanged();
1553 }
1554
1555 final PinnedStackController pinnedStackController = getPinnedStackController();
1556
1557 if (pinnedStackController != null) {
1558 getPinnedStackController().onConfigurationChanged();
1559 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001560 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001561
Andrii Kulian441e4492016-09-29 15:25:00 -07001562 /**
1563 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1564 * bounds were updated.
1565 */
1566 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001567 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1568 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001569 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001570 changedStackList.add(stack.mStackId);
1571 }
1572 }
Winson Chung32c566f2017-04-11 18:31:21 -07001573
1574 // If there was no pinned stack, we still need to notify the controller of the display info
1575 // update as a result of the config change. We do this here to consolidate the flow between
1576 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001577 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001578 mPinnedStackControllerLocked.onDisplayInfoChanged();
1579 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001580 }
1581
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001582 @Override
1583 boolean fillsParent() {
1584 return true;
1585 }
1586
1587 @Override
1588 boolean isVisible() {
1589 return true;
1590 }
1591
1592 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001593 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001594 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001595 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001596 }
1597
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001598 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001599 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1600 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1601 // target, or here in order if there isn't an IME target.
1602 if (traverseTopToBottom) {
1603 for (int i = mChildren.size() - 1; i >= 0; --i) {
1604 final DisplayChildWindowContainer child = mChildren.get(i);
1605 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1606 // In this case the Ime windows will be processed above their target so we skip
1607 // here.
1608 continue;
1609 }
1610 if (child.forAllWindows(callback, traverseTopToBottom)) {
1611 return true;
1612 }
1613 }
1614 } else {
1615 final int count = mChildren.size();
1616 for (int i = 0; i < count; i++) {
1617 final DisplayChildWindowContainer child = mChildren.get(i);
1618 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1619 // In this case the Ime windows will be processed above their target so we skip
1620 // here.
1621 continue;
1622 }
1623 if (child.forAllWindows(callback, traverseTopToBottom)) {
1624 return true;
1625 }
1626 }
1627 }
1628 return false;
1629 }
1630
1631 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1632 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1633 }
1634
Wale Ogunwale399c8692017-05-08 14:22:42 -07001635 @Override
1636 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001637 final WindowManagerPolicy policy = mService.mPolicy;
1638
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001639 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001640 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001641 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001642 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001643 // If the display is frozen, some activities may be in the middle of restarting, and
1644 // thus have removed their old window. If the window has the flag to hide the lock
1645 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1646 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001647 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001648 } else if (policy.isKeyguardLocked()) {
1649 // Use the last orientation the while the display is frozen with the keyguard
1650 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1651 // window. We don't want to check the show when locked window directly though as
1652 // things aren't stable while the display is frozen, for example the window could be
1653 // momentarily unavailable due to activity relaunch.
1654 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001655 + "return " + mLastOrientation);
1656 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001657 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001658 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001659 final int orientation = mAboveAppWindowsContainers.getOrientation();
1660 if (orientation != SCREEN_ORIENTATION_UNSET) {
1661 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001662 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001663 }
1664
Wale Ogunwale399c8692017-05-08 14:22:42 -07001665 // Top system windows are not requesting an orientation. Start searching from apps.
1666 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001667 }
1668
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001669 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001670 // Check if display metrics changed and update base values if needed.
1671 updateBaseDisplayMetricsIfNeeded();
1672
Craig Mautner722285e2012-09-07 13:55:58 -07001673 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001674 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001675
Wale Ogunwale61911492017-10-11 08:50:50 -07001676 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1677 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001678 }
Craig Mautner722285e2012-09-07 13:55:58 -07001679 }
1680
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001681 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001682 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1683 if (displayManagerInternal != null) {
1684 // Bootstrap the default logical display from the display manager.
1685 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1686 if (newDisplayInfo != null) {
1687 mDisplayInfo.copyFrom(newDisplayInfo);
1688 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001689 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001690
Andrii Kuliancd097992017-03-23 18:31:59 -07001691 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1692 mDisplayInfo.logicalDensityDpi);
1693 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1694 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1695 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001696 }
1697
Andrii Kuliancd097992017-03-23 18:31:59 -07001698 /**
1699 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1700 * values.
1701 */
1702 private void updateBaseDisplayMetricsIfNeeded() {
1703 // Get real display metrics without overrides from WM.
1704 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1705 final int orientation = mDisplayInfo.rotation;
1706 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1707 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1708 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1709 final int newDensity = mDisplayInfo.logicalDensityDpi;
1710
1711 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1712 || mInitialDisplayHeight != newHeight
1713 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1714
1715 if (displayMetricsChanged) {
1716 // Check if display size or density is forced.
1717 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1718 || mBaseDisplayHeight != mInitialDisplayHeight;
1719 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1720
1721 // If there is an override set for base values - use it, otherwise use new values.
1722 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1723 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1724 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1725
1726 // Real display metrics changed, so we should also update initial values.
1727 mInitialDisplayWidth = newWidth;
1728 mInitialDisplayHeight = newHeight;
1729 mInitialDisplayDensity = newDensity;
1730 mService.reconfigureDisplayLocked(this);
1731 }
1732 }
1733
Bryce Lee27cec322017-03-21 09:41:37 -07001734 /** Sets the maximum width the screen resolution can be */
1735 void setMaxUiWidth(int width) {
1736 if (DEBUG_DISPLAY) {
1737 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1738 }
1739
1740 mMaxUiWidth = width;
1741
1742 // Update existing metrics.
1743 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1744 }
1745
1746 /** Update base (override) display metrics. */
1747 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1748 mBaseDisplayWidth = baseWidth;
1749 mBaseDisplayHeight = baseHeight;
1750 mBaseDisplayDensity = baseDensity;
1751
1752 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1753 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1754 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1755 mBaseDisplayWidth = mMaxUiWidth;
1756
1757 if (DEBUG_DISPLAY) {
1758 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1759 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1760 + " on display:" + getDisplayId());
1761 }
1762 }
1763
1764 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001765
1766 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001767 }
1768
Chong Zhangf66db432016-01-13 10:39:51 -08001769 void getContentRect(Rect out) {
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001770 out.set(mDisplayFrames.mContent);
Chong Zhangf66db432016-01-13 10:39:51 -08001771 }
1772
Wale Ogunwale61911492017-10-11 08:50:50 -07001773 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001774 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1775 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001776
Wale Ogunwale61911492017-10-11 08:50:50 -07001777 final TaskStack stack = new TaskStack(mService, stackId, controller);
1778 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001779 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001780 }
1781
Andrii Kulian51c1b672017-04-07 18:39:32 -07001782 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001783 final DisplayContent prevDc = stack.getDisplayContent();
1784 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001785 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1786 + " which is not currently attached to any display");
1787 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001788 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001789 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1790 + " to its current displayId=" + mDisplayId);
1791 }
1792
Wale Ogunwale61911492017-10-11 08:50:50 -07001793 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001794 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001795 }
1796
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001797 @Override
1798 protected void addChild(DisplayChildWindowContainer child,
1799 Comparator<DisplayChildWindowContainer> comparator) {
1800 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1801 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001802
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001803 @Override
1804 protected void addChild(DisplayChildWindowContainer child, int index) {
1805 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1806 }
1807
1808 @Override
1809 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001810 // Only allow removal of direct children from this display if the display is in the process
1811 // of been removed.
1812 if (mRemovingDisplay) {
1813 super.removeChild(child);
1814 return;
1815 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001816 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001817 }
1818
Andrii Kuliand2765632016-12-12 22:26:34 -08001819 @Override
1820 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1821 // Children of the display are statically ordered, so the real intention here is to perform
1822 // the operation on the display and not the static direct children.
1823 getParent().positionChildAt(position, this, includingParents);
1824 }
1825
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001826 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001827 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1828 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001829 final int taskId = stack.taskIdFromPoint(x, y);
1830 if (taskId != -1) {
1831 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001832 }
1833 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001834 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001835 }
1836
Chong Zhang8e89b312015-09-09 15:09:30 -07001837 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001838 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001839 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001840 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001841 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001842 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001843 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001844 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1845 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001846 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001847 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001848 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001849
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001850 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1851 if (mTmpTaskForResizePointSearchResult.searchDone) {
1852 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001853 }
1854 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001855 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001856 }
1857
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001858 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001859 // The provided task is the task on this display with focus, so if WindowManagerService's
1860 // focused app is not on this display, focusedTask will be null.
1861 if (focusedTask == null) {
1862 mTouchExcludeRegion.setEmpty();
1863 } else {
1864 mTouchExcludeRegion.set(mBaseDisplayRect);
1865 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1866 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001867 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1868 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001869 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1870 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001871 }
1872 // If we removed the focused task above, add it back and only leave its
1873 // outside touch area in the exclusion. TapDectector is not interested in
1874 // any touch inside the focused task itself.
1875 if (!mTmpRect2.isEmpty()) {
1876 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1877 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001878 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001879 final WindowState inputMethod = mService.mInputMethodWindow;
1880 if (inputMethod != null && inputMethod.isVisibleLw()) {
1881 // If the input method is visible and the user is typing, we don't want these touch
1882 // events to be intercepted and used to change focus. This would likely cause a
1883 // disappearance of the input method.
1884 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001885 if (inputMethod.getDisplayId() == mDisplayId) {
1886 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1887 } else {
1888 // IME is on a different display, so we need to update its tap detector.
1889 // TODO(multidisplay): Remove when IME will always appear on same display.
1890 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1891 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001892 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001893 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1894 WindowState win = mTapExcludedWindows.get(i);
1895 win.getTouchableRegion(mTmpRegion);
1896 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1897 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001898 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001899 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001900 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001901 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001902 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1903 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001904 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001905 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001906 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001907 }
1908
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001909 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001910 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001911 super.switchUser();
1912 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001913 }
1914
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001915 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001916 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1917 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001918 }
1919 }
1920
Wale Ogunwale10124582016-09-15 20:25:50 -07001921 @Override
1922 void removeIfPossible() {
1923 if (isAnimating()) {
1924 mDeferredRemoval = true;
1925 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001926 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001927 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001928 }
1929
Wale Ogunwale10124582016-09-15 20:25:50 -07001930 @Override
1931 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001932 mRemovingDisplay = true;
1933 try {
1934 super.removeImmediately();
1935 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001936 if (mService.canDispatchPointerEvents()) {
1937 if (mTapDetector != null) {
1938 mService.unregisterPointerEventListener(mTapDetector);
1939 }
1940 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1941 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1942 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001943 }
Robert Carrb1579c82017-09-05 14:54:47 -07001944 // The pending transaction won't be applied so we should
1945 // just clean up any surfaces pending destruction.
1946 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001947 } finally {
1948 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001949 }
Craig Mautner95da1082014-02-24 17:54:35 -08001950 }
1951
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001952 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001953 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001954 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001955 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1956
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001957 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001958 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001959 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001960 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001961 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001962 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001963 }
1964
Andrii Kulian0214ed92017-05-16 13:44:05 -07001965 /** @return 'true' if removal of this display content is deferred due to active animation. */
1966 boolean isRemovalDeferred() {
1967 return mDeferredRemoval;
1968 }
1969
Wale Ogunwale10124582016-09-15 20:25:50 -07001970 boolean animateForIme(float interpolatedValue, float animationTarget,
1971 float dividerAnimationTarget) {
1972 boolean updated = false;
1973
Wale Ogunwale61911492017-10-11 08:50:50 -07001974 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1975 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001976 if (stack == null || !stack.isAdjustedForIme()) {
1977 continue;
1978 }
1979
1980 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1981 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1982 updated = true;
1983 } else {
1984 mDividerControllerLocked.mLastAnimationProgress =
1985 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1986 mDividerControllerLocked.mLastDividerProgress =
1987 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1988 updated |= stack.updateAdjustForIme(
1989 mDividerControllerLocked.mLastAnimationProgress,
1990 mDividerControllerLocked.mLastDividerProgress,
1991 false /* force */);
1992 }
1993 if (interpolatedValue >= 1f) {
1994 stack.endImeAdjustAnimation();
1995 }
1996 }
1997
1998 return updated;
1999 }
2000
2001 boolean clearImeAdjustAnimation() {
2002 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002003 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2004 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002005 if (stack != null && stack.isAdjustedForIme()) {
2006 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2007 changed = true;
2008 }
2009 }
2010 return changed;
2011 }
2012
2013 void beginImeAdjustAnimation() {
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.isVisible() && stack.isAdjustedForIme()) {
2017 stack.beginImeAdjustAnimation();
2018 }
2019 }
2020 }
2021
2022 void adjustForImeIfNeeded() {
2023 final WindowState imeWin = mService.mInputMethodWindow;
2024 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2025 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002026 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002027 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2028 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2029 imeTargetStack.getDockSide() : DOCKED_INVALID;
2030 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2031 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2032 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002033 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002034 final boolean imeHeightChanged = imeVisible &&
2035 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2036
2037 // The divider could be adjusted for IME position, or be thinner than usual,
2038 // or both. There are three possible cases:
2039 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2040 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2041 // - If IME is not visible, divider is not moved and is normal width.
2042
2043 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002044 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2045 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002046 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002047 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2048 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002049 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2050 } else {
2051 stack.resetAdjustedForIme(false);
2052 }
2053 }
2054 mDividerControllerLocked.setAdjustedForIme(
2055 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2056 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002057 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2058 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002059 stack.resetAdjustedForIme(!dockVisible);
2060 }
2061 mDividerControllerLocked.setAdjustedForIme(
2062 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2063 }
Winson Chung655332c2016-10-31 13:14:28 -07002064 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002065 }
2066
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002067 void setInputMethodAnimLayerAdjustment(int adj) {
2068 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2069 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002070 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002071 }
2072
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002073 /**
2074 * If a window that has an animation specifying a colored background and the current wallpaper
2075 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2076 * suddenly disappear.
2077 */
2078 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002079 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2080 w -> w.mIsWallpaper && w.isVisibleNow());
2081
2082 if (visibleWallpaper != null) {
2083 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002084 }
2085 return winAnimator.mAnimLayer;
2086 }
2087
Wale Ogunwale10124582016-09-15 20:25:50 -07002088 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002089 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2090 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002091 stack.prepareFreezingTaskBounds();
2092 }
2093 }
2094
Wale Ogunwale94744212015-09-21 19:01:47 -07002095 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002096 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002097
2098 // Compute a transform matrix to undo the coordinate space transformation,
2099 // and present the window at the same physical position it previously occupied.
2100 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2101 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2102
2103 mTmpRectF.set(bounds);
2104 mTmpMatrix.mapRect(mTmpRectF);
2105 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002106 }
2107
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002108 static int deltaRotation(int oldRotation, int newRotation) {
2109 int delta = newRotation - oldRotation;
2110 if (delta < 0) delta += 4;
2111 return delta;
2112 }
2113
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002114 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002115 Matrix outMatrix) {
2116 // For rotations without Z-ordering we don't need the target rectangle's position.
2117 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2118 displayHeight, outMatrix);
2119 }
2120
2121 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2122 float displayWidth, float displayHeight, Matrix outMatrix) {
2123 switch (rotation) {
2124 case ROTATION_0:
2125 outMatrix.reset();
2126 break;
2127 case ROTATION_270:
2128 outMatrix.setRotate(270, 0, 0);
2129 outMatrix.postTranslate(0, displayHeight);
2130 outMatrix.postTranslate(rectTop, 0);
2131 break;
2132 case ROTATION_180:
2133 outMatrix.reset();
2134 break;
2135 case ROTATION_90:
2136 outMatrix.setRotate(90, 0, 0);
2137 outMatrix.postTranslate(displayWidth, 0);
2138 outMatrix.postTranslate(-rectTop, rectLeft);
2139 break;
2140 }
2141 }
2142
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002143 @CallSuper
2144 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002145 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002146 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002147 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002148 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002149 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2150 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002151 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002152 }
2153 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2154 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002155 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2156 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002157 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002158 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002159 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2160 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002161 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002162 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002163 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2164 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002165 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002166 }
2167 proto.write(DPI, mBaseDisplayDensity);
2168 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002169 proto.write(ROTATION, mRotation);
2170 final ScreenRotationAnimation screenRotationAnimation =
2171 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2172 if (screenRotationAnimation != null) {
2173 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2174 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002175 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002176 proto.end(token);
2177 }
2178
Craig Mautnera91f9e22012-09-14 16:22:08 -07002179 public void dump(String prefix, PrintWriter pw) {
2180 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2181 final String subPrefix = " " + prefix;
2182 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2183 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2184 pw.print("dpi");
2185 if (mInitialDisplayWidth != mBaseDisplayWidth
2186 || mInitialDisplayHeight != mBaseDisplayHeight
2187 || mInitialDisplayDensity != mBaseDisplayDensity) {
2188 pw.print(" base=");
2189 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2190 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2191 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002192 if (mDisplayScalingDisabled) {
2193 pw.println(" noscale");
2194 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002195 pw.print(" cur=");
2196 pw.print(mDisplayInfo.logicalWidth);
2197 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2198 pw.print(" app=");
2199 pw.print(mDisplayInfo.appWidth);
2200 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2201 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2202 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2203 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2204 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002205 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002206 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002207 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002208
Craig Mautnerdc548482014-02-05 13:35:24 -08002209 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002210 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002211 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2212 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002213 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002214 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002215
Craig Mautnerdc548482014-02-05 13:35:24 -08002216 pw.println();
2217 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002218 pw.println();
2219 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002220 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002221 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002222 pw.print(" Exiting #"); pw.print(i);
2223 pw.print(' '); pw.print(token);
2224 pw.println(':');
2225 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002226 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002227 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002228
Jorim Jaggi31f71702016-05-04 16:43:04 -07002229 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002230
2231 // Dump stack references
2232 final TaskStack homeStack = getHomeStack();
2233 if (homeStack != null) {
2234 pw.println(prefix + "homeStack=" + homeStack.getName());
2235 }
2236 final TaskStack pinnedStack = getPinnedStack();
2237 if (pinnedStack != null) {
2238 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2239 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002240 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002241 if (splitScreenPrimaryStack != null) {
2242 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2243 }
2244
2245 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002246 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002247 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002248 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002249
2250 if (mInputMethodAnimLayerAdjustment != 0) {
2251 pw.println(subPrefix
2252 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2253 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002254
2255 pw.println();
2256 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002257 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002258
2259 @Override
2260 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002261 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002262 }
2263
2264 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002265 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002266 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002267
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002268 /** Returns true if the stack in the windowing mode is visible. */
2269 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002270 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002271 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002272 }
2273
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002274 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002275 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002276 final int x = (int) xf;
2277 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002278 final WindowState touchedWin = getWindow(w -> {
2279 final int flags = w.mAttrs.flags;
2280 if (!w.isVisibleLw()) {
2281 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002282 }
2283 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002284 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002285 }
2286
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002287 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002288 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002289 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002290 }
2291
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002292 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002293
2294 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002295 return mTmpRegion.contains(x, y) || touchFlags == 0;
2296 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002297
2298 return touchedWin;
2299 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002300
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002301 boolean canAddToastWindowForUid(int uid) {
2302 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002303 // Also if the app is focused adding more than one toast at
2304 // a time for better backwards compatibility.
2305 final WindowState focusedWindowForUid = getWindow(w ->
2306 w.mOwnerUid == uid && w.isFocused());
2307 if (focusedWindowForUid != null) {
2308 return true;
2309 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002310 final WindowState win = getWindow(w ->
2311 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2312 && !w.mWindowRemovalAllowed);
2313 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002314 }
2315
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002316 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002317 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2318 return;
2319 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002320
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002321 // Used to communicate the old focus to the callback method.
2322 mTmpWindow = oldFocus;
2323
2324 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002325 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002326
2327 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002328 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002329
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002330 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002331
2332 if (mTmpWindow == null) {
2333 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2334 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002335 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002336 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002337 }
2338
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002339 /** Updates the layer assignment of windows on this display. */
2340 void assignWindowLayers(boolean setLayoutNeeded) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07002341 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002342 if (setLayoutNeeded) {
2343 setLayoutNeeded();
2344 }
Robert Carrb1579c82017-09-05 14:54:47 -07002345
Robert Carrf59b8dd2017-10-02 18:58:36 -07002346 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002347 // the application of this transaction until the animation pass triggers
2348 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2349 // the hiding and showing of surfaces.
2350 scheduleAnimation();
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002351 }
2352
Wale Ogunwale1666e312016-12-16 11:27:18 -08002353 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2354 // moving containers or resizing them. Need to investigate the best way to have it automatically
2355 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002356 void layoutAndAssignWindowLayersIfNeeded() {
2357 mService.mWindowsChanged = true;
2358 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002359
2360 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2361 false /*updateInputWindows*/)) {
2362 assignWindowLayers(false /* setLayoutNeeded */);
2363 }
2364
2365 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2366 mService.mWindowPlacerLocked.performSurfacePlacement();
2367 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2368 }
2369
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002370 /** Returns true if a leaked surface was destroyed */
2371 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002372 // Used to indicate that a surface was leaked.
2373 mTmpWindow = null;
2374 forAllWindows(w -> {
2375 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002376 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002377 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002378 }
2379 if (!mService.mSessions.contains(wsa.mSession)) {
2380 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002381 + w + " surface=" + wsa.mSurfaceController
2382 + " token=" + w.mToken
2383 + " pid=" + w.mSession.mPid
2384 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002385 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002386 mService.mForceRemoves.add(w);
2387 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002388 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002389 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002390 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002391 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002393 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002394 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002395 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002396 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002397
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002399 }
2400
2401 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002402 * Determine and return the window that should be the IME target.
2403 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2404 * @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 +00002405 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002406 WindowState computeImeTarget(boolean updateImeTarget) {
2407 if (mService.mInputMethodWindow == null) {
2408 // There isn't an IME so there shouldn't be a target...That was easy!
2409 if (updateImeTarget) {
2410 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2411 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2412 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2413 }
2414 return null;
2415 }
2416
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002417 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2418 // same display. Or even when the current IME/target are not on the same screen as the next
2419 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002420 mUpdateImeTarget = updateImeTarget;
2421 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002422
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002423
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002424 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2425 // to be on top of it, but it is not -really- where input will go. So look down below
2426 // for a real window to target...
2427 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2428 final AppWindowToken token = target.mAppToken;
2429 if (token != null) {
2430 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2431 if (betterTarget != null) {
2432 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002433 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002434 }
2435 }
2436
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002437 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2438 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002439
2440 // Now, a special case -- if the last target's window is in the process of exiting, and is
2441 // above the new target, keep on the last target to avoid flicker. Consider for example a
2442 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2443 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2444 // scrim.
2445 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002446 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2447 && (target == null
2448 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002449 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002450 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002451 }
2452
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002453 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2454 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002455
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002456 if (target == null) {
2457 if (updateImeTarget) {
2458 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2459 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2460 + Debug.getCallers(4) : ""));
2461 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2462 }
2463
2464 return null;
2465 }
2466
2467 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002468 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2469 if (token != null) {
2470
2471 // Now some fun for dealing with window animations that modify the Z order. We need
2472 // to look at all windows below the current target that are in this app, finding the
2473 // highest visible one in layering.
2474 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002475 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002476 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002477 }
2478
2479 if (highestTarget != null) {
2480 final AppTransition appTransition = mService.mAppTransition;
2481 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2482 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2483 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002484 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002485
2486 if (appTransition.isTransitionSet()) {
2487 // If we are currently setting up for an animation, hold everything until we
2488 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002489 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2490 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002491 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002492 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002493 // If the window we are currently targeting is involved with an animation,
2494 // and it is on top of the next target we will be over, then hold off on
2495 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002496 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2497 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002498 }
2499 }
2500 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002501
2502 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2503 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2504 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002505 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002506 }
2507
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002508 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002509 }
2510
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002511 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2512 if (target == mService.mInputMethodTarget
2513 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2514 && mInputMethodAnimLayerAdjustment == layerAdj) {
2515 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002516 }
2517
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002518 mService.mInputMethodTarget = target;
2519 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2520 setInputMethodAnimLayerAdjustment(layerAdj);
2521 assignWindowLayers(false /* setLayoutNeeded */);
2522 }
2523
2524 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2525 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2526 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2527 }
2528
2529 // Used to indicate we have reached the first window in the range we are interested in.
2530 mTmpWindow = null;
2531
2532 // TODO: Figure-out a more efficient way to do this.
2533 final WindowState candidate = getWindow(w -> {
2534 if (w == top) {
2535 // Reached the first window in the range we are interested in.
2536 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002537 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002538 if (mTmpWindow == null) {
2539 return false;
2540 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002541
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002542 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2543 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002544 }
2545 // If we reached the bottom of the range of windows we are considering,
2546 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002547 if (w == bottom) {
2548 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002549 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002550 return false;
2551 });
2552
2553 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002554 }
2555
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002556 void setLayoutNeeded() {
2557 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2558 mLayoutNeeded = true;
2559 }
2560
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002561 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002562 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2563 mLayoutNeeded = false;
2564 }
2565
2566 boolean isLayoutNeeded() {
2567 return mLayoutNeeded;
2568 }
2569
Wale Ogunwale02319a62016-09-26 15:21:22 -07002570 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2571 if (mTokenMap.isEmpty()) {
2572 return;
2573 }
2574 pw.println(" Display #" + mDisplayId);
2575 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2576 while (it.hasNext()) {
2577 final WindowToken token = it.next();
2578 pw.print(" ");
2579 pw.print(token);
2580 if (dumpAll) {
2581 pw.println(':');
2582 token.dump(pw, " ");
2583 } else {
2584 pw.println();
2585 }
2586 }
2587 }
2588
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002589 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002590 final int[] index = new int[1];
2591 forAllWindows(w -> {
2592 final WindowStateAnimator wAnim = w.mWinAnimator;
2593 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2594 index[0] = index[0] + 1;
2595 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002596 }
2597
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002598 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002599 forAllWindows(w -> {
2600 w.mWinAnimator.enableSurfaceTrace(fd);
2601 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002602 }
2603
2604 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002605 forAllWindows(w -> {
2606 w.mWinAnimator.disableSurfaceTrace();
2607 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002608 }
2609
Jorim Jaggife762342016-10-13 14:33:27 +02002610 /**
2611 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2612 */
2613 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2614 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002615 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002616 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2617 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002618 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002619 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2620 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002621 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002622 }
2623
Wale Ogunwale494009b82016-10-21 09:01:38 -07002624 boolean checkWaitingForWindows() {
2625
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002626 mHaveBootMsg = false;
2627 mHaveApp = false;
2628 mHaveWallpaper = false;
2629 mHaveKeyguard = true;
2630
2631 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002632 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2633 return true;
2634 }
2635 if (w.isDrawnLw()) {
2636 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002637 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002638 } else if (w.mAttrs.type == TYPE_APPLICATION
2639 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002640 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002641 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002643 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002644 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002645 }
2646 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002647 return false;
2648 });
2649
2650 if (visibleWindow != null) {
2651 // We have a visible window.
2652 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002653 }
2654
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002655 // if the wallpaper service is disabled on the device, we're never going to have
2656 // wallpaper, don't bother waiting for it
2657 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2658 com.android.internal.R.bool.config_enableWallpaperService)
2659 && !mService.mOnlyCore;
2660
Wale Ogunwale494009b82016-10-21 09:01:38 -07002661 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2662 "******** booted=" + mService.mSystemBooted
2663 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002664 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2665 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2666 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002667
2668 // If we are turning on the screen to show the boot message, don't do it until the boot
2669 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002670 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002671 return true;
2672 }
2673
2674 // If we are turning on the screen after the boot is completed normally, don't do so until
2675 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002676 if (mService.mSystemBooted
2677 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002678 return true;
2679 }
2680
2681 return false;
2682 }
2683
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002684 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002685 mTmpWindowAnimator = animator;
2686 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002687 }
2688
2689 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002690 resetAnimationBackgroundAnimator();
2691
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002692 // Used to indicate a detached wallpaper.
2693 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002694 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002695
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002696 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002697
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002698 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002699 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002700 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2701 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002702 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2703 }
2704 }
2705
Wale Ogunwale494009b82016-10-21 09:01:38 -07002706 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002707 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002708 if (imFocus == null) {
2709 return false;
2710 }
2711
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002712 if (DEBUG_INPUT_METHOD) {
2713 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2714 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2715 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002716 }
2717
Wale Ogunwale494009b82016-10-21 09:01:38 -07002718 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2719
2720 if (DEBUG_INPUT_METHOD) {
2721 Slog.i(TAG_WM, "IM target client: " + imeClient);
2722 if (imeClient != null) {
2723 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2724 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2725 }
2726 }
2727
2728 return imeClient != null && imeClient.asBinder() == client.asBinder();
2729 }
2730
2731 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002732 final WindowState win = getWindow(
2733 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2734 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002735 }
2736
2737 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002738 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002739 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002740 final int curValue = w.mSystemUiVisibility;
2741 final int diff = (curValue ^ visibility) & globalDiff;
2742 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002743 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002744 w.mSeq++;
2745 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002746 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002747 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2748 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002749 visibility, newValue, diff);
2750 }
2751 } catch (RemoteException e) {
2752 // so sorry
2753 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002754 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002755 }
2756
2757 void onWindowFreezeTimeout() {
2758 Slog.w(TAG_WM, "Window freeze timeout expired.");
2759 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002760
2761 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002762 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002763 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002764 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002765 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002766 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2767 - mService.mDisplayFreezeTime);
2768 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002769 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002770 mService.mWindowPlacerLocked.performSurfacePlacement();
2771 }
2772
2773 void waitForAllWindowsDrawn() {
2774 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002775 forAllWindows(w -> {
2776 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2777 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2778 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002779 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002780 w.mLastContentInsets.set(-1, -1, -1, -1);
2781 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002782 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002783 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002784 }
2785
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002786 // TODO: Super crazy long method that should be broken down...
2787 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2788
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002789 final int dw = mDisplayInfo.logicalWidth;
2790 final int dh = mDisplayInfo.logicalHeight;
2791 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2792
2793 mTmpUpdateAllDrawn.clear();
2794
2795 int repeats = 0;
2796 do {
2797 repeats++;
2798 if (repeats > 6) {
2799 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2800 clearLayoutNeeded();
2801 break;
2802 }
2803
2804 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2805 pendingLayoutChanges);
2806
Andrii Kulian839def92016-11-02 10:58:58 -07002807 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2808 // the wallpaper window jumping across displays.
2809 // Remove check for default display when there will be support for multiple wallpaper
2810 // targets (on different displays).
2811 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002812 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002813 }
2814
2815 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2816 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2817 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2818 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002819 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002820 }
2821 }
2822
2823 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2824 setLayoutNeeded();
2825 }
2826
2827 // FIRST LOOP: Perform a layout, if needed.
2828 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2829 performLayout(repeats == 1, false /* updateInputWindows */);
2830 } else {
2831 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2832 }
2833
2834 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2835 pendingLayoutChanges = 0;
2836
2837 if (isDefaultDisplay) {
2838 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002839 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002840 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2841 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2842 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2843 }
2844 } while (pendingLayoutChanges != 0);
2845
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002846 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002847
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002848 mTmpRecoveringMemory = recoveringMemory;
2849 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002850
2851 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002852 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2853 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2854 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002855 true /* inTraversal, must call performTraversalInTrans... below */);
2856
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002857 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2858 if (wallpaperVisible != mLastWallpaperVisible) {
2859 mLastWallpaperVisible = wallpaperVisible;
2860 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2861 }
2862
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002863 while (!mTmpUpdateAllDrawn.isEmpty()) {
2864 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2865 // See if any windows have been drawn, so they (and others associated with them)
2866 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002867 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002868 }
2869
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002870 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002871 }
2872
Bryce Leef3c6a472017-11-14 14:53:06 -08002873 private void updateBounds() {
2874 calculateBounds(mTmpBounds);
2875 setBounds(mTmpBounds);
2876 }
2877
2878 // Determines the current display bounds based on the current state
2879 private void calculateBounds(Rect out) {
2880 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2881 final int orientation = mDisplayInfo.rotation;
2882 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2883 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2884 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2885 int width = mDisplayInfo.logicalWidth;
2886 int left = (physWidth - width) / 2;
2887 int height = mDisplayInfo.logicalHeight;
2888 int top = (physHeight - height) / 2;
2889 out.set(left, top, left + width, top + height);
2890 }
2891
2892 @Override
2893 public void getBounds(Rect out) {
2894 calculateBounds(out);
2895 }
2896
2897 private void getBounds(Rect out, int orientation) {
2898 getBounds(out);
2899
2900 // Rotate the Rect if needed.
2901 final int currentRotation = mDisplayInfo.rotation;
2902 final int rotationDelta = deltaRotation(currentRotation, orientation);
2903 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2904 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2905 mTmpRectF.set(out);
2906 mTmpMatrix.mapRect(mTmpRectF);
2907 mTmpRectF.round(out);
2908 }
2909 }
2910
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002911 void performLayout(boolean initial, boolean updateInputWindows) {
2912 if (!isLayoutNeeded()) {
2913 return;
2914 }
2915 clearLayoutNeeded();
2916
2917 final int dw = mDisplayInfo.logicalWidth;
2918 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002919 if (DEBUG_LAYOUT) {
2920 Slog.v(TAG, "-------------------------------------");
2921 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2922 }
2923
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002924 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2925 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2926 // display info above...
2927 mDisplayFrames.mRotation = mRotation;
2928 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002929 if (isDefaultDisplay) {
2930 // Not needed on non-default displays.
2931 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2932 mService.mScreenRect.set(0, 0, dw, dh);
2933 }
2934
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002935 int seq = mService.mLayoutSeq + 1;
2936 if (seq < 0) seq = 0;
2937 mService.mLayoutSeq = seq;
2938
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002939 // Used to indicate that we have processed the dream window and all additional windows are
2940 // behind it.
2941 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002942 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002943
2944 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002945 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002946
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002947 // Used to indicate that we have processed the dream window and all additional attached
2948 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002949 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002950 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951
2952 // Now perform layout of attached windows, which usually depend on the position of the
2953 // window they are attached to. XXX does not deal with windows that are attached to windows
2954 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002955 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002956
2957 // Window frames may have changed. Tell the input dispatcher about it.
2958 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2959 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2960 if (updateInputWindows) {
2961 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2962 }
2963
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002964 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2965 }
2966
2967 /**
2968 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2969 * In portrait mode, it grabs the full screenshot.
2970 *
2971 * @param width the width of the target bitmap
2972 * @param height the height of the target bitmap
2973 * @param includeFullDisplay true if the screen should not be cropped before capture
2974 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2975 * @param config of the output bitmap
2976 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002977 * @param includeDecor whether to include window decors, like the status or navigation bar
2978 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002979 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002980 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002981 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002982 boolean wallpaperOnly, boolean includeDecor) {
2983 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2984 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002985 if (bitmap == null) {
2986 return null;
2987 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002988
2989 if (DEBUG_SCREENSHOT) {
2990 // TEST IF IT's ALL BLACK
2991 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2992 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2993 bitmap.getHeight());
2994 boolean allBlack = true;
2995 final int firstColor = buffer[0];
2996 for (int i = 0; i < buffer.length; i++) {
2997 if (buffer[i] != firstColor) {
2998 allBlack = false;
2999 break;
3000 }
3001 }
3002 if (allBlack) {
3003 final WindowState appWin = mScreenshotApplicationState.appWin;
3004 final int maxLayer = mScreenshotApplicationState.maxLayer;
3005 final int minLayer = mScreenshotApplicationState.minLayer;
3006 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
3007 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
3008 (appWin != null ?
3009 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
3010 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
3011 }
3012 }
3013
3014 // Create a copy of the screenshot that is immutable and backed in ashmem.
3015 // This greatly reduces the overhead of passing the bitmap between processes.
3016 Bitmap ret = bitmap.createAshmemBitmap(config);
3017 bitmap.recycle();
3018 return ret;
3019 }
3020
3021 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
3022 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
3023 boolean includeDecor) {
3024 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
3025 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
3026 }
3027
3028 private <E> E screenshotApplications(IBinder appToken, int width, int height,
3029 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
3030 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003031 int dw = mDisplayInfo.logicalWidth;
3032 int dh = mDisplayInfo.logicalHeight;
3033 if (dw == 0 || dh == 0) {
3034 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3035 + ": returning null. logical widthxheight=" + dw + "x" + dh);
3036 return null;
3037 }
3038
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003039 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003040
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003041 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003042 final Rect frame = new Rect();
3043 final Rect stackBounds = new Rect();
3044
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003045 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003046 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08003047 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003048 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02003049 if (!mService.mPolicy.isScreenOn()) {
3050 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
3051 + " was off.");
3052 return null;
3053 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003054 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003055 mScreenshotApplicationState.appWin = null;
3056 forAllWindows(w -> {
3057 if (!w.mHasSurface) {
3058 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003059 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003060 if (w.mLayer >= aboveAppLayer) {
3061 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003062 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003063 if (wallpaperOnly && !w.mIsWallpaper) {
3064 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003065 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003066 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003067 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003068 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003069 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3070 // then the target window state is this one.
3071 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003072 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003073 }
3074
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003075 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003076 // We have not ran across the target window yet, so it is probably behind
3077 // the wallpaper. This can happen when the keyguard is up and all windows
3078 // are moved behind the wallpaper. We don't want to include the wallpaper
3079 // layer in the screenshot as it will cover-up the layer of the target
3080 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003081 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003082 }
3083 // Fall through. The target window is in front of the wallpaper. For this
3084 // case we want to include the wallpaper layer in the screenshot because
3085 // the target window might have some transparent areas.
3086 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003087 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003088 // This app window is of no interest if it is not associated with the
3089 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003090 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003091 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003092 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003093 }
3094
3095 // Include this window.
3096
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003097 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003098
3099 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003100 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3101 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003102 final Task task = w.getTask();
3103 if (task != null) {
3104 task.getBounds(frame);
3105 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003106
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003107 // No task bounds? Too bad! Ain't no screenshot then.
3108 return true;
3109 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003110 } else {
3111 final Rect wf = w.mFrame;
3112 final Rect cr = w.mContentInsets;
3113 int left = wf.left + cr.left;
3114 int top = wf.top + cr.top;
3115 int right = wf.right - cr.right;
3116 int bottom = wf.bottom - cr.bottom;
3117 frame.union(left, top, right, bottom);
3118 w.getVisibleBounds(stackBounds);
3119 if (!Rect.intersects(frame, stackBounds)) {
3120 // Set frame empty if there's no intersection.
3121 frame.setEmpty();
3122 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003123 }
3124 }
3125
3126 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003127 (w.mAppToken != null && w.mAppToken.token == appToken)
3128 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi406386a82017-09-15 14:17:40 +02003129 if (foundTargetWs && winAnim.getShown() && winAnim.mLastAlpha > 0f) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003130 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003131 }
3132
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003133 if (w.isObscuringDisplay()){
3134 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003135 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003136 return false;
3137 }, true /* traverseTopToBottom */);
3138
3139 final WindowState appWin = mScreenshotApplicationState.appWin;
3140 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003141
3142 if (appToken != null && appWin == null) {
3143 // Can't find a window to snapshot.
3144 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3145 "Screenshot: Couldn't find a surface matching " + appToken);
3146 return null;
3147 }
3148
3149 if (!screenshotReady) {
3150 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3151 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3152 appWin.mWinAnimator.mDrawState)));
3153 return null;
3154 }
3155
3156 // Screenshot is ready to be taken. Everything from here below will continue
3157 // through the bottom of the loop and return a value. We only stay in the loop
3158 // because we don't want to release the mWindowMap lock until the screenshot is
3159 // taken.
3160
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003161
Jorim Jaggi02886a82016-12-06 09:10:06 -08003162 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003163 // Constrain frame to the screen size.
3164 if (!frame.intersect(0, 0, dw, dh)) {
3165 frame.setEmpty();
3166 }
3167 } else {
3168 // Caller just wants entire display.
3169 frame.set(0, 0, dw, dh);
3170 }
3171 if (frame.isEmpty()) {
3172 return null;
3173 }
3174
3175 if (width < 0) {
3176 width = (int) (frame.width() * frameScale);
3177 }
3178 if (height < 0) {
3179 height = (int) (frame.height() * frameScale);
3180 }
3181
3182 // Tell surface flinger what part of the image to crop. Take the top
3183 // right part of the application, and crop the larger dimension to fit.
3184 Rect crop = new Rect(frame);
3185 if (width / (float) frame.width() < height / (float) frame.height()) {
3186 int cropWidth = (int)((float)width / (float)height * frame.height());
3187 crop.right = crop.left + cropWidth;
3188 } else {
3189 int cropHeight = (int)((float)height / (float)width * frame.width());
3190 crop.bottom = crop.top + cropHeight;
3191 }
3192
3193 // The screenshot API does not apply the current screen rotation.
3194 int rot = mDisplay.getRotation();
3195
3196 if (rot == ROTATION_90 || rot == ROTATION_270) {
3197 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3198 }
3199
3200 // Surfaceflinger is not aware of orientation, so convert our logical
3201 // crop to surfaceflinger's portrait orientation.
3202 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3203
3204 if (DEBUG_SCREENSHOT) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003205 forAllWindows(w -> {
3206 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3207 Slog.i(TAG_WM, w + ": " + w.mLayer
3208 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003209 + " surfaceLayer=" + ((controller == null)
3210 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003211 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003212 }
3213
3214 final ScreenRotationAnimation screenRotationAnimation =
3215 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3216 final boolean inRotation = screenRotationAnimation != null &&
3217 screenRotationAnimation.isAnimating();
3218 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3219 "Taking screenshot while rotating");
3220
3221 // We force pending transactions to flush before taking
3222 // the screenshot by pushing an empty synchronous transaction.
3223 SurfaceControl.openTransaction();
3224 SurfaceControl.closeTransactionSync();
3225
Robert Carrb1579c82017-09-05 14:54:47 -07003226 // TODO(b/68392460): We should screenshot Task controls directly
3227 // but it's difficult at the moment as the Task doesn't have the
3228 // correct size set.
3229 bitmap = screenshoter.screenshot(crop, width, height, 0, 1,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003230 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003231 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07003232 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003233 return null;
3234 }
3235 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003236 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003237 }
3238
3239 // TODO: Can this use createRotationMatrix()?
3240 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3241 if (rot == Surface.ROTATION_90) {
3242 final int tmp = crop.top;
3243 crop.top = dw - crop.right;
3244 crop.right = crop.bottom;
3245 crop.bottom = dw - crop.left;
3246 crop.left = tmp;
3247 } else if (rot == Surface.ROTATION_180) {
3248 int tmp = crop.top;
3249 crop.top = dh - crop.bottom;
3250 crop.bottom = dh - tmp;
3251 tmp = crop.right;
3252 crop.right = dw - crop.left;
3253 crop.left = dw - tmp;
3254 } else if (rot == Surface.ROTATION_270) {
3255 final int tmp = crop.top;
3256 crop.top = crop.left;
3257 crop.left = dh - crop.bottom;
3258 crop.bottom = crop.right;
3259 crop.right = dh - tmp;
3260 }
3261 }
3262
3263 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003264 // Used to indicate the layout is needed.
3265 mTmpWindow = null;
3266
3267 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003268 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003269 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003270 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003271 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003272 w.setDisplayLayoutNeeded();
3273 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003274 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003275
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003276 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003277 mService.mWindowPlacerLocked.performSurfacePlacement();
3278 }
3279 }
3280
Wale Ogunwale1666e312016-12-16 11:27:18 -08003281 void setExitingTokensHasVisible(boolean hasVisible) {
3282 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3283 mExitingTokens.get(i).hasVisible = hasVisible;
3284 }
3285
3286 // Initialize state of exiting applications.
3287 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3288 }
3289
3290 void removeExistingTokensIfPossible() {
3291 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3292 final WindowToken token = mExitingTokens.get(i);
3293 if (!token.hasVisible) {
3294 mExitingTokens.remove(i);
3295 }
3296 }
3297
3298 // Time to remove any exiting applications?
3299 mTaskStackContainers.removeExistingAppTokensIfPossible();
3300 }
3301
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003302 @Override
3303 void onDescendantOverrideConfigurationChanged() {
3304 setLayoutNeeded();
3305 mService.requestTraversal();
3306 }
3307
David Stevens9440dc82017-03-16 19:00:20 -07003308 boolean okToDisplay() {
3309 if (mDisplayId == DEFAULT_DISPLAY) {
3310 return !mService.mDisplayFrozen
3311 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3312 }
3313 return mDisplayInfo.state == Display.STATE_ON;
3314 }
3315
3316 boolean okToAnimate() {
3317 return okToDisplay() &&
3318 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3319 }
3320
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003321 static final class TaskForResizePointSearchResult {
3322 boolean searchDone;
3323 Task taskForResize;
3324
3325 void reset() {
3326 searchDone = false;
3327 taskForResize = null;
3328 }
3329 }
Robert Carr3b716242016-08-16 16:02:21 -07003330
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003331 private static final class ApplySurfaceChangesTransactionState {
3332 boolean displayHasContent;
3333 boolean obscured;
3334 boolean syswin;
3335 boolean focusDisplayed;
3336 float preferredRefreshRate;
3337 int preferredModeId;
3338
3339 void reset() {
3340 displayHasContent = false;
3341 obscured = false;
3342 syswin = false;
3343 focusDisplayed = false;
3344 preferredRefreshRate = 0;
3345 preferredModeId = 0;
3346 }
3347 }
3348
3349 private static final class ScreenshotApplicationState {
3350 WindowState appWin;
3351 int maxLayer;
3352 int minLayer;
3353 boolean screenshotReady;
3354
3355 void reset(boolean screenshotReady) {
3356 appWin = null;
3357 maxLayer = 0;
3358 minLayer = 0;
3359 this.screenshotReady = screenshotReady;
3360 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3361 }
3362 }
3363
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003364 /**
3365 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3366 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3367 * homogeneous children type which is currently required by sub-classes of
3368 * {@link WindowContainer} class.
3369 */
3370 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3371
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003372 @Override
3373 boolean fillsParent() {
3374 return true;
3375 }
3376
3377 @Override
3378 boolean isVisible() {
3379 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003380 }
3381 }
3382
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003383 /**
3384 * Window container class that contains all containers on this display relating to Apps.
3385 * I.e Activities.
3386 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003387 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003388 /**
3389 * A control placed at the appropriate level for transitions to occur.
3390 */
3391 SurfaceControl mAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003392
Wale Ogunwale61911492017-10-11 08:50:50 -07003393 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3394 // through the list to find them.
3395 private TaskStack mHomeStack = null;
3396 private TaskStack mPinnedStack = null;
3397 private TaskStack mSplitScreenPrimaryStack = null;
3398
3399 /**
3400 * Returns the topmost stack on the display that is compatible with the input windowing mode
3401 * and activity type. Null is no compatible stack on the display.
3402 */
3403 TaskStack getStack(int windowingMode, int activityType) {
3404 if (activityType == ACTIVITY_TYPE_HOME) {
3405 return mHomeStack;
3406 }
3407 if (windowingMode == WINDOWING_MODE_PINNED) {
3408 return mPinnedStack;
3409 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3410 return mSplitScreenPrimaryStack;
3411 }
3412 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3413 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003414 if (activityType == ACTIVITY_TYPE_UNDEFINED
3415 && windowingMode == stack.getWindowingMode()) {
3416 // Passing in undefined type means we want to match the topmost stack with the
3417 // windowing mode.
3418 return stack;
3419 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003420 if (stack.isCompatible(windowingMode, activityType)) {
3421 return stack;
3422 }
3423 }
3424 return null;
3425 }
3426
3427 @VisibleForTesting
3428 TaskStack getTopStack() {
3429 return mTaskStackContainers.getChildCount() > 0
3430 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3431 }
3432
3433 TaskStack getHomeStack() {
3434 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3435 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3436 }
3437 return mHomeStack;
3438 }
3439
3440 TaskStack getPinnedStack() {
3441 return mPinnedStack;
3442 }
3443
Matthew Ng64e77cf2017-10-31 14:01:31 -07003444 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003445 return mSplitScreenPrimaryStack;
3446 }
3447
Andrii Kulian839def92016-11-02 10:58:58 -07003448 /**
3449 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003450 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003451 */
3452 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003453 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003454 addChild(stack, onTop);
3455 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003456 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003457
Wale Ogunwale61911492017-10-11 08:50:50 -07003458 void onStackWindowingModeChanged(TaskStack stack) {
3459 removeStackReferenceIfNeeded(stack);
3460 addStackReferenceIfNeeded(stack);
3461 if (stack == mPinnedStack && getTopStack() != stack) {
3462 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3463 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3464 }
3465 }
3466
3467 private void addStackReferenceIfNeeded(TaskStack stack) {
3468 if (stack.isActivityTypeHome()) {
3469 if (mHomeStack != null) {
3470 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3471 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3472 }
3473 mHomeStack = stack;
3474 }
3475 final int windowingMode = stack.getWindowingMode();
3476 if (windowingMode == WINDOWING_MODE_PINNED) {
3477 if (mPinnedStack != null) {
3478 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3479 + mPinnedStack + " already exist on display=" + this
3480 + " stack=" + stack);
3481 }
3482 mPinnedStack = stack;
3483 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3484 if (mSplitScreenPrimaryStack != null) {
3485 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3486 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3487 + " already exist on display=" + this + " stack=" + stack);
3488 }
3489 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003490 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003491 }
3492 }
3493
3494 private void removeStackReferenceIfNeeded(TaskStack stack) {
3495 if (stack == mHomeStack) {
3496 mHomeStack = null;
3497 } else if (stack == mPinnedStack) {
3498 mPinnedStack = null;
3499 } else if (stack == mSplitScreenPrimaryStack) {
3500 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003501 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3502 mService.setDockedStackCreateStateLocked(
3503 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3504 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003505 }
Andrii Kulian839def92016-11-02 10:58:58 -07003506 }
3507
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003508 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003509 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3510 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003511 addChild(stack, addIndex);
3512 setLayoutNeeded();
3513 }
3514
Wale Ogunwale61911492017-10-11 08:50:50 -07003515 @Override
3516 protected void removeChild(TaskStack stack) {
3517 super.removeChild(stack);
3518 removeStackReferenceIfNeeded(stack);
3519 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003520
3521 @Override
3522 boolean isOnTop() {
3523 // Considered always on top
3524 return true;
3525 }
3526
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003527 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003528 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003529 if (child.getWindowConfiguration().isAlwaysOnTop()
3530 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003531 // This stack is always-on-top, override the default behavior.
3532 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3533
3534 // Moving to its current position, as we must call super but we don't want to
3535 // perform any meaningful action.
3536 final int currentPosition = mChildren.indexOf(child);
3537 super.positionChildAt(currentPosition, child, false /* includingParents */);
3538 return;
3539 }
3540
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003541 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3542 super.positionChildAt(targetPosition, child, includingParents);
3543
3544 setLayoutNeeded();
3545 }
3546
3547 /**
3548 * When stack is added or repositioned, find a proper position for it.
3549 * This will make sure that pinned stack always stays on top.
3550 * @param requestedPosition Position requested by caller.
3551 * @param stack Stack to be added or positioned.
3552 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3553 * @return The proper position for the stack.
3554 */
3555 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3556 final int topChildPosition = mChildren.size() - 1;
3557 boolean toTop = requestedPosition == POSITION_TOP;
3558 toTop |= adding ? requestedPosition >= topChildPosition + 1
3559 : requestedPosition >= topChildPosition;
3560 int targetPosition = requestedPosition;
3561
Wale Ogunwale61911492017-10-11 08:50:50 -07003562 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003563 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003564 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003565 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003566 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3567 }
3568
3569 // So, stack is moved just below the pinned stack.
3570 // When we're adding a new stack the target is the current pinned stack position.
3571 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003572 // stack, because WindowContainer#positionAt() first removes element and then adds
3573 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003574 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3575 }
3576
3577 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003578 }
3579
3580 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003581 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3582 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003583 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003584 if (super.forAllWindows(callback, traverseTopToBottom)) {
3585 return true;
3586 }
3587 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3588 return true;
3589 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003590 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003591 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3592 return true;
3593 }
3594 if (super.forAllWindows(callback, traverseTopToBottom)) {
3595 return true;
3596 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003597 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003598 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003599 }
3600
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003601 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003602 boolean traverseTopToBottom) {
3603 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3604 // app tokens.
3605 // TODO: Investigate if we need to continue to do this or if we can just process them
3606 // in-order.
3607 if (traverseTopToBottom) {
3608 for (int i = mChildren.size() - 1; i >= 0; --i) {
3609 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3610 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003611 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3612 traverseTopToBottom)) {
3613 return true;
3614 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003615 }
3616 }
3617 } else {
3618 final int count = mChildren.size();
3619 for (int i = 0; i < count; ++i) {
3620 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3621 final int appTokensCount = appTokens.size();
3622 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003623 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3624 traverseTopToBottom)) {
3625 return true;
3626 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003627 }
3628 }
3629 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003630 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003631 }
3632
Wale Ogunwale1666e312016-12-16 11:27:18 -08003633 void setExitingTokensHasVisible(boolean hasVisible) {
3634 for (int i = mChildren.size() - 1; i >= 0; --i) {
3635 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3636 for (int j = appTokens.size() - 1; j >= 0; --j) {
3637 appTokens.get(j).hasVisible = hasVisible;
3638 }
3639 }
3640 }
3641
3642 void removeExistingAppTokensIfPossible() {
3643 for (int i = mChildren.size() - 1; i >= 0; --i) {
3644 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3645 for (int j = appTokens.size() - 1; j >= 0; --j) {
3646 final AppWindowToken token = appTokens.get(j);
3647 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3648 && (!token.mIsExiting || token.isEmpty())) {
3649 // Make sure there is no animation running on this token, so any windows
3650 // associated with it will be removed as soon as their animations are
3651 // complete.
3652 token.mAppAnimator.clearAnimation();
3653 token.mAppAnimator.animating = false;
3654 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3655 "performLayout: App token exiting now removed" + token);
3656 token.removeIfPossible();
3657 }
3658 }
3659 }
3660 }
3661
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003662 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003663 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003664 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3665 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003666 // Apps and their containers are not allowed to specify an orientation while the
3667 // docked or freeform stack is visible...except for the home stack/task if the
3668 // docked stack is minimized and it actually set something.
3669 if (mHomeStack != null && mHomeStack.isVisible()
3670 && mDividerControllerLocked.isMinimizedDock()) {
3671 final int orientation = mHomeStack.getOrientation();
3672 if (orientation != SCREEN_ORIENTATION_UNSET) {
3673 return orientation;
3674 }
3675 }
3676 return SCREEN_ORIENTATION_UNSPECIFIED;
3677 }
3678
3679 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003680 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3681 PackageManager.FEATURE_AUTOMOTIVE);
3682 if (isCar) {
3683 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3684 // allow anything but the default orientation.
3685 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3686 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3687 return SCREEN_ORIENTATION_UNSPECIFIED;
3688 }
3689
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003690 if (orientation != SCREEN_ORIENTATION_UNSET
3691 && orientation != SCREEN_ORIENTATION_BEHIND) {
3692 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3693 "App is requesting an orientation, return " + orientation);
3694 return orientation;
3695 }
3696
3697 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003698 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003699 // The next app has not been requested to be visible, so we keep the current orientation
3700 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003701 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003702 }
Robert Carrb1579c82017-09-05 14:54:47 -07003703
3704 @Override
3705 void assignChildLayers(SurfaceControl.Transaction t) {
3706 final int NORMAL_STACK_STATE = 0;
3707 final int BOOSTED_STATE = 1;
3708 final int ALWAYS_ON_TOP_STATE = 2;
3709
3710 // We allow stacks to change visual order from the AM specified order due to
3711 // Z-boosting during animations. However we must take care to ensure TaskStacks
3712 // which are marked as alwaysOnTop remain that way.
3713 int layer = 0;
3714 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3715 for (int i = 0; i < mChildren.size(); i++) {
3716 final TaskStack s = mChildren.get(i);
3717 layer++;
3718 if (state == NORMAL_STACK_STATE) {
3719 s.assignLayer(t, layer);
3720 } else if (state == BOOSTED_STATE && s.needsZBoost()) {
3721 s.assignLayer(t, layer);
3722 } else if (state == ALWAYS_ON_TOP_STATE &&
3723 s.isAlwaysOnTop()) {
3724 s.assignLayer(t, layer);
3725 }
3726 s.assignChildLayers(t);
3727 }
3728 // The appropriate place for App-Transitions to occur is right
3729 // above all other animations but still below things in the Picture-and-Picture
3730 // windowing mode.
3731 if (state == BOOSTED_STATE && mAnimationLayer != null) {
3732 t.setLayer(mAnimationLayer, layer + 1);
3733 }
3734 }
3735 }
3736
3737 @Override
3738 void onParentSet() {
3739 super.onParentSet();
3740 if (getParent() != null) {
3741 mAnimationLayer = makeSurface().build();
3742 } else {
3743 mAnimationLayer.destroy();
3744 mAnimationLayer = null;
3745 }
3746 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003747 }
3748
3749 /**
3750 * Window container class that contains all containers on this display that are not related to
3751 * Apps. E.g. status bar.
3752 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003753 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3754 /**
3755 * Compares two child window tokens returns -1 if the first is lesser than the second in
3756 * terms of z-order and 1 otherwise.
3757 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003758 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003759 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003760 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3761 token1.mOwnerCanManageAppTokens)
3762 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3763 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003764
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003765 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3766 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3767 return false;
3768 }
3769 final int req = w.mAttrs.screenOrientation;
3770 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3771 || req == SCREEN_ORIENTATION_UNSET) {
3772 return false;
3773 }
3774 return true;
3775 };
3776
Wale Ogunwale3a931692016-11-02 16:49:48 -07003777 private final String mName;
3778 NonAppWindowContainers(String name) {
3779 mName = name;
3780 }
3781
3782 void addChild(WindowToken token) {
3783 addChild(token, mWindowComparator);
3784 }
3785
3786 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003787 int getOrientation() {
3788 final WindowManagerPolicy policy = mService.mPolicy;
3789 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003790 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003791
3792 if (win != null) {
3793 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003794 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003795 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003796 if (mService.mKeyguardGoingAway) {
3797 // Keyguard can't affect the orientation if it is going away...
3798 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3799 return SCREEN_ORIENTATION_UNSET;
3800 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003801 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003802 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003803 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003804 }
3805
Andrii Kulian8ee72852017-03-10 10:36:45 -08003806 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003807
Jorim Jaggi75520d52017-08-24 17:23:19 +02003808 if (policy.isKeyguardShowingAndNotOccluded()
3809 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003810 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003811 }
3812
3813 return SCREEN_ORIENTATION_UNSET;
3814 }
3815
3816 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003817 String getName() {
3818 return mName;
3819 }
Robert Carr3b716242016-08-16 16:02:21 -07003820 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003821
3822 /**
3823 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3824 */
3825 @FunctionalInterface
3826 private interface Screenshoter<E> {
3827 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3828 boolean useIdentityTransform, int rotation);
3829 }
Robert Carrb1579c82017-09-05 14:54:47 -07003830
3831 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3832 return mService.makeSurfaceBuilder(s)
3833 .setParent(mWindowingLayer);
3834 }
3835
3836 @Override
3837 SurfaceSession getSession() {
3838 return mSession;
3839 }
3840
3841 @Override
3842 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003843 SurfaceSession s = child != null ? child.getSession() : getSession();
3844 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003845 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003846
3847 if (child == null) {
3848 return b;
3849 }
3850
3851 b.setName(child.getName());
Robert Carrb1579c82017-09-05 14:54:47 -07003852 if (child.isScreenOverlay()) {
3853 return b.setParent(mOverlayLayer);
3854 } else {
3855 return b.setParent(mWindowingLayer);
3856 }
3857 }
3858
3859 /**
3860 * The makeSurface variants are for use by the window-container
3861 * hierarchy. makeOverlay here is a function for various non windowing
3862 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3863 * and other potpourii.
3864 */
3865 SurfaceControl.Builder makeOverlay() {
3866 return mService.makeSurfaceBuilder(mSession)
3867 .setParent(mOverlayLayer);
3868 }
3869
3870 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003871 applyMagnificationSpec(getPendingTransaction(), spec);
3872 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003873 }
3874
3875 @Override
3876 void onParentSet() {
3877 // Since we are the top of the SurfaceControl hierarchy here
3878 // we create the root surfaces explicitly rather than chaining
3879 // up as the default implementation in onParentSet does. So we
3880 // explicitly do NOT call super here.
3881 }
3882
3883 @Override
3884 void assignChildLayers(SurfaceControl.Transaction t) {
3885 t.setLayer(mOverlayLayer, 1)
3886 .setLayer(mWindowingLayer, 0);
3887
3888 // These are layers as children of "mWindowingLayer"
3889 mBelowAppWindowsContainers.assignLayer(t, 0);
3890 mTaskStackContainers.assignLayer(t, 1);
3891 mAboveAppWindowsContainers.assignLayer(t, 2);
3892
3893 WindowState imeTarget = mService.mInputMethodTarget;
3894 if (imeTarget == null || imeTarget.inSplitScreenWindowingMode()) {
3895 // In split-screen windowing mode we can't layer the
3896 // IME relative to the IME target because it needs to
3897 // go over the docked divider, so instead we place it on top
3898 // of everything and use relative layering of windows which need
3899 // to go above it (see special logic in WindowState#assignLayer)
3900 mImeWindowsContainers.assignLayer(t, 3);
3901 } else {
3902 t.setRelativeLayer(mImeWindowsContainers.getSurfaceControl(),
3903 imeTarget.getSurfaceControl(),
3904 // TODO: We need to use an extra level on the app surface to ensure
3905 // this is always above SurfaceView but always below attached window.
3906 1);
3907 }
3908
3909 // Above we have assigned layers to our children, now we ask them to assign
3910 // layers to their children.
3911 mBelowAppWindowsContainers.assignChildLayers(t);
3912 mTaskStackContainers.assignChildLayers(t);
3913 mAboveAppWindowsContainers.assignChildLayers(t);
3914 mImeWindowsContainers.assignChildLayers(t);
3915 }
3916
3917 /**
3918 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3919 * that the IME target is one of the docked applications. We'd like the docked divider to be
3920 * above both of the applications, and we'd like the IME to be above the docked divider.
3921 * However we need child windows of the applications to be above the IME (Text drag handles).
3922 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3923 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3924 * with {@link #WindowState#assignLayer}
3925 */
3926 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3927 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3928 }
3929
3930 @Override
3931 void destroyAfterPendingTransaction(SurfaceControl surface) {
3932 mPendingDestroyingSurfaces.add(surface);
3933 }
3934
3935 /**
3936 * Destroys any surfaces that have been put into the pending list with
3937 * {@link #destroyAfterTransaction}.
3938 */
3939 void onPendingTransactionApplied() {
3940 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3941 mPendingDestroyingSurfaces.get(i).destroy();
3942 }
3943 mPendingDestroyingSurfaces.clear();
3944 }
Craig Mautner59c00972012-07-30 12:10:24 -07003945}