blob: bd4aa97f528e6313a790017b311e270e8fe6d254 [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);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002176 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2177 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002178
Craig Mautnerdc548482014-02-05 13:35:24 -08002179 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002180 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002181 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2182 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002183 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002184 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002185
Craig Mautnerdc548482014-02-05 13:35:24 -08002186 pw.println();
2187 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002188 pw.println();
2189 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002190 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002191 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002192 pw.print(" Exiting #"); pw.print(i);
2193 pw.print(' '); pw.print(token);
2194 pw.println(':');
2195 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002196 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002197 }
Craig Mautner59c00972012-07-30 12:10:24 -07002198 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002199 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002200 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002201 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002202 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002203 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002204
2205 if (mInputMethodAnimLayerAdjustment != 0) {
2206 pw.println(subPrefix
2207 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2208 }
Craig Mautner59c00972012-07-30 12:10:24 -07002209 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002210
2211 @Override
2212 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002213 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002214 }
2215
2216 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002217 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002218 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002219
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002220 /** Checks if stack with provided id is visible on this display. */
2221 boolean isStackVisible(int stackId) {
2222 final TaskStack stack = getStackById(stackId);
2223 return (stack != null && stack.isVisible());
2224 }
2225
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002226 /**
2227 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2228 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002229 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002230 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002231 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002232 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002233
2234 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002235 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002236 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002237 */
Jorim Jaggife762342016-10-13 14:33:27 +02002238 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002239 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002240 }
2241
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002242 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002243 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002244 final int x = (int) xf;
2245 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002246 final WindowState touchedWin = getWindow(w -> {
2247 final int flags = w.mAttrs.flags;
2248 if (!w.isVisibleLw()) {
2249 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002250 }
2251 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002252 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002253 }
2254
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002255 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002256 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002257 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002258 }
2259
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002260 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002261
2262 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002263 return mTmpRegion.contains(x, y) || touchFlags == 0;
2264 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002265
2266 return touchedWin;
2267 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002268
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002269 boolean canAddToastWindowForUid(int uid) {
2270 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002271 // Also if the app is focused adding more than one toast at
2272 // a time for better backwards compatibility.
2273 final WindowState focusedWindowForUid = getWindow(w ->
2274 w.mOwnerUid == uid && w.isFocused());
2275 if (focusedWindowForUid != null) {
2276 return true;
2277 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002278 final WindowState win = getWindow(w ->
2279 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2280 && !w.mWindowRemovalAllowed);
2281 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002282 }
2283
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002284 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002285 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2286 return;
2287 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002288
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002289 // Used to communicate the old focus to the callback method.
2290 mTmpWindow = oldFocus;
2291
2292 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002293 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002294
2295 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002297
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002298 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002299
2300 if (mTmpWindow == null) {
2301 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2302 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002303 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002304 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002305 }
2306
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002307 /** Updates the layer assignment of windows on this display. */
2308 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002309 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002310 if (setLayoutNeeded) {
2311 setLayoutNeeded();
2312 }
2313 }
2314
Wale Ogunwale1666e312016-12-16 11:27:18 -08002315 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2316 // moving containers or resizing them. Need to investigate the best way to have it automatically
2317 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002318 void layoutAndAssignWindowLayersIfNeeded() {
2319 mService.mWindowsChanged = true;
2320 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002321
2322 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2323 false /*updateInputWindows*/)) {
2324 assignWindowLayers(false /* setLayoutNeeded */);
2325 }
2326
2327 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2328 mService.mWindowPlacerLocked.performSurfacePlacement();
2329 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2330 }
2331
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002332 /** Returns true if a leaked surface was destroyed */
2333 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002334 // Used to indicate that a surface was leaked.
2335 mTmpWindow = null;
2336 forAllWindows(w -> {
2337 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002338 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002339 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002340 }
2341 if (!mService.mSessions.contains(wsa.mSession)) {
2342 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002343 + w + " surface=" + wsa.mSurfaceController
2344 + " token=" + w.mToken
2345 + " pid=" + w.mSession.mPid
2346 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002347 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002348 mService.mForceRemoves.add(w);
2349 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002350 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002351 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002352 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002353 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002354 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002355 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002356 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002357 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002358 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002359
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002360 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002361 }
2362
2363 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002364 * Determine and return the window that should be the IME target.
2365 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2366 * @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 +00002367 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002368 WindowState computeImeTarget(boolean updateImeTarget) {
2369 if (mService.mInputMethodWindow == null) {
2370 // There isn't an IME so there shouldn't be a target...That was easy!
2371 if (updateImeTarget) {
2372 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2373 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2374 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2375 }
2376 return null;
2377 }
2378
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002379 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2380 // same display. Or even when the current IME/target are not on the same screen as the next
2381 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002382 mUpdateImeTarget = updateImeTarget;
2383 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002384
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002385
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002386 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2387 // to be on top of it, but it is not -really- where input will go. So look down below
2388 // for a real window to target...
2389 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2390 final AppWindowToken token = target.mAppToken;
2391 if (token != null) {
2392 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2393 if (betterTarget != null) {
2394 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002395 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002396 }
2397 }
2398
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002399 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2400 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002401
2402 // Now, a special case -- if the last target's window is in the process of exiting, and is
2403 // above the new target, keep on the last target to avoid flicker. Consider for example a
2404 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2405 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2406 // scrim.
2407 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002408 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2409 && (target == null
2410 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002411 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002412 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002413 }
2414
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002415 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2416 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002417
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002418 if (target == null) {
2419 if (updateImeTarget) {
2420 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2421 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2422 + Debug.getCallers(4) : ""));
2423 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2424 }
2425
2426 return null;
2427 }
2428
2429 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002430 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2431 if (token != null) {
2432
2433 // Now some fun for dealing with window animations that modify the Z order. We need
2434 // to look at all windows below the current target that are in this app, finding the
2435 // highest visible one in layering.
2436 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002437 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002438 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002439 }
2440
2441 if (highestTarget != null) {
2442 final AppTransition appTransition = mService.mAppTransition;
2443 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2444 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2445 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002446 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002447
2448 if (appTransition.isTransitionSet()) {
2449 // If we are currently setting up for an animation, hold everything until we
2450 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002451 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2452 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002453 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002454 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002455 // If the window we are currently targeting is involved with an animation,
2456 // and it is on top of the next target we will be over, then hold off on
2457 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002458 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2459 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002460 }
2461 }
2462 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002463
2464 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2465 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2466 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002467 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002468 }
2469
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002470 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002471 }
2472
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002473 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2474 if (target == mService.mInputMethodTarget
2475 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2476 && mInputMethodAnimLayerAdjustment == layerAdj) {
2477 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002478 }
2479
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002480 mService.mInputMethodTarget = target;
2481 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2482 setInputMethodAnimLayerAdjustment(layerAdj);
2483 assignWindowLayers(false /* setLayoutNeeded */);
2484 }
2485
2486 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2487 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2488 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2489 }
2490
2491 // Used to indicate we have reached the first window in the range we are interested in.
2492 mTmpWindow = null;
2493
2494 // TODO: Figure-out a more efficient way to do this.
2495 final WindowState candidate = getWindow(w -> {
2496 if (w == top) {
2497 // Reached the first window in the range we are interested in.
2498 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002499 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002500 if (mTmpWindow == null) {
2501 return false;
2502 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002503
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002504 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2505 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002506 }
2507 // If we reached the bottom of the range of windows we are considering,
2508 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002509 if (w == bottom) {
2510 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002511 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002512 return false;
2513 });
2514
2515 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002516 }
2517
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002518 void setLayoutNeeded() {
2519 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2520 mLayoutNeeded = true;
2521 }
2522
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002523 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002524 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2525 mLayoutNeeded = false;
2526 }
2527
2528 boolean isLayoutNeeded() {
2529 return mLayoutNeeded;
2530 }
2531
Wale Ogunwale02319a62016-09-26 15:21:22 -07002532 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2533 if (mTokenMap.isEmpty()) {
2534 return;
2535 }
2536 pw.println(" Display #" + mDisplayId);
2537 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2538 while (it.hasNext()) {
2539 final WindowToken token = it.next();
2540 pw.print(" ");
2541 pw.print(token);
2542 if (dumpAll) {
2543 pw.println(':');
2544 token.dump(pw, " ");
2545 } else {
2546 pw.println();
2547 }
2548 }
2549 }
2550
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002551 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002552 final int[] index = new int[1];
2553 forAllWindows(w -> {
2554 final WindowStateAnimator wAnim = w.mWinAnimator;
2555 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2556 index[0] = index[0] + 1;
2557 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002558 }
2559
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002560 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002561 forAllWindows(w -> {
2562 w.mWinAnimator.enableSurfaceTrace(fd);
2563 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002564 }
2565
2566 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002567 forAllWindows(w -> {
2568 w.mWinAnimator.disableSurfaceTrace();
2569 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002570 }
2571
Jorim Jaggife762342016-10-13 14:33:27 +02002572 /**
2573 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2574 */
2575 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2576 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002577 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002578 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2579 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002580 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002581 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2582 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002583 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002584 }
2585
Wale Ogunwale494009b82016-10-21 09:01:38 -07002586 boolean checkWaitingForWindows() {
2587
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002588 mHaveBootMsg = false;
2589 mHaveApp = false;
2590 mHaveWallpaper = false;
2591 mHaveKeyguard = true;
2592
2593 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002594 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2595 return true;
2596 }
2597 if (w.isDrawnLw()) {
2598 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002599 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002600 } else if (w.mAttrs.type == TYPE_APPLICATION
2601 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002602 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002603 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002604 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002605 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002606 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002607 }
2608 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002609 return false;
2610 });
2611
2612 if (visibleWindow != null) {
2613 // We have a visible window.
2614 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002615 }
2616
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002617 // if the wallpaper service is disabled on the device, we're never going to have
2618 // wallpaper, don't bother waiting for it
2619 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2620 com.android.internal.R.bool.config_enableWallpaperService)
2621 && !mService.mOnlyCore;
2622
Wale Ogunwale494009b82016-10-21 09:01:38 -07002623 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2624 "******** booted=" + mService.mSystemBooted
2625 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002626 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2627 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2628 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002629
2630 // If we are turning on the screen to show the boot message, don't do it until the boot
2631 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002633 return true;
2634 }
2635
2636 // If we are turning on the screen after the boot is completed normally, don't do so until
2637 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002638 if (mService.mSystemBooted
2639 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002640 return true;
2641 }
2642
2643 return false;
2644 }
2645
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002646 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002647 mTmpWindowAnimator = animator;
2648 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002649 }
2650
2651 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002652 resetAnimationBackgroundAnimator();
2653
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002654 // Used to indicate a detached wallpaper.
2655 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002656 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002657
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002658 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002659
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002660 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002661 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002662 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2663 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002664 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2665 }
2666 }
2667
2668 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002669 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002670 }
2671
Wale Ogunwale494009b82016-10-21 09:01:38 -07002672 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002673 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002674 if (imFocus == null) {
2675 return false;
2676 }
2677
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002678 if (DEBUG_INPUT_METHOD) {
2679 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2680 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2681 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002682 }
2683
Wale Ogunwale494009b82016-10-21 09:01:38 -07002684 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2685
2686 if (DEBUG_INPUT_METHOD) {
2687 Slog.i(TAG_WM, "IM target client: " + imeClient);
2688 if (imeClient != null) {
2689 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2690 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2691 }
2692 }
2693
2694 return imeClient != null && imeClient.asBinder() == client.asBinder();
2695 }
2696
2697 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002698 final WindowState win = getWindow(
2699 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2700 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002701 }
2702
2703 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002704 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002705 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002706 final int curValue = w.mSystemUiVisibility;
2707 final int diff = (curValue ^ visibility) & globalDiff;
2708 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002709 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002710 w.mSeq++;
2711 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002712 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002713 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2714 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002715 visibility, newValue, diff);
2716 }
2717 } catch (RemoteException e) {
2718 // so sorry
2719 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002720 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002721 }
2722
2723 void onWindowFreezeTimeout() {
2724 Slog.w(TAG_WM, "Window freeze timeout expired.");
2725 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002726
2727 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002728 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002729 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002730 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002731 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002732 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2733 - mService.mDisplayFreezeTime);
2734 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002735 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002736 mService.mWindowPlacerLocked.performSurfacePlacement();
2737 }
2738
2739 void waitForAllWindowsDrawn() {
2740 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002741 forAllWindows(w -> {
2742 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2743 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2744 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002745 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002746 w.mLastContentInsets.set(-1, -1, -1, -1);
2747 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002748 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002749 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002750 }
2751
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002752 // TODO: Super crazy long method that should be broken down...
2753 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2754
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002755 final int dw = mDisplayInfo.logicalWidth;
2756 final int dh = mDisplayInfo.logicalHeight;
2757 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2758
2759 mTmpUpdateAllDrawn.clear();
2760
2761 int repeats = 0;
2762 do {
2763 repeats++;
2764 if (repeats > 6) {
2765 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2766 clearLayoutNeeded();
2767 break;
2768 }
2769
2770 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2771 pendingLayoutChanges);
2772
Andrii Kulian839def92016-11-02 10:58:58 -07002773 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2774 // the wallpaper window jumping across displays.
2775 // Remove check for default display when there will be support for multiple wallpaper
2776 // targets (on different displays).
2777 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002778 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002779 }
2780
2781 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2782 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2783 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2784 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002785 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002786 }
2787 }
2788
2789 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2790 setLayoutNeeded();
2791 }
2792
2793 // FIRST LOOP: Perform a layout, if needed.
2794 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2795 performLayout(repeats == 1, false /* updateInputWindows */);
2796 } else {
2797 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2798 }
2799
2800 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2801 pendingLayoutChanges = 0;
2802
2803 if (isDefaultDisplay) {
2804 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002805 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002806 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2807 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2808 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2809 }
2810 } while (pendingLayoutChanges != 0);
2811
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002812 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002813 resetDimming();
2814
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002815 mTmpRecoveringMemory = recoveringMemory;
2816 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002817
2818 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002819 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2820 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2821 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002822 true /* inTraversal, must call performTraversalInTrans... below */);
2823
2824 stopDimmingIfNeeded();
2825
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002826 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2827 if (wallpaperVisible != mLastWallpaperVisible) {
2828 mLastWallpaperVisible = wallpaperVisible;
2829 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2830 }
2831
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002832 while (!mTmpUpdateAllDrawn.isEmpty()) {
2833 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2834 // See if any windows have been drawn, so they (and others associated with them)
2835 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002836 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002837 }
2838
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002839 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002840 }
2841
2842 void performLayout(boolean initial, boolean updateInputWindows) {
2843 if (!isLayoutNeeded()) {
2844 return;
2845 }
2846 clearLayoutNeeded();
2847
2848 final int dw = mDisplayInfo.logicalWidth;
2849 final int dh = mDisplayInfo.logicalHeight;
2850
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002851 if (DEBUG_LAYOUT) {
2852 Slog.v(TAG, "-------------------------------------");
2853 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2854 }
2855
Andrii Kulian8ee72852017-03-10 10:36:45 -08002856 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002857 getConfiguration().uiMode);
2858 if (isDefaultDisplay) {
2859 // Not needed on non-default displays.
2860 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2861 mService.mScreenRect.set(0, 0, dw, dh);
2862 }
2863
2864 mService.mPolicy.getContentRectLw(mContentRect);
2865
2866 int seq = mService.mLayoutSeq + 1;
2867 if (seq < 0) seq = 0;
2868 mService.mLayoutSeq = seq;
2869
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002870 // Used to indicate that we have processed the dream window and all additional windows are
2871 // behind it.
2872 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002873 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002874
2875 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002876 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002877
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002878 // Used to indicate that we have processed the dream window and all additional attached
2879 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002880 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002881 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002882
2883 // Now perform layout of attached windows, which usually depend on the position of the
2884 // window they are attached to. XXX does not deal with windows that are attached to windows
2885 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002886 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002887
2888 // Window frames may have changed. Tell the input dispatcher about it.
2889 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2890 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2891 if (updateInputWindows) {
2892 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2893 }
2894
2895 mService.mPolicy.finishLayoutLw();
2896 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2897 }
2898
2899 /**
2900 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2901 * In portrait mode, it grabs the full screenshot.
2902 *
2903 * @param width the width of the target bitmap
2904 * @param height the height of the target bitmap
2905 * @param includeFullDisplay true if the screen should not be cropped before capture
2906 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2907 * @param config of the output bitmap
2908 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002909 * @param includeDecor whether to include window decors, like the status or navigation bar
2910 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002911 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002912 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002913 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002914 boolean wallpaperOnly, boolean includeDecor) {
2915 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2916 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002917 if (bitmap == null) {
2918 return null;
2919 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002920
2921 if (DEBUG_SCREENSHOT) {
2922 // TEST IF IT's ALL BLACK
2923 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2924 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2925 bitmap.getHeight());
2926 boolean allBlack = true;
2927 final int firstColor = buffer[0];
2928 for (int i = 0; i < buffer.length; i++) {
2929 if (buffer[i] != firstColor) {
2930 allBlack = false;
2931 break;
2932 }
2933 }
2934 if (allBlack) {
2935 final WindowState appWin = mScreenshotApplicationState.appWin;
2936 final int maxLayer = mScreenshotApplicationState.maxLayer;
2937 final int minLayer = mScreenshotApplicationState.minLayer;
2938 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2939 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2940 (appWin != null ?
2941 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2942 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2943 }
2944 }
2945
2946 // Create a copy of the screenshot that is immutable and backed in ashmem.
2947 // This greatly reduces the overhead of passing the bitmap between processes.
2948 Bitmap ret = bitmap.createAshmemBitmap(config);
2949 bitmap.recycle();
2950 return ret;
2951 }
2952
2953 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2954 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2955 boolean includeDecor) {
2956 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2957 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2958 }
2959
2960 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2961 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2962 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002963 int dw = mDisplayInfo.logicalWidth;
2964 int dh = mDisplayInfo.logicalHeight;
2965 if (dw == 0 || dh == 0) {
2966 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2967 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2968 return null;
2969 }
2970
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002971 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002973 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002974 final Rect frame = new Rect();
2975 final Rect stackBounds = new Rect();
2976
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002977 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002978 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002979 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002980 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002981 if (!mService.mPolicy.isScreenOn()) {
2982 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
2983 + " was off.");
2984 return null;
2985 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002986 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002987 mScreenshotApplicationState.appWin = null;
2988 forAllWindows(w -> {
2989 if (!w.mHasSurface) {
2990 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002991 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002992 if (w.mLayer >= aboveAppLayer) {
2993 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002994 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002995 if (wallpaperOnly && !w.mIsWallpaper) {
2996 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002997 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002998 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02002999 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003000 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003001 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3002 // then the target window state is this one.
3003 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003004 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003005 }
3006
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003007 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003008 // We have not ran across the target window yet, so it is probably behind
3009 // the wallpaper. This can happen when the keyguard is up and all windows
3010 // are moved behind the wallpaper. We don't want to include the wallpaper
3011 // layer in the screenshot as it will cover-up the layer of the target
3012 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003013 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003014 }
3015 // Fall through. The target window is in front of the wallpaper. For this
3016 // case we want to include the wallpaper layer in the screenshot because
3017 // the target window might have some transparent areas.
3018 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003019 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003020 // This app window is of no interest if it is not associated with the
3021 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003022 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003023 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003024 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003025 }
3026
3027 // Include this window.
3028
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003029 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003030 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003031 if (mScreenshotApplicationState.maxLayer < layer) {
3032 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003033 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003034 if (mScreenshotApplicationState.minLayer > layer) {
3035 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003036 }
3037
3038 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003039 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3040 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003041 final Task task = w.getTask();
3042 if (task != null) {
3043 task.getBounds(frame);
3044 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003045
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003046 // No task bounds? Too bad! Ain't no screenshot then.
3047 return true;
3048 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003049 } else {
3050 final Rect wf = w.mFrame;
3051 final Rect cr = w.mContentInsets;
3052 int left = wf.left + cr.left;
3053 int top = wf.top + cr.top;
3054 int right = wf.right - cr.right;
3055 int bottom = wf.bottom - cr.bottom;
3056 frame.union(left, top, right, bottom);
3057 w.getVisibleBounds(stackBounds);
3058 if (!Rect.intersects(frame, stackBounds)) {
3059 // Set frame empty if there's no intersection.
3060 frame.setEmpty();
3061 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003062 }
3063 }
3064
3065 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003066 (w.mAppToken != null && w.mAppToken.token == appToken)
3067 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01003068 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003069 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003070 }
3071
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003072 if (w.isObscuringDisplay()){
3073 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003074 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003075 return false;
3076 }, true /* traverseTopToBottom */);
3077
3078 final WindowState appWin = mScreenshotApplicationState.appWin;
3079 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3080 final int maxLayer = mScreenshotApplicationState.maxLayer;
3081 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003082
3083 if (appToken != null && appWin == null) {
3084 // Can't find a window to snapshot.
3085 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3086 "Screenshot: Couldn't find a surface matching " + appToken);
3087 return null;
3088 }
3089
3090 if (!screenshotReady) {
3091 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3092 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3093 appWin.mWinAnimator.mDrawState)));
3094 return null;
3095 }
3096
3097 // Screenshot is ready to be taken. Everything from here below will continue
3098 // through the bottom of the loop and return a value. We only stay in the loop
3099 // because we don't want to release the mWindowMap lock until the screenshot is
3100 // taken.
3101
3102 if (maxLayer == 0) {
3103 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3104 + ": returning null maxLayer=" + maxLayer);
3105 return null;
3106 }
3107
Jorim Jaggi02886a82016-12-06 09:10:06 -08003108 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003109 // Constrain frame to the screen size.
3110 if (!frame.intersect(0, 0, dw, dh)) {
3111 frame.setEmpty();
3112 }
3113 } else {
3114 // Caller just wants entire display.
3115 frame.set(0, 0, dw, dh);
3116 }
3117 if (frame.isEmpty()) {
3118 return null;
3119 }
3120
3121 if (width < 0) {
3122 width = (int) (frame.width() * frameScale);
3123 }
3124 if (height < 0) {
3125 height = (int) (frame.height() * frameScale);
3126 }
3127
3128 // Tell surface flinger what part of the image to crop. Take the top
3129 // right part of the application, and crop the larger dimension to fit.
3130 Rect crop = new Rect(frame);
3131 if (width / (float) frame.width() < height / (float) frame.height()) {
3132 int cropWidth = (int)((float)width / (float)height * frame.height());
3133 crop.right = crop.left + cropWidth;
3134 } else {
3135 int cropHeight = (int)((float)height / (float)width * frame.width());
3136 crop.bottom = crop.top + cropHeight;
3137 }
3138
3139 // The screenshot API does not apply the current screen rotation.
3140 int rot = mDisplay.getRotation();
3141
3142 if (rot == ROTATION_90 || rot == ROTATION_270) {
3143 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3144 }
3145
3146 // Surfaceflinger is not aware of orientation, so convert our logical
3147 // crop to surfaceflinger's portrait orientation.
3148 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3149
3150 if (DEBUG_SCREENSHOT) {
3151 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3152 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003153 forAllWindows(w -> {
3154 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3155 Slog.i(TAG_WM, w + ": " + w.mLayer
3156 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003157 + " surfaceLayer=" + ((controller == null)
3158 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003159 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003160 }
3161
3162 final ScreenRotationAnimation screenRotationAnimation =
3163 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3164 final boolean inRotation = screenRotationAnimation != null &&
3165 screenRotationAnimation.isAnimating();
3166 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3167 "Taking screenshot while rotating");
3168
3169 // We force pending transactions to flush before taking
3170 // the screenshot by pushing an empty synchronous transaction.
3171 SurfaceControl.openTransaction();
3172 SurfaceControl.closeTransactionSync();
3173
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003174 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003175 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003176 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003177 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3178 + ") to layer " + maxLayer);
3179 return null;
3180 }
3181 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003182 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003183 }
3184
3185 // TODO: Can this use createRotationMatrix()?
3186 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3187 if (rot == Surface.ROTATION_90) {
3188 final int tmp = crop.top;
3189 crop.top = dw - crop.right;
3190 crop.right = crop.bottom;
3191 crop.bottom = dw - crop.left;
3192 crop.left = tmp;
3193 } else if (rot == Surface.ROTATION_180) {
3194 int tmp = crop.top;
3195 crop.top = dh - crop.bottom;
3196 crop.bottom = dh - tmp;
3197 tmp = crop.right;
3198 crop.right = dw - crop.left;
3199 crop.left = dw - tmp;
3200 } else if (rot == Surface.ROTATION_270) {
3201 final int tmp = crop.top;
3202 crop.top = crop.left;
3203 crop.left = dh - crop.bottom;
3204 crop.bottom = crop.right;
3205 crop.right = dh - tmp;
3206 }
3207 }
3208
3209 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003210 // Used to indicate the layout is needed.
3211 mTmpWindow = null;
3212
3213 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003214 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003215 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003216 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003217 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003218 w.setDisplayLayoutNeeded();
3219 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003220 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003221
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003222 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003223 mService.mWindowPlacerLocked.performSurfacePlacement();
3224 }
3225 }
3226
Wale Ogunwale1666e312016-12-16 11:27:18 -08003227 void setExitingTokensHasVisible(boolean hasVisible) {
3228 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3229 mExitingTokens.get(i).hasVisible = hasVisible;
3230 }
3231
3232 // Initialize state of exiting applications.
3233 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3234 }
3235
3236 void removeExistingTokensIfPossible() {
3237 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3238 final WindowToken token = mExitingTokens.get(i);
3239 if (!token.hasVisible) {
3240 mExitingTokens.remove(i);
3241 }
3242 }
3243
3244 // Time to remove any exiting applications?
3245 mTaskStackContainers.removeExistingAppTokensIfPossible();
3246 }
3247
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003248 @Override
3249 void onDescendantOverrideConfigurationChanged() {
3250 setLayoutNeeded();
3251 mService.requestTraversal();
3252 }
3253
David Stevens9440dc82017-03-16 19:00:20 -07003254 boolean okToDisplay() {
3255 if (mDisplayId == DEFAULT_DISPLAY) {
3256 return !mService.mDisplayFrozen
3257 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3258 }
3259 return mDisplayInfo.state == Display.STATE_ON;
3260 }
3261
3262 boolean okToAnimate() {
3263 return okToDisplay() &&
3264 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3265 }
3266
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003267 static final class TaskForResizePointSearchResult {
3268 boolean searchDone;
3269 Task taskForResize;
3270
3271 void reset() {
3272 searchDone = false;
3273 taskForResize = null;
3274 }
3275 }
Robert Carr3b716242016-08-16 16:02:21 -07003276
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003277 private static final class ApplySurfaceChangesTransactionState {
3278 boolean displayHasContent;
3279 boolean obscured;
3280 boolean syswin;
3281 boolean focusDisplayed;
3282 float preferredRefreshRate;
3283 int preferredModeId;
3284
3285 void reset() {
3286 displayHasContent = false;
3287 obscured = false;
3288 syswin = false;
3289 focusDisplayed = false;
3290 preferredRefreshRate = 0;
3291 preferredModeId = 0;
3292 }
3293 }
3294
3295 private static final class ScreenshotApplicationState {
3296 WindowState appWin;
3297 int maxLayer;
3298 int minLayer;
3299 boolean screenshotReady;
3300
3301 void reset(boolean screenshotReady) {
3302 appWin = null;
3303 maxLayer = 0;
3304 minLayer = 0;
3305 this.screenshotReady = screenshotReady;
3306 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3307 }
3308 }
3309
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003310 /**
3311 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3312 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3313 * homogeneous children type which is currently required by sub-classes of
3314 * {@link WindowContainer} class.
3315 */
3316 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3317
3318 int size() {
3319 return mChildren.size();
3320 }
3321
3322 E get(int index) {
3323 return mChildren.get(index);
3324 }
3325
3326 @Override
3327 boolean fillsParent() {
3328 return true;
3329 }
3330
3331 @Override
3332 boolean isVisible() {
3333 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003334 }
3335 }
3336
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003337 /**
3338 * Window container class that contains all containers on this display relating to Apps.
3339 * I.e Activities.
3340 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003341 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003342
Andrii Kulian839def92016-11-02 10:58:58 -07003343 /**
3344 * Adds the stack to this container.
3345 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3346 */
3347 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003348 if (stack.mStackId == HOME_STACK_ID) {
3349 if (mHomeStack != null) {
3350 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3351 }
3352 mHomeStack = stack;
3353 }
3354 addChild(stack, onTop);
3355 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003356 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003357
Andrii Kulian839def92016-11-02 10:58:58 -07003358 /** Removes the stack from its container and prepare for changing the parent. */
3359 void removeStackFromDisplay(TaskStack stack) {
3360 removeChild(stack);
3361 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003362 }
3363
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003364 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003365 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3366 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003367 addChild(stack, addIndex);
3368 setLayoutNeeded();
3369 }
3370
Bryce Lee00d586d2017-07-28 20:48:43 -07003371
3372 @Override
3373 boolean isOnTop() {
3374 // Considered always on top
3375 return true;
3376 }
3377
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003378 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003379 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003380 if (child.getWindowConfiguration().isAlwaysOnTop()
3381 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003382 // This stack is always-on-top, override the default behavior.
3383 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3384
3385 // Moving to its current position, as we must call super but we don't want to
3386 // perform any meaningful action.
3387 final int currentPosition = mChildren.indexOf(child);
3388 super.positionChildAt(currentPosition, child, false /* includingParents */);
3389 return;
3390 }
3391
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003392 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3393 super.positionChildAt(targetPosition, child, includingParents);
3394
3395 setLayoutNeeded();
3396 }
3397
3398 /**
3399 * When stack is added or repositioned, find a proper position for it.
3400 * This will make sure that pinned stack always stays on top.
3401 * @param requestedPosition Position requested by caller.
3402 * @param stack Stack to be added or positioned.
3403 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3404 * @return The proper position for the stack.
3405 */
3406 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3407 final int topChildPosition = mChildren.size() - 1;
3408 boolean toTop = requestedPosition == POSITION_TOP;
3409 toTop |= adding ? requestedPosition >= topChildPosition + 1
3410 : requestedPosition >= topChildPosition;
3411 int targetPosition = requestedPosition;
3412
Bryce Lee48f4b572017-04-10 10:54:15 -07003413 if (toTop && stack.mStackId != PINNED_STACK_ID
3414 && getStackById(PINNED_STACK_ID) != null) {
3415 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003416 TaskStack topStack = mChildren.get(topChildPosition);
3417 if (topStack.mStackId != PINNED_STACK_ID) {
3418 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3419 }
3420
3421 // So, stack is moved just below the pinned stack.
3422 // When we're adding a new stack the target is the current pinned stack position.
3423 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003424 // stack, because WindowContainer#positionAt() first removes element and then adds
3425 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003426 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3427 }
3428
3429 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003430 }
3431
3432 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003433 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3434 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003435 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003436 if (super.forAllWindows(callback, traverseTopToBottom)) {
3437 return true;
3438 }
3439 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3440 return true;
3441 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003442 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003443 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3444 return true;
3445 }
3446 if (super.forAllWindows(callback, traverseTopToBottom)) {
3447 return true;
3448 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003449 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003450 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003451 }
3452
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003453 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003454 boolean traverseTopToBottom) {
3455 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3456 // app tokens.
3457 // TODO: Investigate if we need to continue to do this or if we can just process them
3458 // in-order.
3459 if (traverseTopToBottom) {
3460 for (int i = mChildren.size() - 1; i >= 0; --i) {
3461 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3462 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003463 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3464 traverseTopToBottom)) {
3465 return true;
3466 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003467 }
3468 }
3469 } else {
3470 final int count = mChildren.size();
3471 for (int i = 0; i < count; ++i) {
3472 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3473 final int appTokensCount = appTokens.size();
3474 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003475 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3476 traverseTopToBottom)) {
3477 return true;
3478 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003479 }
3480 }
3481 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003482 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003483 }
3484
Wale Ogunwale1666e312016-12-16 11:27:18 -08003485 void setExitingTokensHasVisible(boolean hasVisible) {
3486 for (int i = mChildren.size() - 1; i >= 0; --i) {
3487 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3488 for (int j = appTokens.size() - 1; j >= 0; --j) {
3489 appTokens.get(j).hasVisible = hasVisible;
3490 }
3491 }
3492 }
3493
3494 void removeExistingAppTokensIfPossible() {
3495 for (int i = mChildren.size() - 1; i >= 0; --i) {
3496 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3497 for (int j = appTokens.size() - 1; j >= 0; --j) {
3498 final AppWindowToken token = appTokens.get(j);
3499 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3500 && (!token.mIsExiting || token.isEmpty())) {
3501 // Make sure there is no animation running on this token, so any windows
3502 // associated with it will be removed as soon as their animations are
3503 // complete.
3504 token.mAppAnimator.clearAnimation();
3505 token.mAppAnimator.animating = false;
3506 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3507 "performLayout: App token exiting now removed" + token);
3508 token.removeIfPossible();
3509 }
3510 }
3511 }
3512 }
3513
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003514 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003515 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003516 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003517 // Apps and their containers are not allowed to specify an orientation while the
3518 // docked or freeform stack is visible...except for the home stack/task if the
3519 // docked stack is minimized and it actually set something.
3520 if (mHomeStack != null && mHomeStack.isVisible()
3521 && mDividerControllerLocked.isMinimizedDock()) {
3522 final int orientation = mHomeStack.getOrientation();
3523 if (orientation != SCREEN_ORIENTATION_UNSET) {
3524 return orientation;
3525 }
3526 }
3527 return SCREEN_ORIENTATION_UNSPECIFIED;
3528 }
3529
3530 final int orientation = super.getOrientation();
3531 if (orientation != SCREEN_ORIENTATION_UNSET
3532 && orientation != SCREEN_ORIENTATION_BEHIND) {
3533 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3534 "App is requesting an orientation, return " + orientation);
3535 return orientation;
3536 }
3537
3538 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003539 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003540 // The next app has not been requested to be visible, so we keep the current orientation
3541 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003542 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003543 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003544 }
3545
3546 /**
3547 * Window container class that contains all containers on this display that are not related to
3548 * Apps. E.g. status bar.
3549 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003550 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3551 /**
3552 * Compares two child window tokens returns -1 if the first is lesser than the second in
3553 * terms of z-order and 1 otherwise.
3554 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003555 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003556 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003557 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3558 token1.mOwnerCanManageAppTokens)
3559 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3560 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003561
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003562 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3563 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3564 return false;
3565 }
3566 final int req = w.mAttrs.screenOrientation;
3567 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3568 || req == SCREEN_ORIENTATION_UNSET) {
3569 return false;
3570 }
3571 return true;
3572 };
3573
Wale Ogunwale3a931692016-11-02 16:49:48 -07003574 private final String mName;
3575 NonAppWindowContainers(String name) {
3576 mName = name;
3577 }
3578
3579 void addChild(WindowToken token) {
3580 addChild(token, mWindowComparator);
3581 }
3582
3583 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003584 int getOrientation() {
3585 final WindowManagerPolicy policy = mService.mPolicy;
3586 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003587 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003588
3589 if (win != null) {
3590 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003591 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003592 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003593 if (mService.mKeyguardGoingAway) {
3594 // Keyguard can't affect the orientation if it is going away...
3595 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3596 return SCREEN_ORIENTATION_UNSET;
3597 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003598 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003599 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003600 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003601 }
3602
Andrii Kulian8ee72852017-03-10 10:36:45 -08003603 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003604
Jorim Jaggi75520d52017-08-24 17:23:19 +02003605 if (policy.isKeyguardShowingAndNotOccluded()
3606 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003607 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003608 }
3609
3610 return SCREEN_ORIENTATION_UNSET;
3611 }
3612
3613 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003614 String getName() {
3615 return mName;
3616 }
Robert Carr3b716242016-08-16 16:02:21 -07003617 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003618
3619 /**
3620 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3621 */
3622 @FunctionalInterface
3623 private interface Screenshoter<E> {
3624 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3625 boolean useIdentityTransform, int rotation);
3626 }
Craig Mautner59c00972012-07-30 12:10:24 -07003627}