blob: 058fdae17f9fdefbe27d7e076bad58071ca5860d [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
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800128import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700129import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700130
Robert Carr3b716242016-08-16 16:02:21 -0700131import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700132import java.io.PrintWriter;
133import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700134import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700135import java.util.HashMap;
136import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700137import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700138import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800139import java.util.function.Consumer;
140import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700141
Craig Mautner59c00972012-07-30 12:10:24 -0700142/**
143 * Utility class for keeping track of the WindowStates and other pertinent contents of a
144 * particular Display.
145 *
146 * IMPORTANT: No method from this class should ever be used without holding
147 * WindowManagerService.mWindowMap.
148 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700149class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700150 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700151
152 /** Unique identifier of this stack. */
153 private final int mDisplayId;
154
Wale Ogunwale3a931692016-11-02 16:49:48 -0700155 /** The containers below are the only child containers the display can have. */
156 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700157 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700158 // Contains all non-app window containers that should be displayed above the app containers
159 // (e.g. Status bar)
160 private final NonAppWindowContainers mAboveAppWindowsContainers =
161 new NonAppWindowContainers("mAboveAppWindowsContainers");
162 // Contains all non-app window containers that should be displayed below the app containers
163 // (e.g. Wallpaper).
164 private final NonAppWindowContainers mBelowAppWindowsContainers =
165 new NonAppWindowContainers("mBelowAppWindowsContainers");
166 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
167 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
168 // window containers together and move them in-sync if/when needed.
169 private final NonAppWindowContainers mImeWindowsContainers =
170 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700171
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000172 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800173 private WindowState mTmpWindow2;
174 private WindowAnimator mTmpWindowAnimator;
175 private boolean mTmpRecoveringMemory;
176 private boolean mUpdateImeTarget;
177 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700178 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700179
Wale Ogunwale02319a62016-09-26 15:21:22 -0700180 // Mapping from a token IBinder to a WindowToken object on this display.
181 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
182
Andrii Kuliancd097992017-03-23 18:31:59 -0700183 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700184 int mInitialDisplayWidth = 0;
185 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700186 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700187
188 /**
189 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
190 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
191 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
192 */
Craig Mautner59c00972012-07-30 12:10:24 -0700193 int mBaseDisplayWidth = 0;
194 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700195 /**
196 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
197 * but can be set from Settings or via shell command "adb shell wm density".
198 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
199 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700200 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700201 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700202 private final DisplayInfo mDisplayInfo = new DisplayInfo();
203 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700204 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700205 /**
206 * For default display it contains real metrics, empty for others.
207 * @see WindowManagerService#createWatermarkInTransaction()
208 */
209 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
210 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
211 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
212 /**
213 * Compat metrics computed based on {@link #mDisplayMetrics}.
214 * @see #updateDisplayAndOrientation(int)
215 */
216 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
217
218 /** The desired scaling factor for compatible apps. */
219 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700220
Andrii Kulian8ee72852017-03-10 10:36:45 -0800221 /**
222 * Current rotation of the display.
223 * Constants as per {@link android.view.Surface.Rotation}.
224 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700225 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800226 */
227 private int mRotation = 0;
228 /**
229 * Last applied orientation of the display.
230 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
231 *
232 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
233 */
234 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
235 /**
236 * Flag indicating that the application is receiving an orientation that has different metrics
237 * than it expected. E.g. Portrait instead of Landscape.
238 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700239 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800240 */
241 private boolean mAltOrientation = false;
242 /**
243 * Orientation forced by some window. If there is no visible window that specifies orientation
244 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
245 *
246 * @see NonAppWindowContainers#getOrientation()
247 */
248 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
249 /**
250 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
251 * occluded.
252 *
253 * @see NonAppWindowContainers#getOrientation()
254 */
255 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
256
Andrii Kulian06d07d62017-03-14 11:11:47 -0700257 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700258 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700259
Craig Mautner39834192012-09-02 07:47:24 -0700260 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700261 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700262 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700263 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800264 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700265
Craig Mautnerdc548482014-02-05 13:35:24 -0800266 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700267 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800268
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800269 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
270 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700271 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700272
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700273 /** Detect user tapping outside of current focused task bounds .*/
274 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700275
Craig Mautner6601b7b2013-04-29 10:29:11 -0700276 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700277 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700278
Craig Mautner6601b7b2013-04-29 10:29:11 -0700279 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800280 private final Rect mTmpRect = new Rect();
281 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700282 private final RectF mTmpRectF = new RectF();
283 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800284 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700285
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800286 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700287
Craig Mautner95da1082014-02-24 17:54:35 -0800288 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700289 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800290
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700291 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700292 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700293
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800294 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700295
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800296 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
297
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000298 private boolean mHaveBootMsg = false;
299 private boolean mHaveApp = false;
300 private boolean mHaveWallpaper = false;
301 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700302
303 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
304
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700305 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
306 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000307 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
308 new ApplySurfaceChangesTransactionState();
309 private final ScreenshotApplicationState mScreenshotApplicationState =
310 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700311
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700312 // True if this display is in the process of being removed. Used to determine if the removal of
313 // the display's direct children should be allowed.
314 private boolean mRemovingDisplay = false;
315
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700316 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800317 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700318 int mInputMethodAnimLayerAdjustment;
319
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800320 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
321 WindowStateAnimator winAnimator = w.mWinAnimator;
322 if (winAnimator.hasSurface()) {
323 final boolean wasAnimating = winAnimator.mWasAnimating;
324 final boolean nowAnimating = winAnimator.stepAnimationLocked(
325 mTmpWindowAnimator.mCurrentTime);
326 winAnimator.mWasAnimating = nowAnimating;
327 mTmpWindowAnimator.orAnimating(nowAnimating);
328
329 if (DEBUG_WALLPAPER) Slog.v(TAG,
330 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
331
332 if (wasAnimating && !winAnimator.mAnimating
333 && mWallpaperController.isWallpaperTarget(w)) {
334 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
335 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
336 if (DEBUG_LAYOUT_REPEATS) {
337 mService.mWindowPlacerLocked.debugLayoutRepeats(
338 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
339 }
340 }
341 }
342
343 final AppWindowToken atoken = w.mAppToken;
344 if (winAnimator.mDrawState == READY_TO_SHOW) {
345 if (atoken == null || atoken.allDrawn) {
346 if (w.performShowLocked()) {
347 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
348 if (DEBUG_LAYOUT_REPEATS) {
349 mService.mWindowPlacerLocked.debugLayoutRepeats(
350 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
351 }
352 }
353 }
354 }
355 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
356 if (appAnimator != null && appAnimator.thumbnail != null) {
357 if (appAnimator.thumbnailTransactionSeq
358 != mTmpWindowAnimator.mAnimTransactionSequence) {
359 appAnimator.thumbnailTransactionSeq =
360 mTmpWindowAnimator.mAnimTransactionSequence;
361 appAnimator.thumbnailLayer = 0;
362 }
363 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
364 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
365 }
366 }
367 };
368
369 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
370 final WindowStateAnimator winAnimator = w.mWinAnimator;
371 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
372 return;
373 }
374
375 final int flags = w.mAttrs.flags;
376
377 // If this window is animating, make a note that we have an animating window and take
378 // care of a request to run a detached wallpaper animation.
379 if (winAnimator.mAnimating) {
380 if (winAnimator.mAnimation != null) {
381 if ((flags & FLAG_SHOW_WALLPAPER) != 0
382 && winAnimator.mAnimation.getDetachWallpaper()) {
383 mTmpWindow = w;
384 }
385 final int color = winAnimator.mAnimation.getBackgroundColor();
386 if (color != 0) {
387 final TaskStack stack = w.getStack();
388 if (stack != null) {
389 stack.setAnimationBackground(winAnimator, color);
390 }
391 }
392 }
393 mTmpWindowAnimator.setAnimating(true);
394 }
395
396 // If this window's app token is running a detached wallpaper animation, make a note so
397 // we can ensure the wallpaper is displayed behind it.
398 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
399 if (appAnimator != null && appAnimator.animation != null
400 && appAnimator.animating) {
401 if ((flags & FLAG_SHOW_WALLPAPER) != 0
402 && appAnimator.animation.getDetachWallpaper()) {
403 mTmpWindow = w;
404 }
405
406 final int color = appAnimator.animation.getBackgroundColor();
407 if (color != 0) {
408 final TaskStack stack = w.getStack();
409 if (stack != null) {
410 stack.setAnimationBackground(winAnimator, color);
411 }
412 }
413 }
414 };
415
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800416 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
417 final int lostFocusUid = mTmpWindow.mOwnerUid;
418 final Handler handler = mService.mH;
419 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
420 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
421 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
422 w.mAttrs.hideTimeoutMilliseconds);
423 }
424 }
425 };
426
427 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
428 final AppWindowToken focusedApp = mService.mFocusedApp;
429 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
430 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
431
432 if (!w.canReceiveKeys()) {
433 return false;
434 }
435
436 final AppWindowToken wtoken = w.mAppToken;
437
438 // If this window's application has been removed, just skip it.
439 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
440 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
441 + (wtoken.removed ? "removed" : "sendingToBottom"));
442 return false;
443 }
444
445 if (focusedApp == null) {
446 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
447 + " using new focus @ " + w);
448 mTmpWindow = w;
449 return true;
450 }
451
452 if (!focusedApp.windowsAreFocusable()) {
453 // Current focused app windows aren't focusable...
454 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
455 + " focusable using new focus @ " + w);
456 mTmpWindow = w;
457 return true;
458 }
459
460 // Descend through all of the app tokens and find the first that either matches
461 // win.mAppToken (return win) or mFocusedApp (return null).
462 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
463 if (focusedApp.compareTo(wtoken) > 0) {
464 // App stack below focused app stack. No focus for you!!!
465 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
466 "findFocusedWindow: Reached focused app=" + focusedApp);
467 mTmpWindow = null;
468 return true;
469 }
470 }
471
472 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
473 mTmpWindow = w;
474 return true;
475 };
476
477 private final Consumer<WindowState> mPrepareWindowSurfaces =
478 w -> w.mWinAnimator.prepareSurfaceLocked(true);
479
480 private final Consumer<WindowState> mPerformLayout = w -> {
481 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
482 // wasting time and funky changes while a window is animating away.
483 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
484 || w.isGoneForLayoutLw();
485
486 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
487 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
488 + " mLayoutAttached=" + w.mLayoutAttached
489 + " screen changed=" + w.isConfigChanged());
490 final AppWindowToken atoken = w.mAppToken;
491 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
492 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
493 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
494 + " parentHidden=" + w.isParentWindowHidden());
495 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
496 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
497 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
498 + " parentHidden=" + w.isParentWindowHidden());
499 }
500
501 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
502 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
503 // since that means "perform layout as normal, just don't display").
504 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
505 || ((w.isConfigChanged() || w.setReportResizeHints())
506 && !w.isGoneForLayoutLw() &&
507 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
508 (w.mHasSurface && w.mAppToken != null &&
509 w.mAppToken.layoutConfigChanges)))) {
510 if (!w.mLayoutAttached) {
511 if (mTmpInitial) {
512 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
513 w.mContentChanged = false;
514 }
515 if (w.mAttrs.type == TYPE_DREAM) {
516 // Don't layout windows behind a dream, so that if it does stuff like hide
517 // the status bar we won't get a bad transition when it goes away.
518 mTmpWindow = w;
519 }
520 w.mLayoutNeeded = false;
521 w.prelayout();
522 mService.mPolicy.layoutWindowLw(w, null);
523 w.mLayoutSeq = mService.mLayoutSeq;
524
525 // Window frames may have changed. Update dim layer with the new bounds.
526 final Task task = w.getTask();
527 if (task != null) {
528 mDimLayerController.updateDimLayer(task);
529 }
530
531 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
532 + " mContainingFrame=" + w.mContainingFrame
533 + " mDisplayFrame=" + w.mDisplayFrame);
534 }
535 }
536 };
537
538 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
539 if (w.mLayoutAttached) {
540 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
541 + " mViewVisibility=" + w.mViewVisibility
542 + " mRelayoutCalled=" + w.mRelayoutCalled);
543 // If this view is GONE, then skip it -- keep the current frame, and let the caller
544 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
545 // windows, since that means "perform layout as normal, just don't display").
546 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
547 return;
548 }
549 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
550 || w.mLayoutNeeded) {
551 if (mTmpInitial) {
552 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
553 w.mContentChanged = false;
554 }
555 w.mLayoutNeeded = false;
556 w.prelayout();
557 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
558 w.mLayoutSeq = mService.mLayoutSeq;
559 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
560 + " mContainingFrame=" + w.mContainingFrame
561 + " mDisplayFrame=" + w.mDisplayFrame);
562 }
563 } else if (w.mAttrs.type == TYPE_DREAM) {
564 // Don't layout windows behind a dream, so that if it does stuff like hide the
565 // status bar we won't get a bad transition when it goes away.
566 mTmpWindow = mTmpWindow2;
567 }
568 };
569
570 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
571 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
572 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
573 return w.canBeImeTarget();
574 };
575
576 private final Consumer<WindowState> mApplyPostLayoutPolicy =
577 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
578 mService.mInputMethodTarget);
579
580 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
581 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
582 final boolean obscuredChanged = w.mObscured !=
583 mTmpApplySurfaceChangesTransactionState.obscured;
584 final RootWindowContainer root = mService.mRoot;
585 // Only used if default window
586 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
587
588 // Update effect.
589 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
590 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
591 final boolean isDisplayed = w.isDisplayedLw();
592
593 if (isDisplayed && w.isObscuringDisplay()) {
594 // This window completely covers everything behind it, so we want to leave all
595 // of them as undimmed (for performance reasons).
596 root.mObscuringWindow = w;
597 mTmpApplySurfaceChangesTransactionState.obscured = true;
598 }
599
600 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
601 root.handleNotObscuredLocked(w,
602 mTmpApplySurfaceChangesTransactionState.obscured,
603 mTmpApplySurfaceChangesTransactionState.syswin);
604
605 if (w.mHasSurface && isDisplayed) {
606 final int type = w.mAttrs.type;
607 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
608 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
609 mTmpApplySurfaceChangesTransactionState.syswin = true;
610 }
611 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
612 && w.mAttrs.preferredRefreshRate != 0) {
613 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
614 = w.mAttrs.preferredRefreshRate;
615 }
616 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
617 && w.mAttrs.preferredDisplayModeId != 0) {
618 mTmpApplySurfaceChangesTransactionState.preferredModeId
619 = w.mAttrs.preferredDisplayModeId;
620 }
621 }
622 }
623
624 w.applyDimLayerIfNeeded();
625
626 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
627 && mWallpaperController.isWallpaperTarget(w)) {
628 // This is the wallpaper target and its obscured state changed... make sure the
629 // current wallpaper's visibility has been updated accordingly.
630 mWallpaperController.updateWallpaperVisibility();
631 }
632
633 w.handleWindowMovedIfNeeded();
634
635 final WindowStateAnimator winAnimator = w.mWinAnimator;
636
637 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
638 w.mContentChanged = false;
639
640 // Moved from updateWindowsAndWallpaperLocked().
641 if (w.mHasSurface) {
642 // Take care of the window being ready to display.
643 final boolean committed = winAnimator.commitFinishDrawingLocked();
644 if (isDefaultDisplay && committed) {
645 if (w.mAttrs.type == TYPE_DREAM) {
646 // HACK: When a dream is shown, it may at that point hide the lock screen.
647 // So we need to redo the layout to let the phone window manager make this
648 // happen.
649 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
650 if (DEBUG_LAYOUT_REPEATS) {
651 surfacePlacer.debugLayoutRepeats(
652 "dream and commitFinishDrawingLocked true",
653 pendingLayoutChanges);
654 }
655 }
656 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
657 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
658 "First draw done in potential wallpaper target " + w);
659 root.mWallpaperMayChange = true;
660 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
661 if (DEBUG_LAYOUT_REPEATS) {
662 surfacePlacer.debugLayoutRepeats(
663 "wallpaper and commitFinishDrawingLocked true",
664 pendingLayoutChanges);
665 }
666 }
667 }
Robert Carr0c901692017-04-05 16:11:45 -0700668 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) ||
669 winAnimator.isDummyAnimation()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800670 // Updates the shown frame before we set up the surface. This is needed
671 // because the resizing could change the top-left position (in addition to
672 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
673 // position the surface.
674 //
675 // If an animation is being started, we can't call this method because the
676 // animation hasn't processed its initial transformation yet, but in general
677 // we do want to update the position if the window is animating.
678 winAnimator.computeShownFrameLocked();
679 }
680 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
681 }
682
683 final AppWindowToken atoken = w.mAppToken;
684 if (atoken != null) {
685 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
686 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
687 mTmpUpdateAllDrawn.add(atoken);
688 }
689 }
690
691 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
692 && w.isDisplayedLw()) {
693 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
694 }
695
696 w.updateResizingWindowIfNeeded();
697 };
698
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800699 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800700 * Create new {@link DisplayContent} instance, add itself to the root window container and
701 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700702 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800703 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700704 * @param layersController window layer controller used to assign layer to the windows on this
705 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700706 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
707 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700708 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700709 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700710 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800711
712 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
713 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
714 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
715 + " new=" + display);
716 }
717
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700718 mDisplay = display;
719 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700720 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700721 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700722 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700723 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700724 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700725 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700726 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800727 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700728 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800729 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700730
731 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700732 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700733 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700734 super.addChild(mAboveAppWindowsContainers, null);
735 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800736
737 // Add itself as a child to the root container.
738 mService.mRoot.addChild(this, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700739 }
740
741 int getDisplayId() {
742 return mDisplayId;
743 }
744
Wale Ogunwale02319a62016-09-26 15:21:22 -0700745 WindowToken getWindowToken(IBinder binder) {
746 return mTokenMap.get(binder);
747 }
748
749 AppWindowToken getAppWindowToken(IBinder binder) {
750 final WindowToken token = getWindowToken(binder);
751 if (token == null) {
752 return null;
753 }
754 return token.asAppWindowToken();
755 }
756
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800757 void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700758 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
759 if (dc != null) {
760 // We currently don't support adding a window token to the display if the display
761 // already has the binder mapped to another token. If there is a use case for supporting
762 // this moving forward we will either need to merge the WindowTokens some how or have
763 // the binder map to a list of window tokens.
764 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
765 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
766 }
767 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700768
769 if (token.asAppWindowToken() == null) {
770 // Add non-app token to container hierarchy on the display. App tokens are added through
771 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700772 switch (token.windowType) {
773 case TYPE_WALLPAPER:
774 mBelowAppWindowsContainers.addChild(token);
775 break;
776 case TYPE_INPUT_METHOD:
777 case TYPE_INPUT_METHOD_DIALOG:
778 mImeWindowsContainers.addChild(token);
779 break;
780 default:
781 mAboveAppWindowsContainers.addChild(token);
782 break;
783 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700784 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700785 }
786
787 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700788 final WindowToken token = mTokenMap.remove(binder);
789 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800790 token.setExiting();
791 }
792 return token;
793 }
794
795 /** Changes the display the input window token is housed on to this one. */
796 void reParentWindowToken(WindowToken token) {
797 final DisplayContent prevDc = token.getDisplayContent();
798 if (prevDc == this) {
799 return;
800 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800801 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
802 && token.asAppWindowToken() == null) {
803 // Removed the token from the map, but made sure it's not an app token before removing
804 // from parent.
805 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700806 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800807
808 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700809 }
810
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700811 void removeAppToken(IBinder binder) {
812 final WindowToken token = removeWindowToken(binder);
813 if (token == null) {
814 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
815 return;
816 }
817
818 final AppWindowToken appToken = token.asAppWindowToken();
819
820 if (appToken == null) {
821 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
822 return;
823 }
824
825 appToken.onRemovedFromDisplay();
826 }
827
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700828 Display getDisplay() {
829 return mDisplay;
830 }
831
Craig Mautner59c00972012-07-30 12:10:24 -0700832 DisplayInfo getDisplayInfo() {
833 return mDisplayInfo;
834 }
835
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700836 DisplayMetrics getDisplayMetrics() {
837 return mDisplayMetrics;
838 }
839
Andrii Kulian8ee72852017-03-10 10:36:45 -0800840 int getRotation() {
841 return mRotation;
842 }
843
844 void setRotation(int newRotation) {
845 mRotation = newRotation;
846 }
847
848 int getLastOrientation() {
849 return mLastOrientation;
850 }
851
852 void setLastOrientation(int orientation) {
853 mLastOrientation = orientation;
854 }
855
856 boolean getAltOrientation() {
857 return mAltOrientation;
858 }
859
860 void setAltOrientation(boolean altOrientation) {
861 mAltOrientation = altOrientation;
862 }
863
864 int getLastWindowForcedOrientation() {
865 return mLastWindowForcedOrientation;
866 }
867
Andrii Kulian06d07d62017-03-14 11:11:47 -0700868 /**
869 * Update rotation of the display.
870 *
871 * Returns true if the rotation has been changed. In this case YOU MUST CALL
872 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
873 */
874 boolean updateRotationUnchecked(boolean inTransaction) {
875 if (mService.mDeferredRotationPauseCount > 0) {
876 // Rotation updates have been paused temporarily. Defer the update until
877 // updates have been resumed.
878 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
879 return false;
880 }
881
882 ScreenRotationAnimation screenRotationAnimation =
883 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
884 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
885 // Rotation updates cannot be performed while the previous rotation change
886 // animation is still in progress. Skip this update. We will try updating
887 // again after the animation is finished and the display is unfrozen.
888 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
889 return false;
890 }
891 if (mService.mDisplayFrozen) {
892 // Even if the screen rotation animation has finished (e.g. isAnimating
893 // returns false), there is still some time where we haven't yet unfrozen
894 // the display. We also need to abort rotation here.
895 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
896 "Deferring rotation, still finishing previous rotation");
897 return false;
898 }
899
900 if (!mService.mDisplayEnabled) {
901 // No point choosing a rotation if the display is not enabled.
902 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
903 return false;
904 }
905
906 final int oldRotation = mRotation;
907 final int lastOrientation = mLastOrientation;
908 final boolean oldAltOrientation = mAltOrientation;
909 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700910 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
911 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700912
Robert Carr897215d2017-03-29 12:25:50 -0700913 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700914 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
915 if (seamlessRotated != null) {
916 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
917 // to complete (that is, waiting for windows to redraw). It's tempting to check
918 // w.mSeamlessRotationCount but that could be incorrect in the case of
919 // window-removal.
920 return false;
921 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700922 }
923
924 // TODO: Implement forced rotation changes.
925 // Set mAltOrientation to indicate that the application is receiving
926 // an orientation that has different metrics than it expected.
927 // eg. Portrait instead of Landscape.
928
929 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
930 lastOrientation, rotation);
931
932 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
933 + ", got rotation " + rotation + " which has "
934 + (altOrientation ? "incompatible" : "compatible") + " metrics");
935
936 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
937 // No change.
938 return false;
939 }
940
941 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
942 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
943 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
944
945 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
946 mService.mWaitingForConfig = true;
947 }
948
949 mRotation = rotation;
950 mAltOrientation = altOrientation;
951 if (isDefaultDisplay) {
952 mService.mPolicy.setRotationLw(rotation);
953 }
954
955 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
956 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
957 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
958 WINDOW_FREEZE_TIMEOUT_DURATION);
959
960 setLayoutNeeded();
961 final int[] anim = new int[2];
962 if (isDimming()) {
963 anim[0] = anim[1] = 0;
964 } else {
965 mService.mPolicy.selectRotationAnimationLw(anim);
966 }
967
968 if (!rotateSeamlessly) {
969 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1]);
970 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
971 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
972 mDisplayId);
973 } else {
974 // The screen rotation animation uses a screenshot to freeze the screen
975 // while windows resize underneath.
976 // When we are rotating seamlessly, we allow the elements to transition
977 // to their rotated state independently and without a freeze required.
978 screenRotationAnimation = null;
979
980 // We have to reset this in case a window was removed before it
981 // finished seamless rotation.
982 mService.mSeamlessRotationCount = 0;
983 }
984
985 // We need to update our screen size information to match the new rotation. If the rotation
986 // has actually changed then this method will return true and, according to the comment at
987 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
988 // By updating the Display info here it will be available to
989 // #computeScreenConfiguration() later.
990 updateDisplayAndOrientation(getConfiguration().uiMode);
991
992 if (!inTransaction) {
993 if (SHOW_TRANSACTIONS) {
994 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
995 }
996 mService.openSurfaceTransaction();
997 }
998 try {
999 // NOTE: We disable the rotation in the emulator because
1000 // it doesn't support hardware OpenGL emulation yet.
1001 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1002 && screenRotationAnimation.hasScreenshot()) {
1003 if (screenRotationAnimation.setRotationInTransaction(
1004 rotation, mService.mFxSession,
1005 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1006 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1007 mService.scheduleAnimationLocked();
1008 }
1009 }
1010
1011 if (rotateSeamlessly) {
1012 forAllWindows(w -> {
1013 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1014 }, true /* traverseTopToBottom */);
1015 }
1016
1017 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1018 } finally {
1019 if (!inTransaction) {
1020 mService.closeSurfaceTransaction();
1021 if (SHOW_LIGHT_TRANSACTIONS) {
1022 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1023 }
1024 }
1025 }
1026
1027 forAllWindows(w -> {
1028 // Discard surface after orientation change, these can't be reused.
1029 if (w.mAppToken != null) {
1030 w.mAppToken.destroySavedSurfaces();
1031 }
1032 if (w.mHasSurface && !rotateSeamlessly) {
1033 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
1034 w.mOrientationChanging = true;
1035 mService.mRoot.mOrientationChangeComplete = false;
1036 w.mLastFreezeDuration = 0;
1037 }
1038 w.mReportOrientationChanged = true;
1039 }, true /* traverseTopToBottom */);
1040
1041 if (rotateSeamlessly) {
1042 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1043 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1044 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1045 }
1046
1047 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1048 final WindowManagerService.RotationWatcher rotationWatcher
1049 = mService.mRotationWatchers.get(i);
1050 if (rotationWatcher.mDisplayId == mDisplayId) {
1051 try {
1052 rotationWatcher.mWatcher.onRotationChanged(rotation);
1053 } catch (RemoteException e) {
1054 // Ignore
1055 }
1056 }
1057 }
1058
1059 // TODO (multi-display): Magnification is supported only for the default display.
1060 // Announce rotation only if we will not animate as we already have the
1061 // windows in final state. Otherwise, we make this call at the rotation end.
1062 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1063 && isDefaultDisplay) {
1064 mService.mAccessibilityController.onRotationChangedLocked(this);
1065 }
1066
1067 return true;
1068 }
1069
1070 /**
1071 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1072 * changed.
1073 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1074 */
1075 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1076 // Use the effective "visual" dimensions based on current rotation
1077 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1078 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1079 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1080 int dw = realdw;
1081 int dh = realdh;
1082
1083 if (mAltOrientation) {
1084 if (realdw > realdh) {
1085 // Turn landscape into portrait.
1086 int maxw = (int)(realdh/1.3f);
1087 if (maxw < realdw) {
1088 dw = maxw;
1089 }
1090 } else {
1091 // Turn portrait into landscape.
1092 int maxh = (int)(realdw/1.3f);
1093 if (maxh < realdh) {
1094 dh = maxh;
1095 }
1096 }
1097 }
1098
1099 // Update application display metrics.
1100 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1101 mDisplayId);
1102 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1103 mDisplayId);
1104 mDisplayInfo.rotation = mRotation;
1105 mDisplayInfo.logicalWidth = dw;
1106 mDisplayInfo.logicalHeight = dh;
1107 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1108 mDisplayInfo.appWidth = appWidth;
1109 mDisplayInfo.appHeight = appHeight;
1110 if (isDefaultDisplay) {
1111 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1112 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1113 }
1114 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1115 if (mDisplayScalingDisabled) {
1116 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1117 } else {
1118 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1119 }
1120
1121 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1122 mDisplayInfo);
1123
1124 mBaseDisplayRect.set(0, 0, dw, dh);
1125
1126 if (isDefaultDisplay) {
1127 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1128 mCompatDisplayMetrics);
1129 }
1130 return mDisplayInfo;
1131 }
1132
1133 /**
1134 * Compute display configuration based on display properties and policy settings.
1135 * Do not call if mDisplayReady == false.
1136 */
1137 void computeScreenConfiguration(Configuration config) {
1138 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1139
1140 final int dw = displayInfo.logicalWidth;
1141 final int dh = displayInfo.logicalHeight;
1142 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1143 Configuration.ORIENTATION_LANDSCAPE;
1144 config.screenWidthDp =
1145 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1146 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1147 config.screenHeightDp =
1148 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1149 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001150
1151 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1152 final int leftInset = mTmpRect.left;
1153 final int topInset = mTmpRect.top;
1154 // appBounds at the root level should mirror the app screen size.
1155 config.setAppBounds(leftInset /*left*/, topInset /*top*/, leftInset + displayInfo.appWidth /*right*/,
1156 topInset + displayInfo.appHeight /*bottom*/);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001157 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1158 || displayInfo.rotation == Surface.ROTATION_270);
1159
1160 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1161 mDisplayMetrics.density, config);
1162
1163 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1164 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1165 ? Configuration.SCREENLAYOUT_ROUND_YES
1166 : Configuration.SCREENLAYOUT_ROUND_NO);
1167
1168 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1169 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1170 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1171 dh, mDisplayId);
1172 config.densityDpi = displayInfo.logicalDensityDpi;
1173
1174 config.colorMode =
1175 (displayInfo.isHdr()
1176 ? Configuration.COLOR_MODE_HDR_YES
1177 : Configuration.COLOR_MODE_HDR_NO)
1178 | (displayInfo.isWideColorGamut()
1179 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1180 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1181
1182 // Update the configuration based on available input devices, lid switch,
1183 // and platform configuration.
1184 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1185 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1186 config.navigation = Configuration.NAVIGATION_NONAV;
1187
1188 int keyboardPresence = 0;
1189 int navigationPresence = 0;
1190 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1191 final int len = devices != null ? devices.length : 0;
1192 for (int i = 0; i < len; i++) {
1193 InputDevice device = devices[i];
1194 if (!device.isVirtual()) {
1195 final int sources = device.getSources();
1196 final int presenceFlag = device.isExternal() ?
1197 WindowManagerPolicy.PRESENCE_EXTERNAL :
1198 WindowManagerPolicy.PRESENCE_INTERNAL;
1199
1200 // TODO(multi-display): Configure on per-display basis.
1201 if (mService.mIsTouchDevice) {
1202 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1203 InputDevice.SOURCE_TOUCHSCREEN) {
1204 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1205 }
1206 } else {
1207 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1208 }
1209
1210 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1211 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1212 navigationPresence |= presenceFlag;
1213 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1214 && config.navigation == Configuration.NAVIGATION_NONAV) {
1215 config.navigation = Configuration.NAVIGATION_DPAD;
1216 navigationPresence |= presenceFlag;
1217 }
1218
1219 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1220 config.keyboard = Configuration.KEYBOARD_QWERTY;
1221 keyboardPresence |= presenceFlag;
1222 }
1223 }
1224 }
1225
1226 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1227 config.navigation = Configuration.NAVIGATION_DPAD;
1228 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1229 }
1230
1231 // Determine whether a hard keyboard is available and enabled.
1232 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1233 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1234 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1235 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1236 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1237 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1238 }
1239
1240 // Let the policy update hidden states.
1241 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1242 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1243 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1244 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1245 }
1246
1247 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1248 int displayId) {
1249 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1250 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1251 final int unrotDw, unrotDh;
1252 if (rotated) {
1253 unrotDw = dh;
1254 unrotDh = dw;
1255 } else {
1256 unrotDw = dw;
1257 unrotDh = dh;
1258 }
1259 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1260 displayId);
1261 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1262 displayId);
1263 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1264 displayId);
1265 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1266 displayId);
1267 return sw;
1268 }
1269
1270 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1271 DisplayMetrics dm, int dw, int dh, int displayId) {
1272 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1273 displayId);
1274 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1275 uiMode, displayId);
1276 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1277 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1278 if (curSize == 0 || size < curSize) {
1279 curSize = size;
1280 }
1281 return curSize;
1282 }
1283
1284 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1285 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1286
1287 // We need to determine the smallest width that will occur under normal
1288 // operation. To this, start with the base screen size and compute the
1289 // width under the different possible rotations. We need to un-rotate
1290 // the current screen dimensions before doing this.
1291 int unrotDw, unrotDh;
1292 if (rotated) {
1293 unrotDw = dh;
1294 unrotDh = dw;
1295 } else {
1296 unrotDw = dw;
1297 unrotDh = dh;
1298 }
1299 displayInfo.smallestNominalAppWidth = 1<<30;
1300 displayInfo.smallestNominalAppHeight = 1<<30;
1301 displayInfo.largestNominalAppWidth = 0;
1302 displayInfo.largestNominalAppHeight = 0;
1303 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1304 unrotDh);
1305 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1306 unrotDw);
1307 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1308 unrotDh);
1309 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1310 unrotDw);
1311 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1312 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1313 displayId);
1314 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1315 displayId);
1316 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1317 displayId);
1318 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1319 displayId);
1320 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1321 outConfig.screenLayout = sl;
1322 }
1323
1324 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1325 int uiMode, int displayId) {
1326 // Get the app screen size at this rotation.
1327 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1328 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1329
1330 // Compute the screen layout size class for this rotation.
1331 int longSize = w;
1332 int shortSize = h;
1333 if (longSize < shortSize) {
1334 int tmp = longSize;
1335 longSize = shortSize;
1336 shortSize = tmp;
1337 }
1338 longSize = (int)(longSize/density);
1339 shortSize = (int)(shortSize/density);
1340 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1341 }
1342
1343 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1344 int uiMode, int dw, int dh) {
1345 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1346 displayId);
1347 if (width < displayInfo.smallestNominalAppWidth) {
1348 displayInfo.smallestNominalAppWidth = width;
1349 }
1350 if (width > displayInfo.largestNominalAppWidth) {
1351 displayInfo.largestNominalAppWidth = width;
1352 }
1353 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1354 displayId);
1355 if (height < displayInfo.smallestNominalAppHeight) {
1356 displayInfo.smallestNominalAppHeight = height;
1357 }
1358 if (height > displayInfo.largestNominalAppHeight) {
1359 displayInfo.largestNominalAppHeight = height;
1360 }
1361 }
1362
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001363 DockedStackDividerController getDockedDividerController() {
1364 return mDividerControllerLocked;
1365 }
1366
Winson Chung655332c2016-10-31 13:14:28 -07001367 PinnedStackController getPinnedStackController() {
1368 return mPinnedStackControllerLocked;
1369 }
1370
Jeff Browna506a6e2013-06-04 00:02:38 -07001371 /**
1372 * Returns true if the specified UID has access to this display.
1373 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001374 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001375 return mDisplay.hasAccess(uid);
1376 }
1377
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001378 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001379 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001380 }
1381
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001382 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001383 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001384 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001385 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001386 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001387 }
1388
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001389 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001390 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1391 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001392 if (stack.mStackId == stackId) {
1393 return stack;
1394 }
1395 }
1396 return null;
1397 }
1398
Andrii Kulian441e4492016-09-29 15:25:00 -07001399 @Override
1400 void onConfigurationChanged(Configuration newParentConfig) {
1401 super.onConfigurationChanged(newParentConfig);
1402
Andrii Kulian3a507b52016-09-19 18:14:12 -07001403 // The display size information is heavily dependent on the resources in the current
1404 // configuration, so we need to reconfigure it every time the configuration changes.
1405 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1406 mService.reconfigureDisplayLocked(this);
1407
1408 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001409 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001410 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001411
Andrii Kulian441e4492016-09-29 15:25:00 -07001412 /**
1413 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1414 * bounds were updated.
1415 */
1416 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001417 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1418 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001419 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001420 changedStackList.add(stack.mStackId);
1421 }
1422 }
Winson Chung32c566f2017-04-11 18:31:21 -07001423
1424 // If there was no pinned stack, we still need to notify the controller of the display info
1425 // update as a result of the config change. We do this here to consolidate the flow between
1426 // changes when there is and is not a stack.
1427 if (getStackById(PINNED_STACK_ID) == null) {
1428 mPinnedStackControllerLocked.onDisplayInfoChanged();
1429 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001430 }
1431
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001432 @Override
1433 boolean fillsParent() {
1434 return true;
1435 }
1436
1437 @Override
1438 boolean isVisible() {
1439 return true;
1440 }
1441
1442 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001443 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001444 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001445 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001446 }
1447
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001448 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001449 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1450 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1451 // target, or here in order if there isn't an IME target.
1452 if (traverseTopToBottom) {
1453 for (int i = mChildren.size() - 1; i >= 0; --i) {
1454 final DisplayChildWindowContainer child = mChildren.get(i);
1455 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1456 // In this case the Ime windows will be processed above their target so we skip
1457 // here.
1458 continue;
1459 }
1460 if (child.forAllWindows(callback, traverseTopToBottom)) {
1461 return true;
1462 }
1463 }
1464 } else {
1465 final int count = mChildren.size();
1466 for (int i = 0; i < count; i++) {
1467 final DisplayChildWindowContainer child = mChildren.get(i);
1468 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1469 // In this case the Ime windows will be processed above their target so we skip
1470 // here.
1471 continue;
1472 }
1473 if (child.forAllWindows(callback, traverseTopToBottom)) {
1474 return true;
1475 }
1476 }
1477 }
1478 return false;
1479 }
1480
1481 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1482 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1483 }
1484
1485 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001486 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001487 final WindowManagerPolicy policy = mService.mPolicy;
1488
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001489 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001490 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001491 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001492 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001493 // If the display is frozen, some activities may be in the middle of restarting, and
1494 // thus have removed their old window. If the window has the flag to hide the lock
1495 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1496 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001497 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001498 } else if (policy.isKeyguardLocked()) {
1499 // Use the last orientation the while the display is frozen with the keyguard
1500 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1501 // window. We don't want to check the show when locked window directly though as
1502 // things aren't stable while the display is frozen, for example the window could be
1503 // momentarily unavailable due to activity relaunch.
1504 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001505 + "return " + mLastOrientation);
1506 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001507 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001508 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001509 final int orientation = mAboveAppWindowsContainers.getOrientation();
1510 if (orientation != SCREEN_ORIENTATION_UNSET) {
1511 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001512 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001513 }
1514
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001515 // Top system windows are not requesting an orientation. Start searching from apps.
1516 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001517 }
1518
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001519 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001520 // Check if display metrics changed and update base values if needed.
1521 updateBaseDisplayMetricsIfNeeded();
1522
Craig Mautner722285e2012-09-07 13:55:58 -07001523 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001524 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001525
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001526 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1527 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001528 }
Craig Mautner722285e2012-09-07 13:55:58 -07001529 }
1530
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001531 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001532 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1533 if (displayManagerInternal != null) {
1534 // Bootstrap the default logical display from the display manager.
1535 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1536 if (newDisplayInfo != null) {
1537 mDisplayInfo.copyFrom(newDisplayInfo);
1538 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001539 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001540
Andrii Kuliancd097992017-03-23 18:31:59 -07001541 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1542 mDisplayInfo.logicalDensityDpi);
1543 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1544 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1545 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001546 }
1547
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001548 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001549 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001550 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001551 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001552 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1553 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001554 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001555 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001556 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001557 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001558 out.set(left, top, left + width, top + height);
1559 }
1560
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001561 private void getLogicalDisplayRect(Rect out, int orientation) {
1562 getLogicalDisplayRect(out);
1563
1564 // Rotate the Rect if needed.
1565 final int currentRotation = mDisplayInfo.rotation;
1566 final int rotationDelta = deltaRotation(currentRotation, orientation);
1567 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1568 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1569 mTmpRectF.set(out);
1570 mTmpMatrix.mapRect(mTmpRectF);
1571 mTmpRectF.round(out);
1572 }
1573 }
1574
Andrii Kuliancd097992017-03-23 18:31:59 -07001575 /**
1576 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1577 * values.
1578 */
1579 private void updateBaseDisplayMetricsIfNeeded() {
1580 // Get real display metrics without overrides from WM.
1581 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1582 final int orientation = mDisplayInfo.rotation;
1583 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1584 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1585 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1586 final int newDensity = mDisplayInfo.logicalDensityDpi;
1587
1588 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1589 || mInitialDisplayHeight != newHeight
1590 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1591
1592 if (displayMetricsChanged) {
1593 // Check if display size or density is forced.
1594 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1595 || mBaseDisplayHeight != mInitialDisplayHeight;
1596 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1597
1598 // If there is an override set for base values - use it, otherwise use new values.
1599 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1600 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1601 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1602
1603 // Real display metrics changed, so we should also update initial values.
1604 mInitialDisplayWidth = newWidth;
1605 mInitialDisplayHeight = newHeight;
1606 mInitialDisplayDensity = newDensity;
1607 mService.reconfigureDisplayLocked(this);
1608 }
1609 }
1610
Bryce Lee27cec322017-03-21 09:41:37 -07001611 /** Sets the maximum width the screen resolution can be */
1612 void setMaxUiWidth(int width) {
1613 if (DEBUG_DISPLAY) {
1614 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1615 }
1616
1617 mMaxUiWidth = width;
1618
1619 // Update existing metrics.
1620 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1621 }
1622
1623 /** Update base (override) display metrics. */
1624 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1625 mBaseDisplayWidth = baseWidth;
1626 mBaseDisplayHeight = baseHeight;
1627 mBaseDisplayDensity = baseDensity;
1628
1629 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1630 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1631 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1632 mBaseDisplayWidth = mMaxUiWidth;
1633
1634 if (DEBUG_DISPLAY) {
1635 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1636 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1637 + " on display:" + getDisplayId());
1638 }
1639 }
1640
1641 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1642 }
1643
Chong Zhangf66db432016-01-13 10:39:51 -08001644 void getContentRect(Rect out) {
1645 out.set(mContentRect);
1646 }
1647
Wale Ogunwale1666e312016-12-16 11:27:18 -08001648 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1649 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1650 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001651
Wale Ogunwale1666e312016-12-16 11:27:18 -08001652 TaskStack stack = getStackById(stackId);
1653 if (stack != null) {
1654 // It's already attached to the display...clear mDeferRemoval and move stack to
1655 // appropriate z-order on display as needed.
1656 stack.mDeferRemoval = false;
1657 // We're not moving the display to front when we're adding stacks, only when
1658 // requested to change the position of stack explicitly.
1659 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1660 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001661 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001662 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001663 mTaskStackContainers.addStackToDisplay(stack, onTop);
1664 }
1665
Wale Ogunwale1666e312016-12-16 11:27:18 -08001666 if (stackId == DOCKED_STACK_ID) {
1667 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001668 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001669 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001670 }
1671
Andrii Kulian51c1b672017-04-07 18:39:32 -07001672 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001673 final DisplayContent prevDc = stack.getDisplayContent();
1674 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001675 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1676 + " which is not currently attached to any display");
1677 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001678 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001679 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1680 + " to its current displayId=" + mDisplayId);
1681 }
1682
Wale Ogunwale1666e312016-12-16 11:27:18 -08001683 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001684 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001685 }
1686
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001687 @Override
1688 protected void addChild(DisplayChildWindowContainer child,
1689 Comparator<DisplayChildWindowContainer> comparator) {
1690 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1691 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001692
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001693 @Override
1694 protected void addChild(DisplayChildWindowContainer child, int index) {
1695 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1696 }
1697
1698 @Override
1699 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001700 // Only allow removal of direct children from this display if the display is in the process
1701 // of been removed.
1702 if (mRemovingDisplay) {
1703 super.removeChild(child);
1704 return;
1705 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001706 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001707 }
1708
Andrii Kuliand2765632016-12-12 22:26:34 -08001709 @Override
1710 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1711 // Children of the display are statically ordered, so the real intention here is to perform
1712 // the operation on the display and not the static direct children.
1713 getParent().positionChildAt(position, this, includingParents);
1714 }
1715
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001716 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001717 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1718 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001719 final int taskId = stack.taskIdFromPoint(x, y);
1720 if (taskId != -1) {
1721 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001722 }
1723 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001724 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001725 }
1726
Chong Zhang8e89b312015-09-09 15:09:30 -07001727 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001728 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001729 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001730 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001731 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001732 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001733 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001734 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1735 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001736 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001737 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001738 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001739
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001740 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1741 if (mTmpTaskForResizePointSearchResult.searchDone) {
1742 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001743 }
1744 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001745 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001746 }
1747
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001748 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001749 // The provided task is the task on this display with focus, so if WindowManagerService's
1750 // focused app is not on this display, focusedTask will be null.
1751 if (focusedTask == null) {
1752 mTouchExcludeRegion.setEmpty();
1753 } else {
1754 mTouchExcludeRegion.set(mBaseDisplayRect);
1755 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1756 mTmpRect2.setEmpty();
1757 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1758 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1759 stack.setTouchExcludeRegion(
1760 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1761 }
1762 // If we removed the focused task above, add it back and only leave its
1763 // outside touch area in the exclusion. TapDectector is not interested in
1764 // any touch inside the focused task itself.
1765 if (!mTmpRect2.isEmpty()) {
1766 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1767 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001768 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001769 final WindowState inputMethod = mService.mInputMethodWindow;
1770 if (inputMethod != null && inputMethod.isVisibleLw()) {
1771 // If the input method is visible and the user is typing, we don't want these touch
1772 // events to be intercepted and used to change focus. This would likely cause a
1773 // disappearance of the input method.
1774 inputMethod.getTouchableRegion(mTmpRegion);
1775 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1776 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001777 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1778 WindowState win = mTapExcludedWindows.get(i);
1779 win.getTouchableRegion(mTmpRegion);
1780 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1781 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001782 // TODO(multi-display): Support docked stacks on secondary displays.
1783 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001784 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001785 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001786 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1787 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001788 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001789 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001790 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001791 }
1792
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001793 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001794 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001795 super.switchUser();
1796 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001797 }
1798
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001799 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001800 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1801 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001802 }
1803 }
1804
1805 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001806 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001807 }
1808
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001809 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001810 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001811 }
1812
1813 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001814 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001815 }
1816
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001817 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001818 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001819 }
1820
Wale Ogunwale10124582016-09-15 20:25:50 -07001821 @Override
1822 void removeIfPossible() {
1823 if (isAnimating()) {
1824 mDeferredRemoval = true;
1825 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001826 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001827 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001828 }
1829
Wale Ogunwale10124582016-09-15 20:25:50 -07001830 @Override
1831 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001832 mRemovingDisplay = true;
1833 try {
1834 super.removeImmediately();
1835 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1836 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001837 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001838 mService.unregisterPointerEventListener(mTapDetector);
1839 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1840 }
1841 } finally {
1842 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001843 }
Craig Mautner95da1082014-02-24 17:54:35 -08001844 }
1845
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001846 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001847 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001848 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001849 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1850
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001851 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001852 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001853 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001854 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001855 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001856 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001857 }
1858
Wale Ogunwale10124582016-09-15 20:25:50 -07001859 boolean animateForIme(float interpolatedValue, float animationTarget,
1860 float dividerAnimationTarget) {
1861 boolean updated = false;
1862
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001863 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1864 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001865 if (stack == null || !stack.isAdjustedForIme()) {
1866 continue;
1867 }
1868
1869 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1870 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1871 updated = true;
1872 } else {
1873 mDividerControllerLocked.mLastAnimationProgress =
1874 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1875 mDividerControllerLocked.mLastDividerProgress =
1876 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1877 updated |= stack.updateAdjustForIme(
1878 mDividerControllerLocked.mLastAnimationProgress,
1879 mDividerControllerLocked.mLastDividerProgress,
1880 false /* force */);
1881 }
1882 if (interpolatedValue >= 1f) {
1883 stack.endImeAdjustAnimation();
1884 }
1885 }
1886
1887 return updated;
1888 }
1889
1890 boolean clearImeAdjustAnimation() {
1891 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001892 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1893 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001894 if (stack != null && stack.isAdjustedForIme()) {
1895 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1896 changed = true;
1897 }
1898 }
1899 return changed;
1900 }
1901
1902 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001903 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1904 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001905 if (stack.isVisible() && stack.isAdjustedForIme()) {
1906 stack.beginImeAdjustAnimation();
1907 }
1908 }
1909 }
1910
1911 void adjustForImeIfNeeded() {
1912 final WindowState imeWin = mService.mInputMethodWindow;
1913 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1914 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001915 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001916 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1917 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1918 imeTargetStack.getDockSide() : DOCKED_INVALID;
1919 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1920 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1921 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1922 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1923 final boolean imeHeightChanged = imeVisible &&
1924 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1925
1926 // The divider could be adjusted for IME position, or be thinner than usual,
1927 // or both. There are three possible cases:
1928 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1929 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1930 // - If IME is not visible, divider is not moved and is normal width.
1931
1932 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001933 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1934 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001935 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
1936 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
1937 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1938 } else {
1939 stack.resetAdjustedForIme(false);
1940 }
1941 }
1942 mDividerControllerLocked.setAdjustedForIme(
1943 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1944 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001945 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1946 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001947 stack.resetAdjustedForIme(!dockVisible);
1948 }
1949 mDividerControllerLocked.setAdjustedForIme(
1950 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
1951 }
Winson Chung655332c2016-10-31 13:14:28 -07001952 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07001953 }
1954
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001955 void setInputMethodAnimLayerAdjustment(int adj) {
1956 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
1957 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08001958 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001959 }
1960
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001961 /**
1962 * If a window that has an animation specifying a colored background and the current wallpaper
1963 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
1964 * suddenly disappear.
1965 */
1966 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001967 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
1968 w -> w.mIsWallpaper && w.isVisibleNow());
1969
1970 if (visibleWallpaper != null) {
1971 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001972 }
1973 return winAnimator.mAnimLayer;
1974 }
1975
Wale Ogunwale10124582016-09-15 20:25:50 -07001976 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001977 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1978 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07001979 stack.prepareFreezingTaskBounds();
1980 }
1981 }
1982
Wale Ogunwale94744212015-09-21 19:01:47 -07001983 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001984 getLogicalDisplayRect(mTmpRect, newRotation);
1985
1986 // Compute a transform matrix to undo the coordinate space transformation,
1987 // and present the window at the same physical position it previously occupied.
1988 final int deltaRotation = deltaRotation(newRotation, oldRotation);
1989 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
1990
1991 mTmpRectF.set(bounds);
1992 mTmpMatrix.mapRect(mTmpRectF);
1993 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07001994 }
1995
Wale Ogunwale4a02d812015-02-12 23:01:38 -08001996 static int deltaRotation(int oldRotation, int newRotation) {
1997 int delta = newRotation - oldRotation;
1998 if (delta < 0) delta += 4;
1999 return delta;
2000 }
2001
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002002 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002003 Matrix outMatrix) {
2004 // For rotations without Z-ordering we don't need the target rectangle's position.
2005 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2006 displayHeight, outMatrix);
2007 }
2008
2009 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2010 float displayWidth, float displayHeight, Matrix outMatrix) {
2011 switch (rotation) {
2012 case ROTATION_0:
2013 outMatrix.reset();
2014 break;
2015 case ROTATION_270:
2016 outMatrix.setRotate(270, 0, 0);
2017 outMatrix.postTranslate(0, displayHeight);
2018 outMatrix.postTranslate(rectTop, 0);
2019 break;
2020 case ROTATION_180:
2021 outMatrix.reset();
2022 break;
2023 case ROTATION_90:
2024 outMatrix.setRotate(90, 0, 0);
2025 outMatrix.postTranslate(displayWidth, 0);
2026 outMatrix.postTranslate(-rectTop, rectLeft);
2027 break;
2028 }
2029 }
2030
Craig Mautnera91f9e22012-09-14 16:22:08 -07002031 public void dump(String prefix, PrintWriter pw) {
2032 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2033 final String subPrefix = " " + prefix;
2034 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2035 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2036 pw.print("dpi");
2037 if (mInitialDisplayWidth != mBaseDisplayWidth
2038 || mInitialDisplayHeight != mBaseDisplayHeight
2039 || mInitialDisplayDensity != mBaseDisplayDensity) {
2040 pw.print(" base=");
2041 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2042 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2043 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002044 if (mDisplayScalingDisabled) {
2045 pw.println(" noscale");
2046 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002047 pw.print(" cur=");
2048 pw.print(mDisplayInfo.logicalWidth);
2049 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2050 pw.print(" app=");
2051 pw.print(mDisplayInfo.appWidth);
2052 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2053 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2054 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2055 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2056 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002057 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2058 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002059
Craig Mautnerdc548482014-02-05 13:35:24 -08002060 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002061 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002062 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2063 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002064 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002065 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002066
Craig Mautnerdc548482014-02-05 13:35:24 -08002067 pw.println();
2068 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002069 pw.println();
2070 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002071 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002072 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002073 pw.print(" Exiting #"); pw.print(i);
2074 pw.print(' '); pw.print(token);
2075 pw.println(':');
2076 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002077 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002078 }
Craig Mautner59c00972012-07-30 12:10:24 -07002079 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002080 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002081 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002082 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002083 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002084 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002085
2086 if (mInputMethodAnimLayerAdjustment != 0) {
2087 pw.println(subPrefix
2088 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2089 }
Craig Mautner59c00972012-07-30 12:10:24 -07002090 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002091
2092 @Override
2093 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002094 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002095 }
2096
2097 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002098 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002099 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002100
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002101 /** Checks if stack with provided id is visible on this display. */
2102 boolean isStackVisible(int stackId) {
2103 final TaskStack stack = getStackById(stackId);
2104 return (stack != null && stack.isVisible());
2105 }
2106
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002107 /**
2108 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2109 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002110 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002111 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002112 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002113 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002114
2115 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002116 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002117 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002118 */
Jorim Jaggife762342016-10-13 14:33:27 +02002119 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002120 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002121 }
2122
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002123 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002124 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002125 final int x = (int) xf;
2126 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002127 final WindowState touchedWin = getWindow(w -> {
2128 final int flags = w.mAttrs.flags;
2129 if (!w.isVisibleLw()) {
2130 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002131 }
2132 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002133 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002134 }
2135
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002136 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002137 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002138 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002139 }
2140
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002141 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002142
2143 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002144 return mTmpRegion.contains(x, y) || touchFlags == 0;
2145 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002146
2147 return touchedWin;
2148 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002149
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002150 boolean canAddToastWindowForUid(int uid) {
2151 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002152 // Also if the app is focused adding more than one toast at
2153 // a time for better backwards compatibility.
2154 final WindowState focusedWindowForUid = getWindow(w ->
2155 w.mOwnerUid == uid && w.isFocused());
2156 if (focusedWindowForUid != null) {
2157 return true;
2158 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002159 final WindowState win = getWindow(w ->
2160 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2161 && !w.mWindowRemovalAllowed);
2162 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002163 }
2164
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002165 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002166 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2167 return;
2168 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002169
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002170 // Used to communicate the old focus to the callback method.
2171 mTmpWindow = oldFocus;
2172
2173 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002174 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002175
2176 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002177 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002178
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002179 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002180
2181 if (mTmpWindow == null) {
2182 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2183 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002184 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002185 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002186 }
2187
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002188 /** Updates the layer assignment of windows on this display. */
2189 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002190 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002191 if (setLayoutNeeded) {
2192 setLayoutNeeded();
2193 }
2194 }
2195
Wale Ogunwale1666e312016-12-16 11:27:18 -08002196 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2197 // moving containers or resizing them. Need to investigate the best way to have it automatically
2198 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002199 void layoutAndAssignWindowLayersIfNeeded() {
2200 mService.mWindowsChanged = true;
2201 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002202
2203 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2204 false /*updateInputWindows*/)) {
2205 assignWindowLayers(false /* setLayoutNeeded */);
2206 }
2207
2208 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2209 mService.mWindowPlacerLocked.performSurfacePlacement();
2210 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2211 }
2212
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002213 /** Returns true if a leaked surface was destroyed */
2214 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002215 // Used to indicate that a surface was leaked.
2216 mTmpWindow = null;
2217 forAllWindows(w -> {
2218 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002219 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002220 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002221 }
2222 if (!mService.mSessions.contains(wsa.mSession)) {
2223 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002224 + w + " surface=" + wsa.mSurfaceController
2225 + " token=" + w.mToken
2226 + " pid=" + w.mSession.mPid
2227 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002228 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002229 mService.mForceRemoves.add(w);
2230 mTmpWindow = w;
2231 } else if (w.mAppToken != null && w.mAppToken.clientHidden) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002232 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002233 + w + " surface=" + wsa.mSurfaceController
2234 + " token=" + w.mAppToken
2235 + " saved=" + w.hasSavedSurface());
2236 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002237 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002238 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002239 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002240 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002241
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002242 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002243 }
2244
2245 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002246 * Determine and return the window that should be the IME target.
2247 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2248 * @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 +00002249 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002250 WindowState computeImeTarget(boolean updateImeTarget) {
2251 if (mService.mInputMethodWindow == null) {
2252 // There isn't an IME so there shouldn't be a target...That was easy!
2253 if (updateImeTarget) {
2254 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2255 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2256 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2257 }
2258 return null;
2259 }
2260
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002261 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2262 // same display. Or even when the current IME/target are not on the same screen as the next
2263 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002264 mUpdateImeTarget = updateImeTarget;
2265 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002266
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002267
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002268 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2269 // to be on top of it, but it is not -really- where input will go. So look down below
2270 // for a real window to target...
2271 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2272 final AppWindowToken token = target.mAppToken;
2273 if (token != null) {
2274 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2275 if (betterTarget != null) {
2276 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002277 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002278 }
2279 }
2280
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002281 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2282 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002283
2284 // Now, a special case -- if the last target's window is in the process of exiting, and is
2285 // above the new target, keep on the last target to avoid flicker. Consider for example a
2286 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2287 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2288 // scrim.
2289 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002290 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2291 && (target == null
2292 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002293 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002294 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002295 }
2296
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002297 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2298 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002299
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002300 if (target == null) {
2301 if (updateImeTarget) {
2302 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2303 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2304 + Debug.getCallers(4) : ""));
2305 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2306 }
2307
2308 return null;
2309 }
2310
2311 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002312 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2313 if (token != null) {
2314
2315 // Now some fun for dealing with window animations that modify the Z order. We need
2316 // to look at all windows below the current target that are in this app, finding the
2317 // highest visible one in layering.
2318 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002319 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002320 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002321 }
2322
2323 if (highestTarget != null) {
2324 final AppTransition appTransition = mService.mAppTransition;
2325 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2326 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2327 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002328 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002329
2330 if (appTransition.isTransitionSet()) {
2331 // If we are currently setting up for an animation, hold everything until we
2332 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002333 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2334 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002335 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002336 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002337 // If the window we are currently targeting is involved with an animation,
2338 // and it is on top of the next target we will be over, then hold off on
2339 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002340 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2341 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002342 }
2343 }
2344 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002345
2346 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2347 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2348 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002349 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002350 }
2351
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002352 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002353 }
2354
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002355 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2356 if (target == mService.mInputMethodTarget
2357 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2358 && mInputMethodAnimLayerAdjustment == layerAdj) {
2359 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002360 }
2361
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002362 mService.mInputMethodTarget = target;
2363 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2364 setInputMethodAnimLayerAdjustment(layerAdj);
2365 assignWindowLayers(false /* setLayoutNeeded */);
2366 }
2367
2368 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2369 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2370 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2371 }
2372
2373 // Used to indicate we have reached the first window in the range we are interested in.
2374 mTmpWindow = null;
2375
2376 // TODO: Figure-out a more efficient way to do this.
2377 final WindowState candidate = getWindow(w -> {
2378 if (w == top) {
2379 // Reached the first window in the range we are interested in.
2380 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002381 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 if (mTmpWindow == null) {
2383 return false;
2384 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002385
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002386 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2387 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002388 }
2389 // If we reached the bottom of the range of windows we are considering,
2390 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002391 if (w == bottom) {
2392 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002393 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002394 return false;
2395 });
2396
2397 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002398 }
2399
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002400 void setLayoutNeeded() {
2401 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2402 mLayoutNeeded = true;
2403 }
2404
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002405 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002406 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2407 mLayoutNeeded = false;
2408 }
2409
2410 boolean isLayoutNeeded() {
2411 return mLayoutNeeded;
2412 }
2413
Wale Ogunwale02319a62016-09-26 15:21:22 -07002414 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2415 if (mTokenMap.isEmpty()) {
2416 return;
2417 }
2418 pw.println(" Display #" + mDisplayId);
2419 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2420 while (it.hasNext()) {
2421 final WindowToken token = it.next();
2422 pw.print(" ");
2423 pw.print(token);
2424 if (dumpAll) {
2425 pw.println(':');
2426 token.dump(pw, " ");
2427 } else {
2428 pw.println();
2429 }
2430 }
2431 }
2432
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002433 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002434 final int[] index = new int[1];
2435 forAllWindows(w -> {
2436 final WindowStateAnimator wAnim = w.mWinAnimator;
2437 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2438 index[0] = index[0] + 1;
2439 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002440 }
2441
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002442 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002443 forAllWindows(w -> {
2444 w.mWinAnimator.enableSurfaceTrace(fd);
2445 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002446 }
2447
2448 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002449 forAllWindows(w -> {
2450 w.mWinAnimator.disableSurfaceTrace();
2451 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002452 }
2453
Jorim Jaggife762342016-10-13 14:33:27 +02002454 /**
2455 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2456 */
2457 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2458 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459 forAllWindows(w -> {
2460 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
2461 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002462 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2463 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002464 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002465 }
2466
Wale Ogunwale494009b82016-10-21 09:01:38 -07002467 boolean checkWaitingForWindows() {
2468
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002469 mHaveBootMsg = false;
2470 mHaveApp = false;
2471 mHaveWallpaper = false;
2472 mHaveKeyguard = true;
2473
2474 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002475 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2476 return true;
2477 }
2478 if (w.isDrawnLw()) {
2479 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002480 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002481 } else if (w.mAttrs.type == TYPE_APPLICATION
2482 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002483 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002484 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002485 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002486 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002488 }
2489 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002490 return false;
2491 });
2492
2493 if (visibleWindow != null) {
2494 // We have a visible window.
2495 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002496 }
2497
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002498 // if the wallpaper service is disabled on the device, we're never going to have
2499 // wallpaper, don't bother waiting for it
2500 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2501 com.android.internal.R.bool.config_enableWallpaperService)
2502 && !mService.mOnlyCore;
2503
Wale Ogunwale494009b82016-10-21 09:01:38 -07002504 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2505 "******** booted=" + mService.mSystemBooted
2506 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002507 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2508 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2509 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002510
2511 // If we are turning on the screen to show the boot message, don't do it until the boot
2512 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002513 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002514 return true;
2515 }
2516
2517 // If we are turning on the screen after the boot is completed normally, don't do so until
2518 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002519 if (mService.mSystemBooted
2520 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002521 return true;
2522 }
2523
2524 return false;
2525 }
2526
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002527 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002528 mTmpWindowAnimator = animator;
2529 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002530 }
2531
2532 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002533 resetAnimationBackgroundAnimator();
2534
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002535 // Used to indicate a detached wallpaper.
2536 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002537 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002538
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002539 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002540
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002542 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002543 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2544 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002545 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2546 }
2547 }
2548
2549 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002550 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002551 }
2552
Wale Ogunwale494009b82016-10-21 09:01:38 -07002553 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002554 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002555 if (imFocus == null) {
2556 return false;
2557 }
2558
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002559 if (DEBUG_INPUT_METHOD) {
2560 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2561 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2562 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002563 }
2564
Wale Ogunwale494009b82016-10-21 09:01:38 -07002565 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2566
2567 if (DEBUG_INPUT_METHOD) {
2568 Slog.i(TAG_WM, "IM target client: " + imeClient);
2569 if (imeClient != null) {
2570 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2571 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2572 }
2573 }
2574
2575 return imeClient != null && imeClient.asBinder() == client.asBinder();
2576 }
2577
2578 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002579 final WindowState win = getWindow(
2580 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2581 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002582 }
2583
2584 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002585 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002586 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002587 final int curValue = w.mSystemUiVisibility;
2588 final int diff = (curValue ^ visibility) & globalDiff;
2589 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002590 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002591 w.mSeq++;
2592 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002593 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002594 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2595 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002596 visibility, newValue, diff);
2597 }
2598 } catch (RemoteException e) {
2599 // so sorry
2600 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002601 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002602 }
2603
2604 void onWindowFreezeTimeout() {
2605 Slog.w(TAG_WM, "Window freeze timeout expired.");
2606 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002607
2608 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002609 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002610 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002611 }
2612 w.mOrientationChanging = false;
2613 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2614 - mService.mDisplayFreezeTime);
2615 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002617 mService.mWindowPlacerLocked.performSurfacePlacement();
2618 }
2619
2620 void waitForAllWindowsDrawn() {
2621 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002622 forAllWindows(w -> {
2623 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2624 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2625 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002626 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002627 w.mLastContentInsets.set(-1, -1, -1, -1);
2628 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002629 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002630 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002631 }
2632
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002633 // TODO: Super crazy long method that should be broken down...
2634 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2635
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002636 final int dw = mDisplayInfo.logicalWidth;
2637 final int dh = mDisplayInfo.logicalHeight;
2638 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2639
2640 mTmpUpdateAllDrawn.clear();
2641
2642 int repeats = 0;
2643 do {
2644 repeats++;
2645 if (repeats > 6) {
2646 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2647 clearLayoutNeeded();
2648 break;
2649 }
2650
2651 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2652 pendingLayoutChanges);
2653
Andrii Kulian839def92016-11-02 10:58:58 -07002654 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2655 // the wallpaper window jumping across displays.
2656 // Remove check for default display when there will be support for multiple wallpaper
2657 // targets (on different displays).
2658 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002659 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002660 }
2661
2662 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2663 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2664 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2665 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002666 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002667 }
2668 }
2669
2670 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2671 setLayoutNeeded();
2672 }
2673
2674 // FIRST LOOP: Perform a layout, if needed.
2675 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2676 performLayout(repeats == 1, false /* updateInputWindows */);
2677 } else {
2678 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2679 }
2680
2681 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2682 pendingLayoutChanges = 0;
2683
2684 if (isDefaultDisplay) {
2685 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002686 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002687 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2688 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2689 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2690 }
2691 } while (pendingLayoutChanges != 0);
2692
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002693 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002694 resetDimming();
2695
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002696 mTmpRecoveringMemory = recoveringMemory;
2697 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002698
2699 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002700 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2701 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2702 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002703 true /* inTraversal, must call performTraversalInTrans... below */);
2704
2705 stopDimmingIfNeeded();
2706
2707 while (!mTmpUpdateAllDrawn.isEmpty()) {
2708 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2709 // See if any windows have been drawn, so they (and others associated with them)
2710 // can now be shown.
2711 atoken.updateAllDrawn(this);
2712 }
2713
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002714 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002715 }
2716
2717 void performLayout(boolean initial, boolean updateInputWindows) {
2718 if (!isLayoutNeeded()) {
2719 return;
2720 }
2721 clearLayoutNeeded();
2722
2723 final int dw = mDisplayInfo.logicalWidth;
2724 final int dh = mDisplayInfo.logicalHeight;
2725
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002726 if (DEBUG_LAYOUT) {
2727 Slog.v(TAG, "-------------------------------------");
2728 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2729 }
2730
Andrii Kulian8ee72852017-03-10 10:36:45 -08002731 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002732 getConfiguration().uiMode);
2733 if (isDefaultDisplay) {
2734 // Not needed on non-default displays.
2735 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2736 mService.mScreenRect.set(0, 0, dw, dh);
2737 }
2738
2739 mService.mPolicy.getContentRectLw(mContentRect);
2740
2741 int seq = mService.mLayoutSeq + 1;
2742 if (seq < 0) seq = 0;
2743 mService.mLayoutSeq = seq;
2744
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 // Used to indicate that we have processed the dream window and all additional windows are
2746 // behind it.
2747 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002748 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002749
2750 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002751 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002752
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002753 // Used to indicate that we have processed the dream window and all additional attached
2754 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002755 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002756 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002757
2758 // Now perform layout of attached windows, which usually depend on the position of the
2759 // window they are attached to. XXX does not deal with windows that are attached to windows
2760 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002761 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002762
2763 // Window frames may have changed. Tell the input dispatcher about it.
2764 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2765 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2766 if (updateInputWindows) {
2767 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2768 }
2769
2770 mService.mPolicy.finishLayoutLw();
2771 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2772 }
2773
2774 /**
2775 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2776 * In portrait mode, it grabs the full screenshot.
2777 *
2778 * @param width the width of the target bitmap
2779 * @param height the height of the target bitmap
2780 * @param includeFullDisplay true if the screen should not be cropped before capture
2781 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2782 * @param config of the output bitmap
2783 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002784 * @param includeDecor whether to include window decors, like the status or navigation bar
2785 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002786 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002787 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002788 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002789 boolean wallpaperOnly, boolean includeDecor) {
2790 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2791 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002792 if (bitmap == null) {
2793 return null;
2794 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002795
2796 if (DEBUG_SCREENSHOT) {
2797 // TEST IF IT's ALL BLACK
2798 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2799 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2800 bitmap.getHeight());
2801 boolean allBlack = true;
2802 final int firstColor = buffer[0];
2803 for (int i = 0; i < buffer.length; i++) {
2804 if (buffer[i] != firstColor) {
2805 allBlack = false;
2806 break;
2807 }
2808 }
2809 if (allBlack) {
2810 final WindowState appWin = mScreenshotApplicationState.appWin;
2811 final int maxLayer = mScreenshotApplicationState.maxLayer;
2812 final int minLayer = mScreenshotApplicationState.minLayer;
2813 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2814 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2815 (appWin != null ?
2816 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2817 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2818 }
2819 }
2820
2821 // Create a copy of the screenshot that is immutable and backed in ashmem.
2822 // This greatly reduces the overhead of passing the bitmap between processes.
2823 Bitmap ret = bitmap.createAshmemBitmap(config);
2824 bitmap.recycle();
2825 return ret;
2826 }
2827
2828 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2829 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2830 boolean includeDecor) {
2831 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2832 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2833 }
2834
2835 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2836 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2837 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002838 int dw = mDisplayInfo.logicalWidth;
2839 int dh = mDisplayInfo.logicalHeight;
2840 if (dw == 0 || dh == 0) {
2841 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2842 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2843 return null;
2844 }
2845
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002846 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002847
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002848 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002849 final Rect frame = new Rect();
2850 final Rect stackBounds = new Rect();
2851
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002852 boolean includeImeInScreenshot;
2853 synchronized(mService.mWindowMap) {
2854 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2855 ? mService.mInputMethodTarget.mAppToken : null;
2856 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2857 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2858 // mode because the frame of the IME might not overlap with that of the app.
2859 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2860 // overlapping with the bottom app.
2861 includeImeInScreenshot = imeTargetAppToken != null
2862 && imeTargetAppToken.appToken != null
2863 && imeTargetAppToken.appToken.asBinder() == appToken
2864 && !mService.mInputMethodTarget.isInMultiWindowMode();
2865 }
2866
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002867 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002868 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002869 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002870 synchronized(mService.mWindowMap) {
2871 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002872 mScreenshotApplicationState.appWin = null;
2873 forAllWindows(w -> {
2874 if (!w.mHasSurface) {
2875 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002876 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002877 if (w.mLayer >= aboveAppLayer) {
2878 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002879 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002880 if (wallpaperOnly && !w.mIsWallpaper) {
2881 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002882 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002883 if (w.mIsImWindow) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002884 if (!includeImeInScreenshot) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002885 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002886 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002887 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002888 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2889 // then the target window state is this one.
2890 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002891 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002892 }
2893
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002894 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002895 // We have not ran across the target window yet, so it is probably behind
2896 // the wallpaper. This can happen when the keyguard is up and all windows
2897 // are moved behind the wallpaper. We don't want to include the wallpaper
2898 // layer in the screenshot as it will cover-up the layer of the target
2899 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002900 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002901 }
2902 // Fall through. The target window is in front of the wallpaper. For this
2903 // case we want to include the wallpaper layer in the screenshot because
2904 // the target window might have some transparent areas.
2905 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002906 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002907 // This app window is of no interest if it is not associated with the
2908 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002909 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002910 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002911 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002912 }
2913
2914 // Include this window.
2915
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002916 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002917 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002918 if (mScreenshotApplicationState.maxLayer < layer) {
2919 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002920 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002921 if (mScreenshotApplicationState.minLayer > layer) {
2922 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002923 }
2924
2925 // Don't include wallpaper in bounds calculation
Jorim Jaggibfbd9dc2017-01-20 18:13:01 +01002926 if (!mutableIncludeFullDisplay.value && includeDecor) {
2927 final TaskStack stack = w.getStack();
2928 if (stack != null) {
2929 stack.getBounds(frame);
2930 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02002931
2932 // We want to screenshot with the exact bounds of the surface of the app. Thus,
2933 // intersect it with the frame.
2934 frame.intersect(w.mFrame);
2935 }else if (!mutableIncludeFullDisplay.value && !w.mIsWallpaper) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002936 final Rect wf = w.mFrame;
2937 final Rect cr = w.mContentInsets;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002938 int left = wf.left + cr.left;
2939 int top = wf.top + cr.top;
2940 int right = wf.right - cr.right;
2941 int bottom = wf.bottom - cr.bottom;
2942 frame.union(left, top, right, bottom);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002943 w.getVisibleBounds(stackBounds);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002944 if (!Rect.intersects(frame, stackBounds)) {
2945 // Set frame empty if there's no intersection.
2946 frame.setEmpty();
2947 }
2948 }
2949
2950 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002951 (w.mAppToken != null && w.mAppToken.token == appToken)
2952 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002953 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002954 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955 }
2956
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002957 if (w.isObscuringDisplay()){
2958 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002959 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002960 return false;
2961 }, true /* traverseTopToBottom */);
2962
2963 final WindowState appWin = mScreenshotApplicationState.appWin;
2964 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2965 final int maxLayer = mScreenshotApplicationState.maxLayer;
2966 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002967
2968 if (appToken != null && appWin == null) {
2969 // Can't find a window to snapshot.
2970 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2971 "Screenshot: Couldn't find a surface matching " + appToken);
2972 return null;
2973 }
2974
2975 if (!screenshotReady) {
2976 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2977 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2978 appWin.mWinAnimator.mDrawState)));
2979 return null;
2980 }
2981
2982 // Screenshot is ready to be taken. Everything from here below will continue
2983 // through the bottom of the loop and return a value. We only stay in the loop
2984 // because we don't want to release the mWindowMap lock until the screenshot is
2985 // taken.
2986
2987 if (maxLayer == 0) {
2988 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2989 + ": returning null maxLayer=" + maxLayer);
2990 return null;
2991 }
2992
Jorim Jaggi02886a82016-12-06 09:10:06 -08002993 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002994 // Constrain frame to the screen size.
2995 if (!frame.intersect(0, 0, dw, dh)) {
2996 frame.setEmpty();
2997 }
2998 } else {
2999 // Caller just wants entire display.
3000 frame.set(0, 0, dw, dh);
3001 }
3002 if (frame.isEmpty()) {
3003 return null;
3004 }
3005
3006 if (width < 0) {
3007 width = (int) (frame.width() * frameScale);
3008 }
3009 if (height < 0) {
3010 height = (int) (frame.height() * frameScale);
3011 }
3012
3013 // Tell surface flinger what part of the image to crop. Take the top
3014 // right part of the application, and crop the larger dimension to fit.
3015 Rect crop = new Rect(frame);
3016 if (width / (float) frame.width() < height / (float) frame.height()) {
3017 int cropWidth = (int)((float)width / (float)height * frame.height());
3018 crop.right = crop.left + cropWidth;
3019 } else {
3020 int cropHeight = (int)((float)height / (float)width * frame.width());
3021 crop.bottom = crop.top + cropHeight;
3022 }
3023
3024 // The screenshot API does not apply the current screen rotation.
3025 int rot = mDisplay.getRotation();
3026
3027 if (rot == ROTATION_90 || rot == ROTATION_270) {
3028 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3029 }
3030
3031 // Surfaceflinger is not aware of orientation, so convert our logical
3032 // crop to surfaceflinger's portrait orientation.
3033 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3034
3035 if (DEBUG_SCREENSHOT) {
3036 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3037 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003038 forAllWindows(w -> {
3039 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3040 Slog.i(TAG_WM, w + ": " + w.mLayer
3041 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003042 + " surfaceLayer=" + ((controller == null)
3043 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003044 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003045 }
3046
3047 final ScreenRotationAnimation screenRotationAnimation =
3048 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3049 final boolean inRotation = screenRotationAnimation != null &&
3050 screenRotationAnimation.isAnimating();
3051 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3052 "Taking screenshot while rotating");
3053
3054 // We force pending transactions to flush before taking
3055 // the screenshot by pushing an empty synchronous transaction.
3056 SurfaceControl.openTransaction();
3057 SurfaceControl.closeTransactionSync();
3058
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003059 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003060 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003061 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003062 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3063 + ") to layer " + maxLayer);
3064 return null;
3065 }
3066 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003067 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003068 }
3069
3070 // TODO: Can this use createRotationMatrix()?
3071 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3072 if (rot == Surface.ROTATION_90) {
3073 final int tmp = crop.top;
3074 crop.top = dw - crop.right;
3075 crop.right = crop.bottom;
3076 crop.bottom = dw - crop.left;
3077 crop.left = tmp;
3078 } else if (rot == Surface.ROTATION_180) {
3079 int tmp = crop.top;
3080 crop.top = dh - crop.bottom;
3081 crop.bottom = dh - tmp;
3082 tmp = crop.right;
3083 crop.right = dw - crop.left;
3084 crop.left = dw - tmp;
3085 } else if (rot == Surface.ROTATION_270) {
3086 final int tmp = crop.top;
3087 crop.top = crop.left;
3088 crop.left = dh - crop.bottom;
3089 crop.bottom = crop.right;
3090 crop.right = dh - tmp;
3091 }
3092 }
3093
3094 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003095 // Used to indicate the layout is needed.
3096 mTmpWindow = null;
3097
3098 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003099 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003100 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003101 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003102 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003103 w.setDisplayLayoutNeeded();
3104 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003105 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003106
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003107 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003108 mService.mWindowPlacerLocked.performSurfacePlacement();
3109 }
3110 }
3111
Wale Ogunwale1666e312016-12-16 11:27:18 -08003112 void setExitingTokensHasVisible(boolean hasVisible) {
3113 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3114 mExitingTokens.get(i).hasVisible = hasVisible;
3115 }
3116
3117 // Initialize state of exiting applications.
3118 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3119 }
3120
3121 void removeExistingTokensIfPossible() {
3122 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3123 final WindowToken token = mExitingTokens.get(i);
3124 if (!token.hasVisible) {
3125 mExitingTokens.remove(i);
3126 }
3127 }
3128
3129 // Time to remove any exiting applications?
3130 mTaskStackContainers.removeExistingAppTokensIfPossible();
3131 }
3132
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003133 @Override
3134 void onDescendantOverrideConfigurationChanged() {
3135 setLayoutNeeded();
3136 mService.requestTraversal();
3137 }
3138
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003139 static final class TaskForResizePointSearchResult {
3140 boolean searchDone;
3141 Task taskForResize;
3142
3143 void reset() {
3144 searchDone = false;
3145 taskForResize = null;
3146 }
3147 }
Robert Carr3b716242016-08-16 16:02:21 -07003148
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003149 private static final class ApplySurfaceChangesTransactionState {
3150 boolean displayHasContent;
3151 boolean obscured;
3152 boolean syswin;
3153 boolean focusDisplayed;
3154 float preferredRefreshRate;
3155 int preferredModeId;
3156
3157 void reset() {
3158 displayHasContent = false;
3159 obscured = false;
3160 syswin = false;
3161 focusDisplayed = false;
3162 preferredRefreshRate = 0;
3163 preferredModeId = 0;
3164 }
3165 }
3166
3167 private static final class ScreenshotApplicationState {
3168 WindowState appWin;
3169 int maxLayer;
3170 int minLayer;
3171 boolean screenshotReady;
3172
3173 void reset(boolean screenshotReady) {
3174 appWin = null;
3175 maxLayer = 0;
3176 minLayer = 0;
3177 this.screenshotReady = screenshotReady;
3178 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3179 }
3180 }
3181
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003182 /**
3183 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3184 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3185 * homogeneous children type which is currently required by sub-classes of
3186 * {@link WindowContainer} class.
3187 */
3188 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3189
3190 int size() {
3191 return mChildren.size();
3192 }
3193
3194 E get(int index) {
3195 return mChildren.get(index);
3196 }
3197
3198 @Override
3199 boolean fillsParent() {
3200 return true;
3201 }
3202
3203 @Override
3204 boolean isVisible() {
3205 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003206 }
3207 }
3208
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003209 /**
3210 * Window container class that contains all containers on this display relating to Apps.
3211 * I.e Activities.
3212 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003213 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003214
Andrii Kulian839def92016-11-02 10:58:58 -07003215 /**
3216 * Adds the stack to this container.
3217 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3218 */
3219 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003220 if (stack.mStackId == HOME_STACK_ID) {
3221 if (mHomeStack != null) {
3222 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3223 }
3224 mHomeStack = stack;
3225 }
3226 addChild(stack, onTop);
3227 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003228 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003229
Andrii Kulian839def92016-11-02 10:58:58 -07003230 /** Removes the stack from its container and prepare for changing the parent. */
3231 void removeStackFromDisplay(TaskStack stack) {
3232 removeChild(stack);
3233 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003234 }
3235
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003236 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003237 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3238 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003239 addChild(stack, addIndex);
3240 setLayoutNeeded();
3241 }
3242
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003243 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003244 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3245 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3246 // This stack is always-on-top, override the default behavior.
3247 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3248
3249 // Moving to its current position, as we must call super but we don't want to
3250 // perform any meaningful action.
3251 final int currentPosition = mChildren.indexOf(child);
3252 super.positionChildAt(currentPosition, child, false /* includingParents */);
3253 return;
3254 }
3255
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003256 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3257 super.positionChildAt(targetPosition, child, includingParents);
3258
3259 setLayoutNeeded();
3260 }
3261
3262 /**
3263 * When stack is added or repositioned, find a proper position for it.
3264 * This will make sure that pinned stack always stays on top.
3265 * @param requestedPosition Position requested by caller.
3266 * @param stack Stack to be added or positioned.
3267 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3268 * @return The proper position for the stack.
3269 */
3270 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3271 final int topChildPosition = mChildren.size() - 1;
3272 boolean toTop = requestedPosition == POSITION_TOP;
3273 toTop |= adding ? requestedPosition >= topChildPosition + 1
3274 : requestedPosition >= topChildPosition;
3275 int targetPosition = requestedPosition;
3276
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003277 if (toTop && isStackVisible(PINNED_STACK_ID) && stack.mStackId != PINNED_STACK_ID) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003278 // The pinned stack is always the top most stack (always-on-top) when it is visible.
3279 TaskStack topStack = mChildren.get(topChildPosition);
3280 if (topStack.mStackId != PINNED_STACK_ID) {
3281 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3282 }
3283
3284 // So, stack is moved just below the pinned stack.
3285 // When we're adding a new stack the target is the current pinned stack position.
3286 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003287 // stack, because WindowContainer#positionAt() first removes element and then adds
3288 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003289 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3290 }
3291
3292 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003293 }
3294
3295 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003296 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3297 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003298 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003299 if (super.forAllWindows(callback, traverseTopToBottom)) {
3300 return true;
3301 }
3302 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3303 return true;
3304 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003305 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003306 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3307 return true;
3308 }
3309 if (super.forAllWindows(callback, traverseTopToBottom)) {
3310 return true;
3311 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003312 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003313 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003314 }
3315
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003316 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003317 boolean traverseTopToBottom) {
3318 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3319 // app tokens.
3320 // TODO: Investigate if we need to continue to do this or if we can just process them
3321 // in-order.
3322 if (traverseTopToBottom) {
3323 for (int i = mChildren.size() - 1; i >= 0; --i) {
3324 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3325 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003326 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3327 traverseTopToBottom)) {
3328 return true;
3329 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003330 }
3331 }
3332 } else {
3333 final int count = mChildren.size();
3334 for (int i = 0; i < count; ++i) {
3335 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3336 final int appTokensCount = appTokens.size();
3337 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003338 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3339 traverseTopToBottom)) {
3340 return true;
3341 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003342 }
3343 }
3344 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003345 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003346 }
3347
Wale Ogunwale1666e312016-12-16 11:27:18 -08003348 void setExitingTokensHasVisible(boolean hasVisible) {
3349 for (int i = mChildren.size() - 1; i >= 0; --i) {
3350 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3351 for (int j = appTokens.size() - 1; j >= 0; --j) {
3352 appTokens.get(j).hasVisible = hasVisible;
3353 }
3354 }
3355 }
3356
3357 void removeExistingAppTokensIfPossible() {
3358 for (int i = mChildren.size() - 1; i >= 0; --i) {
3359 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3360 for (int j = appTokens.size() - 1; j >= 0; --j) {
3361 final AppWindowToken token = appTokens.get(j);
3362 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3363 && (!token.mIsExiting || token.isEmpty())) {
3364 // Make sure there is no animation running on this token, so any windows
3365 // associated with it will be removed as soon as their animations are
3366 // complete.
3367 token.mAppAnimator.clearAnimation();
3368 token.mAppAnimator.animating = false;
3369 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3370 "performLayout: App token exiting now removed" + token);
3371 token.removeIfPossible();
3372 }
3373 }
3374 }
3375 }
3376
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003377 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003378 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003379 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003380 // Apps and their containers are not allowed to specify an orientation while the
3381 // docked or freeform stack is visible...except for the home stack/task if the
3382 // docked stack is minimized and it actually set something.
3383 if (mHomeStack != null && mHomeStack.isVisible()
3384 && mDividerControllerLocked.isMinimizedDock()) {
3385 final int orientation = mHomeStack.getOrientation();
3386 if (orientation != SCREEN_ORIENTATION_UNSET) {
3387 return orientation;
3388 }
3389 }
3390 return SCREEN_ORIENTATION_UNSPECIFIED;
3391 }
3392
3393 final int orientation = super.getOrientation();
3394 if (orientation != SCREEN_ORIENTATION_UNSET
3395 && orientation != SCREEN_ORIENTATION_BEHIND) {
3396 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3397 "App is requesting an orientation, return " + orientation);
3398 return orientation;
3399 }
3400
3401 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003402 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003403 // The next app has not been requested to be visible, so we keep the current orientation
3404 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003405 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003406 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003407 }
3408
3409 /**
3410 * Window container class that contains all containers on this display that are not related to
3411 * Apps. E.g. status bar.
3412 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003413 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3414 /**
3415 * Compares two child window tokens returns -1 if the first is lesser than the second in
3416 * terms of z-order and 1 otherwise.
3417 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003418 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003419 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003420 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3421 token1.mOwnerCanManageAppTokens)
3422 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3423 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003424
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003425 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3426 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3427 return false;
3428 }
3429 final int req = w.mAttrs.screenOrientation;
3430 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3431 || req == SCREEN_ORIENTATION_UNSET) {
3432 return false;
3433 }
3434 return true;
3435 };
3436
Wale Ogunwale3a931692016-11-02 16:49:48 -07003437 private final String mName;
3438 NonAppWindowContainers(String name) {
3439 mName = name;
3440 }
3441
3442 void addChild(WindowToken token) {
3443 addChild(token, mWindowComparator);
3444 }
3445
3446 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003447 int getOrientation() {
3448 final WindowManagerPolicy policy = mService.mPolicy;
3449 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003450 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003451
3452 if (win != null) {
3453 final int req = win.mAttrs.screenOrientation;
3454 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
3455 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003456 mLastKeyguardForcedOrientation = req;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003457 }
Andrii Kulian8ee72852017-03-10 10:36:45 -08003458 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003459 }
3460
Andrii Kulian8ee72852017-03-10 10:36:45 -08003461 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003462
3463 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003464 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003465 }
3466
3467 return SCREEN_ORIENTATION_UNSET;
3468 }
3469
3470 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003471 String getName() {
3472 return mName;
3473 }
Robert Carr3b716242016-08-16 16:02:21 -07003474 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003475
3476 /**
3477 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3478 */
3479 @FunctionalInterface
3480 private interface Screenshoter<E> {
3481 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3482 boolean useIdentityTransform, int rotation);
3483 }
Craig Mautner59c00972012-07-30 12:10:24 -07003484}