blob: da524c764ebaa90983fce1d77513dbe744e01810 [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
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800128import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700129import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700130
Robert Carr3b716242016-08-16 16:02:21 -0700131import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700132import java.io.PrintWriter;
133import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700134import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700135import java.util.HashMap;
136import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700137import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700138import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800139import java.util.function.Consumer;
140import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700141
Craig Mautner59c00972012-07-30 12:10:24 -0700142/**
143 * Utility class for keeping track of the WindowStates and other pertinent contents of a
144 * particular Display.
145 *
146 * IMPORTANT: No method from this class should ever be used without holding
147 * WindowManagerService.mWindowMap.
148 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700149class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700150 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700151
152 /** Unique identifier of this stack. */
153 private final int mDisplayId;
154
Wale Ogunwale3a931692016-11-02 16:49:48 -0700155 /** The containers below are the only child containers the display can have. */
156 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700157 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700158 // Contains all non-app window containers that should be displayed above the app containers
159 // (e.g. Status bar)
160 private final NonAppWindowContainers mAboveAppWindowsContainers =
161 new NonAppWindowContainers("mAboveAppWindowsContainers");
162 // Contains all non-app window containers that should be displayed below the app containers
163 // (e.g. Wallpaper).
164 private final NonAppWindowContainers mBelowAppWindowsContainers =
165 new NonAppWindowContainers("mBelowAppWindowsContainers");
166 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
167 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
168 // window containers together and move them in-sync if/when needed.
169 private final NonAppWindowContainers mImeWindowsContainers =
170 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700171
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000172 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800173 private WindowState mTmpWindow2;
174 private WindowAnimator mTmpWindowAnimator;
175 private boolean mTmpRecoveringMemory;
176 private boolean mUpdateImeTarget;
177 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700178 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700179
Wale Ogunwale02319a62016-09-26 15:21:22 -0700180 // Mapping from a token IBinder to a WindowToken object on this display.
181 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
182
Andrii Kuliancd097992017-03-23 18:31:59 -0700183 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700184 int mInitialDisplayWidth = 0;
185 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700186 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700187
188 /**
189 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
190 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
191 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
192 */
Craig Mautner59c00972012-07-30 12:10:24 -0700193 int mBaseDisplayWidth = 0;
194 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700195 /**
196 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
197 * but can be set from Settings or via shell command "adb shell wm density".
198 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
199 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700200 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700201 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700202 private final DisplayInfo mDisplayInfo = new DisplayInfo();
203 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700204 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700205 /**
206 * For default display it contains real metrics, empty for others.
207 * @see WindowManagerService#createWatermarkInTransaction()
208 */
209 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
210 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
211 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
212 /**
213 * Compat metrics computed based on {@link #mDisplayMetrics}.
214 * @see #updateDisplayAndOrientation(int)
215 */
216 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
217
218 /** The desired scaling factor for compatible apps. */
219 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700220
Andrii Kulian8ee72852017-03-10 10:36:45 -0800221 /**
222 * Current rotation of the display.
223 * Constants as per {@link android.view.Surface.Rotation}.
224 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700225 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800226 */
227 private int mRotation = 0;
228 /**
229 * Last applied orientation of the display.
230 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
231 *
232 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
233 */
234 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
235 /**
236 * Flag indicating that the application is receiving an orientation that has different metrics
237 * than it expected. E.g. Portrait instead of Landscape.
238 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700239 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800240 */
241 private boolean mAltOrientation = false;
242 /**
243 * Orientation forced by some window. If there is no visible window that specifies orientation
244 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
245 *
246 * @see NonAppWindowContainers#getOrientation()
247 */
248 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
249 /**
250 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
251 * occluded.
252 *
253 * @see NonAppWindowContainers#getOrientation()
254 */
255 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
256
Andrii Kulian06d07d62017-03-14 11:11:47 -0700257 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700258 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700259
Craig Mautner39834192012-09-02 07:47:24 -0700260 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700261 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700262 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700263 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800264 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700265
Craig Mautnerdc548482014-02-05 13:35:24 -0800266 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700267 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800268
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800269 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
270 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700271 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700272
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700273 /** Detect user tapping outside of current focused task bounds .*/
274 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700275
Craig Mautner6601b7b2013-04-29 10:29:11 -0700276 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700277 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700278
Craig Mautner6601b7b2013-04-29 10:29:11 -0700279 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800280 private final Rect mTmpRect = new Rect();
281 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700282 private final RectF mTmpRectF = new RectF();
283 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800284 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700285
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800286 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700287
Craig Mautner95da1082014-02-24 17:54:35 -0800288 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700289 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800290
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700291 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700292 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700293
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800294 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700295
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800296 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
297
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000298 private boolean mHaveBootMsg = false;
299 private boolean mHaveApp = false;
300 private boolean mHaveWallpaper = false;
301 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700302
303 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
304
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700305 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
306 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000307 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
308 new ApplySurfaceChangesTransactionState();
309 private final ScreenshotApplicationState mScreenshotApplicationState =
310 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700311
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700312 // True if this display is in the process of being removed. Used to determine if the removal of
313 // the display's direct children should be allowed.
314 private boolean mRemovingDisplay = false;
315
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700316 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800317 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700318 int mInputMethodAnimLayerAdjustment;
319
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800320 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
321 WindowStateAnimator winAnimator = w.mWinAnimator;
322 if (winAnimator.hasSurface()) {
323 final boolean wasAnimating = winAnimator.mWasAnimating;
324 final boolean nowAnimating = winAnimator.stepAnimationLocked(
325 mTmpWindowAnimator.mCurrentTime);
326 winAnimator.mWasAnimating = nowAnimating;
327 mTmpWindowAnimator.orAnimating(nowAnimating);
328
329 if (DEBUG_WALLPAPER) Slog.v(TAG,
330 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
331
332 if (wasAnimating && !winAnimator.mAnimating
333 && mWallpaperController.isWallpaperTarget(w)) {
334 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
335 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
336 if (DEBUG_LAYOUT_REPEATS) {
337 mService.mWindowPlacerLocked.debugLayoutRepeats(
338 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
339 }
340 }
341 }
342
343 final AppWindowToken atoken = w.mAppToken;
344 if (winAnimator.mDrawState == READY_TO_SHOW) {
345 if (atoken == null || atoken.allDrawn) {
346 if (w.performShowLocked()) {
347 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
348 if (DEBUG_LAYOUT_REPEATS) {
349 mService.mWindowPlacerLocked.debugLayoutRepeats(
350 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
351 }
352 }
353 }
354 }
355 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
356 if (appAnimator != null && appAnimator.thumbnail != null) {
357 if (appAnimator.thumbnailTransactionSeq
358 != mTmpWindowAnimator.mAnimTransactionSequence) {
359 appAnimator.thumbnailTransactionSeq =
360 mTmpWindowAnimator.mAnimTransactionSequence;
361 appAnimator.thumbnailLayer = 0;
362 }
363 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
364 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
365 }
366 }
367 };
368
369 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
370 final WindowStateAnimator winAnimator = w.mWinAnimator;
371 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
372 return;
373 }
374
375 final int flags = w.mAttrs.flags;
376
377 // If this window is animating, make a note that we have an animating window and take
378 // care of a request to run a detached wallpaper animation.
379 if (winAnimator.mAnimating) {
380 if (winAnimator.mAnimation != null) {
381 if ((flags & FLAG_SHOW_WALLPAPER) != 0
382 && winAnimator.mAnimation.getDetachWallpaper()) {
383 mTmpWindow = w;
384 }
385 final int color = winAnimator.mAnimation.getBackgroundColor();
386 if (color != 0) {
387 final TaskStack stack = w.getStack();
388 if (stack != null) {
389 stack.setAnimationBackground(winAnimator, color);
390 }
391 }
392 }
393 mTmpWindowAnimator.setAnimating(true);
394 }
395
396 // If this window's app token is running a detached wallpaper animation, make a note so
397 // we can ensure the wallpaper is displayed behind it.
398 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
399 if (appAnimator != null && appAnimator.animation != null
400 && appAnimator.animating) {
401 if ((flags & FLAG_SHOW_WALLPAPER) != 0
402 && appAnimator.animation.getDetachWallpaper()) {
403 mTmpWindow = w;
404 }
405
406 final int color = appAnimator.animation.getBackgroundColor();
407 if (color != 0) {
408 final TaskStack stack = w.getStack();
409 if (stack != null) {
410 stack.setAnimationBackground(winAnimator, color);
411 }
412 }
413 }
414 };
415
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800416 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
417 final int lostFocusUid = mTmpWindow.mOwnerUid;
418 final Handler handler = mService.mH;
419 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
420 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
421 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
422 w.mAttrs.hideTimeoutMilliseconds);
423 }
424 }
425 };
426
427 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
428 final AppWindowToken focusedApp = mService.mFocusedApp;
429 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
430 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
431
432 if (!w.canReceiveKeys()) {
433 return false;
434 }
435
436 final AppWindowToken wtoken = w.mAppToken;
437
438 // If this window's application has been removed, just skip it.
439 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
440 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
441 + (wtoken.removed ? "removed" : "sendingToBottom"));
442 return false;
443 }
444
445 if (focusedApp == null) {
446 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
447 + " using new focus @ " + w);
448 mTmpWindow = w;
449 return true;
450 }
451
452 if (!focusedApp.windowsAreFocusable()) {
453 // Current focused app windows aren't focusable...
454 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
455 + " focusable using new focus @ " + w);
456 mTmpWindow = w;
457 return true;
458 }
459
460 // Descend through all of the app tokens and find the first that either matches
461 // win.mAppToken (return win) or mFocusedApp (return null).
462 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
463 if (focusedApp.compareTo(wtoken) > 0) {
464 // App stack below focused app stack. No focus for you!!!
465 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
466 "findFocusedWindow: Reached focused app=" + focusedApp);
467 mTmpWindow = null;
468 return true;
469 }
470 }
471
472 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
473 mTmpWindow = w;
474 return true;
475 };
476
477 private final Consumer<WindowState> mPrepareWindowSurfaces =
478 w -> w.mWinAnimator.prepareSurfaceLocked(true);
479
480 private final Consumer<WindowState> mPerformLayout = w -> {
481 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
482 // wasting time and funky changes while a window is animating away.
483 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
484 || w.isGoneForLayoutLw();
485
486 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
487 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
488 + " mLayoutAttached=" + w.mLayoutAttached
489 + " screen changed=" + w.isConfigChanged());
490 final AppWindowToken atoken = w.mAppToken;
491 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
492 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
493 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
494 + " parentHidden=" + w.isParentWindowHidden());
495 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
496 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
497 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
498 + " parentHidden=" + w.isParentWindowHidden());
499 }
500
501 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
502 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
503 // since that means "perform layout as normal, just don't display").
504 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
505 || ((w.isConfigChanged() || w.setReportResizeHints())
506 && !w.isGoneForLayoutLw() &&
507 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
508 (w.mHasSurface && w.mAppToken != null &&
509 w.mAppToken.layoutConfigChanges)))) {
510 if (!w.mLayoutAttached) {
511 if (mTmpInitial) {
512 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
513 w.mContentChanged = false;
514 }
515 if (w.mAttrs.type == TYPE_DREAM) {
516 // Don't layout windows behind a dream, so that if it does stuff like hide
517 // the status bar we won't get a bad transition when it goes away.
518 mTmpWindow = w;
519 }
520 w.mLayoutNeeded = false;
521 w.prelayout();
522 mService.mPolicy.layoutWindowLw(w, null);
523 w.mLayoutSeq = mService.mLayoutSeq;
524
525 // Window frames may have changed. Update dim layer with the new bounds.
526 final Task task = w.getTask();
527 if (task != null) {
528 mDimLayerController.updateDimLayer(task);
529 }
530
531 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
532 + " mContainingFrame=" + w.mContainingFrame
533 + " mDisplayFrame=" + w.mDisplayFrame);
534 }
535 }
536 };
537
538 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
539 if (w.mLayoutAttached) {
540 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
541 + " mViewVisibility=" + w.mViewVisibility
542 + " mRelayoutCalled=" + w.mRelayoutCalled);
543 // If this view is GONE, then skip it -- keep the current frame, and let the caller
544 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
545 // windows, since that means "perform layout as normal, just don't display").
546 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
547 return;
548 }
549 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
550 || w.mLayoutNeeded) {
551 if (mTmpInitial) {
552 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
553 w.mContentChanged = false;
554 }
555 w.mLayoutNeeded = false;
556 w.prelayout();
557 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
558 w.mLayoutSeq = mService.mLayoutSeq;
559 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
560 + " mContainingFrame=" + w.mContainingFrame
561 + " mDisplayFrame=" + w.mDisplayFrame);
562 }
563 } else if (w.mAttrs.type == TYPE_DREAM) {
564 // Don't layout windows behind a dream, so that if it does stuff like hide the
565 // status bar we won't get a bad transition when it goes away.
566 mTmpWindow = mTmpWindow2;
567 }
568 };
569
570 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
571 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
572 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
573 return w.canBeImeTarget();
574 };
575
576 private final Consumer<WindowState> mApplyPostLayoutPolicy =
577 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
578 mService.mInputMethodTarget);
579
580 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
581 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
582 final boolean obscuredChanged = w.mObscured !=
583 mTmpApplySurfaceChangesTransactionState.obscured;
584 final RootWindowContainer root = mService.mRoot;
585 // Only used if default window
586 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
587
588 // Update effect.
589 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
590 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
591 final boolean isDisplayed = w.isDisplayedLw();
592
593 if (isDisplayed && w.isObscuringDisplay()) {
594 // This window completely covers everything behind it, so we want to leave all
595 // of them as undimmed (for performance reasons).
596 root.mObscuringWindow = w;
597 mTmpApplySurfaceChangesTransactionState.obscured = true;
598 }
599
600 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
601 root.handleNotObscuredLocked(w,
602 mTmpApplySurfaceChangesTransactionState.obscured,
603 mTmpApplySurfaceChangesTransactionState.syswin);
604
605 if (w.mHasSurface && isDisplayed) {
606 final int type = w.mAttrs.type;
607 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
608 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
609 mTmpApplySurfaceChangesTransactionState.syswin = true;
610 }
611 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
612 && w.mAttrs.preferredRefreshRate != 0) {
613 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
614 = w.mAttrs.preferredRefreshRate;
615 }
616 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
617 && w.mAttrs.preferredDisplayModeId != 0) {
618 mTmpApplySurfaceChangesTransactionState.preferredModeId
619 = w.mAttrs.preferredDisplayModeId;
620 }
621 }
622 }
623
624 w.applyDimLayerIfNeeded();
625
626 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
627 && mWallpaperController.isWallpaperTarget(w)) {
628 // This is the wallpaper target and its obscured state changed... make sure the
629 // current wallpaper's visibility has been updated accordingly.
630 mWallpaperController.updateWallpaperVisibility();
631 }
632
633 w.handleWindowMovedIfNeeded();
634
635 final WindowStateAnimator winAnimator = w.mWinAnimator;
636
637 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
638 w.mContentChanged = false;
639
640 // Moved from updateWindowsAndWallpaperLocked().
641 if (w.mHasSurface) {
642 // Take care of the window being ready to display.
643 final boolean committed = winAnimator.commitFinishDrawingLocked();
644 if (isDefaultDisplay && committed) {
645 if (w.mAttrs.type == TYPE_DREAM) {
646 // HACK: When a dream is shown, it may at that point hide the lock screen.
647 // So we need to redo the layout to let the phone window manager make this
648 // happen.
649 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
650 if (DEBUG_LAYOUT_REPEATS) {
651 surfacePlacer.debugLayoutRepeats(
652 "dream and commitFinishDrawingLocked true",
653 pendingLayoutChanges);
654 }
655 }
656 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
657 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
658 "First draw done in potential wallpaper target " + w);
659 root.mWallpaperMayChange = true;
660 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
661 if (DEBUG_LAYOUT_REPEATS) {
662 surfacePlacer.debugLayoutRepeats(
663 "wallpaper and commitFinishDrawingLocked true",
664 pendingLayoutChanges);
665 }
666 }
667 }
668 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
669 // Updates the shown frame before we set up the surface. This is needed
670 // because the resizing could change the top-left position (in addition to
671 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
672 // position the surface.
673 //
674 // If an animation is being started, we can't call this method because the
675 // animation hasn't processed its initial transformation yet, but in general
676 // we do want to update the position if the window is animating.
677 winAnimator.computeShownFrameLocked();
678 }
679 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
680 }
681
682 final AppWindowToken atoken = w.mAppToken;
683 if (atoken != null) {
684 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
685 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
686 mTmpUpdateAllDrawn.add(atoken);
687 }
688 }
689
690 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
691 && w.isDisplayedLw()) {
692 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
693 }
694
695 w.updateResizingWindowIfNeeded();
696 };
697
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800698 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800699 * Create new {@link DisplayContent} instance, add itself to the root window container and
700 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700701 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800702 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700703 * @param layersController window layer controller used to assign layer to the windows on this
704 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700705 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
706 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700707 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700708 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700709 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800710
711 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
712 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
713 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
714 + " new=" + display);
715 }
716
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700717 mDisplay = display;
718 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700719 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700720 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700721 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700722 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700723 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700724 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700725 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800726 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700727 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800728 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700729
730 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700731 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700732 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700733 super.addChild(mAboveAppWindowsContainers, null);
734 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800735
736 // Add itself as a child to the root container.
737 mService.mRoot.addChild(this, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700738 }
739
740 int getDisplayId() {
741 return mDisplayId;
742 }
743
Wale Ogunwale02319a62016-09-26 15:21:22 -0700744 WindowToken getWindowToken(IBinder binder) {
745 return mTokenMap.get(binder);
746 }
747
748 AppWindowToken getAppWindowToken(IBinder binder) {
749 final WindowToken token = getWindowToken(binder);
750 if (token == null) {
751 return null;
752 }
753 return token.asAppWindowToken();
754 }
755
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800756 void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700757 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
758 if (dc != null) {
759 // We currently don't support adding a window token to the display if the display
760 // already has the binder mapped to another token. If there is a use case for supporting
761 // this moving forward we will either need to merge the WindowTokens some how or have
762 // the binder map to a list of window tokens.
763 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
764 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
765 }
766 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700767
768 if (token.asAppWindowToken() == null) {
769 // Add non-app token to container hierarchy on the display. App tokens are added through
770 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700771 switch (token.windowType) {
772 case TYPE_WALLPAPER:
773 mBelowAppWindowsContainers.addChild(token);
774 break;
775 case TYPE_INPUT_METHOD:
776 case TYPE_INPUT_METHOD_DIALOG:
777 mImeWindowsContainers.addChild(token);
778 break;
779 default:
780 mAboveAppWindowsContainers.addChild(token);
781 break;
782 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700783 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700784 }
785
786 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700787 final WindowToken token = mTokenMap.remove(binder);
788 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800789 token.setExiting();
790 }
791 return token;
792 }
793
794 /** Changes the display the input window token is housed on to this one. */
795 void reParentWindowToken(WindowToken token) {
796 final DisplayContent prevDc = token.getDisplayContent();
797 if (prevDc == this) {
798 return;
799 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800800 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
801 && token.asAppWindowToken() == null) {
802 // Removed the token from the map, but made sure it's not an app token before removing
803 // from parent.
804 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700805 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800806
807 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700808 }
809
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700810 void removeAppToken(IBinder binder) {
811 final WindowToken token = removeWindowToken(binder);
812 if (token == null) {
813 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
814 return;
815 }
816
817 final AppWindowToken appToken = token.asAppWindowToken();
818
819 if (appToken == null) {
820 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
821 return;
822 }
823
824 appToken.onRemovedFromDisplay();
825 }
826
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700827 Display getDisplay() {
828 return mDisplay;
829 }
830
Craig Mautner59c00972012-07-30 12:10:24 -0700831 DisplayInfo getDisplayInfo() {
832 return mDisplayInfo;
833 }
834
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700835 DisplayMetrics getDisplayMetrics() {
836 return mDisplayMetrics;
837 }
838
Andrii Kulian8ee72852017-03-10 10:36:45 -0800839 int getRotation() {
840 return mRotation;
841 }
842
843 void setRotation(int newRotation) {
844 mRotation = newRotation;
845 }
846
847 int getLastOrientation() {
848 return mLastOrientation;
849 }
850
851 void setLastOrientation(int orientation) {
852 mLastOrientation = orientation;
853 }
854
855 boolean getAltOrientation() {
856 return mAltOrientation;
857 }
858
859 void setAltOrientation(boolean altOrientation) {
860 mAltOrientation = altOrientation;
861 }
862
863 int getLastWindowForcedOrientation() {
864 return mLastWindowForcedOrientation;
865 }
866
Andrii Kulian06d07d62017-03-14 11:11:47 -0700867 /**
868 * Update rotation of the display.
869 *
870 * Returns true if the rotation has been changed. In this case YOU MUST CALL
871 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
872 */
873 boolean updateRotationUnchecked(boolean inTransaction) {
874 if (mService.mDeferredRotationPauseCount > 0) {
875 // Rotation updates have been paused temporarily. Defer the update until
876 // updates have been resumed.
877 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
878 return false;
879 }
880
881 ScreenRotationAnimation screenRotationAnimation =
882 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
883 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
884 // Rotation updates cannot be performed while the previous rotation change
885 // animation is still in progress. Skip this update. We will try updating
886 // again after the animation is finished and the display is unfrozen.
887 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
888 return false;
889 }
890 if (mService.mDisplayFrozen) {
891 // Even if the screen rotation animation has finished (e.g. isAnimating
892 // returns false), there is still some time where we haven't yet unfrozen
893 // the display. We also need to abort rotation here.
894 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
895 "Deferring rotation, still finishing previous rotation");
896 return false;
897 }
898
899 if (!mService.mDisplayEnabled) {
900 // No point choosing a rotation if the display is not enabled.
901 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
902 return false;
903 }
904
905 final int oldRotation = mRotation;
906 final int lastOrientation = mLastOrientation;
907 final boolean oldAltOrientation = mAltOrientation;
908 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700909 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
910 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700911
Robert Carr897215d2017-03-29 12:25:50 -0700912 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700913 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
914 if (seamlessRotated != null) {
915 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
916 // to complete (that is, waiting for windows to redraw). It's tempting to check
917 // w.mSeamlessRotationCount but that could be incorrect in the case of
918 // window-removal.
919 return false;
920 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700921 }
922
923 // TODO: Implement forced rotation changes.
924 // Set mAltOrientation to indicate that the application is receiving
925 // an orientation that has different metrics than it expected.
926 // eg. Portrait instead of Landscape.
927
928 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
929 lastOrientation, rotation);
930
931 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
932 + ", got rotation " + rotation + " which has "
933 + (altOrientation ? "incompatible" : "compatible") + " metrics");
934
935 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
936 // No change.
937 return false;
938 }
939
940 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
941 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
942 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
943
944 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
945 mService.mWaitingForConfig = true;
946 }
947
948 mRotation = rotation;
949 mAltOrientation = altOrientation;
950 if (isDefaultDisplay) {
951 mService.mPolicy.setRotationLw(rotation);
952 }
953
954 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
955 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
956 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
957 WINDOW_FREEZE_TIMEOUT_DURATION);
958
959 setLayoutNeeded();
960 final int[] anim = new int[2];
961 if (isDimming()) {
962 anim[0] = anim[1] = 0;
963 } else {
964 mService.mPolicy.selectRotationAnimationLw(anim);
965 }
966
967 if (!rotateSeamlessly) {
968 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1]);
969 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
970 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
971 mDisplayId);
972 } else {
973 // The screen rotation animation uses a screenshot to freeze the screen
974 // while windows resize underneath.
975 // When we are rotating seamlessly, we allow the elements to transition
976 // to their rotated state independently and without a freeze required.
977 screenRotationAnimation = null;
978
979 // We have to reset this in case a window was removed before it
980 // finished seamless rotation.
981 mService.mSeamlessRotationCount = 0;
982 }
983
984 // We need to update our screen size information to match the new rotation. If the rotation
985 // has actually changed then this method will return true and, according to the comment at
986 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
987 // By updating the Display info here it will be available to
988 // #computeScreenConfiguration() later.
989 updateDisplayAndOrientation(getConfiguration().uiMode);
990
991 if (!inTransaction) {
992 if (SHOW_TRANSACTIONS) {
993 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
994 }
995 mService.openSurfaceTransaction();
996 }
997 try {
998 // NOTE: We disable the rotation in the emulator because
999 // it doesn't support hardware OpenGL emulation yet.
1000 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1001 && screenRotationAnimation.hasScreenshot()) {
1002 if (screenRotationAnimation.setRotationInTransaction(
1003 rotation, mService.mFxSession,
1004 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1005 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1006 mService.scheduleAnimationLocked();
1007 }
1008 }
1009
1010 if (rotateSeamlessly) {
1011 forAllWindows(w -> {
1012 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1013 }, true /* traverseTopToBottom */);
1014 }
1015
1016 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1017 } finally {
1018 if (!inTransaction) {
1019 mService.closeSurfaceTransaction();
1020 if (SHOW_LIGHT_TRANSACTIONS) {
1021 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1022 }
1023 }
1024 }
1025
1026 forAllWindows(w -> {
1027 // Discard surface after orientation change, these can't be reused.
1028 if (w.mAppToken != null) {
1029 w.mAppToken.destroySavedSurfaces();
1030 }
1031 if (w.mHasSurface && !rotateSeamlessly) {
1032 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
1033 w.mOrientationChanging = true;
1034 mService.mRoot.mOrientationChangeComplete = false;
1035 w.mLastFreezeDuration = 0;
1036 }
1037 w.mReportOrientationChanged = true;
1038 }, true /* traverseTopToBottom */);
1039
1040 if (rotateSeamlessly) {
1041 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1042 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1043 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1044 }
1045
1046 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1047 final WindowManagerService.RotationWatcher rotationWatcher
1048 = mService.mRotationWatchers.get(i);
1049 if (rotationWatcher.mDisplayId == mDisplayId) {
1050 try {
1051 rotationWatcher.mWatcher.onRotationChanged(rotation);
1052 } catch (RemoteException e) {
1053 // Ignore
1054 }
1055 }
1056 }
1057
1058 // TODO (multi-display): Magnification is supported only for the default display.
1059 // Announce rotation only if we will not animate as we already have the
1060 // windows in final state. Otherwise, we make this call at the rotation end.
1061 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1062 && isDefaultDisplay) {
1063 mService.mAccessibilityController.onRotationChangedLocked(this);
1064 }
1065
1066 return true;
1067 }
1068
1069 /**
1070 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1071 * changed.
1072 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1073 */
1074 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1075 // Use the effective "visual" dimensions based on current rotation
1076 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1077 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1078 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1079 int dw = realdw;
1080 int dh = realdh;
1081
1082 if (mAltOrientation) {
1083 if (realdw > realdh) {
1084 // Turn landscape into portrait.
1085 int maxw = (int)(realdh/1.3f);
1086 if (maxw < realdw) {
1087 dw = maxw;
1088 }
1089 } else {
1090 // Turn portrait into landscape.
1091 int maxh = (int)(realdw/1.3f);
1092 if (maxh < realdh) {
1093 dh = maxh;
1094 }
1095 }
1096 }
1097
1098 // Update application display metrics.
1099 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1100 mDisplayId);
1101 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1102 mDisplayId);
1103 mDisplayInfo.rotation = mRotation;
1104 mDisplayInfo.logicalWidth = dw;
1105 mDisplayInfo.logicalHeight = dh;
1106 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1107 mDisplayInfo.appWidth = appWidth;
1108 mDisplayInfo.appHeight = appHeight;
1109 if (isDefaultDisplay) {
1110 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1111 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1112 }
1113 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1114 if (mDisplayScalingDisabled) {
1115 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1116 } else {
1117 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1118 }
1119
1120 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1121 mDisplayInfo);
1122
1123 mBaseDisplayRect.set(0, 0, dw, dh);
1124
1125 if (isDefaultDisplay) {
1126 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1127 mCompatDisplayMetrics);
1128 }
1129 return mDisplayInfo;
1130 }
1131
1132 /**
1133 * Compute display configuration based on display properties and policy settings.
1134 * Do not call if mDisplayReady == false.
1135 */
1136 void computeScreenConfiguration(Configuration config) {
1137 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1138
1139 final int dw = displayInfo.logicalWidth;
1140 final int dh = displayInfo.logicalHeight;
1141 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1142 Configuration.ORIENTATION_LANDSCAPE;
1143 config.screenWidthDp =
1144 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1145 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1146 config.screenHeightDp =
1147 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1148 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001149
1150 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1151 final int leftInset = mTmpRect.left;
1152 final int topInset = mTmpRect.top;
1153 // appBounds at the root level should mirror the app screen size.
1154 config.setAppBounds(leftInset /*left*/, topInset /*top*/, leftInset + displayInfo.appWidth /*right*/,
1155 topInset + displayInfo.appHeight /*bottom*/);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001156 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1157 || displayInfo.rotation == Surface.ROTATION_270);
1158
1159 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1160 mDisplayMetrics.density, config);
1161
1162 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1163 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1164 ? Configuration.SCREENLAYOUT_ROUND_YES
1165 : Configuration.SCREENLAYOUT_ROUND_NO);
1166
1167 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1168 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1169 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1170 dh, mDisplayId);
1171 config.densityDpi = displayInfo.logicalDensityDpi;
1172
1173 config.colorMode =
1174 (displayInfo.isHdr()
1175 ? Configuration.COLOR_MODE_HDR_YES
1176 : Configuration.COLOR_MODE_HDR_NO)
1177 | (displayInfo.isWideColorGamut()
1178 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1179 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1180
1181 // Update the configuration based on available input devices, lid switch,
1182 // and platform configuration.
1183 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1184 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1185 config.navigation = Configuration.NAVIGATION_NONAV;
1186
1187 int keyboardPresence = 0;
1188 int navigationPresence = 0;
1189 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1190 final int len = devices != null ? devices.length : 0;
1191 for (int i = 0; i < len; i++) {
1192 InputDevice device = devices[i];
1193 if (!device.isVirtual()) {
1194 final int sources = device.getSources();
1195 final int presenceFlag = device.isExternal() ?
1196 WindowManagerPolicy.PRESENCE_EXTERNAL :
1197 WindowManagerPolicy.PRESENCE_INTERNAL;
1198
1199 // TODO(multi-display): Configure on per-display basis.
1200 if (mService.mIsTouchDevice) {
1201 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1202 InputDevice.SOURCE_TOUCHSCREEN) {
1203 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1204 }
1205 } else {
1206 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1207 }
1208
1209 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1210 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1211 navigationPresence |= presenceFlag;
1212 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1213 && config.navigation == Configuration.NAVIGATION_NONAV) {
1214 config.navigation = Configuration.NAVIGATION_DPAD;
1215 navigationPresence |= presenceFlag;
1216 }
1217
1218 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1219 config.keyboard = Configuration.KEYBOARD_QWERTY;
1220 keyboardPresence |= presenceFlag;
1221 }
1222 }
1223 }
1224
1225 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1226 config.navigation = Configuration.NAVIGATION_DPAD;
1227 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1228 }
1229
1230 // Determine whether a hard keyboard is available and enabled.
1231 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1232 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1233 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1234 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1235 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1236 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1237 }
1238
1239 // Let the policy update hidden states.
1240 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1241 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1242 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1243 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1244 }
1245
1246 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1247 int displayId) {
1248 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1249 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1250 final int unrotDw, unrotDh;
1251 if (rotated) {
1252 unrotDw = dh;
1253 unrotDh = dw;
1254 } else {
1255 unrotDw = dw;
1256 unrotDh = dh;
1257 }
1258 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1259 displayId);
1260 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1261 displayId);
1262 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1263 displayId);
1264 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1265 displayId);
1266 return sw;
1267 }
1268
1269 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1270 DisplayMetrics dm, int dw, int dh, int displayId) {
1271 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1272 displayId);
1273 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1274 uiMode, displayId);
1275 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1276 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1277 if (curSize == 0 || size < curSize) {
1278 curSize = size;
1279 }
1280 return curSize;
1281 }
1282
1283 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1284 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1285
1286 // We need to determine the smallest width that will occur under normal
1287 // operation. To this, start with the base screen size and compute the
1288 // width under the different possible rotations. We need to un-rotate
1289 // the current screen dimensions before doing this.
1290 int unrotDw, unrotDh;
1291 if (rotated) {
1292 unrotDw = dh;
1293 unrotDh = dw;
1294 } else {
1295 unrotDw = dw;
1296 unrotDh = dh;
1297 }
1298 displayInfo.smallestNominalAppWidth = 1<<30;
1299 displayInfo.smallestNominalAppHeight = 1<<30;
1300 displayInfo.largestNominalAppWidth = 0;
1301 displayInfo.largestNominalAppHeight = 0;
1302 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1303 unrotDh);
1304 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1305 unrotDw);
1306 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1307 unrotDh);
1308 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1309 unrotDw);
1310 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1311 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1312 displayId);
1313 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1314 displayId);
1315 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1316 displayId);
1317 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1318 displayId);
1319 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1320 outConfig.screenLayout = sl;
1321 }
1322
1323 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1324 int uiMode, int displayId) {
1325 // Get the app screen size at this rotation.
1326 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1327 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1328
1329 // Compute the screen layout size class for this rotation.
1330 int longSize = w;
1331 int shortSize = h;
1332 if (longSize < shortSize) {
1333 int tmp = longSize;
1334 longSize = shortSize;
1335 shortSize = tmp;
1336 }
1337 longSize = (int)(longSize/density);
1338 shortSize = (int)(shortSize/density);
1339 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1340 }
1341
1342 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1343 int uiMode, int dw, int dh) {
1344 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1345 displayId);
1346 if (width < displayInfo.smallestNominalAppWidth) {
1347 displayInfo.smallestNominalAppWidth = width;
1348 }
1349 if (width > displayInfo.largestNominalAppWidth) {
1350 displayInfo.largestNominalAppWidth = width;
1351 }
1352 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1353 displayId);
1354 if (height < displayInfo.smallestNominalAppHeight) {
1355 displayInfo.smallestNominalAppHeight = height;
1356 }
1357 if (height > displayInfo.largestNominalAppHeight) {
1358 displayInfo.largestNominalAppHeight = height;
1359 }
1360 }
1361
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001362 DockedStackDividerController getDockedDividerController() {
1363 return mDividerControllerLocked;
1364 }
1365
Winson Chung655332c2016-10-31 13:14:28 -07001366 PinnedStackController getPinnedStackController() {
1367 return mPinnedStackControllerLocked;
1368 }
1369
Jeff Browna506a6e2013-06-04 00:02:38 -07001370 /**
1371 * Returns true if the specified UID has access to this display.
1372 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001373 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001374 return mDisplay.hasAccess(uid);
1375 }
1376
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001377 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001378 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001379 }
1380
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001381 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001382 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001383 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001384 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001385 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001386 }
1387
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001388 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001389 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1390 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001391 if (stack.mStackId == stackId) {
1392 return stack;
1393 }
1394 }
1395 return null;
1396 }
1397
Andrii Kulian441e4492016-09-29 15:25:00 -07001398 @Override
1399 void onConfigurationChanged(Configuration newParentConfig) {
1400 super.onConfigurationChanged(newParentConfig);
1401
Andrii Kulian3a507b52016-09-19 18:14:12 -07001402 // The display size information is heavily dependent on the resources in the current
1403 // configuration, so we need to reconfigure it every time the configuration changes.
1404 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1405 mService.reconfigureDisplayLocked(this);
1406
1407 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001408 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001409 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001410
Andrii Kulian441e4492016-09-29 15:25:00 -07001411 /**
1412 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1413 * bounds were updated.
1414 */
1415 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001416 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1417 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001418 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001419 changedStackList.add(stack.mStackId);
1420 }
1421 }
1422 }
1423
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001424 @Override
1425 boolean fillsParent() {
1426 return true;
1427 }
1428
1429 @Override
1430 boolean isVisible() {
1431 return true;
1432 }
1433
1434 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001435 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001436 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001437 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001438 }
1439
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001440 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001441 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1442 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1443 // target, or here in order if there isn't an IME target.
1444 if (traverseTopToBottom) {
1445 for (int i = mChildren.size() - 1; i >= 0; --i) {
1446 final DisplayChildWindowContainer child = mChildren.get(i);
1447 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1448 // In this case the Ime windows will be processed above their target so we skip
1449 // here.
1450 continue;
1451 }
1452 if (child.forAllWindows(callback, traverseTopToBottom)) {
1453 return true;
1454 }
1455 }
1456 } else {
1457 final int count = mChildren.size();
1458 for (int i = 0; i < count; i++) {
1459 final DisplayChildWindowContainer child = mChildren.get(i);
1460 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1461 // In this case the Ime windows will be processed above their target so we skip
1462 // here.
1463 continue;
1464 }
1465 if (child.forAllWindows(callback, traverseTopToBottom)) {
1466 return true;
1467 }
1468 }
1469 }
1470 return false;
1471 }
1472
1473 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1474 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1475 }
1476
1477 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001478 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001479 final WindowManagerPolicy policy = mService.mPolicy;
1480
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001481 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001482 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001483 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001484 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001485 // If the display is frozen, some activities may be in the middle of restarting, and
1486 // thus have removed their old window. If the window has the flag to hide the lock
1487 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1488 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001489 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001490 } else if (policy.isKeyguardLocked()) {
1491 // Use the last orientation the while the display is frozen with the keyguard
1492 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1493 // window. We don't want to check the show when locked window directly though as
1494 // things aren't stable while the display is frozen, for example the window could be
1495 // momentarily unavailable due to activity relaunch.
1496 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001497 + "return " + mLastOrientation);
1498 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001499 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001500 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001501 final int orientation = mAboveAppWindowsContainers.getOrientation();
1502 if (orientation != SCREEN_ORIENTATION_UNSET) {
1503 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001504 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001505 }
1506
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001507 // Top system windows are not requesting an orientation. Start searching from apps.
1508 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001509 }
1510
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001511 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001512 // Check if display metrics changed and update base values if needed.
1513 updateBaseDisplayMetricsIfNeeded();
1514
Craig Mautner722285e2012-09-07 13:55:58 -07001515 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001516 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001517
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001518 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1519 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001520 }
Craig Mautner722285e2012-09-07 13:55:58 -07001521 }
1522
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001523 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001524 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1525 if (displayManagerInternal != null) {
1526 // Bootstrap the default logical display from the display manager.
1527 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1528 if (newDisplayInfo != null) {
1529 mDisplayInfo.copyFrom(newDisplayInfo);
1530 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001531 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001532
Andrii Kuliancd097992017-03-23 18:31:59 -07001533 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1534 mDisplayInfo.logicalDensityDpi);
1535 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1536 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1537 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001538 }
1539
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001540 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001541 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001542 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001543 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001544 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1545 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001546 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001547 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001548 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001549 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001550 out.set(left, top, left + width, top + height);
1551 }
1552
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001553 private void getLogicalDisplayRect(Rect out, int orientation) {
1554 getLogicalDisplayRect(out);
1555
1556 // Rotate the Rect if needed.
1557 final int currentRotation = mDisplayInfo.rotation;
1558 final int rotationDelta = deltaRotation(currentRotation, orientation);
1559 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1560 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1561 mTmpRectF.set(out);
1562 mTmpMatrix.mapRect(mTmpRectF);
1563 mTmpRectF.round(out);
1564 }
1565 }
1566
Andrii Kuliancd097992017-03-23 18:31:59 -07001567 /**
1568 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1569 * values.
1570 */
1571 private void updateBaseDisplayMetricsIfNeeded() {
1572 // Get real display metrics without overrides from WM.
1573 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1574 final int orientation = mDisplayInfo.rotation;
1575 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1576 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1577 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1578 final int newDensity = mDisplayInfo.logicalDensityDpi;
1579
1580 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1581 || mInitialDisplayHeight != newHeight
1582 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1583
1584 if (displayMetricsChanged) {
1585 // Check if display size or density is forced.
1586 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1587 || mBaseDisplayHeight != mInitialDisplayHeight;
1588 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1589
1590 // If there is an override set for base values - use it, otherwise use new values.
1591 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1592 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1593 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1594
1595 // Real display metrics changed, so we should also update initial values.
1596 mInitialDisplayWidth = newWidth;
1597 mInitialDisplayHeight = newHeight;
1598 mInitialDisplayDensity = newDensity;
1599 mService.reconfigureDisplayLocked(this);
1600 }
1601 }
1602
Bryce Lee27cec322017-03-21 09:41:37 -07001603 /** Sets the maximum width the screen resolution can be */
1604 void setMaxUiWidth(int width) {
1605 if (DEBUG_DISPLAY) {
1606 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1607 }
1608
1609 mMaxUiWidth = width;
1610
1611 // Update existing metrics.
1612 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1613 }
1614
1615 /** Update base (override) display metrics. */
1616 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1617 mBaseDisplayWidth = baseWidth;
1618 mBaseDisplayHeight = baseHeight;
1619 mBaseDisplayDensity = baseDensity;
1620
1621 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1622 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1623 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1624 mBaseDisplayWidth = mMaxUiWidth;
1625
1626 if (DEBUG_DISPLAY) {
1627 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1628 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1629 + " on display:" + getDisplayId());
1630 }
1631 }
1632
1633 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1634 }
1635
Chong Zhangf66db432016-01-13 10:39:51 -08001636 void getContentRect(Rect out) {
1637 out.set(mContentRect);
1638 }
1639
Wale Ogunwale1666e312016-12-16 11:27:18 -08001640 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1641 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1642 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001643
Wale Ogunwale1666e312016-12-16 11:27:18 -08001644 TaskStack stack = getStackById(stackId);
1645 if (stack != null) {
1646 // It's already attached to the display...clear mDeferRemoval and move stack to
1647 // appropriate z-order on display as needed.
1648 stack.mDeferRemoval = false;
1649 // We're not moving the display to front when we're adding stacks, only when
1650 // requested to change the position of stack explicitly.
1651 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1652 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001653 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001654 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001655 mTaskStackContainers.addStackToDisplay(stack, onTop);
1656 }
1657
Wale Ogunwale1666e312016-12-16 11:27:18 -08001658 if (stackId == DOCKED_STACK_ID) {
1659 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001660 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001661 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001662 }
1663
Wale Ogunwale1666e312016-12-16 11:27:18 -08001664 void moveStackToDisplay(TaskStack stack) {
1665 final DisplayContent prevDc = stack.getDisplayContent();
1666 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001667 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1668 + " which is not currently attached to any display");
1669 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001670 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001671 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1672 + " to its current displayId=" + mDisplayId);
1673 }
1674
Wale Ogunwale1666e312016-12-16 11:27:18 -08001675 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
1676 mTaskStackContainers.addStackToDisplay(stack, true /* onTop */);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001677 }
1678
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001679 @Override
1680 protected void addChild(DisplayChildWindowContainer child,
1681 Comparator<DisplayChildWindowContainer> comparator) {
1682 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1683 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001684
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001685 @Override
1686 protected void addChild(DisplayChildWindowContainer child, int index) {
1687 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1688 }
1689
1690 @Override
1691 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001692 // Only allow removal of direct children from this display if the display is in the process
1693 // of been removed.
1694 if (mRemovingDisplay) {
1695 super.removeChild(child);
1696 return;
1697 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001698 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001699 }
1700
Andrii Kuliand2765632016-12-12 22:26:34 -08001701 @Override
1702 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1703 // Children of the display are statically ordered, so the real intention here is to perform
1704 // the operation on the display and not the static direct children.
1705 getParent().positionChildAt(position, this, includingParents);
1706 }
1707
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001708 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001709 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1710 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001711 final int taskId = stack.taskIdFromPoint(x, y);
1712 if (taskId != -1) {
1713 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001714 }
1715 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001716 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001717 }
1718
Chong Zhang8e89b312015-09-09 15:09:30 -07001719 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001720 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001721 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001722 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001723 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001724 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001725 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001726 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1727 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001728 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001729 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001730 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001731
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001732 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1733 if (mTmpTaskForResizePointSearchResult.searchDone) {
1734 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001735 }
1736 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001737 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001738 }
1739
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001740 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001741 // The provided task is the task on this display with focus, so if WindowManagerService's
1742 // focused app is not on this display, focusedTask will be null.
1743 if (focusedTask == null) {
1744 mTouchExcludeRegion.setEmpty();
1745 } else {
1746 mTouchExcludeRegion.set(mBaseDisplayRect);
1747 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1748 mTmpRect2.setEmpty();
1749 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1750 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1751 stack.setTouchExcludeRegion(
1752 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1753 }
1754 // If we removed the focused task above, add it back and only leave its
1755 // outside touch area in the exclusion. TapDectector is not interested in
1756 // any touch inside the focused task itself.
1757 if (!mTmpRect2.isEmpty()) {
1758 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1759 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001760 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001761 final WindowState inputMethod = mService.mInputMethodWindow;
1762 if (inputMethod != null && inputMethod.isVisibleLw()) {
1763 // If the input method is visible and the user is typing, we don't want these touch
1764 // events to be intercepted and used to change focus. This would likely cause a
1765 // disappearance of the input method.
1766 inputMethod.getTouchableRegion(mTmpRegion);
1767 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1768 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001769 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1770 WindowState win = mTapExcludedWindows.get(i);
1771 win.getTouchableRegion(mTmpRegion);
1772 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1773 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001774 // TODO(multi-display): Support docked stacks on secondary displays.
1775 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001776 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001777 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001778 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1779 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001780 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001781 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001782 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001783 }
1784
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001785 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001786 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001787 super.switchUser();
1788 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001789 }
1790
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001791 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001792 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1793 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001794 }
1795 }
1796
1797 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001798 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001799 }
1800
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001801 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001802 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001803 }
1804
1805 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001806 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001807 }
1808
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001809 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001810 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001811 }
1812
Wale Ogunwale10124582016-09-15 20:25:50 -07001813 @Override
1814 void removeIfPossible() {
1815 if (isAnimating()) {
1816 mDeferredRemoval = true;
1817 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001818 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001819 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001820 }
1821
Wale Ogunwale10124582016-09-15 20:25:50 -07001822 @Override
1823 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001824 mRemovingDisplay = true;
1825 try {
1826 super.removeImmediately();
1827 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1828 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001829 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001830 mService.unregisterPointerEventListener(mTapDetector);
1831 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1832 }
1833 } finally {
1834 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001835 }
Craig Mautner95da1082014-02-24 17:54:35 -08001836 }
1837
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001838 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001839 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001840 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001841 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1842
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001843 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001844 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001845 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001846 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001847 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001848 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001849 }
1850
Wale Ogunwale10124582016-09-15 20:25:50 -07001851 boolean animateForIme(float interpolatedValue, float animationTarget,
1852 float dividerAnimationTarget) {
1853 boolean updated = false;
1854
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001855 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1856 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001857 if (stack == null || !stack.isAdjustedForIme()) {
1858 continue;
1859 }
1860
1861 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1862 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1863 updated = true;
1864 } else {
1865 mDividerControllerLocked.mLastAnimationProgress =
1866 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1867 mDividerControllerLocked.mLastDividerProgress =
1868 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1869 updated |= stack.updateAdjustForIme(
1870 mDividerControllerLocked.mLastAnimationProgress,
1871 mDividerControllerLocked.mLastDividerProgress,
1872 false /* force */);
1873 }
1874 if (interpolatedValue >= 1f) {
1875 stack.endImeAdjustAnimation();
1876 }
1877 }
1878
1879 return updated;
1880 }
1881
1882 boolean clearImeAdjustAnimation() {
1883 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001884 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1885 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001886 if (stack != null && stack.isAdjustedForIme()) {
1887 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1888 changed = true;
1889 }
1890 }
1891 return changed;
1892 }
1893
1894 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001895 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1896 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001897 if (stack.isVisible() && stack.isAdjustedForIme()) {
1898 stack.beginImeAdjustAnimation();
1899 }
1900 }
1901 }
1902
1903 void adjustForImeIfNeeded() {
1904 final WindowState imeWin = mService.mInputMethodWindow;
1905 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1906 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001907 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001908 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1909 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1910 imeTargetStack.getDockSide() : DOCKED_INVALID;
1911 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1912 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1913 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1914 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1915 final boolean imeHeightChanged = imeVisible &&
1916 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1917
1918 // The divider could be adjusted for IME position, or be thinner than usual,
1919 // or both. There are three possible cases:
1920 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1921 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1922 // - If IME is not visible, divider is not moved and is normal width.
1923
1924 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001925 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1926 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001927 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
1928 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
1929 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1930 } else {
1931 stack.resetAdjustedForIme(false);
1932 }
1933 }
1934 mDividerControllerLocked.setAdjustedForIme(
1935 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1936 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001937 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1938 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001939 stack.resetAdjustedForIme(!dockVisible);
1940 }
1941 mDividerControllerLocked.setAdjustedForIme(
1942 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
1943 }
Winson Chung655332c2016-10-31 13:14:28 -07001944 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07001945 }
1946
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001947 void setInputMethodAnimLayerAdjustment(int adj) {
1948 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
1949 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08001950 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001951 }
1952
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001953 /**
1954 * If a window that has an animation specifying a colored background and the current wallpaper
1955 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
1956 * suddenly disappear.
1957 */
1958 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001959 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
1960 w -> w.mIsWallpaper && w.isVisibleNow());
1961
1962 if (visibleWallpaper != null) {
1963 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001964 }
1965 return winAnimator.mAnimLayer;
1966 }
1967
Wale Ogunwale10124582016-09-15 20:25:50 -07001968 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001969 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1970 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07001971 stack.prepareFreezingTaskBounds();
1972 }
1973 }
1974
Wale Ogunwale94744212015-09-21 19:01:47 -07001975 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001976 getLogicalDisplayRect(mTmpRect, newRotation);
1977
1978 // Compute a transform matrix to undo the coordinate space transformation,
1979 // and present the window at the same physical position it previously occupied.
1980 final int deltaRotation = deltaRotation(newRotation, oldRotation);
1981 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
1982
1983 mTmpRectF.set(bounds);
1984 mTmpMatrix.mapRect(mTmpRectF);
1985 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07001986 }
1987
Wale Ogunwale4a02d812015-02-12 23:01:38 -08001988 static int deltaRotation(int oldRotation, int newRotation) {
1989 int delta = newRotation - oldRotation;
1990 if (delta < 0) delta += 4;
1991 return delta;
1992 }
1993
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001994 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001995 Matrix outMatrix) {
1996 // For rotations without Z-ordering we don't need the target rectangle's position.
1997 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
1998 displayHeight, outMatrix);
1999 }
2000
2001 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2002 float displayWidth, float displayHeight, Matrix outMatrix) {
2003 switch (rotation) {
2004 case ROTATION_0:
2005 outMatrix.reset();
2006 break;
2007 case ROTATION_270:
2008 outMatrix.setRotate(270, 0, 0);
2009 outMatrix.postTranslate(0, displayHeight);
2010 outMatrix.postTranslate(rectTop, 0);
2011 break;
2012 case ROTATION_180:
2013 outMatrix.reset();
2014 break;
2015 case ROTATION_90:
2016 outMatrix.setRotate(90, 0, 0);
2017 outMatrix.postTranslate(displayWidth, 0);
2018 outMatrix.postTranslate(-rectTop, rectLeft);
2019 break;
2020 }
2021 }
2022
Craig Mautnera91f9e22012-09-14 16:22:08 -07002023 public void dump(String prefix, PrintWriter pw) {
2024 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2025 final String subPrefix = " " + prefix;
2026 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2027 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2028 pw.print("dpi");
2029 if (mInitialDisplayWidth != mBaseDisplayWidth
2030 || mInitialDisplayHeight != mBaseDisplayHeight
2031 || mInitialDisplayDensity != mBaseDisplayDensity) {
2032 pw.print(" base=");
2033 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2034 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2035 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002036 if (mDisplayScalingDisabled) {
2037 pw.println(" noscale");
2038 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002039 pw.print(" cur=");
2040 pw.print(mDisplayInfo.logicalWidth);
2041 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2042 pw.print(" app=");
2043 pw.print(mDisplayInfo.appWidth);
2044 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2045 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2046 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2047 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2048 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002049 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2050 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002051
Craig Mautnerdc548482014-02-05 13:35:24 -08002052 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002053 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002054 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2055 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002056 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002057 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002058
Craig Mautnerdc548482014-02-05 13:35:24 -08002059 pw.println();
2060 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002061 pw.println();
2062 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002063 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002064 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002065 pw.print(" Exiting #"); pw.print(i);
2066 pw.print(' '); pw.print(token);
2067 pw.println(':');
2068 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002069 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002070 }
Craig Mautner59c00972012-07-30 12:10:24 -07002071 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002072 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002073 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002074 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002075 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002076 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002077
2078 if (mInputMethodAnimLayerAdjustment != 0) {
2079 pw.println(subPrefix
2080 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2081 }
Craig Mautner59c00972012-07-30 12:10:24 -07002082 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002083
2084 @Override
2085 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002086 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002087 }
2088
2089 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002090 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002091 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002092
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002093 /** Checks if stack with provided id is visible on this display. */
2094 boolean isStackVisible(int stackId) {
2095 final TaskStack stack = getStackById(stackId);
2096 return (stack != null && stack.isVisible());
2097 }
2098
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002099 /**
2100 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2101 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002102 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002103 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002104 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002105 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002106
2107 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002108 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002109 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002110 */
Jorim Jaggife762342016-10-13 14:33:27 +02002111 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002112 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002113 }
2114
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002115 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002116 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002117 final int x = (int) xf;
2118 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002119 final WindowState touchedWin = getWindow(w -> {
2120 final int flags = w.mAttrs.flags;
2121 if (!w.isVisibleLw()) {
2122 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002123 }
2124 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002125 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002126 }
2127
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002128 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002129 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002130 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002131 }
2132
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002133 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002134
2135 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002136 return mTmpRegion.contains(x, y) || touchFlags == 0;
2137 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002138
2139 return touchedWin;
2140 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002141
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002142 boolean canAddToastWindowForUid(int uid) {
2143 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002144 // Also if the app is focused adding more than one toast at
2145 // a time for better backwards compatibility.
2146 final WindowState focusedWindowForUid = getWindow(w ->
2147 w.mOwnerUid == uid && w.isFocused());
2148 if (focusedWindowForUid != null) {
2149 return true;
2150 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002151 final WindowState win = getWindow(w ->
2152 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2153 && !w.mWindowRemovalAllowed);
2154 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002155 }
2156
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002157 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002158 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2159 return;
2160 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002161
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002162 // Used to communicate the old focus to the callback method.
2163 mTmpWindow = oldFocus;
2164
2165 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002166 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002167
2168 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002169 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002170
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002171 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002172
2173 if (mTmpWindow == null) {
2174 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2175 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002176 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002177 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002178 }
2179
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002180 /** Updates the layer assignment of windows on this display. */
2181 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002182 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002183 if (setLayoutNeeded) {
2184 setLayoutNeeded();
2185 }
2186 }
2187
Wale Ogunwale1666e312016-12-16 11:27:18 -08002188 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2189 // moving containers or resizing them. Need to investigate the best way to have it automatically
2190 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002191 void layoutAndAssignWindowLayersIfNeeded() {
2192 mService.mWindowsChanged = true;
2193 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002194
2195 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2196 false /*updateInputWindows*/)) {
2197 assignWindowLayers(false /* setLayoutNeeded */);
2198 }
2199
2200 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2201 mService.mWindowPlacerLocked.performSurfacePlacement();
2202 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2203 }
2204
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002205 /** Returns true if a leaked surface was destroyed */
2206 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002207 // Used to indicate that a surface was leaked.
2208 mTmpWindow = null;
2209 forAllWindows(w -> {
2210 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002211 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002212 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002213 }
2214 if (!mService.mSessions.contains(wsa.mSession)) {
2215 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002216 + w + " surface=" + wsa.mSurfaceController
2217 + " token=" + w.mToken
2218 + " pid=" + w.mSession.mPid
2219 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002220 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002221 mService.mForceRemoves.add(w);
2222 mTmpWindow = w;
2223 } else if (w.mAppToken != null && w.mAppToken.clientHidden) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002224 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002225 + w + " surface=" + wsa.mSurfaceController
2226 + " token=" + w.mAppToken
2227 + " saved=" + w.hasSavedSurface());
2228 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002229 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002230 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002231 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002232 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002233
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002234 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002235 }
2236
2237 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002238 * Determine and return the window that should be the IME target.
2239 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2240 * @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 +00002241 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002242 WindowState computeImeTarget(boolean updateImeTarget) {
2243 if (mService.mInputMethodWindow == null) {
2244 // There isn't an IME so there shouldn't be a target...That was easy!
2245 if (updateImeTarget) {
2246 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2247 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2248 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2249 }
2250 return null;
2251 }
2252
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002253 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2254 // same display. Or even when the current IME/target are not on the same screen as the next
2255 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002256 mUpdateImeTarget = updateImeTarget;
2257 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002258
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002259
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002260 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2261 // to be on top of it, but it is not -really- where input will go. So look down below
2262 // for a real window to target...
2263 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2264 final AppWindowToken token = target.mAppToken;
2265 if (token != null) {
2266 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2267 if (betterTarget != null) {
2268 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002269 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002270 }
2271 }
2272
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002273 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2274 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002275
2276 // Now, a special case -- if the last target's window is in the process of exiting, and is
2277 // above the new target, keep on the last target to avoid flicker. Consider for example a
2278 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2279 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2280 // scrim.
2281 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002282 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2283 && (target == null
2284 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002285 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002286 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002287 }
2288
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002289 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2290 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002291
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002292 if (target == null) {
2293 if (updateImeTarget) {
2294 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2295 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2296 + Debug.getCallers(4) : ""));
2297 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2298 }
2299
2300 return null;
2301 }
2302
2303 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002304 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2305 if (token != null) {
2306
2307 // Now some fun for dealing with window animations that modify the Z order. We need
2308 // to look at all windows below the current target that are in this app, finding the
2309 // highest visible one in layering.
2310 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002311 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002312 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002313 }
2314
2315 if (highestTarget != null) {
2316 final AppTransition appTransition = mService.mAppTransition;
2317 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2318 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2319 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002320 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002321
2322 if (appTransition.isTransitionSet()) {
2323 // If we are currently setting up for an animation, hold everything until we
2324 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002325 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2326 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002327 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002328 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002329 // If the window we are currently targeting is involved with an animation,
2330 // and it is on top of the next target we will be over, then hold off on
2331 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2333 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002334 }
2335 }
2336 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002337
2338 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2339 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2340 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002341 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002342 }
2343
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002344 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002345 }
2346
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002347 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2348 if (target == mService.mInputMethodTarget
2349 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2350 && mInputMethodAnimLayerAdjustment == layerAdj) {
2351 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002352 }
2353
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002354 mService.mInputMethodTarget = target;
2355 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2356 setInputMethodAnimLayerAdjustment(layerAdj);
2357 assignWindowLayers(false /* setLayoutNeeded */);
2358 }
2359
2360 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2361 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2362 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2363 }
2364
2365 // Used to indicate we have reached the first window in the range we are interested in.
2366 mTmpWindow = null;
2367
2368 // TODO: Figure-out a more efficient way to do this.
2369 final WindowState candidate = getWindow(w -> {
2370 if (w == top) {
2371 // Reached the first window in the range we are interested in.
2372 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002373 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002374 if (mTmpWindow == null) {
2375 return false;
2376 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002377
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002378 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2379 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002380 }
2381 // If we reached the bottom of the range of windows we are considering,
2382 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002383 if (w == bottom) {
2384 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002385 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002386 return false;
2387 });
2388
2389 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002390 }
2391
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002392 void setLayoutNeeded() {
2393 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2394 mLayoutNeeded = true;
2395 }
2396
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002397 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002398 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2399 mLayoutNeeded = false;
2400 }
2401
2402 boolean isLayoutNeeded() {
2403 return mLayoutNeeded;
2404 }
2405
Wale Ogunwale02319a62016-09-26 15:21:22 -07002406 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2407 if (mTokenMap.isEmpty()) {
2408 return;
2409 }
2410 pw.println(" Display #" + mDisplayId);
2411 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2412 while (it.hasNext()) {
2413 final WindowToken token = it.next();
2414 pw.print(" ");
2415 pw.print(token);
2416 if (dumpAll) {
2417 pw.println(':');
2418 token.dump(pw, " ");
2419 } else {
2420 pw.println();
2421 }
2422 }
2423 }
2424
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002425 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002426 final int[] index = new int[1];
2427 forAllWindows(w -> {
2428 final WindowStateAnimator wAnim = w.mWinAnimator;
2429 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2430 index[0] = index[0] + 1;
2431 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002432 }
2433
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002434 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002435 forAllWindows(w -> {
2436 w.mWinAnimator.enableSurfaceTrace(fd);
2437 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002438 }
2439
2440 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002441 forAllWindows(w -> {
2442 w.mWinAnimator.disableSurfaceTrace();
2443 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002444 }
2445
Jorim Jaggife762342016-10-13 14:33:27 +02002446 /**
2447 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2448 */
2449 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2450 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002451 forAllWindows(w -> {
2452 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
2453 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002454 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2455 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002456 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002457 }
2458
Wale Ogunwale494009b82016-10-21 09:01:38 -07002459 boolean checkWaitingForWindows() {
2460
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002461 mHaveBootMsg = false;
2462 mHaveApp = false;
2463 mHaveWallpaper = false;
2464 mHaveKeyguard = true;
2465
2466 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002467 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2468 return true;
2469 }
2470 if (w.isDrawnLw()) {
2471 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002472 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002473 } else if (w.mAttrs.type == TYPE_APPLICATION
2474 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002475 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002476 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002477 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002478 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002479 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002480 }
2481 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002482 return false;
2483 });
2484
2485 if (visibleWindow != null) {
2486 // We have a visible window.
2487 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002488 }
2489
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002490 // if the wallpaper service is disabled on the device, we're never going to have
2491 // wallpaper, don't bother waiting for it
2492 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2493 com.android.internal.R.bool.config_enableWallpaperService)
2494 && !mService.mOnlyCore;
2495
Wale Ogunwale494009b82016-10-21 09:01:38 -07002496 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2497 "******** booted=" + mService.mSystemBooted
2498 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002499 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2500 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2501 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002502
2503 // If we are turning on the screen to show the boot message, don't do it until the boot
2504 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002505 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002506 return true;
2507 }
2508
2509 // If we are turning on the screen after the boot is completed normally, don't do so until
2510 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002511 if (mService.mSystemBooted
2512 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002513 return true;
2514 }
2515
2516 return false;
2517 }
2518
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002519 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002520 mTmpWindowAnimator = animator;
2521 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002522 }
2523
2524 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002525 resetAnimationBackgroundAnimator();
2526
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002527 // Used to indicate a detached wallpaper.
2528 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002529 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002530
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002531 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002532
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002533 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002534 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002535 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2536 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002537 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2538 }
2539 }
2540
2541 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002542 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002543 }
2544
Wale Ogunwale494009b82016-10-21 09:01:38 -07002545 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002546 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002547 if (imFocus == null) {
2548 return false;
2549 }
2550
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002551 if (DEBUG_INPUT_METHOD) {
2552 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2553 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2554 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002555 }
2556
Wale Ogunwale494009b82016-10-21 09:01:38 -07002557 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2558
2559 if (DEBUG_INPUT_METHOD) {
2560 Slog.i(TAG_WM, "IM target client: " + imeClient);
2561 if (imeClient != null) {
2562 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2563 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2564 }
2565 }
2566
2567 return imeClient != null && imeClient.asBinder() == client.asBinder();
2568 }
2569
2570 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002571 final WindowState win = getWindow(
2572 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2573 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002574 }
2575
2576 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002577 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002578 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002579 final int curValue = w.mSystemUiVisibility;
2580 final int diff = (curValue ^ visibility) & globalDiff;
2581 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002582 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002583 w.mSeq++;
2584 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002585 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002586 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2587 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002588 visibility, newValue, diff);
2589 }
2590 } catch (RemoteException e) {
2591 // so sorry
2592 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002593 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002594 }
2595
2596 void onWindowFreezeTimeout() {
2597 Slog.w(TAG_WM, "Window freeze timeout expired.");
2598 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002599
2600 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002601 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002602 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002603 }
2604 w.mOrientationChanging = false;
2605 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2606 - mService.mDisplayFreezeTime);
2607 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002608 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002609 mService.mWindowPlacerLocked.performSurfacePlacement();
2610 }
2611
2612 void waitForAllWindowsDrawn() {
2613 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002614 forAllWindows(w -> {
2615 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2616 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2617 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002618 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002619 w.mLastContentInsets.set(-1, -1, -1, -1);
2620 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002621 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002622 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002623 }
2624
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002625 // TODO: Super crazy long method that should be broken down...
2626 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2627
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002628 final int dw = mDisplayInfo.logicalWidth;
2629 final int dh = mDisplayInfo.logicalHeight;
2630 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2631
2632 mTmpUpdateAllDrawn.clear();
2633
2634 int repeats = 0;
2635 do {
2636 repeats++;
2637 if (repeats > 6) {
2638 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2639 clearLayoutNeeded();
2640 break;
2641 }
2642
2643 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2644 pendingLayoutChanges);
2645
Andrii Kulian839def92016-11-02 10:58:58 -07002646 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2647 // the wallpaper window jumping across displays.
2648 // Remove check for default display when there will be support for multiple wallpaper
2649 // targets (on different displays).
2650 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002651 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002652 }
2653
2654 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2655 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2656 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2657 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002658 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002659 }
2660 }
2661
2662 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2663 setLayoutNeeded();
2664 }
2665
2666 // FIRST LOOP: Perform a layout, if needed.
2667 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2668 performLayout(repeats == 1, false /* updateInputWindows */);
2669 } else {
2670 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2671 }
2672
2673 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2674 pendingLayoutChanges = 0;
2675
2676 if (isDefaultDisplay) {
2677 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002678 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002679 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2680 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2681 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2682 }
2683 } while (pendingLayoutChanges != 0);
2684
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002685 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002686 resetDimming();
2687
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002688 mTmpRecoveringMemory = recoveringMemory;
2689 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002690
2691 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002692 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2693 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2694 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002695 true /* inTraversal, must call performTraversalInTrans... below */);
2696
2697 stopDimmingIfNeeded();
2698
2699 while (!mTmpUpdateAllDrawn.isEmpty()) {
2700 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2701 // See if any windows have been drawn, so they (and others associated with them)
2702 // can now be shown.
2703 atoken.updateAllDrawn(this);
2704 }
2705
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002706 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002707 }
2708
2709 void performLayout(boolean initial, boolean updateInputWindows) {
2710 if (!isLayoutNeeded()) {
2711 return;
2712 }
2713 clearLayoutNeeded();
2714
2715 final int dw = mDisplayInfo.logicalWidth;
2716 final int dh = mDisplayInfo.logicalHeight;
2717
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002718 if (DEBUG_LAYOUT) {
2719 Slog.v(TAG, "-------------------------------------");
2720 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2721 }
2722
Andrii Kulian8ee72852017-03-10 10:36:45 -08002723 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002724 getConfiguration().uiMode);
2725 if (isDefaultDisplay) {
2726 // Not needed on non-default displays.
2727 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2728 mService.mScreenRect.set(0, 0, dw, dh);
2729 }
2730
2731 mService.mPolicy.getContentRectLw(mContentRect);
2732
2733 int seq = mService.mLayoutSeq + 1;
2734 if (seq < 0) seq = 0;
2735 mService.mLayoutSeq = seq;
2736
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002737 // Used to indicate that we have processed the dream window and all additional windows are
2738 // behind it.
2739 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002740 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002741
2742 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002743 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002744
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 // Used to indicate that we have processed the dream window and all additional attached
2746 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002747 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002748 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002749
2750 // Now perform layout of attached windows, which usually depend on the position of the
2751 // window they are attached to. XXX does not deal with windows that are attached to windows
2752 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002753 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002754
2755 // Window frames may have changed. Tell the input dispatcher about it.
2756 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2757 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2758 if (updateInputWindows) {
2759 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2760 }
2761
2762 mService.mPolicy.finishLayoutLw();
2763 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2764 }
2765
2766 /**
2767 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2768 * In portrait mode, it grabs the full screenshot.
2769 *
2770 * @param width the width of the target bitmap
2771 * @param height the height of the target bitmap
2772 * @param includeFullDisplay true if the screen should not be cropped before capture
2773 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2774 * @param config of the output bitmap
2775 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002776 * @param includeDecor whether to include window decors, like the status or navigation bar
2777 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002778 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002779 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002780 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002781 boolean wallpaperOnly, boolean includeDecor) {
2782 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2783 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002784 if (bitmap == null) {
2785 return null;
2786 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002787
2788 if (DEBUG_SCREENSHOT) {
2789 // TEST IF IT's ALL BLACK
2790 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2791 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2792 bitmap.getHeight());
2793 boolean allBlack = true;
2794 final int firstColor = buffer[0];
2795 for (int i = 0; i < buffer.length; i++) {
2796 if (buffer[i] != firstColor) {
2797 allBlack = false;
2798 break;
2799 }
2800 }
2801 if (allBlack) {
2802 final WindowState appWin = mScreenshotApplicationState.appWin;
2803 final int maxLayer = mScreenshotApplicationState.maxLayer;
2804 final int minLayer = mScreenshotApplicationState.minLayer;
2805 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2806 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2807 (appWin != null ?
2808 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2809 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2810 }
2811 }
2812
2813 // Create a copy of the screenshot that is immutable and backed in ashmem.
2814 // This greatly reduces the overhead of passing the bitmap between processes.
2815 Bitmap ret = bitmap.createAshmemBitmap(config);
2816 bitmap.recycle();
2817 return ret;
2818 }
2819
2820 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2821 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2822 boolean includeDecor) {
2823 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2824 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2825 }
2826
2827 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2828 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2829 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002830 int dw = mDisplayInfo.logicalWidth;
2831 int dh = mDisplayInfo.logicalHeight;
2832 if (dw == 0 || dh == 0) {
2833 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2834 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2835 return null;
2836 }
2837
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002838 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002839
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002840 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002841 final Rect frame = new Rect();
2842 final Rect stackBounds = new Rect();
2843
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002844 boolean includeImeInScreenshot;
2845 synchronized(mService.mWindowMap) {
2846 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2847 ? mService.mInputMethodTarget.mAppToken : null;
2848 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2849 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2850 // mode because the frame of the IME might not overlap with that of the app.
2851 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2852 // overlapping with the bottom app.
2853 includeImeInScreenshot = imeTargetAppToken != null
2854 && imeTargetAppToken.appToken != null
2855 && imeTargetAppToken.appToken.asBinder() == appToken
2856 && !mService.mInputMethodTarget.isInMultiWindowMode();
2857 }
2858
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002859 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002860 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002861 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002862 synchronized(mService.mWindowMap) {
2863 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002864 mScreenshotApplicationState.appWin = null;
2865 forAllWindows(w -> {
2866 if (!w.mHasSurface) {
2867 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002868 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002869 if (w.mLayer >= aboveAppLayer) {
2870 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002871 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002872 if (wallpaperOnly && !w.mIsWallpaper) {
2873 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002874 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002875 if (w.mIsImWindow) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002876 if (!includeImeInScreenshot) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002877 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002878 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002879 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002880 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2881 // then the target window state is this one.
2882 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002883 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002884 }
2885
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002886 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002887 // We have not ran across the target window yet, so it is probably behind
2888 // the wallpaper. This can happen when the keyguard is up and all windows
2889 // are moved behind the wallpaper. We don't want to include the wallpaper
2890 // layer in the screenshot as it will cover-up the layer of the target
2891 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002892 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002893 }
2894 // Fall through. The target window is in front of the wallpaper. For this
2895 // case we want to include the wallpaper layer in the screenshot because
2896 // the target window might have some transparent areas.
2897 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002898 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002899 // This app window is of no interest if it is not associated with the
2900 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002901 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002902 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002903 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002904 }
2905
2906 // Include this window.
2907
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002908 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002909 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002910 if (mScreenshotApplicationState.maxLayer < layer) {
2911 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002912 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 if (mScreenshotApplicationState.minLayer > layer) {
2914 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002915 }
2916
2917 // Don't include wallpaper in bounds calculation
Jorim Jaggibfbd9dc2017-01-20 18:13:01 +01002918 if (!mutableIncludeFullDisplay.value && includeDecor) {
2919 final TaskStack stack = w.getStack();
2920 if (stack != null) {
2921 stack.getBounds(frame);
2922 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08002923 } else if (!mutableIncludeFullDisplay.value && !w.mIsWallpaper) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002924 final Rect wf = w.mFrame;
2925 final Rect cr = w.mContentInsets;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002926 int left = wf.left + cr.left;
2927 int top = wf.top + cr.top;
2928 int right = wf.right - cr.right;
2929 int bottom = wf.bottom - cr.bottom;
2930 frame.union(left, top, right, bottom);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002931 w.getVisibleBounds(stackBounds);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002932 if (!Rect.intersects(frame, stackBounds)) {
2933 // Set frame empty if there's no intersection.
2934 frame.setEmpty();
2935 }
2936 }
2937
2938 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002939 (w.mAppToken != null && w.mAppToken.token == appToken)
2940 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002941 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002942 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002943 }
2944
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002945 if (w.isObscuringDisplay()){
2946 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002947 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002948 return false;
2949 }, true /* traverseTopToBottom */);
2950
2951 final WindowState appWin = mScreenshotApplicationState.appWin;
2952 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2953 final int maxLayer = mScreenshotApplicationState.maxLayer;
2954 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955
2956 if (appToken != null && appWin == null) {
2957 // Can't find a window to snapshot.
2958 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2959 "Screenshot: Couldn't find a surface matching " + appToken);
2960 return null;
2961 }
2962
2963 if (!screenshotReady) {
2964 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2965 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2966 appWin.mWinAnimator.mDrawState)));
2967 return null;
2968 }
2969
2970 // Screenshot is ready to be taken. Everything from here below will continue
2971 // through the bottom of the loop and return a value. We only stay in the loop
2972 // because we don't want to release the mWindowMap lock until the screenshot is
2973 // taken.
2974
2975 if (maxLayer == 0) {
2976 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2977 + ": returning null maxLayer=" + maxLayer);
2978 return null;
2979 }
2980
Jorim Jaggi02886a82016-12-06 09:10:06 -08002981 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002982 // Constrain frame to the screen size.
2983 if (!frame.intersect(0, 0, dw, dh)) {
2984 frame.setEmpty();
2985 }
2986 } else {
2987 // Caller just wants entire display.
2988 frame.set(0, 0, dw, dh);
2989 }
2990 if (frame.isEmpty()) {
2991 return null;
2992 }
2993
2994 if (width < 0) {
2995 width = (int) (frame.width() * frameScale);
2996 }
2997 if (height < 0) {
2998 height = (int) (frame.height() * frameScale);
2999 }
3000
3001 // Tell surface flinger what part of the image to crop. Take the top
3002 // right part of the application, and crop the larger dimension to fit.
3003 Rect crop = new Rect(frame);
3004 if (width / (float) frame.width() < height / (float) frame.height()) {
3005 int cropWidth = (int)((float)width / (float)height * frame.height());
3006 crop.right = crop.left + cropWidth;
3007 } else {
3008 int cropHeight = (int)((float)height / (float)width * frame.width());
3009 crop.bottom = crop.top + cropHeight;
3010 }
3011
3012 // The screenshot API does not apply the current screen rotation.
3013 int rot = mDisplay.getRotation();
3014
3015 if (rot == ROTATION_90 || rot == ROTATION_270) {
3016 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3017 }
3018
3019 // Surfaceflinger is not aware of orientation, so convert our logical
3020 // crop to surfaceflinger's portrait orientation.
3021 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3022
3023 if (DEBUG_SCREENSHOT) {
3024 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3025 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003026 forAllWindows(w -> {
3027 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3028 Slog.i(TAG_WM, w + ": " + w.mLayer
3029 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003030 + " surfaceLayer=" + ((controller == null)
3031 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003032 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003033 }
3034
3035 final ScreenRotationAnimation screenRotationAnimation =
3036 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3037 final boolean inRotation = screenRotationAnimation != null &&
3038 screenRotationAnimation.isAnimating();
3039 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3040 "Taking screenshot while rotating");
3041
3042 // We force pending transactions to flush before taking
3043 // the screenshot by pushing an empty synchronous transaction.
3044 SurfaceControl.openTransaction();
3045 SurfaceControl.closeTransactionSync();
3046
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003047 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003048 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003049 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003050 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3051 + ") to layer " + maxLayer);
3052 return null;
3053 }
3054 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003055 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003056 }
3057
3058 // TODO: Can this use createRotationMatrix()?
3059 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3060 if (rot == Surface.ROTATION_90) {
3061 final int tmp = crop.top;
3062 crop.top = dw - crop.right;
3063 crop.right = crop.bottom;
3064 crop.bottom = dw - crop.left;
3065 crop.left = tmp;
3066 } else if (rot == Surface.ROTATION_180) {
3067 int tmp = crop.top;
3068 crop.top = dh - crop.bottom;
3069 crop.bottom = dh - tmp;
3070 tmp = crop.right;
3071 crop.right = dw - crop.left;
3072 crop.left = dw - tmp;
3073 } else if (rot == Surface.ROTATION_270) {
3074 final int tmp = crop.top;
3075 crop.top = crop.left;
3076 crop.left = dh - crop.bottom;
3077 crop.bottom = crop.right;
3078 crop.right = dh - tmp;
3079 }
3080 }
3081
3082 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003083 // Used to indicate the layout is needed.
3084 mTmpWindow = null;
3085
3086 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003087 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003088 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003089 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003090 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003091 w.setDisplayLayoutNeeded();
3092 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003093 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003094
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003095 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003096 mService.mWindowPlacerLocked.performSurfacePlacement();
3097 }
3098 }
3099
Wale Ogunwale1666e312016-12-16 11:27:18 -08003100 void setExitingTokensHasVisible(boolean hasVisible) {
3101 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3102 mExitingTokens.get(i).hasVisible = hasVisible;
3103 }
3104
3105 // Initialize state of exiting applications.
3106 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3107 }
3108
3109 void removeExistingTokensIfPossible() {
3110 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3111 final WindowToken token = mExitingTokens.get(i);
3112 if (!token.hasVisible) {
3113 mExitingTokens.remove(i);
3114 }
3115 }
3116
3117 // Time to remove any exiting applications?
3118 mTaskStackContainers.removeExistingAppTokensIfPossible();
3119 }
3120
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003121 @Override
3122 void onDescendantOverrideConfigurationChanged() {
3123 setLayoutNeeded();
3124 mService.requestTraversal();
3125 }
3126
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003127 static final class TaskForResizePointSearchResult {
3128 boolean searchDone;
3129 Task taskForResize;
3130
3131 void reset() {
3132 searchDone = false;
3133 taskForResize = null;
3134 }
3135 }
Robert Carr3b716242016-08-16 16:02:21 -07003136
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003137 private static final class ApplySurfaceChangesTransactionState {
3138 boolean displayHasContent;
3139 boolean obscured;
3140 boolean syswin;
3141 boolean focusDisplayed;
3142 float preferredRefreshRate;
3143 int preferredModeId;
3144
3145 void reset() {
3146 displayHasContent = false;
3147 obscured = false;
3148 syswin = false;
3149 focusDisplayed = false;
3150 preferredRefreshRate = 0;
3151 preferredModeId = 0;
3152 }
3153 }
3154
3155 private static final class ScreenshotApplicationState {
3156 WindowState appWin;
3157 int maxLayer;
3158 int minLayer;
3159 boolean screenshotReady;
3160
3161 void reset(boolean screenshotReady) {
3162 appWin = null;
3163 maxLayer = 0;
3164 minLayer = 0;
3165 this.screenshotReady = screenshotReady;
3166 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3167 }
3168 }
3169
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003170 /**
3171 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3172 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3173 * homogeneous children type which is currently required by sub-classes of
3174 * {@link WindowContainer} class.
3175 */
3176 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3177
3178 int size() {
3179 return mChildren.size();
3180 }
3181
3182 E get(int index) {
3183 return mChildren.get(index);
3184 }
3185
3186 @Override
3187 boolean fillsParent() {
3188 return true;
3189 }
3190
3191 @Override
3192 boolean isVisible() {
3193 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003194 }
3195 }
3196
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003197 /**
3198 * Window container class that contains all containers on this display relating to Apps.
3199 * I.e Activities.
3200 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003201 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003202
Andrii Kulian839def92016-11-02 10:58:58 -07003203 /**
3204 * Adds the stack to this container.
3205 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3206 */
3207 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003208 if (stack.mStackId == HOME_STACK_ID) {
3209 if (mHomeStack != null) {
3210 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3211 }
3212 mHomeStack = stack;
3213 }
3214 addChild(stack, onTop);
3215 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003216 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003217
Andrii Kulian839def92016-11-02 10:58:58 -07003218 /** Removes the stack from its container and prepare for changing the parent. */
3219 void removeStackFromDisplay(TaskStack stack) {
3220 removeChild(stack);
3221 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003222 }
3223
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003224 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003225 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3226 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003227 addChild(stack, addIndex);
3228 setLayoutNeeded();
3229 }
3230
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003231 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003232 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3233 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3234 // This stack is always-on-top, override the default behavior.
3235 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3236
3237 // Moving to its current position, as we must call super but we don't want to
3238 // perform any meaningful action.
3239 final int currentPosition = mChildren.indexOf(child);
3240 super.positionChildAt(currentPosition, child, false /* includingParents */);
3241 return;
3242 }
3243
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003244 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3245 super.positionChildAt(targetPosition, child, includingParents);
3246
3247 setLayoutNeeded();
3248 }
3249
3250 /**
3251 * When stack is added or repositioned, find a proper position for it.
3252 * This will make sure that pinned stack always stays on top.
3253 * @param requestedPosition Position requested by caller.
3254 * @param stack Stack to be added or positioned.
3255 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3256 * @return The proper position for the stack.
3257 */
3258 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3259 final int topChildPosition = mChildren.size() - 1;
3260 boolean toTop = requestedPosition == POSITION_TOP;
3261 toTop |= adding ? requestedPosition >= topChildPosition + 1
3262 : requestedPosition >= topChildPosition;
3263 int targetPosition = requestedPosition;
3264
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003265 if (toTop && isStackVisible(PINNED_STACK_ID) && stack.mStackId != PINNED_STACK_ID) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003266 // The pinned stack is always the top most stack (always-on-top) when it is visible.
3267 TaskStack topStack = mChildren.get(topChildPosition);
3268 if (topStack.mStackId != PINNED_STACK_ID) {
3269 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3270 }
3271
3272 // So, stack is moved just below the pinned stack.
3273 // When we're adding a new stack the target is the current pinned stack position.
3274 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003275 // stack, because WindowContainer#positionAt() first removes element and then adds
3276 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003277 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3278 }
3279
3280 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003281 }
3282
3283 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003284 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3285 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003286 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003287 if (super.forAllWindows(callback, traverseTopToBottom)) {
3288 return true;
3289 }
3290 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3291 return true;
3292 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003293 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003294 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3295 return true;
3296 }
3297 if (super.forAllWindows(callback, traverseTopToBottom)) {
3298 return true;
3299 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003300 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003301 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003302 }
3303
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003304 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003305 boolean traverseTopToBottom) {
3306 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3307 // app tokens.
3308 // TODO: Investigate if we need to continue to do this or if we can just process them
3309 // in-order.
3310 if (traverseTopToBottom) {
3311 for (int i = mChildren.size() - 1; i >= 0; --i) {
3312 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3313 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003314 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3315 traverseTopToBottom)) {
3316 return true;
3317 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003318 }
3319 }
3320 } else {
3321 final int count = mChildren.size();
3322 for (int i = 0; i < count; ++i) {
3323 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3324 final int appTokensCount = appTokens.size();
3325 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003326 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3327 traverseTopToBottom)) {
3328 return true;
3329 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003330 }
3331 }
3332 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003333 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003334 }
3335
Wale Ogunwale1666e312016-12-16 11:27:18 -08003336 void setExitingTokensHasVisible(boolean hasVisible) {
3337 for (int i = mChildren.size() - 1; i >= 0; --i) {
3338 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3339 for (int j = appTokens.size() - 1; j >= 0; --j) {
3340 appTokens.get(j).hasVisible = hasVisible;
3341 }
3342 }
3343 }
3344
3345 void removeExistingAppTokensIfPossible() {
3346 for (int i = mChildren.size() - 1; i >= 0; --i) {
3347 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3348 for (int j = appTokens.size() - 1; j >= 0; --j) {
3349 final AppWindowToken token = appTokens.get(j);
3350 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3351 && (!token.mIsExiting || token.isEmpty())) {
3352 // Make sure there is no animation running on this token, so any windows
3353 // associated with it will be removed as soon as their animations are
3354 // complete.
3355 token.mAppAnimator.clearAnimation();
3356 token.mAppAnimator.animating = false;
3357 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3358 "performLayout: App token exiting now removed" + token);
3359 token.removeIfPossible();
3360 }
3361 }
3362 }
3363 }
3364
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003365 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003366 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003367 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003368 // Apps and their containers are not allowed to specify an orientation while the
3369 // docked or freeform stack is visible...except for the home stack/task if the
3370 // docked stack is minimized and it actually set something.
3371 if (mHomeStack != null && mHomeStack.isVisible()
3372 && mDividerControllerLocked.isMinimizedDock()) {
3373 final int orientation = mHomeStack.getOrientation();
3374 if (orientation != SCREEN_ORIENTATION_UNSET) {
3375 return orientation;
3376 }
3377 }
3378 return SCREEN_ORIENTATION_UNSPECIFIED;
3379 }
3380
3381 final int orientation = super.getOrientation();
3382 if (orientation != SCREEN_ORIENTATION_UNSET
3383 && orientation != SCREEN_ORIENTATION_BEHIND) {
3384 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3385 "App is requesting an orientation, return " + orientation);
3386 return orientation;
3387 }
3388
3389 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003390 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003391 // The next app has not been requested to be visible, so we keep the current orientation
3392 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003393 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003394 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003395 }
3396
3397 /**
3398 * Window container class that contains all containers on this display that are not related to
3399 * Apps. E.g. status bar.
3400 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003401 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3402 /**
3403 * Compares two child window tokens returns -1 if the first is lesser than the second in
3404 * terms of z-order and 1 otherwise.
3405 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003406 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003407 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003408 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3409 token1.mOwnerCanManageAppTokens)
3410 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3411 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003412
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003413 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3414 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3415 return false;
3416 }
3417 final int req = w.mAttrs.screenOrientation;
3418 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3419 || req == SCREEN_ORIENTATION_UNSET) {
3420 return false;
3421 }
3422 return true;
3423 };
3424
Wale Ogunwale3a931692016-11-02 16:49:48 -07003425 private final String mName;
3426 NonAppWindowContainers(String name) {
3427 mName = name;
3428 }
3429
3430 void addChild(WindowToken token) {
3431 addChild(token, mWindowComparator);
3432 }
3433
3434 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003435 int getOrientation() {
3436 final WindowManagerPolicy policy = mService.mPolicy;
3437 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003438 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003439
3440 if (win != null) {
3441 final int req = win.mAttrs.screenOrientation;
3442 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
3443 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003444 mLastKeyguardForcedOrientation = req;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003445 }
Andrii Kulian8ee72852017-03-10 10:36:45 -08003446 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003447 }
3448
Andrii Kulian8ee72852017-03-10 10:36:45 -08003449 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003450
3451 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003452 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003453 }
3454
3455 return SCREEN_ORIENTATION_UNSET;
3456 }
3457
3458 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003459 String getName() {
3460 return mName;
3461 }
Robert Carr3b716242016-08-16 16:02:21 -07003462 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003463
3464 /**
3465 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3466 */
3467 @FunctionalInterface
3468 private interface Screenshoter<E> {
3469 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3470 boolean useIdentityTransform, int rotation);
3471 }
Craig Mautner59c00972012-07-30 12:10:24 -07003472}