blob: fbe6f94ddc3be15f1de6bb424d1d61cb3c7b3ae7 [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);
1076 w.mOrientationChanging = true;
1077 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;
1186 config.screenWidthDp =
1187 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1188 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1189 config.screenHeightDp =
1190 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1191 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001192
1193 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1194 final int leftInset = mTmpRect.left;
1195 final int topInset = mTmpRect.top;
1196 // appBounds at the root level should mirror the app screen size.
1197 config.setAppBounds(leftInset /*left*/, topInset /*top*/, leftInset + displayInfo.appWidth /*right*/,
1198 topInset + displayInfo.appHeight /*bottom*/);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001199 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1200 || displayInfo.rotation == Surface.ROTATION_270);
1201
1202 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1203 mDisplayMetrics.density, config);
1204
1205 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1206 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1207 ? Configuration.SCREENLAYOUT_ROUND_YES
1208 : Configuration.SCREENLAYOUT_ROUND_NO);
1209
1210 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1211 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1212 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1213 dh, mDisplayId);
1214 config.densityDpi = displayInfo.logicalDensityDpi;
1215
1216 config.colorMode =
1217 (displayInfo.isHdr()
1218 ? Configuration.COLOR_MODE_HDR_YES
1219 : Configuration.COLOR_MODE_HDR_NO)
1220 | (displayInfo.isWideColorGamut()
1221 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1222 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1223
1224 // Update the configuration based on available input devices, lid switch,
1225 // and platform configuration.
1226 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1227 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1228 config.navigation = Configuration.NAVIGATION_NONAV;
1229
1230 int keyboardPresence = 0;
1231 int navigationPresence = 0;
1232 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1233 final int len = devices != null ? devices.length : 0;
1234 for (int i = 0; i < len; i++) {
1235 InputDevice device = devices[i];
1236 if (!device.isVirtual()) {
1237 final int sources = device.getSources();
1238 final int presenceFlag = device.isExternal() ?
1239 WindowManagerPolicy.PRESENCE_EXTERNAL :
1240 WindowManagerPolicy.PRESENCE_INTERNAL;
1241
1242 // TODO(multi-display): Configure on per-display basis.
1243 if (mService.mIsTouchDevice) {
1244 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1245 InputDevice.SOURCE_TOUCHSCREEN) {
1246 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1247 }
1248 } else {
1249 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1250 }
1251
1252 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1253 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1254 navigationPresence |= presenceFlag;
1255 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1256 && config.navigation == Configuration.NAVIGATION_NONAV) {
1257 config.navigation = Configuration.NAVIGATION_DPAD;
1258 navigationPresence |= presenceFlag;
1259 }
1260
1261 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1262 config.keyboard = Configuration.KEYBOARD_QWERTY;
1263 keyboardPresence |= presenceFlag;
1264 }
1265 }
1266 }
1267
1268 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1269 config.navigation = Configuration.NAVIGATION_DPAD;
1270 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1271 }
1272
1273 // Determine whether a hard keyboard is available and enabled.
1274 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1275 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1276 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1277 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1278 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1279 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1280 }
1281
1282 // Let the policy update hidden states.
1283 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1284 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1285 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1286 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1287 }
1288
1289 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1290 int displayId) {
1291 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1292 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1293 final int unrotDw, unrotDh;
1294 if (rotated) {
1295 unrotDw = dh;
1296 unrotDh = dw;
1297 } else {
1298 unrotDw = dw;
1299 unrotDh = dh;
1300 }
1301 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1302 displayId);
1303 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1304 displayId);
1305 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1306 displayId);
1307 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1308 displayId);
1309 return sw;
1310 }
1311
1312 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1313 DisplayMetrics dm, int dw, int dh, int displayId) {
1314 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1315 displayId);
1316 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1317 uiMode, displayId);
1318 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1319 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1320 if (curSize == 0 || size < curSize) {
1321 curSize = size;
1322 }
1323 return curSize;
1324 }
1325
1326 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1327 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1328
1329 // We need to determine the smallest width that will occur under normal
1330 // operation. To this, start with the base screen size and compute the
1331 // width under the different possible rotations. We need to un-rotate
1332 // the current screen dimensions before doing this.
1333 int unrotDw, unrotDh;
1334 if (rotated) {
1335 unrotDw = dh;
1336 unrotDh = dw;
1337 } else {
1338 unrotDw = dw;
1339 unrotDh = dh;
1340 }
1341 displayInfo.smallestNominalAppWidth = 1<<30;
1342 displayInfo.smallestNominalAppHeight = 1<<30;
1343 displayInfo.largestNominalAppWidth = 0;
1344 displayInfo.largestNominalAppHeight = 0;
1345 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1346 unrotDh);
1347 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1348 unrotDw);
1349 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1350 unrotDh);
1351 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1352 unrotDw);
1353 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1354 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1355 displayId);
1356 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1357 displayId);
1358 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1359 displayId);
1360 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1361 displayId);
1362 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1363 outConfig.screenLayout = sl;
1364 }
1365
1366 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1367 int uiMode, int displayId) {
1368 // Get the app screen size at this rotation.
1369 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1370 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1371
1372 // Compute the screen layout size class for this rotation.
1373 int longSize = w;
1374 int shortSize = h;
1375 if (longSize < shortSize) {
1376 int tmp = longSize;
1377 longSize = shortSize;
1378 shortSize = tmp;
1379 }
1380 longSize = (int)(longSize/density);
1381 shortSize = (int)(shortSize/density);
1382 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1383 }
1384
1385 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1386 int uiMode, int dw, int dh) {
1387 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1388 displayId);
1389 if (width < displayInfo.smallestNominalAppWidth) {
1390 displayInfo.smallestNominalAppWidth = width;
1391 }
1392 if (width > displayInfo.largestNominalAppWidth) {
1393 displayInfo.largestNominalAppWidth = width;
1394 }
1395 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1396 displayId);
1397 if (height < displayInfo.smallestNominalAppHeight) {
1398 displayInfo.smallestNominalAppHeight = height;
1399 }
1400 if (height > displayInfo.largestNominalAppHeight) {
1401 displayInfo.largestNominalAppHeight = height;
1402 }
1403 }
1404
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001405 DockedStackDividerController getDockedDividerController() {
1406 return mDividerControllerLocked;
1407 }
1408
Winson Chung655332c2016-10-31 13:14:28 -07001409 PinnedStackController getPinnedStackController() {
1410 return mPinnedStackControllerLocked;
1411 }
1412
Jeff Browna506a6e2013-06-04 00:02:38 -07001413 /**
1414 * Returns true if the specified UID has access to this display.
1415 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001416 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001417 return mDisplay.hasAccess(uid);
1418 }
1419
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001420 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001421 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001422 }
1423
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001424 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001425 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001426 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001427 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001428 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001429 }
1430
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001431 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001432 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1433 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001434 if (stack.mStackId == stackId) {
1435 return stack;
1436 }
1437 }
1438 return null;
1439 }
1440
Bryce Lee48f4b572017-04-10 10:54:15 -07001441 @VisibleForTesting
1442 int getStackCount() {
1443 return mTaskStackContainers.size();
1444 }
1445
1446 @VisibleForTesting
1447 int getStaskPosById(int stackId) {
1448 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1449 final TaskStack stack = mTaskStackContainers.get(i);
1450 if (stack.mStackId == stackId) {
1451 return i;
1452 }
1453 }
1454 return -1;
1455 }
1456
Andrii Kulian441e4492016-09-29 15:25:00 -07001457 @Override
1458 void onConfigurationChanged(Configuration newParentConfig) {
1459 super.onConfigurationChanged(newParentConfig);
1460
Andrii Kulian3a507b52016-09-19 18:14:12 -07001461 // The display size information is heavily dependent on the resources in the current
1462 // configuration, so we need to reconfigure it every time the configuration changes.
1463 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1464 mService.reconfigureDisplayLocked(this);
1465
1466 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001467 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001468 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001469
Andrii Kulian441e4492016-09-29 15:25:00 -07001470 /**
1471 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1472 * bounds were updated.
1473 */
1474 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001475 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1476 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001477 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001478 changedStackList.add(stack.mStackId);
1479 }
1480 }
Winson Chung32c566f2017-04-11 18:31:21 -07001481
1482 // If there was no pinned stack, we still need to notify the controller of the display info
1483 // update as a result of the config change. We do this here to consolidate the flow between
1484 // changes when there is and is not a stack.
1485 if (getStackById(PINNED_STACK_ID) == null) {
1486 mPinnedStackControllerLocked.onDisplayInfoChanged();
1487 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001488 }
1489
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001490 @Override
1491 boolean fillsParent() {
1492 return true;
1493 }
1494
1495 @Override
1496 boolean isVisible() {
1497 return true;
1498 }
1499
1500 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001501 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001502 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001503 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001504 }
1505
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001506 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001507 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1508 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1509 // target, or here in order if there isn't an IME target.
1510 if (traverseTopToBottom) {
1511 for (int i = mChildren.size() - 1; i >= 0; --i) {
1512 final DisplayChildWindowContainer child = mChildren.get(i);
1513 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1514 // In this case the Ime windows will be processed above their target so we skip
1515 // here.
1516 continue;
1517 }
1518 if (child.forAllWindows(callback, traverseTopToBottom)) {
1519 return true;
1520 }
1521 }
1522 } else {
1523 final int count = mChildren.size();
1524 for (int i = 0; i < count; i++) {
1525 final DisplayChildWindowContainer child = mChildren.get(i);
1526 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1527 // In this case the Ime windows will be processed above their target so we skip
1528 // here.
1529 continue;
1530 }
1531 if (child.forAllWindows(callback, traverseTopToBottom)) {
1532 return true;
1533 }
1534 }
1535 }
1536 return false;
1537 }
1538
1539 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1540 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1541 }
1542
Wale Ogunwale399c8692017-05-08 14:22:42 -07001543 @Override
1544 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001545 final WindowManagerPolicy policy = mService.mPolicy;
1546
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001547 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001548 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001549 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001550 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001551 // If the display is frozen, some activities may be in the middle of restarting, and
1552 // thus have removed their old window. If the window has the flag to hide the lock
1553 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1554 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001555 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001556 } else if (policy.isKeyguardLocked()) {
1557 // Use the last orientation the while the display is frozen with the keyguard
1558 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1559 // window. We don't want to check the show when locked window directly though as
1560 // things aren't stable while the display is frozen, for example the window could be
1561 // momentarily unavailable due to activity relaunch.
1562 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001563 + "return " + mLastOrientation);
1564 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001565 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001566 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001567 final int orientation = mAboveAppWindowsContainers.getOrientation();
1568 if (orientation != SCREEN_ORIENTATION_UNSET) {
1569 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001570 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001571 }
1572
Wale Ogunwale399c8692017-05-08 14:22:42 -07001573 // Top system windows are not requesting an orientation. Start searching from apps.
1574 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001575 }
1576
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001577 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001578 // Check if display metrics changed and update base values if needed.
1579 updateBaseDisplayMetricsIfNeeded();
1580
Craig Mautner722285e2012-09-07 13:55:58 -07001581 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001582 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001583
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001584 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1585 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001586 }
Craig Mautner722285e2012-09-07 13:55:58 -07001587 }
1588
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001589 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001590 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1591 if (displayManagerInternal != null) {
1592 // Bootstrap the default logical display from the display manager.
1593 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1594 if (newDisplayInfo != null) {
1595 mDisplayInfo.copyFrom(newDisplayInfo);
1596 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001597 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001598
Andrii Kuliancd097992017-03-23 18:31:59 -07001599 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1600 mDisplayInfo.logicalDensityDpi);
1601 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1602 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1603 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001604 }
1605
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001606 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001607 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001608 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001609 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001610 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1611 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001612 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001613 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001614 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001615 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001616 out.set(left, top, left + width, top + height);
1617 }
1618
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001619 private void getLogicalDisplayRect(Rect out, int orientation) {
1620 getLogicalDisplayRect(out);
1621
1622 // Rotate the Rect if needed.
1623 final int currentRotation = mDisplayInfo.rotation;
1624 final int rotationDelta = deltaRotation(currentRotation, orientation);
1625 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1626 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1627 mTmpRectF.set(out);
1628 mTmpMatrix.mapRect(mTmpRectF);
1629 mTmpRectF.round(out);
1630 }
1631 }
1632
Andrii Kuliancd097992017-03-23 18:31:59 -07001633 /**
1634 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1635 * values.
1636 */
1637 private void updateBaseDisplayMetricsIfNeeded() {
1638 // Get real display metrics without overrides from WM.
1639 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1640 final int orientation = mDisplayInfo.rotation;
1641 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1642 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1643 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1644 final int newDensity = mDisplayInfo.logicalDensityDpi;
1645
1646 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1647 || mInitialDisplayHeight != newHeight
1648 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1649
1650 if (displayMetricsChanged) {
1651 // Check if display size or density is forced.
1652 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1653 || mBaseDisplayHeight != mInitialDisplayHeight;
1654 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1655
1656 // If there is an override set for base values - use it, otherwise use new values.
1657 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1658 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1659 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1660
1661 // Real display metrics changed, so we should also update initial values.
1662 mInitialDisplayWidth = newWidth;
1663 mInitialDisplayHeight = newHeight;
1664 mInitialDisplayDensity = newDensity;
1665 mService.reconfigureDisplayLocked(this);
1666 }
1667 }
1668
Bryce Lee27cec322017-03-21 09:41:37 -07001669 /** Sets the maximum width the screen resolution can be */
1670 void setMaxUiWidth(int width) {
1671 if (DEBUG_DISPLAY) {
1672 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1673 }
1674
1675 mMaxUiWidth = width;
1676
1677 // Update existing metrics.
1678 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1679 }
1680
1681 /** Update base (override) display metrics. */
1682 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1683 mBaseDisplayWidth = baseWidth;
1684 mBaseDisplayHeight = baseHeight;
1685 mBaseDisplayDensity = baseDensity;
1686
1687 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1688 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1689 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1690 mBaseDisplayWidth = mMaxUiWidth;
1691
1692 if (DEBUG_DISPLAY) {
1693 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1694 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1695 + " on display:" + getDisplayId());
1696 }
1697 }
1698
1699 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1700 }
1701
Chong Zhangf66db432016-01-13 10:39:51 -08001702 void getContentRect(Rect out) {
1703 out.set(mContentRect);
1704 }
1705
Wale Ogunwale1666e312016-12-16 11:27:18 -08001706 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1707 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1708 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001709
Wale Ogunwale1666e312016-12-16 11:27:18 -08001710 TaskStack stack = getStackById(stackId);
1711 if (stack != null) {
1712 // It's already attached to the display...clear mDeferRemoval and move stack to
1713 // appropriate z-order on display as needed.
1714 stack.mDeferRemoval = false;
1715 // We're not moving the display to front when we're adding stacks, only when
1716 // requested to change the position of stack explicitly.
1717 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1718 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001719 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001720 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001721 mTaskStackContainers.addStackToDisplay(stack, onTop);
1722 }
1723
Wale Ogunwale1666e312016-12-16 11:27:18 -08001724 if (stackId == DOCKED_STACK_ID) {
1725 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001726 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001727 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001728 }
1729
Andrii Kulian51c1b672017-04-07 18:39:32 -07001730 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001731 final DisplayContent prevDc = stack.getDisplayContent();
1732 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001733 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1734 + " which is not currently attached to any display");
1735 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001736 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001737 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1738 + " to its current displayId=" + mDisplayId);
1739 }
1740
Wale Ogunwale1666e312016-12-16 11:27:18 -08001741 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001742 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001743 }
1744
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001745 @Override
1746 protected void addChild(DisplayChildWindowContainer child,
1747 Comparator<DisplayChildWindowContainer> comparator) {
1748 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1749 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001750
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001751 @Override
1752 protected void addChild(DisplayChildWindowContainer child, int index) {
1753 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1754 }
1755
1756 @Override
1757 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001758 // Only allow removal of direct children from this display if the display is in the process
1759 // of been removed.
1760 if (mRemovingDisplay) {
1761 super.removeChild(child);
1762 return;
1763 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001764 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001765 }
1766
Andrii Kuliand2765632016-12-12 22:26:34 -08001767 @Override
1768 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1769 // Children of the display are statically ordered, so the real intention here is to perform
1770 // the operation on the display and not the static direct children.
1771 getParent().positionChildAt(position, this, includingParents);
1772 }
1773
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001774 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001775 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1776 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001777 final int taskId = stack.taskIdFromPoint(x, y);
1778 if (taskId != -1) {
1779 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001780 }
1781 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001782 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001783 }
1784
Chong Zhang8e89b312015-09-09 15:09:30 -07001785 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001786 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001787 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001788 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001789 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001790 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001791 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001792 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1793 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001794 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001795 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001796 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001797
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001798 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1799 if (mTmpTaskForResizePointSearchResult.searchDone) {
1800 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001801 }
1802 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001803 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001804 }
1805
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001806 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001807 // The provided task is the task on this display with focus, so if WindowManagerService's
1808 // focused app is not on this display, focusedTask will be null.
1809 if (focusedTask == null) {
1810 mTouchExcludeRegion.setEmpty();
1811 } else {
1812 mTouchExcludeRegion.set(mBaseDisplayRect);
1813 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1814 mTmpRect2.setEmpty();
1815 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1816 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1817 stack.setTouchExcludeRegion(
1818 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1819 }
1820 // If we removed the focused task above, add it back and only leave its
1821 // outside touch area in the exclusion. TapDectector is not interested in
1822 // any touch inside the focused task itself.
1823 if (!mTmpRect2.isEmpty()) {
1824 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1825 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001826 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001827 final WindowState inputMethod = mService.mInputMethodWindow;
1828 if (inputMethod != null && inputMethod.isVisibleLw()) {
1829 // If the input method is visible and the user is typing, we don't want these touch
1830 // events to be intercepted and used to change focus. This would likely cause a
1831 // disappearance of the input method.
1832 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001833 if (inputMethod.getDisplayId() == mDisplayId) {
1834 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1835 } else {
1836 // IME is on a different display, so we need to update its tap detector.
1837 // TODO(multidisplay): Remove when IME will always appear on same display.
1838 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1839 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001840 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001841 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1842 WindowState win = mTapExcludedWindows.get(i);
1843 win.getTouchableRegion(mTmpRegion);
1844 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1845 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001846 // TODO(multi-display): Support docked stacks on secondary displays.
1847 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001848 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001849 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001850 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1851 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001852 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001853 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001854 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001855 }
1856
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001857 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001858 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001859 super.switchUser();
1860 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001861 }
1862
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001863 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001864 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1865 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001866 }
1867 }
1868
1869 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001870 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001871 }
1872
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001873 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001874 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001875 }
1876
1877 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001878 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001879 }
1880
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001881 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001882 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001883 }
1884
Wale Ogunwale10124582016-09-15 20:25:50 -07001885 @Override
1886 void removeIfPossible() {
1887 if (isAnimating()) {
1888 mDeferredRemoval = true;
1889 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001890 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001891 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001892 }
1893
Wale Ogunwale10124582016-09-15 20:25:50 -07001894 @Override
1895 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001896 mRemovingDisplay = true;
1897 try {
1898 super.removeImmediately();
1899 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1900 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001901 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001902 mService.unregisterPointerEventListener(mTapDetector);
1903 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1904 }
1905 } finally {
1906 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001907 }
Craig Mautner95da1082014-02-24 17:54:35 -08001908 }
1909
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001910 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001911 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001912 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001913 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1914
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001915 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001916 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001917 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001918 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001919 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001920 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001921 }
1922
Andrii Kulian0214ed92017-05-16 13:44:05 -07001923 /** @return 'true' if removal of this display content is deferred due to active animation. */
1924 boolean isRemovalDeferred() {
1925 return mDeferredRemoval;
1926 }
1927
Wale Ogunwale10124582016-09-15 20:25:50 -07001928 boolean animateForIme(float interpolatedValue, float animationTarget,
1929 float dividerAnimationTarget) {
1930 boolean updated = false;
1931
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001932 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1933 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001934 if (stack == null || !stack.isAdjustedForIme()) {
1935 continue;
1936 }
1937
1938 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1939 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1940 updated = true;
1941 } else {
1942 mDividerControllerLocked.mLastAnimationProgress =
1943 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1944 mDividerControllerLocked.mLastDividerProgress =
1945 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1946 updated |= stack.updateAdjustForIme(
1947 mDividerControllerLocked.mLastAnimationProgress,
1948 mDividerControllerLocked.mLastDividerProgress,
1949 false /* force */);
1950 }
1951 if (interpolatedValue >= 1f) {
1952 stack.endImeAdjustAnimation();
1953 }
1954 }
1955
1956 return updated;
1957 }
1958
1959 boolean clearImeAdjustAnimation() {
1960 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001961 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1962 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001963 if (stack != null && stack.isAdjustedForIme()) {
1964 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1965 changed = true;
1966 }
1967 }
1968 return changed;
1969 }
1970
1971 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001972 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1973 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001974 if (stack.isVisible() && stack.isAdjustedForIme()) {
1975 stack.beginImeAdjustAnimation();
1976 }
1977 }
1978 }
1979
1980 void adjustForImeIfNeeded() {
1981 final WindowState imeWin = mService.mInputMethodWindow;
1982 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1983 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001984 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001985 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1986 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1987 imeTargetStack.getDockSide() : DOCKED_INVALID;
1988 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1989 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1990 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1991 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1992 final boolean imeHeightChanged = imeVisible &&
1993 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1994
1995 // The divider could be adjusted for IME position, or be thinner than usual,
1996 // or both. There are three possible cases:
1997 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1998 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1999 // - If IME is not visible, divider is not moved and is normal width.
2000
2001 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002002 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2003 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002004 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07002005 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
2006 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002007 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2008 } else {
2009 stack.resetAdjustedForIme(false);
2010 }
2011 }
2012 mDividerControllerLocked.setAdjustedForIme(
2013 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2014 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002015 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2016 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002017 stack.resetAdjustedForIme(!dockVisible);
2018 }
2019 mDividerControllerLocked.setAdjustedForIme(
2020 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2021 }
Winson Chung655332c2016-10-31 13:14:28 -07002022 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002023 }
2024
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002025 void setInputMethodAnimLayerAdjustment(int adj) {
2026 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2027 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002028 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002029 }
2030
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002031 /**
2032 * If a window that has an animation specifying a colored background and the current wallpaper
2033 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2034 * suddenly disappear.
2035 */
2036 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002037 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2038 w -> w.mIsWallpaper && w.isVisibleNow());
2039
2040 if (visibleWallpaper != null) {
2041 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002042 }
2043 return winAnimator.mAnimLayer;
2044 }
2045
Wale Ogunwale10124582016-09-15 20:25:50 -07002046 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002047 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2048 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002049 stack.prepareFreezingTaskBounds();
2050 }
2051 }
2052
Wale Ogunwale94744212015-09-21 19:01:47 -07002053 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002054 getLogicalDisplayRect(mTmpRect, newRotation);
2055
2056 // Compute a transform matrix to undo the coordinate space transformation,
2057 // and present the window at the same physical position it previously occupied.
2058 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2059 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2060
2061 mTmpRectF.set(bounds);
2062 mTmpMatrix.mapRect(mTmpRectF);
2063 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002064 }
2065
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002066 static int deltaRotation(int oldRotation, int newRotation) {
2067 int delta = newRotation - oldRotation;
2068 if (delta < 0) delta += 4;
2069 return delta;
2070 }
2071
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002072 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002073 Matrix outMatrix) {
2074 // For rotations without Z-ordering we don't need the target rectangle's position.
2075 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2076 displayHeight, outMatrix);
2077 }
2078
2079 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2080 float displayWidth, float displayHeight, Matrix outMatrix) {
2081 switch (rotation) {
2082 case ROTATION_0:
2083 outMatrix.reset();
2084 break;
2085 case ROTATION_270:
2086 outMatrix.setRotate(270, 0, 0);
2087 outMatrix.postTranslate(0, displayHeight);
2088 outMatrix.postTranslate(rectTop, 0);
2089 break;
2090 case ROTATION_180:
2091 outMatrix.reset();
2092 break;
2093 case ROTATION_90:
2094 outMatrix.setRotate(90, 0, 0);
2095 outMatrix.postTranslate(displayWidth, 0);
2096 outMatrix.postTranslate(-rectTop, rectLeft);
2097 break;
2098 }
2099 }
2100
Craig Mautnera91f9e22012-09-14 16:22:08 -07002101 public void dump(String prefix, PrintWriter pw) {
2102 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2103 final String subPrefix = " " + prefix;
2104 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2105 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2106 pw.print("dpi");
2107 if (mInitialDisplayWidth != mBaseDisplayWidth
2108 || mInitialDisplayHeight != mBaseDisplayHeight
2109 || mInitialDisplayDensity != mBaseDisplayDensity) {
2110 pw.print(" base=");
2111 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2112 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2113 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002114 if (mDisplayScalingDisabled) {
2115 pw.println(" noscale");
2116 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002117 pw.print(" cur=");
2118 pw.print(mDisplayInfo.logicalWidth);
2119 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2120 pw.print(" app=");
2121 pw.print(mDisplayInfo.appWidth);
2122 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2123 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2124 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2125 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2126 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002127 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2128 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002129
Craig Mautnerdc548482014-02-05 13:35:24 -08002130 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002131 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002132 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2133 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002134 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002135 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002136
Craig Mautnerdc548482014-02-05 13:35:24 -08002137 pw.println();
2138 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002139 pw.println();
2140 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002141 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002142 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002143 pw.print(" Exiting #"); pw.print(i);
2144 pw.print(' '); pw.print(token);
2145 pw.println(':');
2146 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002147 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002148 }
Craig Mautner59c00972012-07-30 12:10:24 -07002149 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002150 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002151 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002152 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002153 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002154 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002155
2156 if (mInputMethodAnimLayerAdjustment != 0) {
2157 pw.println(subPrefix
2158 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2159 }
Craig Mautner59c00972012-07-30 12:10:24 -07002160 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002161
2162 @Override
2163 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002164 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002165 }
2166
2167 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002168 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002169 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002170
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002171 /** Checks if stack with provided id is visible on this display. */
2172 boolean isStackVisible(int stackId) {
2173 final TaskStack stack = getStackById(stackId);
2174 return (stack != null && stack.isVisible());
2175 }
2176
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002177 /**
2178 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2179 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002180 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002181 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002182 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002183 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002184
2185 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002186 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002187 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002188 */
Jorim Jaggife762342016-10-13 14:33:27 +02002189 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002190 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002191 }
2192
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002193 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002194 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002195 final int x = (int) xf;
2196 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002197 final WindowState touchedWin = getWindow(w -> {
2198 final int flags = w.mAttrs.flags;
2199 if (!w.isVisibleLw()) {
2200 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002201 }
2202 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002203 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002204 }
2205
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002206 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002207 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002208 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002209 }
2210
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002211 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002212
2213 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002214 return mTmpRegion.contains(x, y) || touchFlags == 0;
2215 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002216
2217 return touchedWin;
2218 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002219
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002220 boolean canAddToastWindowForUid(int uid) {
2221 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002222 // Also if the app is focused adding more than one toast at
2223 // a time for better backwards compatibility.
2224 final WindowState focusedWindowForUid = getWindow(w ->
2225 w.mOwnerUid == uid && w.isFocused());
2226 if (focusedWindowForUid != null) {
2227 return true;
2228 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002229 final WindowState win = getWindow(w ->
2230 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2231 && !w.mWindowRemovalAllowed);
2232 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002233 }
2234
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002235 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002236 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2237 return;
2238 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002239
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002240 // Used to communicate the old focus to the callback method.
2241 mTmpWindow = oldFocus;
2242
2243 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002244 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002245
2246 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002247 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002248
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002249 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002250
2251 if (mTmpWindow == null) {
2252 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2253 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002254 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002255 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002256 }
2257
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002258 /** Updates the layer assignment of windows on this display. */
2259 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002260 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002261 if (setLayoutNeeded) {
2262 setLayoutNeeded();
2263 }
2264 }
2265
Wale Ogunwale1666e312016-12-16 11:27:18 -08002266 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2267 // moving containers or resizing them. Need to investigate the best way to have it automatically
2268 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002269 void layoutAndAssignWindowLayersIfNeeded() {
2270 mService.mWindowsChanged = true;
2271 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002272
2273 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2274 false /*updateInputWindows*/)) {
2275 assignWindowLayers(false /* setLayoutNeeded */);
2276 }
2277
2278 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2279 mService.mWindowPlacerLocked.performSurfacePlacement();
2280 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2281 }
2282
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002283 /** Returns true if a leaked surface was destroyed */
2284 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002285 // Used to indicate that a surface was leaked.
2286 mTmpWindow = null;
2287 forAllWindows(w -> {
2288 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002289 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002290 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002291 }
2292 if (!mService.mSessions.contains(wsa.mSession)) {
2293 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002294 + w + " surface=" + wsa.mSurfaceController
2295 + " token=" + w.mToken
2296 + " pid=" + w.mSession.mPid
2297 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002298 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002299 mService.mForceRemoves.add(w);
2300 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002301 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002302 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002303 + w + " surface=" + wsa.mSurfaceController
2304 + " token=" + w.mAppToken
2305 + " saved=" + w.hasSavedSurface());
2306 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002307 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002308 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002309 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002310 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002311
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002312 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002313 }
2314
2315 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002316 * Determine and return the window that should be the IME target.
2317 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2318 * @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 +00002319 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002320 WindowState computeImeTarget(boolean updateImeTarget) {
2321 if (mService.mInputMethodWindow == null) {
2322 // There isn't an IME so there shouldn't be a target...That was easy!
2323 if (updateImeTarget) {
2324 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2325 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2326 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2327 }
2328 return null;
2329 }
2330
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002331 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2332 // same display. Or even when the current IME/target are not on the same screen as the next
2333 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002334 mUpdateImeTarget = updateImeTarget;
2335 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002336
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002337
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002338 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2339 // to be on top of it, but it is not -really- where input will go. So look down below
2340 // for a real window to target...
2341 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2342 final AppWindowToken token = target.mAppToken;
2343 if (token != null) {
2344 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2345 if (betterTarget != null) {
2346 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002347 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002348 }
2349 }
2350
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002351 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2352 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002353
2354 // Now, a special case -- if the last target's window is in the process of exiting, and is
2355 // above the new target, keep on the last target to avoid flicker. Consider for example a
2356 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2357 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2358 // scrim.
2359 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002360 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2361 && (target == null
2362 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002363 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002364 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002365 }
2366
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002367 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2368 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002369
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002370 if (target == null) {
2371 if (updateImeTarget) {
2372 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2373 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2374 + Debug.getCallers(4) : ""));
2375 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2376 }
2377
2378 return null;
2379 }
2380
2381 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002382 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2383 if (token != null) {
2384
2385 // Now some fun for dealing with window animations that modify the Z order. We need
2386 // to look at all windows below the current target that are in this app, finding the
2387 // highest visible one in layering.
2388 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002389 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002390 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002391 }
2392
2393 if (highestTarget != null) {
2394 final AppTransition appTransition = mService.mAppTransition;
2395 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2396 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2397 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002399
2400 if (appTransition.isTransitionSet()) {
2401 // If we are currently setting up for an animation, hold everything until we
2402 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002403 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2404 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002405 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002406 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002407 // If the window we are currently targeting is involved with an animation,
2408 // and it is on top of the next target we will be over, then hold off on
2409 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002410 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2411 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002412 }
2413 }
2414 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002415
2416 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2417 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2418 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002419 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002420 }
2421
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002422 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002423 }
2424
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002425 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2426 if (target == mService.mInputMethodTarget
2427 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2428 && mInputMethodAnimLayerAdjustment == layerAdj) {
2429 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002430 }
2431
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002432 mService.mInputMethodTarget = target;
2433 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2434 setInputMethodAnimLayerAdjustment(layerAdj);
2435 assignWindowLayers(false /* setLayoutNeeded */);
2436 }
2437
2438 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2439 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2440 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2441 }
2442
2443 // Used to indicate we have reached the first window in the range we are interested in.
2444 mTmpWindow = null;
2445
2446 // TODO: Figure-out a more efficient way to do this.
2447 final WindowState candidate = getWindow(w -> {
2448 if (w == top) {
2449 // Reached the first window in the range we are interested in.
2450 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002451 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002452 if (mTmpWindow == null) {
2453 return false;
2454 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002455
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002456 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2457 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002458 }
2459 // If we reached the bottom of the range of windows we are considering,
2460 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002461 if (w == bottom) {
2462 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002463 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002464 return false;
2465 });
2466
2467 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002468 }
2469
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002470 void setLayoutNeeded() {
2471 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2472 mLayoutNeeded = true;
2473 }
2474
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002475 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002476 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2477 mLayoutNeeded = false;
2478 }
2479
2480 boolean isLayoutNeeded() {
2481 return mLayoutNeeded;
2482 }
2483
Wale Ogunwale02319a62016-09-26 15:21:22 -07002484 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2485 if (mTokenMap.isEmpty()) {
2486 return;
2487 }
2488 pw.println(" Display #" + mDisplayId);
2489 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2490 while (it.hasNext()) {
2491 final WindowToken token = it.next();
2492 pw.print(" ");
2493 pw.print(token);
2494 if (dumpAll) {
2495 pw.println(':');
2496 token.dump(pw, " ");
2497 } else {
2498 pw.println();
2499 }
2500 }
2501 }
2502
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002503 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002504 final int[] index = new int[1];
2505 forAllWindows(w -> {
2506 final WindowStateAnimator wAnim = w.mWinAnimator;
2507 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2508 index[0] = index[0] + 1;
2509 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002510 }
2511
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002512 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002513 forAllWindows(w -> {
2514 w.mWinAnimator.enableSurfaceTrace(fd);
2515 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002516 }
2517
2518 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002519 forAllWindows(w -> {
2520 w.mWinAnimator.disableSurfaceTrace();
2521 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002522 }
2523
Jorim Jaggife762342016-10-13 14:33:27 +02002524 /**
2525 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2526 */
2527 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2528 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002529 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002530 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2531 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002532 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002533 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2534 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002535 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002536 }
2537
Wale Ogunwale494009b82016-10-21 09:01:38 -07002538 boolean checkWaitingForWindows() {
2539
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002540 mHaveBootMsg = false;
2541 mHaveApp = false;
2542 mHaveWallpaper = false;
2543 mHaveKeyguard = true;
2544
2545 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002546 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2547 return true;
2548 }
2549 if (w.isDrawnLw()) {
2550 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002551 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002552 } else if (w.mAttrs.type == TYPE_APPLICATION
2553 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002554 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002555 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002556 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002557 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002558 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002559 }
2560 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002561 return false;
2562 });
2563
2564 if (visibleWindow != null) {
2565 // We have a visible window.
2566 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002567 }
2568
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002569 // if the wallpaper service is disabled on the device, we're never going to have
2570 // wallpaper, don't bother waiting for it
2571 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2572 com.android.internal.R.bool.config_enableWallpaperService)
2573 && !mService.mOnlyCore;
2574
Wale Ogunwale494009b82016-10-21 09:01:38 -07002575 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2576 "******** booted=" + mService.mSystemBooted
2577 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002578 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2579 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2580 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002581
2582 // If we are turning on the screen to show the boot message, don't do it until the boot
2583 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002584 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002585 return true;
2586 }
2587
2588 // If we are turning on the screen after the boot is completed normally, don't do so until
2589 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002590 if (mService.mSystemBooted
2591 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002592 return true;
2593 }
2594
2595 return false;
2596 }
2597
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002598 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002599 mTmpWindowAnimator = animator;
2600 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002601 }
2602
2603 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002604 resetAnimationBackgroundAnimator();
2605
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002606 // Used to indicate a detached wallpaper.
2607 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002608 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002609
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002610 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002611
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002612 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002613 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002614 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2615 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002616 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2617 }
2618 }
2619
2620 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002621 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002622 }
2623
Wale Ogunwale494009b82016-10-21 09:01:38 -07002624 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002625 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002626 if (imFocus == null) {
2627 return false;
2628 }
2629
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002630 if (DEBUG_INPUT_METHOD) {
2631 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2632 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2633 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002634 }
2635
Wale Ogunwale494009b82016-10-21 09:01:38 -07002636 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2637
2638 if (DEBUG_INPUT_METHOD) {
2639 Slog.i(TAG_WM, "IM target client: " + imeClient);
2640 if (imeClient != null) {
2641 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2642 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2643 }
2644 }
2645
2646 return imeClient != null && imeClient.asBinder() == client.asBinder();
2647 }
2648
2649 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650 final WindowState win = getWindow(
2651 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2652 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002653 }
2654
2655 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002656 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002657 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002658 final int curValue = w.mSystemUiVisibility;
2659 final int diff = (curValue ^ visibility) & globalDiff;
2660 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002661 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002662 w.mSeq++;
2663 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002664 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002665 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2666 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002667 visibility, newValue, diff);
2668 }
2669 } catch (RemoteException e) {
2670 // so sorry
2671 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002672 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002673 }
2674
2675 void onWindowFreezeTimeout() {
2676 Slog.w(TAG_WM, "Window freeze timeout expired.");
2677 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002678
2679 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002680 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002681 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002682 }
2683 w.mOrientationChanging = false;
2684 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2685 - mService.mDisplayFreezeTime);
2686 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002687 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002688 mService.mWindowPlacerLocked.performSurfacePlacement();
2689 }
2690
2691 void waitForAllWindowsDrawn() {
2692 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002693 forAllWindows(w -> {
2694 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2695 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2696 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002697 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002698 w.mLastContentInsets.set(-1, -1, -1, -1);
2699 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002700 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002701 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002702 }
2703
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002704 // TODO: Super crazy long method that should be broken down...
2705 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2706
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002707 final int dw = mDisplayInfo.logicalWidth;
2708 final int dh = mDisplayInfo.logicalHeight;
2709 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2710
2711 mTmpUpdateAllDrawn.clear();
2712
2713 int repeats = 0;
2714 do {
2715 repeats++;
2716 if (repeats > 6) {
2717 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2718 clearLayoutNeeded();
2719 break;
2720 }
2721
2722 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2723 pendingLayoutChanges);
2724
Andrii Kulian839def92016-11-02 10:58:58 -07002725 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2726 // the wallpaper window jumping across displays.
2727 // Remove check for default display when there will be support for multiple wallpaper
2728 // targets (on different displays).
2729 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002730 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002731 }
2732
2733 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2734 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2735 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2736 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002737 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002738 }
2739 }
2740
2741 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2742 setLayoutNeeded();
2743 }
2744
2745 // FIRST LOOP: Perform a layout, if needed.
2746 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2747 performLayout(repeats == 1, false /* updateInputWindows */);
2748 } else {
2749 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2750 }
2751
2752 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2753 pendingLayoutChanges = 0;
2754
2755 if (isDefaultDisplay) {
2756 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002757 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002758 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2759 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2760 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2761 }
2762 } while (pendingLayoutChanges != 0);
2763
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002764 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002765 resetDimming();
2766
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002767 mTmpRecoveringMemory = recoveringMemory;
2768 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002769
2770 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002771 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2772 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2773 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002774 true /* inTraversal, must call performTraversalInTrans... below */);
2775
2776 stopDimmingIfNeeded();
2777
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002778 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2779 if (wallpaperVisible != mLastWallpaperVisible) {
2780 mLastWallpaperVisible = wallpaperVisible;
2781 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2782 }
2783
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002784 while (!mTmpUpdateAllDrawn.isEmpty()) {
2785 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2786 // See if any windows have been drawn, so they (and others associated with them)
2787 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002788 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002789 }
2790
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002791 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002792 }
2793
2794 void performLayout(boolean initial, boolean updateInputWindows) {
2795 if (!isLayoutNeeded()) {
2796 return;
2797 }
2798 clearLayoutNeeded();
2799
2800 final int dw = mDisplayInfo.logicalWidth;
2801 final int dh = mDisplayInfo.logicalHeight;
2802
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002803 if (DEBUG_LAYOUT) {
2804 Slog.v(TAG, "-------------------------------------");
2805 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2806 }
2807
Andrii Kulian8ee72852017-03-10 10:36:45 -08002808 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002809 getConfiguration().uiMode);
2810 if (isDefaultDisplay) {
2811 // Not needed on non-default displays.
2812 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2813 mService.mScreenRect.set(0, 0, dw, dh);
2814 }
2815
2816 mService.mPolicy.getContentRectLw(mContentRect);
2817
2818 int seq = mService.mLayoutSeq + 1;
2819 if (seq < 0) seq = 0;
2820 mService.mLayoutSeq = seq;
2821
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002822 // Used to indicate that we have processed the dream window and all additional windows are
2823 // behind it.
2824 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002825 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002826
2827 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002828 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002829
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002830 // Used to indicate that we have processed the dream window and all additional attached
2831 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002832 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002833 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002834
2835 // Now perform layout of attached windows, which usually depend on the position of the
2836 // window they are attached to. XXX does not deal with windows that are attached to windows
2837 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002838 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002839
2840 // Window frames may have changed. Tell the input dispatcher about it.
2841 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2842 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2843 if (updateInputWindows) {
2844 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2845 }
2846
2847 mService.mPolicy.finishLayoutLw();
2848 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2849 }
2850
2851 /**
2852 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2853 * In portrait mode, it grabs the full screenshot.
2854 *
2855 * @param width the width of the target bitmap
2856 * @param height the height of the target bitmap
2857 * @param includeFullDisplay true if the screen should not be cropped before capture
2858 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2859 * @param config of the output bitmap
2860 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002861 * @param includeDecor whether to include window decors, like the status or navigation bar
2862 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002863 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002864 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002865 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002866 boolean wallpaperOnly, boolean includeDecor) {
2867 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2868 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002869 if (bitmap == null) {
2870 return null;
2871 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002872
2873 if (DEBUG_SCREENSHOT) {
2874 // TEST IF IT's ALL BLACK
2875 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2876 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2877 bitmap.getHeight());
2878 boolean allBlack = true;
2879 final int firstColor = buffer[0];
2880 for (int i = 0; i < buffer.length; i++) {
2881 if (buffer[i] != firstColor) {
2882 allBlack = false;
2883 break;
2884 }
2885 }
2886 if (allBlack) {
2887 final WindowState appWin = mScreenshotApplicationState.appWin;
2888 final int maxLayer = mScreenshotApplicationState.maxLayer;
2889 final int minLayer = mScreenshotApplicationState.minLayer;
2890 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2891 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2892 (appWin != null ?
2893 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2894 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2895 }
2896 }
2897
2898 // Create a copy of the screenshot that is immutable and backed in ashmem.
2899 // This greatly reduces the overhead of passing the bitmap between processes.
2900 Bitmap ret = bitmap.createAshmemBitmap(config);
2901 bitmap.recycle();
2902 return ret;
2903 }
2904
2905 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2906 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2907 boolean includeDecor) {
2908 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2909 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2910 }
2911
2912 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2913 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2914 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002915 int dw = mDisplayInfo.logicalWidth;
2916 int dh = mDisplayInfo.logicalHeight;
2917 if (dw == 0 || dh == 0) {
2918 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2919 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2920 return null;
2921 }
2922
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002923 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002924
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002925 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002926 final Rect frame = new Rect();
2927 final Rect stackBounds = new Rect();
2928
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002929 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002930 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002931 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002932 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002933 if (!mService.mPolicy.isScreenOn()) {
2934 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
2935 + " was off.");
2936 return null;
2937 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002938 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002939 mScreenshotApplicationState.appWin = null;
2940 forAllWindows(w -> {
2941 if (!w.mHasSurface) {
2942 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002943 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002944 if (w.mLayer >= aboveAppLayer) {
2945 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002946 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002947 if (wallpaperOnly && !w.mIsWallpaper) {
2948 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002949 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002950 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02002951 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002952 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002953 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2954 // then the target window state is this one.
2955 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002956 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002957 }
2958
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002959 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002960 // We have not ran across the target window yet, so it is probably behind
2961 // the wallpaper. This can happen when the keyguard is up and all windows
2962 // are moved behind the wallpaper. We don't want to include the wallpaper
2963 // layer in the screenshot as it will cover-up the layer of the target
2964 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002965 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966 }
2967 // Fall through. The target window is in front of the wallpaper. For this
2968 // case we want to include the wallpaper layer in the screenshot because
2969 // the target window might have some transparent areas.
2970 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002971 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972 // This app window is of no interest if it is not associated with the
2973 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002974 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002975 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002976 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002977 }
2978
2979 // Include this window.
2980
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002981 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002982 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002983 if (mScreenshotApplicationState.maxLayer < layer) {
2984 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002985 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002986 if (mScreenshotApplicationState.minLayer > layer) {
2987 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002988 }
2989
2990 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02002991 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
2992 if (includeDecor) {
2993 final TaskStack stack = w.getStack();
2994 if (stack != null) {
2995 stack.getBounds(frame);
2996 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02002997
Jorim Jaggi70f59482017-05-04 14:05:59 +02002998 // We want to screenshot with the exact bounds of the surface of the app. Thus,
2999 // intersect it with the frame.
3000 frame.intersect(w.mFrame);
3001 } else {
3002 final Rect wf = w.mFrame;
3003 final Rect cr = w.mContentInsets;
3004 int left = wf.left + cr.left;
3005 int top = wf.top + cr.top;
3006 int right = wf.right - cr.right;
3007 int bottom = wf.bottom - cr.bottom;
3008 frame.union(left, top, right, bottom);
3009 w.getVisibleBounds(stackBounds);
3010 if (!Rect.intersects(frame, stackBounds)) {
3011 // Set frame empty if there's no intersection.
3012 frame.setEmpty();
3013 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003014 }
3015 }
3016
3017 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003018 (w.mAppToken != null && w.mAppToken.token == appToken)
3019 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01003020 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003021 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003022 }
3023
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003024 if (w.isObscuringDisplay()){
3025 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003026 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003027 return false;
3028 }, true /* traverseTopToBottom */);
3029
3030 final WindowState appWin = mScreenshotApplicationState.appWin;
3031 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3032 final int maxLayer = mScreenshotApplicationState.maxLayer;
3033 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003034
3035 if (appToken != null && appWin == null) {
3036 // Can't find a window to snapshot.
3037 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3038 "Screenshot: Couldn't find a surface matching " + appToken);
3039 return null;
3040 }
3041
3042 if (!screenshotReady) {
3043 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3044 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3045 appWin.mWinAnimator.mDrawState)));
3046 return null;
3047 }
3048
3049 // Screenshot is ready to be taken. Everything from here below will continue
3050 // through the bottom of the loop and return a value. We only stay in the loop
3051 // because we don't want to release the mWindowMap lock until the screenshot is
3052 // taken.
3053
3054 if (maxLayer == 0) {
3055 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3056 + ": returning null maxLayer=" + maxLayer);
3057 return null;
3058 }
3059
Jorim Jaggi02886a82016-12-06 09:10:06 -08003060 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003061 // Constrain frame to the screen size.
3062 if (!frame.intersect(0, 0, dw, dh)) {
3063 frame.setEmpty();
3064 }
3065 } else {
3066 // Caller just wants entire display.
3067 frame.set(0, 0, dw, dh);
3068 }
3069 if (frame.isEmpty()) {
3070 return null;
3071 }
3072
3073 if (width < 0) {
3074 width = (int) (frame.width() * frameScale);
3075 }
3076 if (height < 0) {
3077 height = (int) (frame.height() * frameScale);
3078 }
3079
3080 // Tell surface flinger what part of the image to crop. Take the top
3081 // right part of the application, and crop the larger dimension to fit.
3082 Rect crop = new Rect(frame);
3083 if (width / (float) frame.width() < height / (float) frame.height()) {
3084 int cropWidth = (int)((float)width / (float)height * frame.height());
3085 crop.right = crop.left + cropWidth;
3086 } else {
3087 int cropHeight = (int)((float)height / (float)width * frame.width());
3088 crop.bottom = crop.top + cropHeight;
3089 }
3090
3091 // The screenshot API does not apply the current screen rotation.
3092 int rot = mDisplay.getRotation();
3093
3094 if (rot == ROTATION_90 || rot == ROTATION_270) {
3095 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3096 }
3097
3098 // Surfaceflinger is not aware of orientation, so convert our logical
3099 // crop to surfaceflinger's portrait orientation.
3100 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3101
3102 if (DEBUG_SCREENSHOT) {
3103 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3104 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003105 forAllWindows(w -> {
3106 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3107 Slog.i(TAG_WM, w + ": " + w.mLayer
3108 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003109 + " surfaceLayer=" + ((controller == null)
3110 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003111 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003112 }
3113
3114 final ScreenRotationAnimation screenRotationAnimation =
3115 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3116 final boolean inRotation = screenRotationAnimation != null &&
3117 screenRotationAnimation.isAnimating();
3118 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3119 "Taking screenshot while rotating");
3120
3121 // We force pending transactions to flush before taking
3122 // the screenshot by pushing an empty synchronous transaction.
3123 SurfaceControl.openTransaction();
3124 SurfaceControl.closeTransactionSync();
3125
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003126 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003127 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003128 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003129 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3130 + ") to layer " + maxLayer);
3131 return null;
3132 }
3133 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003134 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003135 }
3136
3137 // TODO: Can this use createRotationMatrix()?
3138 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3139 if (rot == Surface.ROTATION_90) {
3140 final int tmp = crop.top;
3141 crop.top = dw - crop.right;
3142 crop.right = crop.bottom;
3143 crop.bottom = dw - crop.left;
3144 crop.left = tmp;
3145 } else if (rot == Surface.ROTATION_180) {
3146 int tmp = crop.top;
3147 crop.top = dh - crop.bottom;
3148 crop.bottom = dh - tmp;
3149 tmp = crop.right;
3150 crop.right = dw - crop.left;
3151 crop.left = dw - tmp;
3152 } else if (rot == Surface.ROTATION_270) {
3153 final int tmp = crop.top;
3154 crop.top = crop.left;
3155 crop.left = dh - crop.bottom;
3156 crop.bottom = crop.right;
3157 crop.right = dh - tmp;
3158 }
3159 }
3160
3161 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003162 // Used to indicate the layout is needed.
3163 mTmpWindow = null;
3164
3165 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003166 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003167 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003168 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003169 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003170 w.setDisplayLayoutNeeded();
3171 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003172 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003173
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003174 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003175 mService.mWindowPlacerLocked.performSurfacePlacement();
3176 }
3177 }
3178
Wale Ogunwale1666e312016-12-16 11:27:18 -08003179 void setExitingTokensHasVisible(boolean hasVisible) {
3180 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3181 mExitingTokens.get(i).hasVisible = hasVisible;
3182 }
3183
3184 // Initialize state of exiting applications.
3185 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3186 }
3187
3188 void removeExistingTokensIfPossible() {
3189 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3190 final WindowToken token = mExitingTokens.get(i);
3191 if (!token.hasVisible) {
3192 mExitingTokens.remove(i);
3193 }
3194 }
3195
3196 // Time to remove any exiting applications?
3197 mTaskStackContainers.removeExistingAppTokensIfPossible();
3198 }
3199
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003200 @Override
3201 void onDescendantOverrideConfigurationChanged() {
3202 setLayoutNeeded();
3203 mService.requestTraversal();
3204 }
3205
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003206 static final class TaskForResizePointSearchResult {
3207 boolean searchDone;
3208 Task taskForResize;
3209
3210 void reset() {
3211 searchDone = false;
3212 taskForResize = null;
3213 }
3214 }
Robert Carr3b716242016-08-16 16:02:21 -07003215
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003216 private static final class ApplySurfaceChangesTransactionState {
3217 boolean displayHasContent;
3218 boolean obscured;
3219 boolean syswin;
3220 boolean focusDisplayed;
3221 float preferredRefreshRate;
3222 int preferredModeId;
3223
3224 void reset() {
3225 displayHasContent = false;
3226 obscured = false;
3227 syswin = false;
3228 focusDisplayed = false;
3229 preferredRefreshRate = 0;
3230 preferredModeId = 0;
3231 }
3232 }
3233
3234 private static final class ScreenshotApplicationState {
3235 WindowState appWin;
3236 int maxLayer;
3237 int minLayer;
3238 boolean screenshotReady;
3239
3240 void reset(boolean screenshotReady) {
3241 appWin = null;
3242 maxLayer = 0;
3243 minLayer = 0;
3244 this.screenshotReady = screenshotReady;
3245 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3246 }
3247 }
3248
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003249 /**
3250 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3251 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3252 * homogeneous children type which is currently required by sub-classes of
3253 * {@link WindowContainer} class.
3254 */
3255 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3256
3257 int size() {
3258 return mChildren.size();
3259 }
3260
3261 E get(int index) {
3262 return mChildren.get(index);
3263 }
3264
3265 @Override
3266 boolean fillsParent() {
3267 return true;
3268 }
3269
3270 @Override
3271 boolean isVisible() {
3272 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003273 }
3274 }
3275
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003276 /**
3277 * Window container class that contains all containers on this display relating to Apps.
3278 * I.e Activities.
3279 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003280 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003281
Andrii Kulian839def92016-11-02 10:58:58 -07003282 /**
3283 * Adds the stack to this container.
3284 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3285 */
3286 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003287 if (stack.mStackId == HOME_STACK_ID) {
3288 if (mHomeStack != null) {
3289 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3290 }
3291 mHomeStack = stack;
3292 }
3293 addChild(stack, onTop);
3294 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003295 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003296
Andrii Kulian839def92016-11-02 10:58:58 -07003297 /** Removes the stack from its container and prepare for changing the parent. */
3298 void removeStackFromDisplay(TaskStack stack) {
3299 removeChild(stack);
3300 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003301 }
3302
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003303 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003304 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3305 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003306 addChild(stack, addIndex);
3307 setLayoutNeeded();
3308 }
3309
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003310 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003311 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3312 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3313 // This stack is always-on-top, override the default behavior.
3314 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3315
3316 // Moving to its current position, as we must call super but we don't want to
3317 // perform any meaningful action.
3318 final int currentPosition = mChildren.indexOf(child);
3319 super.positionChildAt(currentPosition, child, false /* includingParents */);
3320 return;
3321 }
3322
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003323 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3324 super.positionChildAt(targetPosition, child, includingParents);
3325
3326 setLayoutNeeded();
3327 }
3328
3329 /**
3330 * When stack is added or repositioned, find a proper position for it.
3331 * This will make sure that pinned stack always stays on top.
3332 * @param requestedPosition Position requested by caller.
3333 * @param stack Stack to be added or positioned.
3334 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3335 * @return The proper position for the stack.
3336 */
3337 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3338 final int topChildPosition = mChildren.size() - 1;
3339 boolean toTop = requestedPosition == POSITION_TOP;
3340 toTop |= adding ? requestedPosition >= topChildPosition + 1
3341 : requestedPosition >= topChildPosition;
3342 int targetPosition = requestedPosition;
3343
Bryce Lee48f4b572017-04-10 10:54:15 -07003344 if (toTop && stack.mStackId != PINNED_STACK_ID
3345 && getStackById(PINNED_STACK_ID) != null) {
3346 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003347 TaskStack topStack = mChildren.get(topChildPosition);
3348 if (topStack.mStackId != PINNED_STACK_ID) {
3349 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3350 }
3351
3352 // So, stack is moved just below the pinned stack.
3353 // When we're adding a new stack the target is the current pinned stack position.
3354 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003355 // stack, because WindowContainer#positionAt() first removes element and then adds
3356 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003357 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3358 }
3359
3360 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003361 }
3362
3363 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003364 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3365 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003366 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003367 if (super.forAllWindows(callback, traverseTopToBottom)) {
3368 return true;
3369 }
3370 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3371 return true;
3372 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003373 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003374 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3375 return true;
3376 }
3377 if (super.forAllWindows(callback, traverseTopToBottom)) {
3378 return true;
3379 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003380 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003381 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003382 }
3383
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003384 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003385 boolean traverseTopToBottom) {
3386 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3387 // app tokens.
3388 // TODO: Investigate if we need to continue to do this or if we can just process them
3389 // in-order.
3390 if (traverseTopToBottom) {
3391 for (int i = mChildren.size() - 1; i >= 0; --i) {
3392 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3393 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003394 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3395 traverseTopToBottom)) {
3396 return true;
3397 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003398 }
3399 }
3400 } else {
3401 final int count = mChildren.size();
3402 for (int i = 0; i < count; ++i) {
3403 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3404 final int appTokensCount = appTokens.size();
3405 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003406 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3407 traverseTopToBottom)) {
3408 return true;
3409 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003410 }
3411 }
3412 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003413 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003414 }
3415
Wale Ogunwale1666e312016-12-16 11:27:18 -08003416 void setExitingTokensHasVisible(boolean hasVisible) {
3417 for (int i = mChildren.size() - 1; i >= 0; --i) {
3418 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3419 for (int j = appTokens.size() - 1; j >= 0; --j) {
3420 appTokens.get(j).hasVisible = hasVisible;
3421 }
3422 }
3423 }
3424
3425 void removeExistingAppTokensIfPossible() {
3426 for (int i = mChildren.size() - 1; i >= 0; --i) {
3427 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3428 for (int j = appTokens.size() - 1; j >= 0; --j) {
3429 final AppWindowToken token = appTokens.get(j);
3430 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3431 && (!token.mIsExiting || token.isEmpty())) {
3432 // Make sure there is no animation running on this token, so any windows
3433 // associated with it will be removed as soon as their animations are
3434 // complete.
3435 token.mAppAnimator.clearAnimation();
3436 token.mAppAnimator.animating = false;
3437 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3438 "performLayout: App token exiting now removed" + token);
3439 token.removeIfPossible();
3440 }
3441 }
3442 }
3443 }
3444
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003445 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003446 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003447 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003448 // Apps and their containers are not allowed to specify an orientation while the
3449 // docked or freeform stack is visible...except for the home stack/task if the
3450 // docked stack is minimized and it actually set something.
3451 if (mHomeStack != null && mHomeStack.isVisible()
3452 && mDividerControllerLocked.isMinimizedDock()) {
3453 final int orientation = mHomeStack.getOrientation();
3454 if (orientation != SCREEN_ORIENTATION_UNSET) {
3455 return orientation;
3456 }
3457 }
3458 return SCREEN_ORIENTATION_UNSPECIFIED;
3459 }
3460
3461 final int orientation = super.getOrientation();
3462 if (orientation != SCREEN_ORIENTATION_UNSET
3463 && orientation != SCREEN_ORIENTATION_BEHIND) {
3464 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3465 "App is requesting an orientation, return " + orientation);
3466 return orientation;
3467 }
3468
3469 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003470 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003471 // The next app has not been requested to be visible, so we keep the current orientation
3472 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003473 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003474 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003475 }
3476
3477 /**
3478 * Window container class that contains all containers on this display that are not related to
3479 * Apps. E.g. status bar.
3480 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003481 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3482 /**
3483 * Compares two child window tokens returns -1 if the first is lesser than the second in
3484 * terms of z-order and 1 otherwise.
3485 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003486 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003487 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003488 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3489 token1.mOwnerCanManageAppTokens)
3490 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3491 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003492
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003493 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3494 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3495 return false;
3496 }
3497 final int req = w.mAttrs.screenOrientation;
3498 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3499 || req == SCREEN_ORIENTATION_UNSET) {
3500 return false;
3501 }
3502 return true;
3503 };
3504
Wale Ogunwale3a931692016-11-02 16:49:48 -07003505 private final String mName;
3506 NonAppWindowContainers(String name) {
3507 mName = name;
3508 }
3509
3510 void addChild(WindowToken token) {
3511 addChild(token, mWindowComparator);
3512 }
3513
3514 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003515 int getOrientation() {
3516 final WindowManagerPolicy policy = mService.mPolicy;
3517 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003518 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003519
3520 if (win != null) {
3521 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003522 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003523 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003524 if (mService.mKeyguardGoingAway) {
3525 // Keyguard can't affect the orientation if it is going away...
3526 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3527 return SCREEN_ORIENTATION_UNSET;
3528 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003529 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003530 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003531 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003532 }
3533
Andrii Kulian8ee72852017-03-10 10:36:45 -08003534 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003535
3536 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003537 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003538 }
3539
3540 return SCREEN_ORIENTATION_UNSET;
3541 }
3542
3543 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003544 String getName() {
3545 return mName;
3546 }
Robert Carr3b716242016-08-16 16:02:21 -07003547 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003548
3549 /**
3550 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3551 */
3552 @FunctionalInterface
3553 private interface Screenshoter<E> {
3554 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3555 boolean useIdentityTransform, int rotation);
3556 }
Craig Mautner59c00972012-07-30 12:10:24 -07003557}