blob: a8e0d761afc394b297802f006225b4f0929d1f47 [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();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200523 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800524 mService.mPolicy.layoutWindowLw(w, null);
525 w.mLayoutSeq = mService.mLayoutSeq;
526
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200527 // If this is the first layout, we need to initialize the last inset values as
528 // otherwise we'd immediately cause an unnecessary resize.
529 if (firstLayout) {
530 w.updateLastInsetValues();
531 }
532
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800533 // Window frames may have changed. Update dim layer with the new bounds.
534 final Task task = w.getTask();
535 if (task != null) {
536 mDimLayerController.updateDimLayer(task);
537 }
538
539 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
540 + " mContainingFrame=" + w.mContainingFrame
541 + " mDisplayFrame=" + w.mDisplayFrame);
542 }
543 }
544 };
545
546 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
547 if (w.mLayoutAttached) {
548 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
549 + " mViewVisibility=" + w.mViewVisibility
550 + " mRelayoutCalled=" + w.mRelayoutCalled);
551 // If this view is GONE, then skip it -- keep the current frame, and let the caller
552 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
553 // windows, since that means "perform layout as normal, just don't display").
554 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
555 return;
556 }
557 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
558 || w.mLayoutNeeded) {
559 if (mTmpInitial) {
560 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
561 w.mContentChanged = false;
562 }
563 w.mLayoutNeeded = false;
564 w.prelayout();
565 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
566 w.mLayoutSeq = mService.mLayoutSeq;
567 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
568 + " mContainingFrame=" + w.mContainingFrame
569 + " mDisplayFrame=" + w.mDisplayFrame);
570 }
571 } else if (w.mAttrs.type == TYPE_DREAM) {
572 // Don't layout windows behind a dream, so that if it does stuff like hide the
573 // status bar we won't get a bad transition when it goes away.
574 mTmpWindow = mTmpWindow2;
575 }
576 };
577
578 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
579 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
580 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
581 return w.canBeImeTarget();
582 };
583
584 private final Consumer<WindowState> mApplyPostLayoutPolicy =
585 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
586 mService.mInputMethodTarget);
587
588 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
589 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
590 final boolean obscuredChanged = w.mObscured !=
591 mTmpApplySurfaceChangesTransactionState.obscured;
592 final RootWindowContainer root = mService.mRoot;
593 // Only used if default window
594 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
595
596 // Update effect.
597 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
598 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
599 final boolean isDisplayed = w.isDisplayedLw();
600
601 if (isDisplayed && w.isObscuringDisplay()) {
602 // This window completely covers everything behind it, so we want to leave all
603 // of them as undimmed (for performance reasons).
604 root.mObscuringWindow = w;
605 mTmpApplySurfaceChangesTransactionState.obscured = true;
606 }
607
608 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
609 root.handleNotObscuredLocked(w,
610 mTmpApplySurfaceChangesTransactionState.obscured,
611 mTmpApplySurfaceChangesTransactionState.syswin);
612
613 if (w.mHasSurface && isDisplayed) {
614 final int type = w.mAttrs.type;
615 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
616 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
617 mTmpApplySurfaceChangesTransactionState.syswin = true;
618 }
619 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
620 && w.mAttrs.preferredRefreshRate != 0) {
621 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
622 = w.mAttrs.preferredRefreshRate;
623 }
624 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
625 && w.mAttrs.preferredDisplayModeId != 0) {
626 mTmpApplySurfaceChangesTransactionState.preferredModeId
627 = w.mAttrs.preferredDisplayModeId;
628 }
629 }
630 }
631
632 w.applyDimLayerIfNeeded();
633
634 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
635 && mWallpaperController.isWallpaperTarget(w)) {
636 // This is the wallpaper target and its obscured state changed... make sure the
637 // current wallpaper's visibility has been updated accordingly.
638 mWallpaperController.updateWallpaperVisibility();
639 }
640
641 w.handleWindowMovedIfNeeded();
642
643 final WindowStateAnimator winAnimator = w.mWinAnimator;
644
645 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
646 w.mContentChanged = false;
647
648 // Moved from updateWindowsAndWallpaperLocked().
649 if (w.mHasSurface) {
650 // Take care of the window being ready to display.
651 final boolean committed = winAnimator.commitFinishDrawingLocked();
652 if (isDefaultDisplay && committed) {
653 if (w.mAttrs.type == TYPE_DREAM) {
654 // HACK: When a dream is shown, it may at that point hide the lock screen.
655 // So we need to redo the layout to let the phone window manager make this
656 // happen.
657 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
658 if (DEBUG_LAYOUT_REPEATS) {
659 surfacePlacer.debugLayoutRepeats(
660 "dream and commitFinishDrawingLocked true",
661 pendingLayoutChanges);
662 }
663 }
664 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
665 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
666 "First draw done in potential wallpaper target " + w);
667 root.mWallpaperMayChange = true;
668 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
669 if (DEBUG_LAYOUT_REPEATS) {
670 surfacePlacer.debugLayoutRepeats(
671 "wallpaper and commitFinishDrawingLocked true",
672 pendingLayoutChanges);
673 }
674 }
675 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700676 final TaskStack stack = w.getStack();
677 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
678 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800679 // Updates the shown frame before we set up the surface. This is needed
680 // because the resizing could change the top-left position (in addition to
681 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
682 // position the surface.
683 //
684 // If an animation is being started, we can't call this method because the
685 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700686 // we do want to update the position if the window is animating. We make an exception
687 // for the bounds animating state, where an application may have been waiting
688 // for an exit animation to start, but instead enters PiP. We need to ensure
689 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800690 winAnimator.computeShownFrameLocked();
691 }
692 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
693 }
694
695 final AppWindowToken atoken = w.mAppToken;
696 if (atoken != null) {
697 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
698 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
699 mTmpUpdateAllDrawn.add(atoken);
700 }
701 }
702
703 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
704 && w.isDisplayedLw()) {
705 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
706 }
707
708 w.updateResizingWindowIfNeeded();
709 };
710
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800711 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800712 * Create new {@link DisplayContent} instance, add itself to the root window container and
713 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700714 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800715 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700716 * @param layersController window layer controller used to assign layer to the windows on this
717 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700718 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
719 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700720 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700721 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700722 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800723
724 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
725 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
726 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
727 + " new=" + display);
728 }
729
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700730 mDisplay = display;
731 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700732 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700733 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700734 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700735 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700736 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700737 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700738 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800739 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700740 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800741 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700742
743 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700744 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700745 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700746 super.addChild(mAboveAppWindowsContainers, null);
747 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800748
749 // Add itself as a child to the root container.
750 mService.mRoot.addChild(this, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700751 }
752
753 int getDisplayId() {
754 return mDisplayId;
755 }
756
Wale Ogunwale02319a62016-09-26 15:21:22 -0700757 WindowToken getWindowToken(IBinder binder) {
758 return mTokenMap.get(binder);
759 }
760
761 AppWindowToken getAppWindowToken(IBinder binder) {
762 final WindowToken token = getWindowToken(binder);
763 if (token == null) {
764 return null;
765 }
766 return token.asAppWindowToken();
767 }
768
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700769 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700770 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
771 if (dc != null) {
772 // We currently don't support adding a window token to the display if the display
773 // already has the binder mapped to another token. If there is a use case for supporting
774 // this moving forward we will either need to merge the WindowTokens some how or have
775 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700776 throw new IllegalArgumentException("Can't map token=" + token + " to display="
777 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700778 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700779 if (binder == null) {
780 throw new IllegalArgumentException("Can't map token=" + token + " to display="
781 + getName() + " binder is null");
782 }
783 if (token == null) {
784 throw new IllegalArgumentException("Can't map null token to display="
785 + getName() + " binder=" + binder);
786 }
787
Wale Ogunwale02319a62016-09-26 15:21:22 -0700788 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700789
790 if (token.asAppWindowToken() == null) {
791 // Add non-app token to container hierarchy on the display. App tokens are added through
792 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700793 switch (token.windowType) {
794 case TYPE_WALLPAPER:
795 mBelowAppWindowsContainers.addChild(token);
796 break;
797 case TYPE_INPUT_METHOD:
798 case TYPE_INPUT_METHOD_DIALOG:
799 mImeWindowsContainers.addChild(token);
800 break;
801 default:
802 mAboveAppWindowsContainers.addChild(token);
803 break;
804 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700805 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700806 }
807
808 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700809 final WindowToken token = mTokenMap.remove(binder);
810 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800811 token.setExiting();
812 }
813 return token;
814 }
815
816 /** Changes the display the input window token is housed on to this one. */
817 void reParentWindowToken(WindowToken token) {
818 final DisplayContent prevDc = token.getDisplayContent();
819 if (prevDc == this) {
820 return;
821 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800822 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
823 && token.asAppWindowToken() == null) {
824 // Removed the token from the map, but made sure it's not an app token before removing
825 // from parent.
826 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700827 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800828
829 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700830 }
831
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700832 void removeAppToken(IBinder binder) {
833 final WindowToken token = removeWindowToken(binder);
834 if (token == null) {
835 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
836 return;
837 }
838
839 final AppWindowToken appToken = token.asAppWindowToken();
840
841 if (appToken == null) {
842 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
843 return;
844 }
845
846 appToken.onRemovedFromDisplay();
847 }
848
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700849 Display getDisplay() {
850 return mDisplay;
851 }
852
Craig Mautner59c00972012-07-30 12:10:24 -0700853 DisplayInfo getDisplayInfo() {
854 return mDisplayInfo;
855 }
856
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700857 DisplayMetrics getDisplayMetrics() {
858 return mDisplayMetrics;
859 }
860
Andrii Kulian8ee72852017-03-10 10:36:45 -0800861 int getRotation() {
862 return mRotation;
863 }
864
865 void setRotation(int newRotation) {
866 mRotation = newRotation;
867 }
868
869 int getLastOrientation() {
870 return mLastOrientation;
871 }
872
873 void setLastOrientation(int orientation) {
874 mLastOrientation = orientation;
875 }
876
877 boolean getAltOrientation() {
878 return mAltOrientation;
879 }
880
881 void setAltOrientation(boolean altOrientation) {
882 mAltOrientation = altOrientation;
883 }
884
885 int getLastWindowForcedOrientation() {
886 return mLastWindowForcedOrientation;
887 }
888
Andrii Kulian06d07d62017-03-14 11:11:47 -0700889 /**
890 * Update rotation of the display.
891 *
892 * Returns true if the rotation has been changed. In this case YOU MUST CALL
893 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
894 */
895 boolean updateRotationUnchecked(boolean inTransaction) {
896 if (mService.mDeferredRotationPauseCount > 0) {
897 // Rotation updates have been paused temporarily. Defer the update until
898 // updates have been resumed.
899 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
900 return false;
901 }
902
903 ScreenRotationAnimation screenRotationAnimation =
904 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
905 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
906 // Rotation updates cannot be performed while the previous rotation change
907 // animation is still in progress. Skip this update. We will try updating
908 // again after the animation is finished and the display is unfrozen.
909 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
910 return false;
911 }
912 if (mService.mDisplayFrozen) {
913 // Even if the screen rotation animation has finished (e.g. isAnimating
914 // returns false), there is still some time where we haven't yet unfrozen
915 // the display. We also need to abort rotation here.
916 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
917 "Deferring rotation, still finishing previous rotation");
918 return false;
919 }
920
921 if (!mService.mDisplayEnabled) {
922 // No point choosing a rotation if the display is not enabled.
923 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
924 return false;
925 }
926
927 final int oldRotation = mRotation;
928 final int lastOrientation = mLastOrientation;
929 final boolean oldAltOrientation = mAltOrientation;
930 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700931 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
932 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700933
Robert Carr897215d2017-03-29 12:25:50 -0700934 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700935 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
936 if (seamlessRotated != null) {
937 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
938 // to complete (that is, waiting for windows to redraw). It's tempting to check
939 // w.mSeamlessRotationCount but that could be incorrect in the case of
940 // window-removal.
941 return false;
942 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700943 }
944
945 // TODO: Implement forced rotation changes.
946 // Set mAltOrientation to indicate that the application is receiving
947 // an orientation that has different metrics than it expected.
948 // eg. Portrait instead of Landscape.
949
950 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
951 lastOrientation, rotation);
952
953 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
954 + ", got rotation " + rotation + " which has "
955 + (altOrientation ? "incompatible" : "compatible") + " metrics");
956
957 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
958 // No change.
959 return false;
960 }
961
962 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
963 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
964 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
965
966 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
967 mService.mWaitingForConfig = true;
968 }
969
970 mRotation = rotation;
971 mAltOrientation = altOrientation;
972 if (isDefaultDisplay) {
973 mService.mPolicy.setRotationLw(rotation);
974 }
975
976 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
977 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
978 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
979 WINDOW_FREEZE_TIMEOUT_DURATION);
980
981 setLayoutNeeded();
982 final int[] anim = new int[2];
983 if (isDimming()) {
984 anim[0] = anim[1] = 0;
985 } else {
986 mService.mPolicy.selectRotationAnimationLw(anim);
987 }
988
989 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -0700990 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700991 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
992 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
993 mDisplayId);
994 } else {
995 // The screen rotation animation uses a screenshot to freeze the screen
996 // while windows resize underneath.
997 // When we are rotating seamlessly, we allow the elements to transition
998 // to their rotated state independently and without a freeze required.
999 screenRotationAnimation = null;
1000
1001 // We have to reset this in case a window was removed before it
1002 // finished seamless rotation.
1003 mService.mSeamlessRotationCount = 0;
1004 }
1005
1006 // We need to update our screen size information to match the new rotation. If the rotation
1007 // has actually changed then this method will return true and, according to the comment at
1008 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1009 // By updating the Display info here it will be available to
1010 // #computeScreenConfiguration() later.
1011 updateDisplayAndOrientation(getConfiguration().uiMode);
1012
1013 if (!inTransaction) {
1014 if (SHOW_TRANSACTIONS) {
1015 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1016 }
1017 mService.openSurfaceTransaction();
1018 }
1019 try {
1020 // NOTE: We disable the rotation in the emulator because
1021 // it doesn't support hardware OpenGL emulation yet.
1022 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1023 && screenRotationAnimation.hasScreenshot()) {
1024 if (screenRotationAnimation.setRotationInTransaction(
1025 rotation, mService.mFxSession,
1026 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1027 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1028 mService.scheduleAnimationLocked();
1029 }
1030 }
1031
1032 if (rotateSeamlessly) {
1033 forAllWindows(w -> {
1034 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1035 }, true /* traverseTopToBottom */);
1036 }
1037
1038 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1039 } finally {
1040 if (!inTransaction) {
1041 mService.closeSurfaceTransaction();
1042 if (SHOW_LIGHT_TRANSACTIONS) {
1043 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1044 }
1045 }
1046 }
1047
1048 forAllWindows(w -> {
1049 // Discard surface after orientation change, these can't be reused.
1050 if (w.mAppToken != null) {
1051 w.mAppToken.destroySavedSurfaces();
1052 }
1053 if (w.mHasSurface && !rotateSeamlessly) {
1054 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
1055 w.mOrientationChanging = true;
1056 mService.mRoot.mOrientationChangeComplete = false;
1057 w.mLastFreezeDuration = 0;
1058 }
1059 w.mReportOrientationChanged = true;
1060 }, true /* traverseTopToBottom */);
1061
1062 if (rotateSeamlessly) {
1063 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1064 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1065 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1066 }
1067
1068 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1069 final WindowManagerService.RotationWatcher rotationWatcher
1070 = mService.mRotationWatchers.get(i);
1071 if (rotationWatcher.mDisplayId == mDisplayId) {
1072 try {
1073 rotationWatcher.mWatcher.onRotationChanged(rotation);
1074 } catch (RemoteException e) {
1075 // Ignore
1076 }
1077 }
1078 }
1079
1080 // TODO (multi-display): Magnification is supported only for the default display.
1081 // Announce rotation only if we will not animate as we already have the
1082 // windows in final state. Otherwise, we make this call at the rotation end.
1083 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1084 && isDefaultDisplay) {
1085 mService.mAccessibilityController.onRotationChangedLocked(this);
1086 }
1087
1088 return true;
1089 }
1090
1091 /**
1092 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1093 * changed.
1094 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1095 */
1096 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1097 // Use the effective "visual" dimensions based on current rotation
1098 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1099 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1100 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1101 int dw = realdw;
1102 int dh = realdh;
1103
1104 if (mAltOrientation) {
1105 if (realdw > realdh) {
1106 // Turn landscape into portrait.
1107 int maxw = (int)(realdh/1.3f);
1108 if (maxw < realdw) {
1109 dw = maxw;
1110 }
1111 } else {
1112 // Turn portrait into landscape.
1113 int maxh = (int)(realdw/1.3f);
1114 if (maxh < realdh) {
1115 dh = maxh;
1116 }
1117 }
1118 }
1119
1120 // Update application display metrics.
1121 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1122 mDisplayId);
1123 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1124 mDisplayId);
1125 mDisplayInfo.rotation = mRotation;
1126 mDisplayInfo.logicalWidth = dw;
1127 mDisplayInfo.logicalHeight = dh;
1128 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1129 mDisplayInfo.appWidth = appWidth;
1130 mDisplayInfo.appHeight = appHeight;
1131 if (isDefaultDisplay) {
1132 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1133 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1134 }
1135 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1136 if (mDisplayScalingDisabled) {
1137 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1138 } else {
1139 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1140 }
1141
1142 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1143 mDisplayInfo);
1144
1145 mBaseDisplayRect.set(0, 0, dw, dh);
1146
1147 if (isDefaultDisplay) {
1148 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1149 mCompatDisplayMetrics);
1150 }
1151 return mDisplayInfo;
1152 }
1153
1154 /**
1155 * Compute display configuration based on display properties and policy settings.
1156 * Do not call if mDisplayReady == false.
1157 */
1158 void computeScreenConfiguration(Configuration config) {
1159 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1160
1161 final int dw = displayInfo.logicalWidth;
1162 final int dh = displayInfo.logicalHeight;
1163 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1164 Configuration.ORIENTATION_LANDSCAPE;
Bryce Leeafb45bb2017-04-27 13:42:23 -07001165 config.setRotation(displayInfo.rotation);
1166
Andrii Kulian06d07d62017-03-14 11:11:47 -07001167 config.screenWidthDp =
1168 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1169 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1170 config.screenHeightDp =
1171 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1172 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001173
1174 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1175 final int leftInset = mTmpRect.left;
1176 final int topInset = mTmpRect.top;
1177 // appBounds at the root level should mirror the app screen size.
1178 config.setAppBounds(leftInset /*left*/, topInset /*top*/, leftInset + displayInfo.appWidth /*right*/,
1179 topInset + displayInfo.appHeight /*bottom*/);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001180 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1181 || displayInfo.rotation == Surface.ROTATION_270);
1182
1183 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1184 mDisplayMetrics.density, config);
1185
1186 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1187 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1188 ? Configuration.SCREENLAYOUT_ROUND_YES
1189 : Configuration.SCREENLAYOUT_ROUND_NO);
1190
1191 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1192 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1193 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1194 dh, mDisplayId);
1195 config.densityDpi = displayInfo.logicalDensityDpi;
1196
1197 config.colorMode =
1198 (displayInfo.isHdr()
1199 ? Configuration.COLOR_MODE_HDR_YES
1200 : Configuration.COLOR_MODE_HDR_NO)
1201 | (displayInfo.isWideColorGamut()
1202 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1203 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1204
1205 // Update the configuration based on available input devices, lid switch,
1206 // and platform configuration.
1207 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1208 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1209 config.navigation = Configuration.NAVIGATION_NONAV;
1210
1211 int keyboardPresence = 0;
1212 int navigationPresence = 0;
1213 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1214 final int len = devices != null ? devices.length : 0;
1215 for (int i = 0; i < len; i++) {
1216 InputDevice device = devices[i];
1217 if (!device.isVirtual()) {
1218 final int sources = device.getSources();
1219 final int presenceFlag = device.isExternal() ?
1220 WindowManagerPolicy.PRESENCE_EXTERNAL :
1221 WindowManagerPolicy.PRESENCE_INTERNAL;
1222
1223 // TODO(multi-display): Configure on per-display basis.
1224 if (mService.mIsTouchDevice) {
1225 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1226 InputDevice.SOURCE_TOUCHSCREEN) {
1227 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1228 }
1229 } else {
1230 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1231 }
1232
1233 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1234 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1235 navigationPresence |= presenceFlag;
1236 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1237 && config.navigation == Configuration.NAVIGATION_NONAV) {
1238 config.navigation = Configuration.NAVIGATION_DPAD;
1239 navigationPresence |= presenceFlag;
1240 }
1241
1242 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1243 config.keyboard = Configuration.KEYBOARD_QWERTY;
1244 keyboardPresence |= presenceFlag;
1245 }
1246 }
1247 }
1248
1249 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1250 config.navigation = Configuration.NAVIGATION_DPAD;
1251 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1252 }
1253
1254 // Determine whether a hard keyboard is available and enabled.
1255 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1256 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1257 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1258 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1259 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1260 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1261 }
1262
1263 // Let the policy update hidden states.
1264 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1265 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1266 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1267 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1268 }
1269
1270 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1271 int displayId) {
1272 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1273 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1274 final int unrotDw, unrotDh;
1275 if (rotated) {
1276 unrotDw = dh;
1277 unrotDh = dw;
1278 } else {
1279 unrotDw = dw;
1280 unrotDh = dh;
1281 }
1282 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1283 displayId);
1284 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1285 displayId);
1286 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1287 displayId);
1288 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1289 displayId);
1290 return sw;
1291 }
1292
1293 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1294 DisplayMetrics dm, int dw, int dh, int displayId) {
1295 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1296 displayId);
1297 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1298 uiMode, displayId);
1299 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1300 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1301 if (curSize == 0 || size < curSize) {
1302 curSize = size;
1303 }
1304 return curSize;
1305 }
1306
1307 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1308 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1309
1310 // We need to determine the smallest width that will occur under normal
1311 // operation. To this, start with the base screen size and compute the
1312 // width under the different possible rotations. We need to un-rotate
1313 // the current screen dimensions before doing this.
1314 int unrotDw, unrotDh;
1315 if (rotated) {
1316 unrotDw = dh;
1317 unrotDh = dw;
1318 } else {
1319 unrotDw = dw;
1320 unrotDh = dh;
1321 }
1322 displayInfo.smallestNominalAppWidth = 1<<30;
1323 displayInfo.smallestNominalAppHeight = 1<<30;
1324 displayInfo.largestNominalAppWidth = 0;
1325 displayInfo.largestNominalAppHeight = 0;
1326 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1327 unrotDh);
1328 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1329 unrotDw);
1330 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1331 unrotDh);
1332 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1333 unrotDw);
1334 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1335 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1336 displayId);
1337 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1338 displayId);
1339 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1340 displayId);
1341 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1342 displayId);
1343 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1344 outConfig.screenLayout = sl;
1345 }
1346
1347 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1348 int uiMode, int displayId) {
1349 // Get the app screen size at this rotation.
1350 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1351 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1352
1353 // Compute the screen layout size class for this rotation.
1354 int longSize = w;
1355 int shortSize = h;
1356 if (longSize < shortSize) {
1357 int tmp = longSize;
1358 longSize = shortSize;
1359 shortSize = tmp;
1360 }
1361 longSize = (int)(longSize/density);
1362 shortSize = (int)(shortSize/density);
1363 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1364 }
1365
1366 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1367 int uiMode, int dw, int dh) {
1368 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1369 displayId);
1370 if (width < displayInfo.smallestNominalAppWidth) {
1371 displayInfo.smallestNominalAppWidth = width;
1372 }
1373 if (width > displayInfo.largestNominalAppWidth) {
1374 displayInfo.largestNominalAppWidth = width;
1375 }
1376 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1377 displayId);
1378 if (height < displayInfo.smallestNominalAppHeight) {
1379 displayInfo.smallestNominalAppHeight = height;
1380 }
1381 if (height > displayInfo.largestNominalAppHeight) {
1382 displayInfo.largestNominalAppHeight = height;
1383 }
1384 }
1385
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001386 DockedStackDividerController getDockedDividerController() {
1387 return mDividerControllerLocked;
1388 }
1389
Winson Chung655332c2016-10-31 13:14:28 -07001390 PinnedStackController getPinnedStackController() {
1391 return mPinnedStackControllerLocked;
1392 }
1393
Jeff Browna506a6e2013-06-04 00:02:38 -07001394 /**
1395 * Returns true if the specified UID has access to this display.
1396 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001397 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001398 return mDisplay.hasAccess(uid);
1399 }
1400
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001401 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001402 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001403 }
1404
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001405 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001406 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001407 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001408 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001409 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001410 }
1411
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001412 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001413 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1414 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001415 if (stack.mStackId == stackId) {
1416 return stack;
1417 }
1418 }
1419 return null;
1420 }
1421
Bryce Lee48f4b572017-04-10 10:54:15 -07001422 @VisibleForTesting
1423 int getStackCount() {
1424 return mTaskStackContainers.size();
1425 }
1426
1427 @VisibleForTesting
1428 int getStaskPosById(int stackId) {
1429 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1430 final TaskStack stack = mTaskStackContainers.get(i);
1431 if (stack.mStackId == stackId) {
1432 return i;
1433 }
1434 }
1435 return -1;
1436 }
1437
Andrii Kulian441e4492016-09-29 15:25:00 -07001438 @Override
1439 void onConfigurationChanged(Configuration newParentConfig) {
1440 super.onConfigurationChanged(newParentConfig);
1441
Andrii Kulian3a507b52016-09-19 18:14:12 -07001442 // The display size information is heavily dependent on the resources in the current
1443 // configuration, so we need to reconfigure it every time the configuration changes.
1444 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1445 mService.reconfigureDisplayLocked(this);
1446
1447 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001448 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001449 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001450
Andrii Kulian441e4492016-09-29 15:25:00 -07001451 /**
1452 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1453 * bounds were updated.
1454 */
1455 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001456 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1457 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001458 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001459 changedStackList.add(stack.mStackId);
1460 }
1461 }
Winson Chung32c566f2017-04-11 18:31:21 -07001462
1463 // If there was no pinned stack, we still need to notify the controller of the display info
1464 // update as a result of the config change. We do this here to consolidate the flow between
1465 // changes when there is and is not a stack.
1466 if (getStackById(PINNED_STACK_ID) == null) {
1467 mPinnedStackControllerLocked.onDisplayInfoChanged();
1468 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001469 }
1470
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001471 @Override
1472 boolean fillsParent() {
1473 return true;
1474 }
1475
1476 @Override
1477 boolean isVisible() {
1478 return true;
1479 }
1480
1481 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001482 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001483 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001484 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001485 }
1486
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001487 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001488 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1489 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1490 // target, or here in order if there isn't an IME target.
1491 if (traverseTopToBottom) {
1492 for (int i = mChildren.size() - 1; i >= 0; --i) {
1493 final DisplayChildWindowContainer child = mChildren.get(i);
1494 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1495 // In this case the Ime windows will be processed above their target so we skip
1496 // here.
1497 continue;
1498 }
1499 if (child.forAllWindows(callback, traverseTopToBottom)) {
1500 return true;
1501 }
1502 }
1503 } else {
1504 final int count = mChildren.size();
1505 for (int i = 0; i < count; i++) {
1506 final DisplayChildWindowContainer child = mChildren.get(i);
1507 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1508 // In this case the Ime windows will be processed above their target so we skip
1509 // here.
1510 continue;
1511 }
1512 if (child.forAllWindows(callback, traverseTopToBottom)) {
1513 return true;
1514 }
1515 }
1516 }
1517 return false;
1518 }
1519
1520 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1521 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1522 }
1523
Wale Ogunwale399c8692017-05-08 14:22:42 -07001524 @Override
1525 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001526 final WindowManagerPolicy policy = mService.mPolicy;
1527
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001528 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001529 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001530 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001531 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001532 // If the display is frozen, some activities may be in the middle of restarting, and
1533 // thus have removed their old window. If the window has the flag to hide the lock
1534 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1535 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001536 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001537 } else if (policy.isKeyguardLocked()) {
1538 // Use the last orientation the while the display is frozen with the keyguard
1539 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1540 // window. We don't want to check the show when locked window directly though as
1541 // things aren't stable while the display is frozen, for example the window could be
1542 // momentarily unavailable due to activity relaunch.
1543 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001544 + "return " + mLastOrientation);
1545 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001546 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001547 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001548 final int orientation = mAboveAppWindowsContainers.getOrientation();
1549 if (orientation != SCREEN_ORIENTATION_UNSET) {
1550 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001551 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001552 }
1553
Wale Ogunwale399c8692017-05-08 14:22:42 -07001554 // Top system windows are not requesting an orientation. Start searching from apps.
1555 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001556 }
1557
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001558 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001559 // Check if display metrics changed and update base values if needed.
1560 updateBaseDisplayMetricsIfNeeded();
1561
Craig Mautner722285e2012-09-07 13:55:58 -07001562 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001563 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001564
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001565 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1566 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001567 }
Craig Mautner722285e2012-09-07 13:55:58 -07001568 }
1569
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001570 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001571 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1572 if (displayManagerInternal != null) {
1573 // Bootstrap the default logical display from the display manager.
1574 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1575 if (newDisplayInfo != null) {
1576 mDisplayInfo.copyFrom(newDisplayInfo);
1577 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001578 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001579
Andrii Kuliancd097992017-03-23 18:31:59 -07001580 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1581 mDisplayInfo.logicalDensityDpi);
1582 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1583 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1584 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001585 }
1586
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001587 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001588 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001589 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001590 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001591 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1592 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001593 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001594 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001595 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001596 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001597 out.set(left, top, left + width, top + height);
1598 }
1599
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001600 private void getLogicalDisplayRect(Rect out, int orientation) {
1601 getLogicalDisplayRect(out);
1602
1603 // Rotate the Rect if needed.
1604 final int currentRotation = mDisplayInfo.rotation;
1605 final int rotationDelta = deltaRotation(currentRotation, orientation);
1606 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1607 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1608 mTmpRectF.set(out);
1609 mTmpMatrix.mapRect(mTmpRectF);
1610 mTmpRectF.round(out);
1611 }
1612 }
1613
Andrii Kuliancd097992017-03-23 18:31:59 -07001614 /**
1615 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1616 * values.
1617 */
1618 private void updateBaseDisplayMetricsIfNeeded() {
1619 // Get real display metrics without overrides from WM.
1620 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1621 final int orientation = mDisplayInfo.rotation;
1622 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1623 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1624 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1625 final int newDensity = mDisplayInfo.logicalDensityDpi;
1626
1627 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1628 || mInitialDisplayHeight != newHeight
1629 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1630
1631 if (displayMetricsChanged) {
1632 // Check if display size or density is forced.
1633 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1634 || mBaseDisplayHeight != mInitialDisplayHeight;
1635 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1636
1637 // If there is an override set for base values - use it, otherwise use new values.
1638 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1639 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1640 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1641
1642 // Real display metrics changed, so we should also update initial values.
1643 mInitialDisplayWidth = newWidth;
1644 mInitialDisplayHeight = newHeight;
1645 mInitialDisplayDensity = newDensity;
1646 mService.reconfigureDisplayLocked(this);
1647 }
1648 }
1649
Bryce Lee27cec322017-03-21 09:41:37 -07001650 /** Sets the maximum width the screen resolution can be */
1651 void setMaxUiWidth(int width) {
1652 if (DEBUG_DISPLAY) {
1653 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1654 }
1655
1656 mMaxUiWidth = width;
1657
1658 // Update existing metrics.
1659 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1660 }
1661
1662 /** Update base (override) display metrics. */
1663 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1664 mBaseDisplayWidth = baseWidth;
1665 mBaseDisplayHeight = baseHeight;
1666 mBaseDisplayDensity = baseDensity;
1667
1668 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1669 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1670 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1671 mBaseDisplayWidth = mMaxUiWidth;
1672
1673 if (DEBUG_DISPLAY) {
1674 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1675 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1676 + " on display:" + getDisplayId());
1677 }
1678 }
1679
1680 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1681 }
1682
Chong Zhangf66db432016-01-13 10:39:51 -08001683 void getContentRect(Rect out) {
1684 out.set(mContentRect);
1685 }
1686
Wale Ogunwale1666e312016-12-16 11:27:18 -08001687 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1688 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1689 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001690
Wale Ogunwale1666e312016-12-16 11:27:18 -08001691 TaskStack stack = getStackById(stackId);
1692 if (stack != null) {
1693 // It's already attached to the display...clear mDeferRemoval and move stack to
1694 // appropriate z-order on display as needed.
1695 stack.mDeferRemoval = false;
1696 // We're not moving the display to front when we're adding stacks, only when
1697 // requested to change the position of stack explicitly.
1698 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1699 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001700 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001701 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001702 mTaskStackContainers.addStackToDisplay(stack, onTop);
1703 }
1704
Wale Ogunwale1666e312016-12-16 11:27:18 -08001705 if (stackId == DOCKED_STACK_ID) {
1706 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001707 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001708 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001709 }
1710
Andrii Kulian51c1b672017-04-07 18:39:32 -07001711 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001712 final DisplayContent prevDc = stack.getDisplayContent();
1713 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001714 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1715 + " which is not currently attached to any display");
1716 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001717 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001718 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1719 + " to its current displayId=" + mDisplayId);
1720 }
1721
Wale Ogunwale1666e312016-12-16 11:27:18 -08001722 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001723 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001724 }
1725
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001726 @Override
1727 protected void addChild(DisplayChildWindowContainer child,
1728 Comparator<DisplayChildWindowContainer> comparator) {
1729 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1730 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001731
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001732 @Override
1733 protected void addChild(DisplayChildWindowContainer child, int index) {
1734 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1735 }
1736
1737 @Override
1738 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001739 // Only allow removal of direct children from this display if the display is in the process
1740 // of been removed.
1741 if (mRemovingDisplay) {
1742 super.removeChild(child);
1743 return;
1744 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001745 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001746 }
1747
Andrii Kuliand2765632016-12-12 22:26:34 -08001748 @Override
1749 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1750 // Children of the display are statically ordered, so the real intention here is to perform
1751 // the operation on the display and not the static direct children.
1752 getParent().positionChildAt(position, this, includingParents);
1753 }
1754
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001755 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001756 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1757 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001758 final int taskId = stack.taskIdFromPoint(x, y);
1759 if (taskId != -1) {
1760 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001761 }
1762 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001763 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001764 }
1765
Chong Zhang8e89b312015-09-09 15:09:30 -07001766 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001767 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001768 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001769 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001770 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001771 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001772 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001773 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1774 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001775 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001776 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001777 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001778
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001779 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1780 if (mTmpTaskForResizePointSearchResult.searchDone) {
1781 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001782 }
1783 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001784 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001785 }
1786
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001787 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001788 // The provided task is the task on this display with focus, so if WindowManagerService's
1789 // focused app is not on this display, focusedTask will be null.
1790 if (focusedTask == null) {
1791 mTouchExcludeRegion.setEmpty();
1792 } else {
1793 mTouchExcludeRegion.set(mBaseDisplayRect);
1794 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1795 mTmpRect2.setEmpty();
1796 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1797 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1798 stack.setTouchExcludeRegion(
1799 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1800 }
1801 // If we removed the focused task above, add it back and only leave its
1802 // outside touch area in the exclusion. TapDectector is not interested in
1803 // any touch inside the focused task itself.
1804 if (!mTmpRect2.isEmpty()) {
1805 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1806 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001807 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001808 final WindowState inputMethod = mService.mInputMethodWindow;
1809 if (inputMethod != null && inputMethod.isVisibleLw()) {
1810 // If the input method is visible and the user is typing, we don't want these touch
1811 // events to be intercepted and used to change focus. This would likely cause a
1812 // disappearance of the input method.
1813 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001814 if (inputMethod.getDisplayId() == mDisplayId) {
1815 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1816 } else {
1817 // IME is on a different display, so we need to update its tap detector.
1818 // TODO(multidisplay): Remove when IME will always appear on same display.
1819 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1820 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001821 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001822 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1823 WindowState win = mTapExcludedWindows.get(i);
1824 win.getTouchableRegion(mTmpRegion);
1825 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1826 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001827 // TODO(multi-display): Support docked stacks on secondary displays.
1828 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001829 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001830 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001831 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1832 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001833 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001834 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001835 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001836 }
1837
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001838 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001839 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001840 super.switchUser();
1841 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001842 }
1843
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001844 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001845 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1846 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001847 }
1848 }
1849
1850 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001851 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001852 }
1853
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001854 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001855 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001856 }
1857
1858 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001859 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001860 }
1861
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001862 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001863 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001864 }
1865
Wale Ogunwale10124582016-09-15 20:25:50 -07001866 @Override
1867 void removeIfPossible() {
1868 if (isAnimating()) {
1869 mDeferredRemoval = true;
1870 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001871 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001872 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001873 }
1874
Wale Ogunwale10124582016-09-15 20:25:50 -07001875 @Override
1876 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001877 mRemovingDisplay = true;
1878 try {
1879 super.removeImmediately();
1880 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1881 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001882 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001883 mService.unregisterPointerEventListener(mTapDetector);
1884 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1885 }
1886 } finally {
1887 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001888 }
Craig Mautner95da1082014-02-24 17:54:35 -08001889 }
1890
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001891 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001892 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001893 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001894 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1895
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001896 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001897 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001898 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001899 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001900 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001901 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001902 }
1903
Andrii Kulian0214ed92017-05-16 13:44:05 -07001904 /** @return 'true' if removal of this display content is deferred due to active animation. */
1905 boolean isRemovalDeferred() {
1906 return mDeferredRemoval;
1907 }
1908
Wale Ogunwale10124582016-09-15 20:25:50 -07001909 boolean animateForIme(float interpolatedValue, float animationTarget,
1910 float dividerAnimationTarget) {
1911 boolean updated = false;
1912
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001913 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1914 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001915 if (stack == null || !stack.isAdjustedForIme()) {
1916 continue;
1917 }
1918
1919 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1920 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1921 updated = true;
1922 } else {
1923 mDividerControllerLocked.mLastAnimationProgress =
1924 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1925 mDividerControllerLocked.mLastDividerProgress =
1926 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1927 updated |= stack.updateAdjustForIme(
1928 mDividerControllerLocked.mLastAnimationProgress,
1929 mDividerControllerLocked.mLastDividerProgress,
1930 false /* force */);
1931 }
1932 if (interpolatedValue >= 1f) {
1933 stack.endImeAdjustAnimation();
1934 }
1935 }
1936
1937 return updated;
1938 }
1939
1940 boolean clearImeAdjustAnimation() {
1941 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001942 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1943 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001944 if (stack != null && stack.isAdjustedForIme()) {
1945 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1946 changed = true;
1947 }
1948 }
1949 return changed;
1950 }
1951
1952 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001953 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1954 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001955 if (stack.isVisible() && stack.isAdjustedForIme()) {
1956 stack.beginImeAdjustAnimation();
1957 }
1958 }
1959 }
1960
1961 void adjustForImeIfNeeded() {
1962 final WindowState imeWin = mService.mInputMethodWindow;
1963 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1964 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001965 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001966 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1967 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1968 imeTargetStack.getDockSide() : DOCKED_INVALID;
1969 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1970 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1971 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1972 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1973 final boolean imeHeightChanged = imeVisible &&
1974 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1975
1976 // The divider could be adjusted for IME position, or be thinner than usual,
1977 // or both. There are three possible cases:
1978 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1979 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1980 // - If IME is not visible, divider is not moved and is normal width.
1981
1982 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001983 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1984 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001985 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07001986 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
1987 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001988 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1989 } else {
1990 stack.resetAdjustedForIme(false);
1991 }
1992 }
1993 mDividerControllerLocked.setAdjustedForIme(
1994 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1995 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001996 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1997 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001998 stack.resetAdjustedForIme(!dockVisible);
1999 }
2000 mDividerControllerLocked.setAdjustedForIme(
2001 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2002 }
Winson Chung655332c2016-10-31 13:14:28 -07002003 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002004 }
2005
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002006 void setInputMethodAnimLayerAdjustment(int adj) {
2007 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2008 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002009 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002010 }
2011
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002012 /**
2013 * If a window that has an animation specifying a colored background and the current wallpaper
2014 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2015 * suddenly disappear.
2016 */
2017 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002018 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2019 w -> w.mIsWallpaper && w.isVisibleNow());
2020
2021 if (visibleWallpaper != null) {
2022 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002023 }
2024 return winAnimator.mAnimLayer;
2025 }
2026
Wale Ogunwale10124582016-09-15 20:25:50 -07002027 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002028 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2029 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002030 stack.prepareFreezingTaskBounds();
2031 }
2032 }
2033
Wale Ogunwale94744212015-09-21 19:01:47 -07002034 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002035 getLogicalDisplayRect(mTmpRect, newRotation);
2036
2037 // Compute a transform matrix to undo the coordinate space transformation,
2038 // and present the window at the same physical position it previously occupied.
2039 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2040 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2041
2042 mTmpRectF.set(bounds);
2043 mTmpMatrix.mapRect(mTmpRectF);
2044 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002045 }
2046
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002047 static int deltaRotation(int oldRotation, int newRotation) {
2048 int delta = newRotation - oldRotation;
2049 if (delta < 0) delta += 4;
2050 return delta;
2051 }
2052
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002053 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002054 Matrix outMatrix) {
2055 // For rotations without Z-ordering we don't need the target rectangle's position.
2056 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2057 displayHeight, outMatrix);
2058 }
2059
2060 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2061 float displayWidth, float displayHeight, Matrix outMatrix) {
2062 switch (rotation) {
2063 case ROTATION_0:
2064 outMatrix.reset();
2065 break;
2066 case ROTATION_270:
2067 outMatrix.setRotate(270, 0, 0);
2068 outMatrix.postTranslate(0, displayHeight);
2069 outMatrix.postTranslate(rectTop, 0);
2070 break;
2071 case ROTATION_180:
2072 outMatrix.reset();
2073 break;
2074 case ROTATION_90:
2075 outMatrix.setRotate(90, 0, 0);
2076 outMatrix.postTranslate(displayWidth, 0);
2077 outMatrix.postTranslate(-rectTop, rectLeft);
2078 break;
2079 }
2080 }
2081
Craig Mautnera91f9e22012-09-14 16:22:08 -07002082 public void dump(String prefix, PrintWriter pw) {
2083 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2084 final String subPrefix = " " + prefix;
2085 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2086 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2087 pw.print("dpi");
2088 if (mInitialDisplayWidth != mBaseDisplayWidth
2089 || mInitialDisplayHeight != mBaseDisplayHeight
2090 || mInitialDisplayDensity != mBaseDisplayDensity) {
2091 pw.print(" base=");
2092 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2093 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2094 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002095 if (mDisplayScalingDisabled) {
2096 pw.println(" noscale");
2097 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002098 pw.print(" cur=");
2099 pw.print(mDisplayInfo.logicalWidth);
2100 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2101 pw.print(" app=");
2102 pw.print(mDisplayInfo.appWidth);
2103 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2104 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2105 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2106 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2107 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002108 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2109 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002110
Craig Mautnerdc548482014-02-05 13:35:24 -08002111 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002112 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002113 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2114 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002115 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002116 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002117
Craig Mautnerdc548482014-02-05 13:35:24 -08002118 pw.println();
2119 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002120 pw.println();
2121 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002122 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002123 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002124 pw.print(" Exiting #"); pw.print(i);
2125 pw.print(' '); pw.print(token);
2126 pw.println(':');
2127 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002128 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002129 }
Craig Mautner59c00972012-07-30 12:10:24 -07002130 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002131 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002132 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002133 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002134 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002135 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002136
2137 if (mInputMethodAnimLayerAdjustment != 0) {
2138 pw.println(subPrefix
2139 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2140 }
Craig Mautner59c00972012-07-30 12:10:24 -07002141 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002142
2143 @Override
2144 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002145 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002146 }
2147
2148 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002149 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002150 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002151
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002152 /** Checks if stack with provided id is visible on this display. */
2153 boolean isStackVisible(int stackId) {
2154 final TaskStack stack = getStackById(stackId);
2155 return (stack != null && stack.isVisible());
2156 }
2157
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002158 /**
2159 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2160 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002161 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002162 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002163 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002164 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002165
2166 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002167 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002168 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002169 */
Jorim Jaggife762342016-10-13 14:33:27 +02002170 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002171 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002172 }
2173
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002174 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002175 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002176 final int x = (int) xf;
2177 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002178 final WindowState touchedWin = getWindow(w -> {
2179 final int flags = w.mAttrs.flags;
2180 if (!w.isVisibleLw()) {
2181 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002182 }
2183 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002184 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002185 }
2186
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002187 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002188 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002189 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002190 }
2191
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002192 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002193
2194 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002195 return mTmpRegion.contains(x, y) || touchFlags == 0;
2196 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002197
2198 return touchedWin;
2199 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002200
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002201 boolean canAddToastWindowForUid(int uid) {
2202 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002203 // Also if the app is focused adding more than one toast at
2204 // a time for better backwards compatibility.
2205 final WindowState focusedWindowForUid = getWindow(w ->
2206 w.mOwnerUid == uid && w.isFocused());
2207 if (focusedWindowForUid != null) {
2208 return true;
2209 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002210 final WindowState win = getWindow(w ->
2211 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2212 && !w.mWindowRemovalAllowed);
2213 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002214 }
2215
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002216 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002217 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2218 return;
2219 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002220
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002221 // Used to communicate the old focus to the callback method.
2222 mTmpWindow = oldFocus;
2223
2224 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002225 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002226
2227 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002228 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002229
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002230 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002231
2232 if (mTmpWindow == null) {
2233 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2234 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002235 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002236 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002237 }
2238
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002239 /** Updates the layer assignment of windows on this display. */
2240 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002241 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002242 if (setLayoutNeeded) {
2243 setLayoutNeeded();
2244 }
2245 }
2246
Wale Ogunwale1666e312016-12-16 11:27:18 -08002247 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2248 // moving containers or resizing them. Need to investigate the best way to have it automatically
2249 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002250 void layoutAndAssignWindowLayersIfNeeded() {
2251 mService.mWindowsChanged = true;
2252 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002253
2254 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2255 false /*updateInputWindows*/)) {
2256 assignWindowLayers(false /* setLayoutNeeded */);
2257 }
2258
2259 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2260 mService.mWindowPlacerLocked.performSurfacePlacement();
2261 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2262 }
2263
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002264 /** Returns true if a leaked surface was destroyed */
2265 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002266 // Used to indicate that a surface was leaked.
2267 mTmpWindow = null;
2268 forAllWindows(w -> {
2269 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002270 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002271 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002272 }
2273 if (!mService.mSessions.contains(wsa.mSession)) {
2274 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002275 + w + " surface=" + wsa.mSurfaceController
2276 + " token=" + w.mToken
2277 + " pid=" + w.mSession.mPid
2278 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002279 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002280 mService.mForceRemoves.add(w);
2281 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002282 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002283 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002284 + w + " surface=" + wsa.mSurfaceController
2285 + " token=" + w.mAppToken
2286 + " saved=" + w.hasSavedSurface());
2287 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002288 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002289 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002290 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002291 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002292
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002293 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002294 }
2295
2296 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002297 * Determine and return the window that should be the IME target.
2298 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2299 * @return The window that should be used as the IME target or null if there isn't any.
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002300 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002301 WindowState computeImeTarget(boolean updateImeTarget) {
2302 if (mService.mInputMethodWindow == null) {
2303 // There isn't an IME so there shouldn't be a target...That was easy!
2304 if (updateImeTarget) {
2305 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2306 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2307 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2308 }
2309 return null;
2310 }
2311
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002312 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2313 // same display. Or even when the current IME/target are not on the same screen as the next
2314 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002315 mUpdateImeTarget = updateImeTarget;
2316 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002317
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002318
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002319 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2320 // to be on top of it, but it is not -really- where input will go. So look down below
2321 // for a real window to target...
2322 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2323 final AppWindowToken token = target.mAppToken;
2324 if (token != null) {
2325 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2326 if (betterTarget != null) {
2327 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002328 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002329 }
2330 }
2331
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2333 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002334
2335 // Now, a special case -- if the last target's window is in the process of exiting, and is
2336 // above the new target, keep on the last target to avoid flicker. Consider for example a
2337 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2338 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2339 // scrim.
2340 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002341 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2342 && (target == null
2343 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002344 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002345 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002346 }
2347
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002348 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2349 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002350
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002351 if (target == null) {
2352 if (updateImeTarget) {
2353 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2354 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2355 + Debug.getCallers(4) : ""));
2356 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2357 }
2358
2359 return null;
2360 }
2361
2362 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002363 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2364 if (token != null) {
2365
2366 // Now some fun for dealing with window animations that modify the Z order. We need
2367 // to look at all windows below the current target that are in this app, finding the
2368 // highest visible one in layering.
2369 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002370 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002371 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002372 }
2373
2374 if (highestTarget != null) {
2375 final AppTransition appTransition = mService.mAppTransition;
2376 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2377 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2378 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002379 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002380
2381 if (appTransition.isTransitionSet()) {
2382 // If we are currently setting up for an animation, hold everything until we
2383 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002384 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2385 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002386 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002387 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002388 // If the window we are currently targeting is involved with an animation,
2389 // and it is on top of the next target we will be over, then hold off on
2390 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002391 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2392 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002393 }
2394 }
2395 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002396
2397 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2398 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2399 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002400 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002401 }
2402
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002403 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002404 }
2405
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002406 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2407 if (target == mService.mInputMethodTarget
2408 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2409 && mInputMethodAnimLayerAdjustment == layerAdj) {
2410 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002411 }
2412
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002413 mService.mInputMethodTarget = target;
2414 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2415 setInputMethodAnimLayerAdjustment(layerAdj);
2416 assignWindowLayers(false /* setLayoutNeeded */);
2417 }
2418
2419 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2420 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2421 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2422 }
2423
2424 // Used to indicate we have reached the first window in the range we are interested in.
2425 mTmpWindow = null;
2426
2427 // TODO: Figure-out a more efficient way to do this.
2428 final WindowState candidate = getWindow(w -> {
2429 if (w == top) {
2430 // Reached the first window in the range we are interested in.
2431 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002432 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002433 if (mTmpWindow == null) {
2434 return false;
2435 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002436
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002437 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2438 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002439 }
2440 // If we reached the bottom of the range of windows we are considering,
2441 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002442 if (w == bottom) {
2443 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002444 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002445 return false;
2446 });
2447
2448 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002449 }
2450
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002451 void setLayoutNeeded() {
2452 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2453 mLayoutNeeded = true;
2454 }
2455
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002456 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002457 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2458 mLayoutNeeded = false;
2459 }
2460
2461 boolean isLayoutNeeded() {
2462 return mLayoutNeeded;
2463 }
2464
Wale Ogunwale02319a62016-09-26 15:21:22 -07002465 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2466 if (mTokenMap.isEmpty()) {
2467 return;
2468 }
2469 pw.println(" Display #" + mDisplayId);
2470 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2471 while (it.hasNext()) {
2472 final WindowToken token = it.next();
2473 pw.print(" ");
2474 pw.print(token);
2475 if (dumpAll) {
2476 pw.println(':');
2477 token.dump(pw, " ");
2478 } else {
2479 pw.println();
2480 }
2481 }
2482 }
2483
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002484 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002485 final int[] index = new int[1];
2486 forAllWindows(w -> {
2487 final WindowStateAnimator wAnim = w.mWinAnimator;
2488 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2489 index[0] = index[0] + 1;
2490 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002491 }
2492
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002493 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002494 forAllWindows(w -> {
2495 w.mWinAnimator.enableSurfaceTrace(fd);
2496 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002497 }
2498
2499 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002500 forAllWindows(w -> {
2501 w.mWinAnimator.disableSurfaceTrace();
2502 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002503 }
2504
Jorim Jaggife762342016-10-13 14:33:27 +02002505 /**
2506 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2507 */
2508 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2509 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002510 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002511 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2512 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002513 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002514 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2515 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002516 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002517 }
2518
Wale Ogunwale494009b82016-10-21 09:01:38 -07002519 boolean checkWaitingForWindows() {
2520
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002521 mHaveBootMsg = false;
2522 mHaveApp = false;
2523 mHaveWallpaper = false;
2524 mHaveKeyguard = true;
2525
2526 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002527 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2528 return true;
2529 }
2530 if (w.isDrawnLw()) {
2531 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002532 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002533 } else if (w.mAttrs.type == TYPE_APPLICATION
2534 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002535 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002536 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002537 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002538 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002539 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002540 }
2541 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002542 return false;
2543 });
2544
2545 if (visibleWindow != null) {
2546 // We have a visible window.
2547 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002548 }
2549
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002550 // if the wallpaper service is disabled on the device, we're never going to have
2551 // wallpaper, don't bother waiting for it
2552 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2553 com.android.internal.R.bool.config_enableWallpaperService)
2554 && !mService.mOnlyCore;
2555
Wale Ogunwale494009b82016-10-21 09:01:38 -07002556 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2557 "******** booted=" + mService.mSystemBooted
2558 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002559 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2560 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2561 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002562
2563 // If we are turning on the screen to show the boot message, don't do it until the boot
2564 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002565 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002566 return true;
2567 }
2568
2569 // If we are turning on the screen after the boot is completed normally, don't do so until
2570 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002571 if (mService.mSystemBooted
2572 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002573 return true;
2574 }
2575
2576 return false;
2577 }
2578
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002579 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002580 mTmpWindowAnimator = animator;
2581 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002582 }
2583
2584 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002585 resetAnimationBackgroundAnimator();
2586
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002587 // Used to indicate a detached wallpaper.
2588 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002589 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002590
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002591 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002592
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002593 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002594 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002595 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2596 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002597 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2598 }
2599 }
2600
2601 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002602 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002603 }
2604
Wale Ogunwale494009b82016-10-21 09:01:38 -07002605 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002606 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002607 if (imFocus == null) {
2608 return false;
2609 }
2610
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 if (DEBUG_INPUT_METHOD) {
2612 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2613 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2614 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002615 }
2616
Wale Ogunwale494009b82016-10-21 09:01:38 -07002617 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2618
2619 if (DEBUG_INPUT_METHOD) {
2620 Slog.i(TAG_WM, "IM target client: " + imeClient);
2621 if (imeClient != null) {
2622 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2623 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2624 }
2625 }
2626
2627 return imeClient != null && imeClient.asBinder() == client.asBinder();
2628 }
2629
2630 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002631 final WindowState win = getWindow(
2632 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2633 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002634 }
2635
2636 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002637 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002638 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002639 final int curValue = w.mSystemUiVisibility;
2640 final int diff = (curValue ^ visibility) & globalDiff;
2641 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002642 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002643 w.mSeq++;
2644 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002645 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002646 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2647 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002648 visibility, newValue, diff);
2649 }
2650 } catch (RemoteException e) {
2651 // so sorry
2652 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002653 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002654 }
2655
2656 void onWindowFreezeTimeout() {
2657 Slog.w(TAG_WM, "Window freeze timeout expired.");
2658 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002659
2660 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002661 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002662 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002663 }
2664 w.mOrientationChanging = false;
2665 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2666 - mService.mDisplayFreezeTime);
2667 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002668 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002669 mService.mWindowPlacerLocked.performSurfacePlacement();
2670 }
2671
2672 void waitForAllWindowsDrawn() {
2673 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002674 forAllWindows(w -> {
2675 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2676 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2677 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002678 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002679 w.mLastContentInsets.set(-1, -1, -1, -1);
2680 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002681 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002682 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002683 }
2684
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002685 // TODO: Super crazy long method that should be broken down...
2686 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2687
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002688 final int dw = mDisplayInfo.logicalWidth;
2689 final int dh = mDisplayInfo.logicalHeight;
2690 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2691
2692 mTmpUpdateAllDrawn.clear();
2693
2694 int repeats = 0;
2695 do {
2696 repeats++;
2697 if (repeats > 6) {
2698 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2699 clearLayoutNeeded();
2700 break;
2701 }
2702
2703 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2704 pendingLayoutChanges);
2705
Andrii Kulian839def92016-11-02 10:58:58 -07002706 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2707 // the wallpaper window jumping across displays.
2708 // Remove check for default display when there will be support for multiple wallpaper
2709 // targets (on different displays).
2710 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002711 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002712 }
2713
2714 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2715 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2716 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2717 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002718 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002719 }
2720 }
2721
2722 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2723 setLayoutNeeded();
2724 }
2725
2726 // FIRST LOOP: Perform a layout, if needed.
2727 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2728 performLayout(repeats == 1, false /* updateInputWindows */);
2729 } else {
2730 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2731 }
2732
2733 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2734 pendingLayoutChanges = 0;
2735
2736 if (isDefaultDisplay) {
2737 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002738 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002739 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2740 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2741 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2742 }
2743 } while (pendingLayoutChanges != 0);
2744
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002746 resetDimming();
2747
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002748 mTmpRecoveringMemory = recoveringMemory;
2749 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002750
2751 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002752 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2753 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2754 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002755 true /* inTraversal, must call performTraversalInTrans... below */);
2756
2757 stopDimmingIfNeeded();
2758
2759 while (!mTmpUpdateAllDrawn.isEmpty()) {
2760 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2761 // See if any windows have been drawn, so they (and others associated with them)
2762 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002763 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002764 }
2765
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002766 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002767 }
2768
2769 void performLayout(boolean initial, boolean updateInputWindows) {
2770 if (!isLayoutNeeded()) {
2771 return;
2772 }
2773 clearLayoutNeeded();
2774
2775 final int dw = mDisplayInfo.logicalWidth;
2776 final int dh = mDisplayInfo.logicalHeight;
2777
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002778 if (DEBUG_LAYOUT) {
2779 Slog.v(TAG, "-------------------------------------");
2780 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2781 }
2782
Andrii Kulian8ee72852017-03-10 10:36:45 -08002783 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002784 getConfiguration().uiMode);
2785 if (isDefaultDisplay) {
2786 // Not needed on non-default displays.
2787 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2788 mService.mScreenRect.set(0, 0, dw, dh);
2789 }
2790
2791 mService.mPolicy.getContentRectLw(mContentRect);
2792
2793 int seq = mService.mLayoutSeq + 1;
2794 if (seq < 0) seq = 0;
2795 mService.mLayoutSeq = seq;
2796
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002797 // Used to indicate that we have processed the dream window and all additional windows are
2798 // behind it.
2799 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002800 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002801
2802 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002803 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002804
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002805 // Used to indicate that we have processed the dream window and all additional attached
2806 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002807 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002808 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002809
2810 // Now perform layout of attached windows, which usually depend on the position of the
2811 // window they are attached to. XXX does not deal with windows that are attached to windows
2812 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002813 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002814
2815 // Window frames may have changed. Tell the input dispatcher about it.
2816 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2817 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2818 if (updateInputWindows) {
2819 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2820 }
2821
2822 mService.mPolicy.finishLayoutLw();
2823 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2824 }
2825
2826 /**
2827 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2828 * In portrait mode, it grabs the full screenshot.
2829 *
2830 * @param width the width of the target bitmap
2831 * @param height the height of the target bitmap
2832 * @param includeFullDisplay true if the screen should not be cropped before capture
2833 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2834 * @param config of the output bitmap
2835 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002836 * @param includeDecor whether to include window decors, like the status or navigation bar
2837 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002838 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002839 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002840 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002841 boolean wallpaperOnly, boolean includeDecor) {
2842 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2843 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002844 if (bitmap == null) {
2845 return null;
2846 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002847
2848 if (DEBUG_SCREENSHOT) {
2849 // TEST IF IT's ALL BLACK
2850 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2851 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2852 bitmap.getHeight());
2853 boolean allBlack = true;
2854 final int firstColor = buffer[0];
2855 for (int i = 0; i < buffer.length; i++) {
2856 if (buffer[i] != firstColor) {
2857 allBlack = false;
2858 break;
2859 }
2860 }
2861 if (allBlack) {
2862 final WindowState appWin = mScreenshotApplicationState.appWin;
2863 final int maxLayer = mScreenshotApplicationState.maxLayer;
2864 final int minLayer = mScreenshotApplicationState.minLayer;
2865 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2866 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2867 (appWin != null ?
2868 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2869 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2870 }
2871 }
2872
2873 // Create a copy of the screenshot that is immutable and backed in ashmem.
2874 // This greatly reduces the overhead of passing the bitmap between processes.
2875 Bitmap ret = bitmap.createAshmemBitmap(config);
2876 bitmap.recycle();
2877 return ret;
2878 }
2879
2880 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2881 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2882 boolean includeDecor) {
2883 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2884 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2885 }
2886
2887 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2888 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2889 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002890 int dw = mDisplayInfo.logicalWidth;
2891 int dh = mDisplayInfo.logicalHeight;
2892 if (dw == 0 || dh == 0) {
2893 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2894 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2895 return null;
2896 }
2897
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002898 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002899
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002900 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002901 final Rect frame = new Rect();
2902 final Rect stackBounds = new Rect();
2903
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002904 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002905 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002906 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002907 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002908 if (!mService.mPolicy.isScreenOn()) {
2909 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
2910 + " was off.");
2911 return null;
2912 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002913 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002914 mScreenshotApplicationState.appWin = null;
2915 forAllWindows(w -> {
2916 if (!w.mHasSurface) {
2917 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002918 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002919 if (w.mLayer >= aboveAppLayer) {
2920 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002921 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002922 if (wallpaperOnly && !w.mIsWallpaper) {
2923 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002924 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002925 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02002926 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002927 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002928 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2929 // then the target window state is this one.
2930 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002931 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002932 }
2933
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002934 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002935 // We have not ran across the target window yet, so it is probably behind
2936 // the wallpaper. This can happen when the keyguard is up and all windows
2937 // are moved behind the wallpaper. We don't want to include the wallpaper
2938 // layer in the screenshot as it will cover-up the layer of the target
2939 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002940 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002941 }
2942 // Fall through. The target window is in front of the wallpaper. For this
2943 // case we want to include the wallpaper layer in the screenshot because
2944 // the target window might have some transparent areas.
2945 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002946 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002947 // This app window is of no interest if it is not associated with the
2948 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002949 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002950 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002951 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002952 }
2953
2954 // Include this window.
2955
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002956 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002957 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002958 if (mScreenshotApplicationState.maxLayer < layer) {
2959 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002960 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002961 if (mScreenshotApplicationState.minLayer > layer) {
2962 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002963 }
2964
2965 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02002966 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
2967 if (includeDecor) {
2968 final TaskStack stack = w.getStack();
2969 if (stack != null) {
2970 stack.getBounds(frame);
2971 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02002972
Jorim Jaggi70f59482017-05-04 14:05:59 +02002973 // We want to screenshot with the exact bounds of the surface of the app. Thus,
2974 // intersect it with the frame.
2975 frame.intersect(w.mFrame);
2976 } else {
2977 final Rect wf = w.mFrame;
2978 final Rect cr = w.mContentInsets;
2979 int left = wf.left + cr.left;
2980 int top = wf.top + cr.top;
2981 int right = wf.right - cr.right;
2982 int bottom = wf.bottom - cr.bottom;
2983 frame.union(left, top, right, bottom);
2984 w.getVisibleBounds(stackBounds);
2985 if (!Rect.intersects(frame, stackBounds)) {
2986 // Set frame empty if there's no intersection.
2987 frame.setEmpty();
2988 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002989 }
2990 }
2991
2992 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002993 (w.mAppToken != null && w.mAppToken.token == appToken)
2994 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002995 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002996 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002997 }
2998
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002999 if (w.isObscuringDisplay()){
3000 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003001 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003002 return false;
3003 }, true /* traverseTopToBottom */);
3004
3005 final WindowState appWin = mScreenshotApplicationState.appWin;
3006 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3007 final int maxLayer = mScreenshotApplicationState.maxLayer;
3008 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003009
3010 if (appToken != null && appWin == null) {
3011 // Can't find a window to snapshot.
3012 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3013 "Screenshot: Couldn't find a surface matching " + appToken);
3014 return null;
3015 }
3016
3017 if (!screenshotReady) {
3018 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3019 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3020 appWin.mWinAnimator.mDrawState)));
3021 return null;
3022 }
3023
3024 // Screenshot is ready to be taken. Everything from here below will continue
3025 // through the bottom of the loop and return a value. We only stay in the loop
3026 // because we don't want to release the mWindowMap lock until the screenshot is
3027 // taken.
3028
3029 if (maxLayer == 0) {
3030 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3031 + ": returning null maxLayer=" + maxLayer);
3032 return null;
3033 }
3034
Jorim Jaggi02886a82016-12-06 09:10:06 -08003035 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003036 // Constrain frame to the screen size.
3037 if (!frame.intersect(0, 0, dw, dh)) {
3038 frame.setEmpty();
3039 }
3040 } else {
3041 // Caller just wants entire display.
3042 frame.set(0, 0, dw, dh);
3043 }
3044 if (frame.isEmpty()) {
3045 return null;
3046 }
3047
3048 if (width < 0) {
3049 width = (int) (frame.width() * frameScale);
3050 }
3051 if (height < 0) {
3052 height = (int) (frame.height() * frameScale);
3053 }
3054
3055 // Tell surface flinger what part of the image to crop. Take the top
3056 // right part of the application, and crop the larger dimension to fit.
3057 Rect crop = new Rect(frame);
3058 if (width / (float) frame.width() < height / (float) frame.height()) {
3059 int cropWidth = (int)((float)width / (float)height * frame.height());
3060 crop.right = crop.left + cropWidth;
3061 } else {
3062 int cropHeight = (int)((float)height / (float)width * frame.width());
3063 crop.bottom = crop.top + cropHeight;
3064 }
3065
3066 // The screenshot API does not apply the current screen rotation.
3067 int rot = mDisplay.getRotation();
3068
3069 if (rot == ROTATION_90 || rot == ROTATION_270) {
3070 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3071 }
3072
3073 // Surfaceflinger is not aware of orientation, so convert our logical
3074 // crop to surfaceflinger's portrait orientation.
3075 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3076
3077 if (DEBUG_SCREENSHOT) {
3078 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3079 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003080 forAllWindows(w -> {
3081 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3082 Slog.i(TAG_WM, w + ": " + w.mLayer
3083 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003084 + " surfaceLayer=" + ((controller == null)
3085 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003086 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003087 }
3088
3089 final ScreenRotationAnimation screenRotationAnimation =
3090 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3091 final boolean inRotation = screenRotationAnimation != null &&
3092 screenRotationAnimation.isAnimating();
3093 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3094 "Taking screenshot while rotating");
3095
3096 // We force pending transactions to flush before taking
3097 // the screenshot by pushing an empty synchronous transaction.
3098 SurfaceControl.openTransaction();
3099 SurfaceControl.closeTransactionSync();
3100
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003101 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003102 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003103 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003104 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3105 + ") to layer " + maxLayer);
3106 return null;
3107 }
3108 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003109 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003110 }
3111
3112 // TODO: Can this use createRotationMatrix()?
3113 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3114 if (rot == Surface.ROTATION_90) {
3115 final int tmp = crop.top;
3116 crop.top = dw - crop.right;
3117 crop.right = crop.bottom;
3118 crop.bottom = dw - crop.left;
3119 crop.left = tmp;
3120 } else if (rot == Surface.ROTATION_180) {
3121 int tmp = crop.top;
3122 crop.top = dh - crop.bottom;
3123 crop.bottom = dh - tmp;
3124 tmp = crop.right;
3125 crop.right = dw - crop.left;
3126 crop.left = dw - tmp;
3127 } else if (rot == Surface.ROTATION_270) {
3128 final int tmp = crop.top;
3129 crop.top = crop.left;
3130 crop.left = dh - crop.bottom;
3131 crop.bottom = crop.right;
3132 crop.right = dh - tmp;
3133 }
3134 }
3135
3136 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003137 // Used to indicate the layout is needed.
3138 mTmpWindow = null;
3139
3140 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003141 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003142 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003143 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003144 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003145 w.setDisplayLayoutNeeded();
3146 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003147 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003148
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003149 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003150 mService.mWindowPlacerLocked.performSurfacePlacement();
3151 }
3152 }
3153
Wale Ogunwale1666e312016-12-16 11:27:18 -08003154 void setExitingTokensHasVisible(boolean hasVisible) {
3155 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3156 mExitingTokens.get(i).hasVisible = hasVisible;
3157 }
3158
3159 // Initialize state of exiting applications.
3160 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3161 }
3162
3163 void removeExistingTokensIfPossible() {
3164 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3165 final WindowToken token = mExitingTokens.get(i);
3166 if (!token.hasVisible) {
3167 mExitingTokens.remove(i);
3168 }
3169 }
3170
3171 // Time to remove any exiting applications?
3172 mTaskStackContainers.removeExistingAppTokensIfPossible();
3173 }
3174
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003175 @Override
3176 void onDescendantOverrideConfigurationChanged() {
3177 setLayoutNeeded();
3178 mService.requestTraversal();
3179 }
3180
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003181 static final class TaskForResizePointSearchResult {
3182 boolean searchDone;
3183 Task taskForResize;
3184
3185 void reset() {
3186 searchDone = false;
3187 taskForResize = null;
3188 }
3189 }
Robert Carr3b716242016-08-16 16:02:21 -07003190
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003191 private static final class ApplySurfaceChangesTransactionState {
3192 boolean displayHasContent;
3193 boolean obscured;
3194 boolean syswin;
3195 boolean focusDisplayed;
3196 float preferredRefreshRate;
3197 int preferredModeId;
3198
3199 void reset() {
3200 displayHasContent = false;
3201 obscured = false;
3202 syswin = false;
3203 focusDisplayed = false;
3204 preferredRefreshRate = 0;
3205 preferredModeId = 0;
3206 }
3207 }
3208
3209 private static final class ScreenshotApplicationState {
3210 WindowState appWin;
3211 int maxLayer;
3212 int minLayer;
3213 boolean screenshotReady;
3214
3215 void reset(boolean screenshotReady) {
3216 appWin = null;
3217 maxLayer = 0;
3218 minLayer = 0;
3219 this.screenshotReady = screenshotReady;
3220 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3221 }
3222 }
3223
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003224 /**
3225 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3226 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3227 * homogeneous children type which is currently required by sub-classes of
3228 * {@link WindowContainer} class.
3229 */
3230 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3231
3232 int size() {
3233 return mChildren.size();
3234 }
3235
3236 E get(int index) {
3237 return mChildren.get(index);
3238 }
3239
3240 @Override
3241 boolean fillsParent() {
3242 return true;
3243 }
3244
3245 @Override
3246 boolean isVisible() {
3247 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003248 }
3249 }
3250
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003251 /**
3252 * Window container class that contains all containers on this display relating to Apps.
3253 * I.e Activities.
3254 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003255 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003256
Andrii Kulian839def92016-11-02 10:58:58 -07003257 /**
3258 * Adds the stack to this container.
3259 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3260 */
3261 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003262 if (stack.mStackId == HOME_STACK_ID) {
3263 if (mHomeStack != null) {
3264 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3265 }
3266 mHomeStack = stack;
3267 }
3268 addChild(stack, onTop);
3269 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003270 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003271
Andrii Kulian839def92016-11-02 10:58:58 -07003272 /** Removes the stack from its container and prepare for changing the parent. */
3273 void removeStackFromDisplay(TaskStack stack) {
3274 removeChild(stack);
3275 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003276 }
3277
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003278 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003279 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3280 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003281 addChild(stack, addIndex);
3282 setLayoutNeeded();
3283 }
3284
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003285 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003286 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3287 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3288 // This stack is always-on-top, override the default behavior.
3289 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3290
3291 // Moving to its current position, as we must call super but we don't want to
3292 // perform any meaningful action.
3293 final int currentPosition = mChildren.indexOf(child);
3294 super.positionChildAt(currentPosition, child, false /* includingParents */);
3295 return;
3296 }
3297
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003298 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3299 super.positionChildAt(targetPosition, child, includingParents);
3300
3301 setLayoutNeeded();
3302 }
3303
3304 /**
3305 * When stack is added or repositioned, find a proper position for it.
3306 * This will make sure that pinned stack always stays on top.
3307 * @param requestedPosition Position requested by caller.
3308 * @param stack Stack to be added or positioned.
3309 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3310 * @return The proper position for the stack.
3311 */
3312 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3313 final int topChildPosition = mChildren.size() - 1;
3314 boolean toTop = requestedPosition == POSITION_TOP;
3315 toTop |= adding ? requestedPosition >= topChildPosition + 1
3316 : requestedPosition >= topChildPosition;
3317 int targetPosition = requestedPosition;
3318
Bryce Lee48f4b572017-04-10 10:54:15 -07003319 if (toTop && stack.mStackId != PINNED_STACK_ID
3320 && getStackById(PINNED_STACK_ID) != null) {
3321 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003322 TaskStack topStack = mChildren.get(topChildPosition);
3323 if (topStack.mStackId != PINNED_STACK_ID) {
3324 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3325 }
3326
3327 // So, stack is moved just below the pinned stack.
3328 // When we're adding a new stack the target is the current pinned stack position.
3329 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003330 // stack, because WindowContainer#positionAt() first removes element and then adds
3331 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003332 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3333 }
3334
3335 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003336 }
3337
3338 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003339 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3340 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003341 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003342 if (super.forAllWindows(callback, traverseTopToBottom)) {
3343 return true;
3344 }
3345 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3346 return true;
3347 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003348 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003349 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3350 return true;
3351 }
3352 if (super.forAllWindows(callback, traverseTopToBottom)) {
3353 return true;
3354 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003355 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003356 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003357 }
3358
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003359 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003360 boolean traverseTopToBottom) {
3361 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3362 // app tokens.
3363 // TODO: Investigate if we need to continue to do this or if we can just process them
3364 // in-order.
3365 if (traverseTopToBottom) {
3366 for (int i = mChildren.size() - 1; i >= 0; --i) {
3367 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3368 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003369 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3370 traverseTopToBottom)) {
3371 return true;
3372 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003373 }
3374 }
3375 } else {
3376 final int count = mChildren.size();
3377 for (int i = 0; i < count; ++i) {
3378 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3379 final int appTokensCount = appTokens.size();
3380 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003381 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3382 traverseTopToBottom)) {
3383 return true;
3384 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003385 }
3386 }
3387 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003388 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003389 }
3390
Wale Ogunwale1666e312016-12-16 11:27:18 -08003391 void setExitingTokensHasVisible(boolean hasVisible) {
3392 for (int i = mChildren.size() - 1; i >= 0; --i) {
3393 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3394 for (int j = appTokens.size() - 1; j >= 0; --j) {
3395 appTokens.get(j).hasVisible = hasVisible;
3396 }
3397 }
3398 }
3399
3400 void removeExistingAppTokensIfPossible() {
3401 for (int i = mChildren.size() - 1; i >= 0; --i) {
3402 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3403 for (int j = appTokens.size() - 1; j >= 0; --j) {
3404 final AppWindowToken token = appTokens.get(j);
3405 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3406 && (!token.mIsExiting || token.isEmpty())) {
3407 // Make sure there is no animation running on this token, so any windows
3408 // associated with it will be removed as soon as their animations are
3409 // complete.
3410 token.mAppAnimator.clearAnimation();
3411 token.mAppAnimator.animating = false;
3412 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3413 "performLayout: App token exiting now removed" + token);
3414 token.removeIfPossible();
3415 }
3416 }
3417 }
3418 }
3419
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003420 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003421 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003422 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003423 // Apps and their containers are not allowed to specify an orientation while the
3424 // docked or freeform stack is visible...except for the home stack/task if the
3425 // docked stack is minimized and it actually set something.
3426 if (mHomeStack != null && mHomeStack.isVisible()
3427 && mDividerControllerLocked.isMinimizedDock()) {
3428 final int orientation = mHomeStack.getOrientation();
3429 if (orientation != SCREEN_ORIENTATION_UNSET) {
3430 return orientation;
3431 }
3432 }
3433 return SCREEN_ORIENTATION_UNSPECIFIED;
3434 }
3435
3436 final int orientation = super.getOrientation();
3437 if (orientation != SCREEN_ORIENTATION_UNSET
3438 && orientation != SCREEN_ORIENTATION_BEHIND) {
3439 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3440 "App is requesting an orientation, return " + orientation);
3441 return orientation;
3442 }
3443
3444 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003445 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003446 // The next app has not been requested to be visible, so we keep the current orientation
3447 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003448 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003449 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003450 }
3451
3452 /**
3453 * Window container class that contains all containers on this display that are not related to
3454 * Apps. E.g. status bar.
3455 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003456 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3457 /**
3458 * Compares two child window tokens returns -1 if the first is lesser than the second in
3459 * terms of z-order and 1 otherwise.
3460 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003461 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003462 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003463 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3464 token1.mOwnerCanManageAppTokens)
3465 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3466 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003467
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003468 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3469 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3470 return false;
3471 }
3472 final int req = w.mAttrs.screenOrientation;
3473 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3474 || req == SCREEN_ORIENTATION_UNSET) {
3475 return false;
3476 }
3477 return true;
3478 };
3479
Wale Ogunwale3a931692016-11-02 16:49:48 -07003480 private final String mName;
3481 NonAppWindowContainers(String name) {
3482 mName = name;
3483 }
3484
3485 void addChild(WindowToken token) {
3486 addChild(token, mWindowComparator);
3487 }
3488
3489 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003490 int getOrientation() {
3491 final WindowManagerPolicy policy = mService.mPolicy;
3492 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003493 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003494
3495 if (win != null) {
3496 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003497 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003498 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003499 if (mService.mKeyguardGoingAway) {
3500 // Keyguard can't affect the orientation if it is going away...
3501 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3502 return SCREEN_ORIENTATION_UNSET;
3503 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003504 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003505 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003506 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003507 }
3508
Andrii Kulian8ee72852017-03-10 10:36:45 -08003509 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003510
3511 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003512 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003513 }
3514
3515 return SCREEN_ORIENTATION_UNSET;
3516 }
3517
3518 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003519 String getName() {
3520 return mName;
3521 }
Robert Carr3b716242016-08-16 16:02:21 -07003522 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003523
3524 /**
3525 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3526 */
3527 @FunctionalInterface
3528 private interface Screenshoter<E> {
3529 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3530 boolean useIdentityTransform, int rotation);
3531 }
Craig Mautner59c00972012-07-30 12:10:24 -07003532}