blob: 54983c8998fc12041c45c67ba6fcbb48fcd8886c [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Wale Ogunwale51362492016-09-08 17:49:17 -070020import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.HOME_STACK_ID;
22import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
25import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070026import static android.view.Display.DEFAULT_DISPLAY;
27import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070028import static android.view.Surface.ROTATION_0;
29import static android.view.Surface.ROTATION_180;
30import static android.view.Surface.ROTATION_270;
31import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070032import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070033import static android.view.WindowManager.DOCKED_BOTTOM;
34import static android.view.WindowManager.DOCKED_INVALID;
35import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080036import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
37import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
38import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070039import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070040import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070041import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
42import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070043import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070045import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070046import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070047import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070049import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
50import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070052import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
53import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070054import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070055import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070056import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
58import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070059import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080060import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070061import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070062import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070063import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
64import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070076import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070077import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070078import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070079import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080080import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070081import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070082import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
83import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070084import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070085import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070086import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
87import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
89import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
90import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070091import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070092import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070093import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070094import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070095import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070096import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -070097import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070098import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070099import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700100
Andrii Kulian3a507b52016-09-19 18:14:12 -0700101import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700102import android.app.ActivityManager.StackId;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700103import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700104import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700105import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100106import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700107import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800108import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700109import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700110import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100111import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700112import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700113import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700114import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700115import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700116import android.os.RemoteException;
117import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700118import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800119import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700120import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700121import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700122import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700123import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700124import android.view.Surface;
125import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700126import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700127
Bryce Lee48f4b572017-04-10 10:54:15 -0700128import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800129import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700130import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700131
Robert Carr3b716242016-08-16 16:02:21 -0700132import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700133import java.io.PrintWriter;
134import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700135import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700136import java.util.HashMap;
137import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700138import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700139import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800140import java.util.function.Consumer;
141import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700142
Craig Mautner59c00972012-07-30 12:10:24 -0700143/**
144 * Utility class for keeping track of the WindowStates and other pertinent contents of a
145 * particular Display.
146 *
147 * IMPORTANT: No method from this class should ever be used without holding
148 * WindowManagerService.mWindowMap.
149 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700150class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700151 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700152
153 /** Unique identifier of this stack. */
154 private final int mDisplayId;
155
Wale Ogunwale3a931692016-11-02 16:49:48 -0700156 /** The containers below are the only child containers the display can have. */
157 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700158 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700159 // Contains all non-app window containers that should be displayed above the app containers
160 // (e.g. Status bar)
161 private final NonAppWindowContainers mAboveAppWindowsContainers =
162 new NonAppWindowContainers("mAboveAppWindowsContainers");
163 // Contains all non-app window containers that should be displayed below the app containers
164 // (e.g. Wallpaper).
165 private final NonAppWindowContainers mBelowAppWindowsContainers =
166 new NonAppWindowContainers("mBelowAppWindowsContainers");
167 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
168 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
169 // window containers together and move them in-sync if/when needed.
170 private final NonAppWindowContainers mImeWindowsContainers =
171 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700172
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000173 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800174 private WindowState mTmpWindow2;
175 private WindowAnimator mTmpWindowAnimator;
176 private boolean mTmpRecoveringMemory;
177 private boolean mUpdateImeTarget;
178 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700179 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700180
Wale Ogunwale02319a62016-09-26 15:21:22 -0700181 // Mapping from a token IBinder to a WindowToken object on this display.
182 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
183
Andrii Kuliancd097992017-03-23 18:31:59 -0700184 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700185 int mInitialDisplayWidth = 0;
186 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700187 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700188
189 /**
190 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
191 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
192 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
193 */
Craig Mautner59c00972012-07-30 12:10:24 -0700194 int mBaseDisplayWidth = 0;
195 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700196 /**
197 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
198 * but can be set from Settings or via shell command "adb shell wm density".
199 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
200 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700201 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700202 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700203 private final DisplayInfo mDisplayInfo = new DisplayInfo();
204 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700205 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700206 /**
207 * For default display it contains real metrics, empty for others.
208 * @see WindowManagerService#createWatermarkInTransaction()
209 */
210 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
211 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
212 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
213 /**
214 * Compat metrics computed based on {@link #mDisplayMetrics}.
215 * @see #updateDisplayAndOrientation(int)
216 */
217 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
218
219 /** The desired scaling factor for compatible apps. */
220 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700221
Andrii Kulian8ee72852017-03-10 10:36:45 -0800222 /**
223 * Current rotation of the display.
224 * Constants as per {@link android.view.Surface.Rotation}.
225 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700226 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800227 */
228 private int mRotation = 0;
229 /**
230 * Last applied orientation of the display.
231 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
232 *
233 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
234 */
235 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
236 /**
237 * Flag indicating that the application is receiving an orientation that has different metrics
238 * than it expected. E.g. Portrait instead of Landscape.
239 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700240 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800241 */
242 private boolean mAltOrientation = false;
243 /**
244 * Orientation forced by some window. If there is no visible window that specifies orientation
245 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
246 *
247 * @see NonAppWindowContainers#getOrientation()
248 */
249 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
250 /**
251 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
252 * occluded.
253 *
254 * @see NonAppWindowContainers#getOrientation()
255 */
256 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
257
Andrii Kulian06d07d62017-03-14 11:11:47 -0700258 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700259 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700260
Craig Mautner39834192012-09-02 07:47:24 -0700261 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700262 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700263 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700264 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800265 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700266
Craig Mautnerdc548482014-02-05 13:35:24 -0800267 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700268 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800269
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800270 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
271 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700272 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700273
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700274 /** Detect user tapping outside of current focused task bounds .*/
275 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700276
Craig Mautner6601b7b2013-04-29 10:29:11 -0700277 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700278 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700279
Craig Mautner6601b7b2013-04-29 10:29:11 -0700280 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800281 private final Rect mTmpRect = new Rect();
282 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700283 private final RectF mTmpRectF = new RectF();
284 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800285 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700286
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800287 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700288
Craig Mautner95da1082014-02-24 17:54:35 -0800289 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700290 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800291
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700292 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700293 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700294
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800295 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700296
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800297 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
298
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000299 private boolean mHaveBootMsg = false;
300 private boolean mHaveApp = false;
301 private boolean mHaveWallpaper = false;
302 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700303
304 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
305
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700306 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
307 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000308 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
309 new ApplySurfaceChangesTransactionState();
310 private final ScreenshotApplicationState mScreenshotApplicationState =
311 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700312
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700313 // True if this display is in the process of being removed. Used to determine if the removal of
314 // the display's direct children should be allowed.
315 private boolean mRemovingDisplay = false;
316
Bryce Leed1871262017-06-12 14:12:29 -0700317 // {@code false} if this display is in the processing of being created.
318 private boolean mDisplayReady = false;
319
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700320 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800321 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700322 int mInputMethodAnimLayerAdjustment;
323
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800324 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
325 WindowStateAnimator winAnimator = w.mWinAnimator;
326 if (winAnimator.hasSurface()) {
327 final boolean wasAnimating = winAnimator.mWasAnimating;
328 final boolean nowAnimating = winAnimator.stepAnimationLocked(
329 mTmpWindowAnimator.mCurrentTime);
330 winAnimator.mWasAnimating = nowAnimating;
331 mTmpWindowAnimator.orAnimating(nowAnimating);
332
333 if (DEBUG_WALLPAPER) Slog.v(TAG,
334 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
335
336 if (wasAnimating && !winAnimator.mAnimating
337 && mWallpaperController.isWallpaperTarget(w)) {
338 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
339 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
340 if (DEBUG_LAYOUT_REPEATS) {
341 mService.mWindowPlacerLocked.debugLayoutRepeats(
342 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
343 }
344 }
345 }
346
347 final AppWindowToken atoken = w.mAppToken;
348 if (winAnimator.mDrawState == READY_TO_SHOW) {
349 if (atoken == null || atoken.allDrawn) {
350 if (w.performShowLocked()) {
351 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
352 if (DEBUG_LAYOUT_REPEATS) {
353 mService.mWindowPlacerLocked.debugLayoutRepeats(
354 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
355 }
356 }
357 }
358 }
359 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
360 if (appAnimator != null && appAnimator.thumbnail != null) {
361 if (appAnimator.thumbnailTransactionSeq
362 != mTmpWindowAnimator.mAnimTransactionSequence) {
363 appAnimator.thumbnailTransactionSeq =
364 mTmpWindowAnimator.mAnimTransactionSequence;
365 appAnimator.thumbnailLayer = 0;
366 }
367 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
368 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
369 }
370 }
371 };
372
373 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
374 final WindowStateAnimator winAnimator = w.mWinAnimator;
375 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
376 return;
377 }
378
379 final int flags = w.mAttrs.flags;
380
381 // If this window is animating, make a note that we have an animating window and take
382 // care of a request to run a detached wallpaper animation.
383 if (winAnimator.mAnimating) {
384 if (winAnimator.mAnimation != null) {
385 if ((flags & FLAG_SHOW_WALLPAPER) != 0
386 && winAnimator.mAnimation.getDetachWallpaper()) {
387 mTmpWindow = w;
388 }
389 final int color = winAnimator.mAnimation.getBackgroundColor();
390 if (color != 0) {
391 final TaskStack stack = w.getStack();
392 if (stack != null) {
393 stack.setAnimationBackground(winAnimator, color);
394 }
395 }
396 }
397 mTmpWindowAnimator.setAnimating(true);
398 }
399
400 // If this window's app token is running a detached wallpaper animation, make a note so
401 // we can ensure the wallpaper is displayed behind it.
402 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
403 if (appAnimator != null && appAnimator.animation != null
404 && appAnimator.animating) {
405 if ((flags & FLAG_SHOW_WALLPAPER) != 0
406 && appAnimator.animation.getDetachWallpaper()) {
407 mTmpWindow = w;
408 }
409
410 final int color = appAnimator.animation.getBackgroundColor();
411 if (color != 0) {
412 final TaskStack stack = w.getStack();
413 if (stack != null) {
414 stack.setAnimationBackground(winAnimator, color);
415 }
416 }
417 }
418 };
419
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800420 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
421 final int lostFocusUid = mTmpWindow.mOwnerUid;
422 final Handler handler = mService.mH;
423 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
424 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
425 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
426 w.mAttrs.hideTimeoutMilliseconds);
427 }
428 }
429 };
430
431 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
432 final AppWindowToken focusedApp = mService.mFocusedApp;
433 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
434 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
435
436 if (!w.canReceiveKeys()) {
437 return false;
438 }
439
440 final AppWindowToken wtoken = w.mAppToken;
441
442 // If this window's application has been removed, just skip it.
443 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
444 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
445 + (wtoken.removed ? "removed" : "sendingToBottom"));
446 return false;
447 }
448
449 if (focusedApp == null) {
450 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
451 + " using new focus @ " + w);
452 mTmpWindow = w;
453 return true;
454 }
455
456 if (!focusedApp.windowsAreFocusable()) {
457 // Current focused app windows aren't focusable...
458 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
459 + " focusable using new focus @ " + w);
460 mTmpWindow = w;
461 return true;
462 }
463
464 // Descend through all of the app tokens and find the first that either matches
465 // win.mAppToken (return win) or mFocusedApp (return null).
466 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
467 if (focusedApp.compareTo(wtoken) > 0) {
468 // App stack below focused app stack. No focus for you!!!
469 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
470 "findFocusedWindow: Reached focused app=" + focusedApp);
471 mTmpWindow = null;
472 return true;
473 }
474 }
475
476 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
477 mTmpWindow = w;
478 return true;
479 };
480
481 private final Consumer<WindowState> mPrepareWindowSurfaces =
482 w -> w.mWinAnimator.prepareSurfaceLocked(true);
483
484 private final Consumer<WindowState> mPerformLayout = w -> {
485 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
486 // wasting time and funky changes while a window is animating away.
487 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
488 || w.isGoneForLayoutLw();
489
490 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
491 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
492 + " mLayoutAttached=" + w.mLayoutAttached
493 + " screen changed=" + w.isConfigChanged());
494 final AppWindowToken atoken = w.mAppToken;
495 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
496 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
497 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
498 + " parentHidden=" + w.isParentWindowHidden());
499 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
500 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
501 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
502 + " parentHidden=" + w.isParentWindowHidden());
503 }
504
505 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
506 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
507 // since that means "perform layout as normal, just don't display").
508 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
509 || ((w.isConfigChanged() || w.setReportResizeHints())
510 && !w.isGoneForLayoutLw() &&
511 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
512 (w.mHasSurface && w.mAppToken != null &&
513 w.mAppToken.layoutConfigChanges)))) {
514 if (!w.mLayoutAttached) {
515 if (mTmpInitial) {
516 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
517 w.mContentChanged = false;
518 }
519 if (w.mAttrs.type == TYPE_DREAM) {
520 // Don't layout windows behind a dream, so that if it does stuff like hide
521 // the status bar we won't get a bad transition when it goes away.
522 mTmpWindow = w;
523 }
524 w.mLayoutNeeded = false;
525 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200526 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800527 mService.mPolicy.layoutWindowLw(w, null);
528 w.mLayoutSeq = mService.mLayoutSeq;
529
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200530 // If this is the first layout, we need to initialize the last inset values as
531 // otherwise we'd immediately cause an unnecessary resize.
532 if (firstLayout) {
533 w.updateLastInsetValues();
534 }
535
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800536 // Window frames may have changed. Update dim layer with the new bounds.
537 final Task task = w.getTask();
538 if (task != null) {
539 mDimLayerController.updateDimLayer(task);
540 }
541
542 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
543 + " mContainingFrame=" + w.mContainingFrame
544 + " mDisplayFrame=" + w.mDisplayFrame);
545 }
546 }
547 };
548
549 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
550 if (w.mLayoutAttached) {
551 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
552 + " mViewVisibility=" + w.mViewVisibility
553 + " mRelayoutCalled=" + w.mRelayoutCalled);
554 // If this view is GONE, then skip it -- keep the current frame, and let the caller
555 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
556 // windows, since that means "perform layout as normal, just don't display").
557 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
558 return;
559 }
560 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
561 || w.mLayoutNeeded) {
562 if (mTmpInitial) {
563 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
564 w.mContentChanged = false;
565 }
566 w.mLayoutNeeded = false;
567 w.prelayout();
568 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
569 w.mLayoutSeq = mService.mLayoutSeq;
570 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
571 + " mContainingFrame=" + w.mContainingFrame
572 + " mDisplayFrame=" + w.mDisplayFrame);
573 }
574 } else if (w.mAttrs.type == TYPE_DREAM) {
575 // Don't layout windows behind a dream, so that if it does stuff like hide the
576 // status bar we won't get a bad transition when it goes away.
577 mTmpWindow = mTmpWindow2;
578 }
579 };
580
581 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
582 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
583 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
584 return w.canBeImeTarget();
585 };
586
587 private final Consumer<WindowState> mApplyPostLayoutPolicy =
588 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
589 mService.mInputMethodTarget);
590
591 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
592 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
593 final boolean obscuredChanged = w.mObscured !=
594 mTmpApplySurfaceChangesTransactionState.obscured;
595 final RootWindowContainer root = mService.mRoot;
596 // Only used if default window
597 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
598
599 // Update effect.
600 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
601 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
602 final boolean isDisplayed = w.isDisplayedLw();
603
604 if (isDisplayed && w.isObscuringDisplay()) {
605 // This window completely covers everything behind it, so we want to leave all
606 // of them as undimmed (for performance reasons).
607 root.mObscuringWindow = w;
608 mTmpApplySurfaceChangesTransactionState.obscured = true;
609 }
610
611 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
612 root.handleNotObscuredLocked(w,
613 mTmpApplySurfaceChangesTransactionState.obscured,
614 mTmpApplySurfaceChangesTransactionState.syswin);
615
616 if (w.mHasSurface && isDisplayed) {
617 final int type = w.mAttrs.type;
618 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
619 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
620 mTmpApplySurfaceChangesTransactionState.syswin = true;
621 }
622 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
623 && w.mAttrs.preferredRefreshRate != 0) {
624 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
625 = w.mAttrs.preferredRefreshRate;
626 }
627 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
628 && w.mAttrs.preferredDisplayModeId != 0) {
629 mTmpApplySurfaceChangesTransactionState.preferredModeId
630 = w.mAttrs.preferredDisplayModeId;
631 }
632 }
633 }
634
635 w.applyDimLayerIfNeeded();
636
637 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
638 && mWallpaperController.isWallpaperTarget(w)) {
639 // This is the wallpaper target and its obscured state changed... make sure the
640 // current wallpaper's visibility has been updated accordingly.
641 mWallpaperController.updateWallpaperVisibility();
642 }
643
644 w.handleWindowMovedIfNeeded();
645
646 final WindowStateAnimator winAnimator = w.mWinAnimator;
647
648 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
649 w.mContentChanged = false;
650
651 // Moved from updateWindowsAndWallpaperLocked().
652 if (w.mHasSurface) {
653 // Take care of the window being ready to display.
654 final boolean committed = winAnimator.commitFinishDrawingLocked();
655 if (isDefaultDisplay && committed) {
656 if (w.mAttrs.type == TYPE_DREAM) {
657 // HACK: When a dream is shown, it may at that point hide the lock screen.
658 // So we need to redo the layout to let the phone window manager make this
659 // happen.
660 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
661 if (DEBUG_LAYOUT_REPEATS) {
662 surfacePlacer.debugLayoutRepeats(
663 "dream and commitFinishDrawingLocked true",
664 pendingLayoutChanges);
665 }
666 }
667 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
668 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
669 "First draw done in potential wallpaper target " + w);
670 root.mWallpaperMayChange = true;
671 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
672 if (DEBUG_LAYOUT_REPEATS) {
673 surfacePlacer.debugLayoutRepeats(
674 "wallpaper and commitFinishDrawingLocked true",
675 pendingLayoutChanges);
676 }
677 }
678 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700679 final TaskStack stack = w.getStack();
680 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
681 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800682 // Updates the shown frame before we set up the surface. This is needed
683 // because the resizing could change the top-left position (in addition to
684 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
685 // position the surface.
686 //
687 // If an animation is being started, we can't call this method because the
688 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700689 // we do want to update the position if the window is animating. We make an exception
690 // for the bounds animating state, where an application may have been waiting
691 // for an exit animation to start, but instead enters PiP. We need to ensure
692 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800693 winAnimator.computeShownFrameLocked();
694 }
695 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
696 }
697
698 final AppWindowToken atoken = w.mAppToken;
699 if (atoken != null) {
700 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
701 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
702 mTmpUpdateAllDrawn.add(atoken);
703 }
704 }
705
706 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
707 && w.isDisplayedLw()) {
708 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
709 }
710
711 w.updateResizingWindowIfNeeded();
712 };
713
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800714 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800715 * Create new {@link DisplayContent} instance, add itself to the root window container and
716 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700717 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800718 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700719 * @param layersController window layer controller used to assign layer to the windows on this
720 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700721 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
722 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700723 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700724 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700725 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800726 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
727 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
728 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
729 + " new=" + display);
730 }
731
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700732 mDisplay = display;
733 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700734 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700735 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700736 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700737 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700738 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700739 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700740 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800741 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700742 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800743 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700744
745 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700746 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700747 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700748 super.addChild(mAboveAppWindowsContainers, null);
749 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800750
751 // Add itself as a child to the root container.
752 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700753
754 // TODO(b/62541591): evaluate whether this is the best spot to declare the
755 // {@link DisplayContent} ready for use.
756 mDisplayReady = true;
757 }
758
759 boolean isReady() {
760 // The display is ready when the system and the individual display are both ready.
761 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700762 }
763
764 int getDisplayId() {
765 return mDisplayId;
766 }
767
Wale Ogunwale02319a62016-09-26 15:21:22 -0700768 WindowToken getWindowToken(IBinder binder) {
769 return mTokenMap.get(binder);
770 }
771
772 AppWindowToken getAppWindowToken(IBinder binder) {
773 final WindowToken token = getWindowToken(binder);
774 if (token == null) {
775 return null;
776 }
777 return token.asAppWindowToken();
778 }
779
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700780 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700781 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
782 if (dc != null) {
783 // We currently don't support adding a window token to the display if the display
784 // already has the binder mapped to another token. If there is a use case for supporting
785 // this moving forward we will either need to merge the WindowTokens some how or have
786 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700787 throw new IllegalArgumentException("Can't map token=" + token + " to display="
788 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700789 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700790 if (binder == null) {
791 throw new IllegalArgumentException("Can't map token=" + token + " to display="
792 + getName() + " binder is null");
793 }
794 if (token == null) {
795 throw new IllegalArgumentException("Can't map null token to display="
796 + getName() + " binder=" + binder);
797 }
798
Wale Ogunwale02319a62016-09-26 15:21:22 -0700799 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700800
801 if (token.asAppWindowToken() == null) {
802 // Add non-app token to container hierarchy on the display. App tokens are added through
803 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700804 switch (token.windowType) {
805 case TYPE_WALLPAPER:
806 mBelowAppWindowsContainers.addChild(token);
807 break;
808 case TYPE_INPUT_METHOD:
809 case TYPE_INPUT_METHOD_DIALOG:
810 mImeWindowsContainers.addChild(token);
811 break;
812 default:
813 mAboveAppWindowsContainers.addChild(token);
814 break;
815 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700816 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700817 }
818
819 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700820 final WindowToken token = mTokenMap.remove(binder);
821 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800822 token.setExiting();
823 }
824 return token;
825 }
826
827 /** Changes the display the input window token is housed on to this one. */
828 void reParentWindowToken(WindowToken token) {
829 final DisplayContent prevDc = token.getDisplayContent();
830 if (prevDc == this) {
831 return;
832 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800833 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
834 && token.asAppWindowToken() == null) {
835 // Removed the token from the map, but made sure it's not an app token before removing
836 // from parent.
837 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700838 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800839
840 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700841 }
842
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700843 void removeAppToken(IBinder binder) {
844 final WindowToken token = removeWindowToken(binder);
845 if (token == null) {
846 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
847 return;
848 }
849
850 final AppWindowToken appToken = token.asAppWindowToken();
851
852 if (appToken == null) {
853 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
854 return;
855 }
856
857 appToken.onRemovedFromDisplay();
858 }
859
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700860 Display getDisplay() {
861 return mDisplay;
862 }
863
Craig Mautner59c00972012-07-30 12:10:24 -0700864 DisplayInfo getDisplayInfo() {
865 return mDisplayInfo;
866 }
867
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700868 DisplayMetrics getDisplayMetrics() {
869 return mDisplayMetrics;
870 }
871
Andrii Kulian8ee72852017-03-10 10:36:45 -0800872 int getRotation() {
873 return mRotation;
874 }
875
876 void setRotation(int newRotation) {
877 mRotation = newRotation;
878 }
879
880 int getLastOrientation() {
881 return mLastOrientation;
882 }
883
884 void setLastOrientation(int orientation) {
885 mLastOrientation = orientation;
886 }
887
888 boolean getAltOrientation() {
889 return mAltOrientation;
890 }
891
892 void setAltOrientation(boolean altOrientation) {
893 mAltOrientation = altOrientation;
894 }
895
896 int getLastWindowForcedOrientation() {
897 return mLastWindowForcedOrientation;
898 }
899
Andrii Kulian06d07d62017-03-14 11:11:47 -0700900 /**
901 * Update rotation of the display.
902 *
903 * Returns true if the rotation has been changed. In this case YOU MUST CALL
904 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
905 */
906 boolean updateRotationUnchecked(boolean inTransaction) {
907 if (mService.mDeferredRotationPauseCount > 0) {
908 // Rotation updates have been paused temporarily. Defer the update until
909 // updates have been resumed.
910 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
911 return false;
912 }
913
914 ScreenRotationAnimation screenRotationAnimation =
915 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
916 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
917 // Rotation updates cannot be performed while the previous rotation change
918 // animation is still in progress. Skip this update. We will try updating
919 // again after the animation is finished and the display is unfrozen.
920 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
921 return false;
922 }
923 if (mService.mDisplayFrozen) {
924 // Even if the screen rotation animation has finished (e.g. isAnimating
925 // returns false), there is still some time where we haven't yet unfrozen
926 // the display. We also need to abort rotation here.
927 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
928 "Deferring rotation, still finishing previous rotation");
929 return false;
930 }
931
932 if (!mService.mDisplayEnabled) {
933 // No point choosing a rotation if the display is not enabled.
934 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
935 return false;
936 }
937
938 final int oldRotation = mRotation;
939 final int lastOrientation = mLastOrientation;
940 final boolean oldAltOrientation = mAltOrientation;
941 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700942 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
943 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700944
Robert Carr897215d2017-03-29 12:25:50 -0700945 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700946 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
947 if (seamlessRotated != null) {
948 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
949 // to complete (that is, waiting for windows to redraw). It's tempting to check
950 // w.mSeamlessRotationCount but that could be incorrect in the case of
951 // window-removal.
952 return false;
953 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700954 }
955
956 // TODO: Implement forced rotation changes.
957 // Set mAltOrientation to indicate that the application is receiving
958 // an orientation that has different metrics than it expected.
959 // eg. Portrait instead of Landscape.
960
961 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
962 lastOrientation, rotation);
963
964 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
965 + ", got rotation " + rotation + " which has "
966 + (altOrientation ? "incompatible" : "compatible") + " metrics");
967
968 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
969 // No change.
970 return false;
971 }
972
973 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
974 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
975 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
976
977 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
978 mService.mWaitingForConfig = true;
979 }
980
981 mRotation = rotation;
982 mAltOrientation = altOrientation;
983 if (isDefaultDisplay) {
984 mService.mPolicy.setRotationLw(rotation);
985 }
986
987 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
988 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
989 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
990 WINDOW_FREEZE_TIMEOUT_DURATION);
991
992 setLayoutNeeded();
993 final int[] anim = new int[2];
994 if (isDimming()) {
995 anim[0] = anim[1] = 0;
996 } else {
997 mService.mPolicy.selectRotationAnimationLw(anim);
998 }
999
1000 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001001 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001002 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1003 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1004 mDisplayId);
1005 } else {
1006 // The screen rotation animation uses a screenshot to freeze the screen
1007 // while windows resize underneath.
1008 // When we are rotating seamlessly, we allow the elements to transition
1009 // to their rotated state independently and without a freeze required.
1010 screenRotationAnimation = null;
1011
1012 // We have to reset this in case a window was removed before it
1013 // finished seamless rotation.
1014 mService.mSeamlessRotationCount = 0;
1015 }
1016
1017 // We need to update our screen size information to match the new rotation. If the rotation
1018 // has actually changed then this method will return true and, according to the comment at
1019 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1020 // By updating the Display info here it will be available to
1021 // #computeScreenConfiguration() later.
1022 updateDisplayAndOrientation(getConfiguration().uiMode);
1023
1024 if (!inTransaction) {
1025 if (SHOW_TRANSACTIONS) {
1026 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1027 }
1028 mService.openSurfaceTransaction();
1029 }
1030 try {
1031 // NOTE: We disable the rotation in the emulator because
1032 // it doesn't support hardware OpenGL emulation yet.
1033 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1034 && screenRotationAnimation.hasScreenshot()) {
1035 if (screenRotationAnimation.setRotationInTransaction(
1036 rotation, mService.mFxSession,
1037 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1038 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1039 mService.scheduleAnimationLocked();
1040 }
1041 }
1042
1043 if (rotateSeamlessly) {
1044 forAllWindows(w -> {
1045 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1046 }, true /* traverseTopToBottom */);
1047 }
1048
1049 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1050 } finally {
1051 if (!inTransaction) {
1052 mService.closeSurfaceTransaction();
1053 if (SHOW_LIGHT_TRANSACTIONS) {
1054 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1055 }
1056 }
1057 }
1058
1059 forAllWindows(w -> {
1060 // Discard surface after orientation change, these can't be reused.
1061 if (w.mAppToken != null) {
1062 w.mAppToken.destroySavedSurfaces();
1063 }
1064 if (w.mHasSurface && !rotateSeamlessly) {
1065 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
1066 w.mOrientationChanging = true;
1067 mService.mRoot.mOrientationChangeComplete = false;
1068 w.mLastFreezeDuration = 0;
1069 }
1070 w.mReportOrientationChanged = true;
1071 }, true /* traverseTopToBottom */);
1072
1073 if (rotateSeamlessly) {
1074 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1075 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1076 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1077 }
1078
1079 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1080 final WindowManagerService.RotationWatcher rotationWatcher
1081 = mService.mRotationWatchers.get(i);
1082 if (rotationWatcher.mDisplayId == mDisplayId) {
1083 try {
1084 rotationWatcher.mWatcher.onRotationChanged(rotation);
1085 } catch (RemoteException e) {
1086 // Ignore
1087 }
1088 }
1089 }
1090
1091 // TODO (multi-display): Magnification is supported only for the default display.
1092 // Announce rotation only if we will not animate as we already have the
1093 // windows in final state. Otherwise, we make this call at the rotation end.
1094 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1095 && isDefaultDisplay) {
1096 mService.mAccessibilityController.onRotationChangedLocked(this);
1097 }
1098
1099 return true;
1100 }
1101
1102 /**
1103 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1104 * changed.
1105 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1106 */
1107 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1108 // Use the effective "visual" dimensions based on current rotation
1109 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1110 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1111 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1112 int dw = realdw;
1113 int dh = realdh;
1114
1115 if (mAltOrientation) {
1116 if (realdw > realdh) {
1117 // Turn landscape into portrait.
1118 int maxw = (int)(realdh/1.3f);
1119 if (maxw < realdw) {
1120 dw = maxw;
1121 }
1122 } else {
1123 // Turn portrait into landscape.
1124 int maxh = (int)(realdw/1.3f);
1125 if (maxh < realdh) {
1126 dh = maxh;
1127 }
1128 }
1129 }
1130
1131 // Update application display metrics.
1132 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1133 mDisplayId);
1134 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1135 mDisplayId);
1136 mDisplayInfo.rotation = mRotation;
1137 mDisplayInfo.logicalWidth = dw;
1138 mDisplayInfo.logicalHeight = dh;
1139 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1140 mDisplayInfo.appWidth = appWidth;
1141 mDisplayInfo.appHeight = appHeight;
1142 if (isDefaultDisplay) {
1143 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1144 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1145 }
1146 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1147 if (mDisplayScalingDisabled) {
1148 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1149 } else {
1150 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1151 }
1152
1153 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1154 mDisplayInfo);
1155
1156 mBaseDisplayRect.set(0, 0, dw, dh);
1157
1158 if (isDefaultDisplay) {
1159 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1160 mCompatDisplayMetrics);
1161 }
1162 return mDisplayInfo;
1163 }
1164
1165 /**
1166 * Compute display configuration based on display properties and policy settings.
1167 * Do not call if mDisplayReady == false.
1168 */
1169 void computeScreenConfiguration(Configuration config) {
1170 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1171
1172 final int dw = displayInfo.logicalWidth;
1173 final int dh = displayInfo.logicalHeight;
1174 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1175 Configuration.ORIENTATION_LANDSCAPE;
1176 config.screenWidthDp =
1177 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1178 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1179 config.screenHeightDp =
1180 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1181 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001182
1183 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1184 final int leftInset = mTmpRect.left;
1185 final int topInset = mTmpRect.top;
1186 // appBounds at the root level should mirror the app screen size.
1187 config.setAppBounds(leftInset /*left*/, topInset /*top*/, leftInset + displayInfo.appWidth /*right*/,
1188 topInset + displayInfo.appHeight /*bottom*/);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001189 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1190 || displayInfo.rotation == Surface.ROTATION_270);
1191
1192 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1193 mDisplayMetrics.density, config);
1194
1195 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1196 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1197 ? Configuration.SCREENLAYOUT_ROUND_YES
1198 : Configuration.SCREENLAYOUT_ROUND_NO);
1199
1200 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1201 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1202 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1203 dh, mDisplayId);
1204 config.densityDpi = displayInfo.logicalDensityDpi;
1205
1206 config.colorMode =
1207 (displayInfo.isHdr()
1208 ? Configuration.COLOR_MODE_HDR_YES
1209 : Configuration.COLOR_MODE_HDR_NO)
1210 | (displayInfo.isWideColorGamut()
1211 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1212 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1213
1214 // Update the configuration based on available input devices, lid switch,
1215 // and platform configuration.
1216 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1217 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1218 config.navigation = Configuration.NAVIGATION_NONAV;
1219
1220 int keyboardPresence = 0;
1221 int navigationPresence = 0;
1222 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1223 final int len = devices != null ? devices.length : 0;
1224 for (int i = 0; i < len; i++) {
1225 InputDevice device = devices[i];
1226 if (!device.isVirtual()) {
1227 final int sources = device.getSources();
1228 final int presenceFlag = device.isExternal() ?
1229 WindowManagerPolicy.PRESENCE_EXTERNAL :
1230 WindowManagerPolicy.PRESENCE_INTERNAL;
1231
1232 // TODO(multi-display): Configure on per-display basis.
1233 if (mService.mIsTouchDevice) {
1234 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1235 InputDevice.SOURCE_TOUCHSCREEN) {
1236 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1237 }
1238 } else {
1239 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1240 }
1241
1242 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1243 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1244 navigationPresence |= presenceFlag;
1245 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1246 && config.navigation == Configuration.NAVIGATION_NONAV) {
1247 config.navigation = Configuration.NAVIGATION_DPAD;
1248 navigationPresence |= presenceFlag;
1249 }
1250
1251 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1252 config.keyboard = Configuration.KEYBOARD_QWERTY;
1253 keyboardPresence |= presenceFlag;
1254 }
1255 }
1256 }
1257
1258 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1259 config.navigation = Configuration.NAVIGATION_DPAD;
1260 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1261 }
1262
1263 // Determine whether a hard keyboard is available and enabled.
1264 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1265 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1266 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1267 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1268 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1269 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1270 }
1271
1272 // Let the policy update hidden states.
1273 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1274 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1275 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1276 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1277 }
1278
1279 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1280 int displayId) {
1281 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1282 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1283 final int unrotDw, unrotDh;
1284 if (rotated) {
1285 unrotDw = dh;
1286 unrotDh = dw;
1287 } else {
1288 unrotDw = dw;
1289 unrotDh = dh;
1290 }
1291 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1292 displayId);
1293 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1294 displayId);
1295 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1296 displayId);
1297 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1298 displayId);
1299 return sw;
1300 }
1301
1302 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1303 DisplayMetrics dm, int dw, int dh, int displayId) {
1304 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1305 displayId);
1306 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1307 uiMode, displayId);
1308 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1309 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1310 if (curSize == 0 || size < curSize) {
1311 curSize = size;
1312 }
1313 return curSize;
1314 }
1315
1316 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1317 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1318
1319 // We need to determine the smallest width that will occur under normal
1320 // operation. To this, start with the base screen size and compute the
1321 // width under the different possible rotations. We need to un-rotate
1322 // the current screen dimensions before doing this.
1323 int unrotDw, unrotDh;
1324 if (rotated) {
1325 unrotDw = dh;
1326 unrotDh = dw;
1327 } else {
1328 unrotDw = dw;
1329 unrotDh = dh;
1330 }
1331 displayInfo.smallestNominalAppWidth = 1<<30;
1332 displayInfo.smallestNominalAppHeight = 1<<30;
1333 displayInfo.largestNominalAppWidth = 0;
1334 displayInfo.largestNominalAppHeight = 0;
1335 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1336 unrotDh);
1337 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1338 unrotDw);
1339 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1340 unrotDh);
1341 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1342 unrotDw);
1343 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1344 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1345 displayId);
1346 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1347 displayId);
1348 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1349 displayId);
1350 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1351 displayId);
1352 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1353 outConfig.screenLayout = sl;
1354 }
1355
1356 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1357 int uiMode, int displayId) {
1358 // Get the app screen size at this rotation.
1359 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1360 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1361
1362 // Compute the screen layout size class for this rotation.
1363 int longSize = w;
1364 int shortSize = h;
1365 if (longSize < shortSize) {
1366 int tmp = longSize;
1367 longSize = shortSize;
1368 shortSize = tmp;
1369 }
1370 longSize = (int)(longSize/density);
1371 shortSize = (int)(shortSize/density);
1372 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1373 }
1374
1375 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1376 int uiMode, int dw, int dh) {
1377 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1378 displayId);
1379 if (width < displayInfo.smallestNominalAppWidth) {
1380 displayInfo.smallestNominalAppWidth = width;
1381 }
1382 if (width > displayInfo.largestNominalAppWidth) {
1383 displayInfo.largestNominalAppWidth = width;
1384 }
1385 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1386 displayId);
1387 if (height < displayInfo.smallestNominalAppHeight) {
1388 displayInfo.smallestNominalAppHeight = height;
1389 }
1390 if (height > displayInfo.largestNominalAppHeight) {
1391 displayInfo.largestNominalAppHeight = height;
1392 }
1393 }
1394
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001395 DockedStackDividerController getDockedDividerController() {
1396 return mDividerControllerLocked;
1397 }
1398
Winson Chung655332c2016-10-31 13:14:28 -07001399 PinnedStackController getPinnedStackController() {
1400 return mPinnedStackControllerLocked;
1401 }
1402
Jeff Browna506a6e2013-06-04 00:02:38 -07001403 /**
1404 * Returns true if the specified UID has access to this display.
1405 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001406 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001407 return mDisplay.hasAccess(uid);
1408 }
1409
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001410 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001411 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001412 }
1413
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001414 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001415 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001416 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001417 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001418 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001419 }
1420
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001421 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001422 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1423 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001424 if (stack.mStackId == stackId) {
1425 return stack;
1426 }
1427 }
1428 return null;
1429 }
1430
Bryce Lee48f4b572017-04-10 10:54:15 -07001431 @VisibleForTesting
1432 int getStackCount() {
1433 return mTaskStackContainers.size();
1434 }
1435
1436 @VisibleForTesting
1437 int getStaskPosById(int stackId) {
1438 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1439 final TaskStack stack = mTaskStackContainers.get(i);
1440 if (stack.mStackId == stackId) {
1441 return i;
1442 }
1443 }
1444 return -1;
1445 }
1446
Andrii Kulian441e4492016-09-29 15:25:00 -07001447 @Override
1448 void onConfigurationChanged(Configuration newParentConfig) {
1449 super.onConfigurationChanged(newParentConfig);
1450
Andrii Kulian3a507b52016-09-19 18:14:12 -07001451 // The display size information is heavily dependent on the resources in the current
1452 // configuration, so we need to reconfigure it every time the configuration changes.
1453 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1454 mService.reconfigureDisplayLocked(this);
1455
1456 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001457 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001458 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001459
Andrii Kulian441e4492016-09-29 15:25:00 -07001460 /**
1461 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1462 * bounds were updated.
1463 */
1464 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001465 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1466 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001467 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001468 changedStackList.add(stack.mStackId);
1469 }
1470 }
Winson Chung32c566f2017-04-11 18:31:21 -07001471
1472 // If there was no pinned stack, we still need to notify the controller of the display info
1473 // update as a result of the config change. We do this here to consolidate the flow between
1474 // changes when there is and is not a stack.
1475 if (getStackById(PINNED_STACK_ID) == null) {
1476 mPinnedStackControllerLocked.onDisplayInfoChanged();
1477 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001478 }
1479
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001480 @Override
1481 boolean fillsParent() {
1482 return true;
1483 }
1484
1485 @Override
1486 boolean isVisible() {
1487 return true;
1488 }
1489
1490 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001491 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001492 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001493 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001494 }
1495
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001496 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001497 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1498 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1499 // target, or here in order if there isn't an IME target.
1500 if (traverseTopToBottom) {
1501 for (int i = mChildren.size() - 1; i >= 0; --i) {
1502 final DisplayChildWindowContainer child = mChildren.get(i);
1503 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1504 // In this case the Ime windows will be processed above their target so we skip
1505 // here.
1506 continue;
1507 }
1508 if (child.forAllWindows(callback, traverseTopToBottom)) {
1509 return true;
1510 }
1511 }
1512 } else {
1513 final int count = mChildren.size();
1514 for (int i = 0; i < count; i++) {
1515 final DisplayChildWindowContainer child = mChildren.get(i);
1516 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1517 // In this case the Ime windows will be processed above their target so we skip
1518 // here.
1519 continue;
1520 }
1521 if (child.forAllWindows(callback, traverseTopToBottom)) {
1522 return true;
1523 }
1524 }
1525 }
1526 return false;
1527 }
1528
1529 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1530 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1531 }
1532
Wale Ogunwale399c8692017-05-08 14:22:42 -07001533 @Override
1534 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001535 final WindowManagerPolicy policy = mService.mPolicy;
1536
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001537 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001538 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001539 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001540 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001541 // If the display is frozen, some activities may be in the middle of restarting, and
1542 // thus have removed their old window. If the window has the flag to hide the lock
1543 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1544 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001545 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001546 } else if (policy.isKeyguardLocked()) {
1547 // Use the last orientation the while the display is frozen with the keyguard
1548 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1549 // window. We don't want to check the show when locked window directly though as
1550 // things aren't stable while the display is frozen, for example the window could be
1551 // momentarily unavailable due to activity relaunch.
1552 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001553 + "return " + mLastOrientation);
1554 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001555 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001556 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001557 final int orientation = mAboveAppWindowsContainers.getOrientation();
1558 if (orientation != SCREEN_ORIENTATION_UNSET) {
1559 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001560 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001561 }
1562
Wale Ogunwale399c8692017-05-08 14:22:42 -07001563 // Top system windows are not requesting an orientation. Start searching from apps.
1564 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001565 }
1566
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001567 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001568 // Check if display metrics changed and update base values if needed.
1569 updateBaseDisplayMetricsIfNeeded();
1570
Craig Mautner722285e2012-09-07 13:55:58 -07001571 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001572 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001573
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001574 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1575 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001576 }
Craig Mautner722285e2012-09-07 13:55:58 -07001577 }
1578
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001579 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001580 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1581 if (displayManagerInternal != null) {
1582 // Bootstrap the default logical display from the display manager.
1583 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1584 if (newDisplayInfo != null) {
1585 mDisplayInfo.copyFrom(newDisplayInfo);
1586 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001587 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001588
Andrii Kuliancd097992017-03-23 18:31:59 -07001589 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1590 mDisplayInfo.logicalDensityDpi);
1591 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1592 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1593 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001594 }
1595
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001596 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001597 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001598 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001599 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001600 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1601 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001602 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001603 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001604 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001605 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001606 out.set(left, top, left + width, top + height);
1607 }
1608
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001609 private void getLogicalDisplayRect(Rect out, int orientation) {
1610 getLogicalDisplayRect(out);
1611
1612 // Rotate the Rect if needed.
1613 final int currentRotation = mDisplayInfo.rotation;
1614 final int rotationDelta = deltaRotation(currentRotation, orientation);
1615 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1616 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1617 mTmpRectF.set(out);
1618 mTmpMatrix.mapRect(mTmpRectF);
1619 mTmpRectF.round(out);
1620 }
1621 }
1622
Andrii Kuliancd097992017-03-23 18:31:59 -07001623 /**
1624 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1625 * values.
1626 */
1627 private void updateBaseDisplayMetricsIfNeeded() {
1628 // Get real display metrics without overrides from WM.
1629 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1630 final int orientation = mDisplayInfo.rotation;
1631 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1632 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1633 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1634 final int newDensity = mDisplayInfo.logicalDensityDpi;
1635
1636 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1637 || mInitialDisplayHeight != newHeight
1638 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1639
1640 if (displayMetricsChanged) {
1641 // Check if display size or density is forced.
1642 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1643 || mBaseDisplayHeight != mInitialDisplayHeight;
1644 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1645
1646 // If there is an override set for base values - use it, otherwise use new values.
1647 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1648 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1649 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1650
1651 // Real display metrics changed, so we should also update initial values.
1652 mInitialDisplayWidth = newWidth;
1653 mInitialDisplayHeight = newHeight;
1654 mInitialDisplayDensity = newDensity;
1655 mService.reconfigureDisplayLocked(this);
1656 }
1657 }
1658
Bryce Lee27cec322017-03-21 09:41:37 -07001659 /** Sets the maximum width the screen resolution can be */
1660 void setMaxUiWidth(int width) {
1661 if (DEBUG_DISPLAY) {
1662 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1663 }
1664
1665 mMaxUiWidth = width;
1666
1667 // Update existing metrics.
1668 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1669 }
1670
1671 /** Update base (override) display metrics. */
1672 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1673 mBaseDisplayWidth = baseWidth;
1674 mBaseDisplayHeight = baseHeight;
1675 mBaseDisplayDensity = baseDensity;
1676
1677 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1678 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1679 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1680 mBaseDisplayWidth = mMaxUiWidth;
1681
1682 if (DEBUG_DISPLAY) {
1683 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1684 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1685 + " on display:" + getDisplayId());
1686 }
1687 }
1688
1689 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1690 }
1691
Chong Zhangf66db432016-01-13 10:39:51 -08001692 void getContentRect(Rect out) {
1693 out.set(mContentRect);
1694 }
1695
Wale Ogunwale1666e312016-12-16 11:27:18 -08001696 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1697 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1698 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001699
Wale Ogunwale1666e312016-12-16 11:27:18 -08001700 TaskStack stack = getStackById(stackId);
1701 if (stack != null) {
1702 // It's already attached to the display...clear mDeferRemoval and move stack to
1703 // appropriate z-order on display as needed.
1704 stack.mDeferRemoval = false;
1705 // We're not moving the display to front when we're adding stacks, only when
1706 // requested to change the position of stack explicitly.
1707 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1708 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001709 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001710 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001711 mTaskStackContainers.addStackToDisplay(stack, onTop);
1712 }
1713
Wale Ogunwale1666e312016-12-16 11:27:18 -08001714 if (stackId == DOCKED_STACK_ID) {
1715 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001716 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001717 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001718 }
1719
Andrii Kulian51c1b672017-04-07 18:39:32 -07001720 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001721 final DisplayContent prevDc = stack.getDisplayContent();
1722 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001723 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1724 + " which is not currently attached to any display");
1725 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001726 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001727 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1728 + " to its current displayId=" + mDisplayId);
1729 }
1730
Wale Ogunwale1666e312016-12-16 11:27:18 -08001731 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001732 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001733 }
1734
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001735 @Override
1736 protected void addChild(DisplayChildWindowContainer child,
1737 Comparator<DisplayChildWindowContainer> comparator) {
1738 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1739 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001740
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001741 @Override
1742 protected void addChild(DisplayChildWindowContainer child, int index) {
1743 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1744 }
1745
1746 @Override
1747 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001748 // Only allow removal of direct children from this display if the display is in the process
1749 // of been removed.
1750 if (mRemovingDisplay) {
1751 super.removeChild(child);
1752 return;
1753 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001754 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001755 }
1756
Andrii Kuliand2765632016-12-12 22:26:34 -08001757 @Override
1758 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1759 // Children of the display are statically ordered, so the real intention here is to perform
1760 // the operation on the display and not the static direct children.
1761 getParent().positionChildAt(position, this, includingParents);
1762 }
1763
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001764 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001765 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1766 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001767 final int taskId = stack.taskIdFromPoint(x, y);
1768 if (taskId != -1) {
1769 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001770 }
1771 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001772 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001773 }
1774
Chong Zhang8e89b312015-09-09 15:09:30 -07001775 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001776 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001777 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001778 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001779 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001780 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001781 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001782 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1783 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001784 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001785 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001786 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001787
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001788 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1789 if (mTmpTaskForResizePointSearchResult.searchDone) {
1790 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001791 }
1792 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001793 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001794 }
1795
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001796 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001797 // The provided task is the task on this display with focus, so if WindowManagerService's
1798 // focused app is not on this display, focusedTask will be null.
1799 if (focusedTask == null) {
1800 mTouchExcludeRegion.setEmpty();
1801 } else {
1802 mTouchExcludeRegion.set(mBaseDisplayRect);
1803 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1804 mTmpRect2.setEmpty();
1805 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1806 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1807 stack.setTouchExcludeRegion(
1808 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1809 }
1810 // If we removed the focused task above, add it back and only leave its
1811 // outside touch area in the exclusion. TapDectector is not interested in
1812 // any touch inside the focused task itself.
1813 if (!mTmpRect2.isEmpty()) {
1814 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1815 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001816 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001817 final WindowState inputMethod = mService.mInputMethodWindow;
1818 if (inputMethod != null && inputMethod.isVisibleLw()) {
1819 // If the input method is visible and the user is typing, we don't want these touch
1820 // events to be intercepted and used to change focus. This would likely cause a
1821 // disappearance of the input method.
1822 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001823 if (inputMethod.getDisplayId() == mDisplayId) {
1824 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1825 } else {
1826 // IME is on a different display, so we need to update its tap detector.
1827 // TODO(multidisplay): Remove when IME will always appear on same display.
1828 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1829 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001830 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001831 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1832 WindowState win = mTapExcludedWindows.get(i);
1833 win.getTouchableRegion(mTmpRegion);
1834 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1835 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001836 // TODO(multi-display): Support docked stacks on secondary displays.
1837 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001838 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001839 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001840 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1841 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001842 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001843 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001844 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001845 }
1846
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001847 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001848 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001849 super.switchUser();
1850 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001851 }
1852
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001853 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001854 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1855 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001856 }
1857 }
1858
1859 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001860 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001861 }
1862
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001863 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001864 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001865 }
1866
1867 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001868 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001869 }
1870
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001871 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001872 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001873 }
1874
Wale Ogunwale10124582016-09-15 20:25:50 -07001875 @Override
1876 void removeIfPossible() {
1877 if (isAnimating()) {
1878 mDeferredRemoval = true;
1879 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001880 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001881 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001882 }
1883
Wale Ogunwale10124582016-09-15 20:25:50 -07001884 @Override
1885 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001886 mRemovingDisplay = true;
1887 try {
1888 super.removeImmediately();
1889 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1890 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001891 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001892 mService.unregisterPointerEventListener(mTapDetector);
1893 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1894 }
1895 } finally {
1896 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001897 }
Craig Mautner95da1082014-02-24 17:54:35 -08001898 }
1899
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001900 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001901 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001902 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001903 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1904
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001905 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001906 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001907 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001908 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001909 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001910 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001911 }
1912
Andrii Kulian0214ed92017-05-16 13:44:05 -07001913 /** @return 'true' if removal of this display content is deferred due to active animation. */
1914 boolean isRemovalDeferred() {
1915 return mDeferredRemoval;
1916 }
1917
Wale Ogunwale10124582016-09-15 20:25:50 -07001918 boolean animateForIme(float interpolatedValue, float animationTarget,
1919 float dividerAnimationTarget) {
1920 boolean updated = false;
1921
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001922 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1923 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001924 if (stack == null || !stack.isAdjustedForIme()) {
1925 continue;
1926 }
1927
1928 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1929 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1930 updated = true;
1931 } else {
1932 mDividerControllerLocked.mLastAnimationProgress =
1933 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1934 mDividerControllerLocked.mLastDividerProgress =
1935 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1936 updated |= stack.updateAdjustForIme(
1937 mDividerControllerLocked.mLastAnimationProgress,
1938 mDividerControllerLocked.mLastDividerProgress,
1939 false /* force */);
1940 }
1941 if (interpolatedValue >= 1f) {
1942 stack.endImeAdjustAnimation();
1943 }
1944 }
1945
1946 return updated;
1947 }
1948
1949 boolean clearImeAdjustAnimation() {
1950 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001951 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1952 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001953 if (stack != null && stack.isAdjustedForIme()) {
1954 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1955 changed = true;
1956 }
1957 }
1958 return changed;
1959 }
1960
1961 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001962 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1963 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001964 if (stack.isVisible() && stack.isAdjustedForIme()) {
1965 stack.beginImeAdjustAnimation();
1966 }
1967 }
1968 }
1969
1970 void adjustForImeIfNeeded() {
1971 final WindowState imeWin = mService.mInputMethodWindow;
1972 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1973 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001974 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001975 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1976 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1977 imeTargetStack.getDockSide() : DOCKED_INVALID;
1978 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1979 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1980 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1981 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1982 final boolean imeHeightChanged = imeVisible &&
1983 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1984
1985 // The divider could be adjusted for IME position, or be thinner than usual,
1986 // or both. There are three possible cases:
1987 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1988 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1989 // - If IME is not visible, divider is not moved and is normal width.
1990
1991 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001992 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1993 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001994 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07001995 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
1996 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001997 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1998 } else {
1999 stack.resetAdjustedForIme(false);
2000 }
2001 }
2002 mDividerControllerLocked.setAdjustedForIme(
2003 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2004 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002005 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2006 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002007 stack.resetAdjustedForIme(!dockVisible);
2008 }
2009 mDividerControllerLocked.setAdjustedForIme(
2010 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2011 }
Winson Chung655332c2016-10-31 13:14:28 -07002012 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002013 }
2014
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002015 void setInputMethodAnimLayerAdjustment(int adj) {
2016 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2017 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002018 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002019 }
2020
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002021 /**
2022 * If a window that has an animation specifying a colored background and the current wallpaper
2023 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2024 * suddenly disappear.
2025 */
2026 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002027 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2028 w -> w.mIsWallpaper && w.isVisibleNow());
2029
2030 if (visibleWallpaper != null) {
2031 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002032 }
2033 return winAnimator.mAnimLayer;
2034 }
2035
Wale Ogunwale10124582016-09-15 20:25:50 -07002036 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002037 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2038 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002039 stack.prepareFreezingTaskBounds();
2040 }
2041 }
2042
Wale Ogunwale94744212015-09-21 19:01:47 -07002043 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002044 getLogicalDisplayRect(mTmpRect, newRotation);
2045
2046 // Compute a transform matrix to undo the coordinate space transformation,
2047 // and present the window at the same physical position it previously occupied.
2048 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2049 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2050
2051 mTmpRectF.set(bounds);
2052 mTmpMatrix.mapRect(mTmpRectF);
2053 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002054 }
2055
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002056 static int deltaRotation(int oldRotation, int newRotation) {
2057 int delta = newRotation - oldRotation;
2058 if (delta < 0) delta += 4;
2059 return delta;
2060 }
2061
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002062 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002063 Matrix outMatrix) {
2064 // For rotations without Z-ordering we don't need the target rectangle's position.
2065 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2066 displayHeight, outMatrix);
2067 }
2068
2069 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2070 float displayWidth, float displayHeight, Matrix outMatrix) {
2071 switch (rotation) {
2072 case ROTATION_0:
2073 outMatrix.reset();
2074 break;
2075 case ROTATION_270:
2076 outMatrix.setRotate(270, 0, 0);
2077 outMatrix.postTranslate(0, displayHeight);
2078 outMatrix.postTranslate(rectTop, 0);
2079 break;
2080 case ROTATION_180:
2081 outMatrix.reset();
2082 break;
2083 case ROTATION_90:
2084 outMatrix.setRotate(90, 0, 0);
2085 outMatrix.postTranslate(displayWidth, 0);
2086 outMatrix.postTranslate(-rectTop, rectLeft);
2087 break;
2088 }
2089 }
2090
Craig Mautnera91f9e22012-09-14 16:22:08 -07002091 public void dump(String prefix, PrintWriter pw) {
2092 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2093 final String subPrefix = " " + prefix;
2094 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2095 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2096 pw.print("dpi");
2097 if (mInitialDisplayWidth != mBaseDisplayWidth
2098 || mInitialDisplayHeight != mBaseDisplayHeight
2099 || mInitialDisplayDensity != mBaseDisplayDensity) {
2100 pw.print(" base=");
2101 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2102 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2103 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002104 if (mDisplayScalingDisabled) {
2105 pw.println(" noscale");
2106 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002107 pw.print(" cur=");
2108 pw.print(mDisplayInfo.logicalWidth);
2109 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2110 pw.print(" app=");
2111 pw.print(mDisplayInfo.appWidth);
2112 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2113 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2114 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2115 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2116 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002117 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2118 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002119
Craig Mautnerdc548482014-02-05 13:35:24 -08002120 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002121 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002122 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2123 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002124 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002125 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002126
Craig Mautnerdc548482014-02-05 13:35:24 -08002127 pw.println();
2128 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002129 pw.println();
2130 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002131 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002132 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002133 pw.print(" Exiting #"); pw.print(i);
2134 pw.print(' '); pw.print(token);
2135 pw.println(':');
2136 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002137 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002138 }
Craig Mautner59c00972012-07-30 12:10:24 -07002139 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002140 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002141 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002142 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002143 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002144 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002145
2146 if (mInputMethodAnimLayerAdjustment != 0) {
2147 pw.println(subPrefix
2148 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2149 }
Craig Mautner59c00972012-07-30 12:10:24 -07002150 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002151
2152 @Override
2153 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002154 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002155 }
2156
2157 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002158 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002159 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002160
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002161 /** Checks if stack with provided id is visible on this display. */
2162 boolean isStackVisible(int stackId) {
2163 final TaskStack stack = getStackById(stackId);
2164 return (stack != null && stack.isVisible());
2165 }
2166
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002167 /**
2168 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2169 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002170 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002171 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002172 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002173 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002174
2175 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002176 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002177 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002178 */
Jorim Jaggife762342016-10-13 14:33:27 +02002179 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002180 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002181 }
2182
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002183 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002184 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002185 final int x = (int) xf;
2186 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002187 final WindowState touchedWin = getWindow(w -> {
2188 final int flags = w.mAttrs.flags;
2189 if (!w.isVisibleLw()) {
2190 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002191 }
2192 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002193 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002194 }
2195
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002196 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002197 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002198 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002199 }
2200
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002201 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002202
2203 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002204 return mTmpRegion.contains(x, y) || touchFlags == 0;
2205 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002206
2207 return touchedWin;
2208 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002209
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002210 boolean canAddToastWindowForUid(int uid) {
2211 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002212 // Also if the app is focused adding more than one toast at
2213 // a time for better backwards compatibility.
2214 final WindowState focusedWindowForUid = getWindow(w ->
2215 w.mOwnerUid == uid && w.isFocused());
2216 if (focusedWindowForUid != null) {
2217 return true;
2218 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002219 final WindowState win = getWindow(w ->
2220 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2221 && !w.mWindowRemovalAllowed);
2222 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002223 }
2224
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002225 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002226 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2227 return;
2228 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002229
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002230 // Used to communicate the old focus to the callback method.
2231 mTmpWindow = oldFocus;
2232
2233 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002234 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002235
2236 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002237 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002238
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002239 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002240
2241 if (mTmpWindow == null) {
2242 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2243 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002244 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002245 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002246 }
2247
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002248 /** Updates the layer assignment of windows on this display. */
2249 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002250 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002251 if (setLayoutNeeded) {
2252 setLayoutNeeded();
2253 }
2254 }
2255
Wale Ogunwale1666e312016-12-16 11:27:18 -08002256 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2257 // moving containers or resizing them. Need to investigate the best way to have it automatically
2258 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002259 void layoutAndAssignWindowLayersIfNeeded() {
2260 mService.mWindowsChanged = true;
2261 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002262
2263 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2264 false /*updateInputWindows*/)) {
2265 assignWindowLayers(false /* setLayoutNeeded */);
2266 }
2267
2268 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2269 mService.mWindowPlacerLocked.performSurfacePlacement();
2270 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2271 }
2272
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002273 /** Returns true if a leaked surface was destroyed */
2274 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002275 // Used to indicate that a surface was leaked.
2276 mTmpWindow = null;
2277 forAllWindows(w -> {
2278 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002279 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002280 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002281 }
2282 if (!mService.mSessions.contains(wsa.mSession)) {
2283 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002284 + w + " surface=" + wsa.mSurfaceController
2285 + " token=" + w.mToken
2286 + " pid=" + w.mSession.mPid
2287 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002288 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002289 mService.mForceRemoves.add(w);
2290 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002291 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002292 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002293 + w + " surface=" + wsa.mSurfaceController
2294 + " token=" + w.mAppToken
2295 + " saved=" + w.hasSavedSurface());
2296 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002297 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002298 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002299 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002300 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002301
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002302 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002303 }
2304
2305 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002306 * Determine and return the window that should be the IME target.
2307 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2308 * @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 +00002309 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002310 WindowState computeImeTarget(boolean updateImeTarget) {
2311 if (mService.mInputMethodWindow == null) {
2312 // There isn't an IME so there shouldn't be a target...That was easy!
2313 if (updateImeTarget) {
2314 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2315 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2316 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2317 }
2318 return null;
2319 }
2320
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002321 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2322 // same display. Or even when the current IME/target are not on the same screen as the next
2323 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002324 mUpdateImeTarget = updateImeTarget;
2325 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002326
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002327
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002328 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2329 // to be on top of it, but it is not -really- where input will go. So look down below
2330 // for a real window to target...
2331 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2332 final AppWindowToken token = target.mAppToken;
2333 if (token != null) {
2334 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2335 if (betterTarget != null) {
2336 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002337 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002338 }
2339 }
2340
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002341 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2342 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002343
2344 // Now, a special case -- if the last target's window is in the process of exiting, and is
2345 // above the new target, keep on the last target to avoid flicker. Consider for example a
2346 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2347 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2348 // scrim.
2349 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002350 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2351 && (target == null
2352 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002353 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002354 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002355 }
2356
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002357 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2358 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002359
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002360 if (target == null) {
2361 if (updateImeTarget) {
2362 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2363 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2364 + Debug.getCallers(4) : ""));
2365 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2366 }
2367
2368 return null;
2369 }
2370
2371 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002372 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2373 if (token != null) {
2374
2375 // Now some fun for dealing with window animations that modify the Z order. We need
2376 // to look at all windows below the current target that are in this app, finding the
2377 // highest visible one in layering.
2378 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002379 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002380 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002381 }
2382
2383 if (highestTarget != null) {
2384 final AppTransition appTransition = mService.mAppTransition;
2385 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2386 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2387 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002388 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002389
2390 if (appTransition.isTransitionSet()) {
2391 // If we are currently setting up for an animation, hold everything until we
2392 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002393 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2394 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002395 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002396 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002397 // If the window we are currently targeting is involved with an animation,
2398 // and it is on top of the next target we will be over, then hold off on
2399 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002400 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2401 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002402 }
2403 }
2404 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002405
2406 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2407 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2408 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002409 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002410 }
2411
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002412 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002413 }
2414
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002415 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2416 if (target == mService.mInputMethodTarget
2417 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2418 && mInputMethodAnimLayerAdjustment == layerAdj) {
2419 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002420 }
2421
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002422 mService.mInputMethodTarget = target;
2423 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2424 setInputMethodAnimLayerAdjustment(layerAdj);
2425 assignWindowLayers(false /* setLayoutNeeded */);
2426 }
2427
2428 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2429 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2430 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2431 }
2432
2433 // Used to indicate we have reached the first window in the range we are interested in.
2434 mTmpWindow = null;
2435
2436 // TODO: Figure-out a more efficient way to do this.
2437 final WindowState candidate = getWindow(w -> {
2438 if (w == top) {
2439 // Reached the first window in the range we are interested in.
2440 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002441 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002442 if (mTmpWindow == null) {
2443 return false;
2444 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002445
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002446 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2447 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002448 }
2449 // If we reached the bottom of the range of windows we are considering,
2450 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002451 if (w == bottom) {
2452 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002453 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002454 return false;
2455 });
2456
2457 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002458 }
2459
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002460 void setLayoutNeeded() {
2461 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2462 mLayoutNeeded = true;
2463 }
2464
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002465 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002466 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2467 mLayoutNeeded = false;
2468 }
2469
2470 boolean isLayoutNeeded() {
2471 return mLayoutNeeded;
2472 }
2473
Wale Ogunwale02319a62016-09-26 15:21:22 -07002474 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2475 if (mTokenMap.isEmpty()) {
2476 return;
2477 }
2478 pw.println(" Display #" + mDisplayId);
2479 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2480 while (it.hasNext()) {
2481 final WindowToken token = it.next();
2482 pw.print(" ");
2483 pw.print(token);
2484 if (dumpAll) {
2485 pw.println(':');
2486 token.dump(pw, " ");
2487 } else {
2488 pw.println();
2489 }
2490 }
2491 }
2492
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002493 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002494 final int[] index = new int[1];
2495 forAllWindows(w -> {
2496 final WindowStateAnimator wAnim = w.mWinAnimator;
2497 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2498 index[0] = index[0] + 1;
2499 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002500 }
2501
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002502 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002503 forAllWindows(w -> {
2504 w.mWinAnimator.enableSurfaceTrace(fd);
2505 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002506 }
2507
2508 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002509 forAllWindows(w -> {
2510 w.mWinAnimator.disableSurfaceTrace();
2511 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002512 }
2513
Jorim Jaggife762342016-10-13 14:33:27 +02002514 /**
2515 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2516 */
2517 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2518 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002519 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002520 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2521 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002522 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002523 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2524 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002525 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002526 }
2527
Wale Ogunwale494009b82016-10-21 09:01:38 -07002528 boolean checkWaitingForWindows() {
2529
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002530 mHaveBootMsg = false;
2531 mHaveApp = false;
2532 mHaveWallpaper = false;
2533 mHaveKeyguard = true;
2534
2535 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002536 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2537 return true;
2538 }
2539 if (w.isDrawnLw()) {
2540 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002542 } else if (w.mAttrs.type == TYPE_APPLICATION
2543 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002544 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002545 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002546 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002547 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002548 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002549 }
2550 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002551 return false;
2552 });
2553
2554 if (visibleWindow != null) {
2555 // We have a visible window.
2556 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002557 }
2558
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002559 // if the wallpaper service is disabled on the device, we're never going to have
2560 // wallpaper, don't bother waiting for it
2561 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2562 com.android.internal.R.bool.config_enableWallpaperService)
2563 && !mService.mOnlyCore;
2564
Wale Ogunwale494009b82016-10-21 09:01:38 -07002565 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2566 "******** booted=" + mService.mSystemBooted
2567 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002568 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2569 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2570 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002571
2572 // If we are turning on the screen to show the boot message, don't do it until the boot
2573 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002574 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002575 return true;
2576 }
2577
2578 // If we are turning on the screen after the boot is completed normally, don't do so until
2579 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002580 if (mService.mSystemBooted
2581 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002582 return true;
2583 }
2584
2585 return false;
2586 }
2587
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002588 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002589 mTmpWindowAnimator = animator;
2590 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002591 }
2592
2593 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002594 resetAnimationBackgroundAnimator();
2595
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002596 // Used to indicate a detached wallpaper.
2597 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002598 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002599
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002600 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002601
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002602 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002603 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002604 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2605 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002606 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2607 }
2608 }
2609
2610 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002611 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002612 }
2613
Wale Ogunwale494009b82016-10-21 09:01:38 -07002614 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002615 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002616 if (imFocus == null) {
2617 return false;
2618 }
2619
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002620 if (DEBUG_INPUT_METHOD) {
2621 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2622 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2623 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002624 }
2625
Wale Ogunwale494009b82016-10-21 09:01:38 -07002626 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2627
2628 if (DEBUG_INPUT_METHOD) {
2629 Slog.i(TAG_WM, "IM target client: " + imeClient);
2630 if (imeClient != null) {
2631 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2632 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2633 }
2634 }
2635
2636 return imeClient != null && imeClient.asBinder() == client.asBinder();
2637 }
2638
2639 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002640 final WindowState win = getWindow(
2641 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2642 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002643 }
2644
2645 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002646 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002647 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002648 final int curValue = w.mSystemUiVisibility;
2649 final int diff = (curValue ^ visibility) & globalDiff;
2650 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002651 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002652 w.mSeq++;
2653 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002654 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002655 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2656 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002657 visibility, newValue, diff);
2658 }
2659 } catch (RemoteException e) {
2660 // so sorry
2661 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002662 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002663 }
2664
2665 void onWindowFreezeTimeout() {
2666 Slog.w(TAG_WM, "Window freeze timeout expired.");
2667 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002668
2669 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002670 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002671 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002672 }
2673 w.mOrientationChanging = false;
2674 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2675 - mService.mDisplayFreezeTime);
2676 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002677 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002678 mService.mWindowPlacerLocked.performSurfacePlacement();
2679 }
2680
2681 void waitForAllWindowsDrawn() {
2682 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002683 forAllWindows(w -> {
2684 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2685 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2686 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002687 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002688 w.mLastContentInsets.set(-1, -1, -1, -1);
2689 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002690 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002691 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002692 }
2693
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002694 // TODO: Super crazy long method that should be broken down...
2695 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2696
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002697 final int dw = mDisplayInfo.logicalWidth;
2698 final int dh = mDisplayInfo.logicalHeight;
2699 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2700
2701 mTmpUpdateAllDrawn.clear();
2702
2703 int repeats = 0;
2704 do {
2705 repeats++;
2706 if (repeats > 6) {
2707 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2708 clearLayoutNeeded();
2709 break;
2710 }
2711
2712 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2713 pendingLayoutChanges);
2714
Andrii Kulian839def92016-11-02 10:58:58 -07002715 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2716 // the wallpaper window jumping across displays.
2717 // Remove check for default display when there will be support for multiple wallpaper
2718 // targets (on different displays).
2719 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002720 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002721 }
2722
2723 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2724 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2725 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2726 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002727 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002728 }
2729 }
2730
2731 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2732 setLayoutNeeded();
2733 }
2734
2735 // FIRST LOOP: Perform a layout, if needed.
2736 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2737 performLayout(repeats == 1, false /* updateInputWindows */);
2738 } else {
2739 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2740 }
2741
2742 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2743 pendingLayoutChanges = 0;
2744
2745 if (isDefaultDisplay) {
2746 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002747 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002748 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2749 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2750 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2751 }
2752 } while (pendingLayoutChanges != 0);
2753
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002754 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002755 resetDimming();
2756
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002757 mTmpRecoveringMemory = recoveringMemory;
2758 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002759
2760 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002761 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2762 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2763 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002764 true /* inTraversal, must call performTraversalInTrans... below */);
2765
2766 stopDimmingIfNeeded();
2767
2768 while (!mTmpUpdateAllDrawn.isEmpty()) {
2769 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2770 // See if any windows have been drawn, so they (and others associated with them)
2771 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002772 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002773 }
2774
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002775 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002776 }
2777
2778 void performLayout(boolean initial, boolean updateInputWindows) {
2779 if (!isLayoutNeeded()) {
2780 return;
2781 }
2782 clearLayoutNeeded();
2783
2784 final int dw = mDisplayInfo.logicalWidth;
2785 final int dh = mDisplayInfo.logicalHeight;
2786
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002787 if (DEBUG_LAYOUT) {
2788 Slog.v(TAG, "-------------------------------------");
2789 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2790 }
2791
Andrii Kulian8ee72852017-03-10 10:36:45 -08002792 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002793 getConfiguration().uiMode);
2794 if (isDefaultDisplay) {
2795 // Not needed on non-default displays.
2796 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2797 mService.mScreenRect.set(0, 0, dw, dh);
2798 }
2799
2800 mService.mPolicy.getContentRectLw(mContentRect);
2801
2802 int seq = mService.mLayoutSeq + 1;
2803 if (seq < 0) seq = 0;
2804 mService.mLayoutSeq = seq;
2805
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002806 // Used to indicate that we have processed the dream window and all additional windows are
2807 // behind it.
2808 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002809 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002810
2811 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002812 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002813
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002814 // Used to indicate that we have processed the dream window and all additional attached
2815 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002816 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002817 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002818
2819 // Now perform layout of attached windows, which usually depend on the position of the
2820 // window they are attached to. XXX does not deal with windows that are attached to windows
2821 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002822 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002823
2824 // Window frames may have changed. Tell the input dispatcher about it.
2825 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2826 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2827 if (updateInputWindows) {
2828 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2829 }
2830
2831 mService.mPolicy.finishLayoutLw();
2832 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2833 }
2834
2835 /**
2836 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2837 * In portrait mode, it grabs the full screenshot.
2838 *
2839 * @param width the width of the target bitmap
2840 * @param height the height of the target bitmap
2841 * @param includeFullDisplay true if the screen should not be cropped before capture
2842 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2843 * @param config of the output bitmap
2844 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002845 * @param includeDecor whether to include window decors, like the status or navigation bar
2846 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002847 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002848 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002849 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002850 boolean wallpaperOnly, boolean includeDecor) {
2851 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2852 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002853 if (bitmap == null) {
2854 return null;
2855 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002856
2857 if (DEBUG_SCREENSHOT) {
2858 // TEST IF IT's ALL BLACK
2859 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2860 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2861 bitmap.getHeight());
2862 boolean allBlack = true;
2863 final int firstColor = buffer[0];
2864 for (int i = 0; i < buffer.length; i++) {
2865 if (buffer[i] != firstColor) {
2866 allBlack = false;
2867 break;
2868 }
2869 }
2870 if (allBlack) {
2871 final WindowState appWin = mScreenshotApplicationState.appWin;
2872 final int maxLayer = mScreenshotApplicationState.maxLayer;
2873 final int minLayer = mScreenshotApplicationState.minLayer;
2874 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2875 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2876 (appWin != null ?
2877 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2878 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2879 }
2880 }
2881
2882 // Create a copy of the screenshot that is immutable and backed in ashmem.
2883 // This greatly reduces the overhead of passing the bitmap between processes.
2884 Bitmap ret = bitmap.createAshmemBitmap(config);
2885 bitmap.recycle();
2886 return ret;
2887 }
2888
2889 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2890 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2891 boolean includeDecor) {
2892 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2893 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2894 }
2895
2896 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2897 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2898 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002899 int dw = mDisplayInfo.logicalWidth;
2900 int dh = mDisplayInfo.logicalHeight;
2901 if (dw == 0 || dh == 0) {
2902 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2903 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2904 return null;
2905 }
2906
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002907 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002908
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002909 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002910 final Rect frame = new Rect();
2911 final Rect stackBounds = new Rect();
2912
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002913 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002914 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002915 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002916 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002917 if (!mService.mPolicy.isScreenOn()) {
2918 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
2919 + " was off.");
2920 return null;
2921 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002922 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002923 mScreenshotApplicationState.appWin = null;
2924 forAllWindows(w -> {
2925 if (!w.mHasSurface) {
2926 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002927 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002928 if (w.mLayer >= aboveAppLayer) {
2929 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002930 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002931 if (wallpaperOnly && !w.mIsWallpaper) {
2932 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002933 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002934 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02002935 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002936 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002937 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2938 // then the target window state is this one.
2939 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002940 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002941 }
2942
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002943 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002944 // We have not ran across the target window yet, so it is probably behind
2945 // the wallpaper. This can happen when the keyguard is up and all windows
2946 // are moved behind the wallpaper. We don't want to include the wallpaper
2947 // layer in the screenshot as it will cover-up the layer of the target
2948 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002949 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002950 }
2951 // Fall through. The target window is in front of the wallpaper. For this
2952 // case we want to include the wallpaper layer in the screenshot because
2953 // the target window might have some transparent areas.
2954 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002955 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002956 // This app window is of no interest if it is not associated with the
2957 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002958 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002959 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002960 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002961 }
2962
2963 // Include this window.
2964
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002965 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002967 if (mScreenshotApplicationState.maxLayer < layer) {
2968 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002969 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002970 if (mScreenshotApplicationState.minLayer > layer) {
2971 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972 }
2973
2974 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02002975 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
2976 if (includeDecor) {
2977 final TaskStack stack = w.getStack();
2978 if (stack != null) {
2979 stack.getBounds(frame);
2980 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02002981
Jorim Jaggi70f59482017-05-04 14:05:59 +02002982 // We want to screenshot with the exact bounds of the surface of the app. Thus,
2983 // intersect it with the frame.
2984 frame.intersect(w.mFrame);
2985 } else {
2986 final Rect wf = w.mFrame;
2987 final Rect cr = w.mContentInsets;
2988 int left = wf.left + cr.left;
2989 int top = wf.top + cr.top;
2990 int right = wf.right - cr.right;
2991 int bottom = wf.bottom - cr.bottom;
2992 frame.union(left, top, right, bottom);
2993 w.getVisibleBounds(stackBounds);
2994 if (!Rect.intersects(frame, stackBounds)) {
2995 // Set frame empty if there's no intersection.
2996 frame.setEmpty();
2997 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002998 }
2999 }
3000
3001 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003002 (w.mAppToken != null && w.mAppToken.token == appToken)
3003 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01003004 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003005 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003006 }
3007
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003008 if (w.isObscuringDisplay()){
3009 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003010 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003011 return false;
3012 }, true /* traverseTopToBottom */);
3013
3014 final WindowState appWin = mScreenshotApplicationState.appWin;
3015 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3016 final int maxLayer = mScreenshotApplicationState.maxLayer;
3017 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003018
3019 if (appToken != null && appWin == null) {
3020 // Can't find a window to snapshot.
3021 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3022 "Screenshot: Couldn't find a surface matching " + appToken);
3023 return null;
3024 }
3025
3026 if (!screenshotReady) {
3027 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3028 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3029 appWin.mWinAnimator.mDrawState)));
3030 return null;
3031 }
3032
3033 // Screenshot is ready to be taken. Everything from here below will continue
3034 // through the bottom of the loop and return a value. We only stay in the loop
3035 // because we don't want to release the mWindowMap lock until the screenshot is
3036 // taken.
3037
3038 if (maxLayer == 0) {
3039 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3040 + ": returning null maxLayer=" + maxLayer);
3041 return null;
3042 }
3043
Jorim Jaggi02886a82016-12-06 09:10:06 -08003044 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003045 // Constrain frame to the screen size.
3046 if (!frame.intersect(0, 0, dw, dh)) {
3047 frame.setEmpty();
3048 }
3049 } else {
3050 // Caller just wants entire display.
3051 frame.set(0, 0, dw, dh);
3052 }
3053 if (frame.isEmpty()) {
3054 return null;
3055 }
3056
3057 if (width < 0) {
3058 width = (int) (frame.width() * frameScale);
3059 }
3060 if (height < 0) {
3061 height = (int) (frame.height() * frameScale);
3062 }
3063
3064 // Tell surface flinger what part of the image to crop. Take the top
3065 // right part of the application, and crop the larger dimension to fit.
3066 Rect crop = new Rect(frame);
3067 if (width / (float) frame.width() < height / (float) frame.height()) {
3068 int cropWidth = (int)((float)width / (float)height * frame.height());
3069 crop.right = crop.left + cropWidth;
3070 } else {
3071 int cropHeight = (int)((float)height / (float)width * frame.width());
3072 crop.bottom = crop.top + cropHeight;
3073 }
3074
3075 // The screenshot API does not apply the current screen rotation.
3076 int rot = mDisplay.getRotation();
3077
3078 if (rot == ROTATION_90 || rot == ROTATION_270) {
3079 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3080 }
3081
3082 // Surfaceflinger is not aware of orientation, so convert our logical
3083 // crop to surfaceflinger's portrait orientation.
3084 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3085
3086 if (DEBUG_SCREENSHOT) {
3087 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3088 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003089 forAllWindows(w -> {
3090 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3091 Slog.i(TAG_WM, w + ": " + w.mLayer
3092 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003093 + " surfaceLayer=" + ((controller == null)
3094 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003095 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003096 }
3097
3098 final ScreenRotationAnimation screenRotationAnimation =
3099 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3100 final boolean inRotation = screenRotationAnimation != null &&
3101 screenRotationAnimation.isAnimating();
3102 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3103 "Taking screenshot while rotating");
3104
3105 // We force pending transactions to flush before taking
3106 // the screenshot by pushing an empty synchronous transaction.
3107 SurfaceControl.openTransaction();
3108 SurfaceControl.closeTransactionSync();
3109
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003110 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003111 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003112 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003113 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3114 + ") to layer " + maxLayer);
3115 return null;
3116 }
3117 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003118 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003119 }
3120
3121 // TODO: Can this use createRotationMatrix()?
3122 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3123 if (rot == Surface.ROTATION_90) {
3124 final int tmp = crop.top;
3125 crop.top = dw - crop.right;
3126 crop.right = crop.bottom;
3127 crop.bottom = dw - crop.left;
3128 crop.left = tmp;
3129 } else if (rot == Surface.ROTATION_180) {
3130 int tmp = crop.top;
3131 crop.top = dh - crop.bottom;
3132 crop.bottom = dh - tmp;
3133 tmp = crop.right;
3134 crop.right = dw - crop.left;
3135 crop.left = dw - tmp;
3136 } else if (rot == Surface.ROTATION_270) {
3137 final int tmp = crop.top;
3138 crop.top = crop.left;
3139 crop.left = dh - crop.bottom;
3140 crop.bottom = crop.right;
3141 crop.right = dh - tmp;
3142 }
3143 }
3144
3145 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003146 // Used to indicate the layout is needed.
3147 mTmpWindow = null;
3148
3149 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003150 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003151 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003152 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003153 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003154 w.setDisplayLayoutNeeded();
3155 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003156 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003157
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003158 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003159 mService.mWindowPlacerLocked.performSurfacePlacement();
3160 }
3161 }
3162
Wale Ogunwale1666e312016-12-16 11:27:18 -08003163 void setExitingTokensHasVisible(boolean hasVisible) {
3164 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3165 mExitingTokens.get(i).hasVisible = hasVisible;
3166 }
3167
3168 // Initialize state of exiting applications.
3169 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3170 }
3171
3172 void removeExistingTokensIfPossible() {
3173 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3174 final WindowToken token = mExitingTokens.get(i);
3175 if (!token.hasVisible) {
3176 mExitingTokens.remove(i);
3177 }
3178 }
3179
3180 // Time to remove any exiting applications?
3181 mTaskStackContainers.removeExistingAppTokensIfPossible();
3182 }
3183
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003184 @Override
3185 void onDescendantOverrideConfigurationChanged() {
3186 setLayoutNeeded();
3187 mService.requestTraversal();
3188 }
3189
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003190 static final class TaskForResizePointSearchResult {
3191 boolean searchDone;
3192 Task taskForResize;
3193
3194 void reset() {
3195 searchDone = false;
3196 taskForResize = null;
3197 }
3198 }
Robert Carr3b716242016-08-16 16:02:21 -07003199
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003200 private static final class ApplySurfaceChangesTransactionState {
3201 boolean displayHasContent;
3202 boolean obscured;
3203 boolean syswin;
3204 boolean focusDisplayed;
3205 float preferredRefreshRate;
3206 int preferredModeId;
3207
3208 void reset() {
3209 displayHasContent = false;
3210 obscured = false;
3211 syswin = false;
3212 focusDisplayed = false;
3213 preferredRefreshRate = 0;
3214 preferredModeId = 0;
3215 }
3216 }
3217
3218 private static final class ScreenshotApplicationState {
3219 WindowState appWin;
3220 int maxLayer;
3221 int minLayer;
3222 boolean screenshotReady;
3223
3224 void reset(boolean screenshotReady) {
3225 appWin = null;
3226 maxLayer = 0;
3227 minLayer = 0;
3228 this.screenshotReady = screenshotReady;
3229 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3230 }
3231 }
3232
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003233 /**
3234 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3235 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3236 * homogeneous children type which is currently required by sub-classes of
3237 * {@link WindowContainer} class.
3238 */
3239 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3240
3241 int size() {
3242 return mChildren.size();
3243 }
3244
3245 E get(int index) {
3246 return mChildren.get(index);
3247 }
3248
3249 @Override
3250 boolean fillsParent() {
3251 return true;
3252 }
3253
3254 @Override
3255 boolean isVisible() {
3256 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003257 }
3258 }
3259
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003260 /**
3261 * Window container class that contains all containers on this display relating to Apps.
3262 * I.e Activities.
3263 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003264 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003265
Andrii Kulian839def92016-11-02 10:58:58 -07003266 /**
3267 * Adds the stack to this container.
3268 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3269 */
3270 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003271 if (stack.mStackId == HOME_STACK_ID) {
3272 if (mHomeStack != null) {
3273 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3274 }
3275 mHomeStack = stack;
3276 }
3277 addChild(stack, onTop);
3278 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003279 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003280
Andrii Kulian839def92016-11-02 10:58:58 -07003281 /** Removes the stack from its container and prepare for changing the parent. */
3282 void removeStackFromDisplay(TaskStack stack) {
3283 removeChild(stack);
3284 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003285 }
3286
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003287 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003288 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3289 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003290 addChild(stack, addIndex);
3291 setLayoutNeeded();
3292 }
3293
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003294 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003295 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3296 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3297 // This stack is always-on-top, override the default behavior.
3298 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3299
3300 // Moving to its current position, as we must call super but we don't want to
3301 // perform any meaningful action.
3302 final int currentPosition = mChildren.indexOf(child);
3303 super.positionChildAt(currentPosition, child, false /* includingParents */);
3304 return;
3305 }
3306
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003307 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3308 super.positionChildAt(targetPosition, child, includingParents);
3309
3310 setLayoutNeeded();
3311 }
3312
3313 /**
3314 * When stack is added or repositioned, find a proper position for it.
3315 * This will make sure that pinned stack always stays on top.
3316 * @param requestedPosition Position requested by caller.
3317 * @param stack Stack to be added or positioned.
3318 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3319 * @return The proper position for the stack.
3320 */
3321 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3322 final int topChildPosition = mChildren.size() - 1;
3323 boolean toTop = requestedPosition == POSITION_TOP;
3324 toTop |= adding ? requestedPosition >= topChildPosition + 1
3325 : requestedPosition >= topChildPosition;
3326 int targetPosition = requestedPosition;
3327
Bryce Lee48f4b572017-04-10 10:54:15 -07003328 if (toTop && stack.mStackId != PINNED_STACK_ID
3329 && getStackById(PINNED_STACK_ID) != null) {
3330 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003331 TaskStack topStack = mChildren.get(topChildPosition);
3332 if (topStack.mStackId != PINNED_STACK_ID) {
3333 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3334 }
3335
3336 // So, stack is moved just below the pinned stack.
3337 // When we're adding a new stack the target is the current pinned stack position.
3338 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003339 // stack, because WindowContainer#positionAt() first removes element and then adds
3340 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003341 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3342 }
3343
3344 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003345 }
3346
3347 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003348 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3349 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003350 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003351 if (super.forAllWindows(callback, traverseTopToBottom)) {
3352 return true;
3353 }
3354 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3355 return true;
3356 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003357 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003358 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3359 return true;
3360 }
3361 if (super.forAllWindows(callback, traverseTopToBottom)) {
3362 return true;
3363 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003364 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003365 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003366 }
3367
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003368 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003369 boolean traverseTopToBottom) {
3370 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3371 // app tokens.
3372 // TODO: Investigate if we need to continue to do this or if we can just process them
3373 // in-order.
3374 if (traverseTopToBottom) {
3375 for (int i = mChildren.size() - 1; i >= 0; --i) {
3376 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3377 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003378 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3379 traverseTopToBottom)) {
3380 return true;
3381 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003382 }
3383 }
3384 } else {
3385 final int count = mChildren.size();
3386 for (int i = 0; i < count; ++i) {
3387 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3388 final int appTokensCount = appTokens.size();
3389 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003390 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3391 traverseTopToBottom)) {
3392 return true;
3393 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003394 }
3395 }
3396 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003397 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003398 }
3399
Wale Ogunwale1666e312016-12-16 11:27:18 -08003400 void setExitingTokensHasVisible(boolean hasVisible) {
3401 for (int i = mChildren.size() - 1; i >= 0; --i) {
3402 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3403 for (int j = appTokens.size() - 1; j >= 0; --j) {
3404 appTokens.get(j).hasVisible = hasVisible;
3405 }
3406 }
3407 }
3408
3409 void removeExistingAppTokensIfPossible() {
3410 for (int i = mChildren.size() - 1; i >= 0; --i) {
3411 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3412 for (int j = appTokens.size() - 1; j >= 0; --j) {
3413 final AppWindowToken token = appTokens.get(j);
3414 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3415 && (!token.mIsExiting || token.isEmpty())) {
3416 // Make sure there is no animation running on this token, so any windows
3417 // associated with it will be removed as soon as their animations are
3418 // complete.
3419 token.mAppAnimator.clearAnimation();
3420 token.mAppAnimator.animating = false;
3421 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3422 "performLayout: App token exiting now removed" + token);
3423 token.removeIfPossible();
3424 }
3425 }
3426 }
3427 }
3428
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003429 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003430 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003431 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003432 // Apps and their containers are not allowed to specify an orientation while the
3433 // docked or freeform stack is visible...except for the home stack/task if the
3434 // docked stack is minimized and it actually set something.
3435 if (mHomeStack != null && mHomeStack.isVisible()
3436 && mDividerControllerLocked.isMinimizedDock()) {
3437 final int orientation = mHomeStack.getOrientation();
3438 if (orientation != SCREEN_ORIENTATION_UNSET) {
3439 return orientation;
3440 }
3441 }
3442 return SCREEN_ORIENTATION_UNSPECIFIED;
3443 }
3444
3445 final int orientation = super.getOrientation();
3446 if (orientation != SCREEN_ORIENTATION_UNSET
3447 && orientation != SCREEN_ORIENTATION_BEHIND) {
3448 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3449 "App is requesting an orientation, return " + orientation);
3450 return orientation;
3451 }
3452
3453 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003454 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003455 // The next app has not been requested to be visible, so we keep the current orientation
3456 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003457 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003458 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003459 }
3460
3461 /**
3462 * Window container class that contains all containers on this display that are not related to
3463 * Apps. E.g. status bar.
3464 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003465 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3466 /**
3467 * Compares two child window tokens returns -1 if the first is lesser than the second in
3468 * terms of z-order and 1 otherwise.
3469 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003470 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003471 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003472 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3473 token1.mOwnerCanManageAppTokens)
3474 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3475 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003476
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003477 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3478 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3479 return false;
3480 }
3481 final int req = w.mAttrs.screenOrientation;
3482 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3483 || req == SCREEN_ORIENTATION_UNSET) {
3484 return false;
3485 }
3486 return true;
3487 };
3488
Wale Ogunwale3a931692016-11-02 16:49:48 -07003489 private final String mName;
3490 NonAppWindowContainers(String name) {
3491 mName = name;
3492 }
3493
3494 void addChild(WindowToken token) {
3495 addChild(token, mWindowComparator);
3496 }
3497
3498 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003499 int getOrientation() {
3500 final WindowManagerPolicy policy = mService.mPolicy;
3501 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003502 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003503
3504 if (win != null) {
3505 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003506 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003507 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003508 if (mService.mKeyguardGoingAway) {
3509 // Keyguard can't affect the orientation if it is going away...
3510 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3511 return SCREEN_ORIENTATION_UNSET;
3512 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003513 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003514 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003515 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003516 }
3517
Andrii Kulian8ee72852017-03-10 10:36:45 -08003518 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003519
3520 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003521 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003522 }
3523
3524 return SCREEN_ORIENTATION_UNSET;
3525 }
3526
3527 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003528 String getName() {
3529 return mName;
3530 }
Robert Carr3b716242016-08-16 16:02:21 -07003531 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003532
3533 /**
3534 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3535 */
3536 @FunctionalInterface
3537 private interface Screenshoter<E> {
3538 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3539 boolean useIdentityTransform, int rotation);
3540 }
Craig Mautner59c00972012-07-30 12:10:24 -07003541}