blob: 257f2856213e2b0d7031b208b5d9edc80317ebca [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 Ogunwale399c8692017-05-08 14:22:42 -07001515 @Override
1516 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001517 final WindowManagerPolicy policy = mService.mPolicy;
1518
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001519 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001520 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001521 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001522 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001523 // If the display is frozen, some activities may be in the middle of restarting, and
1524 // thus have removed their old window. If the window has the flag to hide the lock
1525 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1526 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001527 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001528 } else if (policy.isKeyguardLocked()) {
1529 // Use the last orientation the while the display is frozen with the keyguard
1530 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1531 // window. We don't want to check the show when locked window directly though as
1532 // things aren't stable while the display is frozen, for example the window could be
1533 // momentarily unavailable due to activity relaunch.
1534 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001535 + "return " + mLastOrientation);
1536 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001537 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001538 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001539 final int orientation = mAboveAppWindowsContainers.getOrientation();
1540 if (orientation != SCREEN_ORIENTATION_UNSET) {
1541 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001542 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001543 }
1544
Wale Ogunwale399c8692017-05-08 14:22:42 -07001545 // Top system windows are not requesting an orientation. Start searching from apps.
1546 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001547 }
1548
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001549 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001550 // Check if display metrics changed and update base values if needed.
1551 updateBaseDisplayMetricsIfNeeded();
1552
Craig Mautner722285e2012-09-07 13:55:58 -07001553 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001554 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001555
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001556 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1557 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001558 }
Craig Mautner722285e2012-09-07 13:55:58 -07001559 }
1560
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001561 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001562 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1563 if (displayManagerInternal != null) {
1564 // Bootstrap the default logical display from the display manager.
1565 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1566 if (newDisplayInfo != null) {
1567 mDisplayInfo.copyFrom(newDisplayInfo);
1568 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001569 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001570
Andrii Kuliancd097992017-03-23 18:31:59 -07001571 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1572 mDisplayInfo.logicalDensityDpi);
1573 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1574 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1575 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001576 }
1577
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001578 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001579 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001580 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001581 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001582 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1583 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001584 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001585 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001586 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001587 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001588 out.set(left, top, left + width, top + height);
1589 }
1590
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001591 private void getLogicalDisplayRect(Rect out, int orientation) {
1592 getLogicalDisplayRect(out);
1593
1594 // Rotate the Rect if needed.
1595 final int currentRotation = mDisplayInfo.rotation;
1596 final int rotationDelta = deltaRotation(currentRotation, orientation);
1597 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1598 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1599 mTmpRectF.set(out);
1600 mTmpMatrix.mapRect(mTmpRectF);
1601 mTmpRectF.round(out);
1602 }
1603 }
1604
Andrii Kuliancd097992017-03-23 18:31:59 -07001605 /**
1606 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1607 * values.
1608 */
1609 private void updateBaseDisplayMetricsIfNeeded() {
1610 // Get real display metrics without overrides from WM.
1611 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1612 final int orientation = mDisplayInfo.rotation;
1613 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1614 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1615 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1616 final int newDensity = mDisplayInfo.logicalDensityDpi;
1617
1618 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1619 || mInitialDisplayHeight != newHeight
1620 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1621
1622 if (displayMetricsChanged) {
1623 // Check if display size or density is forced.
1624 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1625 || mBaseDisplayHeight != mInitialDisplayHeight;
1626 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1627
1628 // If there is an override set for base values - use it, otherwise use new values.
1629 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1630 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1631 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1632
1633 // Real display metrics changed, so we should also update initial values.
1634 mInitialDisplayWidth = newWidth;
1635 mInitialDisplayHeight = newHeight;
1636 mInitialDisplayDensity = newDensity;
1637 mService.reconfigureDisplayLocked(this);
1638 }
1639 }
1640
Bryce Lee27cec322017-03-21 09:41:37 -07001641 /** Sets the maximum width the screen resolution can be */
1642 void setMaxUiWidth(int width) {
1643 if (DEBUG_DISPLAY) {
1644 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1645 }
1646
1647 mMaxUiWidth = width;
1648
1649 // Update existing metrics.
1650 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1651 }
1652
1653 /** Update base (override) display metrics. */
1654 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1655 mBaseDisplayWidth = baseWidth;
1656 mBaseDisplayHeight = baseHeight;
1657 mBaseDisplayDensity = baseDensity;
1658
1659 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1660 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1661 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1662 mBaseDisplayWidth = mMaxUiWidth;
1663
1664 if (DEBUG_DISPLAY) {
1665 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1666 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1667 + " on display:" + getDisplayId());
1668 }
1669 }
1670
1671 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1672 }
1673
Chong Zhangf66db432016-01-13 10:39:51 -08001674 void getContentRect(Rect out) {
1675 out.set(mContentRect);
1676 }
1677
Wale Ogunwale1666e312016-12-16 11:27:18 -08001678 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1679 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1680 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001681
Wale Ogunwale1666e312016-12-16 11:27:18 -08001682 TaskStack stack = getStackById(stackId);
1683 if (stack != null) {
1684 // It's already attached to the display...clear mDeferRemoval and move stack to
1685 // appropriate z-order on display as needed.
1686 stack.mDeferRemoval = false;
1687 // We're not moving the display to front when we're adding stacks, only when
1688 // requested to change the position of stack explicitly.
1689 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1690 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001691 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001692 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001693 mTaskStackContainers.addStackToDisplay(stack, onTop);
1694 }
1695
Wale Ogunwale1666e312016-12-16 11:27:18 -08001696 if (stackId == DOCKED_STACK_ID) {
1697 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001698 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001699 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001700 }
1701
Andrii Kulian51c1b672017-04-07 18:39:32 -07001702 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001703 final DisplayContent prevDc = stack.getDisplayContent();
1704 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001705 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1706 + " which is not currently attached to any display");
1707 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001708 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001709 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1710 + " to its current displayId=" + mDisplayId);
1711 }
1712
Wale Ogunwale1666e312016-12-16 11:27:18 -08001713 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001714 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001715 }
1716
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001717 @Override
1718 protected void addChild(DisplayChildWindowContainer child,
1719 Comparator<DisplayChildWindowContainer> comparator) {
1720 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1721 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001722
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001723 @Override
1724 protected void addChild(DisplayChildWindowContainer child, int index) {
1725 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1726 }
1727
1728 @Override
1729 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001730 // Only allow removal of direct children from this display if the display is in the process
1731 // of been removed.
1732 if (mRemovingDisplay) {
1733 super.removeChild(child);
1734 return;
1735 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001736 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001737 }
1738
Andrii Kuliand2765632016-12-12 22:26:34 -08001739 @Override
1740 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1741 // Children of the display are statically ordered, so the real intention here is to perform
1742 // the operation on the display and not the static direct children.
1743 getParent().positionChildAt(position, this, includingParents);
1744 }
1745
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001746 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001747 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1748 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001749 final int taskId = stack.taskIdFromPoint(x, y);
1750 if (taskId != -1) {
1751 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001752 }
1753 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001754 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001755 }
1756
Chong Zhang8e89b312015-09-09 15:09:30 -07001757 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001758 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001759 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001760 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001761 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001762 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001763 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001764 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1765 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001766 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001767 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001768 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001769
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001770 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1771 if (mTmpTaskForResizePointSearchResult.searchDone) {
1772 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001773 }
1774 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001775 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001776 }
1777
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001778 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001779 // The provided task is the task on this display with focus, so if WindowManagerService's
1780 // focused app is not on this display, focusedTask will be null.
1781 if (focusedTask == null) {
1782 mTouchExcludeRegion.setEmpty();
1783 } else {
1784 mTouchExcludeRegion.set(mBaseDisplayRect);
1785 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1786 mTmpRect2.setEmpty();
1787 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1788 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1789 stack.setTouchExcludeRegion(
1790 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1791 }
1792 // If we removed the focused task above, add it back and only leave its
1793 // outside touch area in the exclusion. TapDectector is not interested in
1794 // any touch inside the focused task itself.
1795 if (!mTmpRect2.isEmpty()) {
1796 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1797 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001798 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001799 final WindowState inputMethod = mService.mInputMethodWindow;
1800 if (inputMethod != null && inputMethod.isVisibleLw()) {
1801 // If the input method is visible and the user is typing, we don't want these touch
1802 // events to be intercepted and used to change focus. This would likely cause a
1803 // disappearance of the input method.
1804 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001805 if (inputMethod.getDisplayId() == mDisplayId) {
1806 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1807 } else {
1808 // IME is on a different display, so we need to update its tap detector.
1809 // TODO(multidisplay): Remove when IME will always appear on same display.
1810 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1811 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001812 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001813 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1814 WindowState win = mTapExcludedWindows.get(i);
1815 win.getTouchableRegion(mTmpRegion);
1816 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1817 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001818 // TODO(multi-display): Support docked stacks on secondary displays.
1819 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001820 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001821 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001822 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1823 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001824 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001825 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001826 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001827 }
1828
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001829 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001830 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001831 super.switchUser();
1832 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001833 }
1834
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001835 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001836 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1837 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001838 }
1839 }
1840
1841 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001842 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001843 }
1844
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001845 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001846 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001847 }
1848
1849 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001850 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001851 }
1852
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001853 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001854 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001855 }
1856
Wale Ogunwale10124582016-09-15 20:25:50 -07001857 @Override
1858 void removeIfPossible() {
1859 if (isAnimating()) {
1860 mDeferredRemoval = true;
1861 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001862 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001863 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001864 }
1865
Wale Ogunwale10124582016-09-15 20:25:50 -07001866 @Override
1867 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001868 mRemovingDisplay = true;
1869 try {
1870 super.removeImmediately();
1871 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1872 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001873 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001874 mService.unregisterPointerEventListener(mTapDetector);
1875 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1876 }
1877 } finally {
1878 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001879 }
Craig Mautner95da1082014-02-24 17:54:35 -08001880 }
1881
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001882 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001883 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001884 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001885 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1886
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001887 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001888 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001889 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001890 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001891 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001892 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001893 }
1894
Wale Ogunwale10124582016-09-15 20:25:50 -07001895 boolean animateForIme(float interpolatedValue, float animationTarget,
1896 float dividerAnimationTarget) {
1897 boolean updated = false;
1898
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001899 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1900 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001901 if (stack == null || !stack.isAdjustedForIme()) {
1902 continue;
1903 }
1904
1905 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1906 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1907 updated = true;
1908 } else {
1909 mDividerControllerLocked.mLastAnimationProgress =
1910 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1911 mDividerControllerLocked.mLastDividerProgress =
1912 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1913 updated |= stack.updateAdjustForIme(
1914 mDividerControllerLocked.mLastAnimationProgress,
1915 mDividerControllerLocked.mLastDividerProgress,
1916 false /* force */);
1917 }
1918 if (interpolatedValue >= 1f) {
1919 stack.endImeAdjustAnimation();
1920 }
1921 }
1922
1923 return updated;
1924 }
1925
1926 boolean clearImeAdjustAnimation() {
1927 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001928 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1929 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001930 if (stack != null && stack.isAdjustedForIme()) {
1931 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1932 changed = true;
1933 }
1934 }
1935 return changed;
1936 }
1937
1938 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001939 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1940 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001941 if (stack.isVisible() && stack.isAdjustedForIme()) {
1942 stack.beginImeAdjustAnimation();
1943 }
1944 }
1945 }
1946
1947 void adjustForImeIfNeeded() {
1948 final WindowState imeWin = mService.mInputMethodWindow;
1949 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1950 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001951 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001952 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1953 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1954 imeTargetStack.getDockSide() : DOCKED_INVALID;
1955 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1956 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1957 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1958 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1959 final boolean imeHeightChanged = imeVisible &&
1960 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1961
1962 // The divider could be adjusted for IME position, or be thinner than usual,
1963 // or both. There are three possible cases:
1964 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1965 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1966 // - If IME is not visible, divider is not moved and is normal width.
1967
1968 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001969 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1970 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001971 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07001972 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
1973 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001974 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1975 } else {
1976 stack.resetAdjustedForIme(false);
1977 }
1978 }
1979 mDividerControllerLocked.setAdjustedForIme(
1980 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1981 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001982 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1983 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001984 stack.resetAdjustedForIme(!dockVisible);
1985 }
1986 mDividerControllerLocked.setAdjustedForIme(
1987 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
1988 }
Winson Chung655332c2016-10-31 13:14:28 -07001989 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07001990 }
1991
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001992 void setInputMethodAnimLayerAdjustment(int adj) {
1993 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
1994 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08001995 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001996 }
1997
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001998 /**
1999 * If a window that has an animation specifying a colored background and the current wallpaper
2000 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2001 * suddenly disappear.
2002 */
2003 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002004 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2005 w -> w.mIsWallpaper && w.isVisibleNow());
2006
2007 if (visibleWallpaper != null) {
2008 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002009 }
2010 return winAnimator.mAnimLayer;
2011 }
2012
Wale Ogunwale10124582016-09-15 20:25:50 -07002013 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002014 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2015 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002016 stack.prepareFreezingTaskBounds();
2017 }
2018 }
2019
Wale Ogunwale94744212015-09-21 19:01:47 -07002020 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002021 getLogicalDisplayRect(mTmpRect, newRotation);
2022
2023 // Compute a transform matrix to undo the coordinate space transformation,
2024 // and present the window at the same physical position it previously occupied.
2025 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2026 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2027
2028 mTmpRectF.set(bounds);
2029 mTmpMatrix.mapRect(mTmpRectF);
2030 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002031 }
2032
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002033 static int deltaRotation(int oldRotation, int newRotation) {
2034 int delta = newRotation - oldRotation;
2035 if (delta < 0) delta += 4;
2036 return delta;
2037 }
2038
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002039 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002040 Matrix outMatrix) {
2041 // For rotations without Z-ordering we don't need the target rectangle's position.
2042 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2043 displayHeight, outMatrix);
2044 }
2045
2046 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2047 float displayWidth, float displayHeight, Matrix outMatrix) {
2048 switch (rotation) {
2049 case ROTATION_0:
2050 outMatrix.reset();
2051 break;
2052 case ROTATION_270:
2053 outMatrix.setRotate(270, 0, 0);
2054 outMatrix.postTranslate(0, displayHeight);
2055 outMatrix.postTranslate(rectTop, 0);
2056 break;
2057 case ROTATION_180:
2058 outMatrix.reset();
2059 break;
2060 case ROTATION_90:
2061 outMatrix.setRotate(90, 0, 0);
2062 outMatrix.postTranslate(displayWidth, 0);
2063 outMatrix.postTranslate(-rectTop, rectLeft);
2064 break;
2065 }
2066 }
2067
Craig Mautnera91f9e22012-09-14 16:22:08 -07002068 public void dump(String prefix, PrintWriter pw) {
2069 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2070 final String subPrefix = " " + prefix;
2071 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2072 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2073 pw.print("dpi");
2074 if (mInitialDisplayWidth != mBaseDisplayWidth
2075 || mInitialDisplayHeight != mBaseDisplayHeight
2076 || mInitialDisplayDensity != mBaseDisplayDensity) {
2077 pw.print(" base=");
2078 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2079 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2080 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002081 if (mDisplayScalingDisabled) {
2082 pw.println(" noscale");
2083 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002084 pw.print(" cur=");
2085 pw.print(mDisplayInfo.logicalWidth);
2086 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2087 pw.print(" app=");
2088 pw.print(mDisplayInfo.appWidth);
2089 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2090 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2091 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2092 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2093 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002094 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2095 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002096
Craig Mautnerdc548482014-02-05 13:35:24 -08002097 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002098 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002099 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2100 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002101 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002102 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002103
Craig Mautnerdc548482014-02-05 13:35:24 -08002104 pw.println();
2105 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002106 pw.println();
2107 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002108 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002109 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002110 pw.print(" Exiting #"); pw.print(i);
2111 pw.print(' '); pw.print(token);
2112 pw.println(':');
2113 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002114 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002115 }
Craig Mautner59c00972012-07-30 12:10:24 -07002116 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002117 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002118 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002119 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002120 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002121 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002122
2123 if (mInputMethodAnimLayerAdjustment != 0) {
2124 pw.println(subPrefix
2125 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2126 }
Craig Mautner59c00972012-07-30 12:10:24 -07002127 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002128
2129 @Override
2130 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002131 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002132 }
2133
2134 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002135 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002136 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002137
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002138 /** Checks if stack with provided id is visible on this display. */
2139 boolean isStackVisible(int stackId) {
2140 final TaskStack stack = getStackById(stackId);
2141 return (stack != null && stack.isVisible());
2142 }
2143
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002144 /**
2145 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2146 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002147 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002148 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002149 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002150 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002151
2152 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002153 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002154 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002155 */
Jorim Jaggife762342016-10-13 14:33:27 +02002156 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002157 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002158 }
2159
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002160 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002161 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002162 final int x = (int) xf;
2163 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002164 final WindowState touchedWin = getWindow(w -> {
2165 final int flags = w.mAttrs.flags;
2166 if (!w.isVisibleLw()) {
2167 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002168 }
2169 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002170 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002171 }
2172
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002173 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002174 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002175 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002176 }
2177
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002178 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002179
2180 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002181 return mTmpRegion.contains(x, y) || touchFlags == 0;
2182 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002183
2184 return touchedWin;
2185 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002186
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002187 boolean canAddToastWindowForUid(int uid) {
2188 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002189 // Also if the app is focused adding more than one toast at
2190 // a time for better backwards compatibility.
2191 final WindowState focusedWindowForUid = getWindow(w ->
2192 w.mOwnerUid == uid && w.isFocused());
2193 if (focusedWindowForUid != null) {
2194 return true;
2195 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002196 final WindowState win = getWindow(w ->
2197 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2198 && !w.mWindowRemovalAllowed);
2199 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002200 }
2201
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002202 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002203 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2204 return;
2205 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002206
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002207 // Used to communicate the old focus to the callback method.
2208 mTmpWindow = oldFocus;
2209
2210 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002211 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002212
2213 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002214 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002215
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002216 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002217
2218 if (mTmpWindow == null) {
2219 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2220 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002221 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002222 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002223 }
2224
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002225 /** Updates the layer assignment of windows on this display. */
2226 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002227 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002228 if (setLayoutNeeded) {
2229 setLayoutNeeded();
2230 }
2231 }
2232
Wale Ogunwale1666e312016-12-16 11:27:18 -08002233 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2234 // moving containers or resizing them. Need to investigate the best way to have it automatically
2235 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002236 void layoutAndAssignWindowLayersIfNeeded() {
2237 mService.mWindowsChanged = true;
2238 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002239
2240 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2241 false /*updateInputWindows*/)) {
2242 assignWindowLayers(false /* setLayoutNeeded */);
2243 }
2244
2245 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2246 mService.mWindowPlacerLocked.performSurfacePlacement();
2247 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2248 }
2249
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002250 /** Returns true if a leaked surface was destroyed */
2251 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002252 // Used to indicate that a surface was leaked.
2253 mTmpWindow = null;
2254 forAllWindows(w -> {
2255 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002256 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002257 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002258 }
2259 if (!mService.mSessions.contains(wsa.mSession)) {
2260 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002261 + w + " surface=" + wsa.mSurfaceController
2262 + " token=" + w.mToken
2263 + " pid=" + w.mSession.mPid
2264 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002265 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002266 mService.mForceRemoves.add(w);
2267 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002268 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002269 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002270 + w + " surface=" + wsa.mSurfaceController
2271 + " token=" + w.mAppToken
2272 + " saved=" + w.hasSavedSurface());
2273 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002274 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002275 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002276 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002277 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002278
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002279 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002280 }
2281
2282 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002283 * Determine and return the window that should be the IME target.
2284 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2285 * @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 +00002286 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002287 WindowState computeImeTarget(boolean updateImeTarget) {
2288 if (mService.mInputMethodWindow == null) {
2289 // There isn't an IME so there shouldn't be a target...That was easy!
2290 if (updateImeTarget) {
2291 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2292 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2293 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2294 }
2295 return null;
2296 }
2297
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002298 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2299 // same display. Or even when the current IME/target are not on the same screen as the next
2300 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002301 mUpdateImeTarget = updateImeTarget;
2302 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002303
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002304
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002305 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2306 // to be on top of it, but it is not -really- where input will go. So look down below
2307 // for a real window to target...
2308 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2309 final AppWindowToken token = target.mAppToken;
2310 if (token != null) {
2311 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2312 if (betterTarget != null) {
2313 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002314 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002315 }
2316 }
2317
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002318 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2319 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002320
2321 // Now, a special case -- if the last target's window is in the process of exiting, and is
2322 // above the new target, keep on the last target to avoid flicker. Consider for example a
2323 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2324 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2325 // scrim.
2326 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002327 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2328 && (target == null
2329 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002330 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002331 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002332 }
2333
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002334 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2335 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002336
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002337 if (target == null) {
2338 if (updateImeTarget) {
2339 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2340 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2341 + Debug.getCallers(4) : ""));
2342 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2343 }
2344
2345 return null;
2346 }
2347
2348 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002349 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2350 if (token != null) {
2351
2352 // Now some fun for dealing with window animations that modify the Z order. We need
2353 // to look at all windows below the current target that are in this app, finding the
2354 // highest visible one in layering.
2355 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002356 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002357 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002358 }
2359
2360 if (highestTarget != null) {
2361 final AppTransition appTransition = mService.mAppTransition;
2362 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2363 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2364 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002365 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002366
2367 if (appTransition.isTransitionSet()) {
2368 // If we are currently setting up for an animation, hold everything until we
2369 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002370 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2371 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002372 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002373 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002374 // If the window we are currently targeting is involved with an animation,
2375 // and it is on top of the next target we will be over, then hold off on
2376 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002377 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2378 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002379 }
2380 }
2381 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382
2383 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2384 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2385 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002386 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002387 }
2388
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002389 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002390 }
2391
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2393 if (target == mService.mInputMethodTarget
2394 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2395 && mInputMethodAnimLayerAdjustment == layerAdj) {
2396 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002397 }
2398
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002399 mService.mInputMethodTarget = target;
2400 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2401 setInputMethodAnimLayerAdjustment(layerAdj);
2402 assignWindowLayers(false /* setLayoutNeeded */);
2403 }
2404
2405 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2406 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2407 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2408 }
2409
2410 // Used to indicate we have reached the first window in the range we are interested in.
2411 mTmpWindow = null;
2412
2413 // TODO: Figure-out a more efficient way to do this.
2414 final WindowState candidate = getWindow(w -> {
2415 if (w == top) {
2416 // Reached the first window in the range we are interested in.
2417 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002418 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002419 if (mTmpWindow == null) {
2420 return false;
2421 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002422
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002423 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2424 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002425 }
2426 // If we reached the bottom of the range of windows we are considering,
2427 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002428 if (w == bottom) {
2429 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002430 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002431 return false;
2432 });
2433
2434 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002435 }
2436
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002437 void setLayoutNeeded() {
2438 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2439 mLayoutNeeded = true;
2440 }
2441
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002442 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002443 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2444 mLayoutNeeded = false;
2445 }
2446
2447 boolean isLayoutNeeded() {
2448 return mLayoutNeeded;
2449 }
2450
Wale Ogunwale02319a62016-09-26 15:21:22 -07002451 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2452 if (mTokenMap.isEmpty()) {
2453 return;
2454 }
2455 pw.println(" Display #" + mDisplayId);
2456 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2457 while (it.hasNext()) {
2458 final WindowToken token = it.next();
2459 pw.print(" ");
2460 pw.print(token);
2461 if (dumpAll) {
2462 pw.println(':');
2463 token.dump(pw, " ");
2464 } else {
2465 pw.println();
2466 }
2467 }
2468 }
2469
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002470 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002471 final int[] index = new int[1];
2472 forAllWindows(w -> {
2473 final WindowStateAnimator wAnim = w.mWinAnimator;
2474 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2475 index[0] = index[0] + 1;
2476 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002477 }
2478
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002479 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002480 forAllWindows(w -> {
2481 w.mWinAnimator.enableSurfaceTrace(fd);
2482 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002483 }
2484
2485 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002486 forAllWindows(w -> {
2487 w.mWinAnimator.disableSurfaceTrace();
2488 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002489 }
2490
Jorim Jaggife762342016-10-13 14:33:27 +02002491 /**
2492 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2493 */
2494 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2495 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002496 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002497 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2498 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002499 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002500 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2501 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002502 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002503 }
2504
Wale Ogunwale494009b82016-10-21 09:01:38 -07002505 boolean checkWaitingForWindows() {
2506
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002507 mHaveBootMsg = false;
2508 mHaveApp = false;
2509 mHaveWallpaper = false;
2510 mHaveKeyguard = true;
2511
2512 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002513 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2514 return true;
2515 }
2516 if (w.isDrawnLw()) {
2517 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002518 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002519 } else if (w.mAttrs.type == TYPE_APPLICATION
2520 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002521 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002522 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002523 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002524 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002525 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002526 }
2527 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002528 return false;
2529 });
2530
2531 if (visibleWindow != null) {
2532 // We have a visible window.
2533 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002534 }
2535
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002536 // if the wallpaper service is disabled on the device, we're never going to have
2537 // wallpaper, don't bother waiting for it
2538 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2539 com.android.internal.R.bool.config_enableWallpaperService)
2540 && !mService.mOnlyCore;
2541
Wale Ogunwale494009b82016-10-21 09:01:38 -07002542 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2543 "******** booted=" + mService.mSystemBooted
2544 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002545 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2546 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2547 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002548
2549 // If we are turning on the screen to show the boot message, don't do it until the boot
2550 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002551 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002552 return true;
2553 }
2554
2555 // If we are turning on the screen after the boot is completed normally, don't do so until
2556 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002557 if (mService.mSystemBooted
2558 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002559 return true;
2560 }
2561
2562 return false;
2563 }
2564
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002565 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002566 mTmpWindowAnimator = animator;
2567 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002568 }
2569
2570 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002571 resetAnimationBackgroundAnimator();
2572
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002573 // Used to indicate a detached wallpaper.
2574 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002575 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002576
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002577 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002578
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002579 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002580 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002581 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2582 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002583 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2584 }
2585 }
2586
2587 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002588 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002589 }
2590
Wale Ogunwale494009b82016-10-21 09:01:38 -07002591 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002592 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002593 if (imFocus == null) {
2594 return false;
2595 }
2596
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002597 if (DEBUG_INPUT_METHOD) {
2598 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2599 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2600 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002601 }
2602
Wale Ogunwale494009b82016-10-21 09:01:38 -07002603 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2604
2605 if (DEBUG_INPUT_METHOD) {
2606 Slog.i(TAG_WM, "IM target client: " + imeClient);
2607 if (imeClient != null) {
2608 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2609 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2610 }
2611 }
2612
2613 return imeClient != null && imeClient.asBinder() == client.asBinder();
2614 }
2615
2616 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002617 final WindowState win = getWindow(
2618 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2619 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002620 }
2621
2622 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002623 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002624 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002625 final int curValue = w.mSystemUiVisibility;
2626 final int diff = (curValue ^ visibility) & globalDiff;
2627 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002628 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002629 w.mSeq++;
2630 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002631 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2633 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002634 visibility, newValue, diff);
2635 }
2636 } catch (RemoteException e) {
2637 // so sorry
2638 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002639 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002640 }
2641
2642 void onWindowFreezeTimeout() {
2643 Slog.w(TAG_WM, "Window freeze timeout expired.");
2644 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002645
2646 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002647 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002648 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002649 }
2650 w.mOrientationChanging = false;
2651 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2652 - mService.mDisplayFreezeTime);
2653 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002654 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002655 mService.mWindowPlacerLocked.performSurfacePlacement();
2656 }
2657
2658 void waitForAllWindowsDrawn() {
2659 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002660 forAllWindows(w -> {
2661 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2662 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2663 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002664 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002665 w.mLastContentInsets.set(-1, -1, -1, -1);
2666 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002667 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002668 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002669 }
2670
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002671 // TODO: Super crazy long method that should be broken down...
2672 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2673
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002674 final int dw = mDisplayInfo.logicalWidth;
2675 final int dh = mDisplayInfo.logicalHeight;
2676 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2677
2678 mTmpUpdateAllDrawn.clear();
2679
2680 int repeats = 0;
2681 do {
2682 repeats++;
2683 if (repeats > 6) {
2684 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2685 clearLayoutNeeded();
2686 break;
2687 }
2688
2689 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2690 pendingLayoutChanges);
2691
Andrii Kulian839def92016-11-02 10:58:58 -07002692 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2693 // the wallpaper window jumping across displays.
2694 // Remove check for default display when there will be support for multiple wallpaper
2695 // targets (on different displays).
2696 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002697 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002698 }
2699
2700 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2701 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2702 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2703 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002704 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002705 }
2706 }
2707
2708 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2709 setLayoutNeeded();
2710 }
2711
2712 // FIRST LOOP: Perform a layout, if needed.
2713 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2714 performLayout(repeats == 1, false /* updateInputWindows */);
2715 } else {
2716 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2717 }
2718
2719 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2720 pendingLayoutChanges = 0;
2721
2722 if (isDefaultDisplay) {
2723 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002724 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002725 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2726 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2727 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2728 }
2729 } while (pendingLayoutChanges != 0);
2730
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002731 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002732 resetDimming();
2733
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002734 mTmpRecoveringMemory = recoveringMemory;
2735 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002736
2737 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002738 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2739 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2740 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002741 true /* inTraversal, must call performTraversalInTrans... below */);
2742
2743 stopDimmingIfNeeded();
2744
2745 while (!mTmpUpdateAllDrawn.isEmpty()) {
2746 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2747 // See if any windows have been drawn, so they (and others associated with them)
2748 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002749 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002750 }
2751
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002752 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002753 }
2754
2755 void performLayout(boolean initial, boolean updateInputWindows) {
2756 if (!isLayoutNeeded()) {
2757 return;
2758 }
2759 clearLayoutNeeded();
2760
2761 final int dw = mDisplayInfo.logicalWidth;
2762 final int dh = mDisplayInfo.logicalHeight;
2763
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002764 if (DEBUG_LAYOUT) {
2765 Slog.v(TAG, "-------------------------------------");
2766 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2767 }
2768
Andrii Kulian8ee72852017-03-10 10:36:45 -08002769 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002770 getConfiguration().uiMode);
2771 if (isDefaultDisplay) {
2772 // Not needed on non-default displays.
2773 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2774 mService.mScreenRect.set(0, 0, dw, dh);
2775 }
2776
2777 mService.mPolicy.getContentRectLw(mContentRect);
2778
2779 int seq = mService.mLayoutSeq + 1;
2780 if (seq < 0) seq = 0;
2781 mService.mLayoutSeq = seq;
2782
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002783 // Used to indicate that we have processed the dream window and all additional windows are
2784 // behind it.
2785 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002786 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002787
2788 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002789 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002790
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002791 // Used to indicate that we have processed the dream window and all additional attached
2792 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002793 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002794 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002795
2796 // Now perform layout of attached windows, which usually depend on the position of the
2797 // window they are attached to. XXX does not deal with windows that are attached to windows
2798 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002799 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002800
2801 // Window frames may have changed. Tell the input dispatcher about it.
2802 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2803 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2804 if (updateInputWindows) {
2805 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2806 }
2807
2808 mService.mPolicy.finishLayoutLw();
2809 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2810 }
2811
2812 /**
2813 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2814 * In portrait mode, it grabs the full screenshot.
2815 *
2816 * @param width the width of the target bitmap
2817 * @param height the height of the target bitmap
2818 * @param includeFullDisplay true if the screen should not be cropped before capture
2819 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2820 * @param config of the output bitmap
2821 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002822 * @param includeDecor whether to include window decors, like the status or navigation bar
2823 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002824 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002825 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002826 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002827 boolean wallpaperOnly, boolean includeDecor) {
2828 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2829 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002830 if (bitmap == null) {
2831 return null;
2832 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002833
2834 if (DEBUG_SCREENSHOT) {
2835 // TEST IF IT's ALL BLACK
2836 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2837 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2838 bitmap.getHeight());
2839 boolean allBlack = true;
2840 final int firstColor = buffer[0];
2841 for (int i = 0; i < buffer.length; i++) {
2842 if (buffer[i] != firstColor) {
2843 allBlack = false;
2844 break;
2845 }
2846 }
2847 if (allBlack) {
2848 final WindowState appWin = mScreenshotApplicationState.appWin;
2849 final int maxLayer = mScreenshotApplicationState.maxLayer;
2850 final int minLayer = mScreenshotApplicationState.minLayer;
2851 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2852 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2853 (appWin != null ?
2854 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2855 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2856 }
2857 }
2858
2859 // Create a copy of the screenshot that is immutable and backed in ashmem.
2860 // This greatly reduces the overhead of passing the bitmap between processes.
2861 Bitmap ret = bitmap.createAshmemBitmap(config);
2862 bitmap.recycle();
2863 return ret;
2864 }
2865
2866 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2867 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2868 boolean includeDecor) {
2869 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2870 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2871 }
2872
2873 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2874 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2875 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002876 int dw = mDisplayInfo.logicalWidth;
2877 int dh = mDisplayInfo.logicalHeight;
2878 if (dw == 0 || dh == 0) {
2879 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2880 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2881 return null;
2882 }
2883
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002884 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002885
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002886 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002887 final Rect frame = new Rect();
2888 final Rect stackBounds = new Rect();
2889
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002890 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002891 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002892 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002893 synchronized(mService.mWindowMap) {
2894 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002895 mScreenshotApplicationState.appWin = null;
2896 forAllWindows(w -> {
2897 if (!w.mHasSurface) {
2898 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002899 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002900 if (w.mLayer >= aboveAppLayer) {
2901 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002902 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002903 if (wallpaperOnly && !w.mIsWallpaper) {
2904 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002905 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002906 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02002907 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002908 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002909 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2910 // then the target window state is this one.
2911 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002912 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002913 }
2914
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002915 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002916 // We have not ran across the target window yet, so it is probably behind
2917 // the wallpaper. This can happen when the keyguard is up and all windows
2918 // are moved behind the wallpaper. We don't want to include the wallpaper
2919 // layer in the screenshot as it will cover-up the layer of the target
2920 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002921 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002922 }
2923 // Fall through. The target window is in front of the wallpaper. For this
2924 // case we want to include the wallpaper layer in the screenshot because
2925 // the target window might have some transparent areas.
2926 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002927 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002928 // This app window is of no interest if it is not associated with the
2929 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002930 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002931 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002932 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002933 }
2934
2935 // Include this window.
2936
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002937 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002938 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002939 if (mScreenshotApplicationState.maxLayer < layer) {
2940 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002941 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002942 if (mScreenshotApplicationState.minLayer > layer) {
2943 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002944 }
2945
2946 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02002947 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
2948 if (includeDecor) {
2949 final TaskStack stack = w.getStack();
2950 if (stack != null) {
2951 stack.getBounds(frame);
2952 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02002953
Jorim Jaggi70f59482017-05-04 14:05:59 +02002954 // We want to screenshot with the exact bounds of the surface of the app. Thus,
2955 // intersect it with the frame.
2956 frame.intersect(w.mFrame);
2957 } else {
2958 final Rect wf = w.mFrame;
2959 final Rect cr = w.mContentInsets;
2960 int left = wf.left + cr.left;
2961 int top = wf.top + cr.top;
2962 int right = wf.right - cr.right;
2963 int bottom = wf.bottom - cr.bottom;
2964 frame.union(left, top, right, bottom);
2965 w.getVisibleBounds(stackBounds);
2966 if (!Rect.intersects(frame, stackBounds)) {
2967 // Set frame empty if there's no intersection.
2968 frame.setEmpty();
2969 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002970 }
2971 }
2972
2973 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002974 (w.mAppToken != null && w.mAppToken.token == appToken)
2975 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002976 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002977 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002978 }
2979
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002980 if (w.isObscuringDisplay()){
2981 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002982 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002983 return false;
2984 }, true /* traverseTopToBottom */);
2985
2986 final WindowState appWin = mScreenshotApplicationState.appWin;
2987 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2988 final int maxLayer = mScreenshotApplicationState.maxLayer;
2989 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002990
2991 if (appToken != null && appWin == null) {
2992 // Can't find a window to snapshot.
2993 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2994 "Screenshot: Couldn't find a surface matching " + appToken);
2995 return null;
2996 }
2997
2998 if (!screenshotReady) {
2999 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3000 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3001 appWin.mWinAnimator.mDrawState)));
3002 return null;
3003 }
3004
3005 // Screenshot is ready to be taken. Everything from here below will continue
3006 // through the bottom of the loop and return a value. We only stay in the loop
3007 // because we don't want to release the mWindowMap lock until the screenshot is
3008 // taken.
3009
3010 if (maxLayer == 0) {
3011 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3012 + ": returning null maxLayer=" + maxLayer);
3013 return null;
3014 }
3015
Jorim Jaggi02886a82016-12-06 09:10:06 -08003016 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003017 // Constrain frame to the screen size.
3018 if (!frame.intersect(0, 0, dw, dh)) {
3019 frame.setEmpty();
3020 }
3021 } else {
3022 // Caller just wants entire display.
3023 frame.set(0, 0, dw, dh);
3024 }
3025 if (frame.isEmpty()) {
3026 return null;
3027 }
3028
3029 if (width < 0) {
3030 width = (int) (frame.width() * frameScale);
3031 }
3032 if (height < 0) {
3033 height = (int) (frame.height() * frameScale);
3034 }
3035
3036 // Tell surface flinger what part of the image to crop. Take the top
3037 // right part of the application, and crop the larger dimension to fit.
3038 Rect crop = new Rect(frame);
3039 if (width / (float) frame.width() < height / (float) frame.height()) {
3040 int cropWidth = (int)((float)width / (float)height * frame.height());
3041 crop.right = crop.left + cropWidth;
3042 } else {
3043 int cropHeight = (int)((float)height / (float)width * frame.width());
3044 crop.bottom = crop.top + cropHeight;
3045 }
3046
3047 // The screenshot API does not apply the current screen rotation.
3048 int rot = mDisplay.getRotation();
3049
3050 if (rot == ROTATION_90 || rot == ROTATION_270) {
3051 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3052 }
3053
3054 // Surfaceflinger is not aware of orientation, so convert our logical
3055 // crop to surfaceflinger's portrait orientation.
3056 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3057
3058 if (DEBUG_SCREENSHOT) {
3059 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3060 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003061 forAllWindows(w -> {
3062 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3063 Slog.i(TAG_WM, w + ": " + w.mLayer
3064 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003065 + " surfaceLayer=" + ((controller == null)
3066 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003067 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003068 }
3069
3070 final ScreenRotationAnimation screenRotationAnimation =
3071 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3072 final boolean inRotation = screenRotationAnimation != null &&
3073 screenRotationAnimation.isAnimating();
3074 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3075 "Taking screenshot while rotating");
3076
3077 // We force pending transactions to flush before taking
3078 // the screenshot by pushing an empty synchronous transaction.
3079 SurfaceControl.openTransaction();
3080 SurfaceControl.closeTransactionSync();
3081
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003082 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003083 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003084 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003085 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3086 + ") to layer " + maxLayer);
3087 return null;
3088 }
3089 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003090 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003091 }
3092
3093 // TODO: Can this use createRotationMatrix()?
3094 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3095 if (rot == Surface.ROTATION_90) {
3096 final int tmp = crop.top;
3097 crop.top = dw - crop.right;
3098 crop.right = crop.bottom;
3099 crop.bottom = dw - crop.left;
3100 crop.left = tmp;
3101 } else if (rot == Surface.ROTATION_180) {
3102 int tmp = crop.top;
3103 crop.top = dh - crop.bottom;
3104 crop.bottom = dh - tmp;
3105 tmp = crop.right;
3106 crop.right = dw - crop.left;
3107 crop.left = dw - tmp;
3108 } else if (rot == Surface.ROTATION_270) {
3109 final int tmp = crop.top;
3110 crop.top = crop.left;
3111 crop.left = dh - crop.bottom;
3112 crop.bottom = crop.right;
3113 crop.right = dh - tmp;
3114 }
3115 }
3116
3117 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003118 // Used to indicate the layout is needed.
3119 mTmpWindow = null;
3120
3121 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003122 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003123 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003124 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003125 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003126 w.setDisplayLayoutNeeded();
3127 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003128 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003129
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003130 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003131 mService.mWindowPlacerLocked.performSurfacePlacement();
3132 }
3133 }
3134
Wale Ogunwale1666e312016-12-16 11:27:18 -08003135 void setExitingTokensHasVisible(boolean hasVisible) {
3136 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3137 mExitingTokens.get(i).hasVisible = hasVisible;
3138 }
3139
3140 // Initialize state of exiting applications.
3141 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3142 }
3143
3144 void removeExistingTokensIfPossible() {
3145 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3146 final WindowToken token = mExitingTokens.get(i);
3147 if (!token.hasVisible) {
3148 mExitingTokens.remove(i);
3149 }
3150 }
3151
3152 // Time to remove any exiting applications?
3153 mTaskStackContainers.removeExistingAppTokensIfPossible();
3154 }
3155
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003156 @Override
3157 void onDescendantOverrideConfigurationChanged() {
3158 setLayoutNeeded();
3159 mService.requestTraversal();
3160 }
3161
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003162 static final class TaskForResizePointSearchResult {
3163 boolean searchDone;
3164 Task taskForResize;
3165
3166 void reset() {
3167 searchDone = false;
3168 taskForResize = null;
3169 }
3170 }
Robert Carr3b716242016-08-16 16:02:21 -07003171
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003172 private static final class ApplySurfaceChangesTransactionState {
3173 boolean displayHasContent;
3174 boolean obscured;
3175 boolean syswin;
3176 boolean focusDisplayed;
3177 float preferredRefreshRate;
3178 int preferredModeId;
3179
3180 void reset() {
3181 displayHasContent = false;
3182 obscured = false;
3183 syswin = false;
3184 focusDisplayed = false;
3185 preferredRefreshRate = 0;
3186 preferredModeId = 0;
3187 }
3188 }
3189
3190 private static final class ScreenshotApplicationState {
3191 WindowState appWin;
3192 int maxLayer;
3193 int minLayer;
3194 boolean screenshotReady;
3195
3196 void reset(boolean screenshotReady) {
3197 appWin = null;
3198 maxLayer = 0;
3199 minLayer = 0;
3200 this.screenshotReady = screenshotReady;
3201 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3202 }
3203 }
3204
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003205 /**
3206 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3207 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3208 * homogeneous children type which is currently required by sub-classes of
3209 * {@link WindowContainer} class.
3210 */
3211 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3212
3213 int size() {
3214 return mChildren.size();
3215 }
3216
3217 E get(int index) {
3218 return mChildren.get(index);
3219 }
3220
3221 @Override
3222 boolean fillsParent() {
3223 return true;
3224 }
3225
3226 @Override
3227 boolean isVisible() {
3228 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003229 }
3230 }
3231
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003232 /**
3233 * Window container class that contains all containers on this display relating to Apps.
3234 * I.e Activities.
3235 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003236 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003237
Andrii Kulian839def92016-11-02 10:58:58 -07003238 /**
3239 * Adds the stack to this container.
3240 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3241 */
3242 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003243 if (stack.mStackId == HOME_STACK_ID) {
3244 if (mHomeStack != null) {
3245 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3246 }
3247 mHomeStack = stack;
3248 }
3249 addChild(stack, onTop);
3250 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003251 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003252
Andrii Kulian839def92016-11-02 10:58:58 -07003253 /** Removes the stack from its container and prepare for changing the parent. */
3254 void removeStackFromDisplay(TaskStack stack) {
3255 removeChild(stack);
3256 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003257 }
3258
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003259 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003260 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3261 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003262 addChild(stack, addIndex);
3263 setLayoutNeeded();
3264 }
3265
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003266 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003267 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3268 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3269 // This stack is always-on-top, override the default behavior.
3270 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3271
3272 // Moving to its current position, as we must call super but we don't want to
3273 // perform any meaningful action.
3274 final int currentPosition = mChildren.indexOf(child);
3275 super.positionChildAt(currentPosition, child, false /* includingParents */);
3276 return;
3277 }
3278
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003279 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3280 super.positionChildAt(targetPosition, child, includingParents);
3281
3282 setLayoutNeeded();
3283 }
3284
3285 /**
3286 * When stack is added or repositioned, find a proper position for it.
3287 * This will make sure that pinned stack always stays on top.
3288 * @param requestedPosition Position requested by caller.
3289 * @param stack Stack to be added or positioned.
3290 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3291 * @return The proper position for the stack.
3292 */
3293 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3294 final int topChildPosition = mChildren.size() - 1;
3295 boolean toTop = requestedPosition == POSITION_TOP;
3296 toTop |= adding ? requestedPosition >= topChildPosition + 1
3297 : requestedPosition >= topChildPosition;
3298 int targetPosition = requestedPosition;
3299
Bryce Lee48f4b572017-04-10 10:54:15 -07003300 if (toTop && stack.mStackId != PINNED_STACK_ID
3301 && getStackById(PINNED_STACK_ID) != null) {
3302 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003303 TaskStack topStack = mChildren.get(topChildPosition);
3304 if (topStack.mStackId != PINNED_STACK_ID) {
3305 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3306 }
3307
3308 // So, stack is moved just below the pinned stack.
3309 // When we're adding a new stack the target is the current pinned stack position.
3310 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003311 // stack, because WindowContainer#positionAt() first removes element and then adds
3312 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003313 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3314 }
3315
3316 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003317 }
3318
3319 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003320 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3321 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003322 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003323 if (super.forAllWindows(callback, traverseTopToBottom)) {
3324 return true;
3325 }
3326 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3327 return true;
3328 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003329 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003330 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3331 return true;
3332 }
3333 if (super.forAllWindows(callback, traverseTopToBottom)) {
3334 return true;
3335 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003336 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003337 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003338 }
3339
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003340 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003341 boolean traverseTopToBottom) {
3342 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3343 // app tokens.
3344 // TODO: Investigate if we need to continue to do this or if we can just process them
3345 // in-order.
3346 if (traverseTopToBottom) {
3347 for (int i = mChildren.size() - 1; i >= 0; --i) {
3348 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3349 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003350 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3351 traverseTopToBottom)) {
3352 return true;
3353 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003354 }
3355 }
3356 } else {
3357 final int count = mChildren.size();
3358 for (int i = 0; i < count; ++i) {
3359 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3360 final int appTokensCount = appTokens.size();
3361 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003362 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3363 traverseTopToBottom)) {
3364 return true;
3365 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003366 }
3367 }
3368 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003369 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003370 }
3371
Wale Ogunwale1666e312016-12-16 11:27:18 -08003372 void setExitingTokensHasVisible(boolean hasVisible) {
3373 for (int i = mChildren.size() - 1; i >= 0; --i) {
3374 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3375 for (int j = appTokens.size() - 1; j >= 0; --j) {
3376 appTokens.get(j).hasVisible = hasVisible;
3377 }
3378 }
3379 }
3380
3381 void removeExistingAppTokensIfPossible() {
3382 for (int i = mChildren.size() - 1; i >= 0; --i) {
3383 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3384 for (int j = appTokens.size() - 1; j >= 0; --j) {
3385 final AppWindowToken token = appTokens.get(j);
3386 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3387 && (!token.mIsExiting || token.isEmpty())) {
3388 // Make sure there is no animation running on this token, so any windows
3389 // associated with it will be removed as soon as their animations are
3390 // complete.
3391 token.mAppAnimator.clearAnimation();
3392 token.mAppAnimator.animating = false;
3393 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3394 "performLayout: App token exiting now removed" + token);
3395 token.removeIfPossible();
3396 }
3397 }
3398 }
3399 }
3400
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003401 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003402 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003403 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003404 // Apps and their containers are not allowed to specify an orientation while the
3405 // docked or freeform stack is visible...except for the home stack/task if the
3406 // docked stack is minimized and it actually set something.
3407 if (mHomeStack != null && mHomeStack.isVisible()
3408 && mDividerControllerLocked.isMinimizedDock()) {
3409 final int orientation = mHomeStack.getOrientation();
3410 if (orientation != SCREEN_ORIENTATION_UNSET) {
3411 return orientation;
3412 }
3413 }
3414 return SCREEN_ORIENTATION_UNSPECIFIED;
3415 }
3416
3417 final int orientation = super.getOrientation();
3418 if (orientation != SCREEN_ORIENTATION_UNSET
3419 && orientation != SCREEN_ORIENTATION_BEHIND) {
3420 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3421 "App is requesting an orientation, return " + orientation);
3422 return orientation;
3423 }
3424
3425 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003426 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003427 // The next app has not been requested to be visible, so we keep the current orientation
3428 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003429 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003430 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003431 }
3432
3433 /**
3434 * Window container class that contains all containers on this display that are not related to
3435 * Apps. E.g. status bar.
3436 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003437 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3438 /**
3439 * Compares two child window tokens returns -1 if the first is lesser than the second in
3440 * terms of z-order and 1 otherwise.
3441 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003442 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003443 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003444 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3445 token1.mOwnerCanManageAppTokens)
3446 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3447 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003448
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003449 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3450 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3451 return false;
3452 }
3453 final int req = w.mAttrs.screenOrientation;
3454 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3455 || req == SCREEN_ORIENTATION_UNSET) {
3456 return false;
3457 }
3458 return true;
3459 };
3460
Wale Ogunwale3a931692016-11-02 16:49:48 -07003461 private final String mName;
3462 NonAppWindowContainers(String name) {
3463 mName = name;
3464 }
3465
3466 void addChild(WindowToken token) {
3467 addChild(token, mWindowComparator);
3468 }
3469
3470 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003471 int getOrientation() {
3472 final WindowManagerPolicy policy = mService.mPolicy;
3473 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003474 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003475
3476 if (win != null) {
3477 final int req = win.mAttrs.screenOrientation;
3478 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
3479 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003480 mLastKeyguardForcedOrientation = req;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003481 }
Andrii Kulian8ee72852017-03-10 10:36:45 -08003482 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003483 }
3484
Andrii Kulian8ee72852017-03-10 10:36:45 -08003485 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003486
3487 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003488 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003489 }
3490
3491 return SCREEN_ORIENTATION_UNSET;
3492 }
3493
3494 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003495 String getName() {
3496 return mName;
3497 }
Robert Carr3b716242016-08-16 16:02:21 -07003498 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003499
3500 /**
3501 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3502 */
3503 @FunctionalInterface
3504 private interface Screenshoter<E> {
3505 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3506 boolean useIdentityTransform, int rotation);
3507 }
Craig Mautner59c00972012-07-30 12:10:24 -07003508}