blob: 5bc4a6b23392b3576ca3284a28a7a6cfe50b6e02 [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Wale Ogunwale51362492016-09-08 17:49:17 -070020import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.HOME_STACK_ID;
22import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
25import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070026import static android.view.Display.DEFAULT_DISPLAY;
27import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070028import static android.view.Surface.ROTATION_0;
29import static android.view.Surface.ROTATION_180;
30import static android.view.Surface.ROTATION_270;
31import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070032import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070033import static android.view.WindowManager.DOCKED_BOTTOM;
34import static android.view.WindowManager.DOCKED_INVALID;
35import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080036import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
37import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
38import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070039import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070040import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070041import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
42import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070043import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070045import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070046import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070047import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070049import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
50import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070052import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
53import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070054import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070055import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070056import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
58import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070059import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080060import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070061import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070062import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070063import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
64import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070076import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070077import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070078import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070079import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080080import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070081import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070082import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
83import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070084import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070085import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070086import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
87import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
89import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
90import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070091import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070092import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070093import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070094import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070095import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070096import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -070097import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070098import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070099import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700100
Andrii Kulian3a507b52016-09-19 18:14:12 -0700101import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700102import android.app.ActivityManager.StackId;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700103import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700104import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700105import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100106import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700107import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800108import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700109import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700110import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100111import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700112import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700113import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700114import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700115import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700116import android.os.RemoteException;
117import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700118import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800119import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700120import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700121import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700122import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700123import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700124import android.view.Surface;
125import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700126import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700127
Bryce Lee48f4b572017-04-10 10:54:15 -0700128import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800129import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700130import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700131
Robert Carr3b716242016-08-16 16:02:21 -0700132import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700133import java.io.PrintWriter;
134import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700135import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700136import java.util.HashMap;
137import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700138import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700139import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800140import java.util.function.Consumer;
141import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700142
Craig Mautner59c00972012-07-30 12:10:24 -0700143/**
144 * Utility class for keeping track of the WindowStates and other pertinent contents of a
145 * particular Display.
146 *
147 * IMPORTANT: No method from this class should ever be used without holding
148 * WindowManagerService.mWindowMap.
149 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700150class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700151 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700152
153 /** Unique identifier of this stack. */
154 private final int mDisplayId;
155
Wale Ogunwale3a931692016-11-02 16:49:48 -0700156 /** The containers below are the only child containers the display can have. */
157 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700158 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700159 // Contains all non-app window containers that should be displayed above the app containers
160 // (e.g. Status bar)
161 private final NonAppWindowContainers mAboveAppWindowsContainers =
162 new NonAppWindowContainers("mAboveAppWindowsContainers");
163 // Contains all non-app window containers that should be displayed below the app containers
164 // (e.g. Wallpaper).
165 private final NonAppWindowContainers mBelowAppWindowsContainers =
166 new NonAppWindowContainers("mBelowAppWindowsContainers");
167 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
168 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
169 // window containers together and move them in-sync if/when needed.
170 private final NonAppWindowContainers mImeWindowsContainers =
171 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700172
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000173 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800174 private WindowState mTmpWindow2;
175 private WindowAnimator mTmpWindowAnimator;
176 private boolean mTmpRecoveringMemory;
177 private boolean mUpdateImeTarget;
178 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700179 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700180
Wale Ogunwale02319a62016-09-26 15:21:22 -0700181 // Mapping from a token IBinder to a WindowToken object on this display.
182 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
183
Andrii Kuliancd097992017-03-23 18:31:59 -0700184 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700185 int mInitialDisplayWidth = 0;
186 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700187 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700188
189 /**
190 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
191 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
192 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
193 */
Craig Mautner59c00972012-07-30 12:10:24 -0700194 int mBaseDisplayWidth = 0;
195 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700196 /**
197 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
198 * but can be set from Settings or via shell command "adb shell wm density".
199 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
200 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700201 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700202 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700203 private final DisplayInfo mDisplayInfo = new DisplayInfo();
204 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700205 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700206 /**
207 * For default display it contains real metrics, empty for others.
208 * @see WindowManagerService#createWatermarkInTransaction()
209 */
210 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
211 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
212 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700213
Andrii Kulian06d07d62017-03-14 11:11:47 -0700214 /**
215 * Compat metrics computed based on {@link #mDisplayMetrics}.
216 * @see #updateDisplayAndOrientation(int)
217 */
218 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
219
220 /** The desired scaling factor for compatible apps. */
221 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700222
Andrii Kulian8ee72852017-03-10 10:36:45 -0800223 /**
224 * Current rotation of the display.
225 * Constants as per {@link android.view.Surface.Rotation}.
226 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700227 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800228 */
229 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700230
Andrii Kulian8ee72852017-03-10 10:36:45 -0800231 /**
232 * Last applied orientation of the display.
233 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
234 *
235 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
236 */
237 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700238
Andrii Kulian8ee72852017-03-10 10:36:45 -0800239 /**
240 * Flag indicating that the application is receiving an orientation that has different metrics
241 * than it expected. E.g. Portrait instead of Landscape.
242 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700243 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800244 */
245 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700246
Andrii Kulian8ee72852017-03-10 10:36:45 -0800247 /**
248 * Orientation forced by some window. If there is no visible window that specifies orientation
249 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
250 *
251 * @see NonAppWindowContainers#getOrientation()
252 */
253 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700254
Andrii Kulian8ee72852017-03-10 10:36:45 -0800255 /**
256 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
257 * occluded.
258 *
259 * @see NonAppWindowContainers#getOrientation()
260 */
261 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
262
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700263 /**
264 * Keep track of wallpaper visibility to notify changes.
265 */
266 private boolean mLastWallpaperVisible = false;
267
Andrii Kulian06d07d62017-03-14 11:11:47 -0700268 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700269 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700270
Craig Mautner39834192012-09-02 07:47:24 -0700271 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700272 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700273 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700274 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800275 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700276
Craig Mautnerdc548482014-02-05 13:35:24 -0800277 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700278 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800279
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800280 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
281 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700282 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700283
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700284 /** Detect user tapping outside of current focused task bounds .*/
285 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700286
Craig Mautner6601b7b2013-04-29 10:29:11 -0700287 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700288 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700289
Craig Mautner6601b7b2013-04-29 10:29:11 -0700290 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800291 private final Rect mTmpRect = new Rect();
292 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700293 private final RectF mTmpRectF = new RectF();
294 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800295 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700296
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800297 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700298
Craig Mautner95da1082014-02-24 17:54:35 -0800299 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700300 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800301
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700302 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700303 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700304
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800305 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700306
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800307 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
308
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000309 private boolean mHaveBootMsg = false;
310 private boolean mHaveApp = false;
311 private boolean mHaveWallpaper = false;
312 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700313
314 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
315
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700316 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
317 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000318 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
319 new ApplySurfaceChangesTransactionState();
320 private final ScreenshotApplicationState mScreenshotApplicationState =
321 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700322
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700323 // True if this display is in the process of being removed. Used to determine if the removal of
324 // the display's direct children should be allowed.
325 private boolean mRemovingDisplay = false;
326
Bryce Leed1871262017-06-12 14:12:29 -0700327 // {@code false} if this display is in the processing of being created.
328 private boolean mDisplayReady = false;
329
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700330 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800331 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700332 int mInputMethodAnimLayerAdjustment;
333
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800334 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
335 WindowStateAnimator winAnimator = w.mWinAnimator;
336 if (winAnimator.hasSurface()) {
337 final boolean wasAnimating = winAnimator.mWasAnimating;
338 final boolean nowAnimating = winAnimator.stepAnimationLocked(
339 mTmpWindowAnimator.mCurrentTime);
340 winAnimator.mWasAnimating = nowAnimating;
341 mTmpWindowAnimator.orAnimating(nowAnimating);
342
343 if (DEBUG_WALLPAPER) Slog.v(TAG,
344 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
345
346 if (wasAnimating && !winAnimator.mAnimating
347 && mWallpaperController.isWallpaperTarget(w)) {
348 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
349 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
350 if (DEBUG_LAYOUT_REPEATS) {
351 mService.mWindowPlacerLocked.debugLayoutRepeats(
352 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
353 }
354 }
355 }
356
357 final AppWindowToken atoken = w.mAppToken;
358 if (winAnimator.mDrawState == READY_TO_SHOW) {
359 if (atoken == null || atoken.allDrawn) {
360 if (w.performShowLocked()) {
361 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
362 if (DEBUG_LAYOUT_REPEATS) {
363 mService.mWindowPlacerLocked.debugLayoutRepeats(
364 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
365 }
366 }
367 }
368 }
369 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
370 if (appAnimator != null && appAnimator.thumbnail != null) {
371 if (appAnimator.thumbnailTransactionSeq
372 != mTmpWindowAnimator.mAnimTransactionSequence) {
373 appAnimator.thumbnailTransactionSeq =
374 mTmpWindowAnimator.mAnimTransactionSequence;
375 appAnimator.thumbnailLayer = 0;
376 }
377 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
378 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
379 }
380 }
381 };
382
383 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
384 final WindowStateAnimator winAnimator = w.mWinAnimator;
385 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
386 return;
387 }
388
389 final int flags = w.mAttrs.flags;
390
391 // If this window is animating, make a note that we have an animating window and take
392 // care of a request to run a detached wallpaper animation.
393 if (winAnimator.mAnimating) {
394 if (winAnimator.mAnimation != null) {
395 if ((flags & FLAG_SHOW_WALLPAPER) != 0
396 && winAnimator.mAnimation.getDetachWallpaper()) {
397 mTmpWindow = w;
398 }
399 final int color = winAnimator.mAnimation.getBackgroundColor();
400 if (color != 0) {
401 final TaskStack stack = w.getStack();
402 if (stack != null) {
403 stack.setAnimationBackground(winAnimator, color);
404 }
405 }
406 }
407 mTmpWindowAnimator.setAnimating(true);
408 }
409
410 // If this window's app token is running a detached wallpaper animation, make a note so
411 // we can ensure the wallpaper is displayed behind it.
412 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
413 if (appAnimator != null && appAnimator.animation != null
414 && appAnimator.animating) {
415 if ((flags & FLAG_SHOW_WALLPAPER) != 0
416 && appAnimator.animation.getDetachWallpaper()) {
417 mTmpWindow = w;
418 }
419
420 final int color = appAnimator.animation.getBackgroundColor();
421 if (color != 0) {
422 final TaskStack stack = w.getStack();
423 if (stack != null) {
424 stack.setAnimationBackground(winAnimator, color);
425 }
426 }
427 }
428 };
429
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800430 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
431 final int lostFocusUid = mTmpWindow.mOwnerUid;
432 final Handler handler = mService.mH;
433 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
434 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
435 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
436 w.mAttrs.hideTimeoutMilliseconds);
437 }
438 }
439 };
440
441 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
442 final AppWindowToken focusedApp = mService.mFocusedApp;
443 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
444 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
445
446 if (!w.canReceiveKeys()) {
447 return false;
448 }
449
450 final AppWindowToken wtoken = w.mAppToken;
451
452 // If this window's application has been removed, just skip it.
453 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
454 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
455 + (wtoken.removed ? "removed" : "sendingToBottom"));
456 return false;
457 }
458
459 if (focusedApp == null) {
460 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
461 + " using new focus @ " + w);
462 mTmpWindow = w;
463 return true;
464 }
465
466 if (!focusedApp.windowsAreFocusable()) {
467 // Current focused app windows aren't focusable...
468 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
469 + " focusable using new focus @ " + w);
470 mTmpWindow = w;
471 return true;
472 }
473
474 // Descend through all of the app tokens and find the first that either matches
475 // win.mAppToken (return win) or mFocusedApp (return null).
476 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
477 if (focusedApp.compareTo(wtoken) > 0) {
478 // App stack below focused app stack. No focus for you!!!
479 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
480 "findFocusedWindow: Reached focused app=" + focusedApp);
481 mTmpWindow = null;
482 return true;
483 }
484 }
485
486 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
487 mTmpWindow = w;
488 return true;
489 };
490
491 private final Consumer<WindowState> mPrepareWindowSurfaces =
492 w -> w.mWinAnimator.prepareSurfaceLocked(true);
493
494 private final Consumer<WindowState> mPerformLayout = w -> {
495 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
496 // wasting time and funky changes while a window is animating away.
497 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
498 || w.isGoneForLayoutLw();
499
500 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
501 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
502 + " mLayoutAttached=" + w.mLayoutAttached
503 + " screen changed=" + w.isConfigChanged());
504 final AppWindowToken atoken = w.mAppToken;
505 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
506 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
507 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
508 + " parentHidden=" + w.isParentWindowHidden());
509 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
510 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
511 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
512 + " parentHidden=" + w.isParentWindowHidden());
513 }
514
515 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
516 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
517 // since that means "perform layout as normal, just don't display").
518 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
519 || ((w.isConfigChanged() || w.setReportResizeHints())
520 && !w.isGoneForLayoutLw() &&
521 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
522 (w.mHasSurface && w.mAppToken != null &&
523 w.mAppToken.layoutConfigChanges)))) {
524 if (!w.mLayoutAttached) {
525 if (mTmpInitial) {
526 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
527 w.mContentChanged = false;
528 }
529 if (w.mAttrs.type == TYPE_DREAM) {
530 // Don't layout windows behind a dream, so that if it does stuff like hide
531 // the status bar we won't get a bad transition when it goes away.
532 mTmpWindow = w;
533 }
534 w.mLayoutNeeded = false;
535 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200536 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800537 mService.mPolicy.layoutWindowLw(w, null);
538 w.mLayoutSeq = mService.mLayoutSeq;
539
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200540 // If this is the first layout, we need to initialize the last inset values as
541 // otherwise we'd immediately cause an unnecessary resize.
542 if (firstLayout) {
543 w.updateLastInsetValues();
544 }
545
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800546 // Window frames may have changed. Update dim layer with the new bounds.
547 final Task task = w.getTask();
548 if (task != null) {
549 mDimLayerController.updateDimLayer(task);
550 }
551
552 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
553 + " mContainingFrame=" + w.mContainingFrame
554 + " mDisplayFrame=" + w.mDisplayFrame);
555 }
556 }
557 };
558
559 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
560 if (w.mLayoutAttached) {
561 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
562 + " mViewVisibility=" + w.mViewVisibility
563 + " mRelayoutCalled=" + w.mRelayoutCalled);
564 // If this view is GONE, then skip it -- keep the current frame, and let the caller
565 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
566 // windows, since that means "perform layout as normal, just don't display").
567 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
568 return;
569 }
570 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
571 || w.mLayoutNeeded) {
572 if (mTmpInitial) {
573 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
574 w.mContentChanged = false;
575 }
576 w.mLayoutNeeded = false;
577 w.prelayout();
578 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
579 w.mLayoutSeq = mService.mLayoutSeq;
580 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
581 + " mContainingFrame=" + w.mContainingFrame
582 + " mDisplayFrame=" + w.mDisplayFrame);
583 }
584 } else if (w.mAttrs.type == TYPE_DREAM) {
585 // Don't layout windows behind a dream, so that if it does stuff like hide the
586 // status bar we won't get a bad transition when it goes away.
587 mTmpWindow = mTmpWindow2;
588 }
589 };
590
591 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
592 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
593 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
594 return w.canBeImeTarget();
595 };
596
597 private final Consumer<WindowState> mApplyPostLayoutPolicy =
598 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
599 mService.mInputMethodTarget);
600
601 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
602 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
603 final boolean obscuredChanged = w.mObscured !=
604 mTmpApplySurfaceChangesTransactionState.obscured;
605 final RootWindowContainer root = mService.mRoot;
606 // Only used if default window
607 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
608
609 // Update effect.
610 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
611 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
612 final boolean isDisplayed = w.isDisplayedLw();
613
614 if (isDisplayed && w.isObscuringDisplay()) {
615 // This window completely covers everything behind it, so we want to leave all
616 // of them as undimmed (for performance reasons).
617 root.mObscuringWindow = w;
618 mTmpApplySurfaceChangesTransactionState.obscured = true;
619 }
620
621 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
622 root.handleNotObscuredLocked(w,
623 mTmpApplySurfaceChangesTransactionState.obscured,
624 mTmpApplySurfaceChangesTransactionState.syswin);
625
626 if (w.mHasSurface && isDisplayed) {
627 final int type = w.mAttrs.type;
628 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
629 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
630 mTmpApplySurfaceChangesTransactionState.syswin = true;
631 }
632 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
633 && w.mAttrs.preferredRefreshRate != 0) {
634 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
635 = w.mAttrs.preferredRefreshRate;
636 }
637 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
638 && w.mAttrs.preferredDisplayModeId != 0) {
639 mTmpApplySurfaceChangesTransactionState.preferredModeId
640 = w.mAttrs.preferredDisplayModeId;
641 }
642 }
643 }
644
645 w.applyDimLayerIfNeeded();
646
647 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
648 && mWallpaperController.isWallpaperTarget(w)) {
649 // This is the wallpaper target and its obscured state changed... make sure the
650 // current wallpaper's visibility has been updated accordingly.
651 mWallpaperController.updateWallpaperVisibility();
652 }
653
654 w.handleWindowMovedIfNeeded();
655
656 final WindowStateAnimator winAnimator = w.mWinAnimator;
657
658 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
659 w.mContentChanged = false;
660
661 // Moved from updateWindowsAndWallpaperLocked().
662 if (w.mHasSurface) {
663 // Take care of the window being ready to display.
664 final boolean committed = winAnimator.commitFinishDrawingLocked();
665 if (isDefaultDisplay && committed) {
666 if (w.mAttrs.type == TYPE_DREAM) {
667 // HACK: When a dream is shown, it may at that point hide the lock screen.
668 // So we need to redo the layout to let the phone window manager make this
669 // happen.
670 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
671 if (DEBUG_LAYOUT_REPEATS) {
672 surfacePlacer.debugLayoutRepeats(
673 "dream and commitFinishDrawingLocked true",
674 pendingLayoutChanges);
675 }
676 }
677 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
678 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
679 "First draw done in potential wallpaper target " + w);
680 root.mWallpaperMayChange = true;
681 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
682 if (DEBUG_LAYOUT_REPEATS) {
683 surfacePlacer.debugLayoutRepeats(
684 "wallpaper and commitFinishDrawingLocked true",
685 pendingLayoutChanges);
686 }
687 }
688 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700689 final TaskStack stack = w.getStack();
690 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
691 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800692 // Updates the shown frame before we set up the surface. This is needed
693 // because the resizing could change the top-left position (in addition to
694 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
695 // position the surface.
696 //
697 // If an animation is being started, we can't call this method because the
698 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700699 // we do want to update the position if the window is animating. We make an exception
700 // for the bounds animating state, where an application may have been waiting
701 // for an exit animation to start, but instead enters PiP. We need to ensure
702 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800703 winAnimator.computeShownFrameLocked();
704 }
705 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
706 }
707
708 final AppWindowToken atoken = w.mAppToken;
709 if (atoken != null) {
710 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
711 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
712 mTmpUpdateAllDrawn.add(atoken);
713 }
714 }
715
716 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
717 && w.isDisplayedLw()) {
718 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
719 }
720
721 w.updateResizingWindowIfNeeded();
722 };
723
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800724 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800725 * Create new {@link DisplayContent} instance, add itself to the root window container and
726 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700727 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800728 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700729 * @param layersController window layer controller used to assign layer to the windows on this
730 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700731 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
732 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700733 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700734 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700735 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800736 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
737 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
738 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
739 + " new=" + display);
740 }
741
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700742 mDisplay = display;
743 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700744 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700745 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700746 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700747 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700748 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700749 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700750 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800751 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700752 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800753 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700754
755 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700756 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700757 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700758 super.addChild(mAboveAppWindowsContainers, null);
759 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800760
761 // Add itself as a child to the root container.
762 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700763
764 // TODO(b/62541591): evaluate whether this is the best spot to declare the
765 // {@link DisplayContent} ready for use.
766 mDisplayReady = true;
767 }
768
769 boolean isReady() {
770 // The display is ready when the system and the individual display are both ready.
771 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700772 }
773
774 int getDisplayId() {
775 return mDisplayId;
776 }
777
Wale Ogunwale02319a62016-09-26 15:21:22 -0700778 WindowToken getWindowToken(IBinder binder) {
779 return mTokenMap.get(binder);
780 }
781
782 AppWindowToken getAppWindowToken(IBinder binder) {
783 final WindowToken token = getWindowToken(binder);
784 if (token == null) {
785 return null;
786 }
787 return token.asAppWindowToken();
788 }
789
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700790 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700791 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
792 if (dc != null) {
793 // We currently don't support adding a window token to the display if the display
794 // already has the binder mapped to another token. If there is a use case for supporting
795 // this moving forward we will either need to merge the WindowTokens some how or have
796 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700797 throw new IllegalArgumentException("Can't map token=" + token + " to display="
798 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700799 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700800 if (binder == null) {
801 throw new IllegalArgumentException("Can't map token=" + token + " to display="
802 + getName() + " binder is null");
803 }
804 if (token == null) {
805 throw new IllegalArgumentException("Can't map null token to display="
806 + getName() + " binder=" + binder);
807 }
808
Wale Ogunwale02319a62016-09-26 15:21:22 -0700809 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700810
811 if (token.asAppWindowToken() == null) {
812 // Add non-app token to container hierarchy on the display. App tokens are added through
813 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700814 switch (token.windowType) {
815 case TYPE_WALLPAPER:
816 mBelowAppWindowsContainers.addChild(token);
817 break;
818 case TYPE_INPUT_METHOD:
819 case TYPE_INPUT_METHOD_DIALOG:
820 mImeWindowsContainers.addChild(token);
821 break;
822 default:
823 mAboveAppWindowsContainers.addChild(token);
824 break;
825 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700826 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700827 }
828
829 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700830 final WindowToken token = mTokenMap.remove(binder);
831 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800832 token.setExiting();
833 }
834 return token;
835 }
836
837 /** Changes the display the input window token is housed on to this one. */
838 void reParentWindowToken(WindowToken token) {
839 final DisplayContent prevDc = token.getDisplayContent();
840 if (prevDc == this) {
841 return;
842 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800843 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
844 && token.asAppWindowToken() == null) {
845 // Removed the token from the map, but made sure it's not an app token before removing
846 // from parent.
847 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700848 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800849
850 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700851 }
852
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700853 void removeAppToken(IBinder binder) {
854 final WindowToken token = removeWindowToken(binder);
855 if (token == null) {
856 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
857 return;
858 }
859
860 final AppWindowToken appToken = token.asAppWindowToken();
861
862 if (appToken == null) {
863 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
864 return;
865 }
866
867 appToken.onRemovedFromDisplay();
868 }
869
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700870 Display getDisplay() {
871 return mDisplay;
872 }
873
Craig Mautner59c00972012-07-30 12:10:24 -0700874 DisplayInfo getDisplayInfo() {
875 return mDisplayInfo;
876 }
877
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700878 DisplayMetrics getDisplayMetrics() {
879 return mDisplayMetrics;
880 }
881
Andrii Kulian8ee72852017-03-10 10:36:45 -0800882 int getRotation() {
883 return mRotation;
884 }
885
886 void setRotation(int newRotation) {
887 mRotation = newRotation;
888 }
889
890 int getLastOrientation() {
891 return mLastOrientation;
892 }
893
894 void setLastOrientation(int orientation) {
895 mLastOrientation = orientation;
896 }
897
898 boolean getAltOrientation() {
899 return mAltOrientation;
900 }
901
902 void setAltOrientation(boolean altOrientation) {
903 mAltOrientation = altOrientation;
904 }
905
906 int getLastWindowForcedOrientation() {
907 return mLastWindowForcedOrientation;
908 }
909
Andrii Kulian06d07d62017-03-14 11:11:47 -0700910 /**
911 * Update rotation of the display.
912 *
913 * Returns true if the rotation has been changed. In this case YOU MUST CALL
914 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
915 */
916 boolean updateRotationUnchecked(boolean inTransaction) {
917 if (mService.mDeferredRotationPauseCount > 0) {
918 // Rotation updates have been paused temporarily. Defer the update until
919 // updates have been resumed.
920 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
921 return false;
922 }
923
924 ScreenRotationAnimation screenRotationAnimation =
925 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
926 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
927 // Rotation updates cannot be performed while the previous rotation change
928 // animation is still in progress. Skip this update. We will try updating
929 // again after the animation is finished and the display is unfrozen.
930 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
931 return false;
932 }
933 if (mService.mDisplayFrozen) {
934 // Even if the screen rotation animation has finished (e.g. isAnimating
935 // returns false), there is still some time where we haven't yet unfrozen
936 // the display. We also need to abort rotation here.
937 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
938 "Deferring rotation, still finishing previous rotation");
939 return false;
940 }
941
942 if (!mService.mDisplayEnabled) {
943 // No point choosing a rotation if the display is not enabled.
944 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
945 return false;
946 }
947
948 final int oldRotation = mRotation;
949 final int lastOrientation = mLastOrientation;
950 final boolean oldAltOrientation = mAltOrientation;
951 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700952 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
953 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700954
Robert Carr897215d2017-03-29 12:25:50 -0700955 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700956 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
957 if (seamlessRotated != null) {
958 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
959 // to complete (that is, waiting for windows to redraw). It's tempting to check
960 // w.mSeamlessRotationCount but that could be incorrect in the case of
961 // window-removal.
962 return false;
963 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700964 }
965
966 // TODO: Implement forced rotation changes.
967 // Set mAltOrientation to indicate that the application is receiving
968 // an orientation that has different metrics than it expected.
969 // eg. Portrait instead of Landscape.
970
971 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
972 lastOrientation, rotation);
973
974 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
975 + ", got rotation " + rotation + " which has "
976 + (altOrientation ? "incompatible" : "compatible") + " metrics");
977
978 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
979 // No change.
980 return false;
981 }
982
983 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
984 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
985 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
986
987 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
988 mService.mWaitingForConfig = true;
989 }
990
991 mRotation = rotation;
992 mAltOrientation = altOrientation;
993 if (isDefaultDisplay) {
994 mService.mPolicy.setRotationLw(rotation);
995 }
996
997 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
998 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
999 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1000 WINDOW_FREEZE_TIMEOUT_DURATION);
1001
1002 setLayoutNeeded();
1003 final int[] anim = new int[2];
1004 if (isDimming()) {
1005 anim[0] = anim[1] = 0;
1006 } else {
1007 mService.mPolicy.selectRotationAnimationLw(anim);
1008 }
1009
1010 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001011 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001012 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1013 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1014 mDisplayId);
1015 } else {
1016 // The screen rotation animation uses a screenshot to freeze the screen
1017 // while windows resize underneath.
1018 // When we are rotating seamlessly, we allow the elements to transition
1019 // to their rotated state independently and without a freeze required.
1020 screenRotationAnimation = null;
1021
1022 // We have to reset this in case a window was removed before it
1023 // finished seamless rotation.
1024 mService.mSeamlessRotationCount = 0;
1025 }
1026
1027 // We need to update our screen size information to match the new rotation. If the rotation
1028 // has actually changed then this method will return true and, according to the comment at
1029 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1030 // By updating the Display info here it will be available to
1031 // #computeScreenConfiguration() later.
1032 updateDisplayAndOrientation(getConfiguration().uiMode);
1033
1034 if (!inTransaction) {
1035 if (SHOW_TRANSACTIONS) {
1036 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1037 }
1038 mService.openSurfaceTransaction();
1039 }
1040 try {
1041 // NOTE: We disable the rotation in the emulator because
1042 // it doesn't support hardware OpenGL emulation yet.
1043 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1044 && screenRotationAnimation.hasScreenshot()) {
1045 if (screenRotationAnimation.setRotationInTransaction(
1046 rotation, mService.mFxSession,
1047 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1048 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1049 mService.scheduleAnimationLocked();
1050 }
1051 }
1052
1053 if (rotateSeamlessly) {
1054 forAllWindows(w -> {
1055 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1056 }, true /* traverseTopToBottom */);
1057 }
1058
1059 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1060 } finally {
1061 if (!inTransaction) {
1062 mService.closeSurfaceTransaction();
1063 if (SHOW_LIGHT_TRANSACTIONS) {
1064 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1065 }
1066 }
1067 }
1068
1069 forAllWindows(w -> {
1070 // Discard surface after orientation change, these can't be reused.
1071 if (w.mAppToken != null) {
1072 w.mAppToken.destroySavedSurfaces();
1073 }
1074 if (w.mHasSurface && !rotateSeamlessly) {
1075 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001076 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001077 mService.mRoot.mOrientationChangeComplete = false;
1078 w.mLastFreezeDuration = 0;
1079 }
1080 w.mReportOrientationChanged = true;
1081 }, true /* traverseTopToBottom */);
1082
1083 if (rotateSeamlessly) {
1084 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1085 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1086 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1087 }
1088
1089 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1090 final WindowManagerService.RotationWatcher rotationWatcher
1091 = mService.mRotationWatchers.get(i);
1092 if (rotationWatcher.mDisplayId == mDisplayId) {
1093 try {
1094 rotationWatcher.mWatcher.onRotationChanged(rotation);
1095 } catch (RemoteException e) {
1096 // Ignore
1097 }
1098 }
1099 }
1100
1101 // TODO (multi-display): Magnification is supported only for the default display.
1102 // Announce rotation only if we will not animate as we already have the
1103 // windows in final state. Otherwise, we make this call at the rotation end.
1104 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1105 && isDefaultDisplay) {
1106 mService.mAccessibilityController.onRotationChangedLocked(this);
1107 }
1108
1109 return true;
1110 }
1111
1112 /**
1113 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1114 * changed.
1115 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1116 */
1117 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1118 // Use the effective "visual" dimensions based on current rotation
1119 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1120 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1121 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1122 int dw = realdw;
1123 int dh = realdh;
1124
1125 if (mAltOrientation) {
1126 if (realdw > realdh) {
1127 // Turn landscape into portrait.
1128 int maxw = (int)(realdh/1.3f);
1129 if (maxw < realdw) {
1130 dw = maxw;
1131 }
1132 } else {
1133 // Turn portrait into landscape.
1134 int maxh = (int)(realdw/1.3f);
1135 if (maxh < realdh) {
1136 dh = maxh;
1137 }
1138 }
1139 }
1140
1141 // Update application display metrics.
1142 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1143 mDisplayId);
1144 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1145 mDisplayId);
1146 mDisplayInfo.rotation = mRotation;
1147 mDisplayInfo.logicalWidth = dw;
1148 mDisplayInfo.logicalHeight = dh;
1149 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1150 mDisplayInfo.appWidth = appWidth;
1151 mDisplayInfo.appHeight = appHeight;
1152 if (isDefaultDisplay) {
1153 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1154 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1155 }
1156 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1157 if (mDisplayScalingDisabled) {
1158 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1159 } else {
1160 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1161 }
1162
1163 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1164 mDisplayInfo);
1165
1166 mBaseDisplayRect.set(0, 0, dw, dh);
1167
1168 if (isDefaultDisplay) {
1169 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1170 mCompatDisplayMetrics);
1171 }
1172 return mDisplayInfo;
1173 }
1174
1175 /**
1176 * Compute display configuration based on display properties and policy settings.
1177 * Do not call if mDisplayReady == false.
1178 */
1179 void computeScreenConfiguration(Configuration config) {
1180 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1181
1182 final int dw = displayInfo.logicalWidth;
1183 final int dh = displayInfo.logicalHeight;
1184 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1185 Configuration.ORIENTATION_LANDSCAPE;
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001186
Andrii Kulian06d07d62017-03-14 11:11:47 -07001187 config.screenWidthDp =
1188 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1189 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1190 config.screenHeightDp =
1191 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1192 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001193
1194 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1195 final int leftInset = mTmpRect.left;
1196 final int topInset = mTmpRect.top;
1197 // appBounds at the root level should mirror the app screen size.
1198 config.setAppBounds(leftInset /*left*/, topInset /*top*/, leftInset + displayInfo.appWidth /*right*/,
1199 topInset + displayInfo.appHeight /*bottom*/);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001200 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1201 || displayInfo.rotation == Surface.ROTATION_270);
1202
1203 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1204 mDisplayMetrics.density, config);
1205
1206 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1207 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1208 ? Configuration.SCREENLAYOUT_ROUND_YES
1209 : Configuration.SCREENLAYOUT_ROUND_NO);
1210
1211 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1212 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1213 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1214 dh, mDisplayId);
1215 config.densityDpi = displayInfo.logicalDensityDpi;
1216
1217 config.colorMode =
1218 (displayInfo.isHdr()
1219 ? Configuration.COLOR_MODE_HDR_YES
1220 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001221 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001222 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1223 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1224
1225 // Update the configuration based on available input devices, lid switch,
1226 // and platform configuration.
1227 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1228 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1229 config.navigation = Configuration.NAVIGATION_NONAV;
1230
1231 int keyboardPresence = 0;
1232 int navigationPresence = 0;
1233 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1234 final int len = devices != null ? devices.length : 0;
1235 for (int i = 0; i < len; i++) {
1236 InputDevice device = devices[i];
1237 if (!device.isVirtual()) {
1238 final int sources = device.getSources();
1239 final int presenceFlag = device.isExternal() ?
1240 WindowManagerPolicy.PRESENCE_EXTERNAL :
1241 WindowManagerPolicy.PRESENCE_INTERNAL;
1242
1243 // TODO(multi-display): Configure on per-display basis.
1244 if (mService.mIsTouchDevice) {
1245 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1246 InputDevice.SOURCE_TOUCHSCREEN) {
1247 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1248 }
1249 } else {
1250 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1251 }
1252
1253 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1254 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1255 navigationPresence |= presenceFlag;
1256 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1257 && config.navigation == Configuration.NAVIGATION_NONAV) {
1258 config.navigation = Configuration.NAVIGATION_DPAD;
1259 navigationPresence |= presenceFlag;
1260 }
1261
1262 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1263 config.keyboard = Configuration.KEYBOARD_QWERTY;
1264 keyboardPresence |= presenceFlag;
1265 }
1266 }
1267 }
1268
1269 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1270 config.navigation = Configuration.NAVIGATION_DPAD;
1271 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1272 }
1273
1274 // Determine whether a hard keyboard is available and enabled.
1275 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1276 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1277 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1278 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1279 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1280 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1281 }
1282
1283 // Let the policy update hidden states.
1284 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1285 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1286 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1287 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1288 }
1289
1290 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1291 int displayId) {
1292 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1293 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1294 final int unrotDw, unrotDh;
1295 if (rotated) {
1296 unrotDw = dh;
1297 unrotDh = dw;
1298 } else {
1299 unrotDw = dw;
1300 unrotDh = dh;
1301 }
1302 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1303 displayId);
1304 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1305 displayId);
1306 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1307 displayId);
1308 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1309 displayId);
1310 return sw;
1311 }
1312
1313 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1314 DisplayMetrics dm, int dw, int dh, int displayId) {
1315 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1316 displayId);
1317 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1318 uiMode, displayId);
1319 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1320 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1321 if (curSize == 0 || size < curSize) {
1322 curSize = size;
1323 }
1324 return curSize;
1325 }
1326
1327 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1328 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1329
1330 // We need to determine the smallest width that will occur under normal
1331 // operation. To this, start with the base screen size and compute the
1332 // width under the different possible rotations. We need to un-rotate
1333 // the current screen dimensions before doing this.
1334 int unrotDw, unrotDh;
1335 if (rotated) {
1336 unrotDw = dh;
1337 unrotDh = dw;
1338 } else {
1339 unrotDw = dw;
1340 unrotDh = dh;
1341 }
1342 displayInfo.smallestNominalAppWidth = 1<<30;
1343 displayInfo.smallestNominalAppHeight = 1<<30;
1344 displayInfo.largestNominalAppWidth = 0;
1345 displayInfo.largestNominalAppHeight = 0;
1346 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1347 unrotDh);
1348 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1349 unrotDw);
1350 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1351 unrotDh);
1352 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1353 unrotDw);
1354 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1355 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1356 displayId);
1357 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1358 displayId);
1359 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1360 displayId);
1361 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1362 displayId);
1363 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1364 outConfig.screenLayout = sl;
1365 }
1366
1367 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1368 int uiMode, int displayId) {
1369 // Get the app screen size at this rotation.
1370 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1371 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1372
1373 // Compute the screen layout size class for this rotation.
1374 int longSize = w;
1375 int shortSize = h;
1376 if (longSize < shortSize) {
1377 int tmp = longSize;
1378 longSize = shortSize;
1379 shortSize = tmp;
1380 }
1381 longSize = (int)(longSize/density);
1382 shortSize = (int)(shortSize/density);
1383 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1384 }
1385
1386 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1387 int uiMode, int dw, int dh) {
1388 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1389 displayId);
1390 if (width < displayInfo.smallestNominalAppWidth) {
1391 displayInfo.smallestNominalAppWidth = width;
1392 }
1393 if (width > displayInfo.largestNominalAppWidth) {
1394 displayInfo.largestNominalAppWidth = width;
1395 }
1396 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1397 displayId);
1398 if (height < displayInfo.smallestNominalAppHeight) {
1399 displayInfo.smallestNominalAppHeight = height;
1400 }
1401 if (height > displayInfo.largestNominalAppHeight) {
1402 displayInfo.largestNominalAppHeight = height;
1403 }
1404 }
1405
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001406 DockedStackDividerController getDockedDividerController() {
1407 return mDividerControllerLocked;
1408 }
1409
Winson Chung655332c2016-10-31 13:14:28 -07001410 PinnedStackController getPinnedStackController() {
1411 return mPinnedStackControllerLocked;
1412 }
1413
Jeff Browna506a6e2013-06-04 00:02:38 -07001414 /**
1415 * Returns true if the specified UID has access to this display.
1416 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001417 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001418 return mDisplay.hasAccess(uid);
1419 }
1420
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001421 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001422 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001423 }
1424
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001425 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001426 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001427 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001428 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001429 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001430 }
1431
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001432 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001433 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1434 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001435 if (stack.mStackId == stackId) {
1436 return stack;
1437 }
1438 }
1439 return null;
1440 }
1441
Bryce Lee48f4b572017-04-10 10:54:15 -07001442 @VisibleForTesting
1443 int getStackCount() {
1444 return mTaskStackContainers.size();
1445 }
1446
1447 @VisibleForTesting
1448 int getStaskPosById(int stackId) {
1449 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1450 final TaskStack stack = mTaskStackContainers.get(i);
1451 if (stack.mStackId == stackId) {
1452 return i;
1453 }
1454 }
1455 return -1;
1456 }
1457
Andrii Kulian441e4492016-09-29 15:25:00 -07001458 @Override
1459 void onConfigurationChanged(Configuration newParentConfig) {
1460 super.onConfigurationChanged(newParentConfig);
1461
Andrii Kulian3a507b52016-09-19 18:14:12 -07001462 // The display size information is heavily dependent on the resources in the current
1463 // configuration, so we need to reconfigure it every time the configuration changes.
1464 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1465 mService.reconfigureDisplayLocked(this);
1466
1467 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001468 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001469 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001470
Andrii Kulian441e4492016-09-29 15:25:00 -07001471 /**
1472 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1473 * bounds were updated.
1474 */
1475 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001476 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1477 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001478 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001479 changedStackList.add(stack.mStackId);
1480 }
1481 }
Winson Chung32c566f2017-04-11 18:31:21 -07001482
1483 // If there was no pinned stack, we still need to notify the controller of the display info
1484 // update as a result of the config change. We do this here to consolidate the flow between
1485 // changes when there is and is not a stack.
1486 if (getStackById(PINNED_STACK_ID) == null) {
1487 mPinnedStackControllerLocked.onDisplayInfoChanged();
1488 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001489 }
1490
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001491 @Override
1492 boolean fillsParent() {
1493 return true;
1494 }
1495
1496 @Override
1497 boolean isVisible() {
1498 return true;
1499 }
1500
1501 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001502 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001503 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001504 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001505 }
1506
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001507 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001508 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1509 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1510 // target, or here in order if there isn't an IME target.
1511 if (traverseTopToBottom) {
1512 for (int i = mChildren.size() - 1; i >= 0; --i) {
1513 final DisplayChildWindowContainer child = mChildren.get(i);
1514 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1515 // In this case the Ime windows will be processed above their target so we skip
1516 // here.
1517 continue;
1518 }
1519 if (child.forAllWindows(callback, traverseTopToBottom)) {
1520 return true;
1521 }
1522 }
1523 } else {
1524 final int count = mChildren.size();
1525 for (int i = 0; i < count; i++) {
1526 final DisplayChildWindowContainer child = mChildren.get(i);
1527 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1528 // In this case the Ime windows will be processed above their target so we skip
1529 // here.
1530 continue;
1531 }
1532 if (child.forAllWindows(callback, traverseTopToBottom)) {
1533 return true;
1534 }
1535 }
1536 }
1537 return false;
1538 }
1539
1540 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1541 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1542 }
1543
Wale Ogunwale399c8692017-05-08 14:22:42 -07001544 @Override
1545 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001546 final WindowManagerPolicy policy = mService.mPolicy;
1547
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001548 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001549 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001550 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001551 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001552 // If the display is frozen, some activities may be in the middle of restarting, and
1553 // thus have removed their old window. If the window has the flag to hide the lock
1554 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1555 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001556 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001557 } else if (policy.isKeyguardLocked()) {
1558 // Use the last orientation the while the display is frozen with the keyguard
1559 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1560 // window. We don't want to check the show when locked window directly though as
1561 // things aren't stable while the display is frozen, for example the window could be
1562 // momentarily unavailable due to activity relaunch.
1563 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001564 + "return " + mLastOrientation);
1565 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001566 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001567 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001568 final int orientation = mAboveAppWindowsContainers.getOrientation();
1569 if (orientation != SCREEN_ORIENTATION_UNSET) {
1570 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001571 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001572 }
1573
Wale Ogunwale399c8692017-05-08 14:22:42 -07001574 // Top system windows are not requesting an orientation. Start searching from apps.
1575 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001576 }
1577
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001578 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001579 // Check if display metrics changed and update base values if needed.
1580 updateBaseDisplayMetricsIfNeeded();
1581
Craig Mautner722285e2012-09-07 13:55:58 -07001582 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001583 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001584
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001585 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1586 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001587 }
Craig Mautner722285e2012-09-07 13:55:58 -07001588 }
1589
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001590 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001591 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1592 if (displayManagerInternal != null) {
1593 // Bootstrap the default logical display from the display manager.
1594 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1595 if (newDisplayInfo != null) {
1596 mDisplayInfo.copyFrom(newDisplayInfo);
1597 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001598 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001599
Andrii Kuliancd097992017-03-23 18:31:59 -07001600 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1601 mDisplayInfo.logicalDensityDpi);
1602 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1603 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1604 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001605 }
1606
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001607 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001608 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001609 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001610 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001611 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1612 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001613 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001614 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001615 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001616 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001617 out.set(left, top, left + width, top + height);
1618 }
1619
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001620 private void getLogicalDisplayRect(Rect out, int orientation) {
1621 getLogicalDisplayRect(out);
1622
1623 // Rotate the Rect if needed.
1624 final int currentRotation = mDisplayInfo.rotation;
1625 final int rotationDelta = deltaRotation(currentRotation, orientation);
1626 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1627 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1628 mTmpRectF.set(out);
1629 mTmpMatrix.mapRect(mTmpRectF);
1630 mTmpRectF.round(out);
1631 }
1632 }
1633
Andrii Kuliancd097992017-03-23 18:31:59 -07001634 /**
1635 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1636 * values.
1637 */
1638 private void updateBaseDisplayMetricsIfNeeded() {
1639 // Get real display metrics without overrides from WM.
1640 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1641 final int orientation = mDisplayInfo.rotation;
1642 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1643 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1644 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1645 final int newDensity = mDisplayInfo.logicalDensityDpi;
1646
1647 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1648 || mInitialDisplayHeight != newHeight
1649 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1650
1651 if (displayMetricsChanged) {
1652 // Check if display size or density is forced.
1653 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1654 || mBaseDisplayHeight != mInitialDisplayHeight;
1655 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1656
1657 // If there is an override set for base values - use it, otherwise use new values.
1658 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1659 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1660 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1661
1662 // Real display metrics changed, so we should also update initial values.
1663 mInitialDisplayWidth = newWidth;
1664 mInitialDisplayHeight = newHeight;
1665 mInitialDisplayDensity = newDensity;
1666 mService.reconfigureDisplayLocked(this);
1667 }
1668 }
1669
Bryce Lee27cec322017-03-21 09:41:37 -07001670 /** Sets the maximum width the screen resolution can be */
1671 void setMaxUiWidth(int width) {
1672 if (DEBUG_DISPLAY) {
1673 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1674 }
1675
1676 mMaxUiWidth = width;
1677
1678 // Update existing metrics.
1679 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1680 }
1681
1682 /** Update base (override) display metrics. */
1683 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1684 mBaseDisplayWidth = baseWidth;
1685 mBaseDisplayHeight = baseHeight;
1686 mBaseDisplayDensity = baseDensity;
1687
1688 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1689 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1690 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1691 mBaseDisplayWidth = mMaxUiWidth;
1692
1693 if (DEBUG_DISPLAY) {
1694 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1695 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1696 + " on display:" + getDisplayId());
1697 }
1698 }
1699
1700 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1701 }
1702
Chong Zhangf66db432016-01-13 10:39:51 -08001703 void getContentRect(Rect out) {
1704 out.set(mContentRect);
1705 }
1706
Wale Ogunwale1666e312016-12-16 11:27:18 -08001707 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1708 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1709 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001710
Wale Ogunwale1666e312016-12-16 11:27:18 -08001711 TaskStack stack = getStackById(stackId);
1712 if (stack != null) {
1713 // It's already attached to the display...clear mDeferRemoval and move stack to
1714 // appropriate z-order on display as needed.
1715 stack.mDeferRemoval = false;
1716 // We're not moving the display to front when we're adding stacks, only when
1717 // requested to change the position of stack explicitly.
1718 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1719 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001720 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001721 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001722 mTaskStackContainers.addStackToDisplay(stack, onTop);
1723 }
1724
Wale Ogunwale1666e312016-12-16 11:27:18 -08001725 if (stackId == DOCKED_STACK_ID) {
1726 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001727 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001728 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001729 }
1730
Andrii Kulian51c1b672017-04-07 18:39:32 -07001731 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001732 final DisplayContent prevDc = stack.getDisplayContent();
1733 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001734 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1735 + " which is not currently attached to any display");
1736 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001737 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001738 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1739 + " to its current displayId=" + mDisplayId);
1740 }
1741
Wale Ogunwale1666e312016-12-16 11:27:18 -08001742 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001743 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001744 }
1745
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001746 @Override
1747 protected void addChild(DisplayChildWindowContainer child,
1748 Comparator<DisplayChildWindowContainer> comparator) {
1749 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1750 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001751
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001752 @Override
1753 protected void addChild(DisplayChildWindowContainer child, int index) {
1754 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1755 }
1756
1757 @Override
1758 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001759 // Only allow removal of direct children from this display if the display is in the process
1760 // of been removed.
1761 if (mRemovingDisplay) {
1762 super.removeChild(child);
1763 return;
1764 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001765 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001766 }
1767
Andrii Kuliand2765632016-12-12 22:26:34 -08001768 @Override
1769 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1770 // Children of the display are statically ordered, so the real intention here is to perform
1771 // the operation on the display and not the static direct children.
1772 getParent().positionChildAt(position, this, includingParents);
1773 }
1774
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001775 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001776 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1777 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001778 final int taskId = stack.taskIdFromPoint(x, y);
1779 if (taskId != -1) {
1780 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001781 }
1782 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001783 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001784 }
1785
Chong Zhang8e89b312015-09-09 15:09:30 -07001786 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001787 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001788 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001789 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001790 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001791 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001792 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001793 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1794 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001795 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001796 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001797 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001798
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001799 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1800 if (mTmpTaskForResizePointSearchResult.searchDone) {
1801 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001802 }
1803 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001804 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001805 }
1806
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001807 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001808 // The provided task is the task on this display with focus, so if WindowManagerService's
1809 // focused app is not on this display, focusedTask will be null.
1810 if (focusedTask == null) {
1811 mTouchExcludeRegion.setEmpty();
1812 } else {
1813 mTouchExcludeRegion.set(mBaseDisplayRect);
1814 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1815 mTmpRect2.setEmpty();
1816 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1817 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1818 stack.setTouchExcludeRegion(
1819 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1820 }
1821 // If we removed the focused task above, add it back and only leave its
1822 // outside touch area in the exclusion. TapDectector is not interested in
1823 // any touch inside the focused task itself.
1824 if (!mTmpRect2.isEmpty()) {
1825 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1826 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001827 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001828 final WindowState inputMethod = mService.mInputMethodWindow;
1829 if (inputMethod != null && inputMethod.isVisibleLw()) {
1830 // If the input method is visible and the user is typing, we don't want these touch
1831 // events to be intercepted and used to change focus. This would likely cause a
1832 // disappearance of the input method.
1833 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001834 if (inputMethod.getDisplayId() == mDisplayId) {
1835 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1836 } else {
1837 // IME is on a different display, so we need to update its tap detector.
1838 // TODO(multidisplay): Remove when IME will always appear on same display.
1839 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1840 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001841 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001842 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1843 WindowState win = mTapExcludedWindows.get(i);
1844 win.getTouchableRegion(mTmpRegion);
1845 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1846 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001847 // TODO(multi-display): Support docked stacks on secondary displays.
1848 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001849 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001850 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001851 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1852 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001853 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001854 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001855 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001856 }
1857
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001858 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001859 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001860 super.switchUser();
1861 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001862 }
1863
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001864 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001865 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1866 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001867 }
1868 }
1869
1870 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001871 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001872 }
1873
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001874 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001875 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001876 }
1877
1878 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001879 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001880 }
1881
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001882 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001883 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001884 }
1885
Wale Ogunwale10124582016-09-15 20:25:50 -07001886 @Override
1887 void removeIfPossible() {
1888 if (isAnimating()) {
1889 mDeferredRemoval = true;
1890 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001891 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001892 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001893 }
1894
Wale Ogunwale10124582016-09-15 20:25:50 -07001895 @Override
1896 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001897 mRemovingDisplay = true;
1898 try {
1899 super.removeImmediately();
1900 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1901 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001902 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001903 mService.unregisterPointerEventListener(mTapDetector);
1904 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1905 }
1906 } finally {
1907 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001908 }
Craig Mautner95da1082014-02-24 17:54:35 -08001909 }
1910
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001911 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001912 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001913 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001914 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1915
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001916 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001917 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001918 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001919 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001920 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001921 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001922 }
1923
Andrii Kulian0214ed92017-05-16 13:44:05 -07001924 /** @return 'true' if removal of this display content is deferred due to active animation. */
1925 boolean isRemovalDeferred() {
1926 return mDeferredRemoval;
1927 }
1928
Wale Ogunwale10124582016-09-15 20:25:50 -07001929 boolean animateForIme(float interpolatedValue, float animationTarget,
1930 float dividerAnimationTarget) {
1931 boolean updated = false;
1932
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001933 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1934 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001935 if (stack == null || !stack.isAdjustedForIme()) {
1936 continue;
1937 }
1938
1939 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1940 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1941 updated = true;
1942 } else {
1943 mDividerControllerLocked.mLastAnimationProgress =
1944 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1945 mDividerControllerLocked.mLastDividerProgress =
1946 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1947 updated |= stack.updateAdjustForIme(
1948 mDividerControllerLocked.mLastAnimationProgress,
1949 mDividerControllerLocked.mLastDividerProgress,
1950 false /* force */);
1951 }
1952 if (interpolatedValue >= 1f) {
1953 stack.endImeAdjustAnimation();
1954 }
1955 }
1956
1957 return updated;
1958 }
1959
1960 boolean clearImeAdjustAnimation() {
1961 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001962 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1963 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001964 if (stack != null && stack.isAdjustedForIme()) {
1965 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1966 changed = true;
1967 }
1968 }
1969 return changed;
1970 }
1971
1972 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001973 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1974 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001975 if (stack.isVisible() && stack.isAdjustedForIme()) {
1976 stack.beginImeAdjustAnimation();
1977 }
1978 }
1979 }
1980
1981 void adjustForImeIfNeeded() {
1982 final WindowState imeWin = mService.mInputMethodWindow;
1983 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1984 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001985 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001986 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1987 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1988 imeTargetStack.getDockSide() : DOCKED_INVALID;
1989 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1990 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1991 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1992 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1993 final boolean imeHeightChanged = imeVisible &&
1994 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1995
1996 // The divider could be adjusted for IME position, or be thinner than usual,
1997 // or both. There are three possible cases:
1998 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1999 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2000 // - If IME is not visible, divider is not moved and is normal width.
2001
2002 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002003 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2004 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002005 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07002006 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
2007 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002008 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2009 } else {
2010 stack.resetAdjustedForIme(false);
2011 }
2012 }
2013 mDividerControllerLocked.setAdjustedForIme(
2014 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2015 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002016 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2017 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002018 stack.resetAdjustedForIme(!dockVisible);
2019 }
2020 mDividerControllerLocked.setAdjustedForIme(
2021 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2022 }
Winson Chung655332c2016-10-31 13:14:28 -07002023 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002024 }
2025
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002026 void setInputMethodAnimLayerAdjustment(int adj) {
2027 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2028 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002029 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002030 }
2031
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002032 /**
2033 * If a window that has an animation specifying a colored background and the current wallpaper
2034 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2035 * suddenly disappear.
2036 */
2037 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002038 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2039 w -> w.mIsWallpaper && w.isVisibleNow());
2040
2041 if (visibleWallpaper != null) {
2042 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002043 }
2044 return winAnimator.mAnimLayer;
2045 }
2046
Wale Ogunwale10124582016-09-15 20:25:50 -07002047 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002048 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2049 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002050 stack.prepareFreezingTaskBounds();
2051 }
2052 }
2053
Wale Ogunwale94744212015-09-21 19:01:47 -07002054 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002055 getLogicalDisplayRect(mTmpRect, newRotation);
2056
2057 // Compute a transform matrix to undo the coordinate space transformation,
2058 // and present the window at the same physical position it previously occupied.
2059 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2060 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2061
2062 mTmpRectF.set(bounds);
2063 mTmpMatrix.mapRect(mTmpRectF);
2064 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002065 }
2066
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002067 static int deltaRotation(int oldRotation, int newRotation) {
2068 int delta = newRotation - oldRotation;
2069 if (delta < 0) delta += 4;
2070 return delta;
2071 }
2072
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002073 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002074 Matrix outMatrix) {
2075 // For rotations without Z-ordering we don't need the target rectangle's position.
2076 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2077 displayHeight, outMatrix);
2078 }
2079
2080 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2081 float displayWidth, float displayHeight, Matrix outMatrix) {
2082 switch (rotation) {
2083 case ROTATION_0:
2084 outMatrix.reset();
2085 break;
2086 case ROTATION_270:
2087 outMatrix.setRotate(270, 0, 0);
2088 outMatrix.postTranslate(0, displayHeight);
2089 outMatrix.postTranslate(rectTop, 0);
2090 break;
2091 case ROTATION_180:
2092 outMatrix.reset();
2093 break;
2094 case ROTATION_90:
2095 outMatrix.setRotate(90, 0, 0);
2096 outMatrix.postTranslate(displayWidth, 0);
2097 outMatrix.postTranslate(-rectTop, rectLeft);
2098 break;
2099 }
2100 }
2101
Craig Mautnera91f9e22012-09-14 16:22:08 -07002102 public void dump(String prefix, PrintWriter pw) {
2103 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2104 final String subPrefix = " " + prefix;
2105 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2106 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2107 pw.print("dpi");
2108 if (mInitialDisplayWidth != mBaseDisplayWidth
2109 || mInitialDisplayHeight != mBaseDisplayHeight
2110 || mInitialDisplayDensity != mBaseDisplayDensity) {
2111 pw.print(" base=");
2112 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2113 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2114 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002115 if (mDisplayScalingDisabled) {
2116 pw.println(" noscale");
2117 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002118 pw.print(" cur=");
2119 pw.print(mDisplayInfo.logicalWidth);
2120 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2121 pw.print(" app=");
2122 pw.print(mDisplayInfo.appWidth);
2123 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2124 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2125 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2126 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2127 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002128 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2129 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002130
Craig Mautnerdc548482014-02-05 13:35:24 -08002131 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002132 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002133 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2134 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002135 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002136 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002137
Craig Mautnerdc548482014-02-05 13:35:24 -08002138 pw.println();
2139 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002140 pw.println();
2141 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002142 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002143 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002144 pw.print(" Exiting #"); pw.print(i);
2145 pw.print(' '); pw.print(token);
2146 pw.println(':');
2147 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002148 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002149 }
Craig Mautner59c00972012-07-30 12:10:24 -07002150 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002151 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002152 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002153 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002154 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002155 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002156
2157 if (mInputMethodAnimLayerAdjustment != 0) {
2158 pw.println(subPrefix
2159 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2160 }
Craig Mautner59c00972012-07-30 12:10:24 -07002161 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002162
2163 @Override
2164 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002165 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002166 }
2167
2168 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002169 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002170 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002171
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002172 /** Checks if stack with provided id is visible on this display. */
2173 boolean isStackVisible(int stackId) {
2174 final TaskStack stack = getStackById(stackId);
2175 return (stack != null && stack.isVisible());
2176 }
2177
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002178 /**
2179 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2180 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002181 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002182 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002183 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002184 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002185
2186 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002187 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002188 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002189 */
Jorim Jaggife762342016-10-13 14:33:27 +02002190 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002191 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002192 }
2193
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002194 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002195 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002196 final int x = (int) xf;
2197 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002198 final WindowState touchedWin = getWindow(w -> {
2199 final int flags = w.mAttrs.flags;
2200 if (!w.isVisibleLw()) {
2201 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002202 }
2203 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002204 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002205 }
2206
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002207 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002208 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002209 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002210 }
2211
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002212 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002213
2214 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002215 return mTmpRegion.contains(x, y) || touchFlags == 0;
2216 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002217
2218 return touchedWin;
2219 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002220
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002221 boolean canAddToastWindowForUid(int uid) {
2222 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002223 // Also if the app is focused adding more than one toast at
2224 // a time for better backwards compatibility.
2225 final WindowState focusedWindowForUid = getWindow(w ->
2226 w.mOwnerUid == uid && w.isFocused());
2227 if (focusedWindowForUid != null) {
2228 return true;
2229 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002230 final WindowState win = getWindow(w ->
2231 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2232 && !w.mWindowRemovalAllowed);
2233 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002234 }
2235
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002236 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002237 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2238 return;
2239 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002240
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002241 // Used to communicate the old focus to the callback method.
2242 mTmpWindow = oldFocus;
2243
2244 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002245 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002246
2247 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002248 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002249
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002250 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002251
2252 if (mTmpWindow == null) {
2253 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2254 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002255 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002256 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002257 }
2258
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002259 /** Updates the layer assignment of windows on this display. */
2260 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002261 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002262 if (setLayoutNeeded) {
2263 setLayoutNeeded();
2264 }
2265 }
2266
Wale Ogunwale1666e312016-12-16 11:27:18 -08002267 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2268 // moving containers or resizing them. Need to investigate the best way to have it automatically
2269 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002270 void layoutAndAssignWindowLayersIfNeeded() {
2271 mService.mWindowsChanged = true;
2272 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002273
2274 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2275 false /*updateInputWindows*/)) {
2276 assignWindowLayers(false /* setLayoutNeeded */);
2277 }
2278
2279 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2280 mService.mWindowPlacerLocked.performSurfacePlacement();
2281 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2282 }
2283
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002284 /** Returns true if a leaked surface was destroyed */
2285 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002286 // Used to indicate that a surface was leaked.
2287 mTmpWindow = null;
2288 forAllWindows(w -> {
2289 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002290 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002291 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002292 }
2293 if (!mService.mSessions.contains(wsa.mSession)) {
2294 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002295 + w + " surface=" + wsa.mSurfaceController
2296 + " token=" + w.mToken
2297 + " pid=" + w.mSession.mPid
2298 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002299 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002300 mService.mForceRemoves.add(w);
2301 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002302 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002303 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002304 + w + " surface=" + wsa.mSurfaceController
2305 + " token=" + w.mAppToken
2306 + " saved=" + w.hasSavedSurface());
2307 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002308 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002309 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002310 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002311 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002312
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002313 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002314 }
2315
2316 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002317 * Determine and return the window that should be the IME target.
2318 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2319 * @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 +00002320 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002321 WindowState computeImeTarget(boolean updateImeTarget) {
2322 if (mService.mInputMethodWindow == null) {
2323 // There isn't an IME so there shouldn't be a target...That was easy!
2324 if (updateImeTarget) {
2325 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2326 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2327 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2328 }
2329 return null;
2330 }
2331
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002332 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2333 // same display. Or even when the current IME/target are not on the same screen as the next
2334 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002335 mUpdateImeTarget = updateImeTarget;
2336 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002337
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002338
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002339 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2340 // to be on top of it, but it is not -really- where input will go. So look down below
2341 // for a real window to target...
2342 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2343 final AppWindowToken token = target.mAppToken;
2344 if (token != null) {
2345 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2346 if (betterTarget != null) {
2347 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002348 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002349 }
2350 }
2351
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002352 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2353 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002354
2355 // Now, a special case -- if the last target's window is in the process of exiting, and is
2356 // above the new target, keep on the last target to avoid flicker. Consider for example a
2357 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2358 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2359 // scrim.
2360 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002361 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2362 && (target == null
2363 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002364 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002365 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002366 }
2367
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002368 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2369 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002370
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002371 if (target == null) {
2372 if (updateImeTarget) {
2373 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2374 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2375 + Debug.getCallers(4) : ""));
2376 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2377 }
2378
2379 return null;
2380 }
2381
2382 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002383 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2384 if (token != null) {
2385
2386 // Now some fun for dealing with window animations that modify the Z order. We need
2387 // to look at all windows below the current target that are in this app, finding the
2388 // highest visible one in layering.
2389 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002390 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002391 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002392 }
2393
2394 if (highestTarget != null) {
2395 final AppTransition appTransition = mService.mAppTransition;
2396 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2397 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2398 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002399 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002400
2401 if (appTransition.isTransitionSet()) {
2402 // If we are currently setting up for an animation, hold everything until we
2403 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002404 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2405 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002406 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002407 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002408 // If the window we are currently targeting is involved with an animation,
2409 // and it is on top of the next target we will be over, then hold off on
2410 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002411 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2412 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002413 }
2414 }
2415 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002416
2417 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2418 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2419 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002420 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002421 }
2422
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002423 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002424 }
2425
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002426 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2427 if (target == mService.mInputMethodTarget
2428 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2429 && mInputMethodAnimLayerAdjustment == layerAdj) {
2430 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002431 }
2432
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002433 mService.mInputMethodTarget = target;
2434 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2435 setInputMethodAnimLayerAdjustment(layerAdj);
2436 assignWindowLayers(false /* setLayoutNeeded */);
2437 }
2438
2439 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2440 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2441 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2442 }
2443
2444 // Used to indicate we have reached the first window in the range we are interested in.
2445 mTmpWindow = null;
2446
2447 // TODO: Figure-out a more efficient way to do this.
2448 final WindowState candidate = getWindow(w -> {
2449 if (w == top) {
2450 // Reached the first window in the range we are interested in.
2451 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002452 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002453 if (mTmpWindow == null) {
2454 return false;
2455 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002456
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002457 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2458 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002459 }
2460 // If we reached the bottom of the range of windows we are considering,
2461 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002462 if (w == bottom) {
2463 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002464 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002465 return false;
2466 });
2467
2468 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002469 }
2470
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002471 void setLayoutNeeded() {
2472 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2473 mLayoutNeeded = true;
2474 }
2475
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002476 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002477 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2478 mLayoutNeeded = false;
2479 }
2480
2481 boolean isLayoutNeeded() {
2482 return mLayoutNeeded;
2483 }
2484
Wale Ogunwale02319a62016-09-26 15:21:22 -07002485 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2486 if (mTokenMap.isEmpty()) {
2487 return;
2488 }
2489 pw.println(" Display #" + mDisplayId);
2490 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2491 while (it.hasNext()) {
2492 final WindowToken token = it.next();
2493 pw.print(" ");
2494 pw.print(token);
2495 if (dumpAll) {
2496 pw.println(':');
2497 token.dump(pw, " ");
2498 } else {
2499 pw.println();
2500 }
2501 }
2502 }
2503
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002504 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002505 final int[] index = new int[1];
2506 forAllWindows(w -> {
2507 final WindowStateAnimator wAnim = w.mWinAnimator;
2508 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2509 index[0] = index[0] + 1;
2510 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002511 }
2512
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002513 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002514 forAllWindows(w -> {
2515 w.mWinAnimator.enableSurfaceTrace(fd);
2516 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002517 }
2518
2519 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002520 forAllWindows(w -> {
2521 w.mWinAnimator.disableSurfaceTrace();
2522 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002523 }
2524
Jorim Jaggife762342016-10-13 14:33:27 +02002525 /**
2526 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2527 */
2528 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2529 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002530 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002531 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2532 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002533 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002534 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2535 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002536 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002537 }
2538
Wale Ogunwale494009b82016-10-21 09:01:38 -07002539 boolean checkWaitingForWindows() {
2540
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 mHaveBootMsg = false;
2542 mHaveApp = false;
2543 mHaveWallpaper = false;
2544 mHaveKeyguard = true;
2545
2546 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002547 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2548 return true;
2549 }
2550 if (w.isDrawnLw()) {
2551 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002552 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002553 } else if (w.mAttrs.type == TYPE_APPLICATION
2554 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002555 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002556 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002557 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002558 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002559 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002560 }
2561 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002562 return false;
2563 });
2564
2565 if (visibleWindow != null) {
2566 // We have a visible window.
2567 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002568 }
2569
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002570 // if the wallpaper service is disabled on the device, we're never going to have
2571 // wallpaper, don't bother waiting for it
2572 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2573 com.android.internal.R.bool.config_enableWallpaperService)
2574 && !mService.mOnlyCore;
2575
Wale Ogunwale494009b82016-10-21 09:01:38 -07002576 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2577 "******** booted=" + mService.mSystemBooted
2578 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002579 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2580 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2581 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002582
2583 // If we are turning on the screen to show the boot message, don't do it until the boot
2584 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002585 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002586 return true;
2587 }
2588
2589 // If we are turning on the screen after the boot is completed normally, don't do so until
2590 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002591 if (mService.mSystemBooted
2592 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002593 return true;
2594 }
2595
2596 return false;
2597 }
2598
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002599 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002600 mTmpWindowAnimator = animator;
2601 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002602 }
2603
2604 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002605 resetAnimationBackgroundAnimator();
2606
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002607 // Used to indicate a detached wallpaper.
2608 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002609 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002610
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002611 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002612
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002613 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002614 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002615 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2616 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002617 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2618 }
2619 }
2620
2621 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002622 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002623 }
2624
Wale Ogunwale494009b82016-10-21 09:01:38 -07002625 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002626 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002627 if (imFocus == null) {
2628 return false;
2629 }
2630
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002631 if (DEBUG_INPUT_METHOD) {
2632 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2633 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2634 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002635 }
2636
Wale Ogunwale494009b82016-10-21 09:01:38 -07002637 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2638
2639 if (DEBUG_INPUT_METHOD) {
2640 Slog.i(TAG_WM, "IM target client: " + imeClient);
2641 if (imeClient != null) {
2642 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2643 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2644 }
2645 }
2646
2647 return imeClient != null && imeClient.asBinder() == client.asBinder();
2648 }
2649
2650 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002651 final WindowState win = getWindow(
2652 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2653 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002654 }
2655
2656 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002657 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002658 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002659 final int curValue = w.mSystemUiVisibility;
2660 final int diff = (curValue ^ visibility) & globalDiff;
2661 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002662 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002663 w.mSeq++;
2664 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002665 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2667 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002668 visibility, newValue, diff);
2669 }
2670 } catch (RemoteException e) {
2671 // so sorry
2672 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002673 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002674 }
2675
2676 void onWindowFreezeTimeout() {
2677 Slog.w(TAG_WM, "Window freeze timeout expired.");
2678 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002679
2680 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002681 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002682 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002683 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002684 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002685 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2686 - mService.mDisplayFreezeTime);
2687 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002688 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002689 mService.mWindowPlacerLocked.performSurfacePlacement();
2690 }
2691
2692 void waitForAllWindowsDrawn() {
2693 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002694 forAllWindows(w -> {
2695 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2696 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2697 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002698 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002699 w.mLastContentInsets.set(-1, -1, -1, -1);
2700 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002701 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002702 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002703 }
2704
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002705 // TODO: Super crazy long method that should be broken down...
2706 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2707
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002708 final int dw = mDisplayInfo.logicalWidth;
2709 final int dh = mDisplayInfo.logicalHeight;
2710 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2711
2712 mTmpUpdateAllDrawn.clear();
2713
2714 int repeats = 0;
2715 do {
2716 repeats++;
2717 if (repeats > 6) {
2718 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2719 clearLayoutNeeded();
2720 break;
2721 }
2722
2723 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2724 pendingLayoutChanges);
2725
Andrii Kulian839def92016-11-02 10:58:58 -07002726 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2727 // the wallpaper window jumping across displays.
2728 // Remove check for default display when there will be support for multiple wallpaper
2729 // targets (on different displays).
2730 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002731 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002732 }
2733
2734 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2735 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2736 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2737 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002738 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002739 }
2740 }
2741
2742 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2743 setLayoutNeeded();
2744 }
2745
2746 // FIRST LOOP: Perform a layout, if needed.
2747 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2748 performLayout(repeats == 1, false /* updateInputWindows */);
2749 } else {
2750 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2751 }
2752
2753 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2754 pendingLayoutChanges = 0;
2755
2756 if (isDefaultDisplay) {
2757 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002758 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002759 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2760 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2761 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2762 }
2763 } while (pendingLayoutChanges != 0);
2764
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002765 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002766 resetDimming();
2767
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002768 mTmpRecoveringMemory = recoveringMemory;
2769 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002770
2771 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002772 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2773 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2774 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002775 true /* inTraversal, must call performTraversalInTrans... below */);
2776
2777 stopDimmingIfNeeded();
2778
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002779 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2780 if (wallpaperVisible != mLastWallpaperVisible) {
2781 mLastWallpaperVisible = wallpaperVisible;
2782 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2783 }
2784
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002785 while (!mTmpUpdateAllDrawn.isEmpty()) {
2786 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2787 // See if any windows have been drawn, so they (and others associated with them)
2788 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002789 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002790 }
2791
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002792 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002793 }
2794
2795 void performLayout(boolean initial, boolean updateInputWindows) {
2796 if (!isLayoutNeeded()) {
2797 return;
2798 }
2799 clearLayoutNeeded();
2800
2801 final int dw = mDisplayInfo.logicalWidth;
2802 final int dh = mDisplayInfo.logicalHeight;
2803
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002804 if (DEBUG_LAYOUT) {
2805 Slog.v(TAG, "-------------------------------------");
2806 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2807 }
2808
Andrii Kulian8ee72852017-03-10 10:36:45 -08002809 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002810 getConfiguration().uiMode);
2811 if (isDefaultDisplay) {
2812 // Not needed on non-default displays.
2813 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2814 mService.mScreenRect.set(0, 0, dw, dh);
2815 }
2816
2817 mService.mPolicy.getContentRectLw(mContentRect);
2818
2819 int seq = mService.mLayoutSeq + 1;
2820 if (seq < 0) seq = 0;
2821 mService.mLayoutSeq = seq;
2822
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002823 // Used to indicate that we have processed the dream window and all additional windows are
2824 // behind it.
2825 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002826 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002827
2828 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002829 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002830
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002831 // Used to indicate that we have processed the dream window and all additional attached
2832 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002833 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002834 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002835
2836 // Now perform layout of attached windows, which usually depend on the position of the
2837 // window they are attached to. XXX does not deal with windows that are attached to windows
2838 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002839 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002840
2841 // Window frames may have changed. Tell the input dispatcher about it.
2842 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2843 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2844 if (updateInputWindows) {
2845 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2846 }
2847
2848 mService.mPolicy.finishLayoutLw();
2849 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2850 }
2851
2852 /**
2853 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2854 * In portrait mode, it grabs the full screenshot.
2855 *
2856 * @param width the width of the target bitmap
2857 * @param height the height of the target bitmap
2858 * @param includeFullDisplay true if the screen should not be cropped before capture
2859 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2860 * @param config of the output bitmap
2861 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002862 * @param includeDecor whether to include window decors, like the status or navigation bar
2863 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002864 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002865 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002866 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002867 boolean wallpaperOnly, boolean includeDecor) {
2868 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2869 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002870 if (bitmap == null) {
2871 return null;
2872 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002873
2874 if (DEBUG_SCREENSHOT) {
2875 // TEST IF IT's ALL BLACK
2876 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2877 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2878 bitmap.getHeight());
2879 boolean allBlack = true;
2880 final int firstColor = buffer[0];
2881 for (int i = 0; i < buffer.length; i++) {
2882 if (buffer[i] != firstColor) {
2883 allBlack = false;
2884 break;
2885 }
2886 }
2887 if (allBlack) {
2888 final WindowState appWin = mScreenshotApplicationState.appWin;
2889 final int maxLayer = mScreenshotApplicationState.maxLayer;
2890 final int minLayer = mScreenshotApplicationState.minLayer;
2891 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2892 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2893 (appWin != null ?
2894 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2895 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2896 }
2897 }
2898
2899 // Create a copy of the screenshot that is immutable and backed in ashmem.
2900 // This greatly reduces the overhead of passing the bitmap between processes.
2901 Bitmap ret = bitmap.createAshmemBitmap(config);
2902 bitmap.recycle();
2903 return ret;
2904 }
2905
2906 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2907 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2908 boolean includeDecor) {
2909 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2910 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2911 }
2912
2913 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2914 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2915 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002916 int dw = mDisplayInfo.logicalWidth;
2917 int dh = mDisplayInfo.logicalHeight;
2918 if (dw == 0 || dh == 0) {
2919 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2920 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2921 return null;
2922 }
2923
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002924 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002925
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002926 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002927 final Rect frame = new Rect();
2928 final Rect stackBounds = new Rect();
2929
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002930 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002931 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002932 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002933 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002934 if (!mService.mPolicy.isScreenOn()) {
2935 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
2936 + " was off.");
2937 return null;
2938 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002939 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002940 mScreenshotApplicationState.appWin = null;
2941 forAllWindows(w -> {
2942 if (!w.mHasSurface) {
2943 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002944 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002945 if (w.mLayer >= aboveAppLayer) {
2946 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002947 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002948 if (wallpaperOnly && !w.mIsWallpaper) {
2949 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002950 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002951 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02002952 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002953 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002954 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2955 // then the target window state is this one.
2956 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002957 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002958 }
2959
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002960 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002961 // We have not ran across the target window yet, so it is probably behind
2962 // the wallpaper. This can happen when the keyguard is up and all windows
2963 // are moved behind the wallpaper. We don't want to include the wallpaper
2964 // layer in the screenshot as it will cover-up the layer of the target
2965 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002966 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002967 }
2968 // Fall through. The target window is in front of the wallpaper. For this
2969 // case we want to include the wallpaper layer in the screenshot because
2970 // the target window might have some transparent areas.
2971 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002972 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002973 // This app window is of no interest if it is not associated with the
2974 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002975 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002976 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002977 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002978 }
2979
2980 // Include this window.
2981
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002982 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002983 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002984 if (mScreenshotApplicationState.maxLayer < layer) {
2985 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002986 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002987 if (mScreenshotApplicationState.minLayer > layer) {
2988 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002989 }
2990
2991 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02002992 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
2993 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02002994 final Task task = w.getTask();
2995 if (task != null) {
2996 task.getBounds(frame);
2997 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02002998
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02002999 // No task bounds? Too bad! Ain't no screenshot then.
3000 return true;
3001 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003002 } else {
3003 final Rect wf = w.mFrame;
3004 final Rect cr = w.mContentInsets;
3005 int left = wf.left + cr.left;
3006 int top = wf.top + cr.top;
3007 int right = wf.right - cr.right;
3008 int bottom = wf.bottom - cr.bottom;
3009 frame.union(left, top, right, bottom);
3010 w.getVisibleBounds(stackBounds);
3011 if (!Rect.intersects(frame, stackBounds)) {
3012 // Set frame empty if there's no intersection.
3013 frame.setEmpty();
3014 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003015 }
3016 }
3017
3018 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003019 (w.mAppToken != null && w.mAppToken.token == appToken)
3020 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01003021 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003022 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003023 }
3024
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003025 if (w.isObscuringDisplay()){
3026 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003027 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 return false;
3029 }, true /* traverseTopToBottom */);
3030
3031 final WindowState appWin = mScreenshotApplicationState.appWin;
3032 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3033 final int maxLayer = mScreenshotApplicationState.maxLayer;
3034 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003035
3036 if (appToken != null && appWin == null) {
3037 // Can't find a window to snapshot.
3038 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3039 "Screenshot: Couldn't find a surface matching " + appToken);
3040 return null;
3041 }
3042
3043 if (!screenshotReady) {
3044 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3045 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3046 appWin.mWinAnimator.mDrawState)));
3047 return null;
3048 }
3049
3050 // Screenshot is ready to be taken. Everything from here below will continue
3051 // through the bottom of the loop and return a value. We only stay in the loop
3052 // because we don't want to release the mWindowMap lock until the screenshot is
3053 // taken.
3054
3055 if (maxLayer == 0) {
3056 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3057 + ": returning null maxLayer=" + maxLayer);
3058 return null;
3059 }
3060
Jorim Jaggi02886a82016-12-06 09:10:06 -08003061 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003062 // Constrain frame to the screen size.
3063 if (!frame.intersect(0, 0, dw, dh)) {
3064 frame.setEmpty();
3065 }
3066 } else {
3067 // Caller just wants entire display.
3068 frame.set(0, 0, dw, dh);
3069 }
3070 if (frame.isEmpty()) {
3071 return null;
3072 }
3073
3074 if (width < 0) {
3075 width = (int) (frame.width() * frameScale);
3076 }
3077 if (height < 0) {
3078 height = (int) (frame.height() * frameScale);
3079 }
3080
3081 // Tell surface flinger what part of the image to crop. Take the top
3082 // right part of the application, and crop the larger dimension to fit.
3083 Rect crop = new Rect(frame);
3084 if (width / (float) frame.width() < height / (float) frame.height()) {
3085 int cropWidth = (int)((float)width / (float)height * frame.height());
3086 crop.right = crop.left + cropWidth;
3087 } else {
3088 int cropHeight = (int)((float)height / (float)width * frame.width());
3089 crop.bottom = crop.top + cropHeight;
3090 }
3091
3092 // The screenshot API does not apply the current screen rotation.
3093 int rot = mDisplay.getRotation();
3094
3095 if (rot == ROTATION_90 || rot == ROTATION_270) {
3096 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3097 }
3098
3099 // Surfaceflinger is not aware of orientation, so convert our logical
3100 // crop to surfaceflinger's portrait orientation.
3101 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3102
3103 if (DEBUG_SCREENSHOT) {
3104 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3105 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003106 forAllWindows(w -> {
3107 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3108 Slog.i(TAG_WM, w + ": " + w.mLayer
3109 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003110 + " surfaceLayer=" + ((controller == null)
3111 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003112 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003113 }
3114
3115 final ScreenRotationAnimation screenRotationAnimation =
3116 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3117 final boolean inRotation = screenRotationAnimation != null &&
3118 screenRotationAnimation.isAnimating();
3119 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3120 "Taking screenshot while rotating");
3121
3122 // We force pending transactions to flush before taking
3123 // the screenshot by pushing an empty synchronous transaction.
3124 SurfaceControl.openTransaction();
3125 SurfaceControl.closeTransactionSync();
3126
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003127 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003128 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003129 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003130 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3131 + ") to layer " + maxLayer);
3132 return null;
3133 }
3134 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003135 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003136 }
3137
3138 // TODO: Can this use createRotationMatrix()?
3139 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3140 if (rot == Surface.ROTATION_90) {
3141 final int tmp = crop.top;
3142 crop.top = dw - crop.right;
3143 crop.right = crop.bottom;
3144 crop.bottom = dw - crop.left;
3145 crop.left = tmp;
3146 } else if (rot == Surface.ROTATION_180) {
3147 int tmp = crop.top;
3148 crop.top = dh - crop.bottom;
3149 crop.bottom = dh - tmp;
3150 tmp = crop.right;
3151 crop.right = dw - crop.left;
3152 crop.left = dw - tmp;
3153 } else if (rot == Surface.ROTATION_270) {
3154 final int tmp = crop.top;
3155 crop.top = crop.left;
3156 crop.left = dh - crop.bottom;
3157 crop.bottom = crop.right;
3158 crop.right = dh - tmp;
3159 }
3160 }
3161
3162 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003163 // Used to indicate the layout is needed.
3164 mTmpWindow = null;
3165
3166 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003167 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003168 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003169 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003170 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003171 w.setDisplayLayoutNeeded();
3172 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003173 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003174
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003175 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003176 mService.mWindowPlacerLocked.performSurfacePlacement();
3177 }
3178 }
3179
Wale Ogunwale1666e312016-12-16 11:27:18 -08003180 void setExitingTokensHasVisible(boolean hasVisible) {
3181 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3182 mExitingTokens.get(i).hasVisible = hasVisible;
3183 }
3184
3185 // Initialize state of exiting applications.
3186 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3187 }
3188
3189 void removeExistingTokensIfPossible() {
3190 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3191 final WindowToken token = mExitingTokens.get(i);
3192 if (!token.hasVisible) {
3193 mExitingTokens.remove(i);
3194 }
3195 }
3196
3197 // Time to remove any exiting applications?
3198 mTaskStackContainers.removeExistingAppTokensIfPossible();
3199 }
3200
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003201 @Override
3202 void onDescendantOverrideConfigurationChanged() {
3203 setLayoutNeeded();
3204 mService.requestTraversal();
3205 }
3206
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003207 static final class TaskForResizePointSearchResult {
3208 boolean searchDone;
3209 Task taskForResize;
3210
3211 void reset() {
3212 searchDone = false;
3213 taskForResize = null;
3214 }
3215 }
Robert Carr3b716242016-08-16 16:02:21 -07003216
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003217 private static final class ApplySurfaceChangesTransactionState {
3218 boolean displayHasContent;
3219 boolean obscured;
3220 boolean syswin;
3221 boolean focusDisplayed;
3222 float preferredRefreshRate;
3223 int preferredModeId;
3224
3225 void reset() {
3226 displayHasContent = false;
3227 obscured = false;
3228 syswin = false;
3229 focusDisplayed = false;
3230 preferredRefreshRate = 0;
3231 preferredModeId = 0;
3232 }
3233 }
3234
3235 private static final class ScreenshotApplicationState {
3236 WindowState appWin;
3237 int maxLayer;
3238 int minLayer;
3239 boolean screenshotReady;
3240
3241 void reset(boolean screenshotReady) {
3242 appWin = null;
3243 maxLayer = 0;
3244 minLayer = 0;
3245 this.screenshotReady = screenshotReady;
3246 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3247 }
3248 }
3249
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003250 /**
3251 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3252 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3253 * homogeneous children type which is currently required by sub-classes of
3254 * {@link WindowContainer} class.
3255 */
3256 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3257
3258 int size() {
3259 return mChildren.size();
3260 }
3261
3262 E get(int index) {
3263 return mChildren.get(index);
3264 }
3265
3266 @Override
3267 boolean fillsParent() {
3268 return true;
3269 }
3270
3271 @Override
3272 boolean isVisible() {
3273 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003274 }
3275 }
3276
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003277 /**
3278 * Window container class that contains all containers on this display relating to Apps.
3279 * I.e Activities.
3280 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003281 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003282
Andrii Kulian839def92016-11-02 10:58:58 -07003283 /**
3284 * Adds the stack to this container.
3285 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3286 */
3287 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003288 if (stack.mStackId == HOME_STACK_ID) {
3289 if (mHomeStack != null) {
3290 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3291 }
3292 mHomeStack = stack;
3293 }
3294 addChild(stack, onTop);
3295 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003296 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003297
Andrii Kulian839def92016-11-02 10:58:58 -07003298 /** Removes the stack from its container and prepare for changing the parent. */
3299 void removeStackFromDisplay(TaskStack stack) {
3300 removeChild(stack);
3301 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003302 }
3303
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003304 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003305 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3306 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003307 addChild(stack, addIndex);
3308 setLayoutNeeded();
3309 }
3310
Bryce Lee00d586d2017-07-28 20:48:43 -07003311
3312 @Override
3313 boolean isOnTop() {
3314 // Considered always on top
3315 return true;
3316 }
3317
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003318 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003319 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3320 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3321 // This stack is always-on-top, override the default behavior.
3322 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3323
3324 // Moving to its current position, as we must call super but we don't want to
3325 // perform any meaningful action.
3326 final int currentPosition = mChildren.indexOf(child);
3327 super.positionChildAt(currentPosition, child, false /* includingParents */);
3328 return;
3329 }
3330
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003331 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3332 super.positionChildAt(targetPosition, child, includingParents);
3333
3334 setLayoutNeeded();
3335 }
3336
3337 /**
3338 * When stack is added or repositioned, find a proper position for it.
3339 * This will make sure that pinned stack always stays on top.
3340 * @param requestedPosition Position requested by caller.
3341 * @param stack Stack to be added or positioned.
3342 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3343 * @return The proper position for the stack.
3344 */
3345 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3346 final int topChildPosition = mChildren.size() - 1;
3347 boolean toTop = requestedPosition == POSITION_TOP;
3348 toTop |= adding ? requestedPosition >= topChildPosition + 1
3349 : requestedPosition >= topChildPosition;
3350 int targetPosition = requestedPosition;
3351
Bryce Lee48f4b572017-04-10 10:54:15 -07003352 if (toTop && stack.mStackId != PINNED_STACK_ID
3353 && getStackById(PINNED_STACK_ID) != null) {
3354 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003355 TaskStack topStack = mChildren.get(topChildPosition);
3356 if (topStack.mStackId != PINNED_STACK_ID) {
3357 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3358 }
3359
3360 // So, stack is moved just below the pinned stack.
3361 // When we're adding a new stack the target is the current pinned stack position.
3362 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003363 // stack, because WindowContainer#positionAt() first removes element and then adds
3364 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003365 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3366 }
3367
3368 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003369 }
3370
3371 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003372 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3373 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003374 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003375 if (super.forAllWindows(callback, traverseTopToBottom)) {
3376 return true;
3377 }
3378 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3379 return true;
3380 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003381 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003382 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3383 return true;
3384 }
3385 if (super.forAllWindows(callback, traverseTopToBottom)) {
3386 return true;
3387 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003388 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003389 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003390 }
3391
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003392 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003393 boolean traverseTopToBottom) {
3394 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3395 // app tokens.
3396 // TODO: Investigate if we need to continue to do this or if we can just process them
3397 // in-order.
3398 if (traverseTopToBottom) {
3399 for (int i = mChildren.size() - 1; i >= 0; --i) {
3400 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3401 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003402 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3403 traverseTopToBottom)) {
3404 return true;
3405 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003406 }
3407 }
3408 } else {
3409 final int count = mChildren.size();
3410 for (int i = 0; i < count; ++i) {
3411 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3412 final int appTokensCount = appTokens.size();
3413 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003414 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3415 traverseTopToBottom)) {
3416 return true;
3417 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003418 }
3419 }
3420 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003421 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003422 }
3423
Wale Ogunwale1666e312016-12-16 11:27:18 -08003424 void setExitingTokensHasVisible(boolean hasVisible) {
3425 for (int i = mChildren.size() - 1; i >= 0; --i) {
3426 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3427 for (int j = appTokens.size() - 1; j >= 0; --j) {
3428 appTokens.get(j).hasVisible = hasVisible;
3429 }
3430 }
3431 }
3432
3433 void removeExistingAppTokensIfPossible() {
3434 for (int i = mChildren.size() - 1; i >= 0; --i) {
3435 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3436 for (int j = appTokens.size() - 1; j >= 0; --j) {
3437 final AppWindowToken token = appTokens.get(j);
3438 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3439 && (!token.mIsExiting || token.isEmpty())) {
3440 // Make sure there is no animation running on this token, so any windows
3441 // associated with it will be removed as soon as their animations are
3442 // complete.
3443 token.mAppAnimator.clearAnimation();
3444 token.mAppAnimator.animating = false;
3445 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3446 "performLayout: App token exiting now removed" + token);
3447 token.removeIfPossible();
3448 }
3449 }
3450 }
3451 }
3452
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003453 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003454 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003455 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003456 // Apps and their containers are not allowed to specify an orientation while the
3457 // docked or freeform stack is visible...except for the home stack/task if the
3458 // docked stack is minimized and it actually set something.
3459 if (mHomeStack != null && mHomeStack.isVisible()
3460 && mDividerControllerLocked.isMinimizedDock()) {
3461 final int orientation = mHomeStack.getOrientation();
3462 if (orientation != SCREEN_ORIENTATION_UNSET) {
3463 return orientation;
3464 }
3465 }
3466 return SCREEN_ORIENTATION_UNSPECIFIED;
3467 }
3468
3469 final int orientation = super.getOrientation();
3470 if (orientation != SCREEN_ORIENTATION_UNSET
3471 && orientation != SCREEN_ORIENTATION_BEHIND) {
3472 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3473 "App is requesting an orientation, return " + orientation);
3474 return orientation;
3475 }
3476
3477 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003478 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003479 // The next app has not been requested to be visible, so we keep the current orientation
3480 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003481 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003482 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003483 }
3484
3485 /**
3486 * Window container class that contains all containers on this display that are not related to
3487 * Apps. E.g. status bar.
3488 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003489 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3490 /**
3491 * Compares two child window tokens returns -1 if the first is lesser than the second in
3492 * terms of z-order and 1 otherwise.
3493 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003494 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003495 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003496 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3497 token1.mOwnerCanManageAppTokens)
3498 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3499 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003500
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003501 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3502 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3503 return false;
3504 }
3505 final int req = w.mAttrs.screenOrientation;
3506 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3507 || req == SCREEN_ORIENTATION_UNSET) {
3508 return false;
3509 }
3510 return true;
3511 };
3512
Wale Ogunwale3a931692016-11-02 16:49:48 -07003513 private final String mName;
3514 NonAppWindowContainers(String name) {
3515 mName = name;
3516 }
3517
3518 void addChild(WindowToken token) {
3519 addChild(token, mWindowComparator);
3520 }
3521
3522 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003523 int getOrientation() {
3524 final WindowManagerPolicy policy = mService.mPolicy;
3525 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003526 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003527
3528 if (win != null) {
3529 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003530 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003531 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003532 if (mService.mKeyguardGoingAway) {
3533 // Keyguard can't affect the orientation if it is going away...
3534 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3535 return SCREEN_ORIENTATION_UNSET;
3536 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003537 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003538 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003539 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003540 }
3541
Andrii Kulian8ee72852017-03-10 10:36:45 -08003542 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003543
3544 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003545 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003546 }
3547
3548 return SCREEN_ORIENTATION_UNSET;
3549 }
3550
3551 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003552 String getName() {
3553 return mName;
3554 }
Robert Carr3b716242016-08-16 16:02:21 -07003555 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003556
3557 /**
3558 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3559 */
3560 @FunctionalInterface
3561 private interface Screenshoter<E> {
3562 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3563 boolean useIdentityTransform, int rotation);
3564 }
Craig Mautner59c00972012-07-30 12:10:24 -07003565}