blob: 300f5c598e7a236b237f6205e2f4a913254cd52e [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Wale Ogunwale51362492016-09-08 17:49:17 -070020import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.HOME_STACK_ID;
22import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
25import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070026import static android.view.Display.DEFAULT_DISPLAY;
27import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070028import static android.view.Surface.ROTATION_0;
29import static android.view.Surface.ROTATION_180;
30import static android.view.Surface.ROTATION_270;
31import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070032import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070033import static android.view.WindowManager.DOCKED_BOTTOM;
34import static android.view.WindowManager.DOCKED_INVALID;
35import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080036import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
37import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
38import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070039import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070040import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070041import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
42import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070043import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070045import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070046import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070047import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070049import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
50import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070052import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
53import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070054import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070055import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070056import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
58import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070059import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080060import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070061import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070062import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070063import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
64import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070076import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070077import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070078import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070079import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080080import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070081import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070082import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
83import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070084import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070085import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070086import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
87import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
89import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
90import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070091import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070092import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070093import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070094import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070095import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070096import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -070097import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070098import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070099import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700100
Andrii Kulian3a507b52016-09-19 18:14:12 -0700101import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700102import android.app.ActivityManager.StackId;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700103import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700104import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700105import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100106import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700107import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800108import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700109import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700110import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100111import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700112import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700113import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700114import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700115import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700116import android.os.RemoteException;
117import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700118import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800119import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700120import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700121import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700122import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700123import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700124import android.view.Surface;
125import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700126import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700127
Bryce Lee48f4b572017-04-10 10:54:15 -0700128import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800129import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700130import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700131
Robert Carr3b716242016-08-16 16:02:21 -0700132import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700133import java.io.PrintWriter;
134import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700135import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700136import java.util.HashMap;
137import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700138import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700139import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800140import java.util.function.Consumer;
141import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700142
Craig Mautner59c00972012-07-30 12:10:24 -0700143/**
144 * Utility class for keeping track of the WindowStates and other pertinent contents of a
145 * particular Display.
146 *
147 * IMPORTANT: No method from this class should ever be used without holding
148 * WindowManagerService.mWindowMap.
149 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700150class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700151 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700152
153 /** Unique identifier of this stack. */
154 private final int mDisplayId;
155
Wale Ogunwale3a931692016-11-02 16:49:48 -0700156 /** The containers below are the only child containers the display can have. */
157 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700158 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700159 // Contains all non-app window containers that should be displayed above the app containers
160 // (e.g. Status bar)
161 private final NonAppWindowContainers mAboveAppWindowsContainers =
162 new NonAppWindowContainers("mAboveAppWindowsContainers");
163 // Contains all non-app window containers that should be displayed below the app containers
164 // (e.g. Wallpaper).
165 private final NonAppWindowContainers mBelowAppWindowsContainers =
166 new NonAppWindowContainers("mBelowAppWindowsContainers");
167 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
168 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
169 // window containers together and move them in-sync if/when needed.
170 private final NonAppWindowContainers mImeWindowsContainers =
171 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700172
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000173 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800174 private WindowState mTmpWindow2;
175 private WindowAnimator mTmpWindowAnimator;
176 private boolean mTmpRecoveringMemory;
177 private boolean mUpdateImeTarget;
178 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700179 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700180
Wale Ogunwale02319a62016-09-26 15:21:22 -0700181 // Mapping from a token IBinder to a WindowToken object on this display.
182 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
183
Andrii Kuliancd097992017-03-23 18:31:59 -0700184 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700185 int mInitialDisplayWidth = 0;
186 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700187 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700188
189 /**
190 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
191 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
192 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
193 */
Craig Mautner59c00972012-07-30 12:10:24 -0700194 int mBaseDisplayWidth = 0;
195 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700196 /**
197 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
198 * but can be set from Settings or via shell command "adb shell wm density".
199 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
200 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700201 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700202 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700203 private final DisplayInfo mDisplayInfo = new DisplayInfo();
204 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700205 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700206 /**
207 * For default display it contains real metrics, empty for others.
208 * @see WindowManagerService#createWatermarkInTransaction()
209 */
210 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
211 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
212 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
213 /**
214 * Compat metrics computed based on {@link #mDisplayMetrics}.
215 * @see #updateDisplayAndOrientation(int)
216 */
217 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
218
219 /** The desired scaling factor for compatible apps. */
220 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700221
Andrii Kulian8ee72852017-03-10 10:36:45 -0800222 /**
223 * Current rotation of the display.
224 * Constants as per {@link android.view.Surface.Rotation}.
225 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700226 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800227 */
228 private int mRotation = 0;
229 /**
230 * Last applied orientation of the display.
231 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
232 *
233 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
234 */
235 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
236 /**
237 * Flag indicating that the application is receiving an orientation that has different metrics
238 * than it expected. E.g. Portrait instead of Landscape.
239 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700240 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800241 */
242 private boolean mAltOrientation = false;
243 /**
244 * Orientation forced by some window. If there is no visible window that specifies orientation
245 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
246 *
247 * @see NonAppWindowContainers#getOrientation()
248 */
249 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
250 /**
251 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
252 * occluded.
253 *
254 * @see NonAppWindowContainers#getOrientation()
255 */
256 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
257
Andrii Kulian06d07d62017-03-14 11:11:47 -0700258 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700259 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700260
Craig Mautner39834192012-09-02 07:47:24 -0700261 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700262 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700263 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700264 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800265 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700266
Craig Mautnerdc548482014-02-05 13:35:24 -0800267 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700268 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800269
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800270 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
271 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700272 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700273
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700274 /** Detect user tapping outside of current focused task bounds .*/
275 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700276
Craig Mautner6601b7b2013-04-29 10:29:11 -0700277 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700278 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700279
Craig Mautner6601b7b2013-04-29 10:29:11 -0700280 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800281 private final Rect mTmpRect = new Rect();
282 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700283 private final RectF mTmpRectF = new RectF();
284 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800285 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700286
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800287 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700288
Craig Mautner95da1082014-02-24 17:54:35 -0800289 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700290 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800291
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700292 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700293 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700294
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800295 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700296
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800297 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
298
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000299 private boolean mHaveBootMsg = false;
300 private boolean mHaveApp = false;
301 private boolean mHaveWallpaper = false;
302 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700303
304 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
305
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700306 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
307 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000308 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
309 new ApplySurfaceChangesTransactionState();
310 private final ScreenshotApplicationState mScreenshotApplicationState =
311 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700312
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700313 // True if this display is in the process of being removed. Used to determine if the removal of
314 // the display's direct children should be allowed.
315 private boolean mRemovingDisplay = false;
316
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700317 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800318 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700319 int mInputMethodAnimLayerAdjustment;
320
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800321 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
322 WindowStateAnimator winAnimator = w.mWinAnimator;
323 if (winAnimator.hasSurface()) {
324 final boolean wasAnimating = winAnimator.mWasAnimating;
325 final boolean nowAnimating = winAnimator.stepAnimationLocked(
326 mTmpWindowAnimator.mCurrentTime);
327 winAnimator.mWasAnimating = nowAnimating;
328 mTmpWindowAnimator.orAnimating(nowAnimating);
329
330 if (DEBUG_WALLPAPER) Slog.v(TAG,
331 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
332
333 if (wasAnimating && !winAnimator.mAnimating
334 && mWallpaperController.isWallpaperTarget(w)) {
335 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
336 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
337 if (DEBUG_LAYOUT_REPEATS) {
338 mService.mWindowPlacerLocked.debugLayoutRepeats(
339 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
340 }
341 }
342 }
343
344 final AppWindowToken atoken = w.mAppToken;
345 if (winAnimator.mDrawState == READY_TO_SHOW) {
346 if (atoken == null || atoken.allDrawn) {
347 if (w.performShowLocked()) {
348 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
349 if (DEBUG_LAYOUT_REPEATS) {
350 mService.mWindowPlacerLocked.debugLayoutRepeats(
351 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
352 }
353 }
354 }
355 }
356 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
357 if (appAnimator != null && appAnimator.thumbnail != null) {
358 if (appAnimator.thumbnailTransactionSeq
359 != mTmpWindowAnimator.mAnimTransactionSequence) {
360 appAnimator.thumbnailTransactionSeq =
361 mTmpWindowAnimator.mAnimTransactionSequence;
362 appAnimator.thumbnailLayer = 0;
363 }
364 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
365 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
366 }
367 }
368 };
369
370 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
371 final WindowStateAnimator winAnimator = w.mWinAnimator;
372 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
373 return;
374 }
375
376 final int flags = w.mAttrs.flags;
377
378 // If this window is animating, make a note that we have an animating window and take
379 // care of a request to run a detached wallpaper animation.
380 if (winAnimator.mAnimating) {
381 if (winAnimator.mAnimation != null) {
382 if ((flags & FLAG_SHOW_WALLPAPER) != 0
383 && winAnimator.mAnimation.getDetachWallpaper()) {
384 mTmpWindow = w;
385 }
386 final int color = winAnimator.mAnimation.getBackgroundColor();
387 if (color != 0) {
388 final TaskStack stack = w.getStack();
389 if (stack != null) {
390 stack.setAnimationBackground(winAnimator, color);
391 }
392 }
393 }
394 mTmpWindowAnimator.setAnimating(true);
395 }
396
397 // If this window's app token is running a detached wallpaper animation, make a note so
398 // we can ensure the wallpaper is displayed behind it.
399 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
400 if (appAnimator != null && appAnimator.animation != null
401 && appAnimator.animating) {
402 if ((flags & FLAG_SHOW_WALLPAPER) != 0
403 && appAnimator.animation.getDetachWallpaper()) {
404 mTmpWindow = w;
405 }
406
407 final int color = appAnimator.animation.getBackgroundColor();
408 if (color != 0) {
409 final TaskStack stack = w.getStack();
410 if (stack != null) {
411 stack.setAnimationBackground(winAnimator, color);
412 }
413 }
414 }
415 };
416
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800417 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
418 final int lostFocusUid = mTmpWindow.mOwnerUid;
419 final Handler handler = mService.mH;
420 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
421 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
422 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
423 w.mAttrs.hideTimeoutMilliseconds);
424 }
425 }
426 };
427
428 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
429 final AppWindowToken focusedApp = mService.mFocusedApp;
430 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
431 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
432
433 if (!w.canReceiveKeys()) {
434 return false;
435 }
436
437 final AppWindowToken wtoken = w.mAppToken;
438
439 // If this window's application has been removed, just skip it.
440 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
441 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
442 + (wtoken.removed ? "removed" : "sendingToBottom"));
443 return false;
444 }
445
446 if (focusedApp == null) {
447 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
448 + " using new focus @ " + w);
449 mTmpWindow = w;
450 return true;
451 }
452
453 if (!focusedApp.windowsAreFocusable()) {
454 // Current focused app windows aren't focusable...
455 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
456 + " focusable using new focus @ " + w);
457 mTmpWindow = w;
458 return true;
459 }
460
461 // Descend through all of the app tokens and find the first that either matches
462 // win.mAppToken (return win) or mFocusedApp (return null).
463 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
464 if (focusedApp.compareTo(wtoken) > 0) {
465 // App stack below focused app stack. No focus for you!!!
466 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
467 "findFocusedWindow: Reached focused app=" + focusedApp);
468 mTmpWindow = null;
469 return true;
470 }
471 }
472
473 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
474 mTmpWindow = w;
475 return true;
476 };
477
478 private final Consumer<WindowState> mPrepareWindowSurfaces =
479 w -> w.mWinAnimator.prepareSurfaceLocked(true);
480
481 private final Consumer<WindowState> mPerformLayout = w -> {
482 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
483 // wasting time and funky changes while a window is animating away.
484 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
485 || w.isGoneForLayoutLw();
486
487 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
488 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
489 + " mLayoutAttached=" + w.mLayoutAttached
490 + " screen changed=" + w.isConfigChanged());
491 final AppWindowToken atoken = w.mAppToken;
492 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
493 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
494 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
495 + " parentHidden=" + w.isParentWindowHidden());
496 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
497 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
498 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
499 + " parentHidden=" + w.isParentWindowHidden());
500 }
501
502 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
503 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
504 // since that means "perform layout as normal, just don't display").
505 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
506 || ((w.isConfigChanged() || w.setReportResizeHints())
507 && !w.isGoneForLayoutLw() &&
508 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
509 (w.mHasSurface && w.mAppToken != null &&
510 w.mAppToken.layoutConfigChanges)))) {
511 if (!w.mLayoutAttached) {
512 if (mTmpInitial) {
513 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
514 w.mContentChanged = false;
515 }
516 if (w.mAttrs.type == TYPE_DREAM) {
517 // Don't layout windows behind a dream, so that if it does stuff like hide
518 // the status bar we won't get a bad transition when it goes away.
519 mTmpWindow = w;
520 }
521 w.mLayoutNeeded = false;
522 w.prelayout();
523 mService.mPolicy.layoutWindowLw(w, null);
524 w.mLayoutSeq = mService.mLayoutSeq;
525
526 // Window frames may have changed. Update dim layer with the new bounds.
527 final Task task = w.getTask();
528 if (task != null) {
529 mDimLayerController.updateDimLayer(task);
530 }
531
532 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
533 + " mContainingFrame=" + w.mContainingFrame
534 + " mDisplayFrame=" + w.mDisplayFrame);
535 }
536 }
537 };
538
539 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
540 if (w.mLayoutAttached) {
541 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
542 + " mViewVisibility=" + w.mViewVisibility
543 + " mRelayoutCalled=" + w.mRelayoutCalled);
544 // If this view is GONE, then skip it -- keep the current frame, and let the caller
545 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
546 // windows, since that means "perform layout as normal, just don't display").
547 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
548 return;
549 }
550 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
551 || w.mLayoutNeeded) {
552 if (mTmpInitial) {
553 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
554 w.mContentChanged = false;
555 }
556 w.mLayoutNeeded = false;
557 w.prelayout();
558 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
559 w.mLayoutSeq = mService.mLayoutSeq;
560 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
561 + " mContainingFrame=" + w.mContainingFrame
562 + " mDisplayFrame=" + w.mDisplayFrame);
563 }
564 } else if (w.mAttrs.type == TYPE_DREAM) {
565 // Don't layout windows behind a dream, so that if it does stuff like hide the
566 // status bar we won't get a bad transition when it goes away.
567 mTmpWindow = mTmpWindow2;
568 }
569 };
570
571 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
572 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
573 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
574 return w.canBeImeTarget();
575 };
576
577 private final Consumer<WindowState> mApplyPostLayoutPolicy =
578 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
579 mService.mInputMethodTarget);
580
581 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
582 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
583 final boolean obscuredChanged = w.mObscured !=
584 mTmpApplySurfaceChangesTransactionState.obscured;
585 final RootWindowContainer root = mService.mRoot;
586 // Only used if default window
587 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
588
589 // Update effect.
590 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
591 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
592 final boolean isDisplayed = w.isDisplayedLw();
593
594 if (isDisplayed && w.isObscuringDisplay()) {
595 // This window completely covers everything behind it, so we want to leave all
596 // of them as undimmed (for performance reasons).
597 root.mObscuringWindow = w;
598 mTmpApplySurfaceChangesTransactionState.obscured = true;
599 }
600
601 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
602 root.handleNotObscuredLocked(w,
603 mTmpApplySurfaceChangesTransactionState.obscured,
604 mTmpApplySurfaceChangesTransactionState.syswin);
605
606 if (w.mHasSurface && isDisplayed) {
607 final int type = w.mAttrs.type;
608 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
609 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
610 mTmpApplySurfaceChangesTransactionState.syswin = true;
611 }
612 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
613 && w.mAttrs.preferredRefreshRate != 0) {
614 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
615 = w.mAttrs.preferredRefreshRate;
616 }
617 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
618 && w.mAttrs.preferredDisplayModeId != 0) {
619 mTmpApplySurfaceChangesTransactionState.preferredModeId
620 = w.mAttrs.preferredDisplayModeId;
621 }
622 }
623 }
624
625 w.applyDimLayerIfNeeded();
626
627 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
628 && mWallpaperController.isWallpaperTarget(w)) {
629 // This is the wallpaper target and its obscured state changed... make sure the
630 // current wallpaper's visibility has been updated accordingly.
631 mWallpaperController.updateWallpaperVisibility();
632 }
633
634 w.handleWindowMovedIfNeeded();
635
636 final WindowStateAnimator winAnimator = w.mWinAnimator;
637
638 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
639 w.mContentChanged = false;
640
641 // Moved from updateWindowsAndWallpaperLocked().
642 if (w.mHasSurface) {
643 // Take care of the window being ready to display.
644 final boolean committed = winAnimator.commitFinishDrawingLocked();
645 if (isDefaultDisplay && committed) {
646 if (w.mAttrs.type == TYPE_DREAM) {
647 // HACK: When a dream is shown, it may at that point hide the lock screen.
648 // So we need to redo the layout to let the phone window manager make this
649 // happen.
650 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
651 if (DEBUG_LAYOUT_REPEATS) {
652 surfacePlacer.debugLayoutRepeats(
653 "dream and commitFinishDrawingLocked true",
654 pendingLayoutChanges);
655 }
656 }
657 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
658 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
659 "First draw done in potential wallpaper target " + w);
660 root.mWallpaperMayChange = true;
661 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
662 if (DEBUG_LAYOUT_REPEATS) {
663 surfacePlacer.debugLayoutRepeats(
664 "wallpaper and commitFinishDrawingLocked true",
665 pendingLayoutChanges);
666 }
667 }
668 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700669 final TaskStack stack = w.getStack();
670 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
671 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800672 // Updates the shown frame before we set up the surface. This is needed
673 // because the resizing could change the top-left position (in addition to
674 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
675 // position the surface.
676 //
677 // If an animation is being started, we can't call this method because the
678 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700679 // we do want to update the position if the window is animating. We make an exception
680 // for the bounds animating state, where an application may have been waiting
681 // for an exit animation to start, but instead enters PiP. We need to ensure
682 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800683 winAnimator.computeShownFrameLocked();
684 }
685 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
686 }
687
688 final AppWindowToken atoken = w.mAppToken;
689 if (atoken != null) {
690 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
691 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
692 mTmpUpdateAllDrawn.add(atoken);
693 }
694 }
695
696 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
697 && w.isDisplayedLw()) {
698 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
699 }
700
701 w.updateResizingWindowIfNeeded();
702 };
703
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800704 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800705 * Create new {@link DisplayContent} instance, add itself to the root window container and
706 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700707 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800708 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700709 * @param layersController window layer controller used to assign layer to the windows on this
710 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700711 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
712 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700713 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700714 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700715 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800716
717 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
718 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
719 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
720 + " new=" + display);
721 }
722
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700723 mDisplay = display;
724 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700725 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700726 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700727 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700728 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700729 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700730 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700731 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800732 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700733 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800734 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700735
736 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700737 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700738 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700739 super.addChild(mAboveAppWindowsContainers, null);
740 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800741
742 // Add itself as a child to the root container.
743 mService.mRoot.addChild(this, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700744 }
745
746 int getDisplayId() {
747 return mDisplayId;
748 }
749
Wale Ogunwale02319a62016-09-26 15:21:22 -0700750 WindowToken getWindowToken(IBinder binder) {
751 return mTokenMap.get(binder);
752 }
753
754 AppWindowToken getAppWindowToken(IBinder binder) {
755 final WindowToken token = getWindowToken(binder);
756 if (token == null) {
757 return null;
758 }
759 return token.asAppWindowToken();
760 }
761
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800762 void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700763 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
764 if (dc != null) {
765 // We currently don't support adding a window token to the display if the display
766 // already has the binder mapped to another token. If there is a use case for supporting
767 // this moving forward we will either need to merge the WindowTokens some how or have
768 // the binder map to a list of window tokens.
769 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
770 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
771 }
772 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700773
774 if (token.asAppWindowToken() == null) {
775 // Add non-app token to container hierarchy on the display. App tokens are added through
776 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700777 switch (token.windowType) {
778 case TYPE_WALLPAPER:
779 mBelowAppWindowsContainers.addChild(token);
780 break;
781 case TYPE_INPUT_METHOD:
782 case TYPE_INPUT_METHOD_DIALOG:
783 mImeWindowsContainers.addChild(token);
784 break;
785 default:
786 mAboveAppWindowsContainers.addChild(token);
787 break;
788 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700789 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700790 }
791
792 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700793 final WindowToken token = mTokenMap.remove(binder);
794 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800795 token.setExiting();
796 }
797 return token;
798 }
799
800 /** Changes the display the input window token is housed on to this one. */
801 void reParentWindowToken(WindowToken token) {
802 final DisplayContent prevDc = token.getDisplayContent();
803 if (prevDc == this) {
804 return;
805 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800806 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
807 && token.asAppWindowToken() == null) {
808 // Removed the token from the map, but made sure it's not an app token before removing
809 // from parent.
810 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700811 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800812
813 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700814 }
815
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700816 void removeAppToken(IBinder binder) {
817 final WindowToken token = removeWindowToken(binder);
818 if (token == null) {
819 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
820 return;
821 }
822
823 final AppWindowToken appToken = token.asAppWindowToken();
824
825 if (appToken == null) {
826 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
827 return;
828 }
829
830 appToken.onRemovedFromDisplay();
831 }
832
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700833 Display getDisplay() {
834 return mDisplay;
835 }
836
Craig Mautner59c00972012-07-30 12:10:24 -0700837 DisplayInfo getDisplayInfo() {
838 return mDisplayInfo;
839 }
840
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700841 DisplayMetrics getDisplayMetrics() {
842 return mDisplayMetrics;
843 }
844
Andrii Kulian8ee72852017-03-10 10:36:45 -0800845 int getRotation() {
846 return mRotation;
847 }
848
849 void setRotation(int newRotation) {
850 mRotation = newRotation;
851 }
852
853 int getLastOrientation() {
854 return mLastOrientation;
855 }
856
857 void setLastOrientation(int orientation) {
858 mLastOrientation = orientation;
859 }
860
861 boolean getAltOrientation() {
862 return mAltOrientation;
863 }
864
865 void setAltOrientation(boolean altOrientation) {
866 mAltOrientation = altOrientation;
867 }
868
869 int getLastWindowForcedOrientation() {
870 return mLastWindowForcedOrientation;
871 }
872
Andrii Kulian06d07d62017-03-14 11:11:47 -0700873 /**
874 * Update rotation of the display.
875 *
876 * Returns true if the rotation has been changed. In this case YOU MUST CALL
877 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
878 */
879 boolean updateRotationUnchecked(boolean inTransaction) {
880 if (mService.mDeferredRotationPauseCount > 0) {
881 // Rotation updates have been paused temporarily. Defer the update until
882 // updates have been resumed.
883 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
884 return false;
885 }
886
887 ScreenRotationAnimation screenRotationAnimation =
888 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
889 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
890 // Rotation updates cannot be performed while the previous rotation change
891 // animation is still in progress. Skip this update. We will try updating
892 // again after the animation is finished and the display is unfrozen.
893 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
894 return false;
895 }
896 if (mService.mDisplayFrozen) {
897 // Even if the screen rotation animation has finished (e.g. isAnimating
898 // returns false), there is still some time where we haven't yet unfrozen
899 // the display. We also need to abort rotation here.
900 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
901 "Deferring rotation, still finishing previous rotation");
902 return false;
903 }
904
905 if (!mService.mDisplayEnabled) {
906 // No point choosing a rotation if the display is not enabled.
907 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
908 return false;
909 }
910
911 final int oldRotation = mRotation;
912 final int lastOrientation = mLastOrientation;
913 final boolean oldAltOrientation = mAltOrientation;
914 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700915 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
916 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700917
Robert Carr897215d2017-03-29 12:25:50 -0700918 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700919 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
920 if (seamlessRotated != null) {
921 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
922 // to complete (that is, waiting for windows to redraw). It's tempting to check
923 // w.mSeamlessRotationCount but that could be incorrect in the case of
924 // window-removal.
925 return false;
926 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700927 }
928
929 // TODO: Implement forced rotation changes.
930 // Set mAltOrientation to indicate that the application is receiving
931 // an orientation that has different metrics than it expected.
932 // eg. Portrait instead of Landscape.
933
934 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
935 lastOrientation, rotation);
936
937 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
938 + ", got rotation " + rotation + " which has "
939 + (altOrientation ? "incompatible" : "compatible") + " metrics");
940
941 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
942 // No change.
943 return false;
944 }
945
946 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
947 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
948 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
949
950 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
951 mService.mWaitingForConfig = true;
952 }
953
954 mRotation = rotation;
955 mAltOrientation = altOrientation;
956 if (isDefaultDisplay) {
957 mService.mPolicy.setRotationLw(rotation);
958 }
959
960 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
961 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
962 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
963 WINDOW_FREEZE_TIMEOUT_DURATION);
964
965 setLayoutNeeded();
966 final int[] anim = new int[2];
967 if (isDimming()) {
968 anim[0] = anim[1] = 0;
969 } else {
970 mService.mPolicy.selectRotationAnimationLw(anim);
971 }
972
973 if (!rotateSeamlessly) {
974 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1]);
975 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
976 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
977 mDisplayId);
978 } else {
979 // The screen rotation animation uses a screenshot to freeze the screen
980 // while windows resize underneath.
981 // When we are rotating seamlessly, we allow the elements to transition
982 // to their rotated state independently and without a freeze required.
983 screenRotationAnimation = null;
984
985 // We have to reset this in case a window was removed before it
986 // finished seamless rotation.
987 mService.mSeamlessRotationCount = 0;
988 }
989
990 // We need to update our screen size information to match the new rotation. If the rotation
991 // has actually changed then this method will return true and, according to the comment at
992 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
993 // By updating the Display info here it will be available to
994 // #computeScreenConfiguration() later.
995 updateDisplayAndOrientation(getConfiguration().uiMode);
996
997 if (!inTransaction) {
998 if (SHOW_TRANSACTIONS) {
999 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1000 }
1001 mService.openSurfaceTransaction();
1002 }
1003 try {
1004 // NOTE: We disable the rotation in the emulator because
1005 // it doesn't support hardware OpenGL emulation yet.
1006 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1007 && screenRotationAnimation.hasScreenshot()) {
1008 if (screenRotationAnimation.setRotationInTransaction(
1009 rotation, mService.mFxSession,
1010 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1011 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1012 mService.scheduleAnimationLocked();
1013 }
1014 }
1015
1016 if (rotateSeamlessly) {
1017 forAllWindows(w -> {
1018 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1019 }, true /* traverseTopToBottom */);
1020 }
1021
1022 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1023 } finally {
1024 if (!inTransaction) {
1025 mService.closeSurfaceTransaction();
1026 if (SHOW_LIGHT_TRANSACTIONS) {
1027 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1028 }
1029 }
1030 }
1031
1032 forAllWindows(w -> {
1033 // Discard surface after orientation change, these can't be reused.
1034 if (w.mAppToken != null) {
1035 w.mAppToken.destroySavedSurfaces();
1036 }
1037 if (w.mHasSurface && !rotateSeamlessly) {
1038 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
1039 w.mOrientationChanging = true;
1040 mService.mRoot.mOrientationChangeComplete = false;
1041 w.mLastFreezeDuration = 0;
1042 }
1043 w.mReportOrientationChanged = true;
1044 }, true /* traverseTopToBottom */);
1045
1046 if (rotateSeamlessly) {
1047 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1048 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1049 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1050 }
1051
1052 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1053 final WindowManagerService.RotationWatcher rotationWatcher
1054 = mService.mRotationWatchers.get(i);
1055 if (rotationWatcher.mDisplayId == mDisplayId) {
1056 try {
1057 rotationWatcher.mWatcher.onRotationChanged(rotation);
1058 } catch (RemoteException e) {
1059 // Ignore
1060 }
1061 }
1062 }
1063
1064 // TODO (multi-display): Magnification is supported only for the default display.
1065 // Announce rotation only if we will not animate as we already have the
1066 // windows in final state. Otherwise, we make this call at the rotation end.
1067 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1068 && isDefaultDisplay) {
1069 mService.mAccessibilityController.onRotationChangedLocked(this);
1070 }
1071
1072 return true;
1073 }
1074
1075 /**
1076 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1077 * changed.
1078 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1079 */
1080 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1081 // Use the effective "visual" dimensions based on current rotation
1082 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1083 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1084 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1085 int dw = realdw;
1086 int dh = realdh;
1087
1088 if (mAltOrientation) {
1089 if (realdw > realdh) {
1090 // Turn landscape into portrait.
1091 int maxw = (int)(realdh/1.3f);
1092 if (maxw < realdw) {
1093 dw = maxw;
1094 }
1095 } else {
1096 // Turn portrait into landscape.
1097 int maxh = (int)(realdw/1.3f);
1098 if (maxh < realdh) {
1099 dh = maxh;
1100 }
1101 }
1102 }
1103
1104 // Update application display metrics.
1105 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1106 mDisplayId);
1107 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1108 mDisplayId);
1109 mDisplayInfo.rotation = mRotation;
1110 mDisplayInfo.logicalWidth = dw;
1111 mDisplayInfo.logicalHeight = dh;
1112 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1113 mDisplayInfo.appWidth = appWidth;
1114 mDisplayInfo.appHeight = appHeight;
1115 if (isDefaultDisplay) {
1116 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1117 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1118 }
1119 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1120 if (mDisplayScalingDisabled) {
1121 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1122 } else {
1123 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1124 }
1125
1126 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1127 mDisplayInfo);
1128
1129 mBaseDisplayRect.set(0, 0, dw, dh);
1130
1131 if (isDefaultDisplay) {
1132 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1133 mCompatDisplayMetrics);
1134 }
1135 return mDisplayInfo;
1136 }
1137
1138 /**
1139 * Compute display configuration based on display properties and policy settings.
1140 * Do not call if mDisplayReady == false.
1141 */
1142 void computeScreenConfiguration(Configuration config) {
1143 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1144
1145 final int dw = displayInfo.logicalWidth;
1146 final int dh = displayInfo.logicalHeight;
1147 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1148 Configuration.ORIENTATION_LANDSCAPE;
1149 config.screenWidthDp =
1150 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1151 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1152 config.screenHeightDp =
1153 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1154 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001155
1156 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1157 final int leftInset = mTmpRect.left;
1158 final int topInset = mTmpRect.top;
1159 // appBounds at the root level should mirror the app screen size.
1160 config.setAppBounds(leftInset /*left*/, topInset /*top*/, leftInset + displayInfo.appWidth /*right*/,
1161 topInset + displayInfo.appHeight /*bottom*/);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001162 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1163 || displayInfo.rotation == Surface.ROTATION_270);
1164
1165 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1166 mDisplayMetrics.density, config);
1167
1168 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1169 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1170 ? Configuration.SCREENLAYOUT_ROUND_YES
1171 : Configuration.SCREENLAYOUT_ROUND_NO);
1172
1173 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1174 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1175 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1176 dh, mDisplayId);
1177 config.densityDpi = displayInfo.logicalDensityDpi;
1178
1179 config.colorMode =
1180 (displayInfo.isHdr()
1181 ? Configuration.COLOR_MODE_HDR_YES
1182 : Configuration.COLOR_MODE_HDR_NO)
1183 | (displayInfo.isWideColorGamut()
1184 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1185 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1186
1187 // Update the configuration based on available input devices, lid switch,
1188 // and platform configuration.
1189 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1190 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1191 config.navigation = Configuration.NAVIGATION_NONAV;
1192
1193 int keyboardPresence = 0;
1194 int navigationPresence = 0;
1195 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1196 final int len = devices != null ? devices.length : 0;
1197 for (int i = 0; i < len; i++) {
1198 InputDevice device = devices[i];
1199 if (!device.isVirtual()) {
1200 final int sources = device.getSources();
1201 final int presenceFlag = device.isExternal() ?
1202 WindowManagerPolicy.PRESENCE_EXTERNAL :
1203 WindowManagerPolicy.PRESENCE_INTERNAL;
1204
1205 // TODO(multi-display): Configure on per-display basis.
1206 if (mService.mIsTouchDevice) {
1207 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1208 InputDevice.SOURCE_TOUCHSCREEN) {
1209 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1210 }
1211 } else {
1212 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1213 }
1214
1215 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1216 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1217 navigationPresence |= presenceFlag;
1218 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1219 && config.navigation == Configuration.NAVIGATION_NONAV) {
1220 config.navigation = Configuration.NAVIGATION_DPAD;
1221 navigationPresence |= presenceFlag;
1222 }
1223
1224 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1225 config.keyboard = Configuration.KEYBOARD_QWERTY;
1226 keyboardPresence |= presenceFlag;
1227 }
1228 }
1229 }
1230
1231 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1232 config.navigation = Configuration.NAVIGATION_DPAD;
1233 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1234 }
1235
1236 // Determine whether a hard keyboard is available and enabled.
1237 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1238 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1239 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1240 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1241 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1242 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1243 }
1244
1245 // Let the policy update hidden states.
1246 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1247 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1248 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1249 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1250 }
1251
1252 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1253 int displayId) {
1254 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1255 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1256 final int unrotDw, unrotDh;
1257 if (rotated) {
1258 unrotDw = dh;
1259 unrotDh = dw;
1260 } else {
1261 unrotDw = dw;
1262 unrotDh = dh;
1263 }
1264 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1265 displayId);
1266 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1267 displayId);
1268 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1269 displayId);
1270 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1271 displayId);
1272 return sw;
1273 }
1274
1275 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1276 DisplayMetrics dm, int dw, int dh, int displayId) {
1277 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1278 displayId);
1279 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1280 uiMode, displayId);
1281 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1282 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1283 if (curSize == 0 || size < curSize) {
1284 curSize = size;
1285 }
1286 return curSize;
1287 }
1288
1289 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1290 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1291
1292 // We need to determine the smallest width that will occur under normal
1293 // operation. To this, start with the base screen size and compute the
1294 // width under the different possible rotations. We need to un-rotate
1295 // the current screen dimensions before doing this.
1296 int unrotDw, unrotDh;
1297 if (rotated) {
1298 unrotDw = dh;
1299 unrotDh = dw;
1300 } else {
1301 unrotDw = dw;
1302 unrotDh = dh;
1303 }
1304 displayInfo.smallestNominalAppWidth = 1<<30;
1305 displayInfo.smallestNominalAppHeight = 1<<30;
1306 displayInfo.largestNominalAppWidth = 0;
1307 displayInfo.largestNominalAppHeight = 0;
1308 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1309 unrotDh);
1310 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1311 unrotDw);
1312 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1313 unrotDh);
1314 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1315 unrotDw);
1316 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1317 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1318 displayId);
1319 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1320 displayId);
1321 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1322 displayId);
1323 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1324 displayId);
1325 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1326 outConfig.screenLayout = sl;
1327 }
1328
1329 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1330 int uiMode, int displayId) {
1331 // Get the app screen size at this rotation.
1332 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1333 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1334
1335 // Compute the screen layout size class for this rotation.
1336 int longSize = w;
1337 int shortSize = h;
1338 if (longSize < shortSize) {
1339 int tmp = longSize;
1340 longSize = shortSize;
1341 shortSize = tmp;
1342 }
1343 longSize = (int)(longSize/density);
1344 shortSize = (int)(shortSize/density);
1345 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1346 }
1347
1348 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1349 int uiMode, int dw, int dh) {
1350 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1351 displayId);
1352 if (width < displayInfo.smallestNominalAppWidth) {
1353 displayInfo.smallestNominalAppWidth = width;
1354 }
1355 if (width > displayInfo.largestNominalAppWidth) {
1356 displayInfo.largestNominalAppWidth = width;
1357 }
1358 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1359 displayId);
1360 if (height < displayInfo.smallestNominalAppHeight) {
1361 displayInfo.smallestNominalAppHeight = height;
1362 }
1363 if (height > displayInfo.largestNominalAppHeight) {
1364 displayInfo.largestNominalAppHeight = height;
1365 }
1366 }
1367
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001368 DockedStackDividerController getDockedDividerController() {
1369 return mDividerControllerLocked;
1370 }
1371
Winson Chung655332c2016-10-31 13:14:28 -07001372 PinnedStackController getPinnedStackController() {
1373 return mPinnedStackControllerLocked;
1374 }
1375
Jeff Browna506a6e2013-06-04 00:02:38 -07001376 /**
1377 * Returns true if the specified UID has access to this display.
1378 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001379 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001380 return mDisplay.hasAccess(uid);
1381 }
1382
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001383 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001384 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001385 }
1386
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001387 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001388 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001389 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001390 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001391 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001392 }
1393
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001394 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001395 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1396 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001397 if (stack.mStackId == stackId) {
1398 return stack;
1399 }
1400 }
1401 return null;
1402 }
1403
Bryce Lee48f4b572017-04-10 10:54:15 -07001404 @VisibleForTesting
1405 int getStackCount() {
1406 return mTaskStackContainers.size();
1407 }
1408
1409 @VisibleForTesting
1410 int getStaskPosById(int stackId) {
1411 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1412 final TaskStack stack = mTaskStackContainers.get(i);
1413 if (stack.mStackId == stackId) {
1414 return i;
1415 }
1416 }
1417 return -1;
1418 }
1419
Andrii Kulian441e4492016-09-29 15:25:00 -07001420 @Override
1421 void onConfigurationChanged(Configuration newParentConfig) {
1422 super.onConfigurationChanged(newParentConfig);
1423
Andrii Kulian3a507b52016-09-19 18:14:12 -07001424 // The display size information is heavily dependent on the resources in the current
1425 // configuration, so we need to reconfigure it every time the configuration changes.
1426 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1427 mService.reconfigureDisplayLocked(this);
1428
1429 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001430 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001431 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001432
Andrii Kulian441e4492016-09-29 15:25:00 -07001433 /**
1434 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1435 * bounds were updated.
1436 */
1437 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001438 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1439 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001440 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001441 changedStackList.add(stack.mStackId);
1442 }
1443 }
Winson Chung32c566f2017-04-11 18:31:21 -07001444
1445 // If there was no pinned stack, we still need to notify the controller of the display info
1446 // update as a result of the config change. We do this here to consolidate the flow between
1447 // changes when there is and is not a stack.
1448 if (getStackById(PINNED_STACK_ID) == null) {
1449 mPinnedStackControllerLocked.onDisplayInfoChanged();
1450 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001451 }
1452
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001453 @Override
1454 boolean fillsParent() {
1455 return true;
1456 }
1457
1458 @Override
1459 boolean isVisible() {
1460 return true;
1461 }
1462
1463 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001464 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001465 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001466 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001467 }
1468
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001469 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001470 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1471 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1472 // target, or here in order if there isn't an IME target.
1473 if (traverseTopToBottom) {
1474 for (int i = mChildren.size() - 1; i >= 0; --i) {
1475 final DisplayChildWindowContainer child = mChildren.get(i);
1476 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1477 // In this case the Ime windows will be processed above their target so we skip
1478 // here.
1479 continue;
1480 }
1481 if (child.forAllWindows(callback, traverseTopToBottom)) {
1482 return true;
1483 }
1484 }
1485 } else {
1486 final int count = mChildren.size();
1487 for (int i = 0; i < count; i++) {
1488 final DisplayChildWindowContainer child = mChildren.get(i);
1489 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1490 // In this case the Ime windows will be processed above their target so we skip
1491 // here.
1492 continue;
1493 }
1494 if (child.forAllWindows(callback, traverseTopToBottom)) {
1495 return true;
1496 }
1497 }
1498 }
1499 return false;
1500 }
1501
1502 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1503 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1504 }
1505
1506 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001507 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001508 final WindowManagerPolicy policy = mService.mPolicy;
1509
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001510 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001511 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001512 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001513 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001514 // If the display is frozen, some activities may be in the middle of restarting, and
1515 // thus have removed their old window. If the window has the flag to hide the lock
1516 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1517 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001518 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001519 } else if (policy.isKeyguardLocked()) {
1520 // Use the last orientation the while the display is frozen with the keyguard
1521 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1522 // window. We don't want to check the show when locked window directly though as
1523 // things aren't stable while the display is frozen, for example the window could be
1524 // momentarily unavailable due to activity relaunch.
1525 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001526 + "return " + mLastOrientation);
1527 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001528 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001529 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001530 final int orientation = mAboveAppWindowsContainers.getOrientation();
1531 if (orientation != SCREEN_ORIENTATION_UNSET) {
1532 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001533 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001534 }
1535
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001536 // Top system windows are not requesting an orientation. Start searching from apps.
1537 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001538 }
1539
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001540 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001541 // Check if display metrics changed and update base values if needed.
1542 updateBaseDisplayMetricsIfNeeded();
1543
Craig Mautner722285e2012-09-07 13:55:58 -07001544 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001545 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001546
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001547 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1548 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001549 }
Craig Mautner722285e2012-09-07 13:55:58 -07001550 }
1551
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001552 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001553 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1554 if (displayManagerInternal != null) {
1555 // Bootstrap the default logical display from the display manager.
1556 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1557 if (newDisplayInfo != null) {
1558 mDisplayInfo.copyFrom(newDisplayInfo);
1559 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001560 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001561
Andrii Kuliancd097992017-03-23 18:31:59 -07001562 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1563 mDisplayInfo.logicalDensityDpi);
1564 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1565 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1566 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001567 }
1568
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001569 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001570 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001571 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001572 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001573 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1574 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001575 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001576 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001577 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001578 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001579 out.set(left, top, left + width, top + height);
1580 }
1581
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001582 private void getLogicalDisplayRect(Rect out, int orientation) {
1583 getLogicalDisplayRect(out);
1584
1585 // Rotate the Rect if needed.
1586 final int currentRotation = mDisplayInfo.rotation;
1587 final int rotationDelta = deltaRotation(currentRotation, orientation);
1588 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1589 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1590 mTmpRectF.set(out);
1591 mTmpMatrix.mapRect(mTmpRectF);
1592 mTmpRectF.round(out);
1593 }
1594 }
1595
Andrii Kuliancd097992017-03-23 18:31:59 -07001596 /**
1597 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1598 * values.
1599 */
1600 private void updateBaseDisplayMetricsIfNeeded() {
1601 // Get real display metrics without overrides from WM.
1602 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1603 final int orientation = mDisplayInfo.rotation;
1604 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1605 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1606 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1607 final int newDensity = mDisplayInfo.logicalDensityDpi;
1608
1609 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1610 || mInitialDisplayHeight != newHeight
1611 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1612
1613 if (displayMetricsChanged) {
1614 // Check if display size or density is forced.
1615 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1616 || mBaseDisplayHeight != mInitialDisplayHeight;
1617 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1618
1619 // If there is an override set for base values - use it, otherwise use new values.
1620 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1621 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1622 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1623
1624 // Real display metrics changed, so we should also update initial values.
1625 mInitialDisplayWidth = newWidth;
1626 mInitialDisplayHeight = newHeight;
1627 mInitialDisplayDensity = newDensity;
1628 mService.reconfigureDisplayLocked(this);
1629 }
1630 }
1631
Bryce Lee27cec322017-03-21 09:41:37 -07001632 /** Sets the maximum width the screen resolution can be */
1633 void setMaxUiWidth(int width) {
1634 if (DEBUG_DISPLAY) {
1635 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1636 }
1637
1638 mMaxUiWidth = width;
1639
1640 // Update existing metrics.
1641 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1642 }
1643
1644 /** Update base (override) display metrics. */
1645 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1646 mBaseDisplayWidth = baseWidth;
1647 mBaseDisplayHeight = baseHeight;
1648 mBaseDisplayDensity = baseDensity;
1649
1650 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1651 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1652 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1653 mBaseDisplayWidth = mMaxUiWidth;
1654
1655 if (DEBUG_DISPLAY) {
1656 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1657 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1658 + " on display:" + getDisplayId());
1659 }
1660 }
1661
1662 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1663 }
1664
Chong Zhangf66db432016-01-13 10:39:51 -08001665 void getContentRect(Rect out) {
1666 out.set(mContentRect);
1667 }
1668
Wale Ogunwale1666e312016-12-16 11:27:18 -08001669 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1670 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1671 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001672
Wale Ogunwale1666e312016-12-16 11:27:18 -08001673 TaskStack stack = getStackById(stackId);
1674 if (stack != null) {
1675 // It's already attached to the display...clear mDeferRemoval and move stack to
1676 // appropriate z-order on display as needed.
1677 stack.mDeferRemoval = false;
1678 // We're not moving the display to front when we're adding stacks, only when
1679 // requested to change the position of stack explicitly.
1680 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1681 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001682 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001683 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001684 mTaskStackContainers.addStackToDisplay(stack, onTop);
1685 }
1686
Wale Ogunwale1666e312016-12-16 11:27:18 -08001687 if (stackId == DOCKED_STACK_ID) {
1688 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001689 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001690 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001691 }
1692
Andrii Kulian51c1b672017-04-07 18:39:32 -07001693 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001694 final DisplayContent prevDc = stack.getDisplayContent();
1695 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001696 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1697 + " which is not currently attached to any display");
1698 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001699 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001700 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1701 + " to its current displayId=" + mDisplayId);
1702 }
1703
Wale Ogunwale1666e312016-12-16 11:27:18 -08001704 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001705 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001706 }
1707
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001708 @Override
1709 protected void addChild(DisplayChildWindowContainer child,
1710 Comparator<DisplayChildWindowContainer> comparator) {
1711 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1712 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001713
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001714 @Override
1715 protected void addChild(DisplayChildWindowContainer child, int index) {
1716 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1717 }
1718
1719 @Override
1720 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001721 // Only allow removal of direct children from this display if the display is in the process
1722 // of been removed.
1723 if (mRemovingDisplay) {
1724 super.removeChild(child);
1725 return;
1726 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001727 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001728 }
1729
Andrii Kuliand2765632016-12-12 22:26:34 -08001730 @Override
1731 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1732 // Children of the display are statically ordered, so the real intention here is to perform
1733 // the operation on the display and not the static direct children.
1734 getParent().positionChildAt(position, this, includingParents);
1735 }
1736
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001737 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001738 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1739 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001740 final int taskId = stack.taskIdFromPoint(x, y);
1741 if (taskId != -1) {
1742 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001743 }
1744 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001745 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001746 }
1747
Chong Zhang8e89b312015-09-09 15:09:30 -07001748 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001749 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001750 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001751 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001752 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001753 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001754 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001755 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1756 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001757 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001758 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001759 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001760
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001761 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1762 if (mTmpTaskForResizePointSearchResult.searchDone) {
1763 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001764 }
1765 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001766 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001767 }
1768
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001769 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001770 // The provided task is the task on this display with focus, so if WindowManagerService's
1771 // focused app is not on this display, focusedTask will be null.
1772 if (focusedTask == null) {
1773 mTouchExcludeRegion.setEmpty();
1774 } else {
1775 mTouchExcludeRegion.set(mBaseDisplayRect);
1776 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1777 mTmpRect2.setEmpty();
1778 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1779 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1780 stack.setTouchExcludeRegion(
1781 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1782 }
1783 // If we removed the focused task above, add it back and only leave its
1784 // outside touch area in the exclusion. TapDectector is not interested in
1785 // any touch inside the focused task itself.
1786 if (!mTmpRect2.isEmpty()) {
1787 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1788 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001789 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001790 final WindowState inputMethod = mService.mInputMethodWindow;
1791 if (inputMethod != null && inputMethod.isVisibleLw()) {
1792 // If the input method is visible and the user is typing, we don't want these touch
1793 // events to be intercepted and used to change focus. This would likely cause a
1794 // disappearance of the input method.
1795 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001796 if (inputMethod.getDisplayId() == mDisplayId) {
1797 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1798 } else {
1799 // IME is on a different display, so we need to update its tap detector.
1800 // TODO(multidisplay): Remove when IME will always appear on same display.
1801 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1802 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001803 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001804 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1805 WindowState win = mTapExcludedWindows.get(i);
1806 win.getTouchableRegion(mTmpRegion);
1807 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1808 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001809 // TODO(multi-display): Support docked stacks on secondary displays.
1810 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001811 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001812 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001813 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1814 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001815 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001816 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001817 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001818 }
1819
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001820 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001821 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001822 super.switchUser();
1823 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001824 }
1825
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001826 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001827 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1828 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001829 }
1830 }
1831
1832 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001833 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001834 }
1835
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001836 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001837 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001838 }
1839
1840 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001841 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001842 }
1843
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001844 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001845 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001846 }
1847
Wale Ogunwale10124582016-09-15 20:25:50 -07001848 @Override
1849 void removeIfPossible() {
1850 if (isAnimating()) {
1851 mDeferredRemoval = true;
1852 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001853 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001854 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001855 }
1856
Wale Ogunwale10124582016-09-15 20:25:50 -07001857 @Override
1858 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001859 mRemovingDisplay = true;
1860 try {
1861 super.removeImmediately();
1862 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1863 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001864 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001865 mService.unregisterPointerEventListener(mTapDetector);
1866 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1867 }
1868 } finally {
1869 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001870 }
Craig Mautner95da1082014-02-24 17:54:35 -08001871 }
1872
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001873 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001874 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001875 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001876 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1877
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001878 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001879 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001880 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001881 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001882 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001883 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001884 }
1885
Wale Ogunwale10124582016-09-15 20:25:50 -07001886 boolean animateForIme(float interpolatedValue, float animationTarget,
1887 float dividerAnimationTarget) {
1888 boolean updated = false;
1889
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001890 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1891 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001892 if (stack == null || !stack.isAdjustedForIme()) {
1893 continue;
1894 }
1895
1896 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1897 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1898 updated = true;
1899 } else {
1900 mDividerControllerLocked.mLastAnimationProgress =
1901 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1902 mDividerControllerLocked.mLastDividerProgress =
1903 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1904 updated |= stack.updateAdjustForIme(
1905 mDividerControllerLocked.mLastAnimationProgress,
1906 mDividerControllerLocked.mLastDividerProgress,
1907 false /* force */);
1908 }
1909 if (interpolatedValue >= 1f) {
1910 stack.endImeAdjustAnimation();
1911 }
1912 }
1913
1914 return updated;
1915 }
1916
1917 boolean clearImeAdjustAnimation() {
1918 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001919 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1920 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001921 if (stack != null && stack.isAdjustedForIme()) {
1922 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1923 changed = true;
1924 }
1925 }
1926 return changed;
1927 }
1928
1929 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001930 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1931 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001932 if (stack.isVisible() && stack.isAdjustedForIme()) {
1933 stack.beginImeAdjustAnimation();
1934 }
1935 }
1936 }
1937
1938 void adjustForImeIfNeeded() {
1939 final WindowState imeWin = mService.mInputMethodWindow;
1940 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1941 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001942 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001943 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1944 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1945 imeTargetStack.getDockSide() : DOCKED_INVALID;
1946 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1947 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1948 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1949 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1950 final boolean imeHeightChanged = imeVisible &&
1951 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1952
1953 // The divider could be adjusted for IME position, or be thinner than usual,
1954 // or both. There are three possible cases:
1955 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1956 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1957 // - If IME is not visible, divider is not moved and is normal width.
1958
1959 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001960 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1961 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001962 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07001963 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
1964 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001965 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1966 } else {
1967 stack.resetAdjustedForIme(false);
1968 }
1969 }
1970 mDividerControllerLocked.setAdjustedForIme(
1971 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1972 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001973 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1974 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001975 stack.resetAdjustedForIme(!dockVisible);
1976 }
1977 mDividerControllerLocked.setAdjustedForIme(
1978 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
1979 }
Winson Chung655332c2016-10-31 13:14:28 -07001980 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07001981 }
1982
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001983 void setInputMethodAnimLayerAdjustment(int adj) {
1984 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
1985 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08001986 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001987 }
1988
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001989 /**
1990 * If a window that has an animation specifying a colored background and the current wallpaper
1991 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
1992 * suddenly disappear.
1993 */
1994 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001995 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
1996 w -> w.mIsWallpaper && w.isVisibleNow());
1997
1998 if (visibleWallpaper != null) {
1999 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002000 }
2001 return winAnimator.mAnimLayer;
2002 }
2003
Wale Ogunwale10124582016-09-15 20:25:50 -07002004 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002005 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2006 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002007 stack.prepareFreezingTaskBounds();
2008 }
2009 }
2010
Wale Ogunwale94744212015-09-21 19:01:47 -07002011 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002012 getLogicalDisplayRect(mTmpRect, newRotation);
2013
2014 // Compute a transform matrix to undo the coordinate space transformation,
2015 // and present the window at the same physical position it previously occupied.
2016 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2017 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2018
2019 mTmpRectF.set(bounds);
2020 mTmpMatrix.mapRect(mTmpRectF);
2021 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002022 }
2023
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002024 static int deltaRotation(int oldRotation, int newRotation) {
2025 int delta = newRotation - oldRotation;
2026 if (delta < 0) delta += 4;
2027 return delta;
2028 }
2029
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002030 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002031 Matrix outMatrix) {
2032 // For rotations without Z-ordering we don't need the target rectangle's position.
2033 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2034 displayHeight, outMatrix);
2035 }
2036
2037 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2038 float displayWidth, float displayHeight, Matrix outMatrix) {
2039 switch (rotation) {
2040 case ROTATION_0:
2041 outMatrix.reset();
2042 break;
2043 case ROTATION_270:
2044 outMatrix.setRotate(270, 0, 0);
2045 outMatrix.postTranslate(0, displayHeight);
2046 outMatrix.postTranslate(rectTop, 0);
2047 break;
2048 case ROTATION_180:
2049 outMatrix.reset();
2050 break;
2051 case ROTATION_90:
2052 outMatrix.setRotate(90, 0, 0);
2053 outMatrix.postTranslate(displayWidth, 0);
2054 outMatrix.postTranslate(-rectTop, rectLeft);
2055 break;
2056 }
2057 }
2058
Craig Mautnera91f9e22012-09-14 16:22:08 -07002059 public void dump(String prefix, PrintWriter pw) {
2060 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2061 final String subPrefix = " " + prefix;
2062 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2063 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2064 pw.print("dpi");
2065 if (mInitialDisplayWidth != mBaseDisplayWidth
2066 || mInitialDisplayHeight != mBaseDisplayHeight
2067 || mInitialDisplayDensity != mBaseDisplayDensity) {
2068 pw.print(" base=");
2069 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2070 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2071 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002072 if (mDisplayScalingDisabled) {
2073 pw.println(" noscale");
2074 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002075 pw.print(" cur=");
2076 pw.print(mDisplayInfo.logicalWidth);
2077 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2078 pw.print(" app=");
2079 pw.print(mDisplayInfo.appWidth);
2080 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2081 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2082 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2083 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2084 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002085 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2086 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002087
Craig Mautnerdc548482014-02-05 13:35:24 -08002088 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002089 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002090 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2091 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002092 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002093 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002094
Craig Mautnerdc548482014-02-05 13:35:24 -08002095 pw.println();
2096 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002097 pw.println();
2098 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002099 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002100 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002101 pw.print(" Exiting #"); pw.print(i);
2102 pw.print(' '); pw.print(token);
2103 pw.println(':');
2104 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002105 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002106 }
Craig Mautner59c00972012-07-30 12:10:24 -07002107 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002108 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002109 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002110 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002111 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002112 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002113
2114 if (mInputMethodAnimLayerAdjustment != 0) {
2115 pw.println(subPrefix
2116 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2117 }
Craig Mautner59c00972012-07-30 12:10:24 -07002118 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002119
2120 @Override
2121 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002122 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002123 }
2124
2125 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002126 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002127 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002128
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002129 /** Checks if stack with provided id is visible on this display. */
2130 boolean isStackVisible(int stackId) {
2131 final TaskStack stack = getStackById(stackId);
2132 return (stack != null && stack.isVisible());
2133 }
2134
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002135 /**
2136 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2137 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002138 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002139 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002140 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002141 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002142
2143 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002144 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002145 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002146 */
Jorim Jaggife762342016-10-13 14:33:27 +02002147 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002148 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002149 }
2150
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002151 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002152 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002153 final int x = (int) xf;
2154 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002155 final WindowState touchedWin = getWindow(w -> {
2156 final int flags = w.mAttrs.flags;
2157 if (!w.isVisibleLw()) {
2158 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002159 }
2160 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002161 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002162 }
2163
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002164 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002165 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002166 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002167 }
2168
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002169 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002170
2171 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002172 return mTmpRegion.contains(x, y) || touchFlags == 0;
2173 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002174
2175 return touchedWin;
2176 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002177
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002178 boolean canAddToastWindowForUid(int uid) {
2179 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002180 // Also if the app is focused adding more than one toast at
2181 // a time for better backwards compatibility.
2182 final WindowState focusedWindowForUid = getWindow(w ->
2183 w.mOwnerUid == uid && w.isFocused());
2184 if (focusedWindowForUid != null) {
2185 return true;
2186 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002187 final WindowState win = getWindow(w ->
2188 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2189 && !w.mWindowRemovalAllowed);
2190 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002191 }
2192
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002193 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002194 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2195 return;
2196 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002197
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002198 // Used to communicate the old focus to the callback method.
2199 mTmpWindow = oldFocus;
2200
2201 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002202 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002203
2204 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002205 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002206
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002207 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002208
2209 if (mTmpWindow == null) {
2210 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2211 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002212 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002213 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002214 }
2215
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002216 /** Updates the layer assignment of windows on this display. */
2217 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002218 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002219 if (setLayoutNeeded) {
2220 setLayoutNeeded();
2221 }
2222 }
2223
Wale Ogunwale1666e312016-12-16 11:27:18 -08002224 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2225 // moving containers or resizing them. Need to investigate the best way to have it automatically
2226 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002227 void layoutAndAssignWindowLayersIfNeeded() {
2228 mService.mWindowsChanged = true;
2229 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002230
2231 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2232 false /*updateInputWindows*/)) {
2233 assignWindowLayers(false /* setLayoutNeeded */);
2234 }
2235
2236 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2237 mService.mWindowPlacerLocked.performSurfacePlacement();
2238 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2239 }
2240
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002241 /** Returns true if a leaked surface was destroyed */
2242 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002243 // Used to indicate that a surface was leaked.
2244 mTmpWindow = null;
2245 forAllWindows(w -> {
2246 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002247 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002248 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002249 }
2250 if (!mService.mSessions.contains(wsa.mSession)) {
2251 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002252 + w + " surface=" + wsa.mSurfaceController
2253 + " token=" + w.mToken
2254 + " pid=" + w.mSession.mPid
2255 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002256 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002257 mService.mForceRemoves.add(w);
2258 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002259 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002260 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002261 + w + " surface=" + wsa.mSurfaceController
2262 + " token=" + w.mAppToken
2263 + " saved=" + w.hasSavedSurface());
2264 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002265 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002266 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002267 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002268 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002269
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002270 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002271 }
2272
2273 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002274 * Determine and return the window that should be the IME target.
2275 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2276 * @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 +00002277 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002278 WindowState computeImeTarget(boolean updateImeTarget) {
2279 if (mService.mInputMethodWindow == null) {
2280 // There isn't an IME so there shouldn't be a target...That was easy!
2281 if (updateImeTarget) {
2282 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2283 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2284 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2285 }
2286 return null;
2287 }
2288
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002289 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2290 // same display. Or even when the current IME/target are not on the same screen as the next
2291 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002292 mUpdateImeTarget = updateImeTarget;
2293 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002294
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002295
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2297 // to be on top of it, but it is not -really- where input will go. So look down below
2298 // for a real window to target...
2299 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2300 final AppWindowToken token = target.mAppToken;
2301 if (token != null) {
2302 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2303 if (betterTarget != null) {
2304 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002305 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002306 }
2307 }
2308
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002309 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2310 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002311
2312 // Now, a special case -- if the last target's window is in the process of exiting, and is
2313 // above the new target, keep on the last target to avoid flicker. Consider for example a
2314 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2315 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2316 // scrim.
2317 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002318 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2319 && (target == null
2320 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002321 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002322 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002323 }
2324
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002325 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2326 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002327
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002328 if (target == null) {
2329 if (updateImeTarget) {
2330 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2331 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2332 + Debug.getCallers(4) : ""));
2333 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2334 }
2335
2336 return null;
2337 }
2338
2339 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002340 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2341 if (token != null) {
2342
2343 // Now some fun for dealing with window animations that modify the Z order. We need
2344 // to look at all windows below the current target that are in this app, finding the
2345 // highest visible one in layering.
2346 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002347 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002348 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002349 }
2350
2351 if (highestTarget != null) {
2352 final AppTransition appTransition = mService.mAppTransition;
2353 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2354 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2355 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002356 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002357
2358 if (appTransition.isTransitionSet()) {
2359 // If we are currently setting up for an animation, hold everything until we
2360 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002361 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2362 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002363 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002364 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002365 // If the window we are currently targeting is involved with an animation,
2366 // and it is on top of the next target we will be over, then hold off on
2367 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002368 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2369 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002370 }
2371 }
2372 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002373
2374 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2375 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2376 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002377 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002378 }
2379
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002380 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002381 }
2382
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002383 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2384 if (target == mService.mInputMethodTarget
2385 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2386 && mInputMethodAnimLayerAdjustment == layerAdj) {
2387 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002388 }
2389
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002390 mService.mInputMethodTarget = target;
2391 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2392 setInputMethodAnimLayerAdjustment(layerAdj);
2393 assignWindowLayers(false /* setLayoutNeeded */);
2394 }
2395
2396 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2397 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2398 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2399 }
2400
2401 // Used to indicate we have reached the first window in the range we are interested in.
2402 mTmpWindow = null;
2403
2404 // TODO: Figure-out a more efficient way to do this.
2405 final WindowState candidate = getWindow(w -> {
2406 if (w == top) {
2407 // Reached the first window in the range we are interested in.
2408 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002409 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002410 if (mTmpWindow == null) {
2411 return false;
2412 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002413
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002414 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2415 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002416 }
2417 // If we reached the bottom of the range of windows we are considering,
2418 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002419 if (w == bottom) {
2420 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002421 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002422 return false;
2423 });
2424
2425 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002426 }
2427
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002428 void setLayoutNeeded() {
2429 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2430 mLayoutNeeded = true;
2431 }
2432
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002433 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002434 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2435 mLayoutNeeded = false;
2436 }
2437
2438 boolean isLayoutNeeded() {
2439 return mLayoutNeeded;
2440 }
2441
Wale Ogunwale02319a62016-09-26 15:21:22 -07002442 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2443 if (mTokenMap.isEmpty()) {
2444 return;
2445 }
2446 pw.println(" Display #" + mDisplayId);
2447 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2448 while (it.hasNext()) {
2449 final WindowToken token = it.next();
2450 pw.print(" ");
2451 pw.print(token);
2452 if (dumpAll) {
2453 pw.println(':');
2454 token.dump(pw, " ");
2455 } else {
2456 pw.println();
2457 }
2458 }
2459 }
2460
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002461 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002462 final int[] index = new int[1];
2463 forAllWindows(w -> {
2464 final WindowStateAnimator wAnim = w.mWinAnimator;
2465 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2466 index[0] = index[0] + 1;
2467 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002468 }
2469
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002470 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002471 forAllWindows(w -> {
2472 w.mWinAnimator.enableSurfaceTrace(fd);
2473 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002474 }
2475
2476 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002477 forAllWindows(w -> {
2478 w.mWinAnimator.disableSurfaceTrace();
2479 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002480 }
2481
Jorim Jaggife762342016-10-13 14:33:27 +02002482 /**
2483 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2484 */
2485 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2486 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487 forAllWindows(w -> {
2488 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
2489 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002490 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2491 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002492 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002493 }
2494
Wale Ogunwale494009b82016-10-21 09:01:38 -07002495 boolean checkWaitingForWindows() {
2496
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002497 mHaveBootMsg = false;
2498 mHaveApp = false;
2499 mHaveWallpaper = false;
2500 mHaveKeyguard = true;
2501
2502 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002503 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2504 return true;
2505 }
2506 if (w.isDrawnLw()) {
2507 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002508 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002509 } else if (w.mAttrs.type == TYPE_APPLICATION
2510 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002511 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002512 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002513 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002514 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002515 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002516 }
2517 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002518 return false;
2519 });
2520
2521 if (visibleWindow != null) {
2522 // We have a visible window.
2523 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002524 }
2525
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002526 // if the wallpaper service is disabled on the device, we're never going to have
2527 // wallpaper, don't bother waiting for it
2528 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2529 com.android.internal.R.bool.config_enableWallpaperService)
2530 && !mService.mOnlyCore;
2531
Wale Ogunwale494009b82016-10-21 09:01:38 -07002532 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2533 "******** booted=" + mService.mSystemBooted
2534 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002535 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2536 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2537 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002538
2539 // If we are turning on the screen to show the boot message, don't do it until the boot
2540 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002542 return true;
2543 }
2544
2545 // If we are turning on the screen after the boot is completed normally, don't do so until
2546 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002547 if (mService.mSystemBooted
2548 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002549 return true;
2550 }
2551
2552 return false;
2553 }
2554
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002555 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002556 mTmpWindowAnimator = animator;
2557 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002558 }
2559
2560 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002561 resetAnimationBackgroundAnimator();
2562
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002563 // Used to indicate a detached wallpaper.
2564 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002565 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002566
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002567 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002568
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002569 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002570 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002571 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2572 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002573 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2574 }
2575 }
2576
2577 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002578 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002579 }
2580
Wale Ogunwale494009b82016-10-21 09:01:38 -07002581 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002582 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002583 if (imFocus == null) {
2584 return false;
2585 }
2586
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002587 if (DEBUG_INPUT_METHOD) {
2588 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2589 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2590 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002591 }
2592
Wale Ogunwale494009b82016-10-21 09:01:38 -07002593 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2594
2595 if (DEBUG_INPUT_METHOD) {
2596 Slog.i(TAG_WM, "IM target client: " + imeClient);
2597 if (imeClient != null) {
2598 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2599 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2600 }
2601 }
2602
2603 return imeClient != null && imeClient.asBinder() == client.asBinder();
2604 }
2605
2606 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002607 final WindowState win = getWindow(
2608 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2609 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002610 }
2611
2612 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002613 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002614 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002615 final int curValue = w.mSystemUiVisibility;
2616 final int diff = (curValue ^ visibility) & globalDiff;
2617 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002618 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002619 w.mSeq++;
2620 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002621 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002622 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2623 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002624 visibility, newValue, diff);
2625 }
2626 } catch (RemoteException e) {
2627 // so sorry
2628 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002629 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002630 }
2631
2632 void onWindowFreezeTimeout() {
2633 Slog.w(TAG_WM, "Window freeze timeout expired.");
2634 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002635
2636 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002637 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002638 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002639 }
2640 w.mOrientationChanging = false;
2641 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2642 - mService.mDisplayFreezeTime);
2643 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002644 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002645 mService.mWindowPlacerLocked.performSurfacePlacement();
2646 }
2647
2648 void waitForAllWindowsDrawn() {
2649 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650 forAllWindows(w -> {
2651 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2652 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2653 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002654 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002655 w.mLastContentInsets.set(-1, -1, -1, -1);
2656 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002657 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002658 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002659 }
2660
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002661 // TODO: Super crazy long method that should be broken down...
2662 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2663
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002664 final int dw = mDisplayInfo.logicalWidth;
2665 final int dh = mDisplayInfo.logicalHeight;
2666 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2667
2668 mTmpUpdateAllDrawn.clear();
2669
2670 int repeats = 0;
2671 do {
2672 repeats++;
2673 if (repeats > 6) {
2674 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2675 clearLayoutNeeded();
2676 break;
2677 }
2678
2679 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2680 pendingLayoutChanges);
2681
Andrii Kulian839def92016-11-02 10:58:58 -07002682 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2683 // the wallpaper window jumping across displays.
2684 // Remove check for default display when there will be support for multiple wallpaper
2685 // targets (on different displays).
2686 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002687 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002688 }
2689
2690 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2691 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2692 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2693 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002694 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002695 }
2696 }
2697
2698 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2699 setLayoutNeeded();
2700 }
2701
2702 // FIRST LOOP: Perform a layout, if needed.
2703 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2704 performLayout(repeats == 1, false /* updateInputWindows */);
2705 } else {
2706 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2707 }
2708
2709 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2710 pendingLayoutChanges = 0;
2711
2712 if (isDefaultDisplay) {
2713 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002714 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002715 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2716 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2717 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2718 }
2719 } while (pendingLayoutChanges != 0);
2720
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002721 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002722 resetDimming();
2723
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002724 mTmpRecoveringMemory = recoveringMemory;
2725 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002726
2727 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002728 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2729 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2730 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002731 true /* inTraversal, must call performTraversalInTrans... below */);
2732
2733 stopDimmingIfNeeded();
2734
2735 while (!mTmpUpdateAllDrawn.isEmpty()) {
2736 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2737 // See if any windows have been drawn, so they (and others associated with them)
2738 // can now be shown.
2739 atoken.updateAllDrawn(this);
2740 }
2741
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002742 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002743 }
2744
2745 void performLayout(boolean initial, boolean updateInputWindows) {
2746 if (!isLayoutNeeded()) {
2747 return;
2748 }
2749 clearLayoutNeeded();
2750
2751 final int dw = mDisplayInfo.logicalWidth;
2752 final int dh = mDisplayInfo.logicalHeight;
2753
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002754 if (DEBUG_LAYOUT) {
2755 Slog.v(TAG, "-------------------------------------");
2756 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2757 }
2758
Andrii Kulian8ee72852017-03-10 10:36:45 -08002759 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002760 getConfiguration().uiMode);
2761 if (isDefaultDisplay) {
2762 // Not needed on non-default displays.
2763 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2764 mService.mScreenRect.set(0, 0, dw, dh);
2765 }
2766
2767 mService.mPolicy.getContentRectLw(mContentRect);
2768
2769 int seq = mService.mLayoutSeq + 1;
2770 if (seq < 0) seq = 0;
2771 mService.mLayoutSeq = seq;
2772
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002773 // Used to indicate that we have processed the dream window and all additional windows are
2774 // behind it.
2775 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002776 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002777
2778 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002779 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002780
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002781 // Used to indicate that we have processed the dream window and all additional attached
2782 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002783 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002784 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002785
2786 // Now perform layout of attached windows, which usually depend on the position of the
2787 // window they are attached to. XXX does not deal with windows that are attached to windows
2788 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002789 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002790
2791 // Window frames may have changed. Tell the input dispatcher about it.
2792 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2793 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2794 if (updateInputWindows) {
2795 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2796 }
2797
2798 mService.mPolicy.finishLayoutLw();
2799 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2800 }
2801
2802 /**
2803 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2804 * In portrait mode, it grabs the full screenshot.
2805 *
2806 * @param width the width of the target bitmap
2807 * @param height the height of the target bitmap
2808 * @param includeFullDisplay true if the screen should not be cropped before capture
2809 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2810 * @param config of the output bitmap
2811 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002812 * @param includeDecor whether to include window decors, like the status or navigation bar
2813 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002814 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002815 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002816 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002817 boolean wallpaperOnly, boolean includeDecor) {
2818 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2819 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002820 if (bitmap == null) {
2821 return null;
2822 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002823
2824 if (DEBUG_SCREENSHOT) {
2825 // TEST IF IT's ALL BLACK
2826 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2827 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2828 bitmap.getHeight());
2829 boolean allBlack = true;
2830 final int firstColor = buffer[0];
2831 for (int i = 0; i < buffer.length; i++) {
2832 if (buffer[i] != firstColor) {
2833 allBlack = false;
2834 break;
2835 }
2836 }
2837 if (allBlack) {
2838 final WindowState appWin = mScreenshotApplicationState.appWin;
2839 final int maxLayer = mScreenshotApplicationState.maxLayer;
2840 final int minLayer = mScreenshotApplicationState.minLayer;
2841 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2842 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2843 (appWin != null ?
2844 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2845 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2846 }
2847 }
2848
2849 // Create a copy of the screenshot that is immutable and backed in ashmem.
2850 // This greatly reduces the overhead of passing the bitmap between processes.
2851 Bitmap ret = bitmap.createAshmemBitmap(config);
2852 bitmap.recycle();
2853 return ret;
2854 }
2855
2856 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2857 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2858 boolean includeDecor) {
2859 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2860 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2861 }
2862
2863 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2864 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2865 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002866 int dw = mDisplayInfo.logicalWidth;
2867 int dh = mDisplayInfo.logicalHeight;
2868 if (dw == 0 || dh == 0) {
2869 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2870 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2871 return null;
2872 }
2873
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002874 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002875
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002876 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002877 final Rect frame = new Rect();
2878 final Rect stackBounds = new Rect();
2879
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002880 boolean includeImeInScreenshot;
2881 synchronized(mService.mWindowMap) {
2882 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2883 ? mService.mInputMethodTarget.mAppToken : null;
2884 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2885 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2886 // mode because the frame of the IME might not overlap with that of the app.
2887 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2888 // overlapping with the bottom app.
2889 includeImeInScreenshot = imeTargetAppToken != null
2890 && imeTargetAppToken.appToken != null
2891 && imeTargetAppToken.appToken.asBinder() == appToken
2892 && !mService.mInputMethodTarget.isInMultiWindowMode();
2893 }
2894
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002895 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002896 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002897 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002898 synchronized(mService.mWindowMap) {
2899 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002900 mScreenshotApplicationState.appWin = null;
2901 forAllWindows(w -> {
2902 if (!w.mHasSurface) {
2903 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002904 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002905 if (w.mLayer >= aboveAppLayer) {
2906 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002907 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002908 if (wallpaperOnly && !w.mIsWallpaper) {
2909 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002910 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002911 if (w.mIsImWindow) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002912 if (!includeImeInScreenshot) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002914 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002915 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002916 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2917 // then the target window state is this one.
2918 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002919 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002920 }
2921
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002922 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002923 // We have not ran across the target window yet, so it is probably behind
2924 // the wallpaper. This can happen when the keyguard is up and all windows
2925 // are moved behind the wallpaper. We don't want to include the wallpaper
2926 // layer in the screenshot as it will cover-up the layer of the target
2927 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002928 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002929 }
2930 // Fall through. The target window is in front of the wallpaper. For this
2931 // case we want to include the wallpaper layer in the screenshot because
2932 // the target window might have some transparent areas.
2933 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002934 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002935 // This app window is of no interest if it is not associated with the
2936 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002937 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002938 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002939 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002940 }
2941
2942 // Include this window.
2943
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002944 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002945 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002946 if (mScreenshotApplicationState.maxLayer < layer) {
2947 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002948 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002949 if (mScreenshotApplicationState.minLayer > layer) {
2950 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951 }
2952
2953 // Don't include wallpaper in bounds calculation
Jorim Jaggibfbd9dc2017-01-20 18:13:01 +01002954 if (!mutableIncludeFullDisplay.value && includeDecor) {
2955 final TaskStack stack = w.getStack();
2956 if (stack != null) {
2957 stack.getBounds(frame);
2958 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02002959
2960 // We want to screenshot with the exact bounds of the surface of the app. Thus,
2961 // intersect it with the frame.
2962 frame.intersect(w.mFrame);
2963 }else if (!mutableIncludeFullDisplay.value && !w.mIsWallpaper) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002964 final Rect wf = w.mFrame;
2965 final Rect cr = w.mContentInsets;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966 int left = wf.left + cr.left;
2967 int top = wf.top + cr.top;
2968 int right = wf.right - cr.right;
2969 int bottom = wf.bottom - cr.bottom;
2970 frame.union(left, top, right, bottom);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002971 w.getVisibleBounds(stackBounds);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972 if (!Rect.intersects(frame, stackBounds)) {
2973 // Set frame empty if there's no intersection.
2974 frame.setEmpty();
2975 }
2976 }
2977
2978 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002979 (w.mAppToken != null && w.mAppToken.token == appToken)
2980 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002981 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002982 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002983 }
2984
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002985 if (w.isObscuringDisplay()){
2986 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002987 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002988 return false;
2989 }, true /* traverseTopToBottom */);
2990
2991 final WindowState appWin = mScreenshotApplicationState.appWin;
2992 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2993 final int maxLayer = mScreenshotApplicationState.maxLayer;
2994 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002995
2996 if (appToken != null && appWin == null) {
2997 // Can't find a window to snapshot.
2998 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2999 "Screenshot: Couldn't find a surface matching " + appToken);
3000 return null;
3001 }
3002
3003 if (!screenshotReady) {
3004 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3005 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3006 appWin.mWinAnimator.mDrawState)));
3007 return null;
3008 }
3009
3010 // Screenshot is ready to be taken. Everything from here below will continue
3011 // through the bottom of the loop and return a value. We only stay in the loop
3012 // because we don't want to release the mWindowMap lock until the screenshot is
3013 // taken.
3014
3015 if (maxLayer == 0) {
3016 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3017 + ": returning null maxLayer=" + maxLayer);
3018 return null;
3019 }
3020
Jorim Jaggi02886a82016-12-06 09:10:06 -08003021 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003022 // Constrain frame to the screen size.
3023 if (!frame.intersect(0, 0, dw, dh)) {
3024 frame.setEmpty();
3025 }
3026 } else {
3027 // Caller just wants entire display.
3028 frame.set(0, 0, dw, dh);
3029 }
3030 if (frame.isEmpty()) {
3031 return null;
3032 }
3033
3034 if (width < 0) {
3035 width = (int) (frame.width() * frameScale);
3036 }
3037 if (height < 0) {
3038 height = (int) (frame.height() * frameScale);
3039 }
3040
3041 // Tell surface flinger what part of the image to crop. Take the top
3042 // right part of the application, and crop the larger dimension to fit.
3043 Rect crop = new Rect(frame);
3044 if (width / (float) frame.width() < height / (float) frame.height()) {
3045 int cropWidth = (int)((float)width / (float)height * frame.height());
3046 crop.right = crop.left + cropWidth;
3047 } else {
3048 int cropHeight = (int)((float)height / (float)width * frame.width());
3049 crop.bottom = crop.top + cropHeight;
3050 }
3051
3052 // The screenshot API does not apply the current screen rotation.
3053 int rot = mDisplay.getRotation();
3054
3055 if (rot == ROTATION_90 || rot == ROTATION_270) {
3056 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3057 }
3058
3059 // Surfaceflinger is not aware of orientation, so convert our logical
3060 // crop to surfaceflinger's portrait orientation.
3061 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3062
3063 if (DEBUG_SCREENSHOT) {
3064 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3065 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003066 forAllWindows(w -> {
3067 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3068 Slog.i(TAG_WM, w + ": " + w.mLayer
3069 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003070 + " surfaceLayer=" + ((controller == null)
3071 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003072 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003073 }
3074
3075 final ScreenRotationAnimation screenRotationAnimation =
3076 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3077 final boolean inRotation = screenRotationAnimation != null &&
3078 screenRotationAnimation.isAnimating();
3079 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3080 "Taking screenshot while rotating");
3081
3082 // We force pending transactions to flush before taking
3083 // the screenshot by pushing an empty synchronous transaction.
3084 SurfaceControl.openTransaction();
3085 SurfaceControl.closeTransactionSync();
3086
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003087 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003088 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003089 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003090 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3091 + ") to layer " + maxLayer);
3092 return null;
3093 }
3094 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003095 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003096 }
3097
3098 // TODO: Can this use createRotationMatrix()?
3099 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3100 if (rot == Surface.ROTATION_90) {
3101 final int tmp = crop.top;
3102 crop.top = dw - crop.right;
3103 crop.right = crop.bottom;
3104 crop.bottom = dw - crop.left;
3105 crop.left = tmp;
3106 } else if (rot == Surface.ROTATION_180) {
3107 int tmp = crop.top;
3108 crop.top = dh - crop.bottom;
3109 crop.bottom = dh - tmp;
3110 tmp = crop.right;
3111 crop.right = dw - crop.left;
3112 crop.left = dw - tmp;
3113 } else if (rot == Surface.ROTATION_270) {
3114 final int tmp = crop.top;
3115 crop.top = crop.left;
3116 crop.left = dh - crop.bottom;
3117 crop.bottom = crop.right;
3118 crop.right = dh - tmp;
3119 }
3120 }
3121
3122 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003123 // Used to indicate the layout is needed.
3124 mTmpWindow = null;
3125
3126 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003127 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003128 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003129 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003130 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003131 w.setDisplayLayoutNeeded();
3132 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003133 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003134
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003135 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003136 mService.mWindowPlacerLocked.performSurfacePlacement();
3137 }
3138 }
3139
Wale Ogunwale1666e312016-12-16 11:27:18 -08003140 void setExitingTokensHasVisible(boolean hasVisible) {
3141 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3142 mExitingTokens.get(i).hasVisible = hasVisible;
3143 }
3144
3145 // Initialize state of exiting applications.
3146 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3147 }
3148
3149 void removeExistingTokensIfPossible() {
3150 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3151 final WindowToken token = mExitingTokens.get(i);
3152 if (!token.hasVisible) {
3153 mExitingTokens.remove(i);
3154 }
3155 }
3156
3157 // Time to remove any exiting applications?
3158 mTaskStackContainers.removeExistingAppTokensIfPossible();
3159 }
3160
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003161 @Override
3162 void onDescendantOverrideConfigurationChanged() {
3163 setLayoutNeeded();
3164 mService.requestTraversal();
3165 }
3166
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003167 static final class TaskForResizePointSearchResult {
3168 boolean searchDone;
3169 Task taskForResize;
3170
3171 void reset() {
3172 searchDone = false;
3173 taskForResize = null;
3174 }
3175 }
Robert Carr3b716242016-08-16 16:02:21 -07003176
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003177 private static final class ApplySurfaceChangesTransactionState {
3178 boolean displayHasContent;
3179 boolean obscured;
3180 boolean syswin;
3181 boolean focusDisplayed;
3182 float preferredRefreshRate;
3183 int preferredModeId;
3184
3185 void reset() {
3186 displayHasContent = false;
3187 obscured = false;
3188 syswin = false;
3189 focusDisplayed = false;
3190 preferredRefreshRate = 0;
3191 preferredModeId = 0;
3192 }
3193 }
3194
3195 private static final class ScreenshotApplicationState {
3196 WindowState appWin;
3197 int maxLayer;
3198 int minLayer;
3199 boolean screenshotReady;
3200
3201 void reset(boolean screenshotReady) {
3202 appWin = null;
3203 maxLayer = 0;
3204 minLayer = 0;
3205 this.screenshotReady = screenshotReady;
3206 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3207 }
3208 }
3209
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003210 /**
3211 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3212 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3213 * homogeneous children type which is currently required by sub-classes of
3214 * {@link WindowContainer} class.
3215 */
3216 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3217
3218 int size() {
3219 return mChildren.size();
3220 }
3221
3222 E get(int index) {
3223 return mChildren.get(index);
3224 }
3225
3226 @Override
3227 boolean fillsParent() {
3228 return true;
3229 }
3230
3231 @Override
3232 boolean isVisible() {
3233 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003234 }
3235 }
3236
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003237 /**
3238 * Window container class that contains all containers on this display relating to Apps.
3239 * I.e Activities.
3240 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003241 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003242
Andrii Kulian839def92016-11-02 10:58:58 -07003243 /**
3244 * Adds the stack to this container.
3245 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3246 */
3247 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003248 if (stack.mStackId == HOME_STACK_ID) {
3249 if (mHomeStack != null) {
3250 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3251 }
3252 mHomeStack = stack;
3253 }
3254 addChild(stack, onTop);
3255 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003256 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003257
Andrii Kulian839def92016-11-02 10:58:58 -07003258 /** Removes the stack from its container and prepare for changing the parent. */
3259 void removeStackFromDisplay(TaskStack stack) {
3260 removeChild(stack);
3261 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003262 }
3263
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003264 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003265 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3266 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003267 addChild(stack, addIndex);
3268 setLayoutNeeded();
3269 }
3270
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003271 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003272 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3273 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3274 // This stack is always-on-top, override the default behavior.
3275 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3276
3277 // Moving to its current position, as we must call super but we don't want to
3278 // perform any meaningful action.
3279 final int currentPosition = mChildren.indexOf(child);
3280 super.positionChildAt(currentPosition, child, false /* includingParents */);
3281 return;
3282 }
3283
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003284 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3285 super.positionChildAt(targetPosition, child, includingParents);
3286
3287 setLayoutNeeded();
3288 }
3289
3290 /**
3291 * When stack is added or repositioned, find a proper position for it.
3292 * This will make sure that pinned stack always stays on top.
3293 * @param requestedPosition Position requested by caller.
3294 * @param stack Stack to be added or positioned.
3295 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3296 * @return The proper position for the stack.
3297 */
3298 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3299 final int topChildPosition = mChildren.size() - 1;
3300 boolean toTop = requestedPosition == POSITION_TOP;
3301 toTop |= adding ? requestedPosition >= topChildPosition + 1
3302 : requestedPosition >= topChildPosition;
3303 int targetPosition = requestedPosition;
3304
Bryce Lee48f4b572017-04-10 10:54:15 -07003305 if (toTop && stack.mStackId != PINNED_STACK_ID
3306 && getStackById(PINNED_STACK_ID) != null) {
3307 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003308 TaskStack topStack = mChildren.get(topChildPosition);
3309 if (topStack.mStackId != PINNED_STACK_ID) {
3310 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3311 }
3312
3313 // So, stack is moved just below the pinned stack.
3314 // When we're adding a new stack the target is the current pinned stack position.
3315 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003316 // stack, because WindowContainer#positionAt() first removes element and then adds
3317 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003318 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3319 }
3320
3321 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003322 }
3323
3324 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003325 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3326 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003327 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003328 if (super.forAllWindows(callback, traverseTopToBottom)) {
3329 return true;
3330 }
3331 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3332 return true;
3333 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003334 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003335 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3336 return true;
3337 }
3338 if (super.forAllWindows(callback, traverseTopToBottom)) {
3339 return true;
3340 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003341 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003342 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003343 }
3344
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003345 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003346 boolean traverseTopToBottom) {
3347 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3348 // app tokens.
3349 // TODO: Investigate if we need to continue to do this or if we can just process them
3350 // in-order.
3351 if (traverseTopToBottom) {
3352 for (int i = mChildren.size() - 1; i >= 0; --i) {
3353 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3354 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003355 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3356 traverseTopToBottom)) {
3357 return true;
3358 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003359 }
3360 }
3361 } else {
3362 final int count = mChildren.size();
3363 for (int i = 0; i < count; ++i) {
3364 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3365 final int appTokensCount = appTokens.size();
3366 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003367 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3368 traverseTopToBottom)) {
3369 return true;
3370 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003371 }
3372 }
3373 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003374 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003375 }
3376
Wale Ogunwale1666e312016-12-16 11:27:18 -08003377 void setExitingTokensHasVisible(boolean hasVisible) {
3378 for (int i = mChildren.size() - 1; i >= 0; --i) {
3379 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3380 for (int j = appTokens.size() - 1; j >= 0; --j) {
3381 appTokens.get(j).hasVisible = hasVisible;
3382 }
3383 }
3384 }
3385
3386 void removeExistingAppTokensIfPossible() {
3387 for (int i = mChildren.size() - 1; i >= 0; --i) {
3388 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3389 for (int j = appTokens.size() - 1; j >= 0; --j) {
3390 final AppWindowToken token = appTokens.get(j);
3391 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3392 && (!token.mIsExiting || token.isEmpty())) {
3393 // Make sure there is no animation running on this token, so any windows
3394 // associated with it will be removed as soon as their animations are
3395 // complete.
3396 token.mAppAnimator.clearAnimation();
3397 token.mAppAnimator.animating = false;
3398 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3399 "performLayout: App token exiting now removed" + token);
3400 token.removeIfPossible();
3401 }
3402 }
3403 }
3404 }
3405
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003406 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003407 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003408 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003409 // Apps and their containers are not allowed to specify an orientation while the
3410 // docked or freeform stack is visible...except for the home stack/task if the
3411 // docked stack is minimized and it actually set something.
3412 if (mHomeStack != null && mHomeStack.isVisible()
3413 && mDividerControllerLocked.isMinimizedDock()) {
3414 final int orientation = mHomeStack.getOrientation();
3415 if (orientation != SCREEN_ORIENTATION_UNSET) {
3416 return orientation;
3417 }
3418 }
3419 return SCREEN_ORIENTATION_UNSPECIFIED;
3420 }
3421
3422 final int orientation = super.getOrientation();
3423 if (orientation != SCREEN_ORIENTATION_UNSET
3424 && orientation != SCREEN_ORIENTATION_BEHIND) {
3425 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3426 "App is requesting an orientation, return " + orientation);
3427 return orientation;
3428 }
3429
3430 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003431 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003432 // The next app has not been requested to be visible, so we keep the current orientation
3433 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003434 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003435 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003436 }
3437
3438 /**
3439 * Window container class that contains all containers on this display that are not related to
3440 * Apps. E.g. status bar.
3441 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003442 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3443 /**
3444 * Compares two child window tokens returns -1 if the first is lesser than the second in
3445 * terms of z-order and 1 otherwise.
3446 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003447 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003448 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003449 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3450 token1.mOwnerCanManageAppTokens)
3451 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3452 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003453
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003454 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3455 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3456 return false;
3457 }
3458 final int req = w.mAttrs.screenOrientation;
3459 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3460 || req == SCREEN_ORIENTATION_UNSET) {
3461 return false;
3462 }
3463 return true;
3464 };
3465
Wale Ogunwale3a931692016-11-02 16:49:48 -07003466 private final String mName;
3467 NonAppWindowContainers(String name) {
3468 mName = name;
3469 }
3470
3471 void addChild(WindowToken token) {
3472 addChild(token, mWindowComparator);
3473 }
3474
3475 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003476 int getOrientation() {
3477 final WindowManagerPolicy policy = mService.mPolicy;
3478 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003479 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003480
3481 if (win != null) {
3482 final int req = win.mAttrs.screenOrientation;
3483 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
3484 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003485 mLastKeyguardForcedOrientation = req;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003486 }
Andrii Kulian8ee72852017-03-10 10:36:45 -08003487 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003488 }
3489
Andrii Kulian8ee72852017-03-10 10:36:45 -08003490 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003491
3492 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003493 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003494 }
3495
3496 return SCREEN_ORIENTATION_UNSET;
3497 }
3498
3499 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003500 String getName() {
3501 return mName;
3502 }
Robert Carr3b716242016-08-16 16:02:21 -07003503 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003504
3505 /**
3506 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3507 */
3508 @FunctionalInterface
3509 private interface Screenshoter<E> {
3510 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3511 boolean useIdentityTransform, int rotation);
3512 }
Craig Mautner59c00972012-07-30 12:10:24 -07003513}