blob: e0e4477a71c292349e20d487e07feb4209156b46 [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 Ogunwale2f569ed2017-05-08 09:15:49 -0700762 private 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.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700769 throw new IllegalArgumentException("Can't map token=" + token + " to display="
770 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700771 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700772 if (binder == null) {
773 throw new IllegalArgumentException("Can't map token=" + token + " to display="
774 + getName() + " binder is null");
775 }
776 if (token == null) {
777 throw new IllegalArgumentException("Can't map null token to display="
778 + getName() + " binder=" + binder);
779 }
780
Wale Ogunwale02319a62016-09-26 15:21:22 -0700781 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700782
783 if (token.asAppWindowToken() == null) {
784 // Add non-app token to container hierarchy on the display. App tokens are added through
785 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700786 switch (token.windowType) {
787 case TYPE_WALLPAPER:
788 mBelowAppWindowsContainers.addChild(token);
789 break;
790 case TYPE_INPUT_METHOD:
791 case TYPE_INPUT_METHOD_DIALOG:
792 mImeWindowsContainers.addChild(token);
793 break;
794 default:
795 mAboveAppWindowsContainers.addChild(token);
796 break;
797 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700798 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700799 }
800
801 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700802 final WindowToken token = mTokenMap.remove(binder);
803 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800804 token.setExiting();
805 }
806 return token;
807 }
808
809 /** Changes the display the input window token is housed on to this one. */
810 void reParentWindowToken(WindowToken token) {
811 final DisplayContent prevDc = token.getDisplayContent();
812 if (prevDc == this) {
813 return;
814 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800815 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
816 && token.asAppWindowToken() == null) {
817 // Removed the token from the map, but made sure it's not an app token before removing
818 // from parent.
819 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700820 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800821
822 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700823 }
824
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700825 void removeAppToken(IBinder binder) {
826 final WindowToken token = removeWindowToken(binder);
827 if (token == null) {
828 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
829 return;
830 }
831
832 final AppWindowToken appToken = token.asAppWindowToken();
833
834 if (appToken == null) {
835 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
836 return;
837 }
838
839 appToken.onRemovedFromDisplay();
840 }
841
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700842 Display getDisplay() {
843 return mDisplay;
844 }
845
Craig Mautner59c00972012-07-30 12:10:24 -0700846 DisplayInfo getDisplayInfo() {
847 return mDisplayInfo;
848 }
849
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700850 DisplayMetrics getDisplayMetrics() {
851 return mDisplayMetrics;
852 }
853
Andrii Kulian8ee72852017-03-10 10:36:45 -0800854 int getRotation() {
855 return mRotation;
856 }
857
858 void setRotation(int newRotation) {
859 mRotation = newRotation;
860 }
861
862 int getLastOrientation() {
863 return mLastOrientation;
864 }
865
866 void setLastOrientation(int orientation) {
867 mLastOrientation = orientation;
868 }
869
870 boolean getAltOrientation() {
871 return mAltOrientation;
872 }
873
874 void setAltOrientation(boolean altOrientation) {
875 mAltOrientation = altOrientation;
876 }
877
878 int getLastWindowForcedOrientation() {
879 return mLastWindowForcedOrientation;
880 }
881
Andrii Kulian06d07d62017-03-14 11:11:47 -0700882 /**
883 * Update rotation of the display.
884 *
885 * Returns true if the rotation has been changed. In this case YOU MUST CALL
886 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
887 */
888 boolean updateRotationUnchecked(boolean inTransaction) {
889 if (mService.mDeferredRotationPauseCount > 0) {
890 // Rotation updates have been paused temporarily. Defer the update until
891 // updates have been resumed.
892 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
893 return false;
894 }
895
896 ScreenRotationAnimation screenRotationAnimation =
897 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
898 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
899 // Rotation updates cannot be performed while the previous rotation change
900 // animation is still in progress. Skip this update. We will try updating
901 // again after the animation is finished and the display is unfrozen.
902 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
903 return false;
904 }
905 if (mService.mDisplayFrozen) {
906 // Even if the screen rotation animation has finished (e.g. isAnimating
907 // returns false), there is still some time where we haven't yet unfrozen
908 // the display. We also need to abort rotation here.
909 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
910 "Deferring rotation, still finishing previous rotation");
911 return false;
912 }
913
914 if (!mService.mDisplayEnabled) {
915 // No point choosing a rotation if the display is not enabled.
916 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
917 return false;
918 }
919
920 final int oldRotation = mRotation;
921 final int lastOrientation = mLastOrientation;
922 final boolean oldAltOrientation = mAltOrientation;
923 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700924 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
925 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700926
Robert Carr897215d2017-03-29 12:25:50 -0700927 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700928 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
929 if (seamlessRotated != null) {
930 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
931 // to complete (that is, waiting for windows to redraw). It's tempting to check
932 // w.mSeamlessRotationCount but that could be incorrect in the case of
933 // window-removal.
934 return false;
935 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700936 }
937
938 // TODO: Implement forced rotation changes.
939 // Set mAltOrientation to indicate that the application is receiving
940 // an orientation that has different metrics than it expected.
941 // eg. Portrait instead of Landscape.
942
943 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
944 lastOrientation, rotation);
945
946 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
947 + ", got rotation " + rotation + " which has "
948 + (altOrientation ? "incompatible" : "compatible") + " metrics");
949
950 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
951 // No change.
952 return false;
953 }
954
955 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
956 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
957 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
958
959 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
960 mService.mWaitingForConfig = true;
961 }
962
963 mRotation = rotation;
964 mAltOrientation = altOrientation;
965 if (isDefaultDisplay) {
966 mService.mPolicy.setRotationLw(rotation);
967 }
968
969 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
970 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
971 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
972 WINDOW_FREEZE_TIMEOUT_DURATION);
973
974 setLayoutNeeded();
975 final int[] anim = new int[2];
976 if (isDimming()) {
977 anim[0] = anim[1] = 0;
978 } else {
979 mService.mPolicy.selectRotationAnimationLw(anim);
980 }
981
982 if (!rotateSeamlessly) {
983 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1]);
984 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
985 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
986 mDisplayId);
987 } else {
988 // The screen rotation animation uses a screenshot to freeze the screen
989 // while windows resize underneath.
990 // When we are rotating seamlessly, we allow the elements to transition
991 // to their rotated state independently and without a freeze required.
992 screenRotationAnimation = null;
993
994 // We have to reset this in case a window was removed before it
995 // finished seamless rotation.
996 mService.mSeamlessRotationCount = 0;
997 }
998
999 // We need to update our screen size information to match the new rotation. If the rotation
1000 // has actually changed then this method will return true and, according to the comment at
1001 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1002 // By updating the Display info here it will be available to
1003 // #computeScreenConfiguration() later.
1004 updateDisplayAndOrientation(getConfiguration().uiMode);
1005
1006 if (!inTransaction) {
1007 if (SHOW_TRANSACTIONS) {
1008 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1009 }
1010 mService.openSurfaceTransaction();
1011 }
1012 try {
1013 // NOTE: We disable the rotation in the emulator because
1014 // it doesn't support hardware OpenGL emulation yet.
1015 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1016 && screenRotationAnimation.hasScreenshot()) {
1017 if (screenRotationAnimation.setRotationInTransaction(
1018 rotation, mService.mFxSession,
1019 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1020 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1021 mService.scheduleAnimationLocked();
1022 }
1023 }
1024
1025 if (rotateSeamlessly) {
1026 forAllWindows(w -> {
1027 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1028 }, true /* traverseTopToBottom */);
1029 }
1030
1031 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1032 } finally {
1033 if (!inTransaction) {
1034 mService.closeSurfaceTransaction();
1035 if (SHOW_LIGHT_TRANSACTIONS) {
1036 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1037 }
1038 }
1039 }
1040
1041 forAllWindows(w -> {
1042 // Discard surface after orientation change, these can't be reused.
1043 if (w.mAppToken != null) {
1044 w.mAppToken.destroySavedSurfaces();
1045 }
1046 if (w.mHasSurface && !rotateSeamlessly) {
1047 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
1048 w.mOrientationChanging = true;
1049 mService.mRoot.mOrientationChangeComplete = false;
1050 w.mLastFreezeDuration = 0;
1051 }
1052 w.mReportOrientationChanged = true;
1053 }, true /* traverseTopToBottom */);
1054
1055 if (rotateSeamlessly) {
1056 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1057 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1058 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1059 }
1060
1061 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1062 final WindowManagerService.RotationWatcher rotationWatcher
1063 = mService.mRotationWatchers.get(i);
1064 if (rotationWatcher.mDisplayId == mDisplayId) {
1065 try {
1066 rotationWatcher.mWatcher.onRotationChanged(rotation);
1067 } catch (RemoteException e) {
1068 // Ignore
1069 }
1070 }
1071 }
1072
1073 // TODO (multi-display): Magnification is supported only for the default display.
1074 // Announce rotation only if we will not animate as we already have the
1075 // windows in final state. Otherwise, we make this call at the rotation end.
1076 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1077 && isDefaultDisplay) {
1078 mService.mAccessibilityController.onRotationChangedLocked(this);
1079 }
1080
1081 return true;
1082 }
1083
1084 /**
1085 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1086 * changed.
1087 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1088 */
1089 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1090 // Use the effective "visual" dimensions based on current rotation
1091 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1092 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1093 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1094 int dw = realdw;
1095 int dh = realdh;
1096
1097 if (mAltOrientation) {
1098 if (realdw > realdh) {
1099 // Turn landscape into portrait.
1100 int maxw = (int)(realdh/1.3f);
1101 if (maxw < realdw) {
1102 dw = maxw;
1103 }
1104 } else {
1105 // Turn portrait into landscape.
1106 int maxh = (int)(realdw/1.3f);
1107 if (maxh < realdh) {
1108 dh = maxh;
1109 }
1110 }
1111 }
1112
1113 // Update application display metrics.
1114 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1115 mDisplayId);
1116 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1117 mDisplayId);
1118 mDisplayInfo.rotation = mRotation;
1119 mDisplayInfo.logicalWidth = dw;
1120 mDisplayInfo.logicalHeight = dh;
1121 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1122 mDisplayInfo.appWidth = appWidth;
1123 mDisplayInfo.appHeight = appHeight;
1124 if (isDefaultDisplay) {
1125 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1126 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1127 }
1128 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1129 if (mDisplayScalingDisabled) {
1130 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1131 } else {
1132 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1133 }
1134
1135 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1136 mDisplayInfo);
1137
1138 mBaseDisplayRect.set(0, 0, dw, dh);
1139
1140 if (isDefaultDisplay) {
1141 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1142 mCompatDisplayMetrics);
1143 }
1144 return mDisplayInfo;
1145 }
1146
1147 /**
1148 * Compute display configuration based on display properties and policy settings.
1149 * Do not call if mDisplayReady == false.
1150 */
1151 void computeScreenConfiguration(Configuration config) {
1152 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1153
1154 final int dw = displayInfo.logicalWidth;
1155 final int dh = displayInfo.logicalHeight;
1156 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1157 Configuration.ORIENTATION_LANDSCAPE;
1158 config.screenWidthDp =
1159 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1160 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1161 config.screenHeightDp =
1162 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1163 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001164
1165 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1166 final int leftInset = mTmpRect.left;
1167 final int topInset = mTmpRect.top;
1168 // appBounds at the root level should mirror the app screen size.
1169 config.setAppBounds(leftInset /*left*/, topInset /*top*/, leftInset + displayInfo.appWidth /*right*/,
1170 topInset + displayInfo.appHeight /*bottom*/);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001171 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1172 || displayInfo.rotation == Surface.ROTATION_270);
1173
1174 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1175 mDisplayMetrics.density, config);
1176
1177 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1178 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1179 ? Configuration.SCREENLAYOUT_ROUND_YES
1180 : Configuration.SCREENLAYOUT_ROUND_NO);
1181
1182 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1183 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1184 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1185 dh, mDisplayId);
1186 config.densityDpi = displayInfo.logicalDensityDpi;
1187
1188 config.colorMode =
1189 (displayInfo.isHdr()
1190 ? Configuration.COLOR_MODE_HDR_YES
1191 : Configuration.COLOR_MODE_HDR_NO)
1192 | (displayInfo.isWideColorGamut()
1193 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1194 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1195
1196 // Update the configuration based on available input devices, lid switch,
1197 // and platform configuration.
1198 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1199 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1200 config.navigation = Configuration.NAVIGATION_NONAV;
1201
1202 int keyboardPresence = 0;
1203 int navigationPresence = 0;
1204 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1205 final int len = devices != null ? devices.length : 0;
1206 for (int i = 0; i < len; i++) {
1207 InputDevice device = devices[i];
1208 if (!device.isVirtual()) {
1209 final int sources = device.getSources();
1210 final int presenceFlag = device.isExternal() ?
1211 WindowManagerPolicy.PRESENCE_EXTERNAL :
1212 WindowManagerPolicy.PRESENCE_INTERNAL;
1213
1214 // TODO(multi-display): Configure on per-display basis.
1215 if (mService.mIsTouchDevice) {
1216 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1217 InputDevice.SOURCE_TOUCHSCREEN) {
1218 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1219 }
1220 } else {
1221 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1222 }
1223
1224 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1225 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1226 navigationPresence |= presenceFlag;
1227 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1228 && config.navigation == Configuration.NAVIGATION_NONAV) {
1229 config.navigation = Configuration.NAVIGATION_DPAD;
1230 navigationPresence |= presenceFlag;
1231 }
1232
1233 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1234 config.keyboard = Configuration.KEYBOARD_QWERTY;
1235 keyboardPresence |= presenceFlag;
1236 }
1237 }
1238 }
1239
1240 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1241 config.navigation = Configuration.NAVIGATION_DPAD;
1242 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1243 }
1244
1245 // Determine whether a hard keyboard is available and enabled.
1246 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1247 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1248 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1249 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1250 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1251 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1252 }
1253
1254 // Let the policy update hidden states.
1255 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1256 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1257 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1258 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1259 }
1260
1261 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1262 int displayId) {
1263 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1264 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1265 final int unrotDw, unrotDh;
1266 if (rotated) {
1267 unrotDw = dh;
1268 unrotDh = dw;
1269 } else {
1270 unrotDw = dw;
1271 unrotDh = dh;
1272 }
1273 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1274 displayId);
1275 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1276 displayId);
1277 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1278 displayId);
1279 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1280 displayId);
1281 return sw;
1282 }
1283
1284 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1285 DisplayMetrics dm, int dw, int dh, int displayId) {
1286 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1287 displayId);
1288 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1289 uiMode, displayId);
1290 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1291 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1292 if (curSize == 0 || size < curSize) {
1293 curSize = size;
1294 }
1295 return curSize;
1296 }
1297
1298 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1299 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1300
1301 // We need to determine the smallest width that will occur under normal
1302 // operation. To this, start with the base screen size and compute the
1303 // width under the different possible rotations. We need to un-rotate
1304 // the current screen dimensions before doing this.
1305 int unrotDw, unrotDh;
1306 if (rotated) {
1307 unrotDw = dh;
1308 unrotDh = dw;
1309 } else {
1310 unrotDw = dw;
1311 unrotDh = dh;
1312 }
1313 displayInfo.smallestNominalAppWidth = 1<<30;
1314 displayInfo.smallestNominalAppHeight = 1<<30;
1315 displayInfo.largestNominalAppWidth = 0;
1316 displayInfo.largestNominalAppHeight = 0;
1317 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1318 unrotDh);
1319 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1320 unrotDw);
1321 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1322 unrotDh);
1323 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1324 unrotDw);
1325 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1326 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1327 displayId);
1328 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1329 displayId);
1330 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1331 displayId);
1332 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1333 displayId);
1334 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1335 outConfig.screenLayout = sl;
1336 }
1337
1338 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1339 int uiMode, int displayId) {
1340 // Get the app screen size at this rotation.
1341 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1342 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1343
1344 // Compute the screen layout size class for this rotation.
1345 int longSize = w;
1346 int shortSize = h;
1347 if (longSize < shortSize) {
1348 int tmp = longSize;
1349 longSize = shortSize;
1350 shortSize = tmp;
1351 }
1352 longSize = (int)(longSize/density);
1353 shortSize = (int)(shortSize/density);
1354 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1355 }
1356
1357 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1358 int uiMode, int dw, int dh) {
1359 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1360 displayId);
1361 if (width < displayInfo.smallestNominalAppWidth) {
1362 displayInfo.smallestNominalAppWidth = width;
1363 }
1364 if (width > displayInfo.largestNominalAppWidth) {
1365 displayInfo.largestNominalAppWidth = width;
1366 }
1367 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1368 displayId);
1369 if (height < displayInfo.smallestNominalAppHeight) {
1370 displayInfo.smallestNominalAppHeight = height;
1371 }
1372 if (height > displayInfo.largestNominalAppHeight) {
1373 displayInfo.largestNominalAppHeight = height;
1374 }
1375 }
1376
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001377 DockedStackDividerController getDockedDividerController() {
1378 return mDividerControllerLocked;
1379 }
1380
Winson Chung655332c2016-10-31 13:14:28 -07001381 PinnedStackController getPinnedStackController() {
1382 return mPinnedStackControllerLocked;
1383 }
1384
Jeff Browna506a6e2013-06-04 00:02:38 -07001385 /**
1386 * Returns true if the specified UID has access to this display.
1387 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001388 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001389 return mDisplay.hasAccess(uid);
1390 }
1391
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001392 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001393 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001394 }
1395
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001396 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001397 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001398 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001399 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001400 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001401 }
1402
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001403 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001404 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1405 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001406 if (stack.mStackId == stackId) {
1407 return stack;
1408 }
1409 }
1410 return null;
1411 }
1412
Bryce Lee48f4b572017-04-10 10:54:15 -07001413 @VisibleForTesting
1414 int getStackCount() {
1415 return mTaskStackContainers.size();
1416 }
1417
1418 @VisibleForTesting
1419 int getStaskPosById(int stackId) {
1420 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1421 final TaskStack stack = mTaskStackContainers.get(i);
1422 if (stack.mStackId == stackId) {
1423 return i;
1424 }
1425 }
1426 return -1;
1427 }
1428
Andrii Kulian441e4492016-09-29 15:25:00 -07001429 @Override
1430 void onConfigurationChanged(Configuration newParentConfig) {
1431 super.onConfigurationChanged(newParentConfig);
1432
Andrii Kulian3a507b52016-09-19 18:14:12 -07001433 // The display size information is heavily dependent on the resources in the current
1434 // configuration, so we need to reconfigure it every time the configuration changes.
1435 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1436 mService.reconfigureDisplayLocked(this);
1437
1438 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001439 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001440 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001441
Andrii Kulian441e4492016-09-29 15:25:00 -07001442 /**
1443 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1444 * bounds were updated.
1445 */
1446 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001447 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1448 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001449 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001450 changedStackList.add(stack.mStackId);
1451 }
1452 }
Winson Chung32c566f2017-04-11 18:31:21 -07001453
1454 // If there was no pinned stack, we still need to notify the controller of the display info
1455 // update as a result of the config change. We do this here to consolidate the flow between
1456 // changes when there is and is not a stack.
1457 if (getStackById(PINNED_STACK_ID) == null) {
1458 mPinnedStackControllerLocked.onDisplayInfoChanged();
1459 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001460 }
1461
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001462 @Override
1463 boolean fillsParent() {
1464 return true;
1465 }
1466
1467 @Override
1468 boolean isVisible() {
1469 return true;
1470 }
1471
1472 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001473 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001474 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001475 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001476 }
1477
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001478 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001479 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1480 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1481 // target, or here in order if there isn't an IME target.
1482 if (traverseTopToBottom) {
1483 for (int i = mChildren.size() - 1; i >= 0; --i) {
1484 final DisplayChildWindowContainer child = mChildren.get(i);
1485 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1486 // In this case the Ime windows will be processed above their target so we skip
1487 // here.
1488 continue;
1489 }
1490 if (child.forAllWindows(callback, traverseTopToBottom)) {
1491 return true;
1492 }
1493 }
1494 } else {
1495 final int count = mChildren.size();
1496 for (int i = 0; i < count; i++) {
1497 final DisplayChildWindowContainer child = mChildren.get(i);
1498 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1499 // In this case the Ime windows will be processed above their target so we skip
1500 // here.
1501 continue;
1502 }
1503 if (child.forAllWindows(callback, traverseTopToBottom)) {
1504 return true;
1505 }
1506 }
1507 }
1508 return false;
1509 }
1510
1511 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1512 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1513 }
1514
Wale Ogunwale5f1ae6a2017-05-02 14:49:04 -07001515 /**
1516 * Returns the orientation that this display should be in factoring in its children containers.
1517 *
1518 * @param includeAppContainers True if then app containers (stacks, tasks, ...) should be
1519 * factored in when determining the orientation. If false only
1520 * non-app/system containers will be used to determine the returned
1521 * orientation.
1522 * @return The orientation the display should be in.
1523 */
1524 int getOrientation(boolean includeAppContainers) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001525 final WindowManagerPolicy policy = mService.mPolicy;
1526
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001527 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001528 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001529 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001530 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001531 // If the display is frozen, some activities may be in the middle of restarting, and
1532 // thus have removed their old window. If the window has the flag to hide the lock
1533 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1534 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001535 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001536 } else if (policy.isKeyguardLocked()) {
1537 // Use the last orientation the while the display is frozen with the keyguard
1538 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1539 // window. We don't want to check the show when locked window directly though as
1540 // things aren't stable while the display is frozen, for example the window could be
1541 // momentarily unavailable due to activity relaunch.
1542 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001543 + "return " + mLastOrientation);
1544 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001545 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001546 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001547 final int orientation = mAboveAppWindowsContainers.getOrientation();
1548 if (orientation != SCREEN_ORIENTATION_UNSET) {
1549 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001550 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001551 }
1552
Wale Ogunwale5f1ae6a2017-05-02 14:49:04 -07001553 // Top system windows are not requesting an orientation. Get orientation from app containers
1554 // if allowed. Otherwise, return the last orientation.
1555 return includeAppContainers ? mTaskStackContainers.getOrientation() : mLastOrientation;
1556 }
1557
1558 @Override
1559 int getOrientation() {
1560 return getOrientation(true /* includeAppContainers */);
Wale Ogunwale51362492016-09-08 17:49:17 -07001561 }
1562
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001563 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001564 // Check if display metrics changed and update base values if needed.
1565 updateBaseDisplayMetricsIfNeeded();
1566
Craig Mautner722285e2012-09-07 13:55:58 -07001567 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001568 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001569
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001570 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1571 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001572 }
Craig Mautner722285e2012-09-07 13:55:58 -07001573 }
1574
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001575 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001576 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1577 if (displayManagerInternal != null) {
1578 // Bootstrap the default logical display from the display manager.
1579 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1580 if (newDisplayInfo != null) {
1581 mDisplayInfo.copyFrom(newDisplayInfo);
1582 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001583 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001584
Andrii Kuliancd097992017-03-23 18:31:59 -07001585 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1586 mDisplayInfo.logicalDensityDpi);
1587 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1588 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1589 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001590 }
1591
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001592 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001593 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001594 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001595 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001596 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1597 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001598 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001599 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001600 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001601 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001602 out.set(left, top, left + width, top + height);
1603 }
1604
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001605 private void getLogicalDisplayRect(Rect out, int orientation) {
1606 getLogicalDisplayRect(out);
1607
1608 // Rotate the Rect if needed.
1609 final int currentRotation = mDisplayInfo.rotation;
1610 final int rotationDelta = deltaRotation(currentRotation, orientation);
1611 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1612 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1613 mTmpRectF.set(out);
1614 mTmpMatrix.mapRect(mTmpRectF);
1615 mTmpRectF.round(out);
1616 }
1617 }
1618
Andrii Kuliancd097992017-03-23 18:31:59 -07001619 /**
1620 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1621 * values.
1622 */
1623 private void updateBaseDisplayMetricsIfNeeded() {
1624 // Get real display metrics without overrides from WM.
1625 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1626 final int orientation = mDisplayInfo.rotation;
1627 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1628 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1629 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1630 final int newDensity = mDisplayInfo.logicalDensityDpi;
1631
1632 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1633 || mInitialDisplayHeight != newHeight
1634 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1635
1636 if (displayMetricsChanged) {
1637 // Check if display size or density is forced.
1638 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1639 || mBaseDisplayHeight != mInitialDisplayHeight;
1640 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1641
1642 // If there is an override set for base values - use it, otherwise use new values.
1643 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1644 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1645 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1646
1647 // Real display metrics changed, so we should also update initial values.
1648 mInitialDisplayWidth = newWidth;
1649 mInitialDisplayHeight = newHeight;
1650 mInitialDisplayDensity = newDensity;
1651 mService.reconfigureDisplayLocked(this);
1652 }
1653 }
1654
Bryce Lee27cec322017-03-21 09:41:37 -07001655 /** Sets the maximum width the screen resolution can be */
1656 void setMaxUiWidth(int width) {
1657 if (DEBUG_DISPLAY) {
1658 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1659 }
1660
1661 mMaxUiWidth = width;
1662
1663 // Update existing metrics.
1664 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1665 }
1666
1667 /** Update base (override) display metrics. */
1668 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1669 mBaseDisplayWidth = baseWidth;
1670 mBaseDisplayHeight = baseHeight;
1671 mBaseDisplayDensity = baseDensity;
1672
1673 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1674 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1675 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1676 mBaseDisplayWidth = mMaxUiWidth;
1677
1678 if (DEBUG_DISPLAY) {
1679 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1680 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1681 + " on display:" + getDisplayId());
1682 }
1683 }
1684
1685 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1686 }
1687
Chong Zhangf66db432016-01-13 10:39:51 -08001688 void getContentRect(Rect out) {
1689 out.set(mContentRect);
1690 }
1691
Wale Ogunwale1666e312016-12-16 11:27:18 -08001692 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1693 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1694 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001695
Wale Ogunwale1666e312016-12-16 11:27:18 -08001696 TaskStack stack = getStackById(stackId);
1697 if (stack != null) {
1698 // It's already attached to the display...clear mDeferRemoval and move stack to
1699 // appropriate z-order on display as needed.
1700 stack.mDeferRemoval = false;
1701 // We're not moving the display to front when we're adding stacks, only when
1702 // requested to change the position of stack explicitly.
1703 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1704 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001705 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001706 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001707 mTaskStackContainers.addStackToDisplay(stack, onTop);
1708 }
1709
Wale Ogunwale1666e312016-12-16 11:27:18 -08001710 if (stackId == DOCKED_STACK_ID) {
1711 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001712 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001713 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001714 }
1715
Andrii Kulian51c1b672017-04-07 18:39:32 -07001716 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001717 final DisplayContent prevDc = stack.getDisplayContent();
1718 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001719 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1720 + " which is not currently attached to any display");
1721 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001722 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001723 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1724 + " to its current displayId=" + mDisplayId);
1725 }
1726
Wale Ogunwale1666e312016-12-16 11:27:18 -08001727 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001728 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001729 }
1730
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001731 @Override
1732 protected void addChild(DisplayChildWindowContainer child,
1733 Comparator<DisplayChildWindowContainer> comparator) {
1734 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1735 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001736
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001737 @Override
1738 protected void addChild(DisplayChildWindowContainer child, int index) {
1739 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1740 }
1741
1742 @Override
1743 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001744 // Only allow removal of direct children from this display if the display is in the process
1745 // of been removed.
1746 if (mRemovingDisplay) {
1747 super.removeChild(child);
1748 return;
1749 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001750 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001751 }
1752
Andrii Kuliand2765632016-12-12 22:26:34 -08001753 @Override
1754 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1755 // Children of the display are statically ordered, so the real intention here is to perform
1756 // the operation on the display and not the static direct children.
1757 getParent().positionChildAt(position, this, includingParents);
1758 }
1759
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001760 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001761 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1762 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001763 final int taskId = stack.taskIdFromPoint(x, y);
1764 if (taskId != -1) {
1765 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001766 }
1767 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001768 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001769 }
1770
Chong Zhang8e89b312015-09-09 15:09:30 -07001771 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001772 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001773 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001774 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001775 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001776 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001777 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001778 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1779 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001780 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001781 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001782 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001783
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001784 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1785 if (mTmpTaskForResizePointSearchResult.searchDone) {
1786 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001787 }
1788 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001789 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001790 }
1791
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001792 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001793 // The provided task is the task on this display with focus, so if WindowManagerService's
1794 // focused app is not on this display, focusedTask will be null.
1795 if (focusedTask == null) {
1796 mTouchExcludeRegion.setEmpty();
1797 } else {
1798 mTouchExcludeRegion.set(mBaseDisplayRect);
1799 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1800 mTmpRect2.setEmpty();
1801 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1802 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1803 stack.setTouchExcludeRegion(
1804 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1805 }
1806 // If we removed the focused task above, add it back and only leave its
1807 // outside touch area in the exclusion. TapDectector is not interested in
1808 // any touch inside the focused task itself.
1809 if (!mTmpRect2.isEmpty()) {
1810 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1811 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001812 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001813 final WindowState inputMethod = mService.mInputMethodWindow;
1814 if (inputMethod != null && inputMethod.isVisibleLw()) {
1815 // If the input method is visible and the user is typing, we don't want these touch
1816 // events to be intercepted and used to change focus. This would likely cause a
1817 // disappearance of the input method.
1818 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001819 if (inputMethod.getDisplayId() == mDisplayId) {
1820 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1821 } else {
1822 // IME is on a different display, so we need to update its tap detector.
1823 // TODO(multidisplay): Remove when IME will always appear on same display.
1824 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1825 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001826 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001827 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1828 WindowState win = mTapExcludedWindows.get(i);
1829 win.getTouchableRegion(mTmpRegion);
1830 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1831 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001832 // TODO(multi-display): Support docked stacks on secondary displays.
1833 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001834 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001835 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001836 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1837 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001838 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001839 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001840 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001841 }
1842
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001843 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001844 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001845 super.switchUser();
1846 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001847 }
1848
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001849 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001850 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1851 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001852 }
1853 }
1854
1855 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001856 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001857 }
1858
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001859 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001860 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001861 }
1862
1863 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001864 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001865 }
1866
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001867 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001868 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001869 }
1870
Wale Ogunwale10124582016-09-15 20:25:50 -07001871 @Override
1872 void removeIfPossible() {
1873 if (isAnimating()) {
1874 mDeferredRemoval = true;
1875 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001876 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001877 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001878 }
1879
Wale Ogunwale10124582016-09-15 20:25:50 -07001880 @Override
1881 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001882 mRemovingDisplay = true;
1883 try {
1884 super.removeImmediately();
1885 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1886 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001887 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001888 mService.unregisterPointerEventListener(mTapDetector);
1889 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1890 }
1891 } finally {
1892 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001893 }
Craig Mautner95da1082014-02-24 17:54:35 -08001894 }
1895
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001896 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001897 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001898 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001899 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1900
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001901 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001902 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001903 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001904 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001905 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001906 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001907 }
1908
Wale Ogunwale10124582016-09-15 20:25:50 -07001909 boolean animateForIme(float interpolatedValue, float animationTarget,
1910 float dividerAnimationTarget) {
1911 boolean updated = false;
1912
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001913 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1914 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001915 if (stack == null || !stack.isAdjustedForIme()) {
1916 continue;
1917 }
1918
1919 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1920 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1921 updated = true;
1922 } else {
1923 mDividerControllerLocked.mLastAnimationProgress =
1924 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1925 mDividerControllerLocked.mLastDividerProgress =
1926 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1927 updated |= stack.updateAdjustForIme(
1928 mDividerControllerLocked.mLastAnimationProgress,
1929 mDividerControllerLocked.mLastDividerProgress,
1930 false /* force */);
1931 }
1932 if (interpolatedValue >= 1f) {
1933 stack.endImeAdjustAnimation();
1934 }
1935 }
1936
1937 return updated;
1938 }
1939
1940 boolean clearImeAdjustAnimation() {
1941 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001942 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1943 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001944 if (stack != null && stack.isAdjustedForIme()) {
1945 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1946 changed = true;
1947 }
1948 }
1949 return changed;
1950 }
1951
1952 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001953 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1954 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001955 if (stack.isVisible() && stack.isAdjustedForIme()) {
1956 stack.beginImeAdjustAnimation();
1957 }
1958 }
1959 }
1960
1961 void adjustForImeIfNeeded() {
1962 final WindowState imeWin = mService.mInputMethodWindow;
1963 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1964 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001965 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001966 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1967 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1968 imeTargetStack.getDockSide() : DOCKED_INVALID;
1969 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1970 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1971 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1972 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1973 final boolean imeHeightChanged = imeVisible &&
1974 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1975
1976 // The divider could be adjusted for IME position, or be thinner than usual,
1977 // or both. There are three possible cases:
1978 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1979 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1980 // - If IME is not visible, divider is not moved and is normal width.
1981
1982 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001983 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1984 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001985 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07001986 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
1987 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001988 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1989 } else {
1990 stack.resetAdjustedForIme(false);
1991 }
1992 }
1993 mDividerControllerLocked.setAdjustedForIme(
1994 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1995 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001996 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1997 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001998 stack.resetAdjustedForIme(!dockVisible);
1999 }
2000 mDividerControllerLocked.setAdjustedForIme(
2001 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2002 }
Winson Chung655332c2016-10-31 13:14:28 -07002003 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002004 }
2005
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002006 void setInputMethodAnimLayerAdjustment(int adj) {
2007 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2008 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002009 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002010 }
2011
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002012 /**
2013 * If a window that has an animation specifying a colored background and the current wallpaper
2014 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2015 * suddenly disappear.
2016 */
2017 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002018 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2019 w -> w.mIsWallpaper && w.isVisibleNow());
2020
2021 if (visibleWallpaper != null) {
2022 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002023 }
2024 return winAnimator.mAnimLayer;
2025 }
2026
Wale Ogunwale10124582016-09-15 20:25:50 -07002027 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002028 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2029 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002030 stack.prepareFreezingTaskBounds();
2031 }
2032 }
2033
Wale Ogunwale94744212015-09-21 19:01:47 -07002034 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002035 getLogicalDisplayRect(mTmpRect, newRotation);
2036
2037 // Compute a transform matrix to undo the coordinate space transformation,
2038 // and present the window at the same physical position it previously occupied.
2039 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2040 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2041
2042 mTmpRectF.set(bounds);
2043 mTmpMatrix.mapRect(mTmpRectF);
2044 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002045 }
2046
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002047 static int deltaRotation(int oldRotation, int newRotation) {
2048 int delta = newRotation - oldRotation;
2049 if (delta < 0) delta += 4;
2050 return delta;
2051 }
2052
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002053 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002054 Matrix outMatrix) {
2055 // For rotations without Z-ordering we don't need the target rectangle's position.
2056 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2057 displayHeight, outMatrix);
2058 }
2059
2060 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2061 float displayWidth, float displayHeight, Matrix outMatrix) {
2062 switch (rotation) {
2063 case ROTATION_0:
2064 outMatrix.reset();
2065 break;
2066 case ROTATION_270:
2067 outMatrix.setRotate(270, 0, 0);
2068 outMatrix.postTranslate(0, displayHeight);
2069 outMatrix.postTranslate(rectTop, 0);
2070 break;
2071 case ROTATION_180:
2072 outMatrix.reset();
2073 break;
2074 case ROTATION_90:
2075 outMatrix.setRotate(90, 0, 0);
2076 outMatrix.postTranslate(displayWidth, 0);
2077 outMatrix.postTranslate(-rectTop, rectLeft);
2078 break;
2079 }
2080 }
2081
Craig Mautnera91f9e22012-09-14 16:22:08 -07002082 public void dump(String prefix, PrintWriter pw) {
2083 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2084 final String subPrefix = " " + prefix;
2085 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2086 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2087 pw.print("dpi");
2088 if (mInitialDisplayWidth != mBaseDisplayWidth
2089 || mInitialDisplayHeight != mBaseDisplayHeight
2090 || mInitialDisplayDensity != mBaseDisplayDensity) {
2091 pw.print(" base=");
2092 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2093 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2094 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002095 if (mDisplayScalingDisabled) {
2096 pw.println(" noscale");
2097 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002098 pw.print(" cur=");
2099 pw.print(mDisplayInfo.logicalWidth);
2100 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2101 pw.print(" app=");
2102 pw.print(mDisplayInfo.appWidth);
2103 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2104 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2105 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2106 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2107 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002108 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2109 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002110
Craig Mautnerdc548482014-02-05 13:35:24 -08002111 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002112 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002113 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2114 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002115 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002116 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002117
Craig Mautnerdc548482014-02-05 13:35:24 -08002118 pw.println();
2119 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002120 pw.println();
2121 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002122 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002123 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002124 pw.print(" Exiting #"); pw.print(i);
2125 pw.print(' '); pw.print(token);
2126 pw.println(':');
2127 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002128 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002129 }
Craig Mautner59c00972012-07-30 12:10:24 -07002130 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002131 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002132 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002133 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002134 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002135 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002136
2137 if (mInputMethodAnimLayerAdjustment != 0) {
2138 pw.println(subPrefix
2139 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2140 }
Craig Mautner59c00972012-07-30 12:10:24 -07002141 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002142
2143 @Override
2144 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002145 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002146 }
2147
2148 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002149 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002150 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002151
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002152 /** Checks if stack with provided id is visible on this display. */
2153 boolean isStackVisible(int stackId) {
2154 final TaskStack stack = getStackById(stackId);
2155 return (stack != null && stack.isVisible());
2156 }
2157
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002158 /**
2159 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2160 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002161 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002162 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002163 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002164 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002165
2166 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002167 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002168 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002169 */
Jorim Jaggife762342016-10-13 14:33:27 +02002170 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002171 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002172 }
2173
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002174 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002175 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002176 final int x = (int) xf;
2177 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002178 final WindowState touchedWin = getWindow(w -> {
2179 final int flags = w.mAttrs.flags;
2180 if (!w.isVisibleLw()) {
2181 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002182 }
2183 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002184 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002185 }
2186
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002187 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002188 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002189 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002190 }
2191
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002192 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002193
2194 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002195 return mTmpRegion.contains(x, y) || touchFlags == 0;
2196 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002197
2198 return touchedWin;
2199 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002200
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002201 boolean canAddToastWindowForUid(int uid) {
2202 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002203 // Also if the app is focused adding more than one toast at
2204 // a time for better backwards compatibility.
2205 final WindowState focusedWindowForUid = getWindow(w ->
2206 w.mOwnerUid == uid && w.isFocused());
2207 if (focusedWindowForUid != null) {
2208 return true;
2209 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002210 final WindowState win = getWindow(w ->
2211 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2212 && !w.mWindowRemovalAllowed);
2213 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002214 }
2215
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002216 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002217 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2218 return;
2219 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002220
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002221 // Used to communicate the old focus to the callback method.
2222 mTmpWindow = oldFocus;
2223
2224 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002225 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002226
2227 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002228 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002229
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002230 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002231
2232 if (mTmpWindow == null) {
2233 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2234 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002235 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002236 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002237 }
2238
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002239 /** Updates the layer assignment of windows on this display. */
2240 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002241 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002242 if (setLayoutNeeded) {
2243 setLayoutNeeded();
2244 }
2245 }
2246
Wale Ogunwale1666e312016-12-16 11:27:18 -08002247 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2248 // moving containers or resizing them. Need to investigate the best way to have it automatically
2249 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002250 void layoutAndAssignWindowLayersIfNeeded() {
2251 mService.mWindowsChanged = true;
2252 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002253
2254 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2255 false /*updateInputWindows*/)) {
2256 assignWindowLayers(false /* setLayoutNeeded */);
2257 }
2258
2259 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2260 mService.mWindowPlacerLocked.performSurfacePlacement();
2261 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2262 }
2263
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002264 /** Returns true if a leaked surface was destroyed */
2265 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002266 // Used to indicate that a surface was leaked.
2267 mTmpWindow = null;
2268 forAllWindows(w -> {
2269 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002270 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002271 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002272 }
2273 if (!mService.mSessions.contains(wsa.mSession)) {
2274 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002275 + w + " surface=" + wsa.mSurfaceController
2276 + " token=" + w.mToken
2277 + " pid=" + w.mSession.mPid
2278 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002279 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002280 mService.mForceRemoves.add(w);
2281 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002282 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002283 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002284 + w + " surface=" + wsa.mSurfaceController
2285 + " token=" + w.mAppToken
2286 + " saved=" + w.hasSavedSurface());
2287 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002288 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002289 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002290 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002291 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002292
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002293 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002294 }
2295
2296 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002297 * Determine and return the window that should be the IME target.
2298 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2299 * @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 +00002300 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002301 WindowState computeImeTarget(boolean updateImeTarget) {
2302 if (mService.mInputMethodWindow == null) {
2303 // There isn't an IME so there shouldn't be a target...That was easy!
2304 if (updateImeTarget) {
2305 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2306 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2307 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2308 }
2309 return null;
2310 }
2311
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002312 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2313 // same display. Or even when the current IME/target are not on the same screen as the next
2314 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002315 mUpdateImeTarget = updateImeTarget;
2316 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002317
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002318
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002319 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2320 // to be on top of it, but it is not -really- where input will go. So look down below
2321 // for a real window to target...
2322 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2323 final AppWindowToken token = target.mAppToken;
2324 if (token != null) {
2325 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2326 if (betterTarget != null) {
2327 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002328 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002329 }
2330 }
2331
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2333 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002334
2335 // Now, a special case -- if the last target's window is in the process of exiting, and is
2336 // above the new target, keep on the last target to avoid flicker. Consider for example a
2337 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2338 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2339 // scrim.
2340 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002341 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2342 && (target == null
2343 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002344 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002345 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002346 }
2347
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002348 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2349 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002350
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002351 if (target == null) {
2352 if (updateImeTarget) {
2353 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2354 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2355 + Debug.getCallers(4) : ""));
2356 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2357 }
2358
2359 return null;
2360 }
2361
2362 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002363 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2364 if (token != null) {
2365
2366 // Now some fun for dealing with window animations that modify the Z order. We need
2367 // to look at all windows below the current target that are in this app, finding the
2368 // highest visible one in layering.
2369 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002370 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002371 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002372 }
2373
2374 if (highestTarget != null) {
2375 final AppTransition appTransition = mService.mAppTransition;
2376 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2377 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2378 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002379 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002380
2381 if (appTransition.isTransitionSet()) {
2382 // If we are currently setting up for an animation, hold everything until we
2383 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002384 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2385 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002386 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002387 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002388 // If the window we are currently targeting is involved with an animation,
2389 // and it is on top of the next target we will be over, then hold off on
2390 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002391 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2392 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002393 }
2394 }
2395 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002396
2397 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2398 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2399 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002400 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002401 }
2402
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002403 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002404 }
2405
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002406 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2407 if (target == mService.mInputMethodTarget
2408 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2409 && mInputMethodAnimLayerAdjustment == layerAdj) {
2410 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002411 }
2412
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002413 mService.mInputMethodTarget = target;
2414 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2415 setInputMethodAnimLayerAdjustment(layerAdj);
2416 assignWindowLayers(false /* setLayoutNeeded */);
2417 }
2418
2419 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2420 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2421 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2422 }
2423
2424 // Used to indicate we have reached the first window in the range we are interested in.
2425 mTmpWindow = null;
2426
2427 // TODO: Figure-out a more efficient way to do this.
2428 final WindowState candidate = getWindow(w -> {
2429 if (w == top) {
2430 // Reached the first window in the range we are interested in.
2431 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002432 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002433 if (mTmpWindow == null) {
2434 return false;
2435 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002436
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002437 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2438 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002439 }
2440 // If we reached the bottom of the range of windows we are considering,
2441 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002442 if (w == bottom) {
2443 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002444 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002445 return false;
2446 });
2447
2448 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002449 }
2450
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002451 void setLayoutNeeded() {
2452 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2453 mLayoutNeeded = true;
2454 }
2455
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002456 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002457 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2458 mLayoutNeeded = false;
2459 }
2460
2461 boolean isLayoutNeeded() {
2462 return mLayoutNeeded;
2463 }
2464
Wale Ogunwale02319a62016-09-26 15:21:22 -07002465 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2466 if (mTokenMap.isEmpty()) {
2467 return;
2468 }
2469 pw.println(" Display #" + mDisplayId);
2470 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2471 while (it.hasNext()) {
2472 final WindowToken token = it.next();
2473 pw.print(" ");
2474 pw.print(token);
2475 if (dumpAll) {
2476 pw.println(':');
2477 token.dump(pw, " ");
2478 } else {
2479 pw.println();
2480 }
2481 }
2482 }
2483
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002484 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002485 final int[] index = new int[1];
2486 forAllWindows(w -> {
2487 final WindowStateAnimator wAnim = w.mWinAnimator;
2488 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2489 index[0] = index[0] + 1;
2490 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002491 }
2492
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002493 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002494 forAllWindows(w -> {
2495 w.mWinAnimator.enableSurfaceTrace(fd);
2496 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002497 }
2498
2499 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002500 forAllWindows(w -> {
2501 w.mWinAnimator.disableSurfaceTrace();
2502 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002503 }
2504
Jorim Jaggife762342016-10-13 14:33:27 +02002505 /**
2506 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2507 */
2508 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2509 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002510 forAllWindows(w -> {
2511 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
2512 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002513 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2514 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002515 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002516 }
2517
Wale Ogunwale494009b82016-10-21 09:01:38 -07002518 boolean checkWaitingForWindows() {
2519
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002520 mHaveBootMsg = false;
2521 mHaveApp = false;
2522 mHaveWallpaper = false;
2523 mHaveKeyguard = true;
2524
2525 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002526 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2527 return true;
2528 }
2529 if (w.isDrawnLw()) {
2530 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002531 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002532 } else if (w.mAttrs.type == TYPE_APPLICATION
2533 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002534 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002535 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002536 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002537 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002538 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002539 }
2540 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 return false;
2542 });
2543
2544 if (visibleWindow != null) {
2545 // We have a visible window.
2546 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002547 }
2548
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002549 // if the wallpaper service is disabled on the device, we're never going to have
2550 // wallpaper, don't bother waiting for it
2551 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2552 com.android.internal.R.bool.config_enableWallpaperService)
2553 && !mService.mOnlyCore;
2554
Wale Ogunwale494009b82016-10-21 09:01:38 -07002555 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2556 "******** booted=" + mService.mSystemBooted
2557 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002558 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2559 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2560 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002561
2562 // If we are turning on the screen to show the boot message, don't do it until the boot
2563 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002564 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002565 return true;
2566 }
2567
2568 // If we are turning on the screen after the boot is completed normally, don't do so until
2569 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002570 if (mService.mSystemBooted
2571 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002572 return true;
2573 }
2574
2575 return false;
2576 }
2577
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002578 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002579 mTmpWindowAnimator = animator;
2580 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002581 }
2582
2583 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002584 resetAnimationBackgroundAnimator();
2585
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002586 // Used to indicate a detached wallpaper.
2587 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002588 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002589
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002590 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002591
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002592 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002593 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002594 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2595 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002596 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2597 }
2598 }
2599
2600 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002601 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002602 }
2603
Wale Ogunwale494009b82016-10-21 09:01:38 -07002604 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002605 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002606 if (imFocus == null) {
2607 return false;
2608 }
2609
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002610 if (DEBUG_INPUT_METHOD) {
2611 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2612 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2613 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002614 }
2615
Wale Ogunwale494009b82016-10-21 09:01:38 -07002616 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2617
2618 if (DEBUG_INPUT_METHOD) {
2619 Slog.i(TAG_WM, "IM target client: " + imeClient);
2620 if (imeClient != null) {
2621 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2622 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2623 }
2624 }
2625
2626 return imeClient != null && imeClient.asBinder() == client.asBinder();
2627 }
2628
2629 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002630 final WindowState win = getWindow(
2631 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2632 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002633 }
2634
2635 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002636 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002637 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002638 final int curValue = w.mSystemUiVisibility;
2639 final int diff = (curValue ^ visibility) & globalDiff;
2640 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002641 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 w.mSeq++;
2643 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002644 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002645 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2646 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002647 visibility, newValue, diff);
2648 }
2649 } catch (RemoteException e) {
2650 // so sorry
2651 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002652 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002653 }
2654
2655 void onWindowFreezeTimeout() {
2656 Slog.w(TAG_WM, "Window freeze timeout expired.");
2657 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002658
2659 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002660 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002661 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002662 }
2663 w.mOrientationChanging = false;
2664 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2665 - mService.mDisplayFreezeTime);
2666 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002667 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002668 mService.mWindowPlacerLocked.performSurfacePlacement();
2669 }
2670
2671 void waitForAllWindowsDrawn() {
2672 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002673 forAllWindows(w -> {
2674 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2675 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2676 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002677 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002678 w.mLastContentInsets.set(-1, -1, -1, -1);
2679 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002680 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002681 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002682 }
2683
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002684 // TODO: Super crazy long method that should be broken down...
2685 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2686
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002687 final int dw = mDisplayInfo.logicalWidth;
2688 final int dh = mDisplayInfo.logicalHeight;
2689 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2690
2691 mTmpUpdateAllDrawn.clear();
2692
2693 int repeats = 0;
2694 do {
2695 repeats++;
2696 if (repeats > 6) {
2697 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2698 clearLayoutNeeded();
2699 break;
2700 }
2701
2702 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2703 pendingLayoutChanges);
2704
Andrii Kulian839def92016-11-02 10:58:58 -07002705 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2706 // the wallpaper window jumping across displays.
2707 // Remove check for default display when there will be support for multiple wallpaper
2708 // targets (on different displays).
2709 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002710 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002711 }
2712
2713 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2714 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2715 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2716 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002717 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002718 }
2719 }
2720
2721 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2722 setLayoutNeeded();
2723 }
2724
2725 // FIRST LOOP: Perform a layout, if needed.
2726 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2727 performLayout(repeats == 1, false /* updateInputWindows */);
2728 } else {
2729 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2730 }
2731
2732 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2733 pendingLayoutChanges = 0;
2734
2735 if (isDefaultDisplay) {
2736 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002737 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002738 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2739 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2740 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2741 }
2742 } while (pendingLayoutChanges != 0);
2743
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002744 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002745 resetDimming();
2746
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002747 mTmpRecoveringMemory = recoveringMemory;
2748 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002749
2750 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002751 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2752 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2753 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002754 true /* inTraversal, must call performTraversalInTrans... below */);
2755
2756 stopDimmingIfNeeded();
2757
2758 while (!mTmpUpdateAllDrawn.isEmpty()) {
2759 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2760 // See if any windows have been drawn, so they (and others associated with them)
2761 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002762 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002763 }
2764
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002765 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002766 }
2767
2768 void performLayout(boolean initial, boolean updateInputWindows) {
2769 if (!isLayoutNeeded()) {
2770 return;
2771 }
2772 clearLayoutNeeded();
2773
2774 final int dw = mDisplayInfo.logicalWidth;
2775 final int dh = mDisplayInfo.logicalHeight;
2776
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002777 if (DEBUG_LAYOUT) {
2778 Slog.v(TAG, "-------------------------------------");
2779 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2780 }
2781
Andrii Kulian8ee72852017-03-10 10:36:45 -08002782 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002783 getConfiguration().uiMode);
2784 if (isDefaultDisplay) {
2785 // Not needed on non-default displays.
2786 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2787 mService.mScreenRect.set(0, 0, dw, dh);
2788 }
2789
2790 mService.mPolicy.getContentRectLw(mContentRect);
2791
2792 int seq = mService.mLayoutSeq + 1;
2793 if (seq < 0) seq = 0;
2794 mService.mLayoutSeq = seq;
2795
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002796 // Used to indicate that we have processed the dream window and all additional windows are
2797 // behind it.
2798 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002799 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002800
2801 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002802 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002803
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002804 // Used to indicate that we have processed the dream window and all additional attached
2805 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002806 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002807 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002808
2809 // Now perform layout of attached windows, which usually depend on the position of the
2810 // window they are attached to. XXX does not deal with windows that are attached to windows
2811 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002812 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002813
2814 // Window frames may have changed. Tell the input dispatcher about it.
2815 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2816 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2817 if (updateInputWindows) {
2818 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2819 }
2820
2821 mService.mPolicy.finishLayoutLw();
2822 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2823 }
2824
2825 /**
2826 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2827 * In portrait mode, it grabs the full screenshot.
2828 *
2829 * @param width the width of the target bitmap
2830 * @param height the height of the target bitmap
2831 * @param includeFullDisplay true if the screen should not be cropped before capture
2832 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2833 * @param config of the output bitmap
2834 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002835 * @param includeDecor whether to include window decors, like the status or navigation bar
2836 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002837 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002838 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002839 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002840 boolean wallpaperOnly, boolean includeDecor) {
2841 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2842 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002843 if (bitmap == null) {
2844 return null;
2845 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002846
2847 if (DEBUG_SCREENSHOT) {
2848 // TEST IF IT's ALL BLACK
2849 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2850 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2851 bitmap.getHeight());
2852 boolean allBlack = true;
2853 final int firstColor = buffer[0];
2854 for (int i = 0; i < buffer.length; i++) {
2855 if (buffer[i] != firstColor) {
2856 allBlack = false;
2857 break;
2858 }
2859 }
2860 if (allBlack) {
2861 final WindowState appWin = mScreenshotApplicationState.appWin;
2862 final int maxLayer = mScreenshotApplicationState.maxLayer;
2863 final int minLayer = mScreenshotApplicationState.minLayer;
2864 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2865 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2866 (appWin != null ?
2867 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2868 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2869 }
2870 }
2871
2872 // Create a copy of the screenshot that is immutable and backed in ashmem.
2873 // This greatly reduces the overhead of passing the bitmap between processes.
2874 Bitmap ret = bitmap.createAshmemBitmap(config);
2875 bitmap.recycle();
2876 return ret;
2877 }
2878
2879 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2880 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2881 boolean includeDecor) {
2882 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2883 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2884 }
2885
2886 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2887 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2888 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002889 int dw = mDisplayInfo.logicalWidth;
2890 int dh = mDisplayInfo.logicalHeight;
2891 if (dw == 0 || dh == 0) {
2892 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2893 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2894 return null;
2895 }
2896
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002897 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002898
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002899 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002900 final Rect frame = new Rect();
2901 final Rect stackBounds = new Rect();
2902
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002903 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002904 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002905 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002906 synchronized(mService.mWindowMap) {
2907 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002908 mScreenshotApplicationState.appWin = null;
2909 forAllWindows(w -> {
2910 if (!w.mHasSurface) {
2911 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002912 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 if (w.mLayer >= aboveAppLayer) {
2914 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002915 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002916 if (wallpaperOnly && !w.mIsWallpaper) {
2917 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002918 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002919 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02002920 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002921 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002922 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2923 // then the target window state is this one.
2924 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002925 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002926 }
2927
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002928 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002929 // We have not ran across the target window yet, so it is probably behind
2930 // the wallpaper. This can happen when the keyguard is up and all windows
2931 // are moved behind the wallpaper. We don't want to include the wallpaper
2932 // layer in the screenshot as it will cover-up the layer of the target
2933 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002934 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002935 }
2936 // Fall through. The target window is in front of the wallpaper. For this
2937 // case we want to include the wallpaper layer in the screenshot because
2938 // the target window might have some transparent areas.
2939 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002940 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002941 // This app window is of no interest if it is not associated with the
2942 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002943 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002944 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002945 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002946 }
2947
2948 // Include this window.
2949
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002950 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002952 if (mScreenshotApplicationState.maxLayer < layer) {
2953 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002954 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002955 if (mScreenshotApplicationState.minLayer > layer) {
2956 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002957 }
2958
2959 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02002960 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
2961 if (includeDecor) {
2962 final TaskStack stack = w.getStack();
2963 if (stack != null) {
2964 stack.getBounds(frame);
2965 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02002966
Jorim Jaggi70f59482017-05-04 14:05:59 +02002967 // We want to screenshot with the exact bounds of the surface of the app. Thus,
2968 // intersect it with the frame.
2969 frame.intersect(w.mFrame);
2970 } else {
2971 final Rect wf = w.mFrame;
2972 final Rect cr = w.mContentInsets;
2973 int left = wf.left + cr.left;
2974 int top = wf.top + cr.top;
2975 int right = wf.right - cr.right;
2976 int bottom = wf.bottom - cr.bottom;
2977 frame.union(left, top, right, bottom);
2978 w.getVisibleBounds(stackBounds);
2979 if (!Rect.intersects(frame, stackBounds)) {
2980 // Set frame empty if there's no intersection.
2981 frame.setEmpty();
2982 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002983 }
2984 }
2985
2986 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002987 (w.mAppToken != null && w.mAppToken.token == appToken)
2988 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002989 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002990 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002991 }
2992
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002993 if (w.isObscuringDisplay()){
2994 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002995 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002996 return false;
2997 }, true /* traverseTopToBottom */);
2998
2999 final WindowState appWin = mScreenshotApplicationState.appWin;
3000 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3001 final int maxLayer = mScreenshotApplicationState.maxLayer;
3002 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003003
3004 if (appToken != null && appWin == null) {
3005 // Can't find a window to snapshot.
3006 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3007 "Screenshot: Couldn't find a surface matching " + appToken);
3008 return null;
3009 }
3010
3011 if (!screenshotReady) {
3012 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3013 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3014 appWin.mWinAnimator.mDrawState)));
3015 return null;
3016 }
3017
3018 // Screenshot is ready to be taken. Everything from here below will continue
3019 // through the bottom of the loop and return a value. We only stay in the loop
3020 // because we don't want to release the mWindowMap lock until the screenshot is
3021 // taken.
3022
3023 if (maxLayer == 0) {
3024 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3025 + ": returning null maxLayer=" + maxLayer);
3026 return null;
3027 }
3028
Jorim Jaggi02886a82016-12-06 09:10:06 -08003029 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003030 // Constrain frame to the screen size.
3031 if (!frame.intersect(0, 0, dw, dh)) {
3032 frame.setEmpty();
3033 }
3034 } else {
3035 // Caller just wants entire display.
3036 frame.set(0, 0, dw, dh);
3037 }
3038 if (frame.isEmpty()) {
3039 return null;
3040 }
3041
3042 if (width < 0) {
3043 width = (int) (frame.width() * frameScale);
3044 }
3045 if (height < 0) {
3046 height = (int) (frame.height() * frameScale);
3047 }
3048
3049 // Tell surface flinger what part of the image to crop. Take the top
3050 // right part of the application, and crop the larger dimension to fit.
3051 Rect crop = new Rect(frame);
3052 if (width / (float) frame.width() < height / (float) frame.height()) {
3053 int cropWidth = (int)((float)width / (float)height * frame.height());
3054 crop.right = crop.left + cropWidth;
3055 } else {
3056 int cropHeight = (int)((float)height / (float)width * frame.width());
3057 crop.bottom = crop.top + cropHeight;
3058 }
3059
3060 // The screenshot API does not apply the current screen rotation.
3061 int rot = mDisplay.getRotation();
3062
3063 if (rot == ROTATION_90 || rot == ROTATION_270) {
3064 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3065 }
3066
3067 // Surfaceflinger is not aware of orientation, so convert our logical
3068 // crop to surfaceflinger's portrait orientation.
3069 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3070
3071 if (DEBUG_SCREENSHOT) {
3072 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3073 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003074 forAllWindows(w -> {
3075 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3076 Slog.i(TAG_WM, w + ": " + w.mLayer
3077 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003078 + " surfaceLayer=" + ((controller == null)
3079 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003080 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003081 }
3082
3083 final ScreenRotationAnimation screenRotationAnimation =
3084 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3085 final boolean inRotation = screenRotationAnimation != null &&
3086 screenRotationAnimation.isAnimating();
3087 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3088 "Taking screenshot while rotating");
3089
3090 // We force pending transactions to flush before taking
3091 // the screenshot by pushing an empty synchronous transaction.
3092 SurfaceControl.openTransaction();
3093 SurfaceControl.closeTransactionSync();
3094
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003095 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003096 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003097 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003098 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3099 + ") to layer " + maxLayer);
3100 return null;
3101 }
3102 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003103 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003104 }
3105
3106 // TODO: Can this use createRotationMatrix()?
3107 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3108 if (rot == Surface.ROTATION_90) {
3109 final int tmp = crop.top;
3110 crop.top = dw - crop.right;
3111 crop.right = crop.bottom;
3112 crop.bottom = dw - crop.left;
3113 crop.left = tmp;
3114 } else if (rot == Surface.ROTATION_180) {
3115 int tmp = crop.top;
3116 crop.top = dh - crop.bottom;
3117 crop.bottom = dh - tmp;
3118 tmp = crop.right;
3119 crop.right = dw - crop.left;
3120 crop.left = dw - tmp;
3121 } else if (rot == Surface.ROTATION_270) {
3122 final int tmp = crop.top;
3123 crop.top = crop.left;
3124 crop.left = dh - crop.bottom;
3125 crop.bottom = crop.right;
3126 crop.right = dh - tmp;
3127 }
3128 }
3129
3130 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003131 // Used to indicate the layout is needed.
3132 mTmpWindow = null;
3133
3134 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003135 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003136 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003137 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003138 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003139 w.setDisplayLayoutNeeded();
3140 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003141 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003142
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003143 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003144 mService.mWindowPlacerLocked.performSurfacePlacement();
3145 }
3146 }
3147
Wale Ogunwale1666e312016-12-16 11:27:18 -08003148 void setExitingTokensHasVisible(boolean hasVisible) {
3149 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3150 mExitingTokens.get(i).hasVisible = hasVisible;
3151 }
3152
3153 // Initialize state of exiting applications.
3154 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3155 }
3156
3157 void removeExistingTokensIfPossible() {
3158 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3159 final WindowToken token = mExitingTokens.get(i);
3160 if (!token.hasVisible) {
3161 mExitingTokens.remove(i);
3162 }
3163 }
3164
3165 // Time to remove any exiting applications?
3166 mTaskStackContainers.removeExistingAppTokensIfPossible();
3167 }
3168
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003169 @Override
3170 void onDescendantOverrideConfigurationChanged() {
3171 setLayoutNeeded();
3172 mService.requestTraversal();
3173 }
3174
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003175 static final class TaskForResizePointSearchResult {
3176 boolean searchDone;
3177 Task taskForResize;
3178
3179 void reset() {
3180 searchDone = false;
3181 taskForResize = null;
3182 }
3183 }
Robert Carr3b716242016-08-16 16:02:21 -07003184
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003185 private static final class ApplySurfaceChangesTransactionState {
3186 boolean displayHasContent;
3187 boolean obscured;
3188 boolean syswin;
3189 boolean focusDisplayed;
3190 float preferredRefreshRate;
3191 int preferredModeId;
3192
3193 void reset() {
3194 displayHasContent = false;
3195 obscured = false;
3196 syswin = false;
3197 focusDisplayed = false;
3198 preferredRefreshRate = 0;
3199 preferredModeId = 0;
3200 }
3201 }
3202
3203 private static final class ScreenshotApplicationState {
3204 WindowState appWin;
3205 int maxLayer;
3206 int minLayer;
3207 boolean screenshotReady;
3208
3209 void reset(boolean screenshotReady) {
3210 appWin = null;
3211 maxLayer = 0;
3212 minLayer = 0;
3213 this.screenshotReady = screenshotReady;
3214 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3215 }
3216 }
3217
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003218 /**
3219 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3220 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3221 * homogeneous children type which is currently required by sub-classes of
3222 * {@link WindowContainer} class.
3223 */
3224 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3225
3226 int size() {
3227 return mChildren.size();
3228 }
3229
3230 E get(int index) {
3231 return mChildren.get(index);
3232 }
3233
3234 @Override
3235 boolean fillsParent() {
3236 return true;
3237 }
3238
3239 @Override
3240 boolean isVisible() {
3241 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003242 }
3243 }
3244
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003245 /**
3246 * Window container class that contains all containers on this display relating to Apps.
3247 * I.e Activities.
3248 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003249 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003250
Andrii Kulian839def92016-11-02 10:58:58 -07003251 /**
3252 * Adds the stack to this container.
3253 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3254 */
3255 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003256 if (stack.mStackId == HOME_STACK_ID) {
3257 if (mHomeStack != null) {
3258 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3259 }
3260 mHomeStack = stack;
3261 }
3262 addChild(stack, onTop);
3263 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003264 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003265
Andrii Kulian839def92016-11-02 10:58:58 -07003266 /** Removes the stack from its container and prepare for changing the parent. */
3267 void removeStackFromDisplay(TaskStack stack) {
3268 removeChild(stack);
3269 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003270 }
3271
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003272 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003273 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3274 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003275 addChild(stack, addIndex);
3276 setLayoutNeeded();
3277 }
3278
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003279 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003280 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3281 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3282 // This stack is always-on-top, override the default behavior.
3283 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3284
3285 // Moving to its current position, as we must call super but we don't want to
3286 // perform any meaningful action.
3287 final int currentPosition = mChildren.indexOf(child);
3288 super.positionChildAt(currentPosition, child, false /* includingParents */);
3289 return;
3290 }
3291
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003292 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3293 super.positionChildAt(targetPosition, child, includingParents);
3294
3295 setLayoutNeeded();
3296 }
3297
3298 /**
3299 * When stack is added or repositioned, find a proper position for it.
3300 * This will make sure that pinned stack always stays on top.
3301 * @param requestedPosition Position requested by caller.
3302 * @param stack Stack to be added or positioned.
3303 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3304 * @return The proper position for the stack.
3305 */
3306 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3307 final int topChildPosition = mChildren.size() - 1;
3308 boolean toTop = requestedPosition == POSITION_TOP;
3309 toTop |= adding ? requestedPosition >= topChildPosition + 1
3310 : requestedPosition >= topChildPosition;
3311 int targetPosition = requestedPosition;
3312
Bryce Lee48f4b572017-04-10 10:54:15 -07003313 if (toTop && stack.mStackId != PINNED_STACK_ID
3314 && getStackById(PINNED_STACK_ID) != null) {
3315 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003316 TaskStack topStack = mChildren.get(topChildPosition);
3317 if (topStack.mStackId != PINNED_STACK_ID) {
3318 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3319 }
3320
3321 // So, stack is moved just below the pinned stack.
3322 // When we're adding a new stack the target is the current pinned stack position.
3323 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003324 // stack, because WindowContainer#positionAt() first removes element and then adds
3325 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003326 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3327 }
3328
3329 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003330 }
3331
3332 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003333 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3334 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003335 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003336 if (super.forAllWindows(callback, traverseTopToBottom)) {
3337 return true;
3338 }
3339 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3340 return true;
3341 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003342 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003343 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3344 return true;
3345 }
3346 if (super.forAllWindows(callback, traverseTopToBottom)) {
3347 return true;
3348 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003349 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003350 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003351 }
3352
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003353 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003354 boolean traverseTopToBottom) {
3355 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3356 // app tokens.
3357 // TODO: Investigate if we need to continue to do this or if we can just process them
3358 // in-order.
3359 if (traverseTopToBottom) {
3360 for (int i = mChildren.size() - 1; i >= 0; --i) {
3361 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3362 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003363 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3364 traverseTopToBottom)) {
3365 return true;
3366 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003367 }
3368 }
3369 } else {
3370 final int count = mChildren.size();
3371 for (int i = 0; i < count; ++i) {
3372 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3373 final int appTokensCount = appTokens.size();
3374 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003375 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3376 traverseTopToBottom)) {
3377 return true;
3378 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003379 }
3380 }
3381 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003382 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003383 }
3384
Wale Ogunwale1666e312016-12-16 11:27:18 -08003385 void setExitingTokensHasVisible(boolean hasVisible) {
3386 for (int i = mChildren.size() - 1; i >= 0; --i) {
3387 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3388 for (int j = appTokens.size() - 1; j >= 0; --j) {
3389 appTokens.get(j).hasVisible = hasVisible;
3390 }
3391 }
3392 }
3393
3394 void removeExistingAppTokensIfPossible() {
3395 for (int i = mChildren.size() - 1; i >= 0; --i) {
3396 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3397 for (int j = appTokens.size() - 1; j >= 0; --j) {
3398 final AppWindowToken token = appTokens.get(j);
3399 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3400 && (!token.mIsExiting || token.isEmpty())) {
3401 // Make sure there is no animation running on this token, so any windows
3402 // associated with it will be removed as soon as their animations are
3403 // complete.
3404 token.mAppAnimator.clearAnimation();
3405 token.mAppAnimator.animating = false;
3406 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3407 "performLayout: App token exiting now removed" + token);
3408 token.removeIfPossible();
3409 }
3410 }
3411 }
3412 }
3413
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003414 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003415 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003416 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003417 // Apps and their containers are not allowed to specify an orientation while the
3418 // docked or freeform stack is visible...except for the home stack/task if the
3419 // docked stack is minimized and it actually set something.
3420 if (mHomeStack != null && mHomeStack.isVisible()
3421 && mDividerControllerLocked.isMinimizedDock()) {
3422 final int orientation = mHomeStack.getOrientation();
3423 if (orientation != SCREEN_ORIENTATION_UNSET) {
3424 return orientation;
3425 }
3426 }
3427 return SCREEN_ORIENTATION_UNSPECIFIED;
3428 }
3429
3430 final int orientation = super.getOrientation();
3431 if (orientation != SCREEN_ORIENTATION_UNSET
3432 && orientation != SCREEN_ORIENTATION_BEHIND) {
3433 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3434 "App is requesting an orientation, return " + orientation);
3435 return orientation;
3436 }
3437
3438 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003439 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003440 // The next app has not been requested to be visible, so we keep the current orientation
3441 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003442 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003443 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003444 }
3445
3446 /**
3447 * Window container class that contains all containers on this display that are not related to
3448 * Apps. E.g. status bar.
3449 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003450 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3451 /**
3452 * Compares two child window tokens returns -1 if the first is lesser than the second in
3453 * terms of z-order and 1 otherwise.
3454 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003455 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003456 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003457 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3458 token1.mOwnerCanManageAppTokens)
3459 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3460 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003461
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003462 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3463 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3464 return false;
3465 }
3466 final int req = w.mAttrs.screenOrientation;
3467 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3468 || req == SCREEN_ORIENTATION_UNSET) {
3469 return false;
3470 }
3471 return true;
3472 };
3473
Wale Ogunwale3a931692016-11-02 16:49:48 -07003474 private final String mName;
3475 NonAppWindowContainers(String name) {
3476 mName = name;
3477 }
3478
3479 void addChild(WindowToken token) {
3480 addChild(token, mWindowComparator);
3481 }
3482
3483 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003484 int getOrientation() {
3485 final WindowManagerPolicy policy = mService.mPolicy;
3486 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003487 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003488
3489 if (win != null) {
3490 final int req = win.mAttrs.screenOrientation;
3491 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
3492 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003493 mLastKeyguardForcedOrientation = req;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003494 }
Andrii Kulian8ee72852017-03-10 10:36:45 -08003495 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003496 }
3497
Andrii Kulian8ee72852017-03-10 10:36:45 -08003498 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003499
3500 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003501 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003502 }
3503
3504 return SCREEN_ORIENTATION_UNSET;
3505 }
3506
3507 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003508 String getName() {
3509 return mName;
3510 }
Robert Carr3b716242016-08-16 16:02:21 -07003511 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003512
3513 /**
3514 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3515 */
3516 @FunctionalInterface
3517 private interface Screenshoter<E> {
3518 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3519 boolean useIdentityTransform, int rotation);
3520 }
Craig Mautner59c00972012-07-30 12:10:24 -07003521}