blob: bbf7d9ff8a52daaa922cc26c074b36f1cd066d3d [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) {
990 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1]);
991 // 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;
1165 config.screenWidthDp =
1166 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1167 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1168 config.screenHeightDp =
1169 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1170 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001171
1172 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1173 final int leftInset = mTmpRect.left;
1174 final int topInset = mTmpRect.top;
1175 // appBounds at the root level should mirror the app screen size.
1176 config.setAppBounds(leftInset /*left*/, topInset /*top*/, leftInset + displayInfo.appWidth /*right*/,
1177 topInset + displayInfo.appHeight /*bottom*/);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001178 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1179 || displayInfo.rotation == Surface.ROTATION_270);
1180
1181 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1182 mDisplayMetrics.density, config);
1183
1184 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1185 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1186 ? Configuration.SCREENLAYOUT_ROUND_YES
1187 : Configuration.SCREENLAYOUT_ROUND_NO);
1188
1189 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1190 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1191 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1192 dh, mDisplayId);
1193 config.densityDpi = displayInfo.logicalDensityDpi;
1194
1195 config.colorMode =
1196 (displayInfo.isHdr()
1197 ? Configuration.COLOR_MODE_HDR_YES
1198 : Configuration.COLOR_MODE_HDR_NO)
1199 | (displayInfo.isWideColorGamut()
1200 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1201 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1202
1203 // Update the configuration based on available input devices, lid switch,
1204 // and platform configuration.
1205 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1206 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1207 config.navigation = Configuration.NAVIGATION_NONAV;
1208
1209 int keyboardPresence = 0;
1210 int navigationPresence = 0;
1211 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1212 final int len = devices != null ? devices.length : 0;
1213 for (int i = 0; i < len; i++) {
1214 InputDevice device = devices[i];
1215 if (!device.isVirtual()) {
1216 final int sources = device.getSources();
1217 final int presenceFlag = device.isExternal() ?
1218 WindowManagerPolicy.PRESENCE_EXTERNAL :
1219 WindowManagerPolicy.PRESENCE_INTERNAL;
1220
1221 // TODO(multi-display): Configure on per-display basis.
1222 if (mService.mIsTouchDevice) {
1223 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1224 InputDevice.SOURCE_TOUCHSCREEN) {
1225 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1226 }
1227 } else {
1228 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1229 }
1230
1231 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1232 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1233 navigationPresence |= presenceFlag;
1234 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1235 && config.navigation == Configuration.NAVIGATION_NONAV) {
1236 config.navigation = Configuration.NAVIGATION_DPAD;
1237 navigationPresence |= presenceFlag;
1238 }
1239
1240 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1241 config.keyboard = Configuration.KEYBOARD_QWERTY;
1242 keyboardPresence |= presenceFlag;
1243 }
1244 }
1245 }
1246
1247 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1248 config.navigation = Configuration.NAVIGATION_DPAD;
1249 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1250 }
1251
1252 // Determine whether a hard keyboard is available and enabled.
1253 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1254 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1255 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1256 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1257 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1258 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1259 }
1260
1261 // Let the policy update hidden states.
1262 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1263 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1264 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1265 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1266 }
1267
1268 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1269 int displayId) {
1270 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1271 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1272 final int unrotDw, unrotDh;
1273 if (rotated) {
1274 unrotDw = dh;
1275 unrotDh = dw;
1276 } else {
1277 unrotDw = dw;
1278 unrotDh = dh;
1279 }
1280 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1281 displayId);
1282 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1283 displayId);
1284 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1285 displayId);
1286 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1287 displayId);
1288 return sw;
1289 }
1290
1291 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1292 DisplayMetrics dm, int dw, int dh, int displayId) {
1293 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1294 displayId);
1295 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1296 uiMode, displayId);
1297 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1298 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1299 if (curSize == 0 || size < curSize) {
1300 curSize = size;
1301 }
1302 return curSize;
1303 }
1304
1305 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1306 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1307
1308 // We need to determine the smallest width that will occur under normal
1309 // operation. To this, start with the base screen size and compute the
1310 // width under the different possible rotations. We need to un-rotate
1311 // the current screen dimensions before doing this.
1312 int unrotDw, unrotDh;
1313 if (rotated) {
1314 unrotDw = dh;
1315 unrotDh = dw;
1316 } else {
1317 unrotDw = dw;
1318 unrotDh = dh;
1319 }
1320 displayInfo.smallestNominalAppWidth = 1<<30;
1321 displayInfo.smallestNominalAppHeight = 1<<30;
1322 displayInfo.largestNominalAppWidth = 0;
1323 displayInfo.largestNominalAppHeight = 0;
1324 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1325 unrotDh);
1326 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1327 unrotDw);
1328 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1329 unrotDh);
1330 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1331 unrotDw);
1332 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1333 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1334 displayId);
1335 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1336 displayId);
1337 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1338 displayId);
1339 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1340 displayId);
1341 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1342 outConfig.screenLayout = sl;
1343 }
1344
1345 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1346 int uiMode, int displayId) {
1347 // Get the app screen size at this rotation.
1348 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1349 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1350
1351 // Compute the screen layout size class for this rotation.
1352 int longSize = w;
1353 int shortSize = h;
1354 if (longSize < shortSize) {
1355 int tmp = longSize;
1356 longSize = shortSize;
1357 shortSize = tmp;
1358 }
1359 longSize = (int)(longSize/density);
1360 shortSize = (int)(shortSize/density);
1361 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1362 }
1363
1364 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1365 int uiMode, int dw, int dh) {
1366 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1367 displayId);
1368 if (width < displayInfo.smallestNominalAppWidth) {
1369 displayInfo.smallestNominalAppWidth = width;
1370 }
1371 if (width > displayInfo.largestNominalAppWidth) {
1372 displayInfo.largestNominalAppWidth = width;
1373 }
1374 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1375 displayId);
1376 if (height < displayInfo.smallestNominalAppHeight) {
1377 displayInfo.smallestNominalAppHeight = height;
1378 }
1379 if (height > displayInfo.largestNominalAppHeight) {
1380 displayInfo.largestNominalAppHeight = height;
1381 }
1382 }
1383
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001384 DockedStackDividerController getDockedDividerController() {
1385 return mDividerControllerLocked;
1386 }
1387
Winson Chung655332c2016-10-31 13:14:28 -07001388 PinnedStackController getPinnedStackController() {
1389 return mPinnedStackControllerLocked;
1390 }
1391
Jeff Browna506a6e2013-06-04 00:02:38 -07001392 /**
1393 * Returns true if the specified UID has access to this display.
1394 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001395 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001396 return mDisplay.hasAccess(uid);
1397 }
1398
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001399 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001400 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001401 }
1402
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001403 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001404 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001405 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001406 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001407 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001408 }
1409
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001410 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001411 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1412 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001413 if (stack.mStackId == stackId) {
1414 return stack;
1415 }
1416 }
1417 return null;
1418 }
1419
Bryce Lee48f4b572017-04-10 10:54:15 -07001420 @VisibleForTesting
1421 int getStackCount() {
1422 return mTaskStackContainers.size();
1423 }
1424
1425 @VisibleForTesting
1426 int getStaskPosById(int stackId) {
1427 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1428 final TaskStack stack = mTaskStackContainers.get(i);
1429 if (stack.mStackId == stackId) {
1430 return i;
1431 }
1432 }
1433 return -1;
1434 }
1435
Andrii Kulian441e4492016-09-29 15:25:00 -07001436 @Override
1437 void onConfigurationChanged(Configuration newParentConfig) {
1438 super.onConfigurationChanged(newParentConfig);
1439
Andrii Kulian3a507b52016-09-19 18:14:12 -07001440 // The display size information is heavily dependent on the resources in the current
1441 // configuration, so we need to reconfigure it every time the configuration changes.
1442 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1443 mService.reconfigureDisplayLocked(this);
1444
1445 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001446 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001447 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001448
Andrii Kulian441e4492016-09-29 15:25:00 -07001449 /**
1450 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1451 * bounds were updated.
1452 */
1453 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001454 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1455 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001456 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001457 changedStackList.add(stack.mStackId);
1458 }
1459 }
Winson Chung32c566f2017-04-11 18:31:21 -07001460
1461 // If there was no pinned stack, we still need to notify the controller of the display info
1462 // update as a result of the config change. We do this here to consolidate the flow between
1463 // changes when there is and is not a stack.
1464 if (getStackById(PINNED_STACK_ID) == null) {
1465 mPinnedStackControllerLocked.onDisplayInfoChanged();
1466 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001467 }
1468
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001469 @Override
1470 boolean fillsParent() {
1471 return true;
1472 }
1473
1474 @Override
1475 boolean isVisible() {
1476 return true;
1477 }
1478
1479 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001480 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001481 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001482 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001483 }
1484
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001485 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001486 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1487 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1488 // target, or here in order if there isn't an IME target.
1489 if (traverseTopToBottom) {
1490 for (int i = mChildren.size() - 1; i >= 0; --i) {
1491 final DisplayChildWindowContainer child = mChildren.get(i);
1492 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1493 // In this case the Ime windows will be processed above their target so we skip
1494 // here.
1495 continue;
1496 }
1497 if (child.forAllWindows(callback, traverseTopToBottom)) {
1498 return true;
1499 }
1500 }
1501 } else {
1502 final int count = mChildren.size();
1503 for (int i = 0; i < count; i++) {
1504 final DisplayChildWindowContainer child = mChildren.get(i);
1505 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1506 // In this case the Ime windows will be processed above their target so we skip
1507 // here.
1508 continue;
1509 }
1510 if (child.forAllWindows(callback, traverseTopToBottom)) {
1511 return true;
1512 }
1513 }
1514 }
1515 return false;
1516 }
1517
1518 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1519 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1520 }
1521
Wale Ogunwale399c8692017-05-08 14:22:42 -07001522 @Override
1523 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001524 final WindowManagerPolicy policy = mService.mPolicy;
1525
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001526 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001527 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001528 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001529 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001530 // If the display is frozen, some activities may be in the middle of restarting, and
1531 // thus have removed their old window. If the window has the flag to hide the lock
1532 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1533 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001534 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001535 } else if (policy.isKeyguardLocked()) {
1536 // Use the last orientation the while the display is frozen with the keyguard
1537 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1538 // window. We don't want to check the show when locked window directly though as
1539 // things aren't stable while the display is frozen, for example the window could be
1540 // momentarily unavailable due to activity relaunch.
1541 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001542 + "return " + mLastOrientation);
1543 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001544 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001545 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001546 final int orientation = mAboveAppWindowsContainers.getOrientation();
1547 if (orientation != SCREEN_ORIENTATION_UNSET) {
1548 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001549 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001550 }
1551
Wale Ogunwale399c8692017-05-08 14:22:42 -07001552 // Top system windows are not requesting an orientation. Start searching from apps.
1553 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001554 }
1555
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001556 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001557 // Check if display metrics changed and update base values if needed.
1558 updateBaseDisplayMetricsIfNeeded();
1559
Craig Mautner722285e2012-09-07 13:55:58 -07001560 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001561 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001562
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001563 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1564 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001565 }
Craig Mautner722285e2012-09-07 13:55:58 -07001566 }
1567
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001568 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001569 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1570 if (displayManagerInternal != null) {
1571 // Bootstrap the default logical display from the display manager.
1572 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1573 if (newDisplayInfo != null) {
1574 mDisplayInfo.copyFrom(newDisplayInfo);
1575 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001576 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001577
Andrii Kuliancd097992017-03-23 18:31:59 -07001578 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1579 mDisplayInfo.logicalDensityDpi);
1580 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1581 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1582 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001583 }
1584
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001585 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001586 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001587 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001588 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001589 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1590 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001591 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001592 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001593 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001594 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001595 out.set(left, top, left + width, top + height);
1596 }
1597
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001598 private void getLogicalDisplayRect(Rect out, int orientation) {
1599 getLogicalDisplayRect(out);
1600
1601 // Rotate the Rect if needed.
1602 final int currentRotation = mDisplayInfo.rotation;
1603 final int rotationDelta = deltaRotation(currentRotation, orientation);
1604 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1605 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1606 mTmpRectF.set(out);
1607 mTmpMatrix.mapRect(mTmpRectF);
1608 mTmpRectF.round(out);
1609 }
1610 }
1611
Andrii Kuliancd097992017-03-23 18:31:59 -07001612 /**
1613 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1614 * values.
1615 */
1616 private void updateBaseDisplayMetricsIfNeeded() {
1617 // Get real display metrics without overrides from WM.
1618 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1619 final int orientation = mDisplayInfo.rotation;
1620 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1621 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1622 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1623 final int newDensity = mDisplayInfo.logicalDensityDpi;
1624
1625 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1626 || mInitialDisplayHeight != newHeight
1627 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1628
1629 if (displayMetricsChanged) {
1630 // Check if display size or density is forced.
1631 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1632 || mBaseDisplayHeight != mInitialDisplayHeight;
1633 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1634
1635 // If there is an override set for base values - use it, otherwise use new values.
1636 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1637 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1638 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1639
1640 // Real display metrics changed, so we should also update initial values.
1641 mInitialDisplayWidth = newWidth;
1642 mInitialDisplayHeight = newHeight;
1643 mInitialDisplayDensity = newDensity;
1644 mService.reconfigureDisplayLocked(this);
1645 }
1646 }
1647
Bryce Lee27cec322017-03-21 09:41:37 -07001648 /** Sets the maximum width the screen resolution can be */
1649 void setMaxUiWidth(int width) {
1650 if (DEBUG_DISPLAY) {
1651 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1652 }
1653
1654 mMaxUiWidth = width;
1655
1656 // Update existing metrics.
1657 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1658 }
1659
1660 /** Update base (override) display metrics. */
1661 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1662 mBaseDisplayWidth = baseWidth;
1663 mBaseDisplayHeight = baseHeight;
1664 mBaseDisplayDensity = baseDensity;
1665
1666 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1667 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1668 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1669 mBaseDisplayWidth = mMaxUiWidth;
1670
1671 if (DEBUG_DISPLAY) {
1672 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1673 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1674 + " on display:" + getDisplayId());
1675 }
1676 }
1677
1678 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1679 }
1680
Chong Zhangf66db432016-01-13 10:39:51 -08001681 void getContentRect(Rect out) {
1682 out.set(mContentRect);
1683 }
1684
Wale Ogunwale1666e312016-12-16 11:27:18 -08001685 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1686 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1687 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001688
Wale Ogunwale1666e312016-12-16 11:27:18 -08001689 TaskStack stack = getStackById(stackId);
1690 if (stack != null) {
1691 // It's already attached to the display...clear mDeferRemoval and move stack to
1692 // appropriate z-order on display as needed.
1693 stack.mDeferRemoval = false;
1694 // We're not moving the display to front when we're adding stacks, only when
1695 // requested to change the position of stack explicitly.
1696 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1697 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001698 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001699 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001700 mTaskStackContainers.addStackToDisplay(stack, onTop);
1701 }
1702
Wale Ogunwale1666e312016-12-16 11:27:18 -08001703 if (stackId == DOCKED_STACK_ID) {
1704 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001705 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001706 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001707 }
1708
Andrii Kulian51c1b672017-04-07 18:39:32 -07001709 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001710 final DisplayContent prevDc = stack.getDisplayContent();
1711 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001712 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1713 + " which is not currently attached to any display");
1714 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001715 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001716 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1717 + " to its current displayId=" + mDisplayId);
1718 }
1719
Wale Ogunwale1666e312016-12-16 11:27:18 -08001720 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001721 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001722 }
1723
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001724 @Override
1725 protected void addChild(DisplayChildWindowContainer child,
1726 Comparator<DisplayChildWindowContainer> comparator) {
1727 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1728 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001729
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001730 @Override
1731 protected void addChild(DisplayChildWindowContainer child, int index) {
1732 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1733 }
1734
1735 @Override
1736 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001737 // Only allow removal of direct children from this display if the display is in the process
1738 // of been removed.
1739 if (mRemovingDisplay) {
1740 super.removeChild(child);
1741 return;
1742 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001743 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001744 }
1745
Andrii Kuliand2765632016-12-12 22:26:34 -08001746 @Override
1747 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1748 // Children of the display are statically ordered, so the real intention here is to perform
1749 // the operation on the display and not the static direct children.
1750 getParent().positionChildAt(position, this, includingParents);
1751 }
1752
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001753 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001754 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1755 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001756 final int taskId = stack.taskIdFromPoint(x, y);
1757 if (taskId != -1) {
1758 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001759 }
1760 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001761 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001762 }
1763
Chong Zhang8e89b312015-09-09 15:09:30 -07001764 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001765 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001766 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001767 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001768 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001769 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001770 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001771 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1772 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001773 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001774 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001775 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001776
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001777 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1778 if (mTmpTaskForResizePointSearchResult.searchDone) {
1779 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001780 }
1781 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001782 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001783 }
1784
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001785 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001786 // The provided task is the task on this display with focus, so if WindowManagerService's
1787 // focused app is not on this display, focusedTask will be null.
1788 if (focusedTask == null) {
1789 mTouchExcludeRegion.setEmpty();
1790 } else {
1791 mTouchExcludeRegion.set(mBaseDisplayRect);
1792 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1793 mTmpRect2.setEmpty();
1794 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1795 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1796 stack.setTouchExcludeRegion(
1797 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1798 }
1799 // If we removed the focused task above, add it back and only leave its
1800 // outside touch area in the exclusion. TapDectector is not interested in
1801 // any touch inside the focused task itself.
1802 if (!mTmpRect2.isEmpty()) {
1803 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1804 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001805 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001806 final WindowState inputMethod = mService.mInputMethodWindow;
1807 if (inputMethod != null && inputMethod.isVisibleLw()) {
1808 // If the input method is visible and the user is typing, we don't want these touch
1809 // events to be intercepted and used to change focus. This would likely cause a
1810 // disappearance of the input method.
1811 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001812 if (inputMethod.getDisplayId() == mDisplayId) {
1813 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1814 } else {
1815 // IME is on a different display, so we need to update its tap detector.
1816 // TODO(multidisplay): Remove when IME will always appear on same display.
1817 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1818 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001819 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001820 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1821 WindowState win = mTapExcludedWindows.get(i);
1822 win.getTouchableRegion(mTmpRegion);
1823 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1824 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001825 // TODO(multi-display): Support docked stacks on secondary displays.
1826 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001827 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001828 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001829 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1830 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001831 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001832 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001833 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001834 }
1835
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001836 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001837 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001838 super.switchUser();
1839 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001840 }
1841
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001842 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001843 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1844 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001845 }
1846 }
1847
1848 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001849 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001850 }
1851
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001852 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001853 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001854 }
1855
1856 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001857 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001858 }
1859
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001860 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001861 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001862 }
1863
Wale Ogunwale10124582016-09-15 20:25:50 -07001864 @Override
1865 void removeIfPossible() {
1866 if (isAnimating()) {
1867 mDeferredRemoval = true;
1868 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001869 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001870 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001871 }
1872
Wale Ogunwale10124582016-09-15 20:25:50 -07001873 @Override
1874 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001875 mRemovingDisplay = true;
1876 try {
1877 super.removeImmediately();
1878 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1879 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001880 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001881 mService.unregisterPointerEventListener(mTapDetector);
1882 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1883 }
1884 } finally {
1885 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001886 }
Craig Mautner95da1082014-02-24 17:54:35 -08001887 }
1888
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001889 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001890 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001891 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001892 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1893
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001894 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001895 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001896 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001897 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001898 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001899 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001900 }
1901
Wale Ogunwale10124582016-09-15 20:25:50 -07001902 boolean animateForIme(float interpolatedValue, float animationTarget,
1903 float dividerAnimationTarget) {
1904 boolean updated = false;
1905
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001906 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1907 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001908 if (stack == null || !stack.isAdjustedForIme()) {
1909 continue;
1910 }
1911
1912 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1913 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1914 updated = true;
1915 } else {
1916 mDividerControllerLocked.mLastAnimationProgress =
1917 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1918 mDividerControllerLocked.mLastDividerProgress =
1919 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1920 updated |= stack.updateAdjustForIme(
1921 mDividerControllerLocked.mLastAnimationProgress,
1922 mDividerControllerLocked.mLastDividerProgress,
1923 false /* force */);
1924 }
1925 if (interpolatedValue >= 1f) {
1926 stack.endImeAdjustAnimation();
1927 }
1928 }
1929
1930 return updated;
1931 }
1932
1933 boolean clearImeAdjustAnimation() {
1934 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001935 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1936 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001937 if (stack != null && stack.isAdjustedForIme()) {
1938 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1939 changed = true;
1940 }
1941 }
1942 return changed;
1943 }
1944
1945 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001946 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1947 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001948 if (stack.isVisible() && stack.isAdjustedForIme()) {
1949 stack.beginImeAdjustAnimation();
1950 }
1951 }
1952 }
1953
1954 void adjustForImeIfNeeded() {
1955 final WindowState imeWin = mService.mInputMethodWindow;
1956 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1957 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001958 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001959 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1960 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1961 imeTargetStack.getDockSide() : DOCKED_INVALID;
1962 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1963 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1964 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1965 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1966 final boolean imeHeightChanged = imeVisible &&
1967 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1968
1969 // The divider could be adjusted for IME position, or be thinner than usual,
1970 // or both. There are three possible cases:
1971 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1972 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1973 // - If IME is not visible, divider is not moved and is normal width.
1974
1975 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001976 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1977 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001978 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07001979 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
1980 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001981 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1982 } else {
1983 stack.resetAdjustedForIme(false);
1984 }
1985 }
1986 mDividerControllerLocked.setAdjustedForIme(
1987 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1988 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001989 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1990 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001991 stack.resetAdjustedForIme(!dockVisible);
1992 }
1993 mDividerControllerLocked.setAdjustedForIme(
1994 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
1995 }
Winson Chung655332c2016-10-31 13:14:28 -07001996 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07001997 }
1998
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001999 void setInputMethodAnimLayerAdjustment(int adj) {
2000 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2001 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002002 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002003 }
2004
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002005 /**
2006 * If a window that has an animation specifying a colored background and the current wallpaper
2007 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2008 * suddenly disappear.
2009 */
2010 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002011 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2012 w -> w.mIsWallpaper && w.isVisibleNow());
2013
2014 if (visibleWallpaper != null) {
2015 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002016 }
2017 return winAnimator.mAnimLayer;
2018 }
2019
Wale Ogunwale10124582016-09-15 20:25:50 -07002020 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002021 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2022 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002023 stack.prepareFreezingTaskBounds();
2024 }
2025 }
2026
Wale Ogunwale94744212015-09-21 19:01:47 -07002027 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002028 getLogicalDisplayRect(mTmpRect, newRotation);
2029
2030 // Compute a transform matrix to undo the coordinate space transformation,
2031 // and present the window at the same physical position it previously occupied.
2032 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2033 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2034
2035 mTmpRectF.set(bounds);
2036 mTmpMatrix.mapRect(mTmpRectF);
2037 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002038 }
2039
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002040 static int deltaRotation(int oldRotation, int newRotation) {
2041 int delta = newRotation - oldRotation;
2042 if (delta < 0) delta += 4;
2043 return delta;
2044 }
2045
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002046 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002047 Matrix outMatrix) {
2048 // For rotations without Z-ordering we don't need the target rectangle's position.
2049 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2050 displayHeight, outMatrix);
2051 }
2052
2053 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2054 float displayWidth, float displayHeight, Matrix outMatrix) {
2055 switch (rotation) {
2056 case ROTATION_0:
2057 outMatrix.reset();
2058 break;
2059 case ROTATION_270:
2060 outMatrix.setRotate(270, 0, 0);
2061 outMatrix.postTranslate(0, displayHeight);
2062 outMatrix.postTranslate(rectTop, 0);
2063 break;
2064 case ROTATION_180:
2065 outMatrix.reset();
2066 break;
2067 case ROTATION_90:
2068 outMatrix.setRotate(90, 0, 0);
2069 outMatrix.postTranslate(displayWidth, 0);
2070 outMatrix.postTranslate(-rectTop, rectLeft);
2071 break;
2072 }
2073 }
2074
Craig Mautnera91f9e22012-09-14 16:22:08 -07002075 public void dump(String prefix, PrintWriter pw) {
2076 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2077 final String subPrefix = " " + prefix;
2078 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2079 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2080 pw.print("dpi");
2081 if (mInitialDisplayWidth != mBaseDisplayWidth
2082 || mInitialDisplayHeight != mBaseDisplayHeight
2083 || mInitialDisplayDensity != mBaseDisplayDensity) {
2084 pw.print(" base=");
2085 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2086 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2087 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002088 if (mDisplayScalingDisabled) {
2089 pw.println(" noscale");
2090 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002091 pw.print(" cur=");
2092 pw.print(mDisplayInfo.logicalWidth);
2093 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2094 pw.print(" app=");
2095 pw.print(mDisplayInfo.appWidth);
2096 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2097 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2098 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2099 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2100 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002101 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2102 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002103
Craig Mautnerdc548482014-02-05 13:35:24 -08002104 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002105 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002106 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2107 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002108 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002109 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002110
Craig Mautnerdc548482014-02-05 13:35:24 -08002111 pw.println();
2112 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002113 pw.println();
2114 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002115 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002116 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002117 pw.print(" Exiting #"); pw.print(i);
2118 pw.print(' '); pw.print(token);
2119 pw.println(':');
2120 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002121 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002122 }
Craig Mautner59c00972012-07-30 12:10:24 -07002123 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002124 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002125 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002126 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002127 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002128 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002129
2130 if (mInputMethodAnimLayerAdjustment != 0) {
2131 pw.println(subPrefix
2132 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2133 }
Craig Mautner59c00972012-07-30 12:10:24 -07002134 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002135
2136 @Override
2137 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002138 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002139 }
2140
2141 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002142 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002143 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002144
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002145 /** Checks if stack with provided id is visible on this display. */
2146 boolean isStackVisible(int stackId) {
2147 final TaskStack stack = getStackById(stackId);
2148 return (stack != null && stack.isVisible());
2149 }
2150
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002151 /**
2152 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2153 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002154 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002155 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002156 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002157 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002158
2159 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002160 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002161 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002162 */
Jorim Jaggife762342016-10-13 14:33:27 +02002163 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002164 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002165 }
2166
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002167 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002168 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002169 final int x = (int) xf;
2170 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002171 final WindowState touchedWin = getWindow(w -> {
2172 final int flags = w.mAttrs.flags;
2173 if (!w.isVisibleLw()) {
2174 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002175 }
2176 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002177 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002178 }
2179
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002180 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002181 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002182 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002183 }
2184
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002185 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002186
2187 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002188 return mTmpRegion.contains(x, y) || touchFlags == 0;
2189 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002190
2191 return touchedWin;
2192 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002193
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002194 boolean canAddToastWindowForUid(int uid) {
2195 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002196 // Also if the app is focused adding more than one toast at
2197 // a time for better backwards compatibility.
2198 final WindowState focusedWindowForUid = getWindow(w ->
2199 w.mOwnerUid == uid && w.isFocused());
2200 if (focusedWindowForUid != null) {
2201 return true;
2202 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002203 final WindowState win = getWindow(w ->
2204 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2205 && !w.mWindowRemovalAllowed);
2206 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002207 }
2208
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002209 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002210 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2211 return;
2212 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002213
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002214 // Used to communicate the old focus to the callback method.
2215 mTmpWindow = oldFocus;
2216
2217 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002218 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002219
2220 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002221 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002222
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002223 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002224
2225 if (mTmpWindow == null) {
2226 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2227 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002228 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002229 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002230 }
2231
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002232 /** Updates the layer assignment of windows on this display. */
2233 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002234 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002235 if (setLayoutNeeded) {
2236 setLayoutNeeded();
2237 }
2238 }
2239
Wale Ogunwale1666e312016-12-16 11:27:18 -08002240 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2241 // moving containers or resizing them. Need to investigate the best way to have it automatically
2242 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002243 void layoutAndAssignWindowLayersIfNeeded() {
2244 mService.mWindowsChanged = true;
2245 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002246
2247 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2248 false /*updateInputWindows*/)) {
2249 assignWindowLayers(false /* setLayoutNeeded */);
2250 }
2251
2252 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2253 mService.mWindowPlacerLocked.performSurfacePlacement();
2254 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2255 }
2256
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002257 /** Returns true if a leaked surface was destroyed */
2258 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002259 // Used to indicate that a surface was leaked.
2260 mTmpWindow = null;
2261 forAllWindows(w -> {
2262 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002263 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002264 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002265 }
2266 if (!mService.mSessions.contains(wsa.mSession)) {
2267 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002268 + w + " surface=" + wsa.mSurfaceController
2269 + " token=" + w.mToken
2270 + " pid=" + w.mSession.mPid
2271 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002272 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002273 mService.mForceRemoves.add(w);
2274 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002275 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002276 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002277 + w + " surface=" + wsa.mSurfaceController
2278 + " token=" + w.mAppToken
2279 + " saved=" + w.hasSavedSurface());
2280 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002281 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002282 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002283 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002284 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002285
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002286 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002287 }
2288
2289 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002290 * Determine and return the window that should be the IME target.
2291 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2292 * @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 +00002293 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002294 WindowState computeImeTarget(boolean updateImeTarget) {
2295 if (mService.mInputMethodWindow == null) {
2296 // There isn't an IME so there shouldn't be a target...That was easy!
2297 if (updateImeTarget) {
2298 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2299 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2300 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2301 }
2302 return null;
2303 }
2304
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002305 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2306 // same display. Or even when the current IME/target are not on the same screen as the next
2307 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002308 mUpdateImeTarget = updateImeTarget;
2309 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002310
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002311
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002312 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2313 // to be on top of it, but it is not -really- where input will go. So look down below
2314 // for a real window to target...
2315 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2316 final AppWindowToken token = target.mAppToken;
2317 if (token != null) {
2318 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2319 if (betterTarget != null) {
2320 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002321 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002322 }
2323 }
2324
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002325 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2326 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002327
2328 // Now, a special case -- if the last target's window is in the process of exiting, and is
2329 // above the new target, keep on the last target to avoid flicker. Consider for example a
2330 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2331 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2332 // scrim.
2333 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002334 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2335 && (target == null
2336 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002337 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002338 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002339 }
2340
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002341 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2342 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002343
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002344 if (target == null) {
2345 if (updateImeTarget) {
2346 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2347 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2348 + Debug.getCallers(4) : ""));
2349 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2350 }
2351
2352 return null;
2353 }
2354
2355 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002356 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2357 if (token != null) {
2358
2359 // Now some fun for dealing with window animations that modify the Z order. We need
2360 // to look at all windows below the current target that are in this app, finding the
2361 // highest visible one in layering.
2362 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002363 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002364 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002365 }
2366
2367 if (highestTarget != null) {
2368 final AppTransition appTransition = mService.mAppTransition;
2369 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2370 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2371 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002372 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002373
2374 if (appTransition.isTransitionSet()) {
2375 // If we are currently setting up for an animation, hold everything until we
2376 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002377 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2378 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002379 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002380 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002381 // If the window we are currently targeting is involved with an animation,
2382 // and it is on top of the next target we will be over, then hold off on
2383 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002384 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2385 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002386 }
2387 }
2388 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002389
2390 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2391 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2392 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002393 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002394 }
2395
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002396 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002397 }
2398
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002399 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2400 if (target == mService.mInputMethodTarget
2401 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2402 && mInputMethodAnimLayerAdjustment == layerAdj) {
2403 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002404 }
2405
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002406 mService.mInputMethodTarget = target;
2407 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2408 setInputMethodAnimLayerAdjustment(layerAdj);
2409 assignWindowLayers(false /* setLayoutNeeded */);
2410 }
2411
2412 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2413 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2414 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2415 }
2416
2417 // Used to indicate we have reached the first window in the range we are interested in.
2418 mTmpWindow = null;
2419
2420 // TODO: Figure-out a more efficient way to do this.
2421 final WindowState candidate = getWindow(w -> {
2422 if (w == top) {
2423 // Reached the first window in the range we are interested in.
2424 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002425 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002426 if (mTmpWindow == null) {
2427 return false;
2428 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002429
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002430 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2431 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002432 }
2433 // If we reached the bottom of the range of windows we are considering,
2434 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002435 if (w == bottom) {
2436 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002437 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002438 return false;
2439 });
2440
2441 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002442 }
2443
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002444 void setLayoutNeeded() {
2445 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2446 mLayoutNeeded = true;
2447 }
2448
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002449 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002450 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2451 mLayoutNeeded = false;
2452 }
2453
2454 boolean isLayoutNeeded() {
2455 return mLayoutNeeded;
2456 }
2457
Wale Ogunwale02319a62016-09-26 15:21:22 -07002458 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2459 if (mTokenMap.isEmpty()) {
2460 return;
2461 }
2462 pw.println(" Display #" + mDisplayId);
2463 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2464 while (it.hasNext()) {
2465 final WindowToken token = it.next();
2466 pw.print(" ");
2467 pw.print(token);
2468 if (dumpAll) {
2469 pw.println(':');
2470 token.dump(pw, " ");
2471 } else {
2472 pw.println();
2473 }
2474 }
2475 }
2476
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002477 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002478 final int[] index = new int[1];
2479 forAllWindows(w -> {
2480 final WindowStateAnimator wAnim = w.mWinAnimator;
2481 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2482 index[0] = index[0] + 1;
2483 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002484 }
2485
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002486 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487 forAllWindows(w -> {
2488 w.mWinAnimator.enableSurfaceTrace(fd);
2489 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002490 }
2491
2492 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002493 forAllWindows(w -> {
2494 w.mWinAnimator.disableSurfaceTrace();
2495 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002496 }
2497
Jorim Jaggife762342016-10-13 14:33:27 +02002498 /**
2499 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2500 */
2501 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2502 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002503 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002504 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2505 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002506 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002507 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2508 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002509 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002510 }
2511
Wale Ogunwale494009b82016-10-21 09:01:38 -07002512 boolean checkWaitingForWindows() {
2513
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002514 mHaveBootMsg = false;
2515 mHaveApp = false;
2516 mHaveWallpaper = false;
2517 mHaveKeyguard = true;
2518
2519 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002520 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2521 return true;
2522 }
2523 if (w.isDrawnLw()) {
2524 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002525 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002526 } else if (w.mAttrs.type == TYPE_APPLICATION
2527 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002528 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002529 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002530 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002531 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002532 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002533 }
2534 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002535 return false;
2536 });
2537
2538 if (visibleWindow != null) {
2539 // We have a visible window.
2540 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002541 }
2542
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002543 // if the wallpaper service is disabled on the device, we're never going to have
2544 // wallpaper, don't bother waiting for it
2545 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2546 com.android.internal.R.bool.config_enableWallpaperService)
2547 && !mService.mOnlyCore;
2548
Wale Ogunwale494009b82016-10-21 09:01:38 -07002549 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2550 "******** booted=" + mService.mSystemBooted
2551 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002552 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2553 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2554 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002555
2556 // If we are turning on the screen to show the boot message, don't do it until the boot
2557 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002558 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002559 return true;
2560 }
2561
2562 // If we are turning on the screen after the boot is completed normally, don't do so until
2563 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002564 if (mService.mSystemBooted
2565 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002566 return true;
2567 }
2568
2569 return false;
2570 }
2571
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002572 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002573 mTmpWindowAnimator = animator;
2574 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002575 }
2576
2577 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002578 resetAnimationBackgroundAnimator();
2579
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002580 // Used to indicate a detached wallpaper.
2581 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002582 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002583
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002584 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002585
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002586 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002587 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002588 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2589 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002590 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2591 }
2592 }
2593
2594 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002595 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002596 }
2597
Wale Ogunwale494009b82016-10-21 09:01:38 -07002598 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002599 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002600 if (imFocus == null) {
2601 return false;
2602 }
2603
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002604 if (DEBUG_INPUT_METHOD) {
2605 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2606 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2607 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002608 }
2609
Wale Ogunwale494009b82016-10-21 09:01:38 -07002610 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2611
2612 if (DEBUG_INPUT_METHOD) {
2613 Slog.i(TAG_WM, "IM target client: " + imeClient);
2614 if (imeClient != null) {
2615 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2616 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2617 }
2618 }
2619
2620 return imeClient != null && imeClient.asBinder() == client.asBinder();
2621 }
2622
2623 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002624 final WindowState win = getWindow(
2625 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2626 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002627 }
2628
2629 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002630 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002631 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 final int curValue = w.mSystemUiVisibility;
2633 final int diff = (curValue ^ visibility) & globalDiff;
2634 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002635 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002636 w.mSeq++;
2637 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002638 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002639 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2640 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002641 visibility, newValue, diff);
2642 }
2643 } catch (RemoteException e) {
2644 // so sorry
2645 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002646 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002647 }
2648
2649 void onWindowFreezeTimeout() {
2650 Slog.w(TAG_WM, "Window freeze timeout expired.");
2651 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002652
2653 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002654 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002655 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002656 }
2657 w.mOrientationChanging = false;
2658 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2659 - mService.mDisplayFreezeTime);
2660 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002661 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002662 mService.mWindowPlacerLocked.performSurfacePlacement();
2663 }
2664
2665 void waitForAllWindowsDrawn() {
2666 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002667 forAllWindows(w -> {
2668 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2669 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2670 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002671 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002672 w.mLastContentInsets.set(-1, -1, -1, -1);
2673 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002674 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002675 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002676 }
2677
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002678 // TODO: Super crazy long method that should be broken down...
2679 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2680
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002681 final int dw = mDisplayInfo.logicalWidth;
2682 final int dh = mDisplayInfo.logicalHeight;
2683 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2684
2685 mTmpUpdateAllDrawn.clear();
2686
2687 int repeats = 0;
2688 do {
2689 repeats++;
2690 if (repeats > 6) {
2691 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2692 clearLayoutNeeded();
2693 break;
2694 }
2695
2696 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2697 pendingLayoutChanges);
2698
Andrii Kulian839def92016-11-02 10:58:58 -07002699 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2700 // the wallpaper window jumping across displays.
2701 // Remove check for default display when there will be support for multiple wallpaper
2702 // targets (on different displays).
2703 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002704 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002705 }
2706
2707 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2708 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2709 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2710 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002711 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002712 }
2713 }
2714
2715 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2716 setLayoutNeeded();
2717 }
2718
2719 // FIRST LOOP: Perform a layout, if needed.
2720 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2721 performLayout(repeats == 1, false /* updateInputWindows */);
2722 } else {
2723 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2724 }
2725
2726 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2727 pendingLayoutChanges = 0;
2728
2729 if (isDefaultDisplay) {
2730 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002731 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002732 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2733 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2734 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2735 }
2736 } while (pendingLayoutChanges != 0);
2737
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002738 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002739 resetDimming();
2740
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002741 mTmpRecoveringMemory = recoveringMemory;
2742 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002743
2744 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2746 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2747 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002748 true /* inTraversal, must call performTraversalInTrans... below */);
2749
2750 stopDimmingIfNeeded();
2751
2752 while (!mTmpUpdateAllDrawn.isEmpty()) {
2753 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2754 // See if any windows have been drawn, so they (and others associated with them)
2755 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002756 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002757 }
2758
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002759 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002760 }
2761
2762 void performLayout(boolean initial, boolean updateInputWindows) {
2763 if (!isLayoutNeeded()) {
2764 return;
2765 }
2766 clearLayoutNeeded();
2767
2768 final int dw = mDisplayInfo.logicalWidth;
2769 final int dh = mDisplayInfo.logicalHeight;
2770
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002771 if (DEBUG_LAYOUT) {
2772 Slog.v(TAG, "-------------------------------------");
2773 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2774 }
2775
Andrii Kulian8ee72852017-03-10 10:36:45 -08002776 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002777 getConfiguration().uiMode);
2778 if (isDefaultDisplay) {
2779 // Not needed on non-default displays.
2780 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2781 mService.mScreenRect.set(0, 0, dw, dh);
2782 }
2783
2784 mService.mPolicy.getContentRectLw(mContentRect);
2785
2786 int seq = mService.mLayoutSeq + 1;
2787 if (seq < 0) seq = 0;
2788 mService.mLayoutSeq = seq;
2789
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002790 // Used to indicate that we have processed the dream window and all additional windows are
2791 // behind it.
2792 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002793 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002794
2795 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002796 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002797
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002798 // Used to indicate that we have processed the dream window and all additional attached
2799 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002800 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002801 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002802
2803 // Now perform layout of attached windows, which usually depend on the position of the
2804 // window they are attached to. XXX does not deal with windows that are attached to windows
2805 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002806 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002807
2808 // Window frames may have changed. Tell the input dispatcher about it.
2809 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2810 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2811 if (updateInputWindows) {
2812 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2813 }
2814
2815 mService.mPolicy.finishLayoutLw();
2816 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2817 }
2818
2819 /**
2820 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2821 * In portrait mode, it grabs the full screenshot.
2822 *
2823 * @param width the width of the target bitmap
2824 * @param height the height of the target bitmap
2825 * @param includeFullDisplay true if the screen should not be cropped before capture
2826 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2827 * @param config of the output bitmap
2828 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002829 * @param includeDecor whether to include window decors, like the status or navigation bar
2830 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002831 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002832 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002833 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002834 boolean wallpaperOnly, boolean includeDecor) {
2835 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2836 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002837 if (bitmap == null) {
2838 return null;
2839 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002840
2841 if (DEBUG_SCREENSHOT) {
2842 // TEST IF IT's ALL BLACK
2843 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2844 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2845 bitmap.getHeight());
2846 boolean allBlack = true;
2847 final int firstColor = buffer[0];
2848 for (int i = 0; i < buffer.length; i++) {
2849 if (buffer[i] != firstColor) {
2850 allBlack = false;
2851 break;
2852 }
2853 }
2854 if (allBlack) {
2855 final WindowState appWin = mScreenshotApplicationState.appWin;
2856 final int maxLayer = mScreenshotApplicationState.maxLayer;
2857 final int minLayer = mScreenshotApplicationState.minLayer;
2858 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2859 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2860 (appWin != null ?
2861 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2862 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2863 }
2864 }
2865
2866 // Create a copy of the screenshot that is immutable and backed in ashmem.
2867 // This greatly reduces the overhead of passing the bitmap between processes.
2868 Bitmap ret = bitmap.createAshmemBitmap(config);
2869 bitmap.recycle();
2870 return ret;
2871 }
2872
2873 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2874 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2875 boolean includeDecor) {
2876 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2877 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2878 }
2879
2880 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2881 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2882 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002883 int dw = mDisplayInfo.logicalWidth;
2884 int dh = mDisplayInfo.logicalHeight;
2885 if (dw == 0 || dh == 0) {
2886 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2887 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2888 return null;
2889 }
2890
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002891 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002892
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002893 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002894 final Rect frame = new Rect();
2895 final Rect stackBounds = new Rect();
2896
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002897 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002898 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002899 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002900 synchronized(mService.mWindowMap) {
2901 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002902 mScreenshotApplicationState.appWin = null;
2903 forAllWindows(w -> {
2904 if (!w.mHasSurface) {
2905 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002906 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002907 if (w.mLayer >= aboveAppLayer) {
2908 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002909 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002910 if (wallpaperOnly && !w.mIsWallpaper) {
2911 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002912 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02002914 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002915 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002916 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2917 // then the target window state is this one.
2918 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002919 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002920 }
2921
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002922 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002923 // We have not ran across the target window yet, so it is probably behind
2924 // the wallpaper. This can happen when the keyguard is up and all windows
2925 // are moved behind the wallpaper. We don't want to include the wallpaper
2926 // layer in the screenshot as it will cover-up the layer of the target
2927 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002928 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002929 }
2930 // Fall through. The target window is in front of the wallpaper. For this
2931 // case we want to include the wallpaper layer in the screenshot because
2932 // the target window might have some transparent areas.
2933 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002934 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002935 // This app window is of no interest if it is not associated with the
2936 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002937 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002938 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002939 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002940 }
2941
2942 // Include this window.
2943
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002944 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002945 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002946 if (mScreenshotApplicationState.maxLayer < layer) {
2947 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002948 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002949 if (mScreenshotApplicationState.minLayer > layer) {
2950 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951 }
2952
2953 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02002954 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
2955 if (includeDecor) {
2956 final TaskStack stack = w.getStack();
2957 if (stack != null) {
2958 stack.getBounds(frame);
2959 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02002960
Jorim Jaggi70f59482017-05-04 14:05:59 +02002961 // We want to screenshot with the exact bounds of the surface of the app. Thus,
2962 // intersect it with the frame.
2963 frame.intersect(w.mFrame);
2964 } else {
2965 final Rect wf = w.mFrame;
2966 final Rect cr = w.mContentInsets;
2967 int left = wf.left + cr.left;
2968 int top = wf.top + cr.top;
2969 int right = wf.right - cr.right;
2970 int bottom = wf.bottom - cr.bottom;
2971 frame.union(left, top, right, bottom);
2972 w.getVisibleBounds(stackBounds);
2973 if (!Rect.intersects(frame, stackBounds)) {
2974 // Set frame empty if there's no intersection.
2975 frame.setEmpty();
2976 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002977 }
2978 }
2979
2980 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002981 (w.mAppToken != null && w.mAppToken.token == appToken)
2982 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002983 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002984 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002985 }
2986
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002987 if (w.isObscuringDisplay()){
2988 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002989 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002990 return false;
2991 }, true /* traverseTopToBottom */);
2992
2993 final WindowState appWin = mScreenshotApplicationState.appWin;
2994 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2995 final int maxLayer = mScreenshotApplicationState.maxLayer;
2996 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002997
2998 if (appToken != null && appWin == null) {
2999 // Can't find a window to snapshot.
3000 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3001 "Screenshot: Couldn't find a surface matching " + appToken);
3002 return null;
3003 }
3004
3005 if (!screenshotReady) {
3006 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3007 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3008 appWin.mWinAnimator.mDrawState)));
3009 return null;
3010 }
3011
3012 // Screenshot is ready to be taken. Everything from here below will continue
3013 // through the bottom of the loop and return a value. We only stay in the loop
3014 // because we don't want to release the mWindowMap lock until the screenshot is
3015 // taken.
3016
3017 if (maxLayer == 0) {
3018 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3019 + ": returning null maxLayer=" + maxLayer);
3020 return null;
3021 }
3022
Jorim Jaggi02886a82016-12-06 09:10:06 -08003023 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003024 // Constrain frame to the screen size.
3025 if (!frame.intersect(0, 0, dw, dh)) {
3026 frame.setEmpty();
3027 }
3028 } else {
3029 // Caller just wants entire display.
3030 frame.set(0, 0, dw, dh);
3031 }
3032 if (frame.isEmpty()) {
3033 return null;
3034 }
3035
3036 if (width < 0) {
3037 width = (int) (frame.width() * frameScale);
3038 }
3039 if (height < 0) {
3040 height = (int) (frame.height() * frameScale);
3041 }
3042
3043 // Tell surface flinger what part of the image to crop. Take the top
3044 // right part of the application, and crop the larger dimension to fit.
3045 Rect crop = new Rect(frame);
3046 if (width / (float) frame.width() < height / (float) frame.height()) {
3047 int cropWidth = (int)((float)width / (float)height * frame.height());
3048 crop.right = crop.left + cropWidth;
3049 } else {
3050 int cropHeight = (int)((float)height / (float)width * frame.width());
3051 crop.bottom = crop.top + cropHeight;
3052 }
3053
3054 // The screenshot API does not apply the current screen rotation.
3055 int rot = mDisplay.getRotation();
3056
3057 if (rot == ROTATION_90 || rot == ROTATION_270) {
3058 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3059 }
3060
3061 // Surfaceflinger is not aware of orientation, so convert our logical
3062 // crop to surfaceflinger's portrait orientation.
3063 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3064
3065 if (DEBUG_SCREENSHOT) {
3066 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3067 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003068 forAllWindows(w -> {
3069 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3070 Slog.i(TAG_WM, w + ": " + w.mLayer
3071 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003072 + " surfaceLayer=" + ((controller == null)
3073 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003074 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003075 }
3076
3077 final ScreenRotationAnimation screenRotationAnimation =
3078 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3079 final boolean inRotation = screenRotationAnimation != null &&
3080 screenRotationAnimation.isAnimating();
3081 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3082 "Taking screenshot while rotating");
3083
3084 // We force pending transactions to flush before taking
3085 // the screenshot by pushing an empty synchronous transaction.
3086 SurfaceControl.openTransaction();
3087 SurfaceControl.closeTransactionSync();
3088
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003089 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003090 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003091 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003092 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3093 + ") to layer " + maxLayer);
3094 return null;
3095 }
3096 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003097 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003098 }
3099
3100 // TODO: Can this use createRotationMatrix()?
3101 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3102 if (rot == Surface.ROTATION_90) {
3103 final int tmp = crop.top;
3104 crop.top = dw - crop.right;
3105 crop.right = crop.bottom;
3106 crop.bottom = dw - crop.left;
3107 crop.left = tmp;
3108 } else if (rot == Surface.ROTATION_180) {
3109 int tmp = crop.top;
3110 crop.top = dh - crop.bottom;
3111 crop.bottom = dh - tmp;
3112 tmp = crop.right;
3113 crop.right = dw - crop.left;
3114 crop.left = dw - tmp;
3115 } else if (rot == Surface.ROTATION_270) {
3116 final int tmp = crop.top;
3117 crop.top = crop.left;
3118 crop.left = dh - crop.bottom;
3119 crop.bottom = crop.right;
3120 crop.right = dh - tmp;
3121 }
3122 }
3123
3124 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003125 // Used to indicate the layout is needed.
3126 mTmpWindow = null;
3127
3128 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003129 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003130 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003131 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003132 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003133 w.setDisplayLayoutNeeded();
3134 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003135 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003136
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003137 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003138 mService.mWindowPlacerLocked.performSurfacePlacement();
3139 }
3140 }
3141
Wale Ogunwale1666e312016-12-16 11:27:18 -08003142 void setExitingTokensHasVisible(boolean hasVisible) {
3143 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3144 mExitingTokens.get(i).hasVisible = hasVisible;
3145 }
3146
3147 // Initialize state of exiting applications.
3148 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3149 }
3150
3151 void removeExistingTokensIfPossible() {
3152 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3153 final WindowToken token = mExitingTokens.get(i);
3154 if (!token.hasVisible) {
3155 mExitingTokens.remove(i);
3156 }
3157 }
3158
3159 // Time to remove any exiting applications?
3160 mTaskStackContainers.removeExistingAppTokensIfPossible();
3161 }
3162
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003163 @Override
3164 void onDescendantOverrideConfigurationChanged() {
3165 setLayoutNeeded();
3166 mService.requestTraversal();
3167 }
3168
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003169 static final class TaskForResizePointSearchResult {
3170 boolean searchDone;
3171 Task taskForResize;
3172
3173 void reset() {
3174 searchDone = false;
3175 taskForResize = null;
3176 }
3177 }
Robert Carr3b716242016-08-16 16:02:21 -07003178
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003179 private static final class ApplySurfaceChangesTransactionState {
3180 boolean displayHasContent;
3181 boolean obscured;
3182 boolean syswin;
3183 boolean focusDisplayed;
3184 float preferredRefreshRate;
3185 int preferredModeId;
3186
3187 void reset() {
3188 displayHasContent = false;
3189 obscured = false;
3190 syswin = false;
3191 focusDisplayed = false;
3192 preferredRefreshRate = 0;
3193 preferredModeId = 0;
3194 }
3195 }
3196
3197 private static final class ScreenshotApplicationState {
3198 WindowState appWin;
3199 int maxLayer;
3200 int minLayer;
3201 boolean screenshotReady;
3202
3203 void reset(boolean screenshotReady) {
3204 appWin = null;
3205 maxLayer = 0;
3206 minLayer = 0;
3207 this.screenshotReady = screenshotReady;
3208 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3209 }
3210 }
3211
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003212 /**
3213 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3214 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3215 * homogeneous children type which is currently required by sub-classes of
3216 * {@link WindowContainer} class.
3217 */
3218 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3219
3220 int size() {
3221 return mChildren.size();
3222 }
3223
3224 E get(int index) {
3225 return mChildren.get(index);
3226 }
3227
3228 @Override
3229 boolean fillsParent() {
3230 return true;
3231 }
3232
3233 @Override
3234 boolean isVisible() {
3235 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003236 }
3237 }
3238
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003239 /**
3240 * Window container class that contains all containers on this display relating to Apps.
3241 * I.e Activities.
3242 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003243 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003244
Andrii Kulian839def92016-11-02 10:58:58 -07003245 /**
3246 * Adds the stack to this container.
3247 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3248 */
3249 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003250 if (stack.mStackId == HOME_STACK_ID) {
3251 if (mHomeStack != null) {
3252 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3253 }
3254 mHomeStack = stack;
3255 }
3256 addChild(stack, onTop);
3257 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003258 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003259
Andrii Kulian839def92016-11-02 10:58:58 -07003260 /** Removes the stack from its container and prepare for changing the parent. */
3261 void removeStackFromDisplay(TaskStack stack) {
3262 removeChild(stack);
3263 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003264 }
3265
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003266 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003267 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3268 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003269 addChild(stack, addIndex);
3270 setLayoutNeeded();
3271 }
3272
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003273 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003274 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3275 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3276 // This stack is always-on-top, override the default behavior.
3277 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3278
3279 // Moving to its current position, as we must call super but we don't want to
3280 // perform any meaningful action.
3281 final int currentPosition = mChildren.indexOf(child);
3282 super.positionChildAt(currentPosition, child, false /* includingParents */);
3283 return;
3284 }
3285
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003286 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3287 super.positionChildAt(targetPosition, child, includingParents);
3288
3289 setLayoutNeeded();
3290 }
3291
3292 /**
3293 * When stack is added or repositioned, find a proper position for it.
3294 * This will make sure that pinned stack always stays on top.
3295 * @param requestedPosition Position requested by caller.
3296 * @param stack Stack to be added or positioned.
3297 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3298 * @return The proper position for the stack.
3299 */
3300 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3301 final int topChildPosition = mChildren.size() - 1;
3302 boolean toTop = requestedPosition == POSITION_TOP;
3303 toTop |= adding ? requestedPosition >= topChildPosition + 1
3304 : requestedPosition >= topChildPosition;
3305 int targetPosition = requestedPosition;
3306
Bryce Lee48f4b572017-04-10 10:54:15 -07003307 if (toTop && stack.mStackId != PINNED_STACK_ID
3308 && getStackById(PINNED_STACK_ID) != null) {
3309 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003310 TaskStack topStack = mChildren.get(topChildPosition);
3311 if (topStack.mStackId != PINNED_STACK_ID) {
3312 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3313 }
3314
3315 // So, stack is moved just below the pinned stack.
3316 // When we're adding a new stack the target is the current pinned stack position.
3317 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003318 // stack, because WindowContainer#positionAt() first removes element and then adds
3319 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003320 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3321 }
3322
3323 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003324 }
3325
3326 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003327 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3328 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003329 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003330 if (super.forAllWindows(callback, traverseTopToBottom)) {
3331 return true;
3332 }
3333 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3334 return true;
3335 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003336 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003337 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3338 return true;
3339 }
3340 if (super.forAllWindows(callback, traverseTopToBottom)) {
3341 return true;
3342 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003343 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003344 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003345 }
3346
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003347 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003348 boolean traverseTopToBottom) {
3349 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3350 // app tokens.
3351 // TODO: Investigate if we need to continue to do this or if we can just process them
3352 // in-order.
3353 if (traverseTopToBottom) {
3354 for (int i = mChildren.size() - 1; i >= 0; --i) {
3355 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3356 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003357 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3358 traverseTopToBottom)) {
3359 return true;
3360 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003361 }
3362 }
3363 } else {
3364 final int count = mChildren.size();
3365 for (int i = 0; i < count; ++i) {
3366 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3367 final int appTokensCount = appTokens.size();
3368 for (int j = 0; j < appTokensCount; 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 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003376 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003377 }
3378
Wale Ogunwale1666e312016-12-16 11:27:18 -08003379 void setExitingTokensHasVisible(boolean hasVisible) {
3380 for (int i = mChildren.size() - 1; i >= 0; --i) {
3381 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3382 for (int j = appTokens.size() - 1; j >= 0; --j) {
3383 appTokens.get(j).hasVisible = hasVisible;
3384 }
3385 }
3386 }
3387
3388 void removeExistingAppTokensIfPossible() {
3389 for (int i = mChildren.size() - 1; i >= 0; --i) {
3390 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3391 for (int j = appTokens.size() - 1; j >= 0; --j) {
3392 final AppWindowToken token = appTokens.get(j);
3393 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3394 && (!token.mIsExiting || token.isEmpty())) {
3395 // Make sure there is no animation running on this token, so any windows
3396 // associated with it will be removed as soon as their animations are
3397 // complete.
3398 token.mAppAnimator.clearAnimation();
3399 token.mAppAnimator.animating = false;
3400 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3401 "performLayout: App token exiting now removed" + token);
3402 token.removeIfPossible();
3403 }
3404 }
3405 }
3406 }
3407
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003408 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003409 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003410 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003411 // Apps and their containers are not allowed to specify an orientation while the
3412 // docked or freeform stack is visible...except for the home stack/task if the
3413 // docked stack is minimized and it actually set something.
3414 if (mHomeStack != null && mHomeStack.isVisible()
3415 && mDividerControllerLocked.isMinimizedDock()) {
3416 final int orientation = mHomeStack.getOrientation();
3417 if (orientation != SCREEN_ORIENTATION_UNSET) {
3418 return orientation;
3419 }
3420 }
3421 return SCREEN_ORIENTATION_UNSPECIFIED;
3422 }
3423
3424 final int orientation = super.getOrientation();
3425 if (orientation != SCREEN_ORIENTATION_UNSET
3426 && orientation != SCREEN_ORIENTATION_BEHIND) {
3427 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3428 "App is requesting an orientation, return " + orientation);
3429 return orientation;
3430 }
3431
3432 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003433 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003434 // The next app has not been requested to be visible, so we keep the current orientation
3435 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003436 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003437 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003438 }
3439
3440 /**
3441 * Window container class that contains all containers on this display that are not related to
3442 * Apps. E.g. status bar.
3443 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003444 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3445 /**
3446 * Compares two child window tokens returns -1 if the first is lesser than the second in
3447 * terms of z-order and 1 otherwise.
3448 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003449 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003450 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003451 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3452 token1.mOwnerCanManageAppTokens)
3453 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3454 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003455
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003456 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3457 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3458 return false;
3459 }
3460 final int req = w.mAttrs.screenOrientation;
3461 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3462 || req == SCREEN_ORIENTATION_UNSET) {
3463 return false;
3464 }
3465 return true;
3466 };
3467
Wale Ogunwale3a931692016-11-02 16:49:48 -07003468 private final String mName;
3469 NonAppWindowContainers(String name) {
3470 mName = name;
3471 }
3472
3473 void addChild(WindowToken token) {
3474 addChild(token, mWindowComparator);
3475 }
3476
3477 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003478 int getOrientation() {
3479 final WindowManagerPolicy policy = mService.mPolicy;
3480 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003481 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003482
3483 if (win != null) {
3484 final int req = win.mAttrs.screenOrientation;
3485 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
3486 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003487 mLastKeyguardForcedOrientation = req;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003488 }
Andrii Kulian8ee72852017-03-10 10:36:45 -08003489 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003490 }
3491
Andrii Kulian8ee72852017-03-10 10:36:45 -08003492 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003493
3494 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003495 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003496 }
3497
3498 return SCREEN_ORIENTATION_UNSET;
3499 }
3500
3501 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003502 String getName() {
3503 return mName;
3504 }
Robert Carr3b716242016-08-16 16:02:21 -07003505 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003506
3507 /**
3508 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3509 */
3510 @FunctionalInterface
3511 private interface Screenshoter<E> {
3512 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3513 boolean useIdentityTransform, int rotation);
3514 }
Craig Mautner59c00972012-07-30 12:10:24 -07003515}