blob: 2e7f8221594bf28b419cf460a641c615dc40f922 [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 Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.HOME_STACK_ID;
22import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
25import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070026import static android.view.Display.DEFAULT_DISPLAY;
27import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070028import static android.view.Surface.ROTATION_0;
29import static android.view.Surface.ROTATION_180;
30import static android.view.Surface.ROTATION_270;
31import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070032import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070033import static android.view.WindowManager.DOCKED_BOTTOM;
34import static android.view.WindowManager.DOCKED_INVALID;
35import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080036import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
37import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
38import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070039import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070040import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070041import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
42import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070043import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070045import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070046import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070047import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070049import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
50import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070052import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
53import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070054import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070055import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070056import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
58import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070059import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080060import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070061import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070062import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070063import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
64import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070076import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070077import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070078import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070079import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080080import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070081import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070082import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
83import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070084import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070085import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070086import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
87import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
89import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
90import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070091import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070092import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070093import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070094import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070095import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070096import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -070097import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070098import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070099import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700100import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
101import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
102import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
103import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
104import static com.android.server.wm.proto.DisplayProto.DPI;
105import static com.android.server.wm.proto.DisplayProto.ID;
106import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
107import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
108import static com.android.server.wm.proto.DisplayProto.STACKS;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700109
Andrii Kulian3a507b52016-09-19 18:14:12 -0700110import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700111import android.app.ActivityManager.StackId;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700112import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700113import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700114import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100115import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700116import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800117import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700118import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700119import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100120import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700121import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700122import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700123import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700124import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700125import android.os.RemoteException;
126import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700127import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800128import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700129import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700130import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700131import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700132import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700133import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700134import android.view.Surface;
135import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700136import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700137
Bryce Lee48f4b572017-04-10 10:54:15 -0700138import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800139import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700140import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700141
Robert Carr3b716242016-08-16 16:02:21 -0700142import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700143import java.io.PrintWriter;
144import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700145import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700146import java.util.HashMap;
147import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700148import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700149import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800150import java.util.function.Consumer;
151import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700152
Craig Mautner59c00972012-07-30 12:10:24 -0700153/**
154 * Utility class for keeping track of the WindowStates and other pertinent contents of a
155 * particular Display.
156 *
157 * IMPORTANT: No method from this class should ever be used without holding
158 * WindowManagerService.mWindowMap.
159 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700160class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700161 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700162
163 /** Unique identifier of this stack. */
164 private final int mDisplayId;
165
Wale Ogunwale3a931692016-11-02 16:49:48 -0700166 /** The containers below are the only child containers the display can have. */
167 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700168 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700169 // Contains all non-app window containers that should be displayed above the app containers
170 // (e.g. Status bar)
171 private final NonAppWindowContainers mAboveAppWindowsContainers =
172 new NonAppWindowContainers("mAboveAppWindowsContainers");
173 // Contains all non-app window containers that should be displayed below the app containers
174 // (e.g. Wallpaper).
175 private final NonAppWindowContainers mBelowAppWindowsContainers =
176 new NonAppWindowContainers("mBelowAppWindowsContainers");
177 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
178 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
179 // window containers together and move them in-sync if/when needed.
180 private final NonAppWindowContainers mImeWindowsContainers =
181 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700182
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000183 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800184 private WindowState mTmpWindow2;
185 private WindowAnimator mTmpWindowAnimator;
186 private boolean mTmpRecoveringMemory;
187 private boolean mUpdateImeTarget;
188 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700189 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700190
Wale Ogunwale02319a62016-09-26 15:21:22 -0700191 // Mapping from a token IBinder to a WindowToken object on this display.
192 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
193
Andrii Kuliancd097992017-03-23 18:31:59 -0700194 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700195 int mInitialDisplayWidth = 0;
196 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700197 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700198
199 /**
200 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
201 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
202 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
203 */
Craig Mautner59c00972012-07-30 12:10:24 -0700204 int mBaseDisplayWidth = 0;
205 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700206 /**
207 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
208 * but can be set from Settings or via shell command "adb shell wm density".
209 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
210 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700211 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700212 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700213 private final DisplayInfo mDisplayInfo = new DisplayInfo();
214 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700215 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700216 /**
217 * For default display it contains real metrics, empty for others.
218 * @see WindowManagerService#createWatermarkInTransaction()
219 */
220 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
221 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
222 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700223
Andrii Kulian06d07d62017-03-14 11:11:47 -0700224 /**
225 * Compat metrics computed based on {@link #mDisplayMetrics}.
226 * @see #updateDisplayAndOrientation(int)
227 */
228 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
229
230 /** The desired scaling factor for compatible apps. */
231 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700232
Andrii Kulian8ee72852017-03-10 10:36:45 -0800233 /**
234 * Current rotation of the display.
235 * Constants as per {@link android.view.Surface.Rotation}.
236 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700237 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800238 */
239 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700240
Andrii Kulian8ee72852017-03-10 10:36:45 -0800241 /**
242 * Last applied orientation of the display.
243 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
244 *
245 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
246 */
247 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700248
Andrii Kulian8ee72852017-03-10 10:36:45 -0800249 /**
250 * Flag indicating that the application is receiving an orientation that has different metrics
251 * than it expected. E.g. Portrait instead of Landscape.
252 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700253 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800254 */
255 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700256
Andrii Kulian8ee72852017-03-10 10:36:45 -0800257 /**
258 * Orientation forced by some window. If there is no visible window that specifies orientation
259 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
260 *
261 * @see NonAppWindowContainers#getOrientation()
262 */
263 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700264
Andrii Kulian8ee72852017-03-10 10:36:45 -0800265 /**
266 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
267 * occluded.
268 *
269 * @see NonAppWindowContainers#getOrientation()
270 */
271 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
272
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700273 /**
274 * Keep track of wallpaper visibility to notify changes.
275 */
276 private boolean mLastWallpaperVisible = false;
277
Andrii Kulian06d07d62017-03-14 11:11:47 -0700278 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700279 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700280
Craig Mautner39834192012-09-02 07:47:24 -0700281 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700282 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700283 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700284 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800285 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700286
Craig Mautnerdc548482014-02-05 13:35:24 -0800287 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700288 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800289
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800290 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
291 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700292 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700293
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700294 /** Detect user tapping outside of current focused task bounds .*/
295 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700296
Craig Mautner6601b7b2013-04-29 10:29:11 -0700297 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700298 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700299
Craig Mautner6601b7b2013-04-29 10:29:11 -0700300 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800301 private final Rect mTmpRect = new Rect();
302 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700303 private final RectF mTmpRectF = new RectF();
304 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800305 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700306
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800307 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700308
Craig Mautner95da1082014-02-24 17:54:35 -0800309 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700310 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800311
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700312 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700313 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700314
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800315 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700316
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800317 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
318
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000319 private boolean mHaveBootMsg = false;
320 private boolean mHaveApp = false;
321 private boolean mHaveWallpaper = false;
322 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700323
324 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
325
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700326 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
327 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000328 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
329 new ApplySurfaceChangesTransactionState();
330 private final ScreenshotApplicationState mScreenshotApplicationState =
331 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700332
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700333 // True if this display is in the process of being removed. Used to determine if the removal of
334 // the display's direct children should be allowed.
335 private boolean mRemovingDisplay = false;
336
Bryce Leed1871262017-06-12 14:12:29 -0700337 // {@code false} if this display is in the processing of being created.
338 private boolean mDisplayReady = false;
339
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700340 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800341 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700342 int mInputMethodAnimLayerAdjustment;
343
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800344 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
345 WindowStateAnimator winAnimator = w.mWinAnimator;
346 if (winAnimator.hasSurface()) {
347 final boolean wasAnimating = winAnimator.mWasAnimating;
348 final boolean nowAnimating = winAnimator.stepAnimationLocked(
349 mTmpWindowAnimator.mCurrentTime);
350 winAnimator.mWasAnimating = nowAnimating;
351 mTmpWindowAnimator.orAnimating(nowAnimating);
352
353 if (DEBUG_WALLPAPER) Slog.v(TAG,
354 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
355
356 if (wasAnimating && !winAnimator.mAnimating
357 && mWallpaperController.isWallpaperTarget(w)) {
358 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
359 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
360 if (DEBUG_LAYOUT_REPEATS) {
361 mService.mWindowPlacerLocked.debugLayoutRepeats(
362 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
363 }
364 }
365 }
366
367 final AppWindowToken atoken = w.mAppToken;
368 if (winAnimator.mDrawState == READY_TO_SHOW) {
369 if (atoken == null || atoken.allDrawn) {
370 if (w.performShowLocked()) {
371 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
372 if (DEBUG_LAYOUT_REPEATS) {
373 mService.mWindowPlacerLocked.debugLayoutRepeats(
374 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
375 }
376 }
377 }
378 }
379 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
380 if (appAnimator != null && appAnimator.thumbnail != null) {
381 if (appAnimator.thumbnailTransactionSeq
382 != mTmpWindowAnimator.mAnimTransactionSequence) {
383 appAnimator.thumbnailTransactionSeq =
384 mTmpWindowAnimator.mAnimTransactionSequence;
385 appAnimator.thumbnailLayer = 0;
386 }
387 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
388 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
389 }
390 }
391 };
392
393 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
394 final WindowStateAnimator winAnimator = w.mWinAnimator;
395 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
396 return;
397 }
398
399 final int flags = w.mAttrs.flags;
400
401 // If this window is animating, make a note that we have an animating window and take
402 // care of a request to run a detached wallpaper animation.
403 if (winAnimator.mAnimating) {
404 if (winAnimator.mAnimation != null) {
405 if ((flags & FLAG_SHOW_WALLPAPER) != 0
406 && winAnimator.mAnimation.getDetachWallpaper()) {
407 mTmpWindow = w;
408 }
409 final int color = winAnimator.mAnimation.getBackgroundColor();
410 if (color != 0) {
411 final TaskStack stack = w.getStack();
412 if (stack != null) {
413 stack.setAnimationBackground(winAnimator, color);
414 }
415 }
416 }
417 mTmpWindowAnimator.setAnimating(true);
418 }
419
420 // If this window's app token is running a detached wallpaper animation, make a note so
421 // we can ensure the wallpaper is displayed behind it.
422 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
423 if (appAnimator != null && appAnimator.animation != null
424 && appAnimator.animating) {
425 if ((flags & FLAG_SHOW_WALLPAPER) != 0
426 && appAnimator.animation.getDetachWallpaper()) {
427 mTmpWindow = w;
428 }
429
430 final int color = appAnimator.animation.getBackgroundColor();
431 if (color != 0) {
432 final TaskStack stack = w.getStack();
433 if (stack != null) {
434 stack.setAnimationBackground(winAnimator, color);
435 }
436 }
437 }
438 };
439
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800440 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
441 final int lostFocusUid = mTmpWindow.mOwnerUid;
442 final Handler handler = mService.mH;
443 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
444 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
445 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
446 w.mAttrs.hideTimeoutMilliseconds);
447 }
448 }
449 };
450
451 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
452 final AppWindowToken focusedApp = mService.mFocusedApp;
453 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
454 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
455
456 if (!w.canReceiveKeys()) {
457 return false;
458 }
459
460 final AppWindowToken wtoken = w.mAppToken;
461
462 // If this window's application has been removed, just skip it.
463 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
464 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
465 + (wtoken.removed ? "removed" : "sendingToBottom"));
466 return false;
467 }
468
469 if (focusedApp == null) {
470 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
471 + " using new focus @ " + w);
472 mTmpWindow = w;
473 return true;
474 }
475
476 if (!focusedApp.windowsAreFocusable()) {
477 // Current focused app windows aren't focusable...
478 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
479 + " focusable using new focus @ " + w);
480 mTmpWindow = w;
481 return true;
482 }
483
484 // Descend through all of the app tokens and find the first that either matches
485 // win.mAppToken (return win) or mFocusedApp (return null).
486 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
487 if (focusedApp.compareTo(wtoken) > 0) {
488 // App stack below focused app stack. No focus for you!!!
489 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
490 "findFocusedWindow: Reached focused app=" + focusedApp);
491 mTmpWindow = null;
492 return true;
493 }
494 }
495
496 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
497 mTmpWindow = w;
498 return true;
499 };
500
501 private final Consumer<WindowState> mPrepareWindowSurfaces =
502 w -> w.mWinAnimator.prepareSurfaceLocked(true);
503
504 private final Consumer<WindowState> mPerformLayout = w -> {
505 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
506 // wasting time and funky changes while a window is animating away.
507 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
508 || w.isGoneForLayoutLw();
509
510 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
511 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
512 + " mLayoutAttached=" + w.mLayoutAttached
513 + " screen changed=" + w.isConfigChanged());
514 final AppWindowToken atoken = w.mAppToken;
515 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
516 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
517 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
518 + " parentHidden=" + w.isParentWindowHidden());
519 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
520 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
521 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
522 + " parentHidden=" + w.isParentWindowHidden());
523 }
524
525 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
526 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
527 // since that means "perform layout as normal, just don't display").
528 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
529 || ((w.isConfigChanged() || w.setReportResizeHints())
530 && !w.isGoneForLayoutLw() &&
531 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
532 (w.mHasSurface && w.mAppToken != null &&
533 w.mAppToken.layoutConfigChanges)))) {
534 if (!w.mLayoutAttached) {
535 if (mTmpInitial) {
536 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
537 w.mContentChanged = false;
538 }
539 if (w.mAttrs.type == TYPE_DREAM) {
540 // Don't layout windows behind a dream, so that if it does stuff like hide
541 // the status bar we won't get a bad transition when it goes away.
542 mTmpWindow = w;
543 }
544 w.mLayoutNeeded = false;
545 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200546 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800547 mService.mPolicy.layoutWindowLw(w, null);
548 w.mLayoutSeq = mService.mLayoutSeq;
549
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200550 // If this is the first layout, we need to initialize the last inset values as
551 // otherwise we'd immediately cause an unnecessary resize.
552 if (firstLayout) {
553 w.updateLastInsetValues();
554 }
555
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800556 // Window frames may have changed. Update dim layer with the new bounds.
557 final Task task = w.getTask();
558 if (task != null) {
559 mDimLayerController.updateDimLayer(task);
560 }
561
562 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
563 + " mContainingFrame=" + w.mContainingFrame
564 + " mDisplayFrame=" + w.mDisplayFrame);
565 }
566 }
567 };
568
569 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
570 if (w.mLayoutAttached) {
571 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
572 + " mViewVisibility=" + w.mViewVisibility
573 + " mRelayoutCalled=" + w.mRelayoutCalled);
574 // If this view is GONE, then skip it -- keep the current frame, and let the caller
575 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
576 // windows, since that means "perform layout as normal, just don't display").
577 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
578 return;
579 }
580 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
581 || w.mLayoutNeeded) {
582 if (mTmpInitial) {
583 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
584 w.mContentChanged = false;
585 }
586 w.mLayoutNeeded = false;
587 w.prelayout();
588 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
589 w.mLayoutSeq = mService.mLayoutSeq;
590 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
591 + " mContainingFrame=" + w.mContainingFrame
592 + " mDisplayFrame=" + w.mDisplayFrame);
593 }
594 } else if (w.mAttrs.type == TYPE_DREAM) {
595 // Don't layout windows behind a dream, so that if it does stuff like hide the
596 // status bar we won't get a bad transition when it goes away.
597 mTmpWindow = mTmpWindow2;
598 }
599 };
600
601 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
602 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
603 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
604 return w.canBeImeTarget();
605 };
606
607 private final Consumer<WindowState> mApplyPostLayoutPolicy =
608 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
609 mService.mInputMethodTarget);
610
611 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
612 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
613 final boolean obscuredChanged = w.mObscured !=
614 mTmpApplySurfaceChangesTransactionState.obscured;
615 final RootWindowContainer root = mService.mRoot;
616 // Only used if default window
617 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
618
619 // Update effect.
620 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
621 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
622 final boolean isDisplayed = w.isDisplayedLw();
623
624 if (isDisplayed && w.isObscuringDisplay()) {
625 // This window completely covers everything behind it, so we want to leave all
626 // of them as undimmed (for performance reasons).
627 root.mObscuringWindow = w;
628 mTmpApplySurfaceChangesTransactionState.obscured = true;
629 }
630
631 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
632 root.handleNotObscuredLocked(w,
633 mTmpApplySurfaceChangesTransactionState.obscured,
634 mTmpApplySurfaceChangesTransactionState.syswin);
635
636 if (w.mHasSurface && isDisplayed) {
637 final int type = w.mAttrs.type;
638 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
639 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
640 mTmpApplySurfaceChangesTransactionState.syswin = true;
641 }
642 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
643 && w.mAttrs.preferredRefreshRate != 0) {
644 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
645 = w.mAttrs.preferredRefreshRate;
646 }
647 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
648 && w.mAttrs.preferredDisplayModeId != 0) {
649 mTmpApplySurfaceChangesTransactionState.preferredModeId
650 = w.mAttrs.preferredDisplayModeId;
651 }
652 }
653 }
654
655 w.applyDimLayerIfNeeded();
656
657 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
658 && mWallpaperController.isWallpaperTarget(w)) {
659 // This is the wallpaper target and its obscured state changed... make sure the
660 // current wallpaper's visibility has been updated accordingly.
661 mWallpaperController.updateWallpaperVisibility();
662 }
663
664 w.handleWindowMovedIfNeeded();
665
666 final WindowStateAnimator winAnimator = w.mWinAnimator;
667
668 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
669 w.mContentChanged = false;
670
671 // Moved from updateWindowsAndWallpaperLocked().
672 if (w.mHasSurface) {
673 // Take care of the window being ready to display.
674 final boolean committed = winAnimator.commitFinishDrawingLocked();
675 if (isDefaultDisplay && committed) {
676 if (w.mAttrs.type == TYPE_DREAM) {
677 // HACK: When a dream is shown, it may at that point hide the lock screen.
678 // So we need to redo the layout to let the phone window manager make this
679 // happen.
680 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
681 if (DEBUG_LAYOUT_REPEATS) {
682 surfacePlacer.debugLayoutRepeats(
683 "dream and commitFinishDrawingLocked true",
684 pendingLayoutChanges);
685 }
686 }
687 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
688 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
689 "First draw done in potential wallpaper target " + w);
690 root.mWallpaperMayChange = true;
691 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
692 if (DEBUG_LAYOUT_REPEATS) {
693 surfacePlacer.debugLayoutRepeats(
694 "wallpaper and commitFinishDrawingLocked true",
695 pendingLayoutChanges);
696 }
697 }
698 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700699 final TaskStack stack = w.getStack();
700 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
701 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800702 // Updates the shown frame before we set up the surface. This is needed
703 // because the resizing could change the top-left position (in addition to
704 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
705 // position the surface.
706 //
707 // If an animation is being started, we can't call this method because the
708 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700709 // we do want to update the position if the window is animating. We make an exception
710 // for the bounds animating state, where an application may have been waiting
711 // for an exit animation to start, but instead enters PiP. We need to ensure
712 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800713 winAnimator.computeShownFrameLocked();
714 }
715 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
716 }
717
718 final AppWindowToken atoken = w.mAppToken;
719 if (atoken != null) {
720 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
721 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
722 mTmpUpdateAllDrawn.add(atoken);
723 }
724 }
725
726 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
727 && w.isDisplayedLw()) {
728 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
729 }
730
731 w.updateResizingWindowIfNeeded();
732 };
733
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800734 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800735 * Create new {@link DisplayContent} instance, add itself to the root window container and
736 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700737 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800738 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700739 * @param layersController window layer controller used to assign layer to the windows on this
740 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700741 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
742 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700743 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700744 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700745 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800746 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
747 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
748 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
749 + " new=" + display);
750 }
751
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700752 mDisplay = display;
753 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700754 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700755 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700756 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700757 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700758 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700759 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700760 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800761 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700762 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800763 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700764
765 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700766 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700767 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700768 super.addChild(mAboveAppWindowsContainers, null);
769 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800770
771 // Add itself as a child to the root container.
772 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700773
774 // TODO(b/62541591): evaluate whether this is the best spot to declare the
775 // {@link DisplayContent} ready for use.
776 mDisplayReady = true;
777 }
778
779 boolean isReady() {
780 // The display is ready when the system and the individual display are both ready.
781 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700782 }
783
784 int getDisplayId() {
785 return mDisplayId;
786 }
787
Wale Ogunwale02319a62016-09-26 15:21:22 -0700788 WindowToken getWindowToken(IBinder binder) {
789 return mTokenMap.get(binder);
790 }
791
792 AppWindowToken getAppWindowToken(IBinder binder) {
793 final WindowToken token = getWindowToken(binder);
794 if (token == null) {
795 return null;
796 }
797 return token.asAppWindowToken();
798 }
799
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700800 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700801 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
802 if (dc != null) {
803 // We currently don't support adding a window token to the display if the display
804 // already has the binder mapped to another token. If there is a use case for supporting
805 // this moving forward we will either need to merge the WindowTokens some how or have
806 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700807 throw new IllegalArgumentException("Can't map token=" + token + " to display="
808 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700809 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700810 if (binder == null) {
811 throw new IllegalArgumentException("Can't map token=" + token + " to display="
812 + getName() + " binder is null");
813 }
814 if (token == null) {
815 throw new IllegalArgumentException("Can't map null token to display="
816 + getName() + " binder=" + binder);
817 }
818
Wale Ogunwale02319a62016-09-26 15:21:22 -0700819 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700820
821 if (token.asAppWindowToken() == null) {
822 // Add non-app token to container hierarchy on the display. App tokens are added through
823 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700824 switch (token.windowType) {
825 case TYPE_WALLPAPER:
826 mBelowAppWindowsContainers.addChild(token);
827 break;
828 case TYPE_INPUT_METHOD:
829 case TYPE_INPUT_METHOD_DIALOG:
830 mImeWindowsContainers.addChild(token);
831 break;
832 default:
833 mAboveAppWindowsContainers.addChild(token);
834 break;
835 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700836 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700837 }
838
839 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700840 final WindowToken token = mTokenMap.remove(binder);
841 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800842 token.setExiting();
843 }
844 return token;
845 }
846
847 /** Changes the display the input window token is housed on to this one. */
848 void reParentWindowToken(WindowToken token) {
849 final DisplayContent prevDc = token.getDisplayContent();
850 if (prevDc == this) {
851 return;
852 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800853 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
854 && token.asAppWindowToken() == null) {
855 // Removed the token from the map, but made sure it's not an app token before removing
856 // from parent.
857 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700858 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800859
860 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700861 }
862
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700863 void removeAppToken(IBinder binder) {
864 final WindowToken token = removeWindowToken(binder);
865 if (token == null) {
866 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
867 return;
868 }
869
870 final AppWindowToken appToken = token.asAppWindowToken();
871
872 if (appToken == null) {
873 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
874 return;
875 }
876
877 appToken.onRemovedFromDisplay();
878 }
879
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700880 Display getDisplay() {
881 return mDisplay;
882 }
883
Craig Mautner59c00972012-07-30 12:10:24 -0700884 DisplayInfo getDisplayInfo() {
885 return mDisplayInfo;
886 }
887
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700888 DisplayMetrics getDisplayMetrics() {
889 return mDisplayMetrics;
890 }
891
Andrii Kulian8ee72852017-03-10 10:36:45 -0800892 int getRotation() {
893 return mRotation;
894 }
895
896 void setRotation(int newRotation) {
897 mRotation = newRotation;
898 }
899
900 int getLastOrientation() {
901 return mLastOrientation;
902 }
903
904 void setLastOrientation(int orientation) {
905 mLastOrientation = orientation;
906 }
907
908 boolean getAltOrientation() {
909 return mAltOrientation;
910 }
911
912 void setAltOrientation(boolean altOrientation) {
913 mAltOrientation = altOrientation;
914 }
915
916 int getLastWindowForcedOrientation() {
917 return mLastWindowForcedOrientation;
918 }
919
Andrii Kulian06d07d62017-03-14 11:11:47 -0700920 /**
921 * Update rotation of the display.
922 *
923 * Returns true if the rotation has been changed. In this case YOU MUST CALL
924 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
925 */
926 boolean updateRotationUnchecked(boolean inTransaction) {
927 if (mService.mDeferredRotationPauseCount > 0) {
928 // Rotation updates have been paused temporarily. Defer the update until
929 // updates have been resumed.
930 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
931 return false;
932 }
933
934 ScreenRotationAnimation screenRotationAnimation =
935 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
936 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
937 // Rotation updates cannot be performed while the previous rotation change
938 // animation is still in progress. Skip this update. We will try updating
939 // again after the animation is finished and the display is unfrozen.
940 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
941 return false;
942 }
943 if (mService.mDisplayFrozen) {
944 // Even if the screen rotation animation has finished (e.g. isAnimating
945 // returns false), there is still some time where we haven't yet unfrozen
946 // the display. We also need to abort rotation here.
947 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
948 "Deferring rotation, still finishing previous rotation");
949 return false;
950 }
951
952 if (!mService.mDisplayEnabled) {
953 // No point choosing a rotation if the display is not enabled.
954 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
955 return false;
956 }
957
958 final int oldRotation = mRotation;
959 final int lastOrientation = mLastOrientation;
960 final boolean oldAltOrientation = mAltOrientation;
961 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700962 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
963 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700964
Robert Carr897215d2017-03-29 12:25:50 -0700965 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700966 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
967 if (seamlessRotated != null) {
968 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
969 // to complete (that is, waiting for windows to redraw). It's tempting to check
970 // w.mSeamlessRotationCount but that could be incorrect in the case of
971 // window-removal.
972 return false;
973 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700974 }
975
976 // TODO: Implement forced rotation changes.
977 // Set mAltOrientation to indicate that the application is receiving
978 // an orientation that has different metrics than it expected.
979 // eg. Portrait instead of Landscape.
980
981 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
982 lastOrientation, rotation);
983
984 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
985 + ", got rotation " + rotation + " which has "
986 + (altOrientation ? "incompatible" : "compatible") + " metrics");
987
988 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
989 // No change.
990 return false;
991 }
992
993 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
994 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
995 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
996
997 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
998 mService.mWaitingForConfig = true;
999 }
1000
1001 mRotation = rotation;
1002 mAltOrientation = altOrientation;
1003 if (isDefaultDisplay) {
1004 mService.mPolicy.setRotationLw(rotation);
1005 }
1006
1007 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1008 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1009 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1010 WINDOW_FREEZE_TIMEOUT_DURATION);
1011
1012 setLayoutNeeded();
1013 final int[] anim = new int[2];
1014 if (isDimming()) {
1015 anim[0] = anim[1] = 0;
1016 } else {
1017 mService.mPolicy.selectRotationAnimationLw(anim);
1018 }
1019
1020 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001021 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001022 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1023 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1024 mDisplayId);
1025 } else {
1026 // The screen rotation animation uses a screenshot to freeze the screen
1027 // while windows resize underneath.
1028 // When we are rotating seamlessly, we allow the elements to transition
1029 // to their rotated state independently and without a freeze required.
1030 screenRotationAnimation = null;
1031
1032 // We have to reset this in case a window was removed before it
1033 // finished seamless rotation.
1034 mService.mSeamlessRotationCount = 0;
1035 }
1036
1037 // We need to update our screen size information to match the new rotation. If the rotation
1038 // has actually changed then this method will return true and, according to the comment at
1039 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1040 // By updating the Display info here it will be available to
1041 // #computeScreenConfiguration() later.
1042 updateDisplayAndOrientation(getConfiguration().uiMode);
1043
1044 if (!inTransaction) {
1045 if (SHOW_TRANSACTIONS) {
1046 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1047 }
1048 mService.openSurfaceTransaction();
1049 }
1050 try {
1051 // NOTE: We disable the rotation in the emulator because
1052 // it doesn't support hardware OpenGL emulation yet.
1053 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1054 && screenRotationAnimation.hasScreenshot()) {
1055 if (screenRotationAnimation.setRotationInTransaction(
1056 rotation, mService.mFxSession,
1057 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1058 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1059 mService.scheduleAnimationLocked();
1060 }
1061 }
1062
1063 if (rotateSeamlessly) {
1064 forAllWindows(w -> {
1065 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1066 }, true /* traverseTopToBottom */);
1067 }
1068
1069 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1070 } finally {
1071 if (!inTransaction) {
1072 mService.closeSurfaceTransaction();
1073 if (SHOW_LIGHT_TRANSACTIONS) {
1074 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1075 }
1076 }
1077 }
1078
1079 forAllWindows(w -> {
1080 // Discard surface after orientation change, these can't be reused.
1081 if (w.mAppToken != null) {
1082 w.mAppToken.destroySavedSurfaces();
1083 }
1084 if (w.mHasSurface && !rotateSeamlessly) {
1085 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001086 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001087 mService.mRoot.mOrientationChangeComplete = false;
1088 w.mLastFreezeDuration = 0;
1089 }
1090 w.mReportOrientationChanged = true;
1091 }, true /* traverseTopToBottom */);
1092
1093 if (rotateSeamlessly) {
1094 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1095 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1096 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1097 }
1098
1099 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1100 final WindowManagerService.RotationWatcher rotationWatcher
1101 = mService.mRotationWatchers.get(i);
1102 if (rotationWatcher.mDisplayId == mDisplayId) {
1103 try {
1104 rotationWatcher.mWatcher.onRotationChanged(rotation);
1105 } catch (RemoteException e) {
1106 // Ignore
1107 }
1108 }
1109 }
1110
1111 // TODO (multi-display): Magnification is supported only for the default display.
1112 // Announce rotation only if we will not animate as we already have the
1113 // windows in final state. Otherwise, we make this call at the rotation end.
1114 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1115 && isDefaultDisplay) {
1116 mService.mAccessibilityController.onRotationChangedLocked(this);
1117 }
1118
1119 return true;
1120 }
1121
1122 /**
1123 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1124 * changed.
1125 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1126 */
1127 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1128 // Use the effective "visual" dimensions based on current rotation
1129 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1130 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1131 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1132 int dw = realdw;
1133 int dh = realdh;
1134
1135 if (mAltOrientation) {
1136 if (realdw > realdh) {
1137 // Turn landscape into portrait.
1138 int maxw = (int)(realdh/1.3f);
1139 if (maxw < realdw) {
1140 dw = maxw;
1141 }
1142 } else {
1143 // Turn portrait into landscape.
1144 int maxh = (int)(realdw/1.3f);
1145 if (maxh < realdh) {
1146 dh = maxh;
1147 }
1148 }
1149 }
1150
1151 // Update application display metrics.
1152 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1153 mDisplayId);
1154 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1155 mDisplayId);
1156 mDisplayInfo.rotation = mRotation;
1157 mDisplayInfo.logicalWidth = dw;
1158 mDisplayInfo.logicalHeight = dh;
1159 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1160 mDisplayInfo.appWidth = appWidth;
1161 mDisplayInfo.appHeight = appHeight;
1162 if (isDefaultDisplay) {
1163 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1164 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1165 }
1166 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1167 if (mDisplayScalingDisabled) {
1168 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1169 } else {
1170 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1171 }
1172
1173 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1174 mDisplayInfo);
1175
1176 mBaseDisplayRect.set(0, 0, dw, dh);
1177
1178 if (isDefaultDisplay) {
1179 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1180 mCompatDisplayMetrics);
1181 }
1182 return mDisplayInfo;
1183 }
1184
1185 /**
1186 * Compute display configuration based on display properties and policy settings.
1187 * Do not call if mDisplayReady == false.
1188 */
1189 void computeScreenConfiguration(Configuration config) {
1190 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1191
1192 final int dw = displayInfo.logicalWidth;
1193 final int dh = displayInfo.logicalHeight;
1194 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1195 Configuration.ORIENTATION_LANDSCAPE;
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001196
Andrii Kulian06d07d62017-03-14 11:11:47 -07001197 config.screenWidthDp =
1198 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1199 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1200 config.screenHeightDp =
1201 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1202 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001203
1204 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1205 final int leftInset = mTmpRect.left;
1206 final int topInset = mTmpRect.top;
1207 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001208 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1209 leftInset + displayInfo.appWidth /* right */,
1210 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001211 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1212 || displayInfo.rotation == Surface.ROTATION_270);
1213
1214 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1215 mDisplayMetrics.density, config);
1216
1217 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1218 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1219 ? Configuration.SCREENLAYOUT_ROUND_YES
1220 : Configuration.SCREENLAYOUT_ROUND_NO);
1221
1222 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1223 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1224 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1225 dh, mDisplayId);
1226 config.densityDpi = displayInfo.logicalDensityDpi;
1227
1228 config.colorMode =
1229 (displayInfo.isHdr()
1230 ? Configuration.COLOR_MODE_HDR_YES
1231 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001232 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001233 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1234 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1235
1236 // Update the configuration based on available input devices, lid switch,
1237 // and platform configuration.
1238 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1239 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1240 config.navigation = Configuration.NAVIGATION_NONAV;
1241
1242 int keyboardPresence = 0;
1243 int navigationPresence = 0;
1244 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1245 final int len = devices != null ? devices.length : 0;
1246 for (int i = 0; i < len; i++) {
1247 InputDevice device = devices[i];
1248 if (!device.isVirtual()) {
1249 final int sources = device.getSources();
1250 final int presenceFlag = device.isExternal() ?
1251 WindowManagerPolicy.PRESENCE_EXTERNAL :
1252 WindowManagerPolicy.PRESENCE_INTERNAL;
1253
1254 // TODO(multi-display): Configure on per-display basis.
1255 if (mService.mIsTouchDevice) {
1256 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1257 InputDevice.SOURCE_TOUCHSCREEN) {
1258 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1259 }
1260 } else {
1261 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1262 }
1263
1264 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1265 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1266 navigationPresence |= presenceFlag;
1267 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1268 && config.navigation == Configuration.NAVIGATION_NONAV) {
1269 config.navigation = Configuration.NAVIGATION_DPAD;
1270 navigationPresence |= presenceFlag;
1271 }
1272
1273 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1274 config.keyboard = Configuration.KEYBOARD_QWERTY;
1275 keyboardPresence |= presenceFlag;
1276 }
1277 }
1278 }
1279
1280 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1281 config.navigation = Configuration.NAVIGATION_DPAD;
1282 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1283 }
1284
1285 // Determine whether a hard keyboard is available and enabled.
1286 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1287 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1288 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1289 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1290 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1291 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1292 }
1293
1294 // Let the policy update hidden states.
1295 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1296 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1297 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1298 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1299 }
1300
1301 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1302 int displayId) {
1303 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1304 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1305 final int unrotDw, unrotDh;
1306 if (rotated) {
1307 unrotDw = dh;
1308 unrotDh = dw;
1309 } else {
1310 unrotDw = dw;
1311 unrotDh = dh;
1312 }
1313 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1314 displayId);
1315 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1316 displayId);
1317 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1318 displayId);
1319 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1320 displayId);
1321 return sw;
1322 }
1323
1324 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1325 DisplayMetrics dm, int dw, int dh, int displayId) {
1326 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1327 displayId);
1328 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1329 uiMode, displayId);
1330 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1331 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1332 if (curSize == 0 || size < curSize) {
1333 curSize = size;
1334 }
1335 return curSize;
1336 }
1337
1338 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1339 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1340
1341 // We need to determine the smallest width that will occur under normal
1342 // operation. To this, start with the base screen size and compute the
1343 // width under the different possible rotations. We need to un-rotate
1344 // the current screen dimensions before doing this.
1345 int unrotDw, unrotDh;
1346 if (rotated) {
1347 unrotDw = dh;
1348 unrotDh = dw;
1349 } else {
1350 unrotDw = dw;
1351 unrotDh = dh;
1352 }
1353 displayInfo.smallestNominalAppWidth = 1<<30;
1354 displayInfo.smallestNominalAppHeight = 1<<30;
1355 displayInfo.largestNominalAppWidth = 0;
1356 displayInfo.largestNominalAppHeight = 0;
1357 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1358 unrotDh);
1359 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1360 unrotDw);
1361 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1362 unrotDh);
1363 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1364 unrotDw);
1365 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1366 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1367 displayId);
1368 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1369 displayId);
1370 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1371 displayId);
1372 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1373 displayId);
1374 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1375 outConfig.screenLayout = sl;
1376 }
1377
1378 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1379 int uiMode, int displayId) {
1380 // Get the app screen size at this rotation.
1381 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1382 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1383
1384 // Compute the screen layout size class for this rotation.
1385 int longSize = w;
1386 int shortSize = h;
1387 if (longSize < shortSize) {
1388 int tmp = longSize;
1389 longSize = shortSize;
1390 shortSize = tmp;
1391 }
1392 longSize = (int)(longSize/density);
1393 shortSize = (int)(shortSize/density);
1394 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1395 }
1396
1397 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1398 int uiMode, int dw, int dh) {
1399 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1400 displayId);
1401 if (width < displayInfo.smallestNominalAppWidth) {
1402 displayInfo.smallestNominalAppWidth = width;
1403 }
1404 if (width > displayInfo.largestNominalAppWidth) {
1405 displayInfo.largestNominalAppWidth = width;
1406 }
1407 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1408 displayId);
1409 if (height < displayInfo.smallestNominalAppHeight) {
1410 displayInfo.smallestNominalAppHeight = height;
1411 }
1412 if (height > displayInfo.largestNominalAppHeight) {
1413 displayInfo.largestNominalAppHeight = height;
1414 }
1415 }
1416
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001417 DockedStackDividerController getDockedDividerController() {
1418 return mDividerControllerLocked;
1419 }
1420
Winson Chung655332c2016-10-31 13:14:28 -07001421 PinnedStackController getPinnedStackController() {
1422 return mPinnedStackControllerLocked;
1423 }
1424
Jeff Browna506a6e2013-06-04 00:02:38 -07001425 /**
1426 * Returns true if the specified UID has access to this display.
1427 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001428 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001429 return mDisplay.hasAccess(uid);
1430 }
1431
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001432 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001433 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001434 }
1435
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001436 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001437 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001438 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001439 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001440 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001441 }
1442
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001443 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001444 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1445 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001446 if (stack.mStackId == stackId) {
1447 return stack;
1448 }
1449 }
1450 return null;
1451 }
1452
Bryce Lee48f4b572017-04-10 10:54:15 -07001453 @VisibleForTesting
1454 int getStackCount() {
1455 return mTaskStackContainers.size();
1456 }
1457
1458 @VisibleForTesting
Wale Ogunwale98d62312017-07-12 09:24:56 -07001459 int getStackPosById(int stackId) {
Bryce Lee48f4b572017-04-10 10:54:15 -07001460 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1461 final TaskStack stack = mTaskStackContainers.get(i);
1462 if (stack.mStackId == stackId) {
1463 return i;
1464 }
1465 }
1466 return -1;
1467 }
1468
Andrii Kulian441e4492016-09-29 15:25:00 -07001469 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001470 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001471 super.onConfigurationChanged(newParentConfig);
1472
Andrii Kulian3a507b52016-09-19 18:14:12 -07001473 // The display size information is heavily dependent on the resources in the current
1474 // configuration, so we need to reconfigure it every time the configuration changes.
1475 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1476 mService.reconfigureDisplayLocked(this);
1477
1478 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001479 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001480 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001481
Andrii Kulian441e4492016-09-29 15:25:00 -07001482 /**
1483 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1484 * bounds were updated.
1485 */
1486 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001487 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1488 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001489 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001490 changedStackList.add(stack.mStackId);
1491 }
1492 }
Winson Chung32c566f2017-04-11 18:31:21 -07001493
1494 // If there was no pinned stack, we still need to notify the controller of the display info
1495 // update as a result of the config change. We do this here to consolidate the flow between
1496 // changes when there is and is not a stack.
1497 if (getStackById(PINNED_STACK_ID) == null) {
1498 mPinnedStackControllerLocked.onDisplayInfoChanged();
1499 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001500 }
1501
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001502 @Override
1503 boolean fillsParent() {
1504 return true;
1505 }
1506
1507 @Override
1508 boolean isVisible() {
1509 return true;
1510 }
1511
1512 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001513 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001514 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001515 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001516 }
1517
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001518 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001519 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1520 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1521 // target, or here in order if there isn't an IME target.
1522 if (traverseTopToBottom) {
1523 for (int i = mChildren.size() - 1; i >= 0; --i) {
1524 final DisplayChildWindowContainer child = mChildren.get(i);
1525 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1526 // In this case the Ime windows will be processed above their target so we skip
1527 // here.
1528 continue;
1529 }
1530 if (child.forAllWindows(callback, traverseTopToBottom)) {
1531 return true;
1532 }
1533 }
1534 } else {
1535 final int count = mChildren.size();
1536 for (int i = 0; i < count; i++) {
1537 final DisplayChildWindowContainer child = mChildren.get(i);
1538 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1539 // In this case the Ime windows will be processed above their target so we skip
1540 // here.
1541 continue;
1542 }
1543 if (child.forAllWindows(callback, traverseTopToBottom)) {
1544 return true;
1545 }
1546 }
1547 }
1548 return false;
1549 }
1550
1551 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1552 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1553 }
1554
Wale Ogunwale399c8692017-05-08 14:22:42 -07001555 @Override
1556 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001557 final WindowManagerPolicy policy = mService.mPolicy;
1558
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001559 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001560 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001561 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001562 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001563 // If the display is frozen, some activities may be in the middle of restarting, and
1564 // thus have removed their old window. If the window has the flag to hide the lock
1565 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1566 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001567 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001568 } else if (policy.isKeyguardLocked()) {
1569 // Use the last orientation the while the display is frozen with the keyguard
1570 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1571 // window. We don't want to check the show when locked window directly though as
1572 // things aren't stable while the display is frozen, for example the window could be
1573 // momentarily unavailable due to activity relaunch.
1574 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001575 + "return " + mLastOrientation);
1576 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001577 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001578 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001579 final int orientation = mAboveAppWindowsContainers.getOrientation();
1580 if (orientation != SCREEN_ORIENTATION_UNSET) {
1581 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001582 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001583 }
1584
Wale Ogunwale399c8692017-05-08 14:22:42 -07001585 // Top system windows are not requesting an orientation. Start searching from apps.
1586 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001587 }
1588
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001589 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001590 // Check if display metrics changed and update base values if needed.
1591 updateBaseDisplayMetricsIfNeeded();
1592
Craig Mautner722285e2012-09-07 13:55:58 -07001593 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001594 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001595
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001596 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1597 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001598 }
Craig Mautner722285e2012-09-07 13:55:58 -07001599 }
1600
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001601 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001602 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1603 if (displayManagerInternal != null) {
1604 // Bootstrap the default logical display from the display manager.
1605 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1606 if (newDisplayInfo != null) {
1607 mDisplayInfo.copyFrom(newDisplayInfo);
1608 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001609 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001610
Andrii Kuliancd097992017-03-23 18:31:59 -07001611 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1612 mDisplayInfo.logicalDensityDpi);
1613 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1614 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1615 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001616 }
1617
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001618 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001619 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001620 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001621 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001622 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1623 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001624 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001625 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001626 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001627 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001628 out.set(left, top, left + width, top + height);
1629 }
1630
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001631 private void getLogicalDisplayRect(Rect out, int orientation) {
1632 getLogicalDisplayRect(out);
1633
1634 // Rotate the Rect if needed.
1635 final int currentRotation = mDisplayInfo.rotation;
1636 final int rotationDelta = deltaRotation(currentRotation, orientation);
1637 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1638 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1639 mTmpRectF.set(out);
1640 mTmpMatrix.mapRect(mTmpRectF);
1641 mTmpRectF.round(out);
1642 }
1643 }
1644
Andrii Kuliancd097992017-03-23 18:31:59 -07001645 /**
1646 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1647 * values.
1648 */
1649 private void updateBaseDisplayMetricsIfNeeded() {
1650 // Get real display metrics without overrides from WM.
1651 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1652 final int orientation = mDisplayInfo.rotation;
1653 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1654 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1655 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1656 final int newDensity = mDisplayInfo.logicalDensityDpi;
1657
1658 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1659 || mInitialDisplayHeight != newHeight
1660 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1661
1662 if (displayMetricsChanged) {
1663 // Check if display size or density is forced.
1664 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1665 || mBaseDisplayHeight != mInitialDisplayHeight;
1666 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1667
1668 // If there is an override set for base values - use it, otherwise use new values.
1669 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1670 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1671 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1672
1673 // Real display metrics changed, so we should also update initial values.
1674 mInitialDisplayWidth = newWidth;
1675 mInitialDisplayHeight = newHeight;
1676 mInitialDisplayDensity = newDensity;
1677 mService.reconfigureDisplayLocked(this);
1678 }
1679 }
1680
Bryce Lee27cec322017-03-21 09:41:37 -07001681 /** Sets the maximum width the screen resolution can be */
1682 void setMaxUiWidth(int width) {
1683 if (DEBUG_DISPLAY) {
1684 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1685 }
1686
1687 mMaxUiWidth = width;
1688
1689 // Update existing metrics.
1690 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1691 }
1692
1693 /** Update base (override) display metrics. */
1694 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1695 mBaseDisplayWidth = baseWidth;
1696 mBaseDisplayHeight = baseHeight;
1697 mBaseDisplayDensity = baseDensity;
1698
1699 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1700 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1701 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1702 mBaseDisplayWidth = mMaxUiWidth;
1703
1704 if (DEBUG_DISPLAY) {
1705 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1706 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1707 + " on display:" + getDisplayId());
1708 }
1709 }
1710
1711 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1712 }
1713
Chong Zhangf66db432016-01-13 10:39:51 -08001714 void getContentRect(Rect out) {
1715 out.set(mContentRect);
1716 }
1717
Wale Ogunwale1666e312016-12-16 11:27:18 -08001718 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1719 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1720 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001721
Wale Ogunwale1666e312016-12-16 11:27:18 -08001722 TaskStack stack = getStackById(stackId);
1723 if (stack != null) {
1724 // It's already attached to the display...clear mDeferRemoval and move stack to
1725 // appropriate z-order on display as needed.
1726 stack.mDeferRemoval = false;
1727 // We're not moving the display to front when we're adding stacks, only when
1728 // requested to change the position of stack explicitly.
1729 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1730 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001731 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001732 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001733 mTaskStackContainers.addStackToDisplay(stack, onTop);
1734 }
1735
Wale Ogunwale1666e312016-12-16 11:27:18 -08001736 if (stackId == DOCKED_STACK_ID) {
1737 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001738 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001739 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001740 }
1741
Andrii Kulian51c1b672017-04-07 18:39:32 -07001742 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001743 final DisplayContent prevDc = stack.getDisplayContent();
1744 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001745 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1746 + " which is not currently attached to any display");
1747 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001748 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001749 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1750 + " to its current displayId=" + mDisplayId);
1751 }
1752
Wale Ogunwale1666e312016-12-16 11:27:18 -08001753 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001754 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001755 }
1756
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001757 @Override
1758 protected void addChild(DisplayChildWindowContainer child,
1759 Comparator<DisplayChildWindowContainer> comparator) {
1760 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1761 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001762
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001763 @Override
1764 protected void addChild(DisplayChildWindowContainer child, int index) {
1765 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1766 }
1767
1768 @Override
1769 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001770 // Only allow removal of direct children from this display if the display is in the process
1771 // of been removed.
1772 if (mRemovingDisplay) {
1773 super.removeChild(child);
1774 return;
1775 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001776 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001777 }
1778
Andrii Kuliand2765632016-12-12 22:26:34 -08001779 @Override
1780 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1781 // Children of the display are statically ordered, so the real intention here is to perform
1782 // the operation on the display and not the static direct children.
1783 getParent().positionChildAt(position, this, includingParents);
1784 }
1785
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001786 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001787 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1788 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001789 final int taskId = stack.taskIdFromPoint(x, y);
1790 if (taskId != -1) {
1791 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001792 }
1793 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001794 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001795 }
1796
Chong Zhang8e89b312015-09-09 15:09:30 -07001797 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001798 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001799 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001800 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001801 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001802 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001803 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001804 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1805 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001806 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001807 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001808 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001809
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001810 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1811 if (mTmpTaskForResizePointSearchResult.searchDone) {
1812 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001813 }
1814 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001815 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001816 }
1817
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001818 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001819 // The provided task is the task on this display with focus, so if WindowManagerService's
1820 // focused app is not on this display, focusedTask will be null.
1821 if (focusedTask == null) {
1822 mTouchExcludeRegion.setEmpty();
1823 } else {
1824 mTouchExcludeRegion.set(mBaseDisplayRect);
1825 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1826 mTmpRect2.setEmpty();
1827 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1828 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1829 stack.setTouchExcludeRegion(
1830 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1831 }
1832 // If we removed the focused task above, add it back and only leave its
1833 // outside touch area in the exclusion. TapDectector is not interested in
1834 // any touch inside the focused task itself.
1835 if (!mTmpRect2.isEmpty()) {
1836 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1837 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001838 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001839 final WindowState inputMethod = mService.mInputMethodWindow;
1840 if (inputMethod != null && inputMethod.isVisibleLw()) {
1841 // If the input method is visible and the user is typing, we don't want these touch
1842 // events to be intercepted and used to change focus. This would likely cause a
1843 // disappearance of the input method.
1844 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001845 if (inputMethod.getDisplayId() == mDisplayId) {
1846 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1847 } else {
1848 // IME is on a different display, so we need to update its tap detector.
1849 // TODO(multidisplay): Remove when IME will always appear on same display.
1850 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1851 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001852 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001853 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1854 WindowState win = mTapExcludedWindows.get(i);
1855 win.getTouchableRegion(mTmpRegion);
1856 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1857 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001858 // TODO(multi-display): Support docked stacks on secondary displays.
1859 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001860 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001861 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001862 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1863 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001864 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001865 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001866 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001867 }
1868
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001869 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001870 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001871 super.switchUser();
1872 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001873 }
1874
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001875 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001876 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1877 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001878 }
1879 }
1880
1881 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001882 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001883 }
1884
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001885 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001886 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001887 }
1888
1889 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001890 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001891 }
1892
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001893 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001894 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001895 }
1896
Wale Ogunwale10124582016-09-15 20:25:50 -07001897 @Override
1898 void removeIfPossible() {
1899 if (isAnimating()) {
1900 mDeferredRemoval = true;
1901 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001902 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001903 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001904 }
1905
Wale Ogunwale10124582016-09-15 20:25:50 -07001906 @Override
1907 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001908 mRemovingDisplay = true;
1909 try {
1910 super.removeImmediately();
1911 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1912 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001913 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001914 mService.unregisterPointerEventListener(mTapDetector);
1915 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1916 }
1917 } finally {
1918 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001919 }
Craig Mautner95da1082014-02-24 17:54:35 -08001920 }
1921
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001922 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001923 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001924 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001925 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1926
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001927 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001928 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001929 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001930 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001931 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001932 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001933 }
1934
Andrii Kulian0214ed92017-05-16 13:44:05 -07001935 /** @return 'true' if removal of this display content is deferred due to active animation. */
1936 boolean isRemovalDeferred() {
1937 return mDeferredRemoval;
1938 }
1939
Wale Ogunwale10124582016-09-15 20:25:50 -07001940 boolean animateForIme(float interpolatedValue, float animationTarget,
1941 float dividerAnimationTarget) {
1942 boolean updated = false;
1943
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001944 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1945 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001946 if (stack == null || !stack.isAdjustedForIme()) {
1947 continue;
1948 }
1949
1950 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1951 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1952 updated = true;
1953 } else {
1954 mDividerControllerLocked.mLastAnimationProgress =
1955 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1956 mDividerControllerLocked.mLastDividerProgress =
1957 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1958 updated |= stack.updateAdjustForIme(
1959 mDividerControllerLocked.mLastAnimationProgress,
1960 mDividerControllerLocked.mLastDividerProgress,
1961 false /* force */);
1962 }
1963 if (interpolatedValue >= 1f) {
1964 stack.endImeAdjustAnimation();
1965 }
1966 }
1967
1968 return updated;
1969 }
1970
1971 boolean clearImeAdjustAnimation() {
1972 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001973 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1974 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001975 if (stack != null && stack.isAdjustedForIme()) {
1976 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1977 changed = true;
1978 }
1979 }
1980 return changed;
1981 }
1982
1983 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001984 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1985 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001986 if (stack.isVisible() && stack.isAdjustedForIme()) {
1987 stack.beginImeAdjustAnimation();
1988 }
1989 }
1990 }
1991
1992 void adjustForImeIfNeeded() {
1993 final WindowState imeWin = mService.mInputMethodWindow;
1994 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1995 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001996 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001997 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1998 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1999 imeTargetStack.getDockSide() : DOCKED_INVALID;
2000 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2001 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2002 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
2003 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
2004 final boolean imeHeightChanged = imeVisible &&
2005 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2006
2007 // The divider could be adjusted for IME position, or be thinner than usual,
2008 // or both. There are three possible cases:
2009 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2010 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2011 // - If IME is not visible, divider is not moved and is normal width.
2012
2013 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002014 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2015 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002016 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07002017 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
2018 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002019 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2020 } else {
2021 stack.resetAdjustedForIme(false);
2022 }
2023 }
2024 mDividerControllerLocked.setAdjustedForIme(
2025 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2026 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002027 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2028 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002029 stack.resetAdjustedForIme(!dockVisible);
2030 }
2031 mDividerControllerLocked.setAdjustedForIme(
2032 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2033 }
Winson Chung655332c2016-10-31 13:14:28 -07002034 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002035 }
2036
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002037 void setInputMethodAnimLayerAdjustment(int adj) {
2038 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2039 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002040 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002041 }
2042
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002043 /**
2044 * If a window that has an animation specifying a colored background and the current wallpaper
2045 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2046 * suddenly disappear.
2047 */
2048 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002049 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2050 w -> w.mIsWallpaper && w.isVisibleNow());
2051
2052 if (visibleWallpaper != null) {
2053 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002054 }
2055 return winAnimator.mAnimLayer;
2056 }
2057
Wale Ogunwale10124582016-09-15 20:25:50 -07002058 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002059 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2060 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002061 stack.prepareFreezingTaskBounds();
2062 }
2063 }
2064
Wale Ogunwale94744212015-09-21 19:01:47 -07002065 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002066 getLogicalDisplayRect(mTmpRect, newRotation);
2067
2068 // Compute a transform matrix to undo the coordinate space transformation,
2069 // and present the window at the same physical position it previously occupied.
2070 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2071 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2072
2073 mTmpRectF.set(bounds);
2074 mTmpMatrix.mapRect(mTmpRectF);
2075 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002076 }
2077
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002078 static int deltaRotation(int oldRotation, int newRotation) {
2079 int delta = newRotation - oldRotation;
2080 if (delta < 0) delta += 4;
2081 return delta;
2082 }
2083
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002084 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002085 Matrix outMatrix) {
2086 // For rotations without Z-ordering we don't need the target rectangle's position.
2087 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2088 displayHeight, outMatrix);
2089 }
2090
2091 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2092 float displayWidth, float displayHeight, Matrix outMatrix) {
2093 switch (rotation) {
2094 case ROTATION_0:
2095 outMatrix.reset();
2096 break;
2097 case ROTATION_270:
2098 outMatrix.setRotate(270, 0, 0);
2099 outMatrix.postTranslate(0, displayHeight);
2100 outMatrix.postTranslate(rectTop, 0);
2101 break;
2102 case ROTATION_180:
2103 outMatrix.reset();
2104 break;
2105 case ROTATION_90:
2106 outMatrix.setRotate(90, 0, 0);
2107 outMatrix.postTranslate(displayWidth, 0);
2108 outMatrix.postTranslate(-rectTop, rectLeft);
2109 break;
2110 }
2111 }
2112
Steven Timotiusaf03df62017-07-18 16:56:43 -07002113 void writeToProto(ProtoOutputStream proto, long fieldId) {
2114 final long token = proto.start(fieldId);
2115 proto.write(ID, mDisplayId);
2116 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2117 final TaskStack stack = mTaskStackContainers.get(stackNdx);
2118 stack.writeToProto(proto, STACKS);
2119 }
2120 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2121 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
2122 for (int i = mAboveAppWindowsContainers.size() - 1; i >= 0; --i) {
2123 final WindowToken windowToken = mAboveAppWindowsContainers.get(i);
2124 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS);
2125 }
2126 for (int i = mBelowAppWindowsContainers.size() - 1; i >= 0; --i) {
2127 final WindowToken windowToken = mBelowAppWindowsContainers.get(i);
2128 windowToken.writeToProto(proto, BELOW_APP_WINDOWS);
2129 }
2130 for (int i = mImeWindowsContainers.size() - 1; i >= 0; --i) {
2131 final WindowToken windowToken = mImeWindowsContainers.get(i);
2132 windowToken.writeToProto(proto, IME_WINDOWS);
2133 }
2134 proto.write(DPI, mBaseDisplayDensity);
2135 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
2136 proto.end(token);
2137 }
2138
Craig Mautnera91f9e22012-09-14 16:22:08 -07002139 public void dump(String prefix, PrintWriter pw) {
2140 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2141 final String subPrefix = " " + prefix;
2142 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2143 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2144 pw.print("dpi");
2145 if (mInitialDisplayWidth != mBaseDisplayWidth
2146 || mInitialDisplayHeight != mBaseDisplayHeight
2147 || mInitialDisplayDensity != mBaseDisplayDensity) {
2148 pw.print(" base=");
2149 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2150 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2151 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002152 if (mDisplayScalingDisabled) {
2153 pw.println(" noscale");
2154 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002155 pw.print(" cur=");
2156 pw.print(mDisplayInfo.logicalWidth);
2157 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2158 pw.print(" app=");
2159 pw.print(mDisplayInfo.appWidth);
2160 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2161 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2162 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2163 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2164 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002165 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2166 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002167
Craig Mautnerdc548482014-02-05 13:35:24 -08002168 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002169 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002170 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2171 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002172 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002173 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002174
Craig Mautnerdc548482014-02-05 13:35:24 -08002175 pw.println();
2176 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002177 pw.println();
2178 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002179 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002180 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002181 pw.print(" Exiting #"); pw.print(i);
2182 pw.print(' '); pw.print(token);
2183 pw.println(':');
2184 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002185 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002186 }
Craig Mautner59c00972012-07-30 12:10:24 -07002187 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002188 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002189 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002190 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002191 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002192 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002193
2194 if (mInputMethodAnimLayerAdjustment != 0) {
2195 pw.println(subPrefix
2196 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2197 }
Craig Mautner59c00972012-07-30 12:10:24 -07002198 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002199
2200 @Override
2201 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002202 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002203 }
2204
2205 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002206 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002207 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002208
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002209 /** Checks if stack with provided id is visible on this display. */
2210 boolean isStackVisible(int stackId) {
2211 final TaskStack stack = getStackById(stackId);
2212 return (stack != null && stack.isVisible());
2213 }
2214
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002215 /**
2216 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2217 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002218 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002219 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002220 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002221 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002222
2223 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002224 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002225 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002226 */
Jorim Jaggife762342016-10-13 14:33:27 +02002227 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002228 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002229 }
2230
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002231 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002232 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002233 final int x = (int) xf;
2234 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002235 final WindowState touchedWin = getWindow(w -> {
2236 final int flags = w.mAttrs.flags;
2237 if (!w.isVisibleLw()) {
2238 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002239 }
2240 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002241 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002242 }
2243
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002244 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002245 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002246 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002247 }
2248
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002249 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002250
2251 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002252 return mTmpRegion.contains(x, y) || touchFlags == 0;
2253 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002254
2255 return touchedWin;
2256 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002257
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002258 boolean canAddToastWindowForUid(int uid) {
2259 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002260 // Also if the app is focused adding more than one toast at
2261 // a time for better backwards compatibility.
2262 final WindowState focusedWindowForUid = getWindow(w ->
2263 w.mOwnerUid == uid && w.isFocused());
2264 if (focusedWindowForUid != null) {
2265 return true;
2266 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002267 final WindowState win = getWindow(w ->
2268 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2269 && !w.mWindowRemovalAllowed);
2270 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002271 }
2272
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002273 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002274 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2275 return;
2276 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002277
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002278 // Used to communicate the old focus to the callback method.
2279 mTmpWindow = oldFocus;
2280
2281 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002282 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002283
2284 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002285 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002286
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002287 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002288
2289 if (mTmpWindow == null) {
2290 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2291 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002292 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002293 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002294 }
2295
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002296 /** Updates the layer assignment of windows on this display. */
2297 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002298 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002299 if (setLayoutNeeded) {
2300 setLayoutNeeded();
2301 }
2302 }
2303
Wale Ogunwale1666e312016-12-16 11:27:18 -08002304 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2305 // moving containers or resizing them. Need to investigate the best way to have it automatically
2306 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002307 void layoutAndAssignWindowLayersIfNeeded() {
2308 mService.mWindowsChanged = true;
2309 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002310
2311 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2312 false /*updateInputWindows*/)) {
2313 assignWindowLayers(false /* setLayoutNeeded */);
2314 }
2315
2316 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2317 mService.mWindowPlacerLocked.performSurfacePlacement();
2318 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2319 }
2320
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002321 /** Returns true if a leaked surface was destroyed */
2322 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002323 // Used to indicate that a surface was leaked.
2324 mTmpWindow = null;
2325 forAllWindows(w -> {
2326 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002327 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002328 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002329 }
2330 if (!mService.mSessions.contains(wsa.mSession)) {
2331 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332 + w + " surface=" + wsa.mSurfaceController
2333 + " token=" + w.mToken
2334 + " pid=" + w.mSession.mPid
2335 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002336 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002337 mService.mForceRemoves.add(w);
2338 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002339 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002340 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002341 + w + " surface=" + wsa.mSurfaceController
2342 + " token=" + w.mAppToken
2343 + " saved=" + w.hasSavedSurface());
2344 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002345 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002346 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002347 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002348 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002349
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002350 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002351 }
2352
2353 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002354 * Determine and return the window that should be the IME target.
2355 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2356 * @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 +00002357 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002358 WindowState computeImeTarget(boolean updateImeTarget) {
2359 if (mService.mInputMethodWindow == null) {
2360 // There isn't an IME so there shouldn't be a target...That was easy!
2361 if (updateImeTarget) {
2362 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2363 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2364 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2365 }
2366 return null;
2367 }
2368
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002369 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2370 // same display. Or even when the current IME/target are not on the same screen as the next
2371 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002372 mUpdateImeTarget = updateImeTarget;
2373 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002374
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002375
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002376 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2377 // to be on top of it, but it is not -really- where input will go. So look down below
2378 // for a real window to target...
2379 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2380 final AppWindowToken token = target.mAppToken;
2381 if (token != null) {
2382 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2383 if (betterTarget != null) {
2384 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002385 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002386 }
2387 }
2388
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002389 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2390 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002391
2392 // Now, a special case -- if the last target's window is in the process of exiting, and is
2393 // above the new target, keep on the last target to avoid flicker. Consider for example a
2394 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2395 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2396 // scrim.
2397 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2399 && (target == null
2400 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002401 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002402 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002403 }
2404
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002405 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2406 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002407
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002408 if (target == null) {
2409 if (updateImeTarget) {
2410 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2411 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2412 + Debug.getCallers(4) : ""));
2413 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2414 }
2415
2416 return null;
2417 }
2418
2419 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002420 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2421 if (token != null) {
2422
2423 // Now some fun for dealing with window animations that modify the Z order. We need
2424 // to look at all windows below the current target that are in this app, finding the
2425 // highest visible one in layering.
2426 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002427 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002428 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002429 }
2430
2431 if (highestTarget != null) {
2432 final AppTransition appTransition = mService.mAppTransition;
2433 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2434 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2435 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002436 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002437
2438 if (appTransition.isTransitionSet()) {
2439 // If we are currently setting up for an animation, hold everything until we
2440 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002441 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2442 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002443 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002444 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002445 // If the window we are currently targeting is involved with an animation,
2446 // and it is on top of the next target we will be over, then hold off on
2447 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002448 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2449 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002450 }
2451 }
2452 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002453
2454 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2455 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2456 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002457 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002458 }
2459
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002460 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002461 }
2462
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002463 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2464 if (target == mService.mInputMethodTarget
2465 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2466 && mInputMethodAnimLayerAdjustment == layerAdj) {
2467 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002468 }
2469
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002470 mService.mInputMethodTarget = target;
2471 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2472 setInputMethodAnimLayerAdjustment(layerAdj);
2473 assignWindowLayers(false /* setLayoutNeeded */);
2474 }
2475
2476 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2477 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2478 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2479 }
2480
2481 // Used to indicate we have reached the first window in the range we are interested in.
2482 mTmpWindow = null;
2483
2484 // TODO: Figure-out a more efficient way to do this.
2485 final WindowState candidate = getWindow(w -> {
2486 if (w == top) {
2487 // Reached the first window in the range we are interested in.
2488 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002489 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002490 if (mTmpWindow == null) {
2491 return false;
2492 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002493
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002494 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2495 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002496 }
2497 // If we reached the bottom of the range of windows we are considering,
2498 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002499 if (w == bottom) {
2500 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002501 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002502 return false;
2503 });
2504
2505 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002506 }
2507
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002508 void setLayoutNeeded() {
2509 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2510 mLayoutNeeded = true;
2511 }
2512
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002513 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002514 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2515 mLayoutNeeded = false;
2516 }
2517
2518 boolean isLayoutNeeded() {
2519 return mLayoutNeeded;
2520 }
2521
Wale Ogunwale02319a62016-09-26 15:21:22 -07002522 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2523 if (mTokenMap.isEmpty()) {
2524 return;
2525 }
2526 pw.println(" Display #" + mDisplayId);
2527 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2528 while (it.hasNext()) {
2529 final WindowToken token = it.next();
2530 pw.print(" ");
2531 pw.print(token);
2532 if (dumpAll) {
2533 pw.println(':');
2534 token.dump(pw, " ");
2535 } else {
2536 pw.println();
2537 }
2538 }
2539 }
2540
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002541 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002542 final int[] index = new int[1];
2543 forAllWindows(w -> {
2544 final WindowStateAnimator wAnim = w.mWinAnimator;
2545 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2546 index[0] = index[0] + 1;
2547 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002548 }
2549
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002550 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002551 forAllWindows(w -> {
2552 w.mWinAnimator.enableSurfaceTrace(fd);
2553 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002554 }
2555
2556 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002557 forAllWindows(w -> {
2558 w.mWinAnimator.disableSurfaceTrace();
2559 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002560 }
2561
Jorim Jaggife762342016-10-13 14:33:27 +02002562 /**
2563 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2564 */
2565 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2566 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002567 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002568 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2569 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002570 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002571 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2572 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002573 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002574 }
2575
Wale Ogunwale494009b82016-10-21 09:01:38 -07002576 boolean checkWaitingForWindows() {
2577
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002578 mHaveBootMsg = false;
2579 mHaveApp = false;
2580 mHaveWallpaper = false;
2581 mHaveKeyguard = true;
2582
2583 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002584 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2585 return true;
2586 }
2587 if (w.isDrawnLw()) {
2588 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002589 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002590 } else if (w.mAttrs.type == TYPE_APPLICATION
2591 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002592 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002593 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002594 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002595 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002596 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002597 }
2598 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002599 return false;
2600 });
2601
2602 if (visibleWindow != null) {
2603 // We have a visible window.
2604 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002605 }
2606
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002607 // if the wallpaper service is disabled on the device, we're never going to have
2608 // wallpaper, don't bother waiting for it
2609 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2610 com.android.internal.R.bool.config_enableWallpaperService)
2611 && !mService.mOnlyCore;
2612
Wale Ogunwale494009b82016-10-21 09:01:38 -07002613 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2614 "******** booted=" + mService.mSystemBooted
2615 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2617 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2618 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002619
2620 // If we are turning on the screen to show the boot message, don't do it until the boot
2621 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002622 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002623 return true;
2624 }
2625
2626 // If we are turning on the screen after the boot is completed normally, don't do so until
2627 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002628 if (mService.mSystemBooted
2629 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002630 return true;
2631 }
2632
2633 return false;
2634 }
2635
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002636 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002637 mTmpWindowAnimator = animator;
2638 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002639 }
2640
2641 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002642 resetAnimationBackgroundAnimator();
2643
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002644 // Used to indicate a detached wallpaper.
2645 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002646 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002647
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002648 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002649
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002651 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002652 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2653 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002654 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2655 }
2656 }
2657
2658 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002659 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002660 }
2661
Wale Ogunwale494009b82016-10-21 09:01:38 -07002662 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002663 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002664 if (imFocus == null) {
2665 return false;
2666 }
2667
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002668 if (DEBUG_INPUT_METHOD) {
2669 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2670 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2671 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002672 }
2673
Wale Ogunwale494009b82016-10-21 09:01:38 -07002674 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2675
2676 if (DEBUG_INPUT_METHOD) {
2677 Slog.i(TAG_WM, "IM target client: " + imeClient);
2678 if (imeClient != null) {
2679 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2680 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2681 }
2682 }
2683
2684 return imeClient != null && imeClient.asBinder() == client.asBinder();
2685 }
2686
2687 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002688 final WindowState win = getWindow(
2689 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2690 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002691 }
2692
2693 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002694 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002695 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002696 final int curValue = w.mSystemUiVisibility;
2697 final int diff = (curValue ^ visibility) & globalDiff;
2698 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002699 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002700 w.mSeq++;
2701 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002702 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002703 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2704 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002705 visibility, newValue, diff);
2706 }
2707 } catch (RemoteException e) {
2708 // so sorry
2709 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002710 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002711 }
2712
2713 void onWindowFreezeTimeout() {
2714 Slog.w(TAG_WM, "Window freeze timeout expired.");
2715 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002716
2717 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002718 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002719 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002720 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002721 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002722 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2723 - mService.mDisplayFreezeTime);
2724 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002725 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002726 mService.mWindowPlacerLocked.performSurfacePlacement();
2727 }
2728
2729 void waitForAllWindowsDrawn() {
2730 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002731 forAllWindows(w -> {
2732 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2733 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2734 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002735 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002736 w.mLastContentInsets.set(-1, -1, -1, -1);
2737 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002738 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002739 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002740 }
2741
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002742 // TODO: Super crazy long method that should be broken down...
2743 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2744
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002745 final int dw = mDisplayInfo.logicalWidth;
2746 final int dh = mDisplayInfo.logicalHeight;
2747 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2748
2749 mTmpUpdateAllDrawn.clear();
2750
2751 int repeats = 0;
2752 do {
2753 repeats++;
2754 if (repeats > 6) {
2755 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2756 clearLayoutNeeded();
2757 break;
2758 }
2759
2760 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2761 pendingLayoutChanges);
2762
Andrii Kulian839def92016-11-02 10:58:58 -07002763 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2764 // the wallpaper window jumping across displays.
2765 // Remove check for default display when there will be support for multiple wallpaper
2766 // targets (on different displays).
2767 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002768 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002769 }
2770
2771 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2772 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2773 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2774 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002775 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002776 }
2777 }
2778
2779 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2780 setLayoutNeeded();
2781 }
2782
2783 // FIRST LOOP: Perform a layout, if needed.
2784 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2785 performLayout(repeats == 1, false /* updateInputWindows */);
2786 } else {
2787 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2788 }
2789
2790 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2791 pendingLayoutChanges = 0;
2792
2793 if (isDefaultDisplay) {
2794 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002795 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002796 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2797 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2798 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2799 }
2800 } while (pendingLayoutChanges != 0);
2801
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002802 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002803 resetDimming();
2804
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002805 mTmpRecoveringMemory = recoveringMemory;
2806 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002807
2808 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002809 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2810 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2811 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002812 true /* inTraversal, must call performTraversalInTrans... below */);
2813
2814 stopDimmingIfNeeded();
2815
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002816 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2817 if (wallpaperVisible != mLastWallpaperVisible) {
2818 mLastWallpaperVisible = wallpaperVisible;
2819 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2820 }
2821
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002822 while (!mTmpUpdateAllDrawn.isEmpty()) {
2823 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2824 // See if any windows have been drawn, so they (and others associated with them)
2825 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002826 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002827 }
2828
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002829 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002830 }
2831
2832 void performLayout(boolean initial, boolean updateInputWindows) {
2833 if (!isLayoutNeeded()) {
2834 return;
2835 }
2836 clearLayoutNeeded();
2837
2838 final int dw = mDisplayInfo.logicalWidth;
2839 final int dh = mDisplayInfo.logicalHeight;
2840
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002841 if (DEBUG_LAYOUT) {
2842 Slog.v(TAG, "-------------------------------------");
2843 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2844 }
2845
Andrii Kulian8ee72852017-03-10 10:36:45 -08002846 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002847 getConfiguration().uiMode);
2848 if (isDefaultDisplay) {
2849 // Not needed on non-default displays.
2850 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2851 mService.mScreenRect.set(0, 0, dw, dh);
2852 }
2853
2854 mService.mPolicy.getContentRectLw(mContentRect);
2855
2856 int seq = mService.mLayoutSeq + 1;
2857 if (seq < 0) seq = 0;
2858 mService.mLayoutSeq = seq;
2859
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002860 // Used to indicate that we have processed the dream window and all additional windows are
2861 // behind it.
2862 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002863 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002864
2865 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002866 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002867
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002868 // Used to indicate that we have processed the dream window and all additional attached
2869 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002870 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002871 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002872
2873 // Now perform layout of attached windows, which usually depend on the position of the
2874 // window they are attached to. XXX does not deal with windows that are attached to windows
2875 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002876 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002877
2878 // Window frames may have changed. Tell the input dispatcher about it.
2879 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2880 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2881 if (updateInputWindows) {
2882 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2883 }
2884
2885 mService.mPolicy.finishLayoutLw();
2886 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2887 }
2888
2889 /**
2890 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2891 * In portrait mode, it grabs the full screenshot.
2892 *
2893 * @param width the width of the target bitmap
2894 * @param height the height of the target bitmap
2895 * @param includeFullDisplay true if the screen should not be cropped before capture
2896 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2897 * @param config of the output bitmap
2898 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002899 * @param includeDecor whether to include window decors, like the status or navigation bar
2900 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002901 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002902 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002903 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002904 boolean wallpaperOnly, boolean includeDecor) {
2905 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2906 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002907 if (bitmap == null) {
2908 return null;
2909 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002910
2911 if (DEBUG_SCREENSHOT) {
2912 // TEST IF IT's ALL BLACK
2913 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2914 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2915 bitmap.getHeight());
2916 boolean allBlack = true;
2917 final int firstColor = buffer[0];
2918 for (int i = 0; i < buffer.length; i++) {
2919 if (buffer[i] != firstColor) {
2920 allBlack = false;
2921 break;
2922 }
2923 }
2924 if (allBlack) {
2925 final WindowState appWin = mScreenshotApplicationState.appWin;
2926 final int maxLayer = mScreenshotApplicationState.maxLayer;
2927 final int minLayer = mScreenshotApplicationState.minLayer;
2928 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2929 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2930 (appWin != null ?
2931 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2932 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2933 }
2934 }
2935
2936 // Create a copy of the screenshot that is immutable and backed in ashmem.
2937 // This greatly reduces the overhead of passing the bitmap between processes.
2938 Bitmap ret = bitmap.createAshmemBitmap(config);
2939 bitmap.recycle();
2940 return ret;
2941 }
2942
2943 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2944 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2945 boolean includeDecor) {
2946 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2947 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2948 }
2949
2950 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2951 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2952 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002953 int dw = mDisplayInfo.logicalWidth;
2954 int dh = mDisplayInfo.logicalHeight;
2955 if (dw == 0 || dh == 0) {
2956 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2957 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2958 return null;
2959 }
2960
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002961 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002962
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002963 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002964 final Rect frame = new Rect();
2965 final Rect stackBounds = new Rect();
2966
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002967 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002968 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002969 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002970 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002971 if (!mService.mPolicy.isScreenOn()) {
2972 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
2973 + " was off.");
2974 return null;
2975 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002976 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002977 mScreenshotApplicationState.appWin = null;
2978 forAllWindows(w -> {
2979 if (!w.mHasSurface) {
2980 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002981 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002982 if (w.mLayer >= aboveAppLayer) {
2983 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002984 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002985 if (wallpaperOnly && !w.mIsWallpaper) {
2986 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002987 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002988 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02002989 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002990 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002991 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2992 // then the target window state is this one.
2993 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002994 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002995 }
2996
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002997 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002998 // We have not ran across the target window yet, so it is probably behind
2999 // the wallpaper. This can happen when the keyguard is up and all windows
3000 // are moved behind the wallpaper. We don't want to include the wallpaper
3001 // layer in the screenshot as it will cover-up the layer of the target
3002 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003003 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003004 }
3005 // Fall through. The target window is in front of the wallpaper. For this
3006 // case we want to include the wallpaper layer in the screenshot because
3007 // the target window might have some transparent areas.
3008 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003009 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003010 // This app window is of no interest if it is not associated with the
3011 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003012 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003013 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003014 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003015 }
3016
3017 // Include this window.
3018
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003019 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003020 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003021 if (mScreenshotApplicationState.maxLayer < layer) {
3022 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003023 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003024 if (mScreenshotApplicationState.minLayer > layer) {
3025 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003026 }
3027
3028 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003029 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3030 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003031 final Task task = w.getTask();
3032 if (task != null) {
3033 task.getBounds(frame);
3034 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003035
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003036 // No task bounds? Too bad! Ain't no screenshot then.
3037 return true;
3038 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003039 } else {
3040 final Rect wf = w.mFrame;
3041 final Rect cr = w.mContentInsets;
3042 int left = wf.left + cr.left;
3043 int top = wf.top + cr.top;
3044 int right = wf.right - cr.right;
3045 int bottom = wf.bottom - cr.bottom;
3046 frame.union(left, top, right, bottom);
3047 w.getVisibleBounds(stackBounds);
3048 if (!Rect.intersects(frame, stackBounds)) {
3049 // Set frame empty if there's no intersection.
3050 frame.setEmpty();
3051 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003052 }
3053 }
3054
3055 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003056 (w.mAppToken != null && w.mAppToken.token == appToken)
3057 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01003058 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003059 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003060 }
3061
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003062 if (w.isObscuringDisplay()){
3063 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003064 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003065 return false;
3066 }, true /* traverseTopToBottom */);
3067
3068 final WindowState appWin = mScreenshotApplicationState.appWin;
3069 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3070 final int maxLayer = mScreenshotApplicationState.maxLayer;
3071 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003072
3073 if (appToken != null && appWin == null) {
3074 // Can't find a window to snapshot.
3075 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3076 "Screenshot: Couldn't find a surface matching " + appToken);
3077 return null;
3078 }
3079
3080 if (!screenshotReady) {
3081 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3082 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3083 appWin.mWinAnimator.mDrawState)));
3084 return null;
3085 }
3086
3087 // Screenshot is ready to be taken. Everything from here below will continue
3088 // through the bottom of the loop and return a value. We only stay in the loop
3089 // because we don't want to release the mWindowMap lock until the screenshot is
3090 // taken.
3091
3092 if (maxLayer == 0) {
3093 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3094 + ": returning null maxLayer=" + maxLayer);
3095 return null;
3096 }
3097
Jorim Jaggi02886a82016-12-06 09:10:06 -08003098 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003099 // Constrain frame to the screen size.
3100 if (!frame.intersect(0, 0, dw, dh)) {
3101 frame.setEmpty();
3102 }
3103 } else {
3104 // Caller just wants entire display.
3105 frame.set(0, 0, dw, dh);
3106 }
3107 if (frame.isEmpty()) {
3108 return null;
3109 }
3110
3111 if (width < 0) {
3112 width = (int) (frame.width() * frameScale);
3113 }
3114 if (height < 0) {
3115 height = (int) (frame.height() * frameScale);
3116 }
3117
3118 // Tell surface flinger what part of the image to crop. Take the top
3119 // right part of the application, and crop the larger dimension to fit.
3120 Rect crop = new Rect(frame);
3121 if (width / (float) frame.width() < height / (float) frame.height()) {
3122 int cropWidth = (int)((float)width / (float)height * frame.height());
3123 crop.right = crop.left + cropWidth;
3124 } else {
3125 int cropHeight = (int)((float)height / (float)width * frame.width());
3126 crop.bottom = crop.top + cropHeight;
3127 }
3128
3129 // The screenshot API does not apply the current screen rotation.
3130 int rot = mDisplay.getRotation();
3131
3132 if (rot == ROTATION_90 || rot == ROTATION_270) {
3133 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3134 }
3135
3136 // Surfaceflinger is not aware of orientation, so convert our logical
3137 // crop to surfaceflinger's portrait orientation.
3138 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3139
3140 if (DEBUG_SCREENSHOT) {
3141 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3142 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003143 forAllWindows(w -> {
3144 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3145 Slog.i(TAG_WM, w + ": " + w.mLayer
3146 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003147 + " surfaceLayer=" + ((controller == null)
3148 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003149 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003150 }
3151
3152 final ScreenRotationAnimation screenRotationAnimation =
3153 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3154 final boolean inRotation = screenRotationAnimation != null &&
3155 screenRotationAnimation.isAnimating();
3156 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3157 "Taking screenshot while rotating");
3158
3159 // We force pending transactions to flush before taking
3160 // the screenshot by pushing an empty synchronous transaction.
3161 SurfaceControl.openTransaction();
3162 SurfaceControl.closeTransactionSync();
3163
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003164 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003165 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003166 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003167 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3168 + ") to layer " + maxLayer);
3169 return null;
3170 }
3171 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003172 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003173 }
3174
3175 // TODO: Can this use createRotationMatrix()?
3176 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3177 if (rot == Surface.ROTATION_90) {
3178 final int tmp = crop.top;
3179 crop.top = dw - crop.right;
3180 crop.right = crop.bottom;
3181 crop.bottom = dw - crop.left;
3182 crop.left = tmp;
3183 } else if (rot == Surface.ROTATION_180) {
3184 int tmp = crop.top;
3185 crop.top = dh - crop.bottom;
3186 crop.bottom = dh - tmp;
3187 tmp = crop.right;
3188 crop.right = dw - crop.left;
3189 crop.left = dw - tmp;
3190 } else if (rot == Surface.ROTATION_270) {
3191 final int tmp = crop.top;
3192 crop.top = crop.left;
3193 crop.left = dh - crop.bottom;
3194 crop.bottom = crop.right;
3195 crop.right = dh - tmp;
3196 }
3197 }
3198
3199 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003200 // Used to indicate the layout is needed.
3201 mTmpWindow = null;
3202
3203 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003204 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003205 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003206 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003207 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003208 w.setDisplayLayoutNeeded();
3209 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003210 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003211
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003212 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003213 mService.mWindowPlacerLocked.performSurfacePlacement();
3214 }
3215 }
3216
Wale Ogunwale1666e312016-12-16 11:27:18 -08003217 void setExitingTokensHasVisible(boolean hasVisible) {
3218 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3219 mExitingTokens.get(i).hasVisible = hasVisible;
3220 }
3221
3222 // Initialize state of exiting applications.
3223 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3224 }
3225
3226 void removeExistingTokensIfPossible() {
3227 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3228 final WindowToken token = mExitingTokens.get(i);
3229 if (!token.hasVisible) {
3230 mExitingTokens.remove(i);
3231 }
3232 }
3233
3234 // Time to remove any exiting applications?
3235 mTaskStackContainers.removeExistingAppTokensIfPossible();
3236 }
3237
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003238 @Override
3239 void onDescendantOverrideConfigurationChanged() {
3240 setLayoutNeeded();
3241 mService.requestTraversal();
3242 }
3243
David Stevens9440dc82017-03-16 19:00:20 -07003244 boolean okToDisplay() {
3245 if (mDisplayId == DEFAULT_DISPLAY) {
3246 return !mService.mDisplayFrozen
3247 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3248 }
3249 return mDisplayInfo.state == Display.STATE_ON;
3250 }
3251
3252 boolean okToAnimate() {
3253 return okToDisplay() &&
3254 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3255 }
3256
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003257 static final class TaskForResizePointSearchResult {
3258 boolean searchDone;
3259 Task taskForResize;
3260
3261 void reset() {
3262 searchDone = false;
3263 taskForResize = null;
3264 }
3265 }
Robert Carr3b716242016-08-16 16:02:21 -07003266
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003267 private static final class ApplySurfaceChangesTransactionState {
3268 boolean displayHasContent;
3269 boolean obscured;
3270 boolean syswin;
3271 boolean focusDisplayed;
3272 float preferredRefreshRate;
3273 int preferredModeId;
3274
3275 void reset() {
3276 displayHasContent = false;
3277 obscured = false;
3278 syswin = false;
3279 focusDisplayed = false;
3280 preferredRefreshRate = 0;
3281 preferredModeId = 0;
3282 }
3283 }
3284
3285 private static final class ScreenshotApplicationState {
3286 WindowState appWin;
3287 int maxLayer;
3288 int minLayer;
3289 boolean screenshotReady;
3290
3291 void reset(boolean screenshotReady) {
3292 appWin = null;
3293 maxLayer = 0;
3294 minLayer = 0;
3295 this.screenshotReady = screenshotReady;
3296 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3297 }
3298 }
3299
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003300 /**
3301 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3302 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3303 * homogeneous children type which is currently required by sub-classes of
3304 * {@link WindowContainer} class.
3305 */
3306 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3307
3308 int size() {
3309 return mChildren.size();
3310 }
3311
3312 E get(int index) {
3313 return mChildren.get(index);
3314 }
3315
3316 @Override
3317 boolean fillsParent() {
3318 return true;
3319 }
3320
3321 @Override
3322 boolean isVisible() {
3323 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003324 }
3325 }
3326
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003327 /**
3328 * Window container class that contains all containers on this display relating to Apps.
3329 * I.e Activities.
3330 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003331 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003332
Andrii Kulian839def92016-11-02 10:58:58 -07003333 /**
3334 * Adds the stack to this container.
3335 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3336 */
3337 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003338 if (stack.mStackId == HOME_STACK_ID) {
3339 if (mHomeStack != null) {
3340 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3341 }
3342 mHomeStack = stack;
3343 }
3344 addChild(stack, onTop);
3345 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003346 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003347
Andrii Kulian839def92016-11-02 10:58:58 -07003348 /** Removes the stack from its container and prepare for changing the parent. */
3349 void removeStackFromDisplay(TaskStack stack) {
3350 removeChild(stack);
3351 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003352 }
3353
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003354 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003355 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3356 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003357 addChild(stack, addIndex);
3358 setLayoutNeeded();
3359 }
3360
Bryce Lee00d586d2017-07-28 20:48:43 -07003361
3362 @Override
3363 boolean isOnTop() {
3364 // Considered always on top
3365 return true;
3366 }
3367
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003368 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003369 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3370 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3371 // This stack is always-on-top, override the default behavior.
3372 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3373
3374 // Moving to its current position, as we must call super but we don't want to
3375 // perform any meaningful action.
3376 final int currentPosition = mChildren.indexOf(child);
3377 super.positionChildAt(currentPosition, child, false /* includingParents */);
3378 return;
3379 }
3380
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003381 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3382 super.positionChildAt(targetPosition, child, includingParents);
3383
3384 setLayoutNeeded();
3385 }
3386
3387 /**
3388 * When stack is added or repositioned, find a proper position for it.
3389 * This will make sure that pinned stack always stays on top.
3390 * @param requestedPosition Position requested by caller.
3391 * @param stack Stack to be added or positioned.
3392 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3393 * @return The proper position for the stack.
3394 */
3395 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3396 final int topChildPosition = mChildren.size() - 1;
3397 boolean toTop = requestedPosition == POSITION_TOP;
3398 toTop |= adding ? requestedPosition >= topChildPosition + 1
3399 : requestedPosition >= topChildPosition;
3400 int targetPosition = requestedPosition;
3401
Bryce Lee48f4b572017-04-10 10:54:15 -07003402 if (toTop && stack.mStackId != PINNED_STACK_ID
3403 && getStackById(PINNED_STACK_ID) != null) {
3404 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003405 TaskStack topStack = mChildren.get(topChildPosition);
3406 if (topStack.mStackId != PINNED_STACK_ID) {
3407 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3408 }
3409
3410 // So, stack is moved just below the pinned stack.
3411 // When we're adding a new stack the target is the current pinned stack position.
3412 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003413 // stack, because WindowContainer#positionAt() first removes element and then adds
3414 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003415 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3416 }
3417
3418 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003419 }
3420
3421 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003422 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3423 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003424 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003425 if (super.forAllWindows(callback, traverseTopToBottom)) {
3426 return true;
3427 }
3428 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3429 return true;
3430 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003431 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003432 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3433 return true;
3434 }
3435 if (super.forAllWindows(callback, traverseTopToBottom)) {
3436 return true;
3437 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003438 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003439 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003440 }
3441
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003442 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003443 boolean traverseTopToBottom) {
3444 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3445 // app tokens.
3446 // TODO: Investigate if we need to continue to do this or if we can just process them
3447 // in-order.
3448 if (traverseTopToBottom) {
3449 for (int i = mChildren.size() - 1; i >= 0; --i) {
3450 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3451 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003452 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3453 traverseTopToBottom)) {
3454 return true;
3455 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003456 }
3457 }
3458 } else {
3459 final int count = mChildren.size();
3460 for (int i = 0; i < count; ++i) {
3461 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3462 final int appTokensCount = appTokens.size();
3463 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003464 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3465 traverseTopToBottom)) {
3466 return true;
3467 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003468 }
3469 }
3470 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003471 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003472 }
3473
Wale Ogunwale1666e312016-12-16 11:27:18 -08003474 void setExitingTokensHasVisible(boolean hasVisible) {
3475 for (int i = mChildren.size() - 1; i >= 0; --i) {
3476 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3477 for (int j = appTokens.size() - 1; j >= 0; --j) {
3478 appTokens.get(j).hasVisible = hasVisible;
3479 }
3480 }
3481 }
3482
3483 void removeExistingAppTokensIfPossible() {
3484 for (int i = mChildren.size() - 1; i >= 0; --i) {
3485 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3486 for (int j = appTokens.size() - 1; j >= 0; --j) {
3487 final AppWindowToken token = appTokens.get(j);
3488 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3489 && (!token.mIsExiting || token.isEmpty())) {
3490 // Make sure there is no animation running on this token, so any windows
3491 // associated with it will be removed as soon as their animations are
3492 // complete.
3493 token.mAppAnimator.clearAnimation();
3494 token.mAppAnimator.animating = false;
3495 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3496 "performLayout: App token exiting now removed" + token);
3497 token.removeIfPossible();
3498 }
3499 }
3500 }
3501 }
3502
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003503 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003504 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003505 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003506 // Apps and their containers are not allowed to specify an orientation while the
3507 // docked or freeform stack is visible...except for the home stack/task if the
3508 // docked stack is minimized and it actually set something.
3509 if (mHomeStack != null && mHomeStack.isVisible()
3510 && mDividerControllerLocked.isMinimizedDock()) {
3511 final int orientation = mHomeStack.getOrientation();
3512 if (orientation != SCREEN_ORIENTATION_UNSET) {
3513 return orientation;
3514 }
3515 }
3516 return SCREEN_ORIENTATION_UNSPECIFIED;
3517 }
3518
3519 final int orientation = super.getOrientation();
3520 if (orientation != SCREEN_ORIENTATION_UNSET
3521 && orientation != SCREEN_ORIENTATION_BEHIND) {
3522 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3523 "App is requesting an orientation, return " + orientation);
3524 return orientation;
3525 }
3526
3527 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003528 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003529 // The next app has not been requested to be visible, so we keep the current orientation
3530 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003531 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003532 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003533 }
3534
3535 /**
3536 * Window container class that contains all containers on this display that are not related to
3537 * Apps. E.g. status bar.
3538 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003539 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3540 /**
3541 * Compares two child window tokens returns -1 if the first is lesser than the second in
3542 * terms of z-order and 1 otherwise.
3543 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003544 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003545 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003546 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3547 token1.mOwnerCanManageAppTokens)
3548 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3549 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003550
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003551 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3552 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3553 return false;
3554 }
3555 final int req = w.mAttrs.screenOrientation;
3556 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3557 || req == SCREEN_ORIENTATION_UNSET) {
3558 return false;
3559 }
3560 return true;
3561 };
3562
Wale Ogunwale3a931692016-11-02 16:49:48 -07003563 private final String mName;
3564 NonAppWindowContainers(String name) {
3565 mName = name;
3566 }
3567
3568 void addChild(WindowToken token) {
3569 addChild(token, mWindowComparator);
3570 }
3571
3572 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003573 int getOrientation() {
3574 final WindowManagerPolicy policy = mService.mPolicy;
3575 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003576 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003577
3578 if (win != null) {
3579 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003580 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003581 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003582 if (mService.mKeyguardGoingAway) {
3583 // Keyguard can't affect the orientation if it is going away...
3584 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3585 return SCREEN_ORIENTATION_UNSET;
3586 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003587 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003588 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003589 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003590 }
3591
Andrii Kulian8ee72852017-03-10 10:36:45 -08003592 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003593
3594 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003595 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003596 }
3597
3598 return SCREEN_ORIENTATION_UNSET;
3599 }
3600
3601 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003602 String getName() {
3603 return mName;
3604 }
Robert Carr3b716242016-08-16 16:02:21 -07003605 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003606
3607 /**
3608 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3609 */
3610 @FunctionalInterface
3611 private interface Screenshoter<E> {
3612 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3613 boolean useIdentityTransform, int rotation);
3614 }
Craig Mautner59c00972012-07-30 12:10:24 -07003615}