blob: d74e482531323341d58f2b7924104db5367cf1b0 [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;
Jorim Jaggi75520d52017-08-24 17:23:19 +020063import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080064import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
68import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070080import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070081import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070083import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080084import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070085import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070086import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
87import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070088import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070089import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070090import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
91import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070092import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
93import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
94import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070096import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070097import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070098import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070099import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700100import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700101import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700103import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700104import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
105import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
106import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
107import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
108import static com.android.server.wm.proto.DisplayProto.DPI;
109import static com.android.server.wm.proto.DisplayProto.ID;
110import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
111import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700112import static com.android.server.wm.proto.DisplayProto.ROTATION;
113import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700114import static com.android.server.wm.proto.DisplayProto.STACKS;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700115
Andrii Kulian3a507b52016-09-19 18:14:12 -0700116import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700117import android.app.ActivityManager.StackId;
Wale Ogunwale687b4272017-07-27 02:56:23 -0700118import android.app.WindowConfiguration;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700119import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700120import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700121import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100122import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700123import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800124import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700125import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700126import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100127import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700128import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700129import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700130import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700131import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700132import android.os.RemoteException;
133import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700134import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800135import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700136import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700137import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700138import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700139import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700140import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700141import android.view.Surface;
142import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700143import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700144
Bryce Lee48f4b572017-04-10 10:54:15 -0700145import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800146import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700147import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700148
Robert Carr3b716242016-08-16 16:02:21 -0700149import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700150import java.io.PrintWriter;
151import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700152import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700153import java.util.HashMap;
154import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700155import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700156import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800157import java.util.function.Consumer;
158import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700159
Craig Mautner59c00972012-07-30 12:10:24 -0700160/**
161 * Utility class for keeping track of the WindowStates and other pertinent contents of a
162 * particular Display.
163 *
164 * IMPORTANT: No method from this class should ever be used without holding
165 * WindowManagerService.mWindowMap.
166 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700167class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700168 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700169
170 /** Unique identifier of this stack. */
171 private final int mDisplayId;
172
Wale Ogunwale3a931692016-11-02 16:49:48 -0700173 /** The containers below are the only child containers the display can have. */
174 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700175 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700176 // Contains all non-app window containers that should be displayed above the app containers
177 // (e.g. Status bar)
178 private final NonAppWindowContainers mAboveAppWindowsContainers =
179 new NonAppWindowContainers("mAboveAppWindowsContainers");
180 // Contains all non-app window containers that should be displayed below the app containers
181 // (e.g. Wallpaper).
182 private final NonAppWindowContainers mBelowAppWindowsContainers =
183 new NonAppWindowContainers("mBelowAppWindowsContainers");
184 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
185 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
186 // window containers together and move them in-sync if/when needed.
187 private final NonAppWindowContainers mImeWindowsContainers =
188 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700189
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000190 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800191 private WindowState mTmpWindow2;
192 private WindowAnimator mTmpWindowAnimator;
193 private boolean mTmpRecoveringMemory;
194 private boolean mUpdateImeTarget;
195 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700196 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700197
Wale Ogunwale02319a62016-09-26 15:21:22 -0700198 // Mapping from a token IBinder to a WindowToken object on this display.
199 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
200
Andrii Kuliancd097992017-03-23 18:31:59 -0700201 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700202 int mInitialDisplayWidth = 0;
203 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700204 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700205
206 /**
207 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
208 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
209 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
210 */
Craig Mautner59c00972012-07-30 12:10:24 -0700211 int mBaseDisplayWidth = 0;
212 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700213 /**
214 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
215 * but can be set from Settings or via shell command "adb shell wm density".
216 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
217 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700218 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700219 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700220 private final DisplayInfo mDisplayInfo = new DisplayInfo();
221 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700222 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700223 /**
224 * For default display it contains real metrics, empty for others.
225 * @see WindowManagerService#createWatermarkInTransaction()
226 */
227 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
228 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
229 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700230
Andrii Kulian06d07d62017-03-14 11:11:47 -0700231 /**
232 * Compat metrics computed based on {@link #mDisplayMetrics}.
233 * @see #updateDisplayAndOrientation(int)
234 */
235 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
236
237 /** The desired scaling factor for compatible apps. */
238 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700239
Andrii Kulian8ee72852017-03-10 10:36:45 -0800240 /**
241 * Current rotation of the display.
242 * Constants as per {@link android.view.Surface.Rotation}.
243 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700244 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800245 */
246 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700247
Andrii Kulian8ee72852017-03-10 10:36:45 -0800248 /**
249 * Last applied orientation of the display.
250 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
251 *
252 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
253 */
254 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700255
Andrii Kulian8ee72852017-03-10 10:36:45 -0800256 /**
257 * Flag indicating that the application is receiving an orientation that has different metrics
258 * than it expected. E.g. Portrait instead of Landscape.
259 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700260 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800261 */
262 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700263
Andrii Kulian8ee72852017-03-10 10:36:45 -0800264 /**
265 * Orientation forced by some window. If there is no visible window that specifies orientation
266 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
267 *
268 * @see NonAppWindowContainers#getOrientation()
269 */
270 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700271
Andrii Kulian8ee72852017-03-10 10:36:45 -0800272 /**
273 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
274 * occluded.
275 *
276 * @see NonAppWindowContainers#getOrientation()
277 */
278 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
279
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700280 /**
281 * Keep track of wallpaper visibility to notify changes.
282 */
283 private boolean mLastWallpaperVisible = false;
284
Andrii Kulian06d07d62017-03-14 11:11:47 -0700285 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700286 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700287
Craig Mautner39834192012-09-02 07:47:24 -0700288 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700289 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700290 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700291 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800292 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700293
Craig Mautnerdc548482014-02-05 13:35:24 -0800294 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700295 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800296
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800297 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
298 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700299 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700300
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700301 /** Detect user tapping outside of current focused task bounds .*/
302 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700303
Craig Mautner6601b7b2013-04-29 10:29:11 -0700304 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700305 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700306
Craig Mautner6601b7b2013-04-29 10:29:11 -0700307 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800308 private final Rect mTmpRect = new Rect();
309 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700310 private final RectF mTmpRectF = new RectF();
311 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800312 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700313
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800314 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700315
Craig Mautner95da1082014-02-24 17:54:35 -0800316 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700317 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800318
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700319 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700320 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700321
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800322 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700323
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800324 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
325
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000326 private boolean mHaveBootMsg = false;
327 private boolean mHaveApp = false;
328 private boolean mHaveWallpaper = false;
329 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700330
331 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
332
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700333 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
334 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000335 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
336 new ApplySurfaceChangesTransactionState();
337 private final ScreenshotApplicationState mScreenshotApplicationState =
338 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700339
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700340 // True if this display is in the process of being removed. Used to determine if the removal of
341 // the display's direct children should be allowed.
342 private boolean mRemovingDisplay = false;
343
Bryce Leed1871262017-06-12 14:12:29 -0700344 // {@code false} if this display is in the processing of being created.
345 private boolean mDisplayReady = false;
346
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700347 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800348 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700349 int mInputMethodAnimLayerAdjustment;
350
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800351 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
352 WindowStateAnimator winAnimator = w.mWinAnimator;
353 if (winAnimator.hasSurface()) {
354 final boolean wasAnimating = winAnimator.mWasAnimating;
355 final boolean nowAnimating = winAnimator.stepAnimationLocked(
356 mTmpWindowAnimator.mCurrentTime);
357 winAnimator.mWasAnimating = nowAnimating;
358 mTmpWindowAnimator.orAnimating(nowAnimating);
359
360 if (DEBUG_WALLPAPER) Slog.v(TAG,
361 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
362
363 if (wasAnimating && !winAnimator.mAnimating
364 && mWallpaperController.isWallpaperTarget(w)) {
365 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
366 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
367 if (DEBUG_LAYOUT_REPEATS) {
368 mService.mWindowPlacerLocked.debugLayoutRepeats(
369 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
370 }
371 }
372 }
373
374 final AppWindowToken atoken = w.mAppToken;
375 if (winAnimator.mDrawState == READY_TO_SHOW) {
376 if (atoken == null || atoken.allDrawn) {
377 if (w.performShowLocked()) {
378 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
379 if (DEBUG_LAYOUT_REPEATS) {
380 mService.mWindowPlacerLocked.debugLayoutRepeats(
381 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
382 }
383 }
384 }
385 }
386 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
387 if (appAnimator != null && appAnimator.thumbnail != null) {
388 if (appAnimator.thumbnailTransactionSeq
389 != mTmpWindowAnimator.mAnimTransactionSequence) {
390 appAnimator.thumbnailTransactionSeq =
391 mTmpWindowAnimator.mAnimTransactionSequence;
392 appAnimator.thumbnailLayer = 0;
393 }
394 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
395 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
396 }
397 }
398 };
399
400 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
401 final WindowStateAnimator winAnimator = w.mWinAnimator;
402 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
403 return;
404 }
405
406 final int flags = w.mAttrs.flags;
407
408 // If this window is animating, make a note that we have an animating window and take
409 // care of a request to run a detached wallpaper animation.
410 if (winAnimator.mAnimating) {
411 if (winAnimator.mAnimation != null) {
412 if ((flags & FLAG_SHOW_WALLPAPER) != 0
413 && winAnimator.mAnimation.getDetachWallpaper()) {
414 mTmpWindow = w;
415 }
416 final int color = winAnimator.mAnimation.getBackgroundColor();
417 if (color != 0) {
418 final TaskStack stack = w.getStack();
419 if (stack != null) {
420 stack.setAnimationBackground(winAnimator, color);
421 }
422 }
423 }
424 mTmpWindowAnimator.setAnimating(true);
425 }
426
427 // If this window's app token is running a detached wallpaper animation, make a note so
428 // we can ensure the wallpaper is displayed behind it.
429 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
430 if (appAnimator != null && appAnimator.animation != null
431 && appAnimator.animating) {
432 if ((flags & FLAG_SHOW_WALLPAPER) != 0
433 && appAnimator.animation.getDetachWallpaper()) {
434 mTmpWindow = w;
435 }
436
437 final int color = appAnimator.animation.getBackgroundColor();
438 if (color != 0) {
439 final TaskStack stack = w.getStack();
440 if (stack != null) {
441 stack.setAnimationBackground(winAnimator, color);
442 }
443 }
444 }
445 };
446
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800447 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
448 final int lostFocusUid = mTmpWindow.mOwnerUid;
449 final Handler handler = mService.mH;
450 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
451 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
452 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
453 w.mAttrs.hideTimeoutMilliseconds);
454 }
455 }
456 };
457
458 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
459 final AppWindowToken focusedApp = mService.mFocusedApp;
460 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
461 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
462
463 if (!w.canReceiveKeys()) {
464 return false;
465 }
466
467 final AppWindowToken wtoken = w.mAppToken;
468
469 // If this window's application has been removed, just skip it.
470 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
471 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
472 + (wtoken.removed ? "removed" : "sendingToBottom"));
473 return false;
474 }
475
476 if (focusedApp == null) {
477 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
478 + " using new focus @ " + w);
479 mTmpWindow = w;
480 return true;
481 }
482
483 if (!focusedApp.windowsAreFocusable()) {
484 // Current focused app windows aren't focusable...
485 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
486 + " focusable using new focus @ " + w);
487 mTmpWindow = w;
488 return true;
489 }
490
491 // Descend through all of the app tokens and find the first that either matches
492 // win.mAppToken (return win) or mFocusedApp (return null).
493 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
494 if (focusedApp.compareTo(wtoken) > 0) {
495 // App stack below focused app stack. No focus for you!!!
496 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
497 "findFocusedWindow: Reached focused app=" + focusedApp);
498 mTmpWindow = null;
499 return true;
500 }
501 }
502
503 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
504 mTmpWindow = w;
505 return true;
506 };
507
508 private final Consumer<WindowState> mPrepareWindowSurfaces =
509 w -> w.mWinAnimator.prepareSurfaceLocked(true);
510
511 private final Consumer<WindowState> mPerformLayout = w -> {
512 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
513 // wasting time and funky changes while a window is animating away.
514 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
515 || w.isGoneForLayoutLw();
516
517 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
518 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
519 + " mLayoutAttached=" + w.mLayoutAttached
520 + " screen changed=" + w.isConfigChanged());
521 final AppWindowToken atoken = w.mAppToken;
522 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
523 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
524 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
525 + " parentHidden=" + w.isParentWindowHidden());
526 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
527 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
528 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
529 + " parentHidden=" + w.isParentWindowHidden());
530 }
531
532 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
533 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
534 // since that means "perform layout as normal, just don't display").
535 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
536 || ((w.isConfigChanged() || w.setReportResizeHints())
537 && !w.isGoneForLayoutLw() &&
538 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
539 (w.mHasSurface && w.mAppToken != null &&
540 w.mAppToken.layoutConfigChanges)))) {
541 if (!w.mLayoutAttached) {
542 if (mTmpInitial) {
543 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
544 w.mContentChanged = false;
545 }
546 if (w.mAttrs.type == TYPE_DREAM) {
547 // Don't layout windows behind a dream, so that if it does stuff like hide
548 // the status bar we won't get a bad transition when it goes away.
549 mTmpWindow = w;
550 }
551 w.mLayoutNeeded = false;
552 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200553 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800554 mService.mPolicy.layoutWindowLw(w, null);
555 w.mLayoutSeq = mService.mLayoutSeq;
556
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200557 // If this is the first layout, we need to initialize the last inset values as
558 // otherwise we'd immediately cause an unnecessary resize.
559 if (firstLayout) {
560 w.updateLastInsetValues();
561 }
562
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800563 // Window frames may have changed. Update dim layer with the new bounds.
564 final Task task = w.getTask();
565 if (task != null) {
566 mDimLayerController.updateDimLayer(task);
567 }
568
569 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
570 + " mContainingFrame=" + w.mContainingFrame
571 + " mDisplayFrame=" + w.mDisplayFrame);
572 }
573 }
574 };
575
576 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
577 if (w.mLayoutAttached) {
578 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
579 + " mViewVisibility=" + w.mViewVisibility
580 + " mRelayoutCalled=" + w.mRelayoutCalled);
581 // If this view is GONE, then skip it -- keep the current frame, and let the caller
582 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
583 // windows, since that means "perform layout as normal, just don't display").
584 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
585 return;
586 }
587 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
588 || w.mLayoutNeeded) {
589 if (mTmpInitial) {
590 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
591 w.mContentChanged = false;
592 }
593 w.mLayoutNeeded = false;
594 w.prelayout();
595 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
596 w.mLayoutSeq = mService.mLayoutSeq;
597 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
598 + " mContainingFrame=" + w.mContainingFrame
599 + " mDisplayFrame=" + w.mDisplayFrame);
600 }
601 } else if (w.mAttrs.type == TYPE_DREAM) {
602 // Don't layout windows behind a dream, so that if it does stuff like hide the
603 // status bar we won't get a bad transition when it goes away.
604 mTmpWindow = mTmpWindow2;
605 }
606 };
607
608 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
609 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
610 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
611 return w.canBeImeTarget();
612 };
613
614 private final Consumer<WindowState> mApplyPostLayoutPolicy =
615 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
616 mService.mInputMethodTarget);
617
618 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
619 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
620 final boolean obscuredChanged = w.mObscured !=
621 mTmpApplySurfaceChangesTransactionState.obscured;
622 final RootWindowContainer root = mService.mRoot;
623 // Only used if default window
624 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
625
626 // Update effect.
627 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
628 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
629 final boolean isDisplayed = w.isDisplayedLw();
630
631 if (isDisplayed && w.isObscuringDisplay()) {
632 // This window completely covers everything behind it, so we want to leave all
633 // of them as undimmed (for performance reasons).
634 root.mObscuringWindow = w;
635 mTmpApplySurfaceChangesTransactionState.obscured = true;
636 }
637
638 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
639 root.handleNotObscuredLocked(w,
640 mTmpApplySurfaceChangesTransactionState.obscured,
641 mTmpApplySurfaceChangesTransactionState.syswin);
642
643 if (w.mHasSurface && isDisplayed) {
644 final int type = w.mAttrs.type;
645 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
646 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
647 mTmpApplySurfaceChangesTransactionState.syswin = true;
648 }
649 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
650 && w.mAttrs.preferredRefreshRate != 0) {
651 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
652 = w.mAttrs.preferredRefreshRate;
653 }
654 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
655 && w.mAttrs.preferredDisplayModeId != 0) {
656 mTmpApplySurfaceChangesTransactionState.preferredModeId
657 = w.mAttrs.preferredDisplayModeId;
658 }
659 }
660 }
661
662 w.applyDimLayerIfNeeded();
663
664 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
665 && mWallpaperController.isWallpaperTarget(w)) {
666 // This is the wallpaper target and its obscured state changed... make sure the
667 // current wallpaper's visibility has been updated accordingly.
668 mWallpaperController.updateWallpaperVisibility();
669 }
670
671 w.handleWindowMovedIfNeeded();
672
673 final WindowStateAnimator winAnimator = w.mWinAnimator;
674
675 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
676 w.mContentChanged = false;
677
678 // Moved from updateWindowsAndWallpaperLocked().
679 if (w.mHasSurface) {
680 // Take care of the window being ready to display.
681 final boolean committed = winAnimator.commitFinishDrawingLocked();
682 if (isDefaultDisplay && committed) {
683 if (w.mAttrs.type == TYPE_DREAM) {
684 // HACK: When a dream is shown, it may at that point hide the lock screen.
685 // So we need to redo the layout to let the phone window manager make this
686 // happen.
687 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
688 if (DEBUG_LAYOUT_REPEATS) {
689 surfacePlacer.debugLayoutRepeats(
690 "dream and commitFinishDrawingLocked true",
691 pendingLayoutChanges);
692 }
693 }
694 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
695 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
696 "First draw done in potential wallpaper target " + w);
697 root.mWallpaperMayChange = true;
698 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
699 if (DEBUG_LAYOUT_REPEATS) {
700 surfacePlacer.debugLayoutRepeats(
701 "wallpaper and commitFinishDrawingLocked true",
702 pendingLayoutChanges);
703 }
704 }
705 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700706 final TaskStack stack = w.getStack();
707 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
708 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800709 // Updates the shown frame before we set up the surface. This is needed
710 // because the resizing could change the top-left position (in addition to
711 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
712 // position the surface.
713 //
714 // If an animation is being started, we can't call this method because the
715 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700716 // we do want to update the position if the window is animating. We make an exception
717 // for the bounds animating state, where an application may have been waiting
718 // for an exit animation to start, but instead enters PiP. We need to ensure
719 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800720 winAnimator.computeShownFrameLocked();
721 }
722 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
723 }
724
725 final AppWindowToken atoken = w.mAppToken;
726 if (atoken != null) {
727 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
728 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
729 mTmpUpdateAllDrawn.add(atoken);
730 }
731 }
732
733 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
734 && w.isDisplayedLw()) {
735 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
736 }
737
738 w.updateResizingWindowIfNeeded();
739 };
740
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800741 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800742 * Create new {@link DisplayContent} instance, add itself to the root window container and
743 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700744 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800745 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700746 * @param layersController window layer controller used to assign layer to the windows on this
747 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700748 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
749 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700750 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700751 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700752 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800753 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
754 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
755 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
756 + " new=" + display);
757 }
758
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700759 mDisplay = display;
760 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700761 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700762 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700763 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700764 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700765 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700766 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700767 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800768 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700769 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800770 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700771
772 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700773 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700774 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700775 super.addChild(mAboveAppWindowsContainers, null);
776 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800777
778 // Add itself as a child to the root container.
779 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700780
781 // TODO(b/62541591): evaluate whether this is the best spot to declare the
782 // {@link DisplayContent} ready for use.
783 mDisplayReady = true;
784 }
785
786 boolean isReady() {
787 // The display is ready when the system and the individual display are both ready.
788 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700789 }
790
791 int getDisplayId() {
792 return mDisplayId;
793 }
794
Wale Ogunwale02319a62016-09-26 15:21:22 -0700795 WindowToken getWindowToken(IBinder binder) {
796 return mTokenMap.get(binder);
797 }
798
799 AppWindowToken getAppWindowToken(IBinder binder) {
800 final WindowToken token = getWindowToken(binder);
801 if (token == null) {
802 return null;
803 }
804 return token.asAppWindowToken();
805 }
806
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700807 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700808 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
809 if (dc != null) {
810 // We currently don't support adding a window token to the display if the display
811 // already has the binder mapped to another token. If there is a use case for supporting
812 // this moving forward we will either need to merge the WindowTokens some how or have
813 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700814 throw new IllegalArgumentException("Can't map token=" + token + " to display="
815 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700816 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700817 if (binder == null) {
818 throw new IllegalArgumentException("Can't map token=" + token + " to display="
819 + getName() + " binder is null");
820 }
821 if (token == null) {
822 throw new IllegalArgumentException("Can't map null token to display="
823 + getName() + " binder=" + binder);
824 }
825
Wale Ogunwale02319a62016-09-26 15:21:22 -0700826 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700827
828 if (token.asAppWindowToken() == null) {
829 // Add non-app token to container hierarchy on the display. App tokens are added through
830 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700831 switch (token.windowType) {
832 case TYPE_WALLPAPER:
833 mBelowAppWindowsContainers.addChild(token);
834 break;
835 case TYPE_INPUT_METHOD:
836 case TYPE_INPUT_METHOD_DIALOG:
837 mImeWindowsContainers.addChild(token);
838 break;
839 default:
840 mAboveAppWindowsContainers.addChild(token);
841 break;
842 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700843 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700844 }
845
846 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700847 final WindowToken token = mTokenMap.remove(binder);
848 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800849 token.setExiting();
850 }
851 return token;
852 }
853
854 /** Changes the display the input window token is housed on to this one. */
855 void reParentWindowToken(WindowToken token) {
856 final DisplayContent prevDc = token.getDisplayContent();
857 if (prevDc == this) {
858 return;
859 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800860 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
861 && token.asAppWindowToken() == null) {
862 // Removed the token from the map, but made sure it's not an app token before removing
863 // from parent.
864 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700865 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800866
867 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700868 }
869
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700870 void removeAppToken(IBinder binder) {
871 final WindowToken token = removeWindowToken(binder);
872 if (token == null) {
873 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
874 return;
875 }
876
877 final AppWindowToken appToken = token.asAppWindowToken();
878
879 if (appToken == null) {
880 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
881 return;
882 }
883
884 appToken.onRemovedFromDisplay();
885 }
886
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700887 Display getDisplay() {
888 return mDisplay;
889 }
890
Craig Mautner59c00972012-07-30 12:10:24 -0700891 DisplayInfo getDisplayInfo() {
892 return mDisplayInfo;
893 }
894
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700895 DisplayMetrics getDisplayMetrics() {
896 return mDisplayMetrics;
897 }
898
Andrii Kulian8ee72852017-03-10 10:36:45 -0800899 int getRotation() {
900 return mRotation;
901 }
902
903 void setRotation(int newRotation) {
904 mRotation = newRotation;
905 }
906
907 int getLastOrientation() {
908 return mLastOrientation;
909 }
910
911 void setLastOrientation(int orientation) {
912 mLastOrientation = orientation;
913 }
914
915 boolean getAltOrientation() {
916 return mAltOrientation;
917 }
918
919 void setAltOrientation(boolean altOrientation) {
920 mAltOrientation = altOrientation;
921 }
922
923 int getLastWindowForcedOrientation() {
924 return mLastWindowForcedOrientation;
925 }
926
Andrii Kulian06d07d62017-03-14 11:11:47 -0700927 /**
928 * Update rotation of the display.
929 *
930 * Returns true if the rotation has been changed. In this case YOU MUST CALL
931 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
932 */
933 boolean updateRotationUnchecked(boolean inTransaction) {
934 if (mService.mDeferredRotationPauseCount > 0) {
935 // Rotation updates have been paused temporarily. Defer the update until
936 // updates have been resumed.
937 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
938 return false;
939 }
940
941 ScreenRotationAnimation screenRotationAnimation =
942 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
943 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
944 // Rotation updates cannot be performed while the previous rotation change
945 // animation is still in progress. Skip this update. We will try updating
946 // again after the animation is finished and the display is unfrozen.
947 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
948 return false;
949 }
950 if (mService.mDisplayFrozen) {
951 // Even if the screen rotation animation has finished (e.g. isAnimating
952 // returns false), there is still some time where we haven't yet unfrozen
953 // the display. We also need to abort rotation here.
954 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
955 "Deferring rotation, still finishing previous rotation");
956 return false;
957 }
958
959 if (!mService.mDisplayEnabled) {
960 // No point choosing a rotation if the display is not enabled.
961 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
962 return false;
963 }
964
965 final int oldRotation = mRotation;
966 final int lastOrientation = mLastOrientation;
967 final boolean oldAltOrientation = mAltOrientation;
968 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700969 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
970 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700971
Robert Carr897215d2017-03-29 12:25:50 -0700972 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700973 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
974 if (seamlessRotated != null) {
975 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
976 // to complete (that is, waiting for windows to redraw). It's tempting to check
977 // w.mSeamlessRotationCount but that could be incorrect in the case of
978 // window-removal.
979 return false;
980 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700981 }
982
983 // TODO: Implement forced rotation changes.
984 // Set mAltOrientation to indicate that the application is receiving
985 // an orientation that has different metrics than it expected.
986 // eg. Portrait instead of Landscape.
987
988 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
989 lastOrientation, rotation);
990
991 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
992 + ", got rotation " + rotation + " which has "
993 + (altOrientation ? "incompatible" : "compatible") + " metrics");
994
995 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
996 // No change.
997 return false;
998 }
999
1000 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1001 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1002 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1003
1004 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1005 mService.mWaitingForConfig = true;
1006 }
1007
1008 mRotation = rotation;
1009 mAltOrientation = altOrientation;
1010 if (isDefaultDisplay) {
1011 mService.mPolicy.setRotationLw(rotation);
1012 }
1013
1014 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1015 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1016 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1017 WINDOW_FREEZE_TIMEOUT_DURATION);
1018
1019 setLayoutNeeded();
1020 final int[] anim = new int[2];
1021 if (isDimming()) {
1022 anim[0] = anim[1] = 0;
1023 } else {
1024 mService.mPolicy.selectRotationAnimationLw(anim);
1025 }
1026
1027 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001028 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001029 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1030 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1031 mDisplayId);
1032 } else {
1033 // The screen rotation animation uses a screenshot to freeze the screen
1034 // while windows resize underneath.
1035 // When we are rotating seamlessly, we allow the elements to transition
1036 // to their rotated state independently and without a freeze required.
1037 screenRotationAnimation = null;
1038
1039 // We have to reset this in case a window was removed before it
1040 // finished seamless rotation.
1041 mService.mSeamlessRotationCount = 0;
1042 }
1043
1044 // We need to update our screen size information to match the new rotation. If the rotation
1045 // has actually changed then this method will return true and, according to the comment at
1046 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1047 // By updating the Display info here it will be available to
1048 // #computeScreenConfiguration() later.
1049 updateDisplayAndOrientation(getConfiguration().uiMode);
1050
1051 if (!inTransaction) {
1052 if (SHOW_TRANSACTIONS) {
1053 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1054 }
1055 mService.openSurfaceTransaction();
1056 }
1057 try {
1058 // NOTE: We disable the rotation in the emulator because
1059 // it doesn't support hardware OpenGL emulation yet.
1060 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1061 && screenRotationAnimation.hasScreenshot()) {
1062 if (screenRotationAnimation.setRotationInTransaction(
1063 rotation, mService.mFxSession,
1064 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1065 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1066 mService.scheduleAnimationLocked();
1067 }
1068 }
1069
1070 if (rotateSeamlessly) {
1071 forAllWindows(w -> {
1072 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1073 }, true /* traverseTopToBottom */);
1074 }
1075
1076 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1077 } finally {
1078 if (!inTransaction) {
1079 mService.closeSurfaceTransaction();
1080 if (SHOW_LIGHT_TRANSACTIONS) {
1081 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1082 }
1083 }
1084 }
1085
1086 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001087 if (w.mHasSurface && !rotateSeamlessly) {
1088 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001089 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001090 mService.mRoot.mOrientationChangeComplete = false;
1091 w.mLastFreezeDuration = 0;
1092 }
1093 w.mReportOrientationChanged = true;
1094 }, true /* traverseTopToBottom */);
1095
1096 if (rotateSeamlessly) {
1097 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1098 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1099 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1100 }
1101
1102 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1103 final WindowManagerService.RotationWatcher rotationWatcher
1104 = mService.mRotationWatchers.get(i);
1105 if (rotationWatcher.mDisplayId == mDisplayId) {
1106 try {
1107 rotationWatcher.mWatcher.onRotationChanged(rotation);
1108 } catch (RemoteException e) {
1109 // Ignore
1110 }
1111 }
1112 }
1113
1114 // TODO (multi-display): Magnification is supported only for the default display.
1115 // Announce rotation only if we will not animate as we already have the
1116 // windows in final state. Otherwise, we make this call at the rotation end.
1117 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1118 && isDefaultDisplay) {
1119 mService.mAccessibilityController.onRotationChangedLocked(this);
1120 }
1121
1122 return true;
1123 }
1124
1125 /**
1126 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1127 * changed.
1128 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1129 */
1130 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1131 // Use the effective "visual" dimensions based on current rotation
1132 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1133 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1134 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1135 int dw = realdw;
1136 int dh = realdh;
1137
1138 if (mAltOrientation) {
1139 if (realdw > realdh) {
1140 // Turn landscape into portrait.
1141 int maxw = (int)(realdh/1.3f);
1142 if (maxw < realdw) {
1143 dw = maxw;
1144 }
1145 } else {
1146 // Turn portrait into landscape.
1147 int maxh = (int)(realdw/1.3f);
1148 if (maxh < realdh) {
1149 dh = maxh;
1150 }
1151 }
1152 }
1153
1154 // Update application display metrics.
1155 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1156 mDisplayId);
1157 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1158 mDisplayId);
1159 mDisplayInfo.rotation = mRotation;
1160 mDisplayInfo.logicalWidth = dw;
1161 mDisplayInfo.logicalHeight = dh;
1162 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1163 mDisplayInfo.appWidth = appWidth;
1164 mDisplayInfo.appHeight = appHeight;
1165 if (isDefaultDisplay) {
1166 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1167 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1168 }
1169 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1170 if (mDisplayScalingDisabled) {
1171 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1172 } else {
1173 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1174 }
1175
1176 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1177 mDisplayInfo);
1178
1179 mBaseDisplayRect.set(0, 0, dw, dh);
1180
1181 if (isDefaultDisplay) {
1182 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1183 mCompatDisplayMetrics);
1184 }
1185 return mDisplayInfo;
1186 }
1187
1188 /**
1189 * Compute display configuration based on display properties and policy settings.
1190 * Do not call if mDisplayReady == false.
1191 */
1192 void computeScreenConfiguration(Configuration config) {
1193 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1194
1195 final int dw = displayInfo.logicalWidth;
1196 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001197 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1198 // TODO: Probably best to set this based on some setting in the display content object,
1199 // so the display can be configured for things like fullscreen.
1200 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001201
Andrii Kulian06d07d62017-03-14 11:11:47 -07001202 config.screenWidthDp =
1203 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1204 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1205 config.screenHeightDp =
1206 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1207 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001208
1209 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1210 final int leftInset = mTmpRect.left;
1211 final int topInset = mTmpRect.top;
1212 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001213 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1214 leftInset + displayInfo.appWidth /* right */,
1215 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001216 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1217 || displayInfo.rotation == Surface.ROTATION_270);
1218
1219 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1220 mDisplayMetrics.density, config);
1221
1222 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1223 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1224 ? Configuration.SCREENLAYOUT_ROUND_YES
1225 : Configuration.SCREENLAYOUT_ROUND_NO);
1226
1227 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1228 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1229 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1230 dh, mDisplayId);
1231 config.densityDpi = displayInfo.logicalDensityDpi;
1232
1233 config.colorMode =
1234 (displayInfo.isHdr()
1235 ? Configuration.COLOR_MODE_HDR_YES
1236 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001237 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001238 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1239 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1240
1241 // Update the configuration based on available input devices, lid switch,
1242 // and platform configuration.
1243 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1244 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1245 config.navigation = Configuration.NAVIGATION_NONAV;
1246
1247 int keyboardPresence = 0;
1248 int navigationPresence = 0;
1249 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1250 final int len = devices != null ? devices.length : 0;
1251 for (int i = 0; i < len; i++) {
1252 InputDevice device = devices[i];
1253 if (!device.isVirtual()) {
1254 final int sources = device.getSources();
1255 final int presenceFlag = device.isExternal() ?
1256 WindowManagerPolicy.PRESENCE_EXTERNAL :
1257 WindowManagerPolicy.PRESENCE_INTERNAL;
1258
1259 // TODO(multi-display): Configure on per-display basis.
1260 if (mService.mIsTouchDevice) {
1261 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1262 InputDevice.SOURCE_TOUCHSCREEN) {
1263 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1264 }
1265 } else {
1266 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1267 }
1268
1269 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1270 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1271 navigationPresence |= presenceFlag;
1272 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1273 && config.navigation == Configuration.NAVIGATION_NONAV) {
1274 config.navigation = Configuration.NAVIGATION_DPAD;
1275 navigationPresence |= presenceFlag;
1276 }
1277
1278 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1279 config.keyboard = Configuration.KEYBOARD_QWERTY;
1280 keyboardPresence |= presenceFlag;
1281 }
1282 }
1283 }
1284
1285 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1286 config.navigation = Configuration.NAVIGATION_DPAD;
1287 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1288 }
1289
1290 // Determine whether a hard keyboard is available and enabled.
1291 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1292 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1293 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1294 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1295 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1296 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1297 }
1298
1299 // Let the policy update hidden states.
1300 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1301 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1302 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1303 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1304 }
1305
1306 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1307 int displayId) {
1308 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1309 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1310 final int unrotDw, unrotDh;
1311 if (rotated) {
1312 unrotDw = dh;
1313 unrotDh = dw;
1314 } else {
1315 unrotDw = dw;
1316 unrotDh = dh;
1317 }
1318 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1319 displayId);
1320 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1321 displayId);
1322 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1323 displayId);
1324 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1325 displayId);
1326 return sw;
1327 }
1328
1329 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1330 DisplayMetrics dm, int dw, int dh, int displayId) {
1331 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1332 displayId);
1333 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1334 uiMode, displayId);
1335 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1336 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1337 if (curSize == 0 || size < curSize) {
1338 curSize = size;
1339 }
1340 return curSize;
1341 }
1342
1343 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1344 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1345
1346 // We need to determine the smallest width that will occur under normal
1347 // operation. To this, start with the base screen size and compute the
1348 // width under the different possible rotations. We need to un-rotate
1349 // the current screen dimensions before doing this.
1350 int unrotDw, unrotDh;
1351 if (rotated) {
1352 unrotDw = dh;
1353 unrotDh = dw;
1354 } else {
1355 unrotDw = dw;
1356 unrotDh = dh;
1357 }
1358 displayInfo.smallestNominalAppWidth = 1<<30;
1359 displayInfo.smallestNominalAppHeight = 1<<30;
1360 displayInfo.largestNominalAppWidth = 0;
1361 displayInfo.largestNominalAppHeight = 0;
1362 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1363 unrotDh);
1364 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1365 unrotDw);
1366 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1367 unrotDh);
1368 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1369 unrotDw);
1370 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1371 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1372 displayId);
1373 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1374 displayId);
1375 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1376 displayId);
1377 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1378 displayId);
1379 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1380 outConfig.screenLayout = sl;
1381 }
1382
1383 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1384 int uiMode, int displayId) {
1385 // Get the app screen size at this rotation.
1386 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1387 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1388
1389 // Compute the screen layout size class for this rotation.
1390 int longSize = w;
1391 int shortSize = h;
1392 if (longSize < shortSize) {
1393 int tmp = longSize;
1394 longSize = shortSize;
1395 shortSize = tmp;
1396 }
1397 longSize = (int)(longSize/density);
1398 shortSize = (int)(shortSize/density);
1399 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1400 }
1401
1402 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1403 int uiMode, int dw, int dh) {
1404 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1405 displayId);
1406 if (width < displayInfo.smallestNominalAppWidth) {
1407 displayInfo.smallestNominalAppWidth = width;
1408 }
1409 if (width > displayInfo.largestNominalAppWidth) {
1410 displayInfo.largestNominalAppWidth = width;
1411 }
1412 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1413 displayId);
1414 if (height < displayInfo.smallestNominalAppHeight) {
1415 displayInfo.smallestNominalAppHeight = height;
1416 }
1417 if (height > displayInfo.largestNominalAppHeight) {
1418 displayInfo.largestNominalAppHeight = height;
1419 }
1420 }
1421
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001422 DockedStackDividerController getDockedDividerController() {
1423 return mDividerControllerLocked;
1424 }
1425
Winson Chung655332c2016-10-31 13:14:28 -07001426 PinnedStackController getPinnedStackController() {
1427 return mPinnedStackControllerLocked;
1428 }
1429
Jeff Browna506a6e2013-06-04 00:02:38 -07001430 /**
1431 * Returns true if the specified UID has access to this display.
1432 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001433 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001434 return mDisplay.hasAccess(uid);
1435 }
1436
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001437 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001438 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001439 }
1440
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001441 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001442 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001443 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001444 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001445 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001446 }
1447
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001448 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001449 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1450 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001451 if (stack.mStackId == stackId) {
1452 return stack;
1453 }
1454 }
1455 return null;
1456 }
1457
Bryce Lee48f4b572017-04-10 10:54:15 -07001458 @VisibleForTesting
1459 int getStackCount() {
1460 return mTaskStackContainers.size();
1461 }
1462
1463 @VisibleForTesting
Wale Ogunwale98d62312017-07-12 09:24:56 -07001464 int getStackPosById(int stackId) {
Bryce Lee48f4b572017-04-10 10:54:15 -07001465 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1466 final TaskStack stack = mTaskStackContainers.get(i);
1467 if (stack.mStackId == stackId) {
1468 return i;
1469 }
1470 }
1471 return -1;
1472 }
1473
Andrii Kulian441e4492016-09-29 15:25:00 -07001474 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001475 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001476 super.onConfigurationChanged(newParentConfig);
1477
Andrii Kulian3a507b52016-09-19 18:14:12 -07001478 // The display size information is heavily dependent on the resources in the current
1479 // configuration, so we need to reconfigure it every time the configuration changes.
1480 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1481 mService.reconfigureDisplayLocked(this);
1482
1483 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001484 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001485 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001486
Andrii Kulian441e4492016-09-29 15:25:00 -07001487 /**
1488 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1489 * bounds were updated.
1490 */
1491 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001492 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1493 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001494 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001495 changedStackList.add(stack.mStackId);
1496 }
1497 }
Winson Chung32c566f2017-04-11 18:31:21 -07001498
1499 // If there was no pinned stack, we still need to notify the controller of the display info
1500 // update as a result of the config change. We do this here to consolidate the flow between
1501 // changes when there is and is not a stack.
1502 if (getStackById(PINNED_STACK_ID) == null) {
1503 mPinnedStackControllerLocked.onDisplayInfoChanged();
1504 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001505 }
1506
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001507 @Override
1508 boolean fillsParent() {
1509 return true;
1510 }
1511
1512 @Override
1513 boolean isVisible() {
1514 return true;
1515 }
1516
1517 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001518 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001519 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001520 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001521 }
1522
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001523 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001524 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1525 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1526 // target, or here in order if there isn't an IME target.
1527 if (traverseTopToBottom) {
1528 for (int i = mChildren.size() - 1; i >= 0; --i) {
1529 final DisplayChildWindowContainer child = mChildren.get(i);
1530 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1531 // In this case the Ime windows will be processed above their target so we skip
1532 // here.
1533 continue;
1534 }
1535 if (child.forAllWindows(callback, traverseTopToBottom)) {
1536 return true;
1537 }
1538 }
1539 } else {
1540 final int count = mChildren.size();
1541 for (int i = 0; i < count; i++) {
1542 final DisplayChildWindowContainer child = mChildren.get(i);
1543 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1544 // In this case the Ime windows will be processed above their target so we skip
1545 // here.
1546 continue;
1547 }
1548 if (child.forAllWindows(callback, traverseTopToBottom)) {
1549 return true;
1550 }
1551 }
1552 }
1553 return false;
1554 }
1555
1556 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1557 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1558 }
1559
Wale Ogunwale399c8692017-05-08 14:22:42 -07001560 @Override
1561 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001562 final WindowManagerPolicy policy = mService.mPolicy;
1563
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001564 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001565 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001566 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001567 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001568 // If the display is frozen, some activities may be in the middle of restarting, and
1569 // thus have removed their old window. If the window has the flag to hide the lock
1570 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1571 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001572 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001573 } else if (policy.isKeyguardLocked()) {
1574 // Use the last orientation the while the display is frozen with the keyguard
1575 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1576 // window. We don't want to check the show when locked window directly though as
1577 // things aren't stable while the display is frozen, for example the window could be
1578 // momentarily unavailable due to activity relaunch.
1579 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001580 + "return " + mLastOrientation);
1581 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001582 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001583 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001584 final int orientation = mAboveAppWindowsContainers.getOrientation();
1585 if (orientation != SCREEN_ORIENTATION_UNSET) {
1586 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001587 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001588 }
1589
Wale Ogunwale399c8692017-05-08 14:22:42 -07001590 // Top system windows are not requesting an orientation. Start searching from apps.
1591 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001592 }
1593
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001594 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001595 // Check if display metrics changed and update base values if needed.
1596 updateBaseDisplayMetricsIfNeeded();
1597
Craig Mautner722285e2012-09-07 13:55:58 -07001598 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001599 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001600
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001601 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1602 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001603 }
Craig Mautner722285e2012-09-07 13:55:58 -07001604 }
1605
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001606 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001607 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1608 if (displayManagerInternal != null) {
1609 // Bootstrap the default logical display from the display manager.
1610 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1611 if (newDisplayInfo != null) {
1612 mDisplayInfo.copyFrom(newDisplayInfo);
1613 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001614 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001615
Andrii Kuliancd097992017-03-23 18:31:59 -07001616 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1617 mDisplayInfo.logicalDensityDpi);
1618 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1619 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1620 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001621 }
1622
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001623 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001624 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001625 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001626 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001627 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1628 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001629 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001630 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001631 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001632 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001633 out.set(left, top, left + width, top + height);
1634 }
1635
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001636 private void getLogicalDisplayRect(Rect out, int orientation) {
1637 getLogicalDisplayRect(out);
1638
1639 // Rotate the Rect if needed.
1640 final int currentRotation = mDisplayInfo.rotation;
1641 final int rotationDelta = deltaRotation(currentRotation, orientation);
1642 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1643 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1644 mTmpRectF.set(out);
1645 mTmpMatrix.mapRect(mTmpRectF);
1646 mTmpRectF.round(out);
1647 }
1648 }
1649
Andrii Kuliancd097992017-03-23 18:31:59 -07001650 /**
1651 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1652 * values.
1653 */
1654 private void updateBaseDisplayMetricsIfNeeded() {
1655 // Get real display metrics without overrides from WM.
1656 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1657 final int orientation = mDisplayInfo.rotation;
1658 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1659 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1660 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1661 final int newDensity = mDisplayInfo.logicalDensityDpi;
1662
1663 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1664 || mInitialDisplayHeight != newHeight
1665 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1666
1667 if (displayMetricsChanged) {
1668 // Check if display size or density is forced.
1669 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1670 || mBaseDisplayHeight != mInitialDisplayHeight;
1671 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1672
1673 // If there is an override set for base values - use it, otherwise use new values.
1674 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1675 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1676 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1677
1678 // Real display metrics changed, so we should also update initial values.
1679 mInitialDisplayWidth = newWidth;
1680 mInitialDisplayHeight = newHeight;
1681 mInitialDisplayDensity = newDensity;
1682 mService.reconfigureDisplayLocked(this);
1683 }
1684 }
1685
Bryce Lee27cec322017-03-21 09:41:37 -07001686 /** Sets the maximum width the screen resolution can be */
1687 void setMaxUiWidth(int width) {
1688 if (DEBUG_DISPLAY) {
1689 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1690 }
1691
1692 mMaxUiWidth = width;
1693
1694 // Update existing metrics.
1695 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1696 }
1697
1698 /** Update base (override) display metrics. */
1699 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1700 mBaseDisplayWidth = baseWidth;
1701 mBaseDisplayHeight = baseHeight;
1702 mBaseDisplayDensity = baseDensity;
1703
1704 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1705 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1706 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1707 mBaseDisplayWidth = mMaxUiWidth;
1708
1709 if (DEBUG_DISPLAY) {
1710 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1711 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1712 + " on display:" + getDisplayId());
1713 }
1714 }
1715
1716 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1717 }
1718
Chong Zhangf66db432016-01-13 10:39:51 -08001719 void getContentRect(Rect out) {
1720 out.set(mContentRect);
1721 }
1722
Wale Ogunwale1666e312016-12-16 11:27:18 -08001723 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1724 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1725 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001726
Wale Ogunwale1666e312016-12-16 11:27:18 -08001727 TaskStack stack = getStackById(stackId);
1728 if (stack != null) {
1729 // It's already attached to the display...clear mDeferRemoval and move stack to
1730 // appropriate z-order on display as needed.
1731 stack.mDeferRemoval = false;
1732 // We're not moving the display to front when we're adding stacks, only when
1733 // requested to change the position of stack explicitly.
1734 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1735 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001736 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001737 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001738 mTaskStackContainers.addStackToDisplay(stack, onTop);
1739 }
1740
Wale Ogunwale1666e312016-12-16 11:27:18 -08001741 if (stackId == DOCKED_STACK_ID) {
1742 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001743 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001744 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001745 }
1746
Andrii Kulian51c1b672017-04-07 18:39:32 -07001747 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001748 final DisplayContent prevDc = stack.getDisplayContent();
1749 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001750 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1751 + " which is not currently attached to any display");
1752 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001753 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001754 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1755 + " to its current displayId=" + mDisplayId);
1756 }
1757
Wale Ogunwale1666e312016-12-16 11:27:18 -08001758 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001759 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001760 }
1761
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001762 @Override
1763 protected void addChild(DisplayChildWindowContainer child,
1764 Comparator<DisplayChildWindowContainer> comparator) {
1765 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1766 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001767
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001768 @Override
1769 protected void addChild(DisplayChildWindowContainer child, int index) {
1770 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1771 }
1772
1773 @Override
1774 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001775 // Only allow removal of direct children from this display if the display is in the process
1776 // of been removed.
1777 if (mRemovingDisplay) {
1778 super.removeChild(child);
1779 return;
1780 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001781 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001782 }
1783
Andrii Kuliand2765632016-12-12 22:26:34 -08001784 @Override
1785 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1786 // Children of the display are statically ordered, so the real intention here is to perform
1787 // the operation on the display and not the static direct children.
1788 getParent().positionChildAt(position, this, includingParents);
1789 }
1790
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001791 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001792 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1793 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001794 final int taskId = stack.taskIdFromPoint(x, y);
1795 if (taskId != -1) {
1796 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001797 }
1798 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001799 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001800 }
1801
Chong Zhang8e89b312015-09-09 15:09:30 -07001802 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001803 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001804 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001805 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001806 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001807 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001808 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001809 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1810 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001811 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001812 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001813 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001814
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001815 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1816 if (mTmpTaskForResizePointSearchResult.searchDone) {
1817 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001818 }
1819 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001820 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001821 }
1822
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001823 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001824 // The provided task is the task on this display with focus, so if WindowManagerService's
1825 // focused app is not on this display, focusedTask will be null.
1826 if (focusedTask == null) {
1827 mTouchExcludeRegion.setEmpty();
1828 } else {
1829 mTouchExcludeRegion.set(mBaseDisplayRect);
1830 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1831 mTmpRect2.setEmpty();
1832 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1833 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1834 stack.setTouchExcludeRegion(
1835 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1836 }
1837 // If we removed the focused task above, add it back and only leave its
1838 // outside touch area in the exclusion. TapDectector is not interested in
1839 // any touch inside the focused task itself.
1840 if (!mTmpRect2.isEmpty()) {
1841 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1842 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001843 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001844 final WindowState inputMethod = mService.mInputMethodWindow;
1845 if (inputMethod != null && inputMethod.isVisibleLw()) {
1846 // If the input method is visible and the user is typing, we don't want these touch
1847 // events to be intercepted and used to change focus. This would likely cause a
1848 // disappearance of the input method.
1849 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001850 if (inputMethod.getDisplayId() == mDisplayId) {
1851 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1852 } else {
1853 // IME is on a different display, so we need to update its tap detector.
1854 // TODO(multidisplay): Remove when IME will always appear on same display.
1855 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1856 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001857 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001858 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1859 WindowState win = mTapExcludedWindows.get(i);
1860 win.getTouchableRegion(mTmpRegion);
1861 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1862 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001863 // TODO(multi-display): Support docked stacks on secondary displays.
1864 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001865 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001866 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001867 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1868 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001869 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001870 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001871 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001872 }
1873
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001874 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001875 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001876 super.switchUser();
1877 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001878 }
1879
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001880 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001881 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1882 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001883 }
1884 }
1885
1886 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001887 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001888 }
1889
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001890 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001891 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001892 }
1893
1894 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001895 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001896 }
1897
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001898 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001899 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001900 }
1901
Wale Ogunwale10124582016-09-15 20:25:50 -07001902 @Override
1903 void removeIfPossible() {
1904 if (isAnimating()) {
1905 mDeferredRemoval = true;
1906 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001907 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001908 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001909 }
1910
Wale Ogunwale10124582016-09-15 20:25:50 -07001911 @Override
1912 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001913 mRemovingDisplay = true;
1914 try {
1915 super.removeImmediately();
1916 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1917 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001918 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001919 mService.unregisterPointerEventListener(mTapDetector);
1920 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1921 }
1922 } finally {
1923 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001924 }
Craig Mautner95da1082014-02-24 17:54:35 -08001925 }
1926
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001927 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001928 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001929 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001930 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1931
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001932 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001933 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001934 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001935 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001936 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001937 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001938 }
1939
Andrii Kulian0214ed92017-05-16 13:44:05 -07001940 /** @return 'true' if removal of this display content is deferred due to active animation. */
1941 boolean isRemovalDeferred() {
1942 return mDeferredRemoval;
1943 }
1944
Wale Ogunwale10124582016-09-15 20:25:50 -07001945 boolean animateForIme(float interpolatedValue, float animationTarget,
1946 float dividerAnimationTarget) {
1947 boolean updated = false;
1948
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001949 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1950 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001951 if (stack == null || !stack.isAdjustedForIme()) {
1952 continue;
1953 }
1954
1955 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1956 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1957 updated = true;
1958 } else {
1959 mDividerControllerLocked.mLastAnimationProgress =
1960 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1961 mDividerControllerLocked.mLastDividerProgress =
1962 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1963 updated |= stack.updateAdjustForIme(
1964 mDividerControllerLocked.mLastAnimationProgress,
1965 mDividerControllerLocked.mLastDividerProgress,
1966 false /* force */);
1967 }
1968 if (interpolatedValue >= 1f) {
1969 stack.endImeAdjustAnimation();
1970 }
1971 }
1972
1973 return updated;
1974 }
1975
1976 boolean clearImeAdjustAnimation() {
1977 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001978 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1979 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001980 if (stack != null && stack.isAdjustedForIme()) {
1981 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1982 changed = true;
1983 }
1984 }
1985 return changed;
1986 }
1987
1988 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001989 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1990 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001991 if (stack.isVisible() && stack.isAdjustedForIme()) {
1992 stack.beginImeAdjustAnimation();
1993 }
1994 }
1995 }
1996
1997 void adjustForImeIfNeeded() {
1998 final WindowState imeWin = mService.mInputMethodWindow;
1999 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2000 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002001 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07002002 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2003 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2004 imeTargetStack.getDockSide() : DOCKED_INVALID;
2005 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2006 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2007 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
2008 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
2009 final boolean imeHeightChanged = imeVisible &&
2010 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2011
2012 // The divider could be adjusted for IME position, or be thinner than usual,
2013 // or both. There are three possible cases:
2014 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2015 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2016 // - If IME is not visible, divider is not moved and is normal width.
2017
2018 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002019 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2020 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002021 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07002022 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
2023 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002024 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2025 } else {
2026 stack.resetAdjustedForIme(false);
2027 }
2028 }
2029 mDividerControllerLocked.setAdjustedForIme(
2030 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2031 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002032 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2033 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002034 stack.resetAdjustedForIme(!dockVisible);
2035 }
2036 mDividerControllerLocked.setAdjustedForIme(
2037 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2038 }
Winson Chung655332c2016-10-31 13:14:28 -07002039 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002040 }
2041
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002042 void setInputMethodAnimLayerAdjustment(int adj) {
2043 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2044 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002045 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002046 }
2047
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002048 /**
2049 * If a window that has an animation specifying a colored background and the current wallpaper
2050 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2051 * suddenly disappear.
2052 */
2053 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002054 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2055 w -> w.mIsWallpaper && w.isVisibleNow());
2056
2057 if (visibleWallpaper != null) {
2058 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002059 }
2060 return winAnimator.mAnimLayer;
2061 }
2062
Wale Ogunwale10124582016-09-15 20:25:50 -07002063 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002064 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2065 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002066 stack.prepareFreezingTaskBounds();
2067 }
2068 }
2069
Wale Ogunwale94744212015-09-21 19:01:47 -07002070 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002071 getLogicalDisplayRect(mTmpRect, newRotation);
2072
2073 // Compute a transform matrix to undo the coordinate space transformation,
2074 // and present the window at the same physical position it previously occupied.
2075 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2076 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2077
2078 mTmpRectF.set(bounds);
2079 mTmpMatrix.mapRect(mTmpRectF);
2080 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002081 }
2082
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002083 static int deltaRotation(int oldRotation, int newRotation) {
2084 int delta = newRotation - oldRotation;
2085 if (delta < 0) delta += 4;
2086 return delta;
2087 }
2088
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002089 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002090 Matrix outMatrix) {
2091 // For rotations without Z-ordering we don't need the target rectangle's position.
2092 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2093 displayHeight, outMatrix);
2094 }
2095
2096 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2097 float displayWidth, float displayHeight, Matrix outMatrix) {
2098 switch (rotation) {
2099 case ROTATION_0:
2100 outMatrix.reset();
2101 break;
2102 case ROTATION_270:
2103 outMatrix.setRotate(270, 0, 0);
2104 outMatrix.postTranslate(0, displayHeight);
2105 outMatrix.postTranslate(rectTop, 0);
2106 break;
2107 case ROTATION_180:
2108 outMatrix.reset();
2109 break;
2110 case ROTATION_90:
2111 outMatrix.setRotate(90, 0, 0);
2112 outMatrix.postTranslate(displayWidth, 0);
2113 outMatrix.postTranslate(-rectTop, rectLeft);
2114 break;
2115 }
2116 }
2117
Steven Timotiusaf03df62017-07-18 16:56:43 -07002118 void writeToProto(ProtoOutputStream proto, long fieldId) {
2119 final long token = proto.start(fieldId);
2120 proto.write(ID, mDisplayId);
2121 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2122 final TaskStack stack = mTaskStackContainers.get(stackNdx);
2123 stack.writeToProto(proto, STACKS);
2124 }
2125 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2126 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
2127 for (int i = mAboveAppWindowsContainers.size() - 1; i >= 0; --i) {
2128 final WindowToken windowToken = mAboveAppWindowsContainers.get(i);
2129 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS);
2130 }
2131 for (int i = mBelowAppWindowsContainers.size() - 1; i >= 0; --i) {
2132 final WindowToken windowToken = mBelowAppWindowsContainers.get(i);
2133 windowToken.writeToProto(proto, BELOW_APP_WINDOWS);
2134 }
2135 for (int i = mImeWindowsContainers.size() - 1; i >= 0; --i) {
2136 final WindowToken windowToken = mImeWindowsContainers.get(i);
2137 windowToken.writeToProto(proto, IME_WINDOWS);
2138 }
2139 proto.write(DPI, mBaseDisplayDensity);
2140 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002141 proto.write(ROTATION, mRotation);
2142 final ScreenRotationAnimation screenRotationAnimation =
2143 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2144 if (screenRotationAnimation != null) {
2145 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2146 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002147 proto.end(token);
2148 }
2149
Craig Mautnera91f9e22012-09-14 16:22:08 -07002150 public void dump(String prefix, PrintWriter pw) {
2151 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2152 final String subPrefix = " " + prefix;
2153 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2154 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2155 pw.print("dpi");
2156 if (mInitialDisplayWidth != mBaseDisplayWidth
2157 || mInitialDisplayHeight != mBaseDisplayHeight
2158 || mInitialDisplayDensity != mBaseDisplayDensity) {
2159 pw.print(" base=");
2160 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2161 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2162 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002163 if (mDisplayScalingDisabled) {
2164 pw.println(" noscale");
2165 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002166 pw.print(" cur=");
2167 pw.print(mDisplayInfo.logicalWidth);
2168 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2169 pw.print(" app=");
2170 pw.print(mDisplayInfo.appWidth);
2171 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2172 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2173 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2174 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2175 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002176 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002177 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002178 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002179
Craig Mautnerdc548482014-02-05 13:35:24 -08002180 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002181 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002182 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2183 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002184 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002185 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002186
Craig Mautnerdc548482014-02-05 13:35:24 -08002187 pw.println();
2188 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002189 pw.println();
2190 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002191 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002192 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002193 pw.print(" Exiting #"); pw.print(i);
2194 pw.print(' '); pw.print(token);
2195 pw.println(':');
2196 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002197 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002198 }
Craig Mautner59c00972012-07-30 12:10:24 -07002199 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002200 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002201 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002202 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002203 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002204 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002205
2206 if (mInputMethodAnimLayerAdjustment != 0) {
2207 pw.println(subPrefix
2208 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2209 }
Craig Mautner59c00972012-07-30 12:10:24 -07002210 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002211
2212 @Override
2213 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002214 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002215 }
2216
2217 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002218 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002219 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002220
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002221 /** Checks if stack with provided id is visible on this display. */
2222 boolean isStackVisible(int stackId) {
2223 final TaskStack stack = getStackById(stackId);
2224 return (stack != null && stack.isVisible());
2225 }
2226
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002227 /**
2228 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2229 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002230 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002231 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002232 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002233 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002234
2235 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002236 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002237 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002238 */
Jorim Jaggife762342016-10-13 14:33:27 +02002239 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002240 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002241 }
2242
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002243 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002244 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002245 final int x = (int) xf;
2246 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002247 final WindowState touchedWin = getWindow(w -> {
2248 final int flags = w.mAttrs.flags;
2249 if (!w.isVisibleLw()) {
2250 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002251 }
2252 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002253 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002254 }
2255
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002256 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002257 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002258 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002259 }
2260
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002261 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002262
2263 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002264 return mTmpRegion.contains(x, y) || touchFlags == 0;
2265 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002266
2267 return touchedWin;
2268 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002269
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002270 boolean canAddToastWindowForUid(int uid) {
2271 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002272 // Also if the app is focused adding more than one toast at
2273 // a time for better backwards compatibility.
2274 final WindowState focusedWindowForUid = getWindow(w ->
2275 w.mOwnerUid == uid && w.isFocused());
2276 if (focusedWindowForUid != null) {
2277 return true;
2278 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002279 final WindowState win = getWindow(w ->
2280 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2281 && !w.mWindowRemovalAllowed);
2282 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002283 }
2284
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002285 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002286 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2287 return;
2288 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002289
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002290 // Used to communicate the old focus to the callback method.
2291 mTmpWindow = oldFocus;
2292
2293 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002294 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002295
2296 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002297 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002298
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002299 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002300
2301 if (mTmpWindow == null) {
2302 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2303 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002304 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002305 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002306 }
2307
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002308 /** Updates the layer assignment of windows on this display. */
2309 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002310 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002311 if (setLayoutNeeded) {
2312 setLayoutNeeded();
2313 }
2314 }
2315
Wale Ogunwale1666e312016-12-16 11:27:18 -08002316 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2317 // moving containers or resizing them. Need to investigate the best way to have it automatically
2318 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002319 void layoutAndAssignWindowLayersIfNeeded() {
2320 mService.mWindowsChanged = true;
2321 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002322
2323 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2324 false /*updateInputWindows*/)) {
2325 assignWindowLayers(false /* setLayoutNeeded */);
2326 }
2327
2328 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2329 mService.mWindowPlacerLocked.performSurfacePlacement();
2330 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2331 }
2332
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002333 /** Returns true if a leaked surface was destroyed */
2334 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002335 // Used to indicate that a surface was leaked.
2336 mTmpWindow = null;
2337 forAllWindows(w -> {
2338 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002339 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002340 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002341 }
2342 if (!mService.mSessions.contains(wsa.mSession)) {
2343 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002344 + w + " surface=" + wsa.mSurfaceController
2345 + " token=" + w.mToken
2346 + " pid=" + w.mSession.mPid
2347 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002348 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002349 mService.mForceRemoves.add(w);
2350 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002351 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002352 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002353 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002354 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002355 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002356 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002357 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002358 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002359 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002360
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002361 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002362 }
2363
2364 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002365 * Determine and return the window that should be the IME target.
2366 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2367 * @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 +00002368 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002369 WindowState computeImeTarget(boolean updateImeTarget) {
2370 if (mService.mInputMethodWindow == null) {
2371 // There isn't an IME so there shouldn't be a target...That was easy!
2372 if (updateImeTarget) {
2373 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2374 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2375 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2376 }
2377 return null;
2378 }
2379
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002380 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2381 // same display. Or even when the current IME/target are not on the same screen as the next
2382 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002383 mUpdateImeTarget = updateImeTarget;
2384 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002385
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002386
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002387 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2388 // to be on top of it, but it is not -really- where input will go. So look down below
2389 // for a real window to target...
2390 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2391 final AppWindowToken token = target.mAppToken;
2392 if (token != null) {
2393 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2394 if (betterTarget != null) {
2395 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002396 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002397 }
2398 }
2399
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002400 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2401 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002402
2403 // Now, a special case -- if the last target's window is in the process of exiting, and is
2404 // above the new target, keep on the last target to avoid flicker. Consider for example a
2405 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2406 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2407 // scrim.
2408 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002409 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2410 && (target == null
2411 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002412 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002413 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002414 }
2415
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002416 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2417 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002418
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002419 if (target == null) {
2420 if (updateImeTarget) {
2421 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2422 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2423 + Debug.getCallers(4) : ""));
2424 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2425 }
2426
2427 return null;
2428 }
2429
2430 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002431 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2432 if (token != null) {
2433
2434 // Now some fun for dealing with window animations that modify the Z order. We need
2435 // to look at all windows below the current target that are in this app, finding the
2436 // highest visible one in layering.
2437 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002438 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002439 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002440 }
2441
2442 if (highestTarget != null) {
2443 final AppTransition appTransition = mService.mAppTransition;
2444 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2445 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2446 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002447 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002448
2449 if (appTransition.isTransitionSet()) {
2450 // If we are currently setting up for an animation, hold everything until we
2451 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002452 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2453 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002454 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002455 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002456 // If the window we are currently targeting is involved with an animation,
2457 // and it is on top of the next target we will be over, then hold off on
2458 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2460 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002461 }
2462 }
2463 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002464
2465 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2466 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2467 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002468 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002469 }
2470
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002471 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002472 }
2473
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002474 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2475 if (target == mService.mInputMethodTarget
2476 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2477 && mInputMethodAnimLayerAdjustment == layerAdj) {
2478 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002479 }
2480
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002481 mService.mInputMethodTarget = target;
2482 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2483 setInputMethodAnimLayerAdjustment(layerAdj);
2484 assignWindowLayers(false /* setLayoutNeeded */);
2485 }
2486
2487 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2488 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2489 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2490 }
2491
2492 // Used to indicate we have reached the first window in the range we are interested in.
2493 mTmpWindow = null;
2494
2495 // TODO: Figure-out a more efficient way to do this.
2496 final WindowState candidate = getWindow(w -> {
2497 if (w == top) {
2498 // Reached the first window in the range we are interested in.
2499 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002500 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002501 if (mTmpWindow == null) {
2502 return false;
2503 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002504
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002505 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2506 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002507 }
2508 // If we reached the bottom of the range of windows we are considering,
2509 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002510 if (w == bottom) {
2511 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002512 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002513 return false;
2514 });
2515
2516 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002517 }
2518
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002519 void setLayoutNeeded() {
2520 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2521 mLayoutNeeded = true;
2522 }
2523
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002524 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002525 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2526 mLayoutNeeded = false;
2527 }
2528
2529 boolean isLayoutNeeded() {
2530 return mLayoutNeeded;
2531 }
2532
Wale Ogunwale02319a62016-09-26 15:21:22 -07002533 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2534 if (mTokenMap.isEmpty()) {
2535 return;
2536 }
2537 pw.println(" Display #" + mDisplayId);
2538 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2539 while (it.hasNext()) {
2540 final WindowToken token = it.next();
2541 pw.print(" ");
2542 pw.print(token);
2543 if (dumpAll) {
2544 pw.println(':');
2545 token.dump(pw, " ");
2546 } else {
2547 pw.println();
2548 }
2549 }
2550 }
2551
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002552 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002553 final int[] index = new int[1];
2554 forAllWindows(w -> {
2555 final WindowStateAnimator wAnim = w.mWinAnimator;
2556 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2557 index[0] = index[0] + 1;
2558 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002559 }
2560
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002561 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002562 forAllWindows(w -> {
2563 w.mWinAnimator.enableSurfaceTrace(fd);
2564 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002565 }
2566
2567 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002568 forAllWindows(w -> {
2569 w.mWinAnimator.disableSurfaceTrace();
2570 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002571 }
2572
Jorim Jaggife762342016-10-13 14:33:27 +02002573 /**
2574 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2575 */
2576 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2577 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002578 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002579 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2580 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002581 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002582 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2583 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002584 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002585 }
2586
Wale Ogunwale494009b82016-10-21 09:01:38 -07002587 boolean checkWaitingForWindows() {
2588
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002589 mHaveBootMsg = false;
2590 mHaveApp = false;
2591 mHaveWallpaper = false;
2592 mHaveKeyguard = true;
2593
2594 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002595 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2596 return true;
2597 }
2598 if (w.isDrawnLw()) {
2599 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002600 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002601 } else if (w.mAttrs.type == TYPE_APPLICATION
2602 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002603 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002604 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002605 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002606 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002607 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002608 }
2609 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002610 return false;
2611 });
2612
2613 if (visibleWindow != null) {
2614 // We have a visible window.
2615 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002616 }
2617
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002618 // if the wallpaper service is disabled on the device, we're never going to have
2619 // wallpaper, don't bother waiting for it
2620 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2621 com.android.internal.R.bool.config_enableWallpaperService)
2622 && !mService.mOnlyCore;
2623
Wale Ogunwale494009b82016-10-21 09:01:38 -07002624 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2625 "******** booted=" + mService.mSystemBooted
2626 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002627 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2628 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2629 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002630
2631 // If we are turning on the screen to show the boot message, don't do it until the boot
2632 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002633 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002634 return true;
2635 }
2636
2637 // If we are turning on the screen after the boot is completed normally, don't do so until
2638 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002639 if (mService.mSystemBooted
2640 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002641 return true;
2642 }
2643
2644 return false;
2645 }
2646
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002647 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002648 mTmpWindowAnimator = animator;
2649 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002650 }
2651
2652 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002653 resetAnimationBackgroundAnimator();
2654
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002655 // Used to indicate a detached wallpaper.
2656 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002657 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002658
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002659 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002660
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002661 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002662 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002663 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2664 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002665 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2666 }
2667 }
2668
2669 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002670 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002671 }
2672
Wale Ogunwale494009b82016-10-21 09:01:38 -07002673 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002674 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002675 if (imFocus == null) {
2676 return false;
2677 }
2678
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002679 if (DEBUG_INPUT_METHOD) {
2680 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2681 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2682 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002683 }
2684
Wale Ogunwale494009b82016-10-21 09:01:38 -07002685 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2686
2687 if (DEBUG_INPUT_METHOD) {
2688 Slog.i(TAG_WM, "IM target client: " + imeClient);
2689 if (imeClient != null) {
2690 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2691 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2692 }
2693 }
2694
2695 return imeClient != null && imeClient.asBinder() == client.asBinder();
2696 }
2697
2698 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002699 final WindowState win = getWindow(
2700 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2701 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002702 }
2703
2704 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002705 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002706 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002707 final int curValue = w.mSystemUiVisibility;
2708 final int diff = (curValue ^ visibility) & globalDiff;
2709 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002710 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002711 w.mSeq++;
2712 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002713 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002714 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2715 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002716 visibility, newValue, diff);
2717 }
2718 } catch (RemoteException e) {
2719 // so sorry
2720 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002721 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002722 }
2723
2724 void onWindowFreezeTimeout() {
2725 Slog.w(TAG_WM, "Window freeze timeout expired.");
2726 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002727
2728 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002729 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002730 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002731 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002732 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002733 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2734 - mService.mDisplayFreezeTime);
2735 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002736 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002737 mService.mWindowPlacerLocked.performSurfacePlacement();
2738 }
2739
2740 void waitForAllWindowsDrawn() {
2741 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002742 forAllWindows(w -> {
2743 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2744 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2745 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002746 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002747 w.mLastContentInsets.set(-1, -1, -1, -1);
2748 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002749 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002750 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002751 }
2752
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002753 // TODO: Super crazy long method that should be broken down...
2754 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2755
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002756 final int dw = mDisplayInfo.logicalWidth;
2757 final int dh = mDisplayInfo.logicalHeight;
2758 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2759
2760 mTmpUpdateAllDrawn.clear();
2761
2762 int repeats = 0;
2763 do {
2764 repeats++;
2765 if (repeats > 6) {
2766 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2767 clearLayoutNeeded();
2768 break;
2769 }
2770
2771 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2772 pendingLayoutChanges);
2773
Andrii Kulian839def92016-11-02 10:58:58 -07002774 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2775 // the wallpaper window jumping across displays.
2776 // Remove check for default display when there will be support for multiple wallpaper
2777 // targets (on different displays).
2778 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002779 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002780 }
2781
2782 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2783 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2784 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2785 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002786 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002787 }
2788 }
2789
2790 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2791 setLayoutNeeded();
2792 }
2793
2794 // FIRST LOOP: Perform a layout, if needed.
2795 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2796 performLayout(repeats == 1, false /* updateInputWindows */);
2797 } else {
2798 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2799 }
2800
2801 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2802 pendingLayoutChanges = 0;
2803
2804 if (isDefaultDisplay) {
2805 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002806 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002807 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2808 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2809 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2810 }
2811 } while (pendingLayoutChanges != 0);
2812
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002813 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002814 resetDimming();
2815
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002816 mTmpRecoveringMemory = recoveringMemory;
2817 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002818
2819 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002820 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2821 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2822 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002823 true /* inTraversal, must call performTraversalInTrans... below */);
2824
2825 stopDimmingIfNeeded();
2826
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002827 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2828 if (wallpaperVisible != mLastWallpaperVisible) {
2829 mLastWallpaperVisible = wallpaperVisible;
2830 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2831 }
2832
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002833 while (!mTmpUpdateAllDrawn.isEmpty()) {
2834 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2835 // See if any windows have been drawn, so they (and others associated with them)
2836 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002837 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002838 }
2839
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002840 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002841 }
2842
2843 void performLayout(boolean initial, boolean updateInputWindows) {
2844 if (!isLayoutNeeded()) {
2845 return;
2846 }
2847 clearLayoutNeeded();
2848
2849 final int dw = mDisplayInfo.logicalWidth;
2850 final int dh = mDisplayInfo.logicalHeight;
2851
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002852 if (DEBUG_LAYOUT) {
2853 Slog.v(TAG, "-------------------------------------");
2854 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2855 }
2856
Andrii Kulian8ee72852017-03-10 10:36:45 -08002857 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002858 getConfiguration().uiMode);
2859 if (isDefaultDisplay) {
2860 // Not needed on non-default displays.
2861 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2862 mService.mScreenRect.set(0, 0, dw, dh);
2863 }
2864
2865 mService.mPolicy.getContentRectLw(mContentRect);
2866
2867 int seq = mService.mLayoutSeq + 1;
2868 if (seq < 0) seq = 0;
2869 mService.mLayoutSeq = seq;
2870
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002871 // Used to indicate that we have processed the dream window and all additional windows are
2872 // behind it.
2873 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002874 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002875
2876 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002877 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002878
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002879 // Used to indicate that we have processed the dream window and all additional attached
2880 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002881 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002882 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002883
2884 // Now perform layout of attached windows, which usually depend on the position of the
2885 // window they are attached to. XXX does not deal with windows that are attached to windows
2886 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002887 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002888
2889 // Window frames may have changed. Tell the input dispatcher about it.
2890 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2891 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2892 if (updateInputWindows) {
2893 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2894 }
2895
2896 mService.mPolicy.finishLayoutLw();
2897 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2898 }
2899
2900 /**
2901 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2902 * In portrait mode, it grabs the full screenshot.
2903 *
2904 * @param width the width of the target bitmap
2905 * @param height the height of the target bitmap
2906 * @param includeFullDisplay true if the screen should not be cropped before capture
2907 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2908 * @param config of the output bitmap
2909 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002910 * @param includeDecor whether to include window decors, like the status or navigation bar
2911 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002912 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002914 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002915 boolean wallpaperOnly, boolean includeDecor) {
2916 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2917 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002918 if (bitmap == null) {
2919 return null;
2920 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002921
2922 if (DEBUG_SCREENSHOT) {
2923 // TEST IF IT's ALL BLACK
2924 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2925 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2926 bitmap.getHeight());
2927 boolean allBlack = true;
2928 final int firstColor = buffer[0];
2929 for (int i = 0; i < buffer.length; i++) {
2930 if (buffer[i] != firstColor) {
2931 allBlack = false;
2932 break;
2933 }
2934 }
2935 if (allBlack) {
2936 final WindowState appWin = mScreenshotApplicationState.appWin;
2937 final int maxLayer = mScreenshotApplicationState.maxLayer;
2938 final int minLayer = mScreenshotApplicationState.minLayer;
2939 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2940 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2941 (appWin != null ?
2942 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2943 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2944 }
2945 }
2946
2947 // Create a copy of the screenshot that is immutable and backed in ashmem.
2948 // This greatly reduces the overhead of passing the bitmap between processes.
2949 Bitmap ret = bitmap.createAshmemBitmap(config);
2950 bitmap.recycle();
2951 return ret;
2952 }
2953
2954 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2955 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2956 boolean includeDecor) {
2957 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2958 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2959 }
2960
2961 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2962 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2963 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002964 int dw = mDisplayInfo.logicalWidth;
2965 int dh = mDisplayInfo.logicalHeight;
2966 if (dw == 0 || dh == 0) {
2967 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2968 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2969 return null;
2970 }
2971
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002972 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002973
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002974 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002975 final Rect frame = new Rect();
2976 final Rect stackBounds = new Rect();
2977
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002978 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002979 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002980 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002981 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002982 if (!mService.mPolicy.isScreenOn()) {
2983 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
2984 + " was off.");
2985 return null;
2986 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002987 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002988 mScreenshotApplicationState.appWin = null;
2989 forAllWindows(w -> {
2990 if (!w.mHasSurface) {
2991 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002992 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002993 if (w.mLayer >= aboveAppLayer) {
2994 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002995 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002996 if (wallpaperOnly && !w.mIsWallpaper) {
2997 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002998 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002999 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003000 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003001 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003002 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3003 // then the target window state is this one.
3004 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003005 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003006 }
3007
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003008 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003009 // We have not ran across the target window yet, so it is probably behind
3010 // the wallpaper. This can happen when the keyguard is up and all windows
3011 // are moved behind the wallpaper. We don't want to include the wallpaper
3012 // layer in the screenshot as it will cover-up the layer of the target
3013 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003014 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003015 }
3016 // Fall through. The target window is in front of the wallpaper. For this
3017 // case we want to include the wallpaper layer in the screenshot because
3018 // the target window might have some transparent areas.
3019 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003020 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003021 // This app window is of no interest if it is not associated with the
3022 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003023 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003024 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003025 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003026 }
3027
3028 // Include this window.
3029
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003030 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003031 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003032 if (mScreenshotApplicationState.maxLayer < layer) {
3033 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003034 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003035 if (mScreenshotApplicationState.minLayer > layer) {
3036 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003037 }
3038
3039 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003040 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3041 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003042 final Task task = w.getTask();
3043 if (task != null) {
3044 task.getBounds(frame);
3045 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003046
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003047 // No task bounds? Too bad! Ain't no screenshot then.
3048 return true;
3049 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003050 } else {
3051 final Rect wf = w.mFrame;
3052 final Rect cr = w.mContentInsets;
3053 int left = wf.left + cr.left;
3054 int top = wf.top + cr.top;
3055 int right = wf.right - cr.right;
3056 int bottom = wf.bottom - cr.bottom;
3057 frame.union(left, top, right, bottom);
3058 w.getVisibleBounds(stackBounds);
3059 if (!Rect.intersects(frame, stackBounds)) {
3060 // Set frame empty if there's no intersection.
3061 frame.setEmpty();
3062 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003063 }
3064 }
3065
3066 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003067 (w.mAppToken != null && w.mAppToken.token == appToken)
3068 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01003069 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003070 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003071 }
3072
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003073 if (w.isObscuringDisplay()){
3074 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003075 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003076 return false;
3077 }, true /* traverseTopToBottom */);
3078
3079 final WindowState appWin = mScreenshotApplicationState.appWin;
3080 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3081 final int maxLayer = mScreenshotApplicationState.maxLayer;
3082 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003083
3084 if (appToken != null && appWin == null) {
3085 // Can't find a window to snapshot.
3086 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3087 "Screenshot: Couldn't find a surface matching " + appToken);
3088 return null;
3089 }
3090
3091 if (!screenshotReady) {
3092 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3093 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3094 appWin.mWinAnimator.mDrawState)));
3095 return null;
3096 }
3097
3098 // Screenshot is ready to be taken. Everything from here below will continue
3099 // through the bottom of the loop and return a value. We only stay in the loop
3100 // because we don't want to release the mWindowMap lock until the screenshot is
3101 // taken.
3102
3103 if (maxLayer == 0) {
3104 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3105 + ": returning null maxLayer=" + maxLayer);
3106 return null;
3107 }
3108
Jorim Jaggi02886a82016-12-06 09:10:06 -08003109 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003110 // Constrain frame to the screen size.
3111 if (!frame.intersect(0, 0, dw, dh)) {
3112 frame.setEmpty();
3113 }
3114 } else {
3115 // Caller just wants entire display.
3116 frame.set(0, 0, dw, dh);
3117 }
3118 if (frame.isEmpty()) {
3119 return null;
3120 }
3121
3122 if (width < 0) {
3123 width = (int) (frame.width() * frameScale);
3124 }
3125 if (height < 0) {
3126 height = (int) (frame.height() * frameScale);
3127 }
3128
3129 // Tell surface flinger what part of the image to crop. Take the top
3130 // right part of the application, and crop the larger dimension to fit.
3131 Rect crop = new Rect(frame);
3132 if (width / (float) frame.width() < height / (float) frame.height()) {
3133 int cropWidth = (int)((float)width / (float)height * frame.height());
3134 crop.right = crop.left + cropWidth;
3135 } else {
3136 int cropHeight = (int)((float)height / (float)width * frame.width());
3137 crop.bottom = crop.top + cropHeight;
3138 }
3139
3140 // The screenshot API does not apply the current screen rotation.
3141 int rot = mDisplay.getRotation();
3142
3143 if (rot == ROTATION_90 || rot == ROTATION_270) {
3144 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3145 }
3146
3147 // Surfaceflinger is not aware of orientation, so convert our logical
3148 // crop to surfaceflinger's portrait orientation.
3149 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3150
3151 if (DEBUG_SCREENSHOT) {
3152 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3153 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003154 forAllWindows(w -> {
3155 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3156 Slog.i(TAG_WM, w + ": " + w.mLayer
3157 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003158 + " surfaceLayer=" + ((controller == null)
3159 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003160 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003161 }
3162
3163 final ScreenRotationAnimation screenRotationAnimation =
3164 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3165 final boolean inRotation = screenRotationAnimation != null &&
3166 screenRotationAnimation.isAnimating();
3167 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3168 "Taking screenshot while rotating");
3169
3170 // We force pending transactions to flush before taking
3171 // the screenshot by pushing an empty synchronous transaction.
3172 SurfaceControl.openTransaction();
3173 SurfaceControl.closeTransactionSync();
3174
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003175 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003176 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003177 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003178 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3179 + ") to layer " + maxLayer);
3180 return null;
3181 }
3182 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003183 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003184 }
3185
3186 // TODO: Can this use createRotationMatrix()?
3187 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3188 if (rot == Surface.ROTATION_90) {
3189 final int tmp = crop.top;
3190 crop.top = dw - crop.right;
3191 crop.right = crop.bottom;
3192 crop.bottom = dw - crop.left;
3193 crop.left = tmp;
3194 } else if (rot == Surface.ROTATION_180) {
3195 int tmp = crop.top;
3196 crop.top = dh - crop.bottom;
3197 crop.bottom = dh - tmp;
3198 tmp = crop.right;
3199 crop.right = dw - crop.left;
3200 crop.left = dw - tmp;
3201 } else if (rot == Surface.ROTATION_270) {
3202 final int tmp = crop.top;
3203 crop.top = crop.left;
3204 crop.left = dh - crop.bottom;
3205 crop.bottom = crop.right;
3206 crop.right = dh - tmp;
3207 }
3208 }
3209
3210 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003211 // Used to indicate the layout is needed.
3212 mTmpWindow = null;
3213
3214 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003215 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003216 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003217 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003218 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003219 w.setDisplayLayoutNeeded();
3220 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003221 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003222
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003223 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003224 mService.mWindowPlacerLocked.performSurfacePlacement();
3225 }
3226 }
3227
Wale Ogunwale1666e312016-12-16 11:27:18 -08003228 void setExitingTokensHasVisible(boolean hasVisible) {
3229 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3230 mExitingTokens.get(i).hasVisible = hasVisible;
3231 }
3232
3233 // Initialize state of exiting applications.
3234 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3235 }
3236
3237 void removeExistingTokensIfPossible() {
3238 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3239 final WindowToken token = mExitingTokens.get(i);
3240 if (!token.hasVisible) {
3241 mExitingTokens.remove(i);
3242 }
3243 }
3244
3245 // Time to remove any exiting applications?
3246 mTaskStackContainers.removeExistingAppTokensIfPossible();
3247 }
3248
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003249 @Override
3250 void onDescendantOverrideConfigurationChanged() {
3251 setLayoutNeeded();
3252 mService.requestTraversal();
3253 }
3254
David Stevens9440dc82017-03-16 19:00:20 -07003255 boolean okToDisplay() {
3256 if (mDisplayId == DEFAULT_DISPLAY) {
3257 return !mService.mDisplayFrozen
3258 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3259 }
3260 return mDisplayInfo.state == Display.STATE_ON;
3261 }
3262
3263 boolean okToAnimate() {
3264 return okToDisplay() &&
3265 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3266 }
3267
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003268 static final class TaskForResizePointSearchResult {
3269 boolean searchDone;
3270 Task taskForResize;
3271
3272 void reset() {
3273 searchDone = false;
3274 taskForResize = null;
3275 }
3276 }
Robert Carr3b716242016-08-16 16:02:21 -07003277
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003278 private static final class ApplySurfaceChangesTransactionState {
3279 boolean displayHasContent;
3280 boolean obscured;
3281 boolean syswin;
3282 boolean focusDisplayed;
3283 float preferredRefreshRate;
3284 int preferredModeId;
3285
3286 void reset() {
3287 displayHasContent = false;
3288 obscured = false;
3289 syswin = false;
3290 focusDisplayed = false;
3291 preferredRefreshRate = 0;
3292 preferredModeId = 0;
3293 }
3294 }
3295
3296 private static final class ScreenshotApplicationState {
3297 WindowState appWin;
3298 int maxLayer;
3299 int minLayer;
3300 boolean screenshotReady;
3301
3302 void reset(boolean screenshotReady) {
3303 appWin = null;
3304 maxLayer = 0;
3305 minLayer = 0;
3306 this.screenshotReady = screenshotReady;
3307 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3308 }
3309 }
3310
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003311 /**
3312 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3313 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3314 * homogeneous children type which is currently required by sub-classes of
3315 * {@link WindowContainer} class.
3316 */
3317 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3318
3319 int size() {
3320 return mChildren.size();
3321 }
3322
3323 E get(int index) {
3324 return mChildren.get(index);
3325 }
3326
3327 @Override
3328 boolean fillsParent() {
3329 return true;
3330 }
3331
3332 @Override
3333 boolean isVisible() {
3334 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003335 }
3336 }
3337
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003338 /**
3339 * Window container class that contains all containers on this display relating to Apps.
3340 * I.e Activities.
3341 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003342 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003343
Andrii Kulian839def92016-11-02 10:58:58 -07003344 /**
3345 * Adds the stack to this container.
3346 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3347 */
3348 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003349 if (stack.mStackId == HOME_STACK_ID) {
3350 if (mHomeStack != null) {
3351 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3352 }
3353 mHomeStack = stack;
3354 }
3355 addChild(stack, onTop);
3356 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003357 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003358
Andrii Kulian839def92016-11-02 10:58:58 -07003359 /** Removes the stack from its container and prepare for changing the parent. */
3360 void removeStackFromDisplay(TaskStack stack) {
3361 removeChild(stack);
3362 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003363 }
3364
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003365 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003366 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3367 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003368 addChild(stack, addIndex);
3369 setLayoutNeeded();
3370 }
3371
Bryce Lee00d586d2017-07-28 20:48:43 -07003372
3373 @Override
3374 boolean isOnTop() {
3375 // Considered always on top
3376 return true;
3377 }
3378
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003379 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003380 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003381 if (child.getWindowConfiguration().isAlwaysOnTop()
3382 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003383 // This stack is always-on-top, override the default behavior.
3384 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3385
3386 // Moving to its current position, as we must call super but we don't want to
3387 // perform any meaningful action.
3388 final int currentPosition = mChildren.indexOf(child);
3389 super.positionChildAt(currentPosition, child, false /* includingParents */);
3390 return;
3391 }
3392
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003393 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3394 super.positionChildAt(targetPosition, child, includingParents);
3395
3396 setLayoutNeeded();
3397 }
3398
3399 /**
3400 * When stack is added or repositioned, find a proper position for it.
3401 * This will make sure that pinned stack always stays on top.
3402 * @param requestedPosition Position requested by caller.
3403 * @param stack Stack to be added or positioned.
3404 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3405 * @return The proper position for the stack.
3406 */
3407 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3408 final int topChildPosition = mChildren.size() - 1;
3409 boolean toTop = requestedPosition == POSITION_TOP;
3410 toTop |= adding ? requestedPosition >= topChildPosition + 1
3411 : requestedPosition >= topChildPosition;
3412 int targetPosition = requestedPosition;
3413
Bryce Lee48f4b572017-04-10 10:54:15 -07003414 if (toTop && stack.mStackId != PINNED_STACK_ID
3415 && getStackById(PINNED_STACK_ID) != null) {
3416 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003417 TaskStack topStack = mChildren.get(topChildPosition);
3418 if (topStack.mStackId != PINNED_STACK_ID) {
3419 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3420 }
3421
3422 // So, stack is moved just below the pinned stack.
3423 // When we're adding a new stack the target is the current pinned stack position.
3424 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003425 // stack, because WindowContainer#positionAt() first removes element and then adds
3426 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003427 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3428 }
3429
3430 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003431 }
3432
3433 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003434 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3435 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003436 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003437 if (super.forAllWindows(callback, traverseTopToBottom)) {
3438 return true;
3439 }
3440 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3441 return true;
3442 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003443 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003444 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3445 return true;
3446 }
3447 if (super.forAllWindows(callback, traverseTopToBottom)) {
3448 return true;
3449 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003450 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003451 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003452 }
3453
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003454 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003455 boolean traverseTopToBottom) {
3456 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3457 // app tokens.
3458 // TODO: Investigate if we need to continue to do this or if we can just process them
3459 // in-order.
3460 if (traverseTopToBottom) {
3461 for (int i = mChildren.size() - 1; i >= 0; --i) {
3462 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3463 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003464 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3465 traverseTopToBottom)) {
3466 return true;
3467 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003468 }
3469 }
3470 } else {
3471 final int count = mChildren.size();
3472 for (int i = 0; i < count; ++i) {
3473 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3474 final int appTokensCount = appTokens.size();
3475 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003476 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3477 traverseTopToBottom)) {
3478 return true;
3479 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003480 }
3481 }
3482 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003483 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003484 }
3485
Wale Ogunwale1666e312016-12-16 11:27:18 -08003486 void setExitingTokensHasVisible(boolean hasVisible) {
3487 for (int i = mChildren.size() - 1; i >= 0; --i) {
3488 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3489 for (int j = appTokens.size() - 1; j >= 0; --j) {
3490 appTokens.get(j).hasVisible = hasVisible;
3491 }
3492 }
3493 }
3494
3495 void removeExistingAppTokensIfPossible() {
3496 for (int i = mChildren.size() - 1; i >= 0; --i) {
3497 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3498 for (int j = appTokens.size() - 1; j >= 0; --j) {
3499 final AppWindowToken token = appTokens.get(j);
3500 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3501 && (!token.mIsExiting || token.isEmpty())) {
3502 // Make sure there is no animation running on this token, so any windows
3503 // associated with it will be removed as soon as their animations are
3504 // complete.
3505 token.mAppAnimator.clearAnimation();
3506 token.mAppAnimator.animating = false;
3507 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3508 "performLayout: App token exiting now removed" + token);
3509 token.removeIfPossible();
3510 }
3511 }
3512 }
3513 }
3514
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003515 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003516 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003517 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003518 // Apps and their containers are not allowed to specify an orientation while the
3519 // docked or freeform stack is visible...except for the home stack/task if the
3520 // docked stack is minimized and it actually set something.
3521 if (mHomeStack != null && mHomeStack.isVisible()
3522 && mDividerControllerLocked.isMinimizedDock()) {
3523 final int orientation = mHomeStack.getOrientation();
3524 if (orientation != SCREEN_ORIENTATION_UNSET) {
3525 return orientation;
3526 }
3527 }
3528 return SCREEN_ORIENTATION_UNSPECIFIED;
3529 }
3530
3531 final int orientation = super.getOrientation();
3532 if (orientation != SCREEN_ORIENTATION_UNSET
3533 && orientation != SCREEN_ORIENTATION_BEHIND) {
3534 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3535 "App is requesting an orientation, return " + orientation);
3536 return orientation;
3537 }
3538
3539 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003540 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003541 // The next app has not been requested to be visible, so we keep the current orientation
3542 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003543 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003544 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003545 }
3546
3547 /**
3548 * Window container class that contains all containers on this display that are not related to
3549 * Apps. E.g. status bar.
3550 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003551 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3552 /**
3553 * Compares two child window tokens returns -1 if the first is lesser than the second in
3554 * terms of z-order and 1 otherwise.
3555 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003556 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003557 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003558 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3559 token1.mOwnerCanManageAppTokens)
3560 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3561 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003562
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003563 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3564 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3565 return false;
3566 }
3567 final int req = w.mAttrs.screenOrientation;
3568 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3569 || req == SCREEN_ORIENTATION_UNSET) {
3570 return false;
3571 }
3572 return true;
3573 };
3574
Wale Ogunwale3a931692016-11-02 16:49:48 -07003575 private final String mName;
3576 NonAppWindowContainers(String name) {
3577 mName = name;
3578 }
3579
3580 void addChild(WindowToken token) {
3581 addChild(token, mWindowComparator);
3582 }
3583
3584 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003585 int getOrientation() {
3586 final WindowManagerPolicy policy = mService.mPolicy;
3587 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003588 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003589
3590 if (win != null) {
3591 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003592 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003593 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003594 if (mService.mKeyguardGoingAway) {
3595 // Keyguard can't affect the orientation if it is going away...
3596 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3597 return SCREEN_ORIENTATION_UNSET;
3598 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003599 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003600 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003601 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003602 }
3603
Andrii Kulian8ee72852017-03-10 10:36:45 -08003604 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003605
Jorim Jaggi75520d52017-08-24 17:23:19 +02003606 if (policy.isKeyguardShowingAndNotOccluded()
3607 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003608 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003609 }
3610
3611 return SCREEN_ORIENTATION_UNSET;
3612 }
3613
3614 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003615 String getName() {
3616 return mName;
3617 }
Robert Carr3b716242016-08-16 16:02:21 -07003618 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003619
3620 /**
3621 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3622 */
3623 @FunctionalInterface
3624 private interface Screenshoter<E> {
3625 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3626 boolean useIdentityTransform, int rotation);
3627 }
Craig Mautner59c00972012-07-30 12:10:24 -07003628}