blob: 91cce3175b0abb101c30a1481b7e203aeffa0f5e [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 Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Wale Ogunwale51362492016-09-08 17:49:17 -070020import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
24import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070025import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
27import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070028import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
29import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070030import static android.view.Display.DEFAULT_DISPLAY;
31import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070032import static android.view.Surface.ROTATION_0;
33import static android.view.Surface.ROTATION_180;
34import static android.view.Surface.ROTATION_270;
35import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070036import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070037import static android.view.WindowManager.DOCKED_BOTTOM;
38import static android.view.WindowManager.DOCKED_INVALID;
39import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080040import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
41import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070043import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070044import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070045import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
46import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070047import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070048import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070050import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070052import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070053import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
54import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070055import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070056import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
57import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070058import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070059import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070060import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070061import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
62import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070063import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi75520d52017-08-24 17:23:19 +020064import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
69import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070081import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070084import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070086import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070087import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
88import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070089import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070090import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070091import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
92import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070093import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
94import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
95import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070096import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070097import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070098import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070099import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700100import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700101import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700102import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700103import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700104import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700105import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
106import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
107import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
108import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
109import static com.android.server.wm.proto.DisplayProto.DPI;
110import static com.android.server.wm.proto.DisplayProto.ID;
111import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
112import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700113import static com.android.server.wm.proto.DisplayProto.ROTATION;
114import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700115import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700116import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700117
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700118import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700119import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700120import android.app.ActivityManager.StackId;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700121import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700122import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700123import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100124import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700125import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800126import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700127import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700128import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100129import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700130import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700131import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700132import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700133import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700134import android.os.RemoteException;
135import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700136import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800137import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700138import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700139import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700140import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700141import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700142import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700143import android.view.Surface;
144import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700145import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700146
Bryce Lee48f4b572017-04-10 10:54:15 -0700147import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800148import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700149import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700150
Robert Carr3b716242016-08-16 16:02:21 -0700151import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700152import java.io.PrintWriter;
153import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700154import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700155import java.util.HashMap;
156import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700157import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700158import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800159import java.util.function.Consumer;
160import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700161
Craig Mautner59c00972012-07-30 12:10:24 -0700162/**
163 * Utility class for keeping track of the WindowStates and other pertinent contents of a
164 * particular Display.
165 *
166 * IMPORTANT: No method from this class should ever be used without holding
167 * WindowManagerService.mWindowMap.
168 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700169class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700170 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700171
172 /** Unique identifier of this stack. */
173 private final int mDisplayId;
174
Wale Ogunwale3a931692016-11-02 16:49:48 -0700175 /** The containers below are the only child containers the display can have. */
176 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700177 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700178 // Contains all non-app window containers that should be displayed above the app containers
179 // (e.g. Status bar)
180 private final NonAppWindowContainers mAboveAppWindowsContainers =
181 new NonAppWindowContainers("mAboveAppWindowsContainers");
182 // Contains all non-app window containers that should be displayed below the app containers
183 // (e.g. Wallpaper).
184 private final NonAppWindowContainers mBelowAppWindowsContainers =
185 new NonAppWindowContainers("mBelowAppWindowsContainers");
186 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
187 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
188 // window containers together and move them in-sync if/when needed.
189 private final NonAppWindowContainers mImeWindowsContainers =
190 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700191
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000192 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800193 private WindowState mTmpWindow2;
194 private WindowAnimator mTmpWindowAnimator;
195 private boolean mTmpRecoveringMemory;
196 private boolean mUpdateImeTarget;
197 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700198 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700199
Wale Ogunwale02319a62016-09-26 15:21:22 -0700200 // Mapping from a token IBinder to a WindowToken object on this display.
201 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
202
Andrii Kuliancd097992017-03-23 18:31:59 -0700203 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700204 int mInitialDisplayWidth = 0;
205 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700206 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700207
208 /**
209 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
210 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
211 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
212 */
Craig Mautner59c00972012-07-30 12:10:24 -0700213 int mBaseDisplayWidth = 0;
214 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700215 /**
216 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
217 * but can be set from Settings or via shell command "adb shell wm density".
218 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
219 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700220 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700221 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700222 private final DisplayInfo mDisplayInfo = new DisplayInfo();
223 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700224 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700225 /**
226 * For default display it contains real metrics, empty for others.
227 * @see WindowManagerService#createWatermarkInTransaction()
228 */
229 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
230 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
231 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700232
Andrii Kulian06d07d62017-03-14 11:11:47 -0700233 /**
234 * Compat metrics computed based on {@link #mDisplayMetrics}.
235 * @see #updateDisplayAndOrientation(int)
236 */
237 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
238
239 /** The desired scaling factor for compatible apps. */
240 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700241
Andrii Kulian8ee72852017-03-10 10:36:45 -0800242 /**
243 * Current rotation of the display.
244 * Constants as per {@link android.view.Surface.Rotation}.
245 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700246 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800247 */
248 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700249
Andrii Kulian8ee72852017-03-10 10:36:45 -0800250 /**
251 * Last applied orientation of the display.
252 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
253 *
254 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
255 */
256 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700257
Andrii Kulian8ee72852017-03-10 10:36:45 -0800258 /**
259 * Flag indicating that the application is receiving an orientation that has different metrics
260 * than it expected. E.g. Portrait instead of Landscape.
261 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700262 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800263 */
264 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700265
Andrii Kulian8ee72852017-03-10 10:36:45 -0800266 /**
267 * Orientation forced by some window. If there is no visible window that specifies orientation
268 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
269 *
270 * @see NonAppWindowContainers#getOrientation()
271 */
272 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700273
Andrii Kulian8ee72852017-03-10 10:36:45 -0800274 /**
275 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
276 * occluded.
277 *
278 * @see NonAppWindowContainers#getOrientation()
279 */
280 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
281
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700282 /**
283 * Keep track of wallpaper visibility to notify changes.
284 */
285 private boolean mLastWallpaperVisible = false;
286
Andrii Kulian06d07d62017-03-14 11:11:47 -0700287 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700288 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700289
Craig Mautner39834192012-09-02 07:47:24 -0700290 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700291 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700292 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700293 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800294 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700295
Craig Mautnerdc548482014-02-05 13:35:24 -0800296 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700297 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800298
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800299 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
300 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700301 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700302
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700303 /** Detect user tapping outside of current focused task bounds .*/
304 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700305
Craig Mautner6601b7b2013-04-29 10:29:11 -0700306 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700307 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700308
Craig Mautner6601b7b2013-04-29 10:29:11 -0700309 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800310 private final Rect mTmpRect = new Rect();
311 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700312 private final RectF mTmpRectF = new RectF();
313 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800314 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700315
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800316 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700317
Craig Mautner95da1082014-02-24 17:54:35 -0800318 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700319 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800320
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700321 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700322 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700323
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800324 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700325
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800326 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
327
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000328 private boolean mHaveBootMsg = false;
329 private boolean mHaveApp = false;
330 private boolean mHaveWallpaper = false;
331 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700332
333 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
334
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700335 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
336 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000337 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
338 new ApplySurfaceChangesTransactionState();
339 private final ScreenshotApplicationState mScreenshotApplicationState =
340 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700341
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700342 // True if this display is in the process of being removed. Used to determine if the removal of
343 // the display's direct children should be allowed.
344 private boolean mRemovingDisplay = false;
345
Bryce Leed1871262017-06-12 14:12:29 -0700346 // {@code false} if this display is in the processing of being created.
347 private boolean mDisplayReady = false;
348
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700349 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800350 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700351 int mInputMethodAnimLayerAdjustment;
352
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800353 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
354 WindowStateAnimator winAnimator = w.mWinAnimator;
355 if (winAnimator.hasSurface()) {
356 final boolean wasAnimating = winAnimator.mWasAnimating;
357 final boolean nowAnimating = winAnimator.stepAnimationLocked(
358 mTmpWindowAnimator.mCurrentTime);
359 winAnimator.mWasAnimating = nowAnimating;
360 mTmpWindowAnimator.orAnimating(nowAnimating);
361
362 if (DEBUG_WALLPAPER) Slog.v(TAG,
363 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
364
365 if (wasAnimating && !winAnimator.mAnimating
366 && mWallpaperController.isWallpaperTarget(w)) {
367 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
368 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
369 if (DEBUG_LAYOUT_REPEATS) {
370 mService.mWindowPlacerLocked.debugLayoutRepeats(
371 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
372 }
373 }
374 }
375
376 final AppWindowToken atoken = w.mAppToken;
377 if (winAnimator.mDrawState == READY_TO_SHOW) {
378 if (atoken == null || atoken.allDrawn) {
379 if (w.performShowLocked()) {
380 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
381 if (DEBUG_LAYOUT_REPEATS) {
382 mService.mWindowPlacerLocked.debugLayoutRepeats(
383 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
384 }
385 }
386 }
387 }
388 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
389 if (appAnimator != null && appAnimator.thumbnail != null) {
390 if (appAnimator.thumbnailTransactionSeq
391 != mTmpWindowAnimator.mAnimTransactionSequence) {
392 appAnimator.thumbnailTransactionSeq =
393 mTmpWindowAnimator.mAnimTransactionSequence;
394 appAnimator.thumbnailLayer = 0;
395 }
396 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
397 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
398 }
399 }
400 };
401
402 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
403 final WindowStateAnimator winAnimator = w.mWinAnimator;
404 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
405 return;
406 }
407
408 final int flags = w.mAttrs.flags;
409
410 // If this window is animating, make a note that we have an animating window and take
411 // care of a request to run a detached wallpaper animation.
412 if (winAnimator.mAnimating) {
413 if (winAnimator.mAnimation != null) {
414 if ((flags & FLAG_SHOW_WALLPAPER) != 0
415 && winAnimator.mAnimation.getDetachWallpaper()) {
416 mTmpWindow = w;
417 }
418 final int color = winAnimator.mAnimation.getBackgroundColor();
419 if (color != 0) {
420 final TaskStack stack = w.getStack();
421 if (stack != null) {
422 stack.setAnimationBackground(winAnimator, color);
423 }
424 }
425 }
426 mTmpWindowAnimator.setAnimating(true);
427 }
428
429 // If this window's app token is running a detached wallpaper animation, make a note so
430 // we can ensure the wallpaper is displayed behind it.
431 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
432 if (appAnimator != null && appAnimator.animation != null
433 && appAnimator.animating) {
434 if ((flags & FLAG_SHOW_WALLPAPER) != 0
435 && appAnimator.animation.getDetachWallpaper()) {
436 mTmpWindow = w;
437 }
438
439 final int color = appAnimator.animation.getBackgroundColor();
440 if (color != 0) {
441 final TaskStack stack = w.getStack();
442 if (stack != null) {
443 stack.setAnimationBackground(winAnimator, color);
444 }
445 }
446 }
447 };
448
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800449 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
450 final int lostFocusUid = mTmpWindow.mOwnerUid;
451 final Handler handler = mService.mH;
452 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
453 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
454 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
455 w.mAttrs.hideTimeoutMilliseconds);
456 }
457 }
458 };
459
460 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
461 final AppWindowToken focusedApp = mService.mFocusedApp;
462 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
463 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
464
465 if (!w.canReceiveKeys()) {
466 return false;
467 }
468
469 final AppWindowToken wtoken = w.mAppToken;
470
471 // If this window's application has been removed, just skip it.
472 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
473 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
474 + (wtoken.removed ? "removed" : "sendingToBottom"));
475 return false;
476 }
477
478 if (focusedApp == null) {
479 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
480 + " using new focus @ " + w);
481 mTmpWindow = w;
482 return true;
483 }
484
485 if (!focusedApp.windowsAreFocusable()) {
486 // Current focused app windows aren't focusable...
487 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
488 + " focusable using new focus @ " + w);
489 mTmpWindow = w;
490 return true;
491 }
492
493 // Descend through all of the app tokens and find the first that either matches
494 // win.mAppToken (return win) or mFocusedApp (return null).
495 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
496 if (focusedApp.compareTo(wtoken) > 0) {
497 // App stack below focused app stack. No focus for you!!!
498 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
499 "findFocusedWindow: Reached focused app=" + focusedApp);
500 mTmpWindow = null;
501 return true;
502 }
503 }
504
505 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
506 mTmpWindow = w;
507 return true;
508 };
509
510 private final Consumer<WindowState> mPrepareWindowSurfaces =
511 w -> w.mWinAnimator.prepareSurfaceLocked(true);
512
513 private final Consumer<WindowState> mPerformLayout = w -> {
514 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
515 // wasting time and funky changes while a window is animating away.
516 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
517 || w.isGoneForLayoutLw();
518
519 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
520 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
521 + " mLayoutAttached=" + w.mLayoutAttached
522 + " screen changed=" + w.isConfigChanged());
523 final AppWindowToken atoken = w.mAppToken;
524 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
525 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
526 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
527 + " parentHidden=" + w.isParentWindowHidden());
528 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
529 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
530 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
531 + " parentHidden=" + w.isParentWindowHidden());
532 }
533
534 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
535 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
536 // since that means "perform layout as normal, just don't display").
537 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
538 || ((w.isConfigChanged() || w.setReportResizeHints())
539 && !w.isGoneForLayoutLw() &&
540 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
541 (w.mHasSurface && w.mAppToken != null &&
542 w.mAppToken.layoutConfigChanges)))) {
543 if (!w.mLayoutAttached) {
544 if (mTmpInitial) {
545 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
546 w.mContentChanged = false;
547 }
548 if (w.mAttrs.type == TYPE_DREAM) {
549 // Don't layout windows behind a dream, so that if it does stuff like hide
550 // the status bar we won't get a bad transition when it goes away.
551 mTmpWindow = w;
552 }
553 w.mLayoutNeeded = false;
554 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200555 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800556 mService.mPolicy.layoutWindowLw(w, null);
557 w.mLayoutSeq = mService.mLayoutSeq;
558
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200559 // If this is the first layout, we need to initialize the last inset values as
560 // otherwise we'd immediately cause an unnecessary resize.
561 if (firstLayout) {
562 w.updateLastInsetValues();
563 }
564
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800565 // Window frames may have changed. Update dim layer with the new bounds.
566 final Task task = w.getTask();
567 if (task != null) {
568 mDimLayerController.updateDimLayer(task);
569 }
570
571 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
572 + " mContainingFrame=" + w.mContainingFrame
573 + " mDisplayFrame=" + w.mDisplayFrame);
574 }
575 }
576 };
577
578 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
579 if (w.mLayoutAttached) {
580 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
581 + " mViewVisibility=" + w.mViewVisibility
582 + " mRelayoutCalled=" + w.mRelayoutCalled);
583 // If this view is GONE, then skip it -- keep the current frame, and let the caller
584 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
585 // windows, since that means "perform layout as normal, just don't display").
586 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
587 return;
588 }
589 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
590 || w.mLayoutNeeded) {
591 if (mTmpInitial) {
592 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
593 w.mContentChanged = false;
594 }
595 w.mLayoutNeeded = false;
596 w.prelayout();
597 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
598 w.mLayoutSeq = mService.mLayoutSeq;
599 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
600 + " mContainingFrame=" + w.mContainingFrame
601 + " mDisplayFrame=" + w.mDisplayFrame);
602 }
603 } else if (w.mAttrs.type == TYPE_DREAM) {
604 // Don't layout windows behind a dream, so that if it does stuff like hide the
605 // status bar we won't get a bad transition when it goes away.
606 mTmpWindow = mTmpWindow2;
607 }
608 };
609
610 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
611 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
612 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
613 return w.canBeImeTarget();
614 };
615
616 private final Consumer<WindowState> mApplyPostLayoutPolicy =
617 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
618 mService.mInputMethodTarget);
619
620 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
621 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
622 final boolean obscuredChanged = w.mObscured !=
623 mTmpApplySurfaceChangesTransactionState.obscured;
624 final RootWindowContainer root = mService.mRoot;
625 // Only used if default window
626 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
627
628 // Update effect.
629 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
630 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
631 final boolean isDisplayed = w.isDisplayedLw();
632
633 if (isDisplayed && w.isObscuringDisplay()) {
634 // This window completely covers everything behind it, so we want to leave all
635 // of them as undimmed (for performance reasons).
636 root.mObscuringWindow = w;
637 mTmpApplySurfaceChangesTransactionState.obscured = true;
638 }
639
640 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
641 root.handleNotObscuredLocked(w,
642 mTmpApplySurfaceChangesTransactionState.obscured,
643 mTmpApplySurfaceChangesTransactionState.syswin);
644
645 if (w.mHasSurface && isDisplayed) {
646 final int type = w.mAttrs.type;
647 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
648 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
649 mTmpApplySurfaceChangesTransactionState.syswin = true;
650 }
651 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
652 && w.mAttrs.preferredRefreshRate != 0) {
653 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
654 = w.mAttrs.preferredRefreshRate;
655 }
656 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
657 && w.mAttrs.preferredDisplayModeId != 0) {
658 mTmpApplySurfaceChangesTransactionState.preferredModeId
659 = w.mAttrs.preferredDisplayModeId;
660 }
661 }
662 }
663
664 w.applyDimLayerIfNeeded();
665
666 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
667 && mWallpaperController.isWallpaperTarget(w)) {
668 // This is the wallpaper target and its obscured state changed... make sure the
669 // current wallpaper's visibility has been updated accordingly.
670 mWallpaperController.updateWallpaperVisibility();
671 }
672
673 w.handleWindowMovedIfNeeded();
674
675 final WindowStateAnimator winAnimator = w.mWinAnimator;
676
677 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
678 w.mContentChanged = false;
679
680 // Moved from updateWindowsAndWallpaperLocked().
681 if (w.mHasSurface) {
682 // Take care of the window being ready to display.
683 final boolean committed = winAnimator.commitFinishDrawingLocked();
684 if (isDefaultDisplay && committed) {
685 if (w.mAttrs.type == TYPE_DREAM) {
686 // HACK: When a dream is shown, it may at that point hide the lock screen.
687 // So we need to redo the layout to let the phone window manager make this
688 // happen.
689 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
690 if (DEBUG_LAYOUT_REPEATS) {
691 surfacePlacer.debugLayoutRepeats(
692 "dream and commitFinishDrawingLocked true",
693 pendingLayoutChanges);
694 }
695 }
696 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
697 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
698 "First draw done in potential wallpaper target " + w);
699 root.mWallpaperMayChange = true;
700 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
701 if (DEBUG_LAYOUT_REPEATS) {
702 surfacePlacer.debugLayoutRepeats(
703 "wallpaper and commitFinishDrawingLocked true",
704 pendingLayoutChanges);
705 }
706 }
707 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700708 final TaskStack stack = w.getStack();
709 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
710 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800711 // Updates the shown frame before we set up the surface. This is needed
712 // because the resizing could change the top-left position (in addition to
713 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
714 // position the surface.
715 //
716 // If an animation is being started, we can't call this method because the
717 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700718 // we do want to update the position if the window is animating. We make an exception
719 // for the bounds animating state, where an application may have been waiting
720 // for an exit animation to start, but instead enters PiP. We need to ensure
721 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800722 winAnimator.computeShownFrameLocked();
723 }
724 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
725 }
726
727 final AppWindowToken atoken = w.mAppToken;
728 if (atoken != null) {
729 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
730 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
731 mTmpUpdateAllDrawn.add(atoken);
732 }
733 }
734
735 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
736 && w.isDisplayedLw()) {
737 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
738 }
739
740 w.updateResizingWindowIfNeeded();
741 };
742
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800743 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800744 * Create new {@link DisplayContent} instance, add itself to the root window container and
745 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700746 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800747 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700748 * @param layersController window layer controller used to assign layer to the windows on this
749 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700750 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
751 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700752 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700753 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700754 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800755 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
756 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
757 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
758 + " new=" + display);
759 }
760
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700761 mDisplay = display;
762 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700763 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700764 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700765 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700766 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700767 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700768 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700769 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800770 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700771 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800772 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700773
774 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700775 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700776 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700777 super.addChild(mAboveAppWindowsContainers, null);
778 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800779
780 // Add itself as a child to the root container.
781 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700782
783 // TODO(b/62541591): evaluate whether this is the best spot to declare the
784 // {@link DisplayContent} ready for use.
785 mDisplayReady = true;
786 }
787
788 boolean isReady() {
789 // The display is ready when the system and the individual display are both ready.
790 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700791 }
792
793 int getDisplayId() {
794 return mDisplayId;
795 }
796
Wale Ogunwale02319a62016-09-26 15:21:22 -0700797 WindowToken getWindowToken(IBinder binder) {
798 return mTokenMap.get(binder);
799 }
800
801 AppWindowToken getAppWindowToken(IBinder binder) {
802 final WindowToken token = getWindowToken(binder);
803 if (token == null) {
804 return null;
805 }
806 return token.asAppWindowToken();
807 }
808
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700809 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700810 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
811 if (dc != null) {
812 // We currently don't support adding a window token to the display if the display
813 // already has the binder mapped to another token. If there is a use case for supporting
814 // this moving forward we will either need to merge the WindowTokens some how or have
815 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700816 throw new IllegalArgumentException("Can't map token=" + token + " to display="
817 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700818 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700819 if (binder == null) {
820 throw new IllegalArgumentException("Can't map token=" + token + " to display="
821 + getName() + " binder is null");
822 }
823 if (token == null) {
824 throw new IllegalArgumentException("Can't map null token to display="
825 + getName() + " binder=" + binder);
826 }
827
Wale Ogunwale02319a62016-09-26 15:21:22 -0700828 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700829
830 if (token.asAppWindowToken() == null) {
831 // Add non-app token to container hierarchy on the display. App tokens are added through
832 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700833 switch (token.windowType) {
834 case TYPE_WALLPAPER:
835 mBelowAppWindowsContainers.addChild(token);
836 break;
837 case TYPE_INPUT_METHOD:
838 case TYPE_INPUT_METHOD_DIALOG:
839 mImeWindowsContainers.addChild(token);
840 break;
841 default:
842 mAboveAppWindowsContainers.addChild(token);
843 break;
844 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700845 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700846 }
847
848 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700849 final WindowToken token = mTokenMap.remove(binder);
850 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800851 token.setExiting();
852 }
853 return token;
854 }
855
856 /** Changes the display the input window token is housed on to this one. */
857 void reParentWindowToken(WindowToken token) {
858 final DisplayContent prevDc = token.getDisplayContent();
859 if (prevDc == this) {
860 return;
861 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800862 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
863 && token.asAppWindowToken() == null) {
864 // Removed the token from the map, but made sure it's not an app token before removing
865 // from parent.
866 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700867 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800868
869 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700870 }
871
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700872 void removeAppToken(IBinder binder) {
873 final WindowToken token = removeWindowToken(binder);
874 if (token == null) {
875 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
876 return;
877 }
878
879 final AppWindowToken appToken = token.asAppWindowToken();
880
881 if (appToken == null) {
882 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
883 return;
884 }
885
886 appToken.onRemovedFromDisplay();
887 }
888
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700889 Display getDisplay() {
890 return mDisplay;
891 }
892
Craig Mautner59c00972012-07-30 12:10:24 -0700893 DisplayInfo getDisplayInfo() {
894 return mDisplayInfo;
895 }
896
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700897 DisplayMetrics getDisplayMetrics() {
898 return mDisplayMetrics;
899 }
900
Andrii Kulian8ee72852017-03-10 10:36:45 -0800901 int getRotation() {
902 return mRotation;
903 }
904
905 void setRotation(int newRotation) {
906 mRotation = newRotation;
907 }
908
909 int getLastOrientation() {
910 return mLastOrientation;
911 }
912
913 void setLastOrientation(int orientation) {
914 mLastOrientation = orientation;
915 }
916
917 boolean getAltOrientation() {
918 return mAltOrientation;
919 }
920
921 void setAltOrientation(boolean altOrientation) {
922 mAltOrientation = altOrientation;
923 }
924
925 int getLastWindowForcedOrientation() {
926 return mLastWindowForcedOrientation;
927 }
928
Andrii Kulian06d07d62017-03-14 11:11:47 -0700929 /**
930 * Update rotation of the display.
931 *
932 * Returns true if the rotation has been changed. In this case YOU MUST CALL
933 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
934 */
935 boolean updateRotationUnchecked(boolean inTransaction) {
936 if (mService.mDeferredRotationPauseCount > 0) {
937 // Rotation updates have been paused temporarily. Defer the update until
938 // updates have been resumed.
939 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
940 return false;
941 }
942
943 ScreenRotationAnimation screenRotationAnimation =
944 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
945 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
946 // Rotation updates cannot be performed while the previous rotation change
947 // animation is still in progress. Skip this update. We will try updating
948 // again after the animation is finished and the display is unfrozen.
949 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
950 return false;
951 }
952 if (mService.mDisplayFrozen) {
953 // Even if the screen rotation animation has finished (e.g. isAnimating
954 // returns false), there is still some time where we haven't yet unfrozen
955 // the display. We also need to abort rotation here.
956 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
957 "Deferring rotation, still finishing previous rotation");
958 return false;
959 }
960
961 if (!mService.mDisplayEnabled) {
962 // No point choosing a rotation if the display is not enabled.
963 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
964 return false;
965 }
966
967 final int oldRotation = mRotation;
968 final int lastOrientation = mLastOrientation;
969 final boolean oldAltOrientation = mAltOrientation;
970 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700971 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
972 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700973
Robert Carr897215d2017-03-29 12:25:50 -0700974 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700975 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
976 if (seamlessRotated != null) {
977 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
978 // to complete (that is, waiting for windows to redraw). It's tempting to check
979 // w.mSeamlessRotationCount but that could be incorrect in the case of
980 // window-removal.
981 return false;
982 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700983 }
984
985 // TODO: Implement forced rotation changes.
986 // Set mAltOrientation to indicate that the application is receiving
987 // an orientation that has different metrics than it expected.
988 // eg. Portrait instead of Landscape.
989
990 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
991 lastOrientation, rotation);
992
993 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
994 + ", got rotation " + rotation + " which has "
995 + (altOrientation ? "incompatible" : "compatible") + " metrics");
996
997 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
998 // No change.
999 return false;
1000 }
1001
1002 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1003 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1004 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1005
1006 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1007 mService.mWaitingForConfig = true;
1008 }
1009
1010 mRotation = rotation;
1011 mAltOrientation = altOrientation;
1012 if (isDefaultDisplay) {
1013 mService.mPolicy.setRotationLw(rotation);
1014 }
1015
1016 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1017 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1018 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1019 WINDOW_FREEZE_TIMEOUT_DURATION);
1020
1021 setLayoutNeeded();
1022 final int[] anim = new int[2];
1023 if (isDimming()) {
1024 anim[0] = anim[1] = 0;
1025 } else {
1026 mService.mPolicy.selectRotationAnimationLw(anim);
1027 }
1028
1029 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001030 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001031 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1032 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1033 mDisplayId);
1034 } else {
1035 // The screen rotation animation uses a screenshot to freeze the screen
1036 // while windows resize underneath.
1037 // When we are rotating seamlessly, we allow the elements to transition
1038 // to their rotated state independently and without a freeze required.
1039 screenRotationAnimation = null;
1040
1041 // We have to reset this in case a window was removed before it
1042 // finished seamless rotation.
1043 mService.mSeamlessRotationCount = 0;
1044 }
1045
1046 // We need to update our screen size information to match the new rotation. If the rotation
1047 // has actually changed then this method will return true and, according to the comment at
1048 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1049 // By updating the Display info here it will be available to
1050 // #computeScreenConfiguration() later.
1051 updateDisplayAndOrientation(getConfiguration().uiMode);
1052
1053 if (!inTransaction) {
1054 if (SHOW_TRANSACTIONS) {
1055 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1056 }
1057 mService.openSurfaceTransaction();
1058 }
1059 try {
1060 // NOTE: We disable the rotation in the emulator because
1061 // it doesn't support hardware OpenGL emulation yet.
1062 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1063 && screenRotationAnimation.hasScreenshot()) {
1064 if (screenRotationAnimation.setRotationInTransaction(
1065 rotation, mService.mFxSession,
1066 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1067 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1068 mService.scheduleAnimationLocked();
1069 }
1070 }
1071
1072 if (rotateSeamlessly) {
1073 forAllWindows(w -> {
1074 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1075 }, true /* traverseTopToBottom */);
1076 }
1077
1078 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1079 } finally {
1080 if (!inTransaction) {
1081 mService.closeSurfaceTransaction();
1082 if (SHOW_LIGHT_TRANSACTIONS) {
1083 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1084 }
1085 }
1086 }
1087
1088 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001089 if (w.mHasSurface && !rotateSeamlessly) {
1090 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001091 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001092 mService.mRoot.mOrientationChangeComplete = false;
1093 w.mLastFreezeDuration = 0;
1094 }
1095 w.mReportOrientationChanged = true;
1096 }, true /* traverseTopToBottom */);
1097
1098 if (rotateSeamlessly) {
1099 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1100 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1101 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1102 }
1103
1104 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1105 final WindowManagerService.RotationWatcher rotationWatcher
1106 = mService.mRotationWatchers.get(i);
1107 if (rotationWatcher.mDisplayId == mDisplayId) {
1108 try {
1109 rotationWatcher.mWatcher.onRotationChanged(rotation);
1110 } catch (RemoteException e) {
1111 // Ignore
1112 }
1113 }
1114 }
1115
1116 // TODO (multi-display): Magnification is supported only for the default display.
1117 // Announce rotation only if we will not animate as we already have the
1118 // windows in final state. Otherwise, we make this call at the rotation end.
1119 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1120 && isDefaultDisplay) {
1121 mService.mAccessibilityController.onRotationChangedLocked(this);
1122 }
1123
1124 return true;
1125 }
1126
1127 /**
1128 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1129 * changed.
1130 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1131 */
1132 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1133 // Use the effective "visual" dimensions based on current rotation
1134 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1135 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1136 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1137 int dw = realdw;
1138 int dh = realdh;
1139
1140 if (mAltOrientation) {
1141 if (realdw > realdh) {
1142 // Turn landscape into portrait.
1143 int maxw = (int)(realdh/1.3f);
1144 if (maxw < realdw) {
1145 dw = maxw;
1146 }
1147 } else {
1148 // Turn portrait into landscape.
1149 int maxh = (int)(realdw/1.3f);
1150 if (maxh < realdh) {
1151 dh = maxh;
1152 }
1153 }
1154 }
1155
1156 // Update application display metrics.
1157 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1158 mDisplayId);
1159 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1160 mDisplayId);
1161 mDisplayInfo.rotation = mRotation;
1162 mDisplayInfo.logicalWidth = dw;
1163 mDisplayInfo.logicalHeight = dh;
1164 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1165 mDisplayInfo.appWidth = appWidth;
1166 mDisplayInfo.appHeight = appHeight;
1167 if (isDefaultDisplay) {
1168 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1169 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1170 }
1171 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1172 if (mDisplayScalingDisabled) {
1173 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1174 } else {
1175 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1176 }
1177
1178 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1179 mDisplayInfo);
1180
1181 mBaseDisplayRect.set(0, 0, dw, dh);
1182
1183 if (isDefaultDisplay) {
1184 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1185 mCompatDisplayMetrics);
1186 }
1187 return mDisplayInfo;
1188 }
1189
1190 /**
1191 * Compute display configuration based on display properties and policy settings.
1192 * Do not call if mDisplayReady == false.
1193 */
1194 void computeScreenConfiguration(Configuration config) {
1195 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1196
1197 final int dw = displayInfo.logicalWidth;
1198 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001199 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1200 // TODO: Probably best to set this based on some setting in the display content object,
1201 // so the display can be configured for things like fullscreen.
1202 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001203
Andrii Kulian06d07d62017-03-14 11:11:47 -07001204 config.screenWidthDp =
1205 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1206 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1207 config.screenHeightDp =
1208 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1209 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001210
1211 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1212 final int leftInset = mTmpRect.left;
1213 final int topInset = mTmpRect.top;
1214 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001215 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1216 leftInset + displayInfo.appWidth /* right */,
1217 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001218 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1219 || displayInfo.rotation == Surface.ROTATION_270);
1220
1221 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1222 mDisplayMetrics.density, config);
1223
1224 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1225 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1226 ? Configuration.SCREENLAYOUT_ROUND_YES
1227 : Configuration.SCREENLAYOUT_ROUND_NO);
1228
1229 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1230 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1231 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1232 dh, mDisplayId);
1233 config.densityDpi = displayInfo.logicalDensityDpi;
1234
1235 config.colorMode =
1236 (displayInfo.isHdr()
1237 ? Configuration.COLOR_MODE_HDR_YES
1238 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001239 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001240 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1241 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1242
1243 // Update the configuration based on available input devices, lid switch,
1244 // and platform configuration.
1245 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1246 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1247 config.navigation = Configuration.NAVIGATION_NONAV;
1248
1249 int keyboardPresence = 0;
1250 int navigationPresence = 0;
1251 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1252 final int len = devices != null ? devices.length : 0;
1253 for (int i = 0; i < len; i++) {
1254 InputDevice device = devices[i];
1255 if (!device.isVirtual()) {
1256 final int sources = device.getSources();
1257 final int presenceFlag = device.isExternal() ?
1258 WindowManagerPolicy.PRESENCE_EXTERNAL :
1259 WindowManagerPolicy.PRESENCE_INTERNAL;
1260
1261 // TODO(multi-display): Configure on per-display basis.
1262 if (mService.mIsTouchDevice) {
1263 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1264 InputDevice.SOURCE_TOUCHSCREEN) {
1265 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1266 }
1267 } else {
1268 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1269 }
1270
1271 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1272 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1273 navigationPresence |= presenceFlag;
1274 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1275 && config.navigation == Configuration.NAVIGATION_NONAV) {
1276 config.navigation = Configuration.NAVIGATION_DPAD;
1277 navigationPresence |= presenceFlag;
1278 }
1279
1280 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1281 config.keyboard = Configuration.KEYBOARD_QWERTY;
1282 keyboardPresence |= presenceFlag;
1283 }
1284 }
1285 }
1286
1287 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1288 config.navigation = Configuration.NAVIGATION_DPAD;
1289 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1290 }
1291
1292 // Determine whether a hard keyboard is available and enabled.
1293 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1294 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1295 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1296 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1297 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1298 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1299 }
1300
1301 // Let the policy update hidden states.
1302 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1303 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1304 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1305 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1306 }
1307
1308 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1309 int displayId) {
1310 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1311 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1312 final int unrotDw, unrotDh;
1313 if (rotated) {
1314 unrotDw = dh;
1315 unrotDh = dw;
1316 } else {
1317 unrotDw = dw;
1318 unrotDh = dh;
1319 }
1320 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1321 displayId);
1322 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1323 displayId);
1324 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1325 displayId);
1326 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1327 displayId);
1328 return sw;
1329 }
1330
1331 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1332 DisplayMetrics dm, int dw, int dh, int displayId) {
1333 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1334 displayId);
1335 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1336 uiMode, displayId);
1337 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1338 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1339 if (curSize == 0 || size < curSize) {
1340 curSize = size;
1341 }
1342 return curSize;
1343 }
1344
1345 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1346 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1347
1348 // We need to determine the smallest width that will occur under normal
1349 // operation. To this, start with the base screen size and compute the
1350 // width under the different possible rotations. We need to un-rotate
1351 // the current screen dimensions before doing this.
1352 int unrotDw, unrotDh;
1353 if (rotated) {
1354 unrotDw = dh;
1355 unrotDh = dw;
1356 } else {
1357 unrotDw = dw;
1358 unrotDh = dh;
1359 }
1360 displayInfo.smallestNominalAppWidth = 1<<30;
1361 displayInfo.smallestNominalAppHeight = 1<<30;
1362 displayInfo.largestNominalAppWidth = 0;
1363 displayInfo.largestNominalAppHeight = 0;
1364 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1365 unrotDh);
1366 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1367 unrotDw);
1368 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1369 unrotDh);
1370 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1371 unrotDw);
1372 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1373 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1374 displayId);
1375 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1376 displayId);
1377 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1378 displayId);
1379 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1380 displayId);
1381 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1382 outConfig.screenLayout = sl;
1383 }
1384
1385 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1386 int uiMode, int displayId) {
1387 // Get the app screen size at this rotation.
1388 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1389 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1390
1391 // Compute the screen layout size class for this rotation.
1392 int longSize = w;
1393 int shortSize = h;
1394 if (longSize < shortSize) {
1395 int tmp = longSize;
1396 longSize = shortSize;
1397 shortSize = tmp;
1398 }
1399 longSize = (int)(longSize/density);
1400 shortSize = (int)(shortSize/density);
1401 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1402 }
1403
1404 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1405 int uiMode, int dw, int dh) {
1406 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1407 displayId);
1408 if (width < displayInfo.smallestNominalAppWidth) {
1409 displayInfo.smallestNominalAppWidth = width;
1410 }
1411 if (width > displayInfo.largestNominalAppWidth) {
1412 displayInfo.largestNominalAppWidth = width;
1413 }
1414 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1415 displayId);
1416 if (height < displayInfo.smallestNominalAppHeight) {
1417 displayInfo.smallestNominalAppHeight = height;
1418 }
1419 if (height > displayInfo.largestNominalAppHeight) {
1420 displayInfo.largestNominalAppHeight = height;
1421 }
1422 }
1423
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001424 DockedStackDividerController getDockedDividerController() {
1425 return mDividerControllerLocked;
1426 }
1427
Winson Chung655332c2016-10-31 13:14:28 -07001428 PinnedStackController getPinnedStackController() {
1429 return mPinnedStackControllerLocked;
1430 }
1431
Jeff Browna506a6e2013-06-04 00:02:38 -07001432 /**
1433 * Returns true if the specified UID has access to this display.
1434 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001435 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001436 return mDisplay.hasAccess(uid);
1437 }
1438
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001439 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001440 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001441 }
1442
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001443 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001444 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001445 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001446 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001447 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001448 }
1449
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001450 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001451 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1452 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001453 if (stack.mStackId == stackId) {
1454 return stack;
1455 }
1456 }
1457 return null;
1458 }
1459
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001460 /**
1461 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1462 * Null is no compatible stack on the display.
1463 */
1464 TaskStack getStack(int windowingMode) {
1465 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1466 }
1467
1468 /**
1469 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1470 * activity type. Null is no compatible stack on the display.
1471 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001472 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001473 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1474 final TaskStack stack = mTaskStackContainers.get(i);
1475 if (stack.isCompatible(windowingMode, activityType)) {
1476 return stack;
1477 }
1478 }
1479 return null;
1480 }
1481
Bryce Lee48f4b572017-04-10 10:54:15 -07001482 @VisibleForTesting
1483 int getStackCount() {
1484 return mTaskStackContainers.size();
1485 }
1486
1487 @VisibleForTesting
Wale Ogunwale68278562017-09-23 17:13:55 -07001488 int getStackPosition(int windowingMode, int activityType) {
Bryce Lee48f4b572017-04-10 10:54:15 -07001489 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1490 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07001491 if (stack.isCompatible(windowingMode, activityType)) {
Bryce Lee48f4b572017-04-10 10:54:15 -07001492 return i;
1493 }
1494 }
1495 return -1;
1496 }
1497
Andrii Kulian441e4492016-09-29 15:25:00 -07001498 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001499 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001500 super.onConfigurationChanged(newParentConfig);
1501
Andrii Kulian3a507b52016-09-19 18:14:12 -07001502 // The display size information is heavily dependent on the resources in the current
1503 // configuration, so we need to reconfigure it every time the configuration changes.
1504 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1505 mService.reconfigureDisplayLocked(this);
1506
1507 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001508 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001509 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001510
Andrii Kulian441e4492016-09-29 15:25:00 -07001511 /**
1512 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1513 * bounds were updated.
1514 */
1515 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001516 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1517 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001518 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001519 changedStackList.add(stack.mStackId);
1520 }
1521 }
Winson Chung32c566f2017-04-11 18:31:21 -07001522
1523 // If there was no pinned stack, we still need to notify the controller of the display info
1524 // update as a result of the config change. We do this here to consolidate the flow between
1525 // changes when there is and is not a stack.
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001526 if (getStack(WINDOWING_MODE_PINNED) == null) {
Winson Chung32c566f2017-04-11 18:31:21 -07001527 mPinnedStackControllerLocked.onDisplayInfoChanged();
1528 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001529 }
1530
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001531 @Override
1532 boolean fillsParent() {
1533 return true;
1534 }
1535
1536 @Override
1537 boolean isVisible() {
1538 return true;
1539 }
1540
1541 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001542 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001543 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001544 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001545 }
1546
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001547 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001548 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1549 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1550 // target, or here in order if there isn't an IME target.
1551 if (traverseTopToBottom) {
1552 for (int i = mChildren.size() - 1; i >= 0; --i) {
1553 final DisplayChildWindowContainer child = mChildren.get(i);
1554 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1555 // In this case the Ime windows will be processed above their target so we skip
1556 // here.
1557 continue;
1558 }
1559 if (child.forAllWindows(callback, traverseTopToBottom)) {
1560 return true;
1561 }
1562 }
1563 } else {
1564 final int count = mChildren.size();
1565 for (int i = 0; i < count; i++) {
1566 final DisplayChildWindowContainer child = mChildren.get(i);
1567 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1568 // In this case the Ime windows will be processed above their target so we skip
1569 // here.
1570 continue;
1571 }
1572 if (child.forAllWindows(callback, traverseTopToBottom)) {
1573 return true;
1574 }
1575 }
1576 }
1577 return false;
1578 }
1579
1580 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1581 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1582 }
1583
Wale Ogunwale399c8692017-05-08 14:22:42 -07001584 @Override
1585 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001586 final WindowManagerPolicy policy = mService.mPolicy;
1587
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001588 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001589 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001590 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001591 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001592 // If the display is frozen, some activities may be in the middle of restarting, and
1593 // thus have removed their old window. If the window has the flag to hide the lock
1594 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1595 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001596 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001597 } else if (policy.isKeyguardLocked()) {
1598 // Use the last orientation the while the display is frozen with the keyguard
1599 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1600 // window. We don't want to check the show when locked window directly though as
1601 // things aren't stable while the display is frozen, for example the window could be
1602 // momentarily unavailable due to activity relaunch.
1603 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001604 + "return " + mLastOrientation);
1605 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001606 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001607 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001608 final int orientation = mAboveAppWindowsContainers.getOrientation();
1609 if (orientation != SCREEN_ORIENTATION_UNSET) {
1610 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001611 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001612 }
1613
Wale Ogunwale399c8692017-05-08 14:22:42 -07001614 // Top system windows are not requesting an orientation. Start searching from apps.
1615 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001616 }
1617
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001618 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001619 // Check if display metrics changed and update base values if needed.
1620 updateBaseDisplayMetricsIfNeeded();
1621
Craig Mautner722285e2012-09-07 13:55:58 -07001622 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001623 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001624
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001625 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1626 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001627 }
Craig Mautner722285e2012-09-07 13:55:58 -07001628 }
1629
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001630 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001631 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1632 if (displayManagerInternal != null) {
1633 // Bootstrap the default logical display from the display manager.
1634 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1635 if (newDisplayInfo != null) {
1636 mDisplayInfo.copyFrom(newDisplayInfo);
1637 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001638 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001639
Andrii Kuliancd097992017-03-23 18:31:59 -07001640 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1641 mDisplayInfo.logicalDensityDpi);
1642 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1643 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1644 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001645 }
1646
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001647 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001648 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001649 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001650 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001651 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1652 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001653 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001654 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001655 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001656 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001657 out.set(left, top, left + width, top + height);
1658 }
1659
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001660 private void getLogicalDisplayRect(Rect out, int orientation) {
1661 getLogicalDisplayRect(out);
1662
1663 // Rotate the Rect if needed.
1664 final int currentRotation = mDisplayInfo.rotation;
1665 final int rotationDelta = deltaRotation(currentRotation, orientation);
1666 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1667 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1668 mTmpRectF.set(out);
1669 mTmpMatrix.mapRect(mTmpRectF);
1670 mTmpRectF.round(out);
1671 }
1672 }
1673
Andrii Kuliancd097992017-03-23 18:31:59 -07001674 /**
1675 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1676 * values.
1677 */
1678 private void updateBaseDisplayMetricsIfNeeded() {
1679 // Get real display metrics without overrides from WM.
1680 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1681 final int orientation = mDisplayInfo.rotation;
1682 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1683 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1684 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1685 final int newDensity = mDisplayInfo.logicalDensityDpi;
1686
1687 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1688 || mInitialDisplayHeight != newHeight
1689 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1690
1691 if (displayMetricsChanged) {
1692 // Check if display size or density is forced.
1693 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1694 || mBaseDisplayHeight != mInitialDisplayHeight;
1695 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1696
1697 // If there is an override set for base values - use it, otherwise use new values.
1698 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1699 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1700 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1701
1702 // Real display metrics changed, so we should also update initial values.
1703 mInitialDisplayWidth = newWidth;
1704 mInitialDisplayHeight = newHeight;
1705 mInitialDisplayDensity = newDensity;
1706 mService.reconfigureDisplayLocked(this);
1707 }
1708 }
1709
Bryce Lee27cec322017-03-21 09:41:37 -07001710 /** Sets the maximum width the screen resolution can be */
1711 void setMaxUiWidth(int width) {
1712 if (DEBUG_DISPLAY) {
1713 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1714 }
1715
1716 mMaxUiWidth = width;
1717
1718 // Update existing metrics.
1719 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1720 }
1721
1722 /** Update base (override) display metrics. */
1723 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1724 mBaseDisplayWidth = baseWidth;
1725 mBaseDisplayHeight = baseHeight;
1726 mBaseDisplayDensity = baseDensity;
1727
1728 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1729 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1730 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1731 mBaseDisplayWidth = mMaxUiWidth;
1732
1733 if (DEBUG_DISPLAY) {
1734 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1735 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1736 + " on display:" + getDisplayId());
1737 }
1738 }
1739
1740 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1741 }
1742
Chong Zhangf66db432016-01-13 10:39:51 -08001743 void getContentRect(Rect out) {
1744 out.set(mContentRect);
1745 }
1746
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001747 TaskStack addStackToDisplay(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001748 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1749 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001750
Wale Ogunwale1666e312016-12-16 11:27:18 -08001751 TaskStack stack = getStackById(stackId);
1752 if (stack != null) {
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001753 // It's already attached to the display...clear mDeferRemoval, set controller, and move
1754 // stack to appropriate z-order on display as needed.
Wale Ogunwale1666e312016-12-16 11:27:18 -08001755 stack.mDeferRemoval = false;
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001756 stack.setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001757 // We're not moving the display to front when we're adding stacks, only when
1758 // requested to change the position of stack explicitly.
1759 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1760 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001761 } else {
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001762 stack = new TaskStack(mService, stackId, controller);
Andrii Kulian839def92016-11-02 10:58:58 -07001763 mTaskStackContainers.addStackToDisplay(stack, onTop);
1764 }
1765
Wale Ogunwale1666e312016-12-16 11:27:18 -08001766 if (stackId == DOCKED_STACK_ID) {
1767 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001768 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001769 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001770 }
1771
Andrii Kulian51c1b672017-04-07 18:39:32 -07001772 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001773 final DisplayContent prevDc = stack.getDisplayContent();
1774 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001775 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1776 + " which is not currently attached to any display");
1777 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001778 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001779 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1780 + " to its current displayId=" + mDisplayId);
1781 }
1782
Wale Ogunwale1666e312016-12-16 11:27:18 -08001783 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001784 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001785 }
1786
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001787 @Override
1788 protected void addChild(DisplayChildWindowContainer child,
1789 Comparator<DisplayChildWindowContainer> comparator) {
1790 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1791 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001792
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001793 @Override
1794 protected void addChild(DisplayChildWindowContainer child, int index) {
1795 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1796 }
1797
1798 @Override
1799 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001800 // Only allow removal of direct children from this display if the display is in the process
1801 // of been removed.
1802 if (mRemovingDisplay) {
1803 super.removeChild(child);
1804 return;
1805 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001806 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001807 }
1808
Andrii Kuliand2765632016-12-12 22:26:34 -08001809 @Override
1810 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1811 // Children of the display are statically ordered, so the real intention here is to perform
1812 // the operation on the display and not the static direct children.
1813 getParent().positionChildAt(position, this, includingParents);
1814 }
1815
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001816 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001817 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1818 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001819 final int taskId = stack.taskIdFromPoint(x, y);
1820 if (taskId != -1) {
1821 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001822 }
1823 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001824 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001825 }
1826
Chong Zhang8e89b312015-09-09 15:09:30 -07001827 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001828 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001829 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001830 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001831 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001832 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001833 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001834 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1835 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001836 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001837 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001838 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001839
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001840 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1841 if (mTmpTaskForResizePointSearchResult.searchDone) {
1842 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001843 }
1844 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001845 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001846 }
1847
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001848 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001849 // The provided task is the task on this display with focus, so if WindowManagerService's
1850 // focused app is not on this display, focusedTask will be null.
1851 if (focusedTask == null) {
1852 mTouchExcludeRegion.setEmpty();
1853 } else {
1854 mTouchExcludeRegion.set(mBaseDisplayRect);
1855 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1856 mTmpRect2.setEmpty();
1857 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1858 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1859 stack.setTouchExcludeRegion(
1860 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1861 }
1862 // If we removed the focused task above, add it back and only leave its
1863 // outside touch area in the exclusion. TapDectector is not interested in
1864 // any touch inside the focused task itself.
1865 if (!mTmpRect2.isEmpty()) {
1866 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1867 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001868 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001869 final WindowState inputMethod = mService.mInputMethodWindow;
1870 if (inputMethod != null && inputMethod.isVisibleLw()) {
1871 // If the input method is visible and the user is typing, we don't want these touch
1872 // events to be intercepted and used to change focus. This would likely cause a
1873 // disappearance of the input method.
1874 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001875 if (inputMethod.getDisplayId() == mDisplayId) {
1876 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1877 } else {
1878 // IME is on a different display, so we need to update its tap detector.
1879 // TODO(multidisplay): Remove when IME will always appear on same display.
1880 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1881 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001882 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001883 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1884 WindowState win = mTapExcludedWindows.get(i);
1885 win.getTouchableRegion(mTmpRegion);
1886 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1887 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001888 // TODO(multi-display): Support docked stacks on secondary displays.
1889 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001890 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001891 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001892 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1893 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001894 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001895 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001896 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001897 }
1898
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001899 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001900 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001901 super.switchUser();
1902 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001903 }
1904
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001905 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001906 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1907 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001908 }
1909 }
1910
1911 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001912 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001913 }
1914
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001915 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001916 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001917 }
1918
1919 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001920 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001921 }
1922
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001923 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001924 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001925 }
1926
Wale Ogunwale10124582016-09-15 20:25:50 -07001927 @Override
1928 void removeIfPossible() {
1929 if (isAnimating()) {
1930 mDeferredRemoval = true;
1931 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001932 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001933 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001934 }
1935
Wale Ogunwale10124582016-09-15 20:25:50 -07001936 @Override
1937 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001938 mRemovingDisplay = true;
1939 try {
1940 super.removeImmediately();
1941 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1942 mDimLayerController.close();
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001943 if (mService.canDispatchPointerEvents()) {
1944 if (mTapDetector != null) {
1945 mService.unregisterPointerEventListener(mTapDetector);
1946 }
1947 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1948 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1949 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001950 }
1951 } finally {
1952 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001953 }
Craig Mautner95da1082014-02-24 17:54:35 -08001954 }
1955
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001956 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001957 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001958 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001959 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1960
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001961 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001962 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001963 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001964 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001965 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001966 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001967 }
1968
Andrii Kulian0214ed92017-05-16 13:44:05 -07001969 /** @return 'true' if removal of this display content is deferred due to active animation. */
1970 boolean isRemovalDeferred() {
1971 return mDeferredRemoval;
1972 }
1973
Wale Ogunwale10124582016-09-15 20:25:50 -07001974 boolean animateForIme(float interpolatedValue, float animationTarget,
1975 float dividerAnimationTarget) {
1976 boolean updated = false;
1977
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001978 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1979 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001980 if (stack == null || !stack.isAdjustedForIme()) {
1981 continue;
1982 }
1983
1984 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1985 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1986 updated = true;
1987 } else {
1988 mDividerControllerLocked.mLastAnimationProgress =
1989 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1990 mDividerControllerLocked.mLastDividerProgress =
1991 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1992 updated |= stack.updateAdjustForIme(
1993 mDividerControllerLocked.mLastAnimationProgress,
1994 mDividerControllerLocked.mLastDividerProgress,
1995 false /* force */);
1996 }
1997 if (interpolatedValue >= 1f) {
1998 stack.endImeAdjustAnimation();
1999 }
2000 }
2001
2002 return updated;
2003 }
2004
2005 boolean clearImeAdjustAnimation() {
2006 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002007 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2008 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002009 if (stack != null && stack.isAdjustedForIme()) {
2010 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2011 changed = true;
2012 }
2013 }
2014 return changed;
2015 }
2016
2017 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002018 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2019 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002020 if (stack.isVisible() && stack.isAdjustedForIme()) {
2021 stack.beginImeAdjustAnimation();
2022 }
2023 }
2024 }
2025
2026 void adjustForImeIfNeeded() {
2027 final WindowState imeWin = mService.mInputMethodWindow;
2028 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2029 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002030 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07002031 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2032 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2033 imeTargetStack.getDockSide() : DOCKED_INVALID;
2034 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2035 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2036 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
2037 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
2038 final boolean imeHeightChanged = imeVisible &&
2039 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2040
2041 // The divider could be adjusted for IME position, or be thinner than usual,
2042 // or both. There are three possible cases:
2043 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2044 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2045 // - If IME is not visible, divider is not moved and is normal width.
2046
2047 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002048 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2049 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002050 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002051 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2052 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002053 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2054 } else {
2055 stack.resetAdjustedForIme(false);
2056 }
2057 }
2058 mDividerControllerLocked.setAdjustedForIme(
2059 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2060 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002061 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2062 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002063 stack.resetAdjustedForIme(!dockVisible);
2064 }
2065 mDividerControllerLocked.setAdjustedForIme(
2066 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2067 }
Winson Chung655332c2016-10-31 13:14:28 -07002068 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002069 }
2070
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002071 void setInputMethodAnimLayerAdjustment(int adj) {
2072 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2073 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002074 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002075 }
2076
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002077 /**
2078 * If a window that has an animation specifying a colored background and the current wallpaper
2079 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2080 * suddenly disappear.
2081 */
2082 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002083 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2084 w -> w.mIsWallpaper && w.isVisibleNow());
2085
2086 if (visibleWallpaper != null) {
2087 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002088 }
2089 return winAnimator.mAnimLayer;
2090 }
2091
Wale Ogunwale10124582016-09-15 20:25:50 -07002092 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002093 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2094 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002095 stack.prepareFreezingTaskBounds();
2096 }
2097 }
2098
Wale Ogunwale94744212015-09-21 19:01:47 -07002099 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002100 getLogicalDisplayRect(mTmpRect, newRotation);
2101
2102 // Compute a transform matrix to undo the coordinate space transformation,
2103 // and present the window at the same physical position it previously occupied.
2104 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2105 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2106
2107 mTmpRectF.set(bounds);
2108 mTmpMatrix.mapRect(mTmpRectF);
2109 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002110 }
2111
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002112 static int deltaRotation(int oldRotation, int newRotation) {
2113 int delta = newRotation - oldRotation;
2114 if (delta < 0) delta += 4;
2115 return delta;
2116 }
2117
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002118 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002119 Matrix outMatrix) {
2120 // For rotations without Z-ordering we don't need the target rectangle's position.
2121 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2122 displayHeight, outMatrix);
2123 }
2124
2125 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2126 float displayWidth, float displayHeight, Matrix outMatrix) {
2127 switch (rotation) {
2128 case ROTATION_0:
2129 outMatrix.reset();
2130 break;
2131 case ROTATION_270:
2132 outMatrix.setRotate(270, 0, 0);
2133 outMatrix.postTranslate(0, displayHeight);
2134 outMatrix.postTranslate(rectTop, 0);
2135 break;
2136 case ROTATION_180:
2137 outMatrix.reset();
2138 break;
2139 case ROTATION_90:
2140 outMatrix.setRotate(90, 0, 0);
2141 outMatrix.postTranslate(displayWidth, 0);
2142 outMatrix.postTranslate(-rectTop, rectLeft);
2143 break;
2144 }
2145 }
2146
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002147 @CallSuper
2148 @Override
2149 public void writeToProto(ProtoOutputStream proto, long fieldId) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002150 final long token = proto.start(fieldId);
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002151 super.writeToProto(proto, WINDOW_CONTAINER);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002152 proto.write(ID, mDisplayId);
2153 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2154 final TaskStack stack = mTaskStackContainers.get(stackNdx);
2155 stack.writeToProto(proto, STACKS);
2156 }
2157 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2158 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
2159 for (int i = mAboveAppWindowsContainers.size() - 1; i >= 0; --i) {
2160 final WindowToken windowToken = mAboveAppWindowsContainers.get(i);
2161 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS);
2162 }
2163 for (int i = mBelowAppWindowsContainers.size() - 1; i >= 0; --i) {
2164 final WindowToken windowToken = mBelowAppWindowsContainers.get(i);
2165 windowToken.writeToProto(proto, BELOW_APP_WINDOWS);
2166 }
2167 for (int i = mImeWindowsContainers.size() - 1; i >= 0; --i) {
2168 final WindowToken windowToken = mImeWindowsContainers.get(i);
2169 windowToken.writeToProto(proto, IME_WINDOWS);
2170 }
2171 proto.write(DPI, mBaseDisplayDensity);
2172 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002173 proto.write(ROTATION, mRotation);
2174 final ScreenRotationAnimation screenRotationAnimation =
2175 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2176 if (screenRotationAnimation != null) {
2177 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2178 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002179 proto.end(token);
2180 }
2181
Craig Mautnera91f9e22012-09-14 16:22:08 -07002182 public void dump(String prefix, PrintWriter pw) {
2183 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2184 final String subPrefix = " " + prefix;
2185 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2186 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2187 pw.print("dpi");
2188 if (mInitialDisplayWidth != mBaseDisplayWidth
2189 || mInitialDisplayHeight != mBaseDisplayHeight
2190 || mInitialDisplayDensity != mBaseDisplayDensity) {
2191 pw.print(" base=");
2192 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2193 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2194 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002195 if (mDisplayScalingDisabled) {
2196 pw.println(" noscale");
2197 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002198 pw.print(" cur=");
2199 pw.print(mDisplayInfo.logicalWidth);
2200 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2201 pw.print(" app=");
2202 pw.print(mDisplayInfo.appWidth);
2203 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2204 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2205 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2206 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2207 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002208 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002209 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002210 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002211
Craig Mautnerdc548482014-02-05 13:35:24 -08002212 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002213 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002214 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2215 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002216 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002217 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002218
Craig Mautnerdc548482014-02-05 13:35:24 -08002219 pw.println();
2220 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002221 pw.println();
2222 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002223 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002224 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002225 pw.print(" Exiting #"); pw.print(i);
2226 pw.print(' '); pw.print(token);
2227 pw.println(':');
2228 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002229 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002230 }
Craig Mautner59c00972012-07-30 12:10:24 -07002231 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002232 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002233 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002234 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002235 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002236 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002237
2238 if (mInputMethodAnimLayerAdjustment != 0) {
2239 pw.println(subPrefix
2240 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2241 }
Craig Mautner59c00972012-07-30 12:10:24 -07002242 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002243
2244 @Override
2245 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002246 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002247 }
2248
2249 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002250 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002251 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002252
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002253 /** Checks if stack with provided id is visible on this display. */
2254 boolean isStackVisible(int stackId) {
2255 final TaskStack stack = getStackById(stackId);
2256 return (stack != null && stack.isVisible());
2257 }
2258
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002259 /**
2260 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2261 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002262 TaskStack getDockedStackLocked() {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002263 final TaskStack stack = getStack(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002264 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002265 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002266
2267 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002268 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002269 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002270 */
Jorim Jaggife762342016-10-13 14:33:27 +02002271 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002272 return getStack(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002273 }
2274
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002275 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002276 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002277 final int x = (int) xf;
2278 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002279 final WindowState touchedWin = getWindow(w -> {
2280 final int flags = w.mAttrs.flags;
2281 if (!w.isVisibleLw()) {
2282 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002283 }
2284 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002285 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002286 }
2287
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002288 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002289 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002290 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002291 }
2292
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002293 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002294
2295 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296 return mTmpRegion.contains(x, y) || touchFlags == 0;
2297 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002298
2299 return touchedWin;
2300 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002301
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002302 boolean canAddToastWindowForUid(int uid) {
2303 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002304 // Also if the app is focused adding more than one toast at
2305 // a time for better backwards compatibility.
2306 final WindowState focusedWindowForUid = getWindow(w ->
2307 w.mOwnerUid == uid && w.isFocused());
2308 if (focusedWindowForUid != null) {
2309 return true;
2310 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002311 final WindowState win = getWindow(w ->
2312 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2313 && !w.mWindowRemovalAllowed);
2314 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002315 }
2316
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002317 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002318 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2319 return;
2320 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002321
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002322 // Used to communicate the old focus to the callback method.
2323 mTmpWindow = oldFocus;
2324
2325 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002326 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002327
2328 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002329 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002330
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002331 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332
2333 if (mTmpWindow == null) {
2334 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2335 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002336 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002337 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002338 }
2339
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002340 /** Updates the layer assignment of windows on this display. */
2341 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002342 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002343 if (setLayoutNeeded) {
2344 setLayoutNeeded();
2345 }
2346 }
2347
Wale Ogunwale1666e312016-12-16 11:27:18 -08002348 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2349 // moving containers or resizing them. Need to investigate the best way to have it automatically
2350 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002351 void layoutAndAssignWindowLayersIfNeeded() {
2352 mService.mWindowsChanged = true;
2353 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002354
2355 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2356 false /*updateInputWindows*/)) {
2357 assignWindowLayers(false /* setLayoutNeeded */);
2358 }
2359
2360 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2361 mService.mWindowPlacerLocked.performSurfacePlacement();
2362 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2363 }
2364
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002365 /** Returns true if a leaked surface was destroyed */
2366 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002367 // Used to indicate that a surface was leaked.
2368 mTmpWindow = null;
2369 forAllWindows(w -> {
2370 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002371 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002372 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002373 }
2374 if (!mService.mSessions.contains(wsa.mSession)) {
2375 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002376 + w + " surface=" + wsa.mSurfaceController
2377 + " token=" + w.mToken
2378 + " pid=" + w.mSession.mPid
2379 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002380 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002381 mService.mForceRemoves.add(w);
2382 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002383 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002384 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002385 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002386 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002387 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002388 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002389 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002390 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002391 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002392
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002393 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002394 }
2395
2396 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002397 * Determine and return the window that should be the IME target.
2398 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2399 * @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 +00002400 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002401 WindowState computeImeTarget(boolean updateImeTarget) {
2402 if (mService.mInputMethodWindow == null) {
2403 // There isn't an IME so there shouldn't be a target...That was easy!
2404 if (updateImeTarget) {
2405 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2406 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2407 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2408 }
2409 return null;
2410 }
2411
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002412 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2413 // same display. Or even when the current IME/target are not on the same screen as the next
2414 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002415 mUpdateImeTarget = updateImeTarget;
2416 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002417
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002418
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002419 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2420 // to be on top of it, but it is not -really- where input will go. So look down below
2421 // for a real window to target...
2422 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2423 final AppWindowToken token = target.mAppToken;
2424 if (token != null) {
2425 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2426 if (betterTarget != null) {
2427 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002428 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002429 }
2430 }
2431
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002432 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2433 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002434
2435 // Now, a special case -- if the last target's window is in the process of exiting, and is
2436 // above the new target, keep on the last target to avoid flicker. Consider for example a
2437 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2438 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2439 // scrim.
2440 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002441 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2442 && (target == null
2443 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002444 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002445 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002446 }
2447
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002448 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2449 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002450
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002451 if (target == null) {
2452 if (updateImeTarget) {
2453 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2454 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2455 + Debug.getCallers(4) : ""));
2456 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2457 }
2458
2459 return null;
2460 }
2461
2462 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002463 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2464 if (token != null) {
2465
2466 // Now some fun for dealing with window animations that modify the Z order. We need
2467 // to look at all windows below the current target that are in this app, finding the
2468 // highest visible one in layering.
2469 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002470 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002471 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002472 }
2473
2474 if (highestTarget != null) {
2475 final AppTransition appTransition = mService.mAppTransition;
2476 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2477 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2478 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002479 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002480
2481 if (appTransition.isTransitionSet()) {
2482 // If we are currently setting up for an animation, hold everything until we
2483 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002484 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2485 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002486 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002488 // If the window we are currently targeting is involved with an animation,
2489 // and it is on top of the next target we will be over, then hold off on
2490 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002491 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2492 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002493 }
2494 }
2495 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002496
2497 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2498 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2499 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002500 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002501 }
2502
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002503 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002504 }
2505
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002506 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2507 if (target == mService.mInputMethodTarget
2508 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2509 && mInputMethodAnimLayerAdjustment == layerAdj) {
2510 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002511 }
2512
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002513 mService.mInputMethodTarget = target;
2514 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2515 setInputMethodAnimLayerAdjustment(layerAdj);
2516 assignWindowLayers(false /* setLayoutNeeded */);
2517 }
2518
2519 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2520 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2521 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2522 }
2523
2524 // Used to indicate we have reached the first window in the range we are interested in.
2525 mTmpWindow = null;
2526
2527 // TODO: Figure-out a more efficient way to do this.
2528 final WindowState candidate = getWindow(w -> {
2529 if (w == top) {
2530 // Reached the first window in the range we are interested in.
2531 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002532 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002533 if (mTmpWindow == null) {
2534 return false;
2535 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002536
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002537 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2538 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002539 }
2540 // If we reached the bottom of the range of windows we are considering,
2541 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002542 if (w == bottom) {
2543 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002544 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002545 return false;
2546 });
2547
2548 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002549 }
2550
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002551 void setLayoutNeeded() {
2552 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2553 mLayoutNeeded = true;
2554 }
2555
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002556 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002557 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2558 mLayoutNeeded = false;
2559 }
2560
2561 boolean isLayoutNeeded() {
2562 return mLayoutNeeded;
2563 }
2564
Wale Ogunwale02319a62016-09-26 15:21:22 -07002565 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2566 if (mTokenMap.isEmpty()) {
2567 return;
2568 }
2569 pw.println(" Display #" + mDisplayId);
2570 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2571 while (it.hasNext()) {
2572 final WindowToken token = it.next();
2573 pw.print(" ");
2574 pw.print(token);
2575 if (dumpAll) {
2576 pw.println(':');
2577 token.dump(pw, " ");
2578 } else {
2579 pw.println();
2580 }
2581 }
2582 }
2583
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002584 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002585 final int[] index = new int[1];
2586 forAllWindows(w -> {
2587 final WindowStateAnimator wAnim = w.mWinAnimator;
2588 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2589 index[0] = index[0] + 1;
2590 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002591 }
2592
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002593 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002594 forAllWindows(w -> {
2595 w.mWinAnimator.enableSurfaceTrace(fd);
2596 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002597 }
2598
2599 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002600 forAllWindows(w -> {
2601 w.mWinAnimator.disableSurfaceTrace();
2602 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002603 }
2604
Jorim Jaggife762342016-10-13 14:33:27 +02002605 /**
2606 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2607 */
2608 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2609 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002610 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002611 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2612 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002613 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002614 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2615 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002617 }
2618
Wale Ogunwale494009b82016-10-21 09:01:38 -07002619 boolean checkWaitingForWindows() {
2620
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002621 mHaveBootMsg = false;
2622 mHaveApp = false;
2623 mHaveWallpaper = false;
2624 mHaveKeyguard = true;
2625
2626 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002627 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2628 return true;
2629 }
2630 if (w.isDrawnLw()) {
2631 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002633 } else if (w.mAttrs.type == TYPE_APPLICATION
2634 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002635 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002636 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002637 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002638 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002639 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002640 }
2641 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 return false;
2643 });
2644
2645 if (visibleWindow != null) {
2646 // We have a visible window.
2647 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002648 }
2649
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650 // if the wallpaper service is disabled on the device, we're never going to have
2651 // wallpaper, don't bother waiting for it
2652 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2653 com.android.internal.R.bool.config_enableWallpaperService)
2654 && !mService.mOnlyCore;
2655
Wale Ogunwale494009b82016-10-21 09:01:38 -07002656 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2657 "******** booted=" + mService.mSystemBooted
2658 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002659 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2660 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2661 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002662
2663 // If we are turning on the screen to show the boot message, don't do it until the boot
2664 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002665 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002666 return true;
2667 }
2668
2669 // If we are turning on the screen after the boot is completed normally, don't do so until
2670 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002671 if (mService.mSystemBooted
2672 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002673 return true;
2674 }
2675
2676 return false;
2677 }
2678
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002679 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002680 mTmpWindowAnimator = animator;
2681 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002682 }
2683
2684 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002685 resetAnimationBackgroundAnimator();
2686
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002687 // Used to indicate a detached wallpaper.
2688 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002689 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002690
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002691 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002692
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002693 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002694 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002695 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2696 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002697 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2698 }
2699 }
2700
2701 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002702 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002703 }
2704
Wale Ogunwale494009b82016-10-21 09:01:38 -07002705 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002706 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002707 if (imFocus == null) {
2708 return false;
2709 }
2710
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002711 if (DEBUG_INPUT_METHOD) {
2712 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2713 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2714 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002715 }
2716
Wale Ogunwale494009b82016-10-21 09:01:38 -07002717 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2718
2719 if (DEBUG_INPUT_METHOD) {
2720 Slog.i(TAG_WM, "IM target client: " + imeClient);
2721 if (imeClient != null) {
2722 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2723 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2724 }
2725 }
2726
2727 return imeClient != null && imeClient.asBinder() == client.asBinder();
2728 }
2729
2730 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002731 final WindowState win = getWindow(
2732 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2733 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002734 }
2735
2736 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002737 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002738 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002739 final int curValue = w.mSystemUiVisibility;
2740 final int diff = (curValue ^ visibility) & globalDiff;
2741 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002742 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002743 w.mSeq++;
2744 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002745 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002746 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2747 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002748 visibility, newValue, diff);
2749 }
2750 } catch (RemoteException e) {
2751 // so sorry
2752 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002753 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002754 }
2755
2756 void onWindowFreezeTimeout() {
2757 Slog.w(TAG_WM, "Window freeze timeout expired.");
2758 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002759
2760 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002761 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002762 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002763 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002764 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002765 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2766 - mService.mDisplayFreezeTime);
2767 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002768 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002769 mService.mWindowPlacerLocked.performSurfacePlacement();
2770 }
2771
2772 void waitForAllWindowsDrawn() {
2773 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002774 forAllWindows(w -> {
2775 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2776 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2777 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002778 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002779 w.mLastContentInsets.set(-1, -1, -1, -1);
2780 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002781 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002782 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002783 }
2784
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002785 // TODO: Super crazy long method that should be broken down...
2786 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2787
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002788 final int dw = mDisplayInfo.logicalWidth;
2789 final int dh = mDisplayInfo.logicalHeight;
2790 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2791
2792 mTmpUpdateAllDrawn.clear();
2793
2794 int repeats = 0;
2795 do {
2796 repeats++;
2797 if (repeats > 6) {
2798 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2799 clearLayoutNeeded();
2800 break;
2801 }
2802
2803 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2804 pendingLayoutChanges);
2805
Andrii Kulian839def92016-11-02 10:58:58 -07002806 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2807 // the wallpaper window jumping across displays.
2808 // Remove check for default display when there will be support for multiple wallpaper
2809 // targets (on different displays).
2810 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002811 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002812 }
2813
2814 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2815 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2816 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2817 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002818 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002819 }
2820 }
2821
2822 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2823 setLayoutNeeded();
2824 }
2825
2826 // FIRST LOOP: Perform a layout, if needed.
2827 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2828 performLayout(repeats == 1, false /* updateInputWindows */);
2829 } else {
2830 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2831 }
2832
2833 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2834 pendingLayoutChanges = 0;
2835
2836 if (isDefaultDisplay) {
2837 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002838 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002839 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2840 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2841 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2842 }
2843 } while (pendingLayoutChanges != 0);
2844
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002845 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002846 resetDimming();
2847
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
2857 stopDimmingIfNeeded();
2858
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002859 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2860 if (wallpaperVisible != mLastWallpaperVisible) {
2861 mLastWallpaperVisible = wallpaperVisible;
2862 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2863 }
2864
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002865 while (!mTmpUpdateAllDrawn.isEmpty()) {
2866 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2867 // See if any windows have been drawn, so they (and others associated with them)
2868 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002869 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002870 }
2871
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002872 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002873 }
2874
2875 void performLayout(boolean initial, boolean updateInputWindows) {
2876 if (!isLayoutNeeded()) {
2877 return;
2878 }
2879 clearLayoutNeeded();
2880
2881 final int dw = mDisplayInfo.logicalWidth;
2882 final int dh = mDisplayInfo.logicalHeight;
2883
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002884 if (DEBUG_LAYOUT) {
2885 Slog.v(TAG, "-------------------------------------");
2886 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2887 }
2888
Andrii Kulian8ee72852017-03-10 10:36:45 -08002889 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002890 getConfiguration().uiMode);
2891 if (isDefaultDisplay) {
2892 // Not needed on non-default displays.
2893 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2894 mService.mScreenRect.set(0, 0, dw, dh);
2895 }
2896
2897 mService.mPolicy.getContentRectLw(mContentRect);
2898
2899 int seq = mService.mLayoutSeq + 1;
2900 if (seq < 0) seq = 0;
2901 mService.mLayoutSeq = seq;
2902
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002903 // Used to indicate that we have processed the dream window and all additional windows are
2904 // behind it.
2905 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002906 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002907
2908 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002909 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002910
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002911 // Used to indicate that we have processed the dream window and all additional attached
2912 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002913 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002914 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002915
2916 // Now perform layout of attached windows, which usually depend on the position of the
2917 // window they are attached to. XXX does not deal with windows that are attached to windows
2918 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002919 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002920
2921 // Window frames may have changed. Tell the input dispatcher about it.
2922 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2923 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2924 if (updateInputWindows) {
2925 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2926 }
2927
2928 mService.mPolicy.finishLayoutLw();
2929 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2930 }
2931
2932 /**
2933 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2934 * In portrait mode, it grabs the full screenshot.
2935 *
2936 * @param width the width of the target bitmap
2937 * @param height the height of the target bitmap
2938 * @param includeFullDisplay true if the screen should not be cropped before capture
2939 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2940 * @param config of the output bitmap
2941 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002942 * @param includeDecor whether to include window decors, like the status or navigation bar
2943 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002944 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002945 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002946 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002947 boolean wallpaperOnly, boolean includeDecor) {
2948 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2949 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002950 if (bitmap == null) {
2951 return null;
2952 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002953
2954 if (DEBUG_SCREENSHOT) {
2955 // TEST IF IT's ALL BLACK
2956 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2957 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2958 bitmap.getHeight());
2959 boolean allBlack = true;
2960 final int firstColor = buffer[0];
2961 for (int i = 0; i < buffer.length; i++) {
2962 if (buffer[i] != firstColor) {
2963 allBlack = false;
2964 break;
2965 }
2966 }
2967 if (allBlack) {
2968 final WindowState appWin = mScreenshotApplicationState.appWin;
2969 final int maxLayer = mScreenshotApplicationState.maxLayer;
2970 final int minLayer = mScreenshotApplicationState.minLayer;
2971 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2972 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2973 (appWin != null ?
2974 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2975 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2976 }
2977 }
2978
2979 // Create a copy of the screenshot that is immutable and backed in ashmem.
2980 // This greatly reduces the overhead of passing the bitmap between processes.
2981 Bitmap ret = bitmap.createAshmemBitmap(config);
2982 bitmap.recycle();
2983 return ret;
2984 }
2985
2986 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2987 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2988 boolean includeDecor) {
2989 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2990 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2991 }
2992
2993 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2994 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2995 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002996 int dw = mDisplayInfo.logicalWidth;
2997 int dh = mDisplayInfo.logicalHeight;
2998 if (dw == 0 || dh == 0) {
2999 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3000 + ": returning null. logical widthxheight=" + dw + "x" + dh);
3001 return null;
3002 }
3003
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003004 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003005
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003006 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003007 final Rect frame = new Rect();
3008 final Rect stackBounds = new Rect();
3009
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003010 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003011 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08003012 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003013 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02003014 if (!mService.mPolicy.isScreenOn()) {
3015 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
3016 + " was off.");
3017 return null;
3018 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003019 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003020 mScreenshotApplicationState.appWin = null;
3021 forAllWindows(w -> {
3022 if (!w.mHasSurface) {
3023 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003024 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003025 if (w.mLayer >= aboveAppLayer) {
3026 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003027 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 if (wallpaperOnly && !w.mIsWallpaper) {
3029 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003030 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003031 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003032 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003033 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003034 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3035 // then the target window state is this one.
3036 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003037 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003038 }
3039
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003040 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003041 // We have not ran across the target window yet, so it is probably behind
3042 // the wallpaper. This can happen when the keyguard is up and all windows
3043 // are moved behind the wallpaper. We don't want to include the wallpaper
3044 // layer in the screenshot as it will cover-up the layer of the target
3045 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003046 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003047 }
3048 // Fall through. The target window is in front of the wallpaper. For this
3049 // case we want to include the wallpaper layer in the screenshot because
3050 // the target window might have some transparent areas.
3051 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003052 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003053 // This app window is of no interest if it is not associated with the
3054 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003055 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003056 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003057 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003058 }
3059
3060 // Include this window.
3061
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003062 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003063 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003064 if (mScreenshotApplicationState.maxLayer < layer) {
3065 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003066 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003067 if (mScreenshotApplicationState.minLayer > layer) {
3068 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003069 }
3070
3071 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003072 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3073 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003074 final Task task = w.getTask();
3075 if (task != null) {
3076 task.getBounds(frame);
3077 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003078
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003079 // No task bounds? Too bad! Ain't no screenshot then.
3080 return true;
3081 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003082 } else {
3083 final Rect wf = w.mFrame;
3084 final Rect cr = w.mContentInsets;
3085 int left = wf.left + cr.left;
3086 int top = wf.top + cr.top;
3087 int right = wf.right - cr.right;
3088 int bottom = wf.bottom - cr.bottom;
3089 frame.union(left, top, right, bottom);
3090 w.getVisibleBounds(stackBounds);
3091 if (!Rect.intersects(frame, stackBounds)) {
3092 // Set frame empty if there's no intersection.
3093 frame.setEmpty();
3094 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003095 }
3096 }
3097
3098 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003099 (w.mAppToken != null && w.mAppToken.token == appToken)
3100 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi406386a82017-09-15 14:17:40 +02003101 if (foundTargetWs && winAnim.getShown() && winAnim.mLastAlpha > 0f) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003102 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003103 }
3104
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003105 if (w.isObscuringDisplay()){
3106 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003107 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003108 return false;
3109 }, true /* traverseTopToBottom */);
3110
3111 final WindowState appWin = mScreenshotApplicationState.appWin;
3112 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3113 final int maxLayer = mScreenshotApplicationState.maxLayer;
3114 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003115
3116 if (appToken != null && appWin == null) {
3117 // Can't find a window to snapshot.
3118 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3119 "Screenshot: Couldn't find a surface matching " + appToken);
3120 return null;
3121 }
3122
3123 if (!screenshotReady) {
3124 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3125 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3126 appWin.mWinAnimator.mDrawState)));
3127 return null;
3128 }
3129
3130 // Screenshot is ready to be taken. Everything from here below will continue
3131 // through the bottom of the loop and return a value. We only stay in the loop
3132 // because we don't want to release the mWindowMap lock until the screenshot is
3133 // taken.
3134
3135 if (maxLayer == 0) {
3136 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3137 + ": returning null maxLayer=" + maxLayer);
3138 return null;
3139 }
3140
Jorim Jaggi02886a82016-12-06 09:10:06 -08003141 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003142 // Constrain frame to the screen size.
3143 if (!frame.intersect(0, 0, dw, dh)) {
3144 frame.setEmpty();
3145 }
3146 } else {
3147 // Caller just wants entire display.
3148 frame.set(0, 0, dw, dh);
3149 }
3150 if (frame.isEmpty()) {
3151 return null;
3152 }
3153
3154 if (width < 0) {
3155 width = (int) (frame.width() * frameScale);
3156 }
3157 if (height < 0) {
3158 height = (int) (frame.height() * frameScale);
3159 }
3160
3161 // Tell surface flinger what part of the image to crop. Take the top
3162 // right part of the application, and crop the larger dimension to fit.
3163 Rect crop = new Rect(frame);
3164 if (width / (float) frame.width() < height / (float) frame.height()) {
3165 int cropWidth = (int)((float)width / (float)height * frame.height());
3166 crop.right = crop.left + cropWidth;
3167 } else {
3168 int cropHeight = (int)((float)height / (float)width * frame.width());
3169 crop.bottom = crop.top + cropHeight;
3170 }
3171
3172 // The screenshot API does not apply the current screen rotation.
3173 int rot = mDisplay.getRotation();
3174
3175 if (rot == ROTATION_90 || rot == ROTATION_270) {
3176 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3177 }
3178
3179 // Surfaceflinger is not aware of orientation, so convert our logical
3180 // crop to surfaceflinger's portrait orientation.
3181 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3182
3183 if (DEBUG_SCREENSHOT) {
3184 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3185 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003186 forAllWindows(w -> {
3187 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3188 Slog.i(TAG_WM, w + ": " + w.mLayer
3189 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003190 + " surfaceLayer=" + ((controller == null)
3191 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003192 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003193 }
3194
3195 final ScreenRotationAnimation screenRotationAnimation =
3196 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3197 final boolean inRotation = screenRotationAnimation != null &&
3198 screenRotationAnimation.isAnimating();
3199 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3200 "Taking screenshot while rotating");
3201
3202 // We force pending transactions to flush before taking
3203 // the screenshot by pushing an empty synchronous transaction.
3204 SurfaceControl.openTransaction();
3205 SurfaceControl.closeTransactionSync();
3206
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003207 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003208 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003209 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003210 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3211 + ") to layer " + maxLayer);
3212 return null;
3213 }
3214 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003215 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003216 }
3217
3218 // TODO: Can this use createRotationMatrix()?
3219 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3220 if (rot == Surface.ROTATION_90) {
3221 final int tmp = crop.top;
3222 crop.top = dw - crop.right;
3223 crop.right = crop.bottom;
3224 crop.bottom = dw - crop.left;
3225 crop.left = tmp;
3226 } else if (rot == Surface.ROTATION_180) {
3227 int tmp = crop.top;
3228 crop.top = dh - crop.bottom;
3229 crop.bottom = dh - tmp;
3230 tmp = crop.right;
3231 crop.right = dw - crop.left;
3232 crop.left = dw - tmp;
3233 } else if (rot == Surface.ROTATION_270) {
3234 final int tmp = crop.top;
3235 crop.top = crop.left;
3236 crop.left = dh - crop.bottom;
3237 crop.bottom = crop.right;
3238 crop.right = dh - tmp;
3239 }
3240 }
3241
3242 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003243 // Used to indicate the layout is needed.
3244 mTmpWindow = null;
3245
3246 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003247 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003248 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003249 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003250 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003251 w.setDisplayLayoutNeeded();
3252 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003253 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003254
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003255 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003256 mService.mWindowPlacerLocked.performSurfacePlacement();
3257 }
3258 }
3259
Wale Ogunwale1666e312016-12-16 11:27:18 -08003260 void setExitingTokensHasVisible(boolean hasVisible) {
3261 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3262 mExitingTokens.get(i).hasVisible = hasVisible;
3263 }
3264
3265 // Initialize state of exiting applications.
3266 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3267 }
3268
3269 void removeExistingTokensIfPossible() {
3270 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3271 final WindowToken token = mExitingTokens.get(i);
3272 if (!token.hasVisible) {
3273 mExitingTokens.remove(i);
3274 }
3275 }
3276
3277 // Time to remove any exiting applications?
3278 mTaskStackContainers.removeExistingAppTokensIfPossible();
3279 }
3280
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003281 @Override
3282 void onDescendantOverrideConfigurationChanged() {
3283 setLayoutNeeded();
3284 mService.requestTraversal();
3285 }
3286
David Stevens9440dc82017-03-16 19:00:20 -07003287 boolean okToDisplay() {
3288 if (mDisplayId == DEFAULT_DISPLAY) {
3289 return !mService.mDisplayFrozen
3290 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3291 }
3292 return mDisplayInfo.state == Display.STATE_ON;
3293 }
3294
3295 boolean okToAnimate() {
3296 return okToDisplay() &&
3297 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3298 }
3299
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003300 static final class TaskForResizePointSearchResult {
3301 boolean searchDone;
3302 Task taskForResize;
3303
3304 void reset() {
3305 searchDone = false;
3306 taskForResize = null;
3307 }
3308 }
Robert Carr3b716242016-08-16 16:02:21 -07003309
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003310 private static final class ApplySurfaceChangesTransactionState {
3311 boolean displayHasContent;
3312 boolean obscured;
3313 boolean syswin;
3314 boolean focusDisplayed;
3315 float preferredRefreshRate;
3316 int preferredModeId;
3317
3318 void reset() {
3319 displayHasContent = false;
3320 obscured = false;
3321 syswin = false;
3322 focusDisplayed = false;
3323 preferredRefreshRate = 0;
3324 preferredModeId = 0;
3325 }
3326 }
3327
3328 private static final class ScreenshotApplicationState {
3329 WindowState appWin;
3330 int maxLayer;
3331 int minLayer;
3332 boolean screenshotReady;
3333
3334 void reset(boolean screenshotReady) {
3335 appWin = null;
3336 maxLayer = 0;
3337 minLayer = 0;
3338 this.screenshotReady = screenshotReady;
3339 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3340 }
3341 }
3342
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003343 /**
3344 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3345 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3346 * homogeneous children type which is currently required by sub-classes of
3347 * {@link WindowContainer} class.
3348 */
3349 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3350
3351 int size() {
3352 return mChildren.size();
3353 }
3354
3355 E get(int index) {
3356 return mChildren.get(index);
3357 }
3358
3359 @Override
3360 boolean fillsParent() {
3361 return true;
3362 }
3363
3364 @Override
3365 boolean isVisible() {
3366 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003367 }
3368 }
3369
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003370 /**
3371 * Window container class that contains all containers on this display relating to Apps.
3372 * I.e Activities.
3373 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003374 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003375
Andrii Kulian839def92016-11-02 10:58:58 -07003376 /**
3377 * Adds the stack to this container.
3378 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3379 */
3380 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale68278562017-09-23 17:13:55 -07003381 if (stack.isActivityTypeHome()) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003382 if (mHomeStack != null) {
3383 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3384 }
3385 mHomeStack = stack;
3386 }
3387 addChild(stack, onTop);
3388 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003389 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003390
Andrii Kulian839def92016-11-02 10:58:58 -07003391 /** Removes the stack from its container and prepare for changing the parent. */
3392 void removeStackFromDisplay(TaskStack stack) {
3393 removeChild(stack);
3394 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003395 }
3396
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003397 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003398 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3399 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003400 addChild(stack, addIndex);
3401 setLayoutNeeded();
3402 }
3403
Bryce Lee00d586d2017-07-28 20:48:43 -07003404
3405 @Override
3406 boolean isOnTop() {
3407 // Considered always on top
3408 return true;
3409 }
3410
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003411 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003412 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003413 if (child.getWindowConfiguration().isAlwaysOnTop()
3414 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003415 // This stack is always-on-top, override the default behavior.
3416 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3417
3418 // Moving to its current position, as we must call super but we don't want to
3419 // perform any meaningful action.
3420 final int currentPosition = mChildren.indexOf(child);
3421 super.positionChildAt(currentPosition, child, false /* includingParents */);
3422 return;
3423 }
3424
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003425 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3426 super.positionChildAt(targetPosition, child, includingParents);
3427
3428 setLayoutNeeded();
3429 }
3430
3431 /**
3432 * When stack is added or repositioned, find a proper position for it.
3433 * This will make sure that pinned stack always stays on top.
3434 * @param requestedPosition Position requested by caller.
3435 * @param stack Stack to be added or positioned.
3436 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3437 * @return The proper position for the stack.
3438 */
3439 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3440 final int topChildPosition = mChildren.size() - 1;
3441 boolean toTop = requestedPosition == POSITION_TOP;
3442 toTop |= adding ? requestedPosition >= topChildPosition + 1
3443 : requestedPosition >= topChildPosition;
3444 int targetPosition = requestedPosition;
3445
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003446 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED
3447 && getStack(WINDOWING_MODE_PINNED) != null) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003448 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003449 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003450 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003451 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3452 }
3453
3454 // So, stack is moved just below the pinned stack.
3455 // When we're adding a new stack the target is the current pinned stack position.
3456 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003457 // stack, because WindowContainer#positionAt() first removes element and then adds
3458 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003459 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3460 }
3461
3462 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003463 }
3464
3465 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003466 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3467 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003468 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003469 if (super.forAllWindows(callback, traverseTopToBottom)) {
3470 return true;
3471 }
3472 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3473 return true;
3474 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003475 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003476 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3477 return true;
3478 }
3479 if (super.forAllWindows(callback, traverseTopToBottom)) {
3480 return true;
3481 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003482 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003483 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003484 }
3485
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003486 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003487 boolean traverseTopToBottom) {
3488 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3489 // app tokens.
3490 // TODO: Investigate if we need to continue to do this or if we can just process them
3491 // in-order.
3492 if (traverseTopToBottom) {
3493 for (int i = mChildren.size() - 1; i >= 0; --i) {
3494 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3495 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003496 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3497 traverseTopToBottom)) {
3498 return true;
3499 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003500 }
3501 }
3502 } else {
3503 final int count = mChildren.size();
3504 for (int i = 0; i < count; ++i) {
3505 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3506 final int appTokensCount = appTokens.size();
3507 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003508 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3509 traverseTopToBottom)) {
3510 return true;
3511 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003512 }
3513 }
3514 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003515 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003516 }
3517
Wale Ogunwale1666e312016-12-16 11:27:18 -08003518 void setExitingTokensHasVisible(boolean hasVisible) {
3519 for (int i = mChildren.size() - 1; i >= 0; --i) {
3520 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3521 for (int j = appTokens.size() - 1; j >= 0; --j) {
3522 appTokens.get(j).hasVisible = hasVisible;
3523 }
3524 }
3525 }
3526
3527 void removeExistingAppTokensIfPossible() {
3528 for (int i = mChildren.size() - 1; i >= 0; --i) {
3529 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3530 for (int j = appTokens.size() - 1; j >= 0; --j) {
3531 final AppWindowToken token = appTokens.get(j);
3532 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3533 && (!token.mIsExiting || token.isEmpty())) {
3534 // Make sure there is no animation running on this token, so any windows
3535 // associated with it will be removed as soon as their animations are
3536 // complete.
3537 token.mAppAnimator.clearAnimation();
3538 token.mAppAnimator.animating = false;
3539 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3540 "performLayout: App token exiting now removed" + token);
3541 token.removeIfPossible();
3542 }
3543 }
3544 }
3545 }
3546
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003547 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003548 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003549 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003550 // Apps and their containers are not allowed to specify an orientation while the
3551 // docked or freeform stack is visible...except for the home stack/task if the
3552 // docked stack is minimized and it actually set something.
3553 if (mHomeStack != null && mHomeStack.isVisible()
3554 && mDividerControllerLocked.isMinimizedDock()) {
3555 final int orientation = mHomeStack.getOrientation();
3556 if (orientation != SCREEN_ORIENTATION_UNSET) {
3557 return orientation;
3558 }
3559 }
3560 return SCREEN_ORIENTATION_UNSPECIFIED;
3561 }
3562
3563 final int orientation = super.getOrientation();
3564 if (orientation != SCREEN_ORIENTATION_UNSET
3565 && orientation != SCREEN_ORIENTATION_BEHIND) {
3566 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3567 "App is requesting an orientation, return " + orientation);
3568 return orientation;
3569 }
3570
3571 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003572 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003573 // The next app has not been requested to be visible, so we keep the current orientation
3574 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003575 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003576 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003577 }
3578
3579 /**
3580 * Window container class that contains all containers on this display that are not related to
3581 * Apps. E.g. status bar.
3582 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003583 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3584 /**
3585 * Compares two child window tokens returns -1 if the first is lesser than the second in
3586 * terms of z-order and 1 otherwise.
3587 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003588 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003589 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003590 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3591 token1.mOwnerCanManageAppTokens)
3592 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3593 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003594
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003595 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3596 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3597 return false;
3598 }
3599 final int req = w.mAttrs.screenOrientation;
3600 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3601 || req == SCREEN_ORIENTATION_UNSET) {
3602 return false;
3603 }
3604 return true;
3605 };
3606
Wale Ogunwale3a931692016-11-02 16:49:48 -07003607 private final String mName;
3608 NonAppWindowContainers(String name) {
3609 mName = name;
3610 }
3611
3612 void addChild(WindowToken token) {
3613 addChild(token, mWindowComparator);
3614 }
3615
3616 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003617 int getOrientation() {
3618 final WindowManagerPolicy policy = mService.mPolicy;
3619 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003620 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003621
3622 if (win != null) {
3623 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003624 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003625 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003626 if (mService.mKeyguardGoingAway) {
3627 // Keyguard can't affect the orientation if it is going away...
3628 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3629 return SCREEN_ORIENTATION_UNSET;
3630 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003631 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003632 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003633 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003634 }
3635
Andrii Kulian8ee72852017-03-10 10:36:45 -08003636 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003637
Jorim Jaggi75520d52017-08-24 17:23:19 +02003638 if (policy.isKeyguardShowingAndNotOccluded()
3639 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003640 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003641 }
3642
3643 return SCREEN_ORIENTATION_UNSET;
3644 }
3645
3646 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003647 String getName() {
3648 return mName;
3649 }
Robert Carr3b716242016-08-16 16:02:21 -07003650 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003651
3652 /**
3653 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3654 */
3655 @FunctionalInterface
3656 private interface Screenshoter<E> {
3657 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3658 boolean useIdentityTransform, int rotation);
3659 }
Craig Mautner59c00972012-07-30 12:10:24 -07003660}