blob: bf1c3f74632633037a946b50ad90a47bd413a737 [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 Ogunwale687b4272017-07-27 02:56:23 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070025import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
26import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070027import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
28import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070029import static android.view.Display.DEFAULT_DISPLAY;
30import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070031import static android.view.Surface.ROTATION_0;
32import static android.view.Surface.ROTATION_180;
33import static android.view.Surface.ROTATION_270;
34import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070035import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070036import static android.view.WindowManager.DOCKED_BOTTOM;
37import static android.view.WindowManager.DOCKED_INVALID;
38import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080039import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
40import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
41import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070042import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070043import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070044import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
45import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070046import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070047import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070048import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070050import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070051import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070052import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
53import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070054import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070055import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
56import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070057import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070058import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070059import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070060import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
61import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070062import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080063import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070064import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
67import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070079import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070080import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070081import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070082import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080083import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070084import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070085import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
86import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070087import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070089import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
90import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
92import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
93import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070094import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070096import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070097import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070098import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070099import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700100import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700101import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700103import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
104import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
105import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
106import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
107import static com.android.server.wm.proto.DisplayProto.DPI;
108import static com.android.server.wm.proto.DisplayProto.ID;
109import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
110import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
111import static com.android.server.wm.proto.DisplayProto.STACKS;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700112
Andrii Kulian3a507b52016-09-19 18:14:12 -0700113import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700114import android.app.ActivityManager.StackId;
Wale Ogunwale687b4272017-07-27 02:56:23 -0700115import android.app.WindowConfiguration;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700116import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700117import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700118import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100119import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700120import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800121import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700122import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700123import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100124import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700125import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700126import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700127import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700128import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700129import android.os.RemoteException;
130import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700131import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800132import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700133import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700134import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700135import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700136import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700137import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700138import android.view.Surface;
139import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700140import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700141
Bryce Lee48f4b572017-04-10 10:54:15 -0700142import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800143import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700144import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700145
Robert Carr3b716242016-08-16 16:02:21 -0700146import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700147import java.io.PrintWriter;
148import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700149import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700150import java.util.HashMap;
151import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700152import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700153import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800154import java.util.function.Consumer;
155import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700156
Craig Mautner59c00972012-07-30 12:10:24 -0700157/**
158 * Utility class for keeping track of the WindowStates and other pertinent contents of a
159 * particular Display.
160 *
161 * IMPORTANT: No method from this class should ever be used without holding
162 * WindowManagerService.mWindowMap.
163 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700164class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700165 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700166
167 /** Unique identifier of this stack. */
168 private final int mDisplayId;
169
Wale Ogunwale3a931692016-11-02 16:49:48 -0700170 /** The containers below are the only child containers the display can have. */
171 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700172 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700173 // Contains all non-app window containers that should be displayed above the app containers
174 // (e.g. Status bar)
175 private final NonAppWindowContainers mAboveAppWindowsContainers =
176 new NonAppWindowContainers("mAboveAppWindowsContainers");
177 // Contains all non-app window containers that should be displayed below the app containers
178 // (e.g. Wallpaper).
179 private final NonAppWindowContainers mBelowAppWindowsContainers =
180 new NonAppWindowContainers("mBelowAppWindowsContainers");
181 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
182 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
183 // window containers together and move them in-sync if/when needed.
184 private final NonAppWindowContainers mImeWindowsContainers =
185 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700186
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000187 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800188 private WindowState mTmpWindow2;
189 private WindowAnimator mTmpWindowAnimator;
190 private boolean mTmpRecoveringMemory;
191 private boolean mUpdateImeTarget;
192 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700193 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700194
Wale Ogunwale02319a62016-09-26 15:21:22 -0700195 // Mapping from a token IBinder to a WindowToken object on this display.
196 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
197
Andrii Kuliancd097992017-03-23 18:31:59 -0700198 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700199 int mInitialDisplayWidth = 0;
200 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700201 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700202
203 /**
204 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
205 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
206 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
207 */
Craig Mautner59c00972012-07-30 12:10:24 -0700208 int mBaseDisplayWidth = 0;
209 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700210 /**
211 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
212 * but can be set from Settings or via shell command "adb shell wm density".
213 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
214 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700215 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700216 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700217 private final DisplayInfo mDisplayInfo = new DisplayInfo();
218 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700219 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700220 /**
221 * For default display it contains real metrics, empty for others.
222 * @see WindowManagerService#createWatermarkInTransaction()
223 */
224 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
225 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
226 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700227
Andrii Kulian06d07d62017-03-14 11:11:47 -0700228 /**
229 * Compat metrics computed based on {@link #mDisplayMetrics}.
230 * @see #updateDisplayAndOrientation(int)
231 */
232 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
233
234 /** The desired scaling factor for compatible apps. */
235 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700236
Andrii Kulian8ee72852017-03-10 10:36:45 -0800237 /**
238 * Current rotation of the display.
239 * Constants as per {@link android.view.Surface.Rotation}.
240 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700241 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800242 */
243 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700244
Andrii Kulian8ee72852017-03-10 10:36:45 -0800245 /**
246 * Last applied orientation of the display.
247 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
248 *
249 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
250 */
251 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700252
Andrii Kulian8ee72852017-03-10 10:36:45 -0800253 /**
254 * Flag indicating that the application is receiving an orientation that has different metrics
255 * than it expected. E.g. Portrait instead of Landscape.
256 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700257 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800258 */
259 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700260
Andrii Kulian8ee72852017-03-10 10:36:45 -0800261 /**
262 * Orientation forced by some window. If there is no visible window that specifies orientation
263 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
264 *
265 * @see NonAppWindowContainers#getOrientation()
266 */
267 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700268
Andrii Kulian8ee72852017-03-10 10:36:45 -0800269 /**
270 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
271 * occluded.
272 *
273 * @see NonAppWindowContainers#getOrientation()
274 */
275 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
276
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700277 /**
278 * Keep track of wallpaper visibility to notify changes.
279 */
280 private boolean mLastWallpaperVisible = false;
281
Andrii Kulian06d07d62017-03-14 11:11:47 -0700282 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700283 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700284
Craig Mautner39834192012-09-02 07:47:24 -0700285 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700286 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700287 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700288 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800289 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700290
Craig Mautnerdc548482014-02-05 13:35:24 -0800291 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700292 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800293
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800294 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
295 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700296 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700297
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700298 /** Detect user tapping outside of current focused task bounds .*/
299 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700300
Craig Mautner6601b7b2013-04-29 10:29:11 -0700301 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700302 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700303
Craig Mautner6601b7b2013-04-29 10:29:11 -0700304 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800305 private final Rect mTmpRect = new Rect();
306 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700307 private final RectF mTmpRectF = new RectF();
308 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800309 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700310
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800311 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700312
Craig Mautner95da1082014-02-24 17:54:35 -0800313 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700314 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800315
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700316 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700317 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700318
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800319 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700320
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800321 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
322
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000323 private boolean mHaveBootMsg = false;
324 private boolean mHaveApp = false;
325 private boolean mHaveWallpaper = false;
326 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700327
328 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
329
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700330 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
331 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000332 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
333 new ApplySurfaceChangesTransactionState();
334 private final ScreenshotApplicationState mScreenshotApplicationState =
335 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700336
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700337 // True if this display is in the process of being removed. Used to determine if the removal of
338 // the display's direct children should be allowed.
339 private boolean mRemovingDisplay = false;
340
Bryce Leed1871262017-06-12 14:12:29 -0700341 // {@code false} if this display is in the processing of being created.
342 private boolean mDisplayReady = false;
343
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700344 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800345 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700346 int mInputMethodAnimLayerAdjustment;
347
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800348 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
349 WindowStateAnimator winAnimator = w.mWinAnimator;
350 if (winAnimator.hasSurface()) {
351 final boolean wasAnimating = winAnimator.mWasAnimating;
352 final boolean nowAnimating = winAnimator.stepAnimationLocked(
353 mTmpWindowAnimator.mCurrentTime);
354 winAnimator.mWasAnimating = nowAnimating;
355 mTmpWindowAnimator.orAnimating(nowAnimating);
356
357 if (DEBUG_WALLPAPER) Slog.v(TAG,
358 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
359
360 if (wasAnimating && !winAnimator.mAnimating
361 && mWallpaperController.isWallpaperTarget(w)) {
362 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
363 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
364 if (DEBUG_LAYOUT_REPEATS) {
365 mService.mWindowPlacerLocked.debugLayoutRepeats(
366 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
367 }
368 }
369 }
370
371 final AppWindowToken atoken = w.mAppToken;
372 if (winAnimator.mDrawState == READY_TO_SHOW) {
373 if (atoken == null || atoken.allDrawn) {
374 if (w.performShowLocked()) {
375 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
376 if (DEBUG_LAYOUT_REPEATS) {
377 mService.mWindowPlacerLocked.debugLayoutRepeats(
378 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
379 }
380 }
381 }
382 }
383 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
384 if (appAnimator != null && appAnimator.thumbnail != null) {
385 if (appAnimator.thumbnailTransactionSeq
386 != mTmpWindowAnimator.mAnimTransactionSequence) {
387 appAnimator.thumbnailTransactionSeq =
388 mTmpWindowAnimator.mAnimTransactionSequence;
389 appAnimator.thumbnailLayer = 0;
390 }
391 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
392 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
393 }
394 }
395 };
396
397 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
398 final WindowStateAnimator winAnimator = w.mWinAnimator;
399 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
400 return;
401 }
402
403 final int flags = w.mAttrs.flags;
404
405 // If this window is animating, make a note that we have an animating window and take
406 // care of a request to run a detached wallpaper animation.
407 if (winAnimator.mAnimating) {
408 if (winAnimator.mAnimation != null) {
409 if ((flags & FLAG_SHOW_WALLPAPER) != 0
410 && winAnimator.mAnimation.getDetachWallpaper()) {
411 mTmpWindow = w;
412 }
413 final int color = winAnimator.mAnimation.getBackgroundColor();
414 if (color != 0) {
415 final TaskStack stack = w.getStack();
416 if (stack != null) {
417 stack.setAnimationBackground(winAnimator, color);
418 }
419 }
420 }
421 mTmpWindowAnimator.setAnimating(true);
422 }
423
424 // If this window's app token is running a detached wallpaper animation, make a note so
425 // we can ensure the wallpaper is displayed behind it.
426 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
427 if (appAnimator != null && appAnimator.animation != null
428 && appAnimator.animating) {
429 if ((flags & FLAG_SHOW_WALLPAPER) != 0
430 && appAnimator.animation.getDetachWallpaper()) {
431 mTmpWindow = w;
432 }
433
434 final int color = appAnimator.animation.getBackgroundColor();
435 if (color != 0) {
436 final TaskStack stack = w.getStack();
437 if (stack != null) {
438 stack.setAnimationBackground(winAnimator, color);
439 }
440 }
441 }
442 };
443
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800444 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
445 final int lostFocusUid = mTmpWindow.mOwnerUid;
446 final Handler handler = mService.mH;
447 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
448 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
449 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
450 w.mAttrs.hideTimeoutMilliseconds);
451 }
452 }
453 };
454
455 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
456 final AppWindowToken focusedApp = mService.mFocusedApp;
457 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
458 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
459
460 if (!w.canReceiveKeys()) {
461 return false;
462 }
463
464 final AppWindowToken wtoken = w.mAppToken;
465
466 // If this window's application has been removed, just skip it.
467 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
468 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
469 + (wtoken.removed ? "removed" : "sendingToBottom"));
470 return false;
471 }
472
473 if (focusedApp == null) {
474 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
475 + " using new focus @ " + w);
476 mTmpWindow = w;
477 return true;
478 }
479
480 if (!focusedApp.windowsAreFocusable()) {
481 // Current focused app windows aren't focusable...
482 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
483 + " focusable using new focus @ " + w);
484 mTmpWindow = w;
485 return true;
486 }
487
488 // Descend through all of the app tokens and find the first that either matches
489 // win.mAppToken (return win) or mFocusedApp (return null).
490 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
491 if (focusedApp.compareTo(wtoken) > 0) {
492 // App stack below focused app stack. No focus for you!!!
493 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
494 "findFocusedWindow: Reached focused app=" + focusedApp);
495 mTmpWindow = null;
496 return true;
497 }
498 }
499
500 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
501 mTmpWindow = w;
502 return true;
503 };
504
505 private final Consumer<WindowState> mPrepareWindowSurfaces =
506 w -> w.mWinAnimator.prepareSurfaceLocked(true);
507
508 private final Consumer<WindowState> mPerformLayout = w -> {
509 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
510 // wasting time and funky changes while a window is animating away.
511 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
512 || w.isGoneForLayoutLw();
513
514 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
515 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
516 + " mLayoutAttached=" + w.mLayoutAttached
517 + " screen changed=" + w.isConfigChanged());
518 final AppWindowToken atoken = w.mAppToken;
519 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
520 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
521 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
522 + " parentHidden=" + w.isParentWindowHidden());
523 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
524 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
525 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
526 + " parentHidden=" + w.isParentWindowHidden());
527 }
528
529 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
530 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
531 // since that means "perform layout as normal, just don't display").
532 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
533 || ((w.isConfigChanged() || w.setReportResizeHints())
534 && !w.isGoneForLayoutLw() &&
535 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
536 (w.mHasSurface && w.mAppToken != null &&
537 w.mAppToken.layoutConfigChanges)))) {
538 if (!w.mLayoutAttached) {
539 if (mTmpInitial) {
540 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
541 w.mContentChanged = false;
542 }
543 if (w.mAttrs.type == TYPE_DREAM) {
544 // Don't layout windows behind a dream, so that if it does stuff like hide
545 // the status bar we won't get a bad transition when it goes away.
546 mTmpWindow = w;
547 }
548 w.mLayoutNeeded = false;
549 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200550 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800551 mService.mPolicy.layoutWindowLw(w, null);
552 w.mLayoutSeq = mService.mLayoutSeq;
553
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200554 // If this is the first layout, we need to initialize the last inset values as
555 // otherwise we'd immediately cause an unnecessary resize.
556 if (firstLayout) {
557 w.updateLastInsetValues();
558 }
559
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800560 // Window frames may have changed. Update dim layer with the new bounds.
561 final Task task = w.getTask();
562 if (task != null) {
563 mDimLayerController.updateDimLayer(task);
564 }
565
566 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
567 + " mContainingFrame=" + w.mContainingFrame
568 + " mDisplayFrame=" + w.mDisplayFrame);
569 }
570 }
571 };
572
573 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
574 if (w.mLayoutAttached) {
575 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
576 + " mViewVisibility=" + w.mViewVisibility
577 + " mRelayoutCalled=" + w.mRelayoutCalled);
578 // If this view is GONE, then skip it -- keep the current frame, and let the caller
579 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
580 // windows, since that means "perform layout as normal, just don't display").
581 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
582 return;
583 }
584 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
585 || w.mLayoutNeeded) {
586 if (mTmpInitial) {
587 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
588 w.mContentChanged = false;
589 }
590 w.mLayoutNeeded = false;
591 w.prelayout();
592 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
593 w.mLayoutSeq = mService.mLayoutSeq;
594 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
595 + " mContainingFrame=" + w.mContainingFrame
596 + " mDisplayFrame=" + w.mDisplayFrame);
597 }
598 } else if (w.mAttrs.type == TYPE_DREAM) {
599 // Don't layout windows behind a dream, so that if it does stuff like hide the
600 // status bar we won't get a bad transition when it goes away.
601 mTmpWindow = mTmpWindow2;
602 }
603 };
604
605 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
606 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
607 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
608 return w.canBeImeTarget();
609 };
610
611 private final Consumer<WindowState> mApplyPostLayoutPolicy =
612 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
613 mService.mInputMethodTarget);
614
615 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
616 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
617 final boolean obscuredChanged = w.mObscured !=
618 mTmpApplySurfaceChangesTransactionState.obscured;
619 final RootWindowContainer root = mService.mRoot;
620 // Only used if default window
621 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
622
623 // Update effect.
624 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
625 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
626 final boolean isDisplayed = w.isDisplayedLw();
627
628 if (isDisplayed && w.isObscuringDisplay()) {
629 // This window completely covers everything behind it, so we want to leave all
630 // of them as undimmed (for performance reasons).
631 root.mObscuringWindow = w;
632 mTmpApplySurfaceChangesTransactionState.obscured = true;
633 }
634
635 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
636 root.handleNotObscuredLocked(w,
637 mTmpApplySurfaceChangesTransactionState.obscured,
638 mTmpApplySurfaceChangesTransactionState.syswin);
639
640 if (w.mHasSurface && isDisplayed) {
641 final int type = w.mAttrs.type;
642 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
643 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
644 mTmpApplySurfaceChangesTransactionState.syswin = true;
645 }
646 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
647 && w.mAttrs.preferredRefreshRate != 0) {
648 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
649 = w.mAttrs.preferredRefreshRate;
650 }
651 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
652 && w.mAttrs.preferredDisplayModeId != 0) {
653 mTmpApplySurfaceChangesTransactionState.preferredModeId
654 = w.mAttrs.preferredDisplayModeId;
655 }
656 }
657 }
658
659 w.applyDimLayerIfNeeded();
660
661 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
662 && mWallpaperController.isWallpaperTarget(w)) {
663 // This is the wallpaper target and its obscured state changed... make sure the
664 // current wallpaper's visibility has been updated accordingly.
665 mWallpaperController.updateWallpaperVisibility();
666 }
667
668 w.handleWindowMovedIfNeeded();
669
670 final WindowStateAnimator winAnimator = w.mWinAnimator;
671
672 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
673 w.mContentChanged = false;
674
675 // Moved from updateWindowsAndWallpaperLocked().
676 if (w.mHasSurface) {
677 // Take care of the window being ready to display.
678 final boolean committed = winAnimator.commitFinishDrawingLocked();
679 if (isDefaultDisplay && committed) {
680 if (w.mAttrs.type == TYPE_DREAM) {
681 // HACK: When a dream is shown, it may at that point hide the lock screen.
682 // So we need to redo the layout to let the phone window manager make this
683 // happen.
684 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
685 if (DEBUG_LAYOUT_REPEATS) {
686 surfacePlacer.debugLayoutRepeats(
687 "dream and commitFinishDrawingLocked true",
688 pendingLayoutChanges);
689 }
690 }
691 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
692 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
693 "First draw done in potential wallpaper target " + w);
694 root.mWallpaperMayChange = true;
695 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
696 if (DEBUG_LAYOUT_REPEATS) {
697 surfacePlacer.debugLayoutRepeats(
698 "wallpaper and commitFinishDrawingLocked true",
699 pendingLayoutChanges);
700 }
701 }
702 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700703 final TaskStack stack = w.getStack();
704 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
705 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800706 // Updates the shown frame before we set up the surface. This is needed
707 // because the resizing could change the top-left position (in addition to
708 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
709 // position the surface.
710 //
711 // If an animation is being started, we can't call this method because the
712 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700713 // we do want to update the position if the window is animating. We make an exception
714 // for the bounds animating state, where an application may have been waiting
715 // for an exit animation to start, but instead enters PiP. We need to ensure
716 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800717 winAnimator.computeShownFrameLocked();
718 }
719 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
720 }
721
722 final AppWindowToken atoken = w.mAppToken;
723 if (atoken != null) {
724 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
725 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
726 mTmpUpdateAllDrawn.add(atoken);
727 }
728 }
729
730 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
731 && w.isDisplayedLw()) {
732 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
733 }
734
735 w.updateResizingWindowIfNeeded();
736 };
737
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800738 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800739 * Create new {@link DisplayContent} instance, add itself to the root window container and
740 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700741 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800742 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700743 * @param layersController window layer controller used to assign layer to the windows on this
744 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700745 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
746 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700747 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700748 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700749 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800750 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
751 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
752 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
753 + " new=" + display);
754 }
755
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700756 mDisplay = display;
757 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700758 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700759 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700760 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700761 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700762 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700763 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700764 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800765 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700766 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800767 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700768
769 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700770 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700771 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700772 super.addChild(mAboveAppWindowsContainers, null);
773 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800774
775 // Add itself as a child to the root container.
776 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700777
778 // TODO(b/62541591): evaluate whether this is the best spot to declare the
779 // {@link DisplayContent} ready for use.
780 mDisplayReady = true;
781 }
782
783 boolean isReady() {
784 // The display is ready when the system and the individual display are both ready.
785 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700786 }
787
788 int getDisplayId() {
789 return mDisplayId;
790 }
791
Wale Ogunwale02319a62016-09-26 15:21:22 -0700792 WindowToken getWindowToken(IBinder binder) {
793 return mTokenMap.get(binder);
794 }
795
796 AppWindowToken getAppWindowToken(IBinder binder) {
797 final WindowToken token = getWindowToken(binder);
798 if (token == null) {
799 return null;
800 }
801 return token.asAppWindowToken();
802 }
803
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700804 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700805 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
806 if (dc != null) {
807 // We currently don't support adding a window token to the display if the display
808 // already has the binder mapped to another token. If there is a use case for supporting
809 // this moving forward we will either need to merge the WindowTokens some how or have
810 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700811 throw new IllegalArgumentException("Can't map token=" + token + " to display="
812 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700813 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700814 if (binder == null) {
815 throw new IllegalArgumentException("Can't map token=" + token + " to display="
816 + getName() + " binder is null");
817 }
818 if (token == null) {
819 throw new IllegalArgumentException("Can't map null token to display="
820 + getName() + " binder=" + binder);
821 }
822
Wale Ogunwale02319a62016-09-26 15:21:22 -0700823 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700824
825 if (token.asAppWindowToken() == null) {
826 // Add non-app token to container hierarchy on the display. App tokens are added through
827 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700828 switch (token.windowType) {
829 case TYPE_WALLPAPER:
830 mBelowAppWindowsContainers.addChild(token);
831 break;
832 case TYPE_INPUT_METHOD:
833 case TYPE_INPUT_METHOD_DIALOG:
834 mImeWindowsContainers.addChild(token);
835 break;
836 default:
837 mAboveAppWindowsContainers.addChild(token);
838 break;
839 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700840 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700841 }
842
843 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700844 final WindowToken token = mTokenMap.remove(binder);
845 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800846 token.setExiting();
847 }
848 return token;
849 }
850
851 /** Changes the display the input window token is housed on to this one. */
852 void reParentWindowToken(WindowToken token) {
853 final DisplayContent prevDc = token.getDisplayContent();
854 if (prevDc == this) {
855 return;
856 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800857 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
858 && token.asAppWindowToken() == null) {
859 // Removed the token from the map, but made sure it's not an app token before removing
860 // from parent.
861 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700862 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800863
864 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700865 }
866
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700867 void removeAppToken(IBinder binder) {
868 final WindowToken token = removeWindowToken(binder);
869 if (token == null) {
870 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
871 return;
872 }
873
874 final AppWindowToken appToken = token.asAppWindowToken();
875
876 if (appToken == null) {
877 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
878 return;
879 }
880
881 appToken.onRemovedFromDisplay();
882 }
883
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700884 Display getDisplay() {
885 return mDisplay;
886 }
887
Craig Mautner59c00972012-07-30 12:10:24 -0700888 DisplayInfo getDisplayInfo() {
889 return mDisplayInfo;
890 }
891
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700892 DisplayMetrics getDisplayMetrics() {
893 return mDisplayMetrics;
894 }
895
Andrii Kulian8ee72852017-03-10 10:36:45 -0800896 int getRotation() {
897 return mRotation;
898 }
899
900 void setRotation(int newRotation) {
901 mRotation = newRotation;
902 }
903
904 int getLastOrientation() {
905 return mLastOrientation;
906 }
907
908 void setLastOrientation(int orientation) {
909 mLastOrientation = orientation;
910 }
911
912 boolean getAltOrientation() {
913 return mAltOrientation;
914 }
915
916 void setAltOrientation(boolean altOrientation) {
917 mAltOrientation = altOrientation;
918 }
919
920 int getLastWindowForcedOrientation() {
921 return mLastWindowForcedOrientation;
922 }
923
Andrii Kulian06d07d62017-03-14 11:11:47 -0700924 /**
925 * Update rotation of the display.
926 *
927 * Returns true if the rotation has been changed. In this case YOU MUST CALL
928 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
929 */
930 boolean updateRotationUnchecked(boolean inTransaction) {
931 if (mService.mDeferredRotationPauseCount > 0) {
932 // Rotation updates have been paused temporarily. Defer the update until
933 // updates have been resumed.
934 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
935 return false;
936 }
937
938 ScreenRotationAnimation screenRotationAnimation =
939 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
940 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
941 // Rotation updates cannot be performed while the previous rotation change
942 // animation is still in progress. Skip this update. We will try updating
943 // again after the animation is finished and the display is unfrozen.
944 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
945 return false;
946 }
947 if (mService.mDisplayFrozen) {
948 // Even if the screen rotation animation has finished (e.g. isAnimating
949 // returns false), there is still some time where we haven't yet unfrozen
950 // the display. We also need to abort rotation here.
951 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
952 "Deferring rotation, still finishing previous rotation");
953 return false;
954 }
955
956 if (!mService.mDisplayEnabled) {
957 // No point choosing a rotation if the display is not enabled.
958 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
959 return false;
960 }
961
962 final int oldRotation = mRotation;
963 final int lastOrientation = mLastOrientation;
964 final boolean oldAltOrientation = mAltOrientation;
965 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700966 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
967 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700968
Robert Carr897215d2017-03-29 12:25:50 -0700969 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700970 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
971 if (seamlessRotated != null) {
972 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
973 // to complete (that is, waiting for windows to redraw). It's tempting to check
974 // w.mSeamlessRotationCount but that could be incorrect in the case of
975 // window-removal.
976 return false;
977 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700978 }
979
980 // TODO: Implement forced rotation changes.
981 // Set mAltOrientation to indicate that the application is receiving
982 // an orientation that has different metrics than it expected.
983 // eg. Portrait instead of Landscape.
984
985 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
986 lastOrientation, rotation);
987
988 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
989 + ", got rotation " + rotation + " which has "
990 + (altOrientation ? "incompatible" : "compatible") + " metrics");
991
992 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
993 // No change.
994 return false;
995 }
996
997 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
998 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
999 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1000
1001 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1002 mService.mWaitingForConfig = true;
1003 }
1004
1005 mRotation = rotation;
1006 mAltOrientation = altOrientation;
1007 if (isDefaultDisplay) {
1008 mService.mPolicy.setRotationLw(rotation);
1009 }
1010
1011 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1012 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1013 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1014 WINDOW_FREEZE_TIMEOUT_DURATION);
1015
1016 setLayoutNeeded();
1017 final int[] anim = new int[2];
1018 if (isDimming()) {
1019 anim[0] = anim[1] = 0;
1020 } else {
1021 mService.mPolicy.selectRotationAnimationLw(anim);
1022 }
1023
1024 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001025 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001026 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1027 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1028 mDisplayId);
1029 } else {
1030 // The screen rotation animation uses a screenshot to freeze the screen
1031 // while windows resize underneath.
1032 // When we are rotating seamlessly, we allow the elements to transition
1033 // to their rotated state independently and without a freeze required.
1034 screenRotationAnimation = null;
1035
1036 // We have to reset this in case a window was removed before it
1037 // finished seamless rotation.
1038 mService.mSeamlessRotationCount = 0;
1039 }
1040
1041 // We need to update our screen size information to match the new rotation. If the rotation
1042 // has actually changed then this method will return true and, according to the comment at
1043 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1044 // By updating the Display info here it will be available to
1045 // #computeScreenConfiguration() later.
1046 updateDisplayAndOrientation(getConfiguration().uiMode);
1047
1048 if (!inTransaction) {
1049 if (SHOW_TRANSACTIONS) {
1050 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1051 }
1052 mService.openSurfaceTransaction();
1053 }
1054 try {
1055 // NOTE: We disable the rotation in the emulator because
1056 // it doesn't support hardware OpenGL emulation yet.
1057 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1058 && screenRotationAnimation.hasScreenshot()) {
1059 if (screenRotationAnimation.setRotationInTransaction(
1060 rotation, mService.mFxSession,
1061 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1062 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1063 mService.scheduleAnimationLocked();
1064 }
1065 }
1066
1067 if (rotateSeamlessly) {
1068 forAllWindows(w -> {
1069 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1070 }, true /* traverseTopToBottom */);
1071 }
1072
1073 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1074 } finally {
1075 if (!inTransaction) {
1076 mService.closeSurfaceTransaction();
1077 if (SHOW_LIGHT_TRANSACTIONS) {
1078 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1079 }
1080 }
1081 }
1082
1083 forAllWindows(w -> {
1084 // Discard surface after orientation change, these can't be reused.
1085 if (w.mAppToken != null) {
1086 w.mAppToken.destroySavedSurfaces();
1087 }
1088 if (w.mHasSurface && !rotateSeamlessly) {
1089 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001090 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001091 mService.mRoot.mOrientationChangeComplete = false;
1092 w.mLastFreezeDuration = 0;
1093 }
1094 w.mReportOrientationChanged = true;
1095 }, true /* traverseTopToBottom */);
1096
1097 if (rotateSeamlessly) {
1098 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1099 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1100 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1101 }
1102
1103 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1104 final WindowManagerService.RotationWatcher rotationWatcher
1105 = mService.mRotationWatchers.get(i);
1106 if (rotationWatcher.mDisplayId == mDisplayId) {
1107 try {
1108 rotationWatcher.mWatcher.onRotationChanged(rotation);
1109 } catch (RemoteException e) {
1110 // Ignore
1111 }
1112 }
1113 }
1114
1115 // TODO (multi-display): Magnification is supported only for the default display.
1116 // Announce rotation only if we will not animate as we already have the
1117 // windows in final state. Otherwise, we make this call at the rotation end.
1118 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1119 && isDefaultDisplay) {
1120 mService.mAccessibilityController.onRotationChangedLocked(this);
1121 }
1122
1123 return true;
1124 }
1125
1126 /**
1127 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1128 * changed.
1129 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1130 */
1131 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1132 // Use the effective "visual" dimensions based on current rotation
1133 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1134 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1135 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1136 int dw = realdw;
1137 int dh = realdh;
1138
1139 if (mAltOrientation) {
1140 if (realdw > realdh) {
1141 // Turn landscape into portrait.
1142 int maxw = (int)(realdh/1.3f);
1143 if (maxw < realdw) {
1144 dw = maxw;
1145 }
1146 } else {
1147 // Turn portrait into landscape.
1148 int maxh = (int)(realdw/1.3f);
1149 if (maxh < realdh) {
1150 dh = maxh;
1151 }
1152 }
1153 }
1154
1155 // Update application display metrics.
1156 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1157 mDisplayId);
1158 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1159 mDisplayId);
1160 mDisplayInfo.rotation = mRotation;
1161 mDisplayInfo.logicalWidth = dw;
1162 mDisplayInfo.logicalHeight = dh;
1163 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1164 mDisplayInfo.appWidth = appWidth;
1165 mDisplayInfo.appHeight = appHeight;
1166 if (isDefaultDisplay) {
1167 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1168 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1169 }
1170 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1171 if (mDisplayScalingDisabled) {
1172 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1173 } else {
1174 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1175 }
1176
1177 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1178 mDisplayInfo);
1179
1180 mBaseDisplayRect.set(0, 0, dw, dh);
1181
1182 if (isDefaultDisplay) {
1183 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1184 mCompatDisplayMetrics);
1185 }
1186 return mDisplayInfo;
1187 }
1188
1189 /**
1190 * Compute display configuration based on display properties and policy settings.
1191 * Do not call if mDisplayReady == false.
1192 */
1193 void computeScreenConfiguration(Configuration config) {
1194 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1195
1196 final int dw = displayInfo.logicalWidth;
1197 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001198 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1199 // TODO: Probably best to set this based on some setting in the display content object,
1200 // so the display can be configured for things like fullscreen.
1201 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001202
Andrii Kulian06d07d62017-03-14 11:11:47 -07001203 config.screenWidthDp =
1204 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1205 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1206 config.screenHeightDp =
1207 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1208 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001209
1210 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1211 final int leftInset = mTmpRect.left;
1212 final int topInset = mTmpRect.top;
1213 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001214 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1215 leftInset + displayInfo.appWidth /* right */,
1216 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001217 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1218 || displayInfo.rotation == Surface.ROTATION_270);
1219
1220 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1221 mDisplayMetrics.density, config);
1222
1223 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1224 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1225 ? Configuration.SCREENLAYOUT_ROUND_YES
1226 : Configuration.SCREENLAYOUT_ROUND_NO);
1227
1228 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1229 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1230 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1231 dh, mDisplayId);
1232 config.densityDpi = displayInfo.logicalDensityDpi;
1233
1234 config.colorMode =
1235 (displayInfo.isHdr()
1236 ? Configuration.COLOR_MODE_HDR_YES
1237 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001238 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001239 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1240 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1241
1242 // Update the configuration based on available input devices, lid switch,
1243 // and platform configuration.
1244 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1245 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1246 config.navigation = Configuration.NAVIGATION_NONAV;
1247
1248 int keyboardPresence = 0;
1249 int navigationPresence = 0;
1250 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1251 final int len = devices != null ? devices.length : 0;
1252 for (int i = 0; i < len; i++) {
1253 InputDevice device = devices[i];
1254 if (!device.isVirtual()) {
1255 final int sources = device.getSources();
1256 final int presenceFlag = device.isExternal() ?
1257 WindowManagerPolicy.PRESENCE_EXTERNAL :
1258 WindowManagerPolicy.PRESENCE_INTERNAL;
1259
1260 // TODO(multi-display): Configure on per-display basis.
1261 if (mService.mIsTouchDevice) {
1262 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1263 InputDevice.SOURCE_TOUCHSCREEN) {
1264 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1265 }
1266 } else {
1267 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1268 }
1269
1270 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1271 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1272 navigationPresence |= presenceFlag;
1273 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1274 && config.navigation == Configuration.NAVIGATION_NONAV) {
1275 config.navigation = Configuration.NAVIGATION_DPAD;
1276 navigationPresence |= presenceFlag;
1277 }
1278
1279 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1280 config.keyboard = Configuration.KEYBOARD_QWERTY;
1281 keyboardPresence |= presenceFlag;
1282 }
1283 }
1284 }
1285
1286 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1287 config.navigation = Configuration.NAVIGATION_DPAD;
1288 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1289 }
1290
1291 // Determine whether a hard keyboard is available and enabled.
1292 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1293 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1294 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1295 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1296 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1297 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1298 }
1299
1300 // Let the policy update hidden states.
1301 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1302 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1303 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1304 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1305 }
1306
1307 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1308 int displayId) {
1309 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1310 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1311 final int unrotDw, unrotDh;
1312 if (rotated) {
1313 unrotDw = dh;
1314 unrotDh = dw;
1315 } else {
1316 unrotDw = dw;
1317 unrotDh = dh;
1318 }
1319 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1320 displayId);
1321 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1322 displayId);
1323 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1324 displayId);
1325 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1326 displayId);
1327 return sw;
1328 }
1329
1330 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1331 DisplayMetrics dm, int dw, int dh, int displayId) {
1332 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1333 displayId);
1334 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1335 uiMode, displayId);
1336 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1337 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1338 if (curSize == 0 || size < curSize) {
1339 curSize = size;
1340 }
1341 return curSize;
1342 }
1343
1344 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1345 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1346
1347 // We need to determine the smallest width that will occur under normal
1348 // operation. To this, start with the base screen size and compute the
1349 // width under the different possible rotations. We need to un-rotate
1350 // the current screen dimensions before doing this.
1351 int unrotDw, unrotDh;
1352 if (rotated) {
1353 unrotDw = dh;
1354 unrotDh = dw;
1355 } else {
1356 unrotDw = dw;
1357 unrotDh = dh;
1358 }
1359 displayInfo.smallestNominalAppWidth = 1<<30;
1360 displayInfo.smallestNominalAppHeight = 1<<30;
1361 displayInfo.largestNominalAppWidth = 0;
1362 displayInfo.largestNominalAppHeight = 0;
1363 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1364 unrotDh);
1365 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1366 unrotDw);
1367 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1368 unrotDh);
1369 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1370 unrotDw);
1371 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1372 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1373 displayId);
1374 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1375 displayId);
1376 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1377 displayId);
1378 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1379 displayId);
1380 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1381 outConfig.screenLayout = sl;
1382 }
1383
1384 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1385 int uiMode, int displayId) {
1386 // Get the app screen size at this rotation.
1387 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1388 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1389
1390 // Compute the screen layout size class for this rotation.
1391 int longSize = w;
1392 int shortSize = h;
1393 if (longSize < shortSize) {
1394 int tmp = longSize;
1395 longSize = shortSize;
1396 shortSize = tmp;
1397 }
1398 longSize = (int)(longSize/density);
1399 shortSize = (int)(shortSize/density);
1400 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1401 }
1402
1403 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1404 int uiMode, int dw, int dh) {
1405 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1406 displayId);
1407 if (width < displayInfo.smallestNominalAppWidth) {
1408 displayInfo.smallestNominalAppWidth = width;
1409 }
1410 if (width > displayInfo.largestNominalAppWidth) {
1411 displayInfo.largestNominalAppWidth = width;
1412 }
1413 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1414 displayId);
1415 if (height < displayInfo.smallestNominalAppHeight) {
1416 displayInfo.smallestNominalAppHeight = height;
1417 }
1418 if (height > displayInfo.largestNominalAppHeight) {
1419 displayInfo.largestNominalAppHeight = height;
1420 }
1421 }
1422
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001423 DockedStackDividerController getDockedDividerController() {
1424 return mDividerControllerLocked;
1425 }
1426
Winson Chung655332c2016-10-31 13:14:28 -07001427 PinnedStackController getPinnedStackController() {
1428 return mPinnedStackControllerLocked;
1429 }
1430
Jeff Browna506a6e2013-06-04 00:02:38 -07001431 /**
1432 * Returns true if the specified UID has access to this display.
1433 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001434 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001435 return mDisplay.hasAccess(uid);
1436 }
1437
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001438 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001439 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001440 }
1441
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001442 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001443 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001444 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001445 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001446 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001447 }
1448
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001449 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001450 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1451 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001452 if (stack.mStackId == stackId) {
1453 return stack;
1454 }
1455 }
1456 return null;
1457 }
1458
Bryce Lee48f4b572017-04-10 10:54:15 -07001459 @VisibleForTesting
1460 int getStackCount() {
1461 return mTaskStackContainers.size();
1462 }
1463
1464 @VisibleForTesting
Wale Ogunwale98d62312017-07-12 09:24:56 -07001465 int getStackPosById(int stackId) {
Bryce Lee48f4b572017-04-10 10:54:15 -07001466 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1467 final TaskStack stack = mTaskStackContainers.get(i);
1468 if (stack.mStackId == stackId) {
1469 return i;
1470 }
1471 }
1472 return -1;
1473 }
1474
Andrii Kulian441e4492016-09-29 15:25:00 -07001475 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001476 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001477 super.onConfigurationChanged(newParentConfig);
1478
Andrii Kulian3a507b52016-09-19 18:14:12 -07001479 // The display size information is heavily dependent on the resources in the current
1480 // configuration, so we need to reconfigure it every time the configuration changes.
1481 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1482 mService.reconfigureDisplayLocked(this);
1483
1484 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001485 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001486 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001487
Andrii Kulian441e4492016-09-29 15:25:00 -07001488 /**
1489 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1490 * bounds were updated.
1491 */
1492 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001493 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1494 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001495 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001496 changedStackList.add(stack.mStackId);
1497 }
1498 }
Winson Chung32c566f2017-04-11 18:31:21 -07001499
1500 // If there was no pinned stack, we still need to notify the controller of the display info
1501 // update as a result of the config change. We do this here to consolidate the flow between
1502 // changes when there is and is not a stack.
1503 if (getStackById(PINNED_STACK_ID) == null) {
1504 mPinnedStackControllerLocked.onDisplayInfoChanged();
1505 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001506 }
1507
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001508 @Override
1509 boolean fillsParent() {
1510 return true;
1511 }
1512
1513 @Override
1514 boolean isVisible() {
1515 return true;
1516 }
1517
1518 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001519 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001520 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001521 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001522 }
1523
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001524 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001525 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1526 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1527 // target, or here in order if there isn't an IME target.
1528 if (traverseTopToBottom) {
1529 for (int i = mChildren.size() - 1; i >= 0; --i) {
1530 final DisplayChildWindowContainer child = mChildren.get(i);
1531 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1532 // In this case the Ime windows will be processed above their target so we skip
1533 // here.
1534 continue;
1535 }
1536 if (child.forAllWindows(callback, traverseTopToBottom)) {
1537 return true;
1538 }
1539 }
1540 } else {
1541 final int count = mChildren.size();
1542 for (int i = 0; i < count; i++) {
1543 final DisplayChildWindowContainer child = mChildren.get(i);
1544 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1545 // In this case the Ime windows will be processed above their target so we skip
1546 // here.
1547 continue;
1548 }
1549 if (child.forAllWindows(callback, traverseTopToBottom)) {
1550 return true;
1551 }
1552 }
1553 }
1554 return false;
1555 }
1556
1557 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1558 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1559 }
1560
Wale Ogunwale399c8692017-05-08 14:22:42 -07001561 @Override
1562 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001563 final WindowManagerPolicy policy = mService.mPolicy;
1564
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001565 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001566 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001567 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001568 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001569 // If the display is frozen, some activities may be in the middle of restarting, and
1570 // thus have removed their old window. If the window has the flag to hide the lock
1571 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1572 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001573 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001574 } else if (policy.isKeyguardLocked()) {
1575 // Use the last orientation the while the display is frozen with the keyguard
1576 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1577 // window. We don't want to check the show when locked window directly though as
1578 // things aren't stable while the display is frozen, for example the window could be
1579 // momentarily unavailable due to activity relaunch.
1580 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001581 + "return " + mLastOrientation);
1582 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001583 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001584 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001585 final int orientation = mAboveAppWindowsContainers.getOrientation();
1586 if (orientation != SCREEN_ORIENTATION_UNSET) {
1587 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001588 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001589 }
1590
Wale Ogunwale399c8692017-05-08 14:22:42 -07001591 // Top system windows are not requesting an orientation. Start searching from apps.
1592 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001593 }
1594
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001595 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001596 // Check if display metrics changed and update base values if needed.
1597 updateBaseDisplayMetricsIfNeeded();
1598
Craig Mautner722285e2012-09-07 13:55:58 -07001599 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001600 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001601
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001602 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1603 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001604 }
Craig Mautner722285e2012-09-07 13:55:58 -07001605 }
1606
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001607 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001608 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1609 if (displayManagerInternal != null) {
1610 // Bootstrap the default logical display from the display manager.
1611 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1612 if (newDisplayInfo != null) {
1613 mDisplayInfo.copyFrom(newDisplayInfo);
1614 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001615 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001616
Andrii Kuliancd097992017-03-23 18:31:59 -07001617 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1618 mDisplayInfo.logicalDensityDpi);
1619 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1620 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1621 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001622 }
1623
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001624 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001625 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001626 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001627 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001628 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1629 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001630 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001631 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001632 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001633 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001634 out.set(left, top, left + width, top + height);
1635 }
1636
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001637 private void getLogicalDisplayRect(Rect out, int orientation) {
1638 getLogicalDisplayRect(out);
1639
1640 // Rotate the Rect if needed.
1641 final int currentRotation = mDisplayInfo.rotation;
1642 final int rotationDelta = deltaRotation(currentRotation, orientation);
1643 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1644 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1645 mTmpRectF.set(out);
1646 mTmpMatrix.mapRect(mTmpRectF);
1647 mTmpRectF.round(out);
1648 }
1649 }
1650
Andrii Kuliancd097992017-03-23 18:31:59 -07001651 /**
1652 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1653 * values.
1654 */
1655 private void updateBaseDisplayMetricsIfNeeded() {
1656 // Get real display metrics without overrides from WM.
1657 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1658 final int orientation = mDisplayInfo.rotation;
1659 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1660 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1661 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1662 final int newDensity = mDisplayInfo.logicalDensityDpi;
1663
1664 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1665 || mInitialDisplayHeight != newHeight
1666 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1667
1668 if (displayMetricsChanged) {
1669 // Check if display size or density is forced.
1670 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1671 || mBaseDisplayHeight != mInitialDisplayHeight;
1672 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1673
1674 // If there is an override set for base values - use it, otherwise use new values.
1675 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1676 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1677 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1678
1679 // Real display metrics changed, so we should also update initial values.
1680 mInitialDisplayWidth = newWidth;
1681 mInitialDisplayHeight = newHeight;
1682 mInitialDisplayDensity = newDensity;
1683 mService.reconfigureDisplayLocked(this);
1684 }
1685 }
1686
Bryce Lee27cec322017-03-21 09:41:37 -07001687 /** Sets the maximum width the screen resolution can be */
1688 void setMaxUiWidth(int width) {
1689 if (DEBUG_DISPLAY) {
1690 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1691 }
1692
1693 mMaxUiWidth = width;
1694
1695 // Update existing metrics.
1696 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1697 }
1698
1699 /** Update base (override) display metrics. */
1700 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1701 mBaseDisplayWidth = baseWidth;
1702 mBaseDisplayHeight = baseHeight;
1703 mBaseDisplayDensity = baseDensity;
1704
1705 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1706 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1707 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1708 mBaseDisplayWidth = mMaxUiWidth;
1709
1710 if (DEBUG_DISPLAY) {
1711 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1712 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1713 + " on display:" + getDisplayId());
1714 }
1715 }
1716
1717 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1718 }
1719
Chong Zhangf66db432016-01-13 10:39:51 -08001720 void getContentRect(Rect out) {
1721 out.set(mContentRect);
1722 }
1723
Wale Ogunwale1666e312016-12-16 11:27:18 -08001724 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1725 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1726 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001727
Wale Ogunwale1666e312016-12-16 11:27:18 -08001728 TaskStack stack = getStackById(stackId);
1729 if (stack != null) {
1730 // It's already attached to the display...clear mDeferRemoval and move stack to
1731 // appropriate z-order on display as needed.
1732 stack.mDeferRemoval = false;
1733 // We're not moving the display to front when we're adding stacks, only when
1734 // requested to change the position of stack explicitly.
1735 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1736 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001737 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001738 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001739 mTaskStackContainers.addStackToDisplay(stack, onTop);
1740 }
1741
Wale Ogunwale1666e312016-12-16 11:27:18 -08001742 if (stackId == DOCKED_STACK_ID) {
1743 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001744 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001745 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001746 }
1747
Andrii Kulian51c1b672017-04-07 18:39:32 -07001748 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001749 final DisplayContent prevDc = stack.getDisplayContent();
1750 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001751 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1752 + " which is not currently attached to any display");
1753 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001754 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001755 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1756 + " to its current displayId=" + mDisplayId);
1757 }
1758
Wale Ogunwale1666e312016-12-16 11:27:18 -08001759 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001760 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001761 }
1762
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001763 @Override
1764 protected void addChild(DisplayChildWindowContainer child,
1765 Comparator<DisplayChildWindowContainer> comparator) {
1766 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1767 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001768
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001769 @Override
1770 protected void addChild(DisplayChildWindowContainer child, int index) {
1771 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1772 }
1773
1774 @Override
1775 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001776 // Only allow removal of direct children from this display if the display is in the process
1777 // of been removed.
1778 if (mRemovingDisplay) {
1779 super.removeChild(child);
1780 return;
1781 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001782 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001783 }
1784
Andrii Kuliand2765632016-12-12 22:26:34 -08001785 @Override
1786 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1787 // Children of the display are statically ordered, so the real intention here is to perform
1788 // the operation on the display and not the static direct children.
1789 getParent().positionChildAt(position, this, includingParents);
1790 }
1791
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001792 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001793 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1794 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001795 final int taskId = stack.taskIdFromPoint(x, y);
1796 if (taskId != -1) {
1797 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001798 }
1799 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001800 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001801 }
1802
Chong Zhang8e89b312015-09-09 15:09:30 -07001803 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001804 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001805 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001806 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001807 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001808 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001809 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001810 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1811 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001812 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001813 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001814 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001815
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001816 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1817 if (mTmpTaskForResizePointSearchResult.searchDone) {
1818 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001819 }
1820 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001821 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001822 }
1823
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001824 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001825 // The provided task is the task on this display with focus, so if WindowManagerService's
1826 // focused app is not on this display, focusedTask will be null.
1827 if (focusedTask == null) {
1828 mTouchExcludeRegion.setEmpty();
1829 } else {
1830 mTouchExcludeRegion.set(mBaseDisplayRect);
1831 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1832 mTmpRect2.setEmpty();
1833 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1834 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1835 stack.setTouchExcludeRegion(
1836 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1837 }
1838 // If we removed the focused task above, add it back and only leave its
1839 // outside touch area in the exclusion. TapDectector is not interested in
1840 // any touch inside the focused task itself.
1841 if (!mTmpRect2.isEmpty()) {
1842 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1843 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001844 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001845 final WindowState inputMethod = mService.mInputMethodWindow;
1846 if (inputMethod != null && inputMethod.isVisibleLw()) {
1847 // If the input method is visible and the user is typing, we don't want these touch
1848 // events to be intercepted and used to change focus. This would likely cause a
1849 // disappearance of the input method.
1850 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001851 if (inputMethod.getDisplayId() == mDisplayId) {
1852 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1853 } else {
1854 // IME is on a different display, so we need to update its tap detector.
1855 // TODO(multidisplay): Remove when IME will always appear on same display.
1856 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1857 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001858 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001859 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1860 WindowState win = mTapExcludedWindows.get(i);
1861 win.getTouchableRegion(mTmpRegion);
1862 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1863 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001864 // TODO(multi-display): Support docked stacks on secondary displays.
1865 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001866 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001867 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001868 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1869 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001870 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001871 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001872 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001873 }
1874
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001875 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001876 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001877 super.switchUser();
1878 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001879 }
1880
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001881 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001882 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1883 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001884 }
1885 }
1886
1887 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001888 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001889 }
1890
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001891 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001892 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001893 }
1894
1895 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001896 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001897 }
1898
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001899 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001900 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001901 }
1902
Wale Ogunwale10124582016-09-15 20:25:50 -07001903 @Override
1904 void removeIfPossible() {
1905 if (isAnimating()) {
1906 mDeferredRemoval = true;
1907 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001908 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001909 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001910 }
1911
Wale Ogunwale10124582016-09-15 20:25:50 -07001912 @Override
1913 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001914 mRemovingDisplay = true;
1915 try {
1916 super.removeImmediately();
1917 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1918 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001919 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001920 mService.unregisterPointerEventListener(mTapDetector);
1921 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1922 }
1923 } finally {
1924 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001925 }
Craig Mautner95da1082014-02-24 17:54:35 -08001926 }
1927
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001928 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001929 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001930 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001931 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1932
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001933 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001934 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001935 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001936 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001937 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001938 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001939 }
1940
Andrii Kulian0214ed92017-05-16 13:44:05 -07001941 /** @return 'true' if removal of this display content is deferred due to active animation. */
1942 boolean isRemovalDeferred() {
1943 return mDeferredRemoval;
1944 }
1945
Wale Ogunwale10124582016-09-15 20:25:50 -07001946 boolean animateForIme(float interpolatedValue, float animationTarget,
1947 float dividerAnimationTarget) {
1948 boolean updated = false;
1949
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001950 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1951 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001952 if (stack == null || !stack.isAdjustedForIme()) {
1953 continue;
1954 }
1955
1956 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1957 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1958 updated = true;
1959 } else {
1960 mDividerControllerLocked.mLastAnimationProgress =
1961 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1962 mDividerControllerLocked.mLastDividerProgress =
1963 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1964 updated |= stack.updateAdjustForIme(
1965 mDividerControllerLocked.mLastAnimationProgress,
1966 mDividerControllerLocked.mLastDividerProgress,
1967 false /* force */);
1968 }
1969 if (interpolatedValue >= 1f) {
1970 stack.endImeAdjustAnimation();
1971 }
1972 }
1973
1974 return updated;
1975 }
1976
1977 boolean clearImeAdjustAnimation() {
1978 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001979 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1980 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001981 if (stack != null && stack.isAdjustedForIme()) {
1982 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1983 changed = true;
1984 }
1985 }
1986 return changed;
1987 }
1988
1989 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001990 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1991 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001992 if (stack.isVisible() && stack.isAdjustedForIme()) {
1993 stack.beginImeAdjustAnimation();
1994 }
1995 }
1996 }
1997
1998 void adjustForImeIfNeeded() {
1999 final WindowState imeWin = mService.mInputMethodWindow;
2000 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2001 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002002 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07002003 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2004 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2005 imeTargetStack.getDockSide() : DOCKED_INVALID;
2006 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2007 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2008 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
2009 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
2010 final boolean imeHeightChanged = imeVisible &&
2011 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2012
2013 // The divider could be adjusted for IME position, or be thinner than usual,
2014 // or both. There are three possible cases:
2015 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2016 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2017 // - If IME is not visible, divider is not moved and is normal width.
2018
2019 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002020 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2021 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002022 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07002023 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
2024 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002025 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2026 } else {
2027 stack.resetAdjustedForIme(false);
2028 }
2029 }
2030 mDividerControllerLocked.setAdjustedForIme(
2031 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2032 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002033 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2034 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002035 stack.resetAdjustedForIme(!dockVisible);
2036 }
2037 mDividerControllerLocked.setAdjustedForIme(
2038 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2039 }
Winson Chung655332c2016-10-31 13:14:28 -07002040 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002041 }
2042
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002043 void setInputMethodAnimLayerAdjustment(int adj) {
2044 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2045 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002046 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002047 }
2048
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002049 /**
2050 * If a window that has an animation specifying a colored background and the current wallpaper
2051 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2052 * suddenly disappear.
2053 */
2054 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002055 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2056 w -> w.mIsWallpaper && w.isVisibleNow());
2057
2058 if (visibleWallpaper != null) {
2059 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002060 }
2061 return winAnimator.mAnimLayer;
2062 }
2063
Wale Ogunwale10124582016-09-15 20:25:50 -07002064 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002065 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2066 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002067 stack.prepareFreezingTaskBounds();
2068 }
2069 }
2070
Wale Ogunwale94744212015-09-21 19:01:47 -07002071 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002072 getLogicalDisplayRect(mTmpRect, newRotation);
2073
2074 // Compute a transform matrix to undo the coordinate space transformation,
2075 // and present the window at the same physical position it previously occupied.
2076 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2077 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2078
2079 mTmpRectF.set(bounds);
2080 mTmpMatrix.mapRect(mTmpRectF);
2081 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002082 }
2083
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002084 static int deltaRotation(int oldRotation, int newRotation) {
2085 int delta = newRotation - oldRotation;
2086 if (delta < 0) delta += 4;
2087 return delta;
2088 }
2089
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002090 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002091 Matrix outMatrix) {
2092 // For rotations without Z-ordering we don't need the target rectangle's position.
2093 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2094 displayHeight, outMatrix);
2095 }
2096
2097 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2098 float displayWidth, float displayHeight, Matrix outMatrix) {
2099 switch (rotation) {
2100 case ROTATION_0:
2101 outMatrix.reset();
2102 break;
2103 case ROTATION_270:
2104 outMatrix.setRotate(270, 0, 0);
2105 outMatrix.postTranslate(0, displayHeight);
2106 outMatrix.postTranslate(rectTop, 0);
2107 break;
2108 case ROTATION_180:
2109 outMatrix.reset();
2110 break;
2111 case ROTATION_90:
2112 outMatrix.setRotate(90, 0, 0);
2113 outMatrix.postTranslate(displayWidth, 0);
2114 outMatrix.postTranslate(-rectTop, rectLeft);
2115 break;
2116 }
2117 }
2118
Steven Timotiusaf03df62017-07-18 16:56:43 -07002119 void writeToProto(ProtoOutputStream proto, long fieldId) {
2120 final long token = proto.start(fieldId);
2121 proto.write(ID, mDisplayId);
2122 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2123 final TaskStack stack = mTaskStackContainers.get(stackNdx);
2124 stack.writeToProto(proto, STACKS);
2125 }
2126 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2127 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
2128 for (int i = mAboveAppWindowsContainers.size() - 1; i >= 0; --i) {
2129 final WindowToken windowToken = mAboveAppWindowsContainers.get(i);
2130 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS);
2131 }
2132 for (int i = mBelowAppWindowsContainers.size() - 1; i >= 0; --i) {
2133 final WindowToken windowToken = mBelowAppWindowsContainers.get(i);
2134 windowToken.writeToProto(proto, BELOW_APP_WINDOWS);
2135 }
2136 for (int i = mImeWindowsContainers.size() - 1; i >= 0; --i) {
2137 final WindowToken windowToken = mImeWindowsContainers.get(i);
2138 windowToken.writeToProto(proto, IME_WINDOWS);
2139 }
2140 proto.write(DPI, mBaseDisplayDensity);
2141 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
2142 proto.end(token);
2143 }
2144
Craig Mautnera91f9e22012-09-14 16:22:08 -07002145 public void dump(String prefix, PrintWriter pw) {
2146 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2147 final String subPrefix = " " + prefix;
2148 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2149 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2150 pw.print("dpi");
2151 if (mInitialDisplayWidth != mBaseDisplayWidth
2152 || mInitialDisplayHeight != mBaseDisplayHeight
2153 || mInitialDisplayDensity != mBaseDisplayDensity) {
2154 pw.print(" base=");
2155 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2156 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2157 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002158 if (mDisplayScalingDisabled) {
2159 pw.println(" noscale");
2160 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002161 pw.print(" cur=");
2162 pw.print(mDisplayInfo.logicalWidth);
2163 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2164 pw.print(" app=");
2165 pw.print(mDisplayInfo.appWidth);
2166 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2167 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2168 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2169 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2170 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002171 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2172 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002173
Craig Mautnerdc548482014-02-05 13:35:24 -08002174 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002175 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002176 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2177 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002178 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002179 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002180
Craig Mautnerdc548482014-02-05 13:35:24 -08002181 pw.println();
2182 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002183 pw.println();
2184 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002185 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002186 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002187 pw.print(" Exiting #"); pw.print(i);
2188 pw.print(' '); pw.print(token);
2189 pw.println(':');
2190 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002191 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002192 }
Craig Mautner59c00972012-07-30 12:10:24 -07002193 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002194 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002195 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002196 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002197 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002198 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002199
2200 if (mInputMethodAnimLayerAdjustment != 0) {
2201 pw.println(subPrefix
2202 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2203 }
Craig Mautner59c00972012-07-30 12:10:24 -07002204 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002205
2206 @Override
2207 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002208 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002209 }
2210
2211 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002212 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002213 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002214
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002215 /** Checks if stack with provided id is visible on this display. */
2216 boolean isStackVisible(int stackId) {
2217 final TaskStack stack = getStackById(stackId);
2218 return (stack != null && stack.isVisible());
2219 }
2220
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002221 /**
2222 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2223 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002224 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002225 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002226 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002227 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002228
2229 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002230 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002231 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002232 */
Jorim Jaggife762342016-10-13 14:33:27 +02002233 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002234 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002235 }
2236
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002237 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002238 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002239 final int x = (int) xf;
2240 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002241 final WindowState touchedWin = getWindow(w -> {
2242 final int flags = w.mAttrs.flags;
2243 if (!w.isVisibleLw()) {
2244 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002245 }
2246 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002247 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002248 }
2249
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002250 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002251 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002252 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002253 }
2254
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002255 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002256
2257 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002258 return mTmpRegion.contains(x, y) || touchFlags == 0;
2259 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002260
2261 return touchedWin;
2262 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002263
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002264 boolean canAddToastWindowForUid(int uid) {
2265 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002266 // Also if the app is focused adding more than one toast at
2267 // a time for better backwards compatibility.
2268 final WindowState focusedWindowForUid = getWindow(w ->
2269 w.mOwnerUid == uid && w.isFocused());
2270 if (focusedWindowForUid != null) {
2271 return true;
2272 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002273 final WindowState win = getWindow(w ->
2274 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2275 && !w.mWindowRemovalAllowed);
2276 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002277 }
2278
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002279 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002280 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2281 return;
2282 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002283
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002284 // Used to communicate the old focus to the callback method.
2285 mTmpWindow = oldFocus;
2286
2287 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002288 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002289
2290 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002291 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002292
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002293 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002294
2295 if (mTmpWindow == null) {
2296 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2297 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002298 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002299 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002300 }
2301
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002302 /** Updates the layer assignment of windows on this display. */
2303 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002304 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002305 if (setLayoutNeeded) {
2306 setLayoutNeeded();
2307 }
2308 }
2309
Wale Ogunwale1666e312016-12-16 11:27:18 -08002310 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2311 // moving containers or resizing them. Need to investigate the best way to have it automatically
2312 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002313 void layoutAndAssignWindowLayersIfNeeded() {
2314 mService.mWindowsChanged = true;
2315 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002316
2317 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2318 false /*updateInputWindows*/)) {
2319 assignWindowLayers(false /* setLayoutNeeded */);
2320 }
2321
2322 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2323 mService.mWindowPlacerLocked.performSurfacePlacement();
2324 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2325 }
2326
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002327 /** Returns true if a leaked surface was destroyed */
2328 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002329 // Used to indicate that a surface was leaked.
2330 mTmpWindow = null;
2331 forAllWindows(w -> {
2332 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002333 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002334 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002335 }
2336 if (!mService.mSessions.contains(wsa.mSession)) {
2337 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002338 + w + " surface=" + wsa.mSurfaceController
2339 + " token=" + w.mToken
2340 + " pid=" + w.mSession.mPid
2341 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002342 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002343 mService.mForceRemoves.add(w);
2344 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002345 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002346 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002347 + w + " surface=" + wsa.mSurfaceController
2348 + " token=" + w.mAppToken
2349 + " saved=" + w.hasSavedSurface());
2350 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002351 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002352 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002353 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002354 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002355
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002356 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002357 }
2358
2359 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002360 * Determine and return the window that should be the IME target.
2361 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2362 * @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 +00002363 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002364 WindowState computeImeTarget(boolean updateImeTarget) {
2365 if (mService.mInputMethodWindow == null) {
2366 // There isn't an IME so there shouldn't be a target...That was easy!
2367 if (updateImeTarget) {
2368 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2369 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2370 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2371 }
2372 return null;
2373 }
2374
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002375 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2376 // same display. Or even when the current IME/target are not on the same screen as the next
2377 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002378 mUpdateImeTarget = updateImeTarget;
2379 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002380
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002381
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2383 // to be on top of it, but it is not -really- where input will go. So look down below
2384 // for a real window to target...
2385 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2386 final AppWindowToken token = target.mAppToken;
2387 if (token != null) {
2388 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2389 if (betterTarget != null) {
2390 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002391 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002392 }
2393 }
2394
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002395 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2396 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002397
2398 // Now, a special case -- if the last target's window is in the process of exiting, and is
2399 // above the new target, keep on the last target to avoid flicker. Consider for example a
2400 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2401 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2402 // scrim.
2403 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002404 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2405 && (target == null
2406 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002407 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002408 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002409 }
2410
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002411 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2412 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002413
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002414 if (target == null) {
2415 if (updateImeTarget) {
2416 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2417 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2418 + Debug.getCallers(4) : ""));
2419 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2420 }
2421
2422 return null;
2423 }
2424
2425 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002426 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2427 if (token != null) {
2428
2429 // Now some fun for dealing with window animations that modify the Z order. We need
2430 // to look at all windows below the current target that are in this app, finding the
2431 // highest visible one in layering.
2432 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002433 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002434 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002435 }
2436
2437 if (highestTarget != null) {
2438 final AppTransition appTransition = mService.mAppTransition;
2439 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2440 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2441 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002442 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002443
2444 if (appTransition.isTransitionSet()) {
2445 // If we are currently setting up for an animation, hold everything until we
2446 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002447 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2448 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002449 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002450 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002451 // If the window we are currently targeting is involved with an animation,
2452 // and it is on top of the next target we will be over, then hold off on
2453 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002454 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2455 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002456 }
2457 }
2458 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459
2460 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2461 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2462 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002463 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002464 }
2465
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002466 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002467 }
2468
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002469 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2470 if (target == mService.mInputMethodTarget
2471 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2472 && mInputMethodAnimLayerAdjustment == layerAdj) {
2473 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002474 }
2475
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002476 mService.mInputMethodTarget = target;
2477 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2478 setInputMethodAnimLayerAdjustment(layerAdj);
2479 assignWindowLayers(false /* setLayoutNeeded */);
2480 }
2481
2482 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2483 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2484 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2485 }
2486
2487 // Used to indicate we have reached the first window in the range we are interested in.
2488 mTmpWindow = null;
2489
2490 // TODO: Figure-out a more efficient way to do this.
2491 final WindowState candidate = getWindow(w -> {
2492 if (w == top) {
2493 // Reached the first window in the range we are interested in.
2494 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002495 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002496 if (mTmpWindow == null) {
2497 return false;
2498 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002499
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002500 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2501 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002502 }
2503 // If we reached the bottom of the range of windows we are considering,
2504 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002505 if (w == bottom) {
2506 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002507 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002508 return false;
2509 });
2510
2511 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002512 }
2513
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002514 void setLayoutNeeded() {
2515 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2516 mLayoutNeeded = true;
2517 }
2518
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002519 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002520 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2521 mLayoutNeeded = false;
2522 }
2523
2524 boolean isLayoutNeeded() {
2525 return mLayoutNeeded;
2526 }
2527
Wale Ogunwale02319a62016-09-26 15:21:22 -07002528 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2529 if (mTokenMap.isEmpty()) {
2530 return;
2531 }
2532 pw.println(" Display #" + mDisplayId);
2533 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2534 while (it.hasNext()) {
2535 final WindowToken token = it.next();
2536 pw.print(" ");
2537 pw.print(token);
2538 if (dumpAll) {
2539 pw.println(':');
2540 token.dump(pw, " ");
2541 } else {
2542 pw.println();
2543 }
2544 }
2545 }
2546
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002547 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002548 final int[] index = new int[1];
2549 forAllWindows(w -> {
2550 final WindowStateAnimator wAnim = w.mWinAnimator;
2551 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2552 index[0] = index[0] + 1;
2553 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002554 }
2555
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002556 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002557 forAllWindows(w -> {
2558 w.mWinAnimator.enableSurfaceTrace(fd);
2559 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002560 }
2561
2562 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002563 forAllWindows(w -> {
2564 w.mWinAnimator.disableSurfaceTrace();
2565 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002566 }
2567
Jorim Jaggife762342016-10-13 14:33:27 +02002568 /**
2569 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2570 */
2571 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2572 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002573 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002574 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2575 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002576 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002577 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2578 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002579 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002580 }
2581
Wale Ogunwale494009b82016-10-21 09:01:38 -07002582 boolean checkWaitingForWindows() {
2583
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002584 mHaveBootMsg = false;
2585 mHaveApp = false;
2586 mHaveWallpaper = false;
2587 mHaveKeyguard = true;
2588
2589 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002590 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2591 return true;
2592 }
2593 if (w.isDrawnLw()) {
2594 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002595 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002596 } else if (w.mAttrs.type == TYPE_APPLICATION
2597 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002598 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002599 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002600 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002601 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002602 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002603 }
2604 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002605 return false;
2606 });
2607
2608 if (visibleWindow != null) {
2609 // We have a visible window.
2610 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002611 }
2612
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002613 // if the wallpaper service is disabled on the device, we're never going to have
2614 // wallpaper, don't bother waiting for it
2615 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2616 com.android.internal.R.bool.config_enableWallpaperService)
2617 && !mService.mOnlyCore;
2618
Wale Ogunwale494009b82016-10-21 09:01:38 -07002619 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2620 "******** booted=" + mService.mSystemBooted
2621 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002622 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2623 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2624 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002625
2626 // If we are turning on the screen to show the boot message, don't do it until the boot
2627 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002628 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002629 return true;
2630 }
2631
2632 // If we are turning on the screen after the boot is completed normally, don't do so until
2633 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002634 if (mService.mSystemBooted
2635 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002636 return true;
2637 }
2638
2639 return false;
2640 }
2641
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002642 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002643 mTmpWindowAnimator = animator;
2644 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002645 }
2646
2647 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002648 resetAnimationBackgroundAnimator();
2649
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650 // Used to indicate a detached wallpaper.
2651 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002652 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002653
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002654 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002655
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002656 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002657 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002658 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2659 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002660 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2661 }
2662 }
2663
2664 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002665 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002666 }
2667
Wale Ogunwale494009b82016-10-21 09:01:38 -07002668 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002669 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002670 if (imFocus == null) {
2671 return false;
2672 }
2673
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002674 if (DEBUG_INPUT_METHOD) {
2675 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2676 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2677 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002678 }
2679
Wale Ogunwale494009b82016-10-21 09:01:38 -07002680 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2681
2682 if (DEBUG_INPUT_METHOD) {
2683 Slog.i(TAG_WM, "IM target client: " + imeClient);
2684 if (imeClient != null) {
2685 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2686 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2687 }
2688 }
2689
2690 return imeClient != null && imeClient.asBinder() == client.asBinder();
2691 }
2692
2693 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002694 final WindowState win = getWindow(
2695 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2696 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002697 }
2698
2699 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002700 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002701 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002702 final int curValue = w.mSystemUiVisibility;
2703 final int diff = (curValue ^ visibility) & globalDiff;
2704 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002705 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002706 w.mSeq++;
2707 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002708 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002709 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2710 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002711 visibility, newValue, diff);
2712 }
2713 } catch (RemoteException e) {
2714 // so sorry
2715 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002716 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002717 }
2718
2719 void onWindowFreezeTimeout() {
2720 Slog.w(TAG_WM, "Window freeze timeout expired.");
2721 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002722
2723 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002724 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002725 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002726 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002727 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002728 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2729 - mService.mDisplayFreezeTime);
2730 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002731 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002732 mService.mWindowPlacerLocked.performSurfacePlacement();
2733 }
2734
2735 void waitForAllWindowsDrawn() {
2736 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002737 forAllWindows(w -> {
2738 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2739 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2740 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002741 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002742 w.mLastContentInsets.set(-1, -1, -1, -1);
2743 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002744 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002746 }
2747
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002748 // TODO: Super crazy long method that should be broken down...
2749 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2750
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002751 final int dw = mDisplayInfo.logicalWidth;
2752 final int dh = mDisplayInfo.logicalHeight;
2753 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2754
2755 mTmpUpdateAllDrawn.clear();
2756
2757 int repeats = 0;
2758 do {
2759 repeats++;
2760 if (repeats > 6) {
2761 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2762 clearLayoutNeeded();
2763 break;
2764 }
2765
2766 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2767 pendingLayoutChanges);
2768
Andrii Kulian839def92016-11-02 10:58:58 -07002769 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2770 // the wallpaper window jumping across displays.
2771 // Remove check for default display when there will be support for multiple wallpaper
2772 // targets (on different displays).
2773 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002774 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002775 }
2776
2777 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2778 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2779 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2780 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002781 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002782 }
2783 }
2784
2785 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2786 setLayoutNeeded();
2787 }
2788
2789 // FIRST LOOP: Perform a layout, if needed.
2790 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2791 performLayout(repeats == 1, false /* updateInputWindows */);
2792 } else {
2793 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2794 }
2795
2796 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2797 pendingLayoutChanges = 0;
2798
2799 if (isDefaultDisplay) {
2800 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002801 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002802 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2803 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2804 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2805 }
2806 } while (pendingLayoutChanges != 0);
2807
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002808 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002809 resetDimming();
2810
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002811 mTmpRecoveringMemory = recoveringMemory;
2812 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002813
2814 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002815 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2816 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2817 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002818 true /* inTraversal, must call performTraversalInTrans... below */);
2819
2820 stopDimmingIfNeeded();
2821
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002822 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2823 if (wallpaperVisible != mLastWallpaperVisible) {
2824 mLastWallpaperVisible = wallpaperVisible;
2825 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2826 }
2827
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002828 while (!mTmpUpdateAllDrawn.isEmpty()) {
2829 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2830 // See if any windows have been drawn, so they (and others associated with them)
2831 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002832 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002833 }
2834
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002835 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002836 }
2837
2838 void performLayout(boolean initial, boolean updateInputWindows) {
2839 if (!isLayoutNeeded()) {
2840 return;
2841 }
2842 clearLayoutNeeded();
2843
2844 final int dw = mDisplayInfo.logicalWidth;
2845 final int dh = mDisplayInfo.logicalHeight;
2846
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002847 if (DEBUG_LAYOUT) {
2848 Slog.v(TAG, "-------------------------------------");
2849 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2850 }
2851
Andrii Kulian8ee72852017-03-10 10:36:45 -08002852 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002853 getConfiguration().uiMode);
2854 if (isDefaultDisplay) {
2855 // Not needed on non-default displays.
2856 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2857 mService.mScreenRect.set(0, 0, dw, dh);
2858 }
2859
2860 mService.mPolicy.getContentRectLw(mContentRect);
2861
2862 int seq = mService.mLayoutSeq + 1;
2863 if (seq < 0) seq = 0;
2864 mService.mLayoutSeq = seq;
2865
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002866 // Used to indicate that we have processed the dream window and all additional windows are
2867 // behind it.
2868 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002869 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002870
2871 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002872 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002873
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002874 // Used to indicate that we have processed the dream window and all additional attached
2875 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002876 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002877 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002878
2879 // Now perform layout of attached windows, which usually depend on the position of the
2880 // window they are attached to. XXX does not deal with windows that are attached to windows
2881 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002882 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002883
2884 // Window frames may have changed. Tell the input dispatcher about it.
2885 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2886 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2887 if (updateInputWindows) {
2888 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2889 }
2890
2891 mService.mPolicy.finishLayoutLw();
2892 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2893 }
2894
2895 /**
2896 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2897 * In portrait mode, it grabs the full screenshot.
2898 *
2899 * @param width the width of the target bitmap
2900 * @param height the height of the target bitmap
2901 * @param includeFullDisplay true if the screen should not be cropped before capture
2902 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2903 * @param config of the output bitmap
2904 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002905 * @param includeDecor whether to include window decors, like the status or navigation bar
2906 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002907 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002908 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002909 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002910 boolean wallpaperOnly, boolean includeDecor) {
2911 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2912 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002913 if (bitmap == null) {
2914 return null;
2915 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002916
2917 if (DEBUG_SCREENSHOT) {
2918 // TEST IF IT's ALL BLACK
2919 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2920 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2921 bitmap.getHeight());
2922 boolean allBlack = true;
2923 final int firstColor = buffer[0];
2924 for (int i = 0; i < buffer.length; i++) {
2925 if (buffer[i] != firstColor) {
2926 allBlack = false;
2927 break;
2928 }
2929 }
2930 if (allBlack) {
2931 final WindowState appWin = mScreenshotApplicationState.appWin;
2932 final int maxLayer = mScreenshotApplicationState.maxLayer;
2933 final int minLayer = mScreenshotApplicationState.minLayer;
2934 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2935 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2936 (appWin != null ?
2937 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2938 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2939 }
2940 }
2941
2942 // Create a copy of the screenshot that is immutable and backed in ashmem.
2943 // This greatly reduces the overhead of passing the bitmap between processes.
2944 Bitmap ret = bitmap.createAshmemBitmap(config);
2945 bitmap.recycle();
2946 return ret;
2947 }
2948
2949 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2950 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2951 boolean includeDecor) {
2952 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2953 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2954 }
2955
2956 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2957 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2958 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002959 int dw = mDisplayInfo.logicalWidth;
2960 int dh = mDisplayInfo.logicalHeight;
2961 if (dw == 0 || dh == 0) {
2962 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2963 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2964 return null;
2965 }
2966
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002967 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002968
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002969 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002970 final Rect frame = new Rect();
2971 final Rect stackBounds = new Rect();
2972
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002973 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002974 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002975 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002976 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002977 if (!mService.mPolicy.isScreenOn()) {
2978 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
2979 + " was off.");
2980 return null;
2981 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002982 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002983 mScreenshotApplicationState.appWin = null;
2984 forAllWindows(w -> {
2985 if (!w.mHasSurface) {
2986 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002987 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002988 if (w.mLayer >= aboveAppLayer) {
2989 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002990 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002991 if (wallpaperOnly && !w.mIsWallpaper) {
2992 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002993 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002994 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02002995 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002996 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002997 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2998 // then the target window state is this one.
2999 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003000 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003001 }
3002
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003003 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003004 // We have not ran across the target window yet, so it is probably behind
3005 // the wallpaper. This can happen when the keyguard is up and all windows
3006 // are moved behind the wallpaper. We don't want to include the wallpaper
3007 // layer in the screenshot as it will cover-up the layer of the target
3008 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003009 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003010 }
3011 // Fall through. The target window is in front of the wallpaper. For this
3012 // case we want to include the wallpaper layer in the screenshot because
3013 // the target window might have some transparent areas.
3014 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003015 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003016 // This app window is of no interest if it is not associated with the
3017 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003018 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003019 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003020 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003021 }
3022
3023 // Include this window.
3024
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003025 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003026 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003027 if (mScreenshotApplicationState.maxLayer < layer) {
3028 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003029 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003030 if (mScreenshotApplicationState.minLayer > layer) {
3031 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003032 }
3033
3034 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003035 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3036 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003037 final Task task = w.getTask();
3038 if (task != null) {
3039 task.getBounds(frame);
3040 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003041
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003042 // No task bounds? Too bad! Ain't no screenshot then.
3043 return true;
3044 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003045 } else {
3046 final Rect wf = w.mFrame;
3047 final Rect cr = w.mContentInsets;
3048 int left = wf.left + cr.left;
3049 int top = wf.top + cr.top;
3050 int right = wf.right - cr.right;
3051 int bottom = wf.bottom - cr.bottom;
3052 frame.union(left, top, right, bottom);
3053 w.getVisibleBounds(stackBounds);
3054 if (!Rect.intersects(frame, stackBounds)) {
3055 // Set frame empty if there's no intersection.
3056 frame.setEmpty();
3057 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003058 }
3059 }
3060
3061 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003062 (w.mAppToken != null && w.mAppToken.token == appToken)
3063 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01003064 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003065 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003066 }
3067
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003068 if (w.isObscuringDisplay()){
3069 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003070 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003071 return false;
3072 }, true /* traverseTopToBottom */);
3073
3074 final WindowState appWin = mScreenshotApplicationState.appWin;
3075 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3076 final int maxLayer = mScreenshotApplicationState.maxLayer;
3077 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003078
3079 if (appToken != null && appWin == null) {
3080 // Can't find a window to snapshot.
3081 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3082 "Screenshot: Couldn't find a surface matching " + appToken);
3083 return null;
3084 }
3085
3086 if (!screenshotReady) {
3087 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3088 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3089 appWin.mWinAnimator.mDrawState)));
3090 return null;
3091 }
3092
3093 // Screenshot is ready to be taken. Everything from here below will continue
3094 // through the bottom of the loop and return a value. We only stay in the loop
3095 // because we don't want to release the mWindowMap lock until the screenshot is
3096 // taken.
3097
3098 if (maxLayer == 0) {
3099 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3100 + ": returning null maxLayer=" + maxLayer);
3101 return null;
3102 }
3103
Jorim Jaggi02886a82016-12-06 09:10:06 -08003104 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003105 // Constrain frame to the screen size.
3106 if (!frame.intersect(0, 0, dw, dh)) {
3107 frame.setEmpty();
3108 }
3109 } else {
3110 // Caller just wants entire display.
3111 frame.set(0, 0, dw, dh);
3112 }
3113 if (frame.isEmpty()) {
3114 return null;
3115 }
3116
3117 if (width < 0) {
3118 width = (int) (frame.width() * frameScale);
3119 }
3120 if (height < 0) {
3121 height = (int) (frame.height() * frameScale);
3122 }
3123
3124 // Tell surface flinger what part of the image to crop. Take the top
3125 // right part of the application, and crop the larger dimension to fit.
3126 Rect crop = new Rect(frame);
3127 if (width / (float) frame.width() < height / (float) frame.height()) {
3128 int cropWidth = (int)((float)width / (float)height * frame.height());
3129 crop.right = crop.left + cropWidth;
3130 } else {
3131 int cropHeight = (int)((float)height / (float)width * frame.width());
3132 crop.bottom = crop.top + cropHeight;
3133 }
3134
3135 // The screenshot API does not apply the current screen rotation.
3136 int rot = mDisplay.getRotation();
3137
3138 if (rot == ROTATION_90 || rot == ROTATION_270) {
3139 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3140 }
3141
3142 // Surfaceflinger is not aware of orientation, so convert our logical
3143 // crop to surfaceflinger's portrait orientation.
3144 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3145
3146 if (DEBUG_SCREENSHOT) {
3147 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3148 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003149 forAllWindows(w -> {
3150 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3151 Slog.i(TAG_WM, w + ": " + w.mLayer
3152 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003153 + " surfaceLayer=" + ((controller == null)
3154 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003155 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003156 }
3157
3158 final ScreenRotationAnimation screenRotationAnimation =
3159 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3160 final boolean inRotation = screenRotationAnimation != null &&
3161 screenRotationAnimation.isAnimating();
3162 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3163 "Taking screenshot while rotating");
3164
3165 // We force pending transactions to flush before taking
3166 // the screenshot by pushing an empty synchronous transaction.
3167 SurfaceControl.openTransaction();
3168 SurfaceControl.closeTransactionSync();
3169
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003170 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003171 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003172 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003173 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3174 + ") to layer " + maxLayer);
3175 return null;
3176 }
3177 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003178 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003179 }
3180
3181 // TODO: Can this use createRotationMatrix()?
3182 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3183 if (rot == Surface.ROTATION_90) {
3184 final int tmp = crop.top;
3185 crop.top = dw - crop.right;
3186 crop.right = crop.bottom;
3187 crop.bottom = dw - crop.left;
3188 crop.left = tmp;
3189 } else if (rot == Surface.ROTATION_180) {
3190 int tmp = crop.top;
3191 crop.top = dh - crop.bottom;
3192 crop.bottom = dh - tmp;
3193 tmp = crop.right;
3194 crop.right = dw - crop.left;
3195 crop.left = dw - tmp;
3196 } else if (rot == Surface.ROTATION_270) {
3197 final int tmp = crop.top;
3198 crop.top = crop.left;
3199 crop.left = dh - crop.bottom;
3200 crop.bottom = crop.right;
3201 crop.right = dh - tmp;
3202 }
3203 }
3204
3205 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003206 // Used to indicate the layout is needed.
3207 mTmpWindow = null;
3208
3209 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003210 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003211 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003212 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003213 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003214 w.setDisplayLayoutNeeded();
3215 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003216 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003217
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003218 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003219 mService.mWindowPlacerLocked.performSurfacePlacement();
3220 }
3221 }
3222
Wale Ogunwale1666e312016-12-16 11:27:18 -08003223 void setExitingTokensHasVisible(boolean hasVisible) {
3224 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3225 mExitingTokens.get(i).hasVisible = hasVisible;
3226 }
3227
3228 // Initialize state of exiting applications.
3229 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3230 }
3231
3232 void removeExistingTokensIfPossible() {
3233 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3234 final WindowToken token = mExitingTokens.get(i);
3235 if (!token.hasVisible) {
3236 mExitingTokens.remove(i);
3237 }
3238 }
3239
3240 // Time to remove any exiting applications?
3241 mTaskStackContainers.removeExistingAppTokensIfPossible();
3242 }
3243
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003244 @Override
3245 void onDescendantOverrideConfigurationChanged() {
3246 setLayoutNeeded();
3247 mService.requestTraversal();
3248 }
3249
David Stevens9440dc82017-03-16 19:00:20 -07003250 boolean okToDisplay() {
3251 if (mDisplayId == DEFAULT_DISPLAY) {
3252 return !mService.mDisplayFrozen
3253 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3254 }
3255 return mDisplayInfo.state == Display.STATE_ON;
3256 }
3257
3258 boolean okToAnimate() {
3259 return okToDisplay() &&
3260 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3261 }
3262
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003263 static final class TaskForResizePointSearchResult {
3264 boolean searchDone;
3265 Task taskForResize;
3266
3267 void reset() {
3268 searchDone = false;
3269 taskForResize = null;
3270 }
3271 }
Robert Carr3b716242016-08-16 16:02:21 -07003272
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003273 private static final class ApplySurfaceChangesTransactionState {
3274 boolean displayHasContent;
3275 boolean obscured;
3276 boolean syswin;
3277 boolean focusDisplayed;
3278 float preferredRefreshRate;
3279 int preferredModeId;
3280
3281 void reset() {
3282 displayHasContent = false;
3283 obscured = false;
3284 syswin = false;
3285 focusDisplayed = false;
3286 preferredRefreshRate = 0;
3287 preferredModeId = 0;
3288 }
3289 }
3290
3291 private static final class ScreenshotApplicationState {
3292 WindowState appWin;
3293 int maxLayer;
3294 int minLayer;
3295 boolean screenshotReady;
3296
3297 void reset(boolean screenshotReady) {
3298 appWin = null;
3299 maxLayer = 0;
3300 minLayer = 0;
3301 this.screenshotReady = screenshotReady;
3302 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3303 }
3304 }
3305
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003306 /**
3307 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3308 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3309 * homogeneous children type which is currently required by sub-classes of
3310 * {@link WindowContainer} class.
3311 */
3312 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3313
3314 int size() {
3315 return mChildren.size();
3316 }
3317
3318 E get(int index) {
3319 return mChildren.get(index);
3320 }
3321
3322 @Override
3323 boolean fillsParent() {
3324 return true;
3325 }
3326
3327 @Override
3328 boolean isVisible() {
3329 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003330 }
3331 }
3332
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003333 /**
3334 * Window container class that contains all containers on this display relating to Apps.
3335 * I.e Activities.
3336 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003337 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003338
Andrii Kulian839def92016-11-02 10:58:58 -07003339 /**
3340 * Adds the stack to this container.
3341 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3342 */
3343 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003344 if (stack.mStackId == HOME_STACK_ID) {
3345 if (mHomeStack != null) {
3346 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3347 }
3348 mHomeStack = stack;
3349 }
3350 addChild(stack, onTop);
3351 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003352 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003353
Andrii Kulian839def92016-11-02 10:58:58 -07003354 /** Removes the stack from its container and prepare for changing the parent. */
3355 void removeStackFromDisplay(TaskStack stack) {
3356 removeChild(stack);
3357 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003358 }
3359
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003360 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003361 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3362 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003363 addChild(stack, addIndex);
3364 setLayoutNeeded();
3365 }
3366
Bryce Lee00d586d2017-07-28 20:48:43 -07003367
3368 @Override
3369 boolean isOnTop() {
3370 // Considered always on top
3371 return true;
3372 }
3373
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003374 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003375 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003376 if (child.getWindowConfiguration().isAlwaysOnTop()
3377 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003378 // This stack is always-on-top, override the default behavior.
3379 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3380
3381 // Moving to its current position, as we must call super but we don't want to
3382 // perform any meaningful action.
3383 final int currentPosition = mChildren.indexOf(child);
3384 super.positionChildAt(currentPosition, child, false /* includingParents */);
3385 return;
3386 }
3387
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003388 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3389 super.positionChildAt(targetPosition, child, includingParents);
3390
3391 setLayoutNeeded();
3392 }
3393
3394 /**
3395 * When stack is added or repositioned, find a proper position for it.
3396 * This will make sure that pinned stack always stays on top.
3397 * @param requestedPosition Position requested by caller.
3398 * @param stack Stack to be added or positioned.
3399 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3400 * @return The proper position for the stack.
3401 */
3402 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3403 final int topChildPosition = mChildren.size() - 1;
3404 boolean toTop = requestedPosition == POSITION_TOP;
3405 toTop |= adding ? requestedPosition >= topChildPosition + 1
3406 : requestedPosition >= topChildPosition;
3407 int targetPosition = requestedPosition;
3408
Bryce Lee48f4b572017-04-10 10:54:15 -07003409 if (toTop && stack.mStackId != PINNED_STACK_ID
3410 && getStackById(PINNED_STACK_ID) != null) {
3411 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003412 TaskStack topStack = mChildren.get(topChildPosition);
3413 if (topStack.mStackId != PINNED_STACK_ID) {
3414 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3415 }
3416
3417 // So, stack is moved just below the pinned stack.
3418 // When we're adding a new stack the target is the current pinned stack position.
3419 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003420 // stack, because WindowContainer#positionAt() first removes element and then adds
3421 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003422 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3423 }
3424
3425 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003426 }
3427
3428 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003429 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3430 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003431 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003432 if (super.forAllWindows(callback, traverseTopToBottom)) {
3433 return true;
3434 }
3435 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3436 return true;
3437 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003438 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003439 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3440 return true;
3441 }
3442 if (super.forAllWindows(callback, traverseTopToBottom)) {
3443 return true;
3444 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003445 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003446 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003447 }
3448
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003449 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003450 boolean traverseTopToBottom) {
3451 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3452 // app tokens.
3453 // TODO: Investigate if we need to continue to do this or if we can just process them
3454 // in-order.
3455 if (traverseTopToBottom) {
3456 for (int i = mChildren.size() - 1; i >= 0; --i) {
3457 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3458 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003459 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3460 traverseTopToBottom)) {
3461 return true;
3462 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003463 }
3464 }
3465 } else {
3466 final int count = mChildren.size();
3467 for (int i = 0; i < count; ++i) {
3468 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3469 final int appTokensCount = appTokens.size();
3470 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003471 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3472 traverseTopToBottom)) {
3473 return true;
3474 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003475 }
3476 }
3477 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003478 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003479 }
3480
Wale Ogunwale1666e312016-12-16 11:27:18 -08003481 void setExitingTokensHasVisible(boolean hasVisible) {
3482 for (int i = mChildren.size() - 1; i >= 0; --i) {
3483 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3484 for (int j = appTokens.size() - 1; j >= 0; --j) {
3485 appTokens.get(j).hasVisible = hasVisible;
3486 }
3487 }
3488 }
3489
3490 void removeExistingAppTokensIfPossible() {
3491 for (int i = mChildren.size() - 1; i >= 0; --i) {
3492 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3493 for (int j = appTokens.size() - 1; j >= 0; --j) {
3494 final AppWindowToken token = appTokens.get(j);
3495 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3496 && (!token.mIsExiting || token.isEmpty())) {
3497 // Make sure there is no animation running on this token, so any windows
3498 // associated with it will be removed as soon as their animations are
3499 // complete.
3500 token.mAppAnimator.clearAnimation();
3501 token.mAppAnimator.animating = false;
3502 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3503 "performLayout: App token exiting now removed" + token);
3504 token.removeIfPossible();
3505 }
3506 }
3507 }
3508 }
3509
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003510 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003511 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003512 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003513 // Apps and their containers are not allowed to specify an orientation while the
3514 // docked or freeform stack is visible...except for the home stack/task if the
3515 // docked stack is minimized and it actually set something.
3516 if (mHomeStack != null && mHomeStack.isVisible()
3517 && mDividerControllerLocked.isMinimizedDock()) {
3518 final int orientation = mHomeStack.getOrientation();
3519 if (orientation != SCREEN_ORIENTATION_UNSET) {
3520 return orientation;
3521 }
3522 }
3523 return SCREEN_ORIENTATION_UNSPECIFIED;
3524 }
3525
3526 final int orientation = super.getOrientation();
3527 if (orientation != SCREEN_ORIENTATION_UNSET
3528 && orientation != SCREEN_ORIENTATION_BEHIND) {
3529 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3530 "App is requesting an orientation, return " + orientation);
3531 return orientation;
3532 }
3533
3534 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003535 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003536 // The next app has not been requested to be visible, so we keep the current orientation
3537 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003538 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003539 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003540 }
3541
3542 /**
3543 * Window container class that contains all containers on this display that are not related to
3544 * Apps. E.g. status bar.
3545 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003546 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3547 /**
3548 * Compares two child window tokens returns -1 if the first is lesser than the second in
3549 * terms of z-order and 1 otherwise.
3550 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003551 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003552 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003553 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3554 token1.mOwnerCanManageAppTokens)
3555 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3556 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003557
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003558 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3559 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3560 return false;
3561 }
3562 final int req = w.mAttrs.screenOrientation;
3563 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3564 || req == SCREEN_ORIENTATION_UNSET) {
3565 return false;
3566 }
3567 return true;
3568 };
3569
Wale Ogunwale3a931692016-11-02 16:49:48 -07003570 private final String mName;
3571 NonAppWindowContainers(String name) {
3572 mName = name;
3573 }
3574
3575 void addChild(WindowToken token) {
3576 addChild(token, mWindowComparator);
3577 }
3578
3579 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003580 int getOrientation() {
3581 final WindowManagerPolicy policy = mService.mPolicy;
3582 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003583 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003584
3585 if (win != null) {
3586 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003587 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003588 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003589 if (mService.mKeyguardGoingAway) {
3590 // Keyguard can't affect the orientation if it is going away...
3591 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3592 return SCREEN_ORIENTATION_UNSET;
3593 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003594 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003595 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003596 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003597 }
3598
Andrii Kulian8ee72852017-03-10 10:36:45 -08003599 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003600
3601 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003602 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003603 }
3604
3605 return SCREEN_ORIENTATION_UNSET;
3606 }
3607
3608 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003609 String getName() {
3610 return mName;
3611 }
Robert Carr3b716242016-08-16 16:02:21 -07003612 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003613
3614 /**
3615 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3616 */
3617 @FunctionalInterface
3618 private interface Screenshoter<E> {
3619 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3620 boolean useIdentityTransform, int rotation);
3621 }
Craig Mautner59c00972012-07-30 12:10:24 -07003622}