blob: 05f4626259d786227f8b2fab028ca11022a54598 [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();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700213
Andrii Kulian06d07d62017-03-14 11:11:47 -0700214 /**
215 * Compat metrics computed based on {@link #mDisplayMetrics}.
216 * @see #updateDisplayAndOrientation(int)
217 */
218 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
219
220 /** The desired scaling factor for compatible apps. */
221 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700222
Andrii Kulian8ee72852017-03-10 10:36:45 -0800223 /**
224 * Current rotation of the display.
225 * Constants as per {@link android.view.Surface.Rotation}.
226 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700227 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800228 */
229 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700230
Andrii Kulian8ee72852017-03-10 10:36:45 -0800231 /**
232 * Last applied orientation of the display.
233 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
234 *
235 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
236 */
237 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700238
Andrii Kulian8ee72852017-03-10 10:36:45 -0800239 /**
240 * Flag indicating that the application is receiving an orientation that has different metrics
241 * than it expected. E.g. Portrait instead of Landscape.
242 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700243 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800244 */
245 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700246
Andrii Kulian8ee72852017-03-10 10:36:45 -0800247 /**
248 * Orientation forced by some window. If there is no visible window that specifies orientation
249 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
250 *
251 * @see NonAppWindowContainers#getOrientation()
252 */
253 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700254
Andrii Kulian8ee72852017-03-10 10:36:45 -0800255 /**
256 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
257 * occluded.
258 *
259 * @see NonAppWindowContainers#getOrientation()
260 */
261 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
262
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700263 /**
264 * Keep track of wallpaper visibility to notify changes.
265 */
266 private boolean mLastWallpaperVisible = false;
267
Andrii Kulian06d07d62017-03-14 11:11:47 -0700268 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700269 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700270
Craig Mautner39834192012-09-02 07:47:24 -0700271 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700272 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700273 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700274 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800275 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700276
Craig Mautnerdc548482014-02-05 13:35:24 -0800277 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700278 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800279
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800280 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
281 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700282 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700283
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700284 /** Detect user tapping outside of current focused task bounds .*/
285 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700286
Craig Mautner6601b7b2013-04-29 10:29:11 -0700287 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700288 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700289
Craig Mautner6601b7b2013-04-29 10:29:11 -0700290 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800291 private final Rect mTmpRect = new Rect();
292 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700293 private final RectF mTmpRectF = new RectF();
294 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800295 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700296
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800297 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700298
Craig Mautner95da1082014-02-24 17:54:35 -0800299 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700300 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800301
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700302 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700303 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700304
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800305 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700306
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800307 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
308
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000309 private boolean mHaveBootMsg = false;
310 private boolean mHaveApp = false;
311 private boolean mHaveWallpaper = false;
312 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700313
314 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
315
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700316 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
317 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000318 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
319 new ApplySurfaceChangesTransactionState();
320 private final ScreenshotApplicationState mScreenshotApplicationState =
321 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700322
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700323 // True if this display is in the process of being removed. Used to determine if the removal of
324 // the display's direct children should be allowed.
325 private boolean mRemovingDisplay = false;
326
Bryce Leed1871262017-06-12 14:12:29 -0700327 // {@code false} if this display is in the processing of being created.
328 private boolean mDisplayReady = false;
329
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700330 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800331 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700332 int mInputMethodAnimLayerAdjustment;
333
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800334 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
335 WindowStateAnimator winAnimator = w.mWinAnimator;
336 if (winAnimator.hasSurface()) {
337 final boolean wasAnimating = winAnimator.mWasAnimating;
338 final boolean nowAnimating = winAnimator.stepAnimationLocked(
339 mTmpWindowAnimator.mCurrentTime);
340 winAnimator.mWasAnimating = nowAnimating;
341 mTmpWindowAnimator.orAnimating(nowAnimating);
342
343 if (DEBUG_WALLPAPER) Slog.v(TAG,
344 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
345
346 if (wasAnimating && !winAnimator.mAnimating
347 && mWallpaperController.isWallpaperTarget(w)) {
348 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
349 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
350 if (DEBUG_LAYOUT_REPEATS) {
351 mService.mWindowPlacerLocked.debugLayoutRepeats(
352 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
353 }
354 }
355 }
356
357 final AppWindowToken atoken = w.mAppToken;
358 if (winAnimator.mDrawState == READY_TO_SHOW) {
359 if (atoken == null || atoken.allDrawn) {
360 if (w.performShowLocked()) {
361 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
362 if (DEBUG_LAYOUT_REPEATS) {
363 mService.mWindowPlacerLocked.debugLayoutRepeats(
364 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
365 }
366 }
367 }
368 }
369 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
370 if (appAnimator != null && appAnimator.thumbnail != null) {
371 if (appAnimator.thumbnailTransactionSeq
372 != mTmpWindowAnimator.mAnimTransactionSequence) {
373 appAnimator.thumbnailTransactionSeq =
374 mTmpWindowAnimator.mAnimTransactionSequence;
375 appAnimator.thumbnailLayer = 0;
376 }
377 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
378 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
379 }
380 }
381 };
382
383 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
384 final WindowStateAnimator winAnimator = w.mWinAnimator;
385 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
386 return;
387 }
388
389 final int flags = w.mAttrs.flags;
390
391 // If this window is animating, make a note that we have an animating window and take
392 // care of a request to run a detached wallpaper animation.
393 if (winAnimator.mAnimating) {
394 if (winAnimator.mAnimation != null) {
395 if ((flags & FLAG_SHOW_WALLPAPER) != 0
396 && winAnimator.mAnimation.getDetachWallpaper()) {
397 mTmpWindow = w;
398 }
399 final int color = winAnimator.mAnimation.getBackgroundColor();
400 if (color != 0) {
401 final TaskStack stack = w.getStack();
402 if (stack != null) {
403 stack.setAnimationBackground(winAnimator, color);
404 }
405 }
406 }
407 mTmpWindowAnimator.setAnimating(true);
408 }
409
410 // If this window's app token is running a detached wallpaper animation, make a note so
411 // we can ensure the wallpaper is displayed behind it.
412 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
413 if (appAnimator != null && appAnimator.animation != null
414 && appAnimator.animating) {
415 if ((flags & FLAG_SHOW_WALLPAPER) != 0
416 && appAnimator.animation.getDetachWallpaper()) {
417 mTmpWindow = w;
418 }
419
420 final int color = appAnimator.animation.getBackgroundColor();
421 if (color != 0) {
422 final TaskStack stack = w.getStack();
423 if (stack != null) {
424 stack.setAnimationBackground(winAnimator, color);
425 }
426 }
427 }
428 };
429
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800430 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
431 final int lostFocusUid = mTmpWindow.mOwnerUid;
432 final Handler handler = mService.mH;
433 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
434 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
435 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
436 w.mAttrs.hideTimeoutMilliseconds);
437 }
438 }
439 };
440
441 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
442 final AppWindowToken focusedApp = mService.mFocusedApp;
443 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
444 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
445
446 if (!w.canReceiveKeys()) {
447 return false;
448 }
449
450 final AppWindowToken wtoken = w.mAppToken;
451
452 // If this window's application has been removed, just skip it.
453 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
454 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
455 + (wtoken.removed ? "removed" : "sendingToBottom"));
456 return false;
457 }
458
459 if (focusedApp == null) {
460 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
461 + " using new focus @ " + w);
462 mTmpWindow = w;
463 return true;
464 }
465
466 if (!focusedApp.windowsAreFocusable()) {
467 // Current focused app windows aren't focusable...
468 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
469 + " focusable using new focus @ " + w);
470 mTmpWindow = w;
471 return true;
472 }
473
474 // Descend through all of the app tokens and find the first that either matches
475 // win.mAppToken (return win) or mFocusedApp (return null).
476 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
477 if (focusedApp.compareTo(wtoken) > 0) {
478 // App stack below focused app stack. No focus for you!!!
479 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
480 "findFocusedWindow: Reached focused app=" + focusedApp);
481 mTmpWindow = null;
482 return true;
483 }
484 }
485
486 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
487 mTmpWindow = w;
488 return true;
489 };
490
491 private final Consumer<WindowState> mPrepareWindowSurfaces =
492 w -> w.mWinAnimator.prepareSurfaceLocked(true);
493
494 private final Consumer<WindowState> mPerformLayout = w -> {
495 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
496 // wasting time and funky changes while a window is animating away.
497 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
498 || w.isGoneForLayoutLw();
499
500 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
501 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
502 + " mLayoutAttached=" + w.mLayoutAttached
503 + " screen changed=" + w.isConfigChanged());
504 final AppWindowToken atoken = w.mAppToken;
505 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
506 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
507 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
508 + " parentHidden=" + w.isParentWindowHidden());
509 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
510 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
511 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
512 + " parentHidden=" + w.isParentWindowHidden());
513 }
514
515 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
516 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
517 // since that means "perform layout as normal, just don't display").
518 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
519 || ((w.isConfigChanged() || w.setReportResizeHints())
520 && !w.isGoneForLayoutLw() &&
521 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
522 (w.mHasSurface && w.mAppToken != null &&
523 w.mAppToken.layoutConfigChanges)))) {
524 if (!w.mLayoutAttached) {
525 if (mTmpInitial) {
526 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
527 w.mContentChanged = false;
528 }
529 if (w.mAttrs.type == TYPE_DREAM) {
530 // Don't layout windows behind a dream, so that if it does stuff like hide
531 // the status bar we won't get a bad transition when it goes away.
532 mTmpWindow = w;
533 }
534 w.mLayoutNeeded = false;
535 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200536 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800537 mService.mPolicy.layoutWindowLw(w, null);
538 w.mLayoutSeq = mService.mLayoutSeq;
539
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200540 // If this is the first layout, we need to initialize the last inset values as
541 // otherwise we'd immediately cause an unnecessary resize.
542 if (firstLayout) {
543 w.updateLastInsetValues();
544 }
545
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800546 // Window frames may have changed. Update dim layer with the new bounds.
547 final Task task = w.getTask();
548 if (task != null) {
549 mDimLayerController.updateDimLayer(task);
550 }
551
552 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
553 + " mContainingFrame=" + w.mContainingFrame
554 + " mDisplayFrame=" + w.mDisplayFrame);
555 }
556 }
557 };
558
559 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
560 if (w.mLayoutAttached) {
561 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
562 + " mViewVisibility=" + w.mViewVisibility
563 + " mRelayoutCalled=" + w.mRelayoutCalled);
564 // If this view is GONE, then skip it -- keep the current frame, and let the caller
565 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
566 // windows, since that means "perform layout as normal, just don't display").
567 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
568 return;
569 }
570 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
571 || w.mLayoutNeeded) {
572 if (mTmpInitial) {
573 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
574 w.mContentChanged = false;
575 }
576 w.mLayoutNeeded = false;
577 w.prelayout();
578 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
579 w.mLayoutSeq = mService.mLayoutSeq;
580 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
581 + " mContainingFrame=" + w.mContainingFrame
582 + " mDisplayFrame=" + w.mDisplayFrame);
583 }
584 } else if (w.mAttrs.type == TYPE_DREAM) {
585 // Don't layout windows behind a dream, so that if it does stuff like hide the
586 // status bar we won't get a bad transition when it goes away.
587 mTmpWindow = mTmpWindow2;
588 }
589 };
590
591 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
592 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
593 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
594 return w.canBeImeTarget();
595 };
596
597 private final Consumer<WindowState> mApplyPostLayoutPolicy =
598 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
599 mService.mInputMethodTarget);
600
601 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
602 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
603 final boolean obscuredChanged = w.mObscured !=
604 mTmpApplySurfaceChangesTransactionState.obscured;
605 final RootWindowContainer root = mService.mRoot;
606 // Only used if default window
607 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
608
609 // Update effect.
610 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
611 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
612 final boolean isDisplayed = w.isDisplayedLw();
613
614 if (isDisplayed && w.isObscuringDisplay()) {
615 // This window completely covers everything behind it, so we want to leave all
616 // of them as undimmed (for performance reasons).
617 root.mObscuringWindow = w;
618 mTmpApplySurfaceChangesTransactionState.obscured = true;
619 }
620
621 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
622 root.handleNotObscuredLocked(w,
623 mTmpApplySurfaceChangesTransactionState.obscured,
624 mTmpApplySurfaceChangesTransactionState.syswin);
625
626 if (w.mHasSurface && isDisplayed) {
627 final int type = w.mAttrs.type;
628 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
629 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
630 mTmpApplySurfaceChangesTransactionState.syswin = true;
631 }
632 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
633 && w.mAttrs.preferredRefreshRate != 0) {
634 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
635 = w.mAttrs.preferredRefreshRate;
636 }
637 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
638 && w.mAttrs.preferredDisplayModeId != 0) {
639 mTmpApplySurfaceChangesTransactionState.preferredModeId
640 = w.mAttrs.preferredDisplayModeId;
641 }
642 }
643 }
644
645 w.applyDimLayerIfNeeded();
646
647 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
648 && mWallpaperController.isWallpaperTarget(w)) {
649 // This is the wallpaper target and its obscured state changed... make sure the
650 // current wallpaper's visibility has been updated accordingly.
651 mWallpaperController.updateWallpaperVisibility();
652 }
653
654 w.handleWindowMovedIfNeeded();
655
656 final WindowStateAnimator winAnimator = w.mWinAnimator;
657
658 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
659 w.mContentChanged = false;
660
661 // Moved from updateWindowsAndWallpaperLocked().
662 if (w.mHasSurface) {
663 // Take care of the window being ready to display.
664 final boolean committed = winAnimator.commitFinishDrawingLocked();
665 if (isDefaultDisplay && committed) {
666 if (w.mAttrs.type == TYPE_DREAM) {
667 // HACK: When a dream is shown, it may at that point hide the lock screen.
668 // So we need to redo the layout to let the phone window manager make this
669 // happen.
670 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
671 if (DEBUG_LAYOUT_REPEATS) {
672 surfacePlacer.debugLayoutRepeats(
673 "dream and commitFinishDrawingLocked true",
674 pendingLayoutChanges);
675 }
676 }
677 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
678 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
679 "First draw done in potential wallpaper target " + w);
680 root.mWallpaperMayChange = true;
681 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
682 if (DEBUG_LAYOUT_REPEATS) {
683 surfacePlacer.debugLayoutRepeats(
684 "wallpaper and commitFinishDrawingLocked true",
685 pendingLayoutChanges);
686 }
687 }
688 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700689 final TaskStack stack = w.getStack();
690 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
691 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800692 // Updates the shown frame before we set up the surface. This is needed
693 // because the resizing could change the top-left position (in addition to
694 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
695 // position the surface.
696 //
697 // If an animation is being started, we can't call this method because the
698 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700699 // we do want to update the position if the window is animating. We make an exception
700 // for the bounds animating state, where an application may have been waiting
701 // for an exit animation to start, but instead enters PiP. We need to ensure
702 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800703 winAnimator.computeShownFrameLocked();
704 }
705 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
706 }
707
708 final AppWindowToken atoken = w.mAppToken;
709 if (atoken != null) {
710 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
711 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
712 mTmpUpdateAllDrawn.add(atoken);
713 }
714 }
715
716 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
717 && w.isDisplayedLw()) {
718 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
719 }
720
721 w.updateResizingWindowIfNeeded();
722 };
723
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800724 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800725 * Create new {@link DisplayContent} instance, add itself to the root window container and
726 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700727 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800728 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700729 * @param layersController window layer controller used to assign layer to the windows on this
730 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700731 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
732 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700733 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700734 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700735 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800736 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
737 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
738 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
739 + " new=" + display);
740 }
741
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700742 mDisplay = display;
743 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700744 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700745 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700746 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700747 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700748 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700749 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700750 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800751 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700752 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800753 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700754
755 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700756 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700757 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700758 super.addChild(mAboveAppWindowsContainers, null);
759 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800760
761 // Add itself as a child to the root container.
762 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700763
764 // TODO(b/62541591): evaluate whether this is the best spot to declare the
765 // {@link DisplayContent} ready for use.
766 mDisplayReady = true;
767 }
768
769 boolean isReady() {
770 // The display is ready when the system and the individual display are both ready.
771 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700772 }
773
774 int getDisplayId() {
775 return mDisplayId;
776 }
777
Wale Ogunwale02319a62016-09-26 15:21:22 -0700778 WindowToken getWindowToken(IBinder binder) {
779 return mTokenMap.get(binder);
780 }
781
782 AppWindowToken getAppWindowToken(IBinder binder) {
783 final WindowToken token = getWindowToken(binder);
784 if (token == null) {
785 return null;
786 }
787 return token.asAppWindowToken();
788 }
789
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700790 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700791 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
792 if (dc != null) {
793 // We currently don't support adding a window token to the display if the display
794 // already has the binder mapped to another token. If there is a use case for supporting
795 // this moving forward we will either need to merge the WindowTokens some how or have
796 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700797 throw new IllegalArgumentException("Can't map token=" + token + " to display="
798 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700799 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700800 if (binder == null) {
801 throw new IllegalArgumentException("Can't map token=" + token + " to display="
802 + getName() + " binder is null");
803 }
804 if (token == null) {
805 throw new IllegalArgumentException("Can't map null token to display="
806 + getName() + " binder=" + binder);
807 }
808
Wale Ogunwale02319a62016-09-26 15:21:22 -0700809 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700810
811 if (token.asAppWindowToken() == null) {
812 // Add non-app token to container hierarchy on the display. App tokens are added through
813 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700814 switch (token.windowType) {
815 case TYPE_WALLPAPER:
816 mBelowAppWindowsContainers.addChild(token);
817 break;
818 case TYPE_INPUT_METHOD:
819 case TYPE_INPUT_METHOD_DIALOG:
820 mImeWindowsContainers.addChild(token);
821 break;
822 default:
823 mAboveAppWindowsContainers.addChild(token);
824 break;
825 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700826 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700827 }
828
829 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700830 final WindowToken token = mTokenMap.remove(binder);
831 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800832 token.setExiting();
833 }
834 return token;
835 }
836
837 /** Changes the display the input window token is housed on to this one. */
838 void reParentWindowToken(WindowToken token) {
839 final DisplayContent prevDc = token.getDisplayContent();
840 if (prevDc == this) {
841 return;
842 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800843 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
844 && token.asAppWindowToken() == null) {
845 // Removed the token from the map, but made sure it's not an app token before removing
846 // from parent.
847 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700848 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800849
850 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700851 }
852
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700853 void removeAppToken(IBinder binder) {
854 final WindowToken token = removeWindowToken(binder);
855 if (token == null) {
856 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
857 return;
858 }
859
860 final AppWindowToken appToken = token.asAppWindowToken();
861
862 if (appToken == null) {
863 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
864 return;
865 }
866
867 appToken.onRemovedFromDisplay();
868 }
869
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700870 Display getDisplay() {
871 return mDisplay;
872 }
873
Craig Mautner59c00972012-07-30 12:10:24 -0700874 DisplayInfo getDisplayInfo() {
875 return mDisplayInfo;
876 }
877
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700878 DisplayMetrics getDisplayMetrics() {
879 return mDisplayMetrics;
880 }
881
Andrii Kulian8ee72852017-03-10 10:36:45 -0800882 int getRotation() {
883 return mRotation;
884 }
885
886 void setRotation(int newRotation) {
887 mRotation = newRotation;
888 }
889
890 int getLastOrientation() {
891 return mLastOrientation;
892 }
893
894 void setLastOrientation(int orientation) {
895 mLastOrientation = orientation;
896 }
897
898 boolean getAltOrientation() {
899 return mAltOrientation;
900 }
901
902 void setAltOrientation(boolean altOrientation) {
903 mAltOrientation = altOrientation;
904 }
905
906 int getLastWindowForcedOrientation() {
907 return mLastWindowForcedOrientation;
908 }
909
Andrii Kulian06d07d62017-03-14 11:11:47 -0700910 /**
911 * Update rotation of the display.
912 *
913 * Returns true if the rotation has been changed. In this case YOU MUST CALL
914 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
915 */
916 boolean updateRotationUnchecked(boolean inTransaction) {
917 if (mService.mDeferredRotationPauseCount > 0) {
918 // Rotation updates have been paused temporarily. Defer the update until
919 // updates have been resumed.
920 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
921 return false;
922 }
923
924 ScreenRotationAnimation screenRotationAnimation =
925 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
926 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
927 // Rotation updates cannot be performed while the previous rotation change
928 // animation is still in progress. Skip this update. We will try updating
929 // again after the animation is finished and the display is unfrozen.
930 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
931 return false;
932 }
933 if (mService.mDisplayFrozen) {
934 // Even if the screen rotation animation has finished (e.g. isAnimating
935 // returns false), there is still some time where we haven't yet unfrozen
936 // the display. We also need to abort rotation here.
937 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
938 "Deferring rotation, still finishing previous rotation");
939 return false;
940 }
941
942 if (!mService.mDisplayEnabled) {
943 // No point choosing a rotation if the display is not enabled.
944 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
945 return false;
946 }
947
948 final int oldRotation = mRotation;
949 final int lastOrientation = mLastOrientation;
950 final boolean oldAltOrientation = mAltOrientation;
951 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700952 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
953 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700954
Robert Carr897215d2017-03-29 12:25:50 -0700955 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700956 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
957 if (seamlessRotated != null) {
958 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
959 // to complete (that is, waiting for windows to redraw). It's tempting to check
960 // w.mSeamlessRotationCount but that could be incorrect in the case of
961 // window-removal.
962 return false;
963 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700964 }
965
966 // TODO: Implement forced rotation changes.
967 // Set mAltOrientation to indicate that the application is receiving
968 // an orientation that has different metrics than it expected.
969 // eg. Portrait instead of Landscape.
970
971 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
972 lastOrientation, rotation);
973
974 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
975 + ", got rotation " + rotation + " which has "
976 + (altOrientation ? "incompatible" : "compatible") + " metrics");
977
978 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
979 // No change.
980 return false;
981 }
982
983 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
984 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
985 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
986
987 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
988 mService.mWaitingForConfig = true;
989 }
990
991 mRotation = rotation;
992 mAltOrientation = altOrientation;
993 if (isDefaultDisplay) {
994 mService.mPolicy.setRotationLw(rotation);
995 }
996
997 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
998 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
999 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1000 WINDOW_FREEZE_TIMEOUT_DURATION);
1001
1002 setLayoutNeeded();
1003 final int[] anim = new int[2];
1004 if (isDimming()) {
1005 anim[0] = anim[1] = 0;
1006 } else {
1007 mService.mPolicy.selectRotationAnimationLw(anim);
1008 }
1009
1010 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001011 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001012 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1013 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1014 mDisplayId);
1015 } else {
1016 // The screen rotation animation uses a screenshot to freeze the screen
1017 // while windows resize underneath.
1018 // When we are rotating seamlessly, we allow the elements to transition
1019 // to their rotated state independently and without a freeze required.
1020 screenRotationAnimation = null;
1021
1022 // We have to reset this in case a window was removed before it
1023 // finished seamless rotation.
1024 mService.mSeamlessRotationCount = 0;
1025 }
1026
1027 // We need to update our screen size information to match the new rotation. If the rotation
1028 // has actually changed then this method will return true and, according to the comment at
1029 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1030 // By updating the Display info here it will be available to
1031 // #computeScreenConfiguration() later.
1032 updateDisplayAndOrientation(getConfiguration().uiMode);
1033
1034 if (!inTransaction) {
1035 if (SHOW_TRANSACTIONS) {
1036 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1037 }
1038 mService.openSurfaceTransaction();
1039 }
1040 try {
1041 // NOTE: We disable the rotation in the emulator because
1042 // it doesn't support hardware OpenGL emulation yet.
1043 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1044 && screenRotationAnimation.hasScreenshot()) {
1045 if (screenRotationAnimation.setRotationInTransaction(
1046 rotation, mService.mFxSession,
1047 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1048 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1049 mService.scheduleAnimationLocked();
1050 }
1051 }
1052
1053 if (rotateSeamlessly) {
1054 forAllWindows(w -> {
1055 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1056 }, true /* traverseTopToBottom */);
1057 }
1058
1059 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1060 } finally {
1061 if (!inTransaction) {
1062 mService.closeSurfaceTransaction();
1063 if (SHOW_LIGHT_TRANSACTIONS) {
1064 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1065 }
1066 }
1067 }
1068
1069 forAllWindows(w -> {
1070 // Discard surface after orientation change, these can't be reused.
1071 if (w.mAppToken != null) {
1072 w.mAppToken.destroySavedSurfaces();
1073 }
1074 if (w.mHasSurface && !rotateSeamlessly) {
1075 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001076 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001077 mService.mRoot.mOrientationChangeComplete = false;
1078 w.mLastFreezeDuration = 0;
1079 }
1080 w.mReportOrientationChanged = true;
1081 }, true /* traverseTopToBottom */);
1082
1083 if (rotateSeamlessly) {
1084 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1085 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1086 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1087 }
1088
1089 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1090 final WindowManagerService.RotationWatcher rotationWatcher
1091 = mService.mRotationWatchers.get(i);
1092 if (rotationWatcher.mDisplayId == mDisplayId) {
1093 try {
1094 rotationWatcher.mWatcher.onRotationChanged(rotation);
1095 } catch (RemoteException e) {
1096 // Ignore
1097 }
1098 }
1099 }
1100
1101 // TODO (multi-display): Magnification is supported only for the default display.
1102 // Announce rotation only if we will not animate as we already have the
1103 // windows in final state. Otherwise, we make this call at the rotation end.
1104 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1105 && isDefaultDisplay) {
1106 mService.mAccessibilityController.onRotationChangedLocked(this);
1107 }
1108
1109 return true;
1110 }
1111
1112 /**
1113 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1114 * changed.
1115 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1116 */
1117 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1118 // Use the effective "visual" dimensions based on current rotation
1119 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1120 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1121 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1122 int dw = realdw;
1123 int dh = realdh;
1124
1125 if (mAltOrientation) {
1126 if (realdw > realdh) {
1127 // Turn landscape into portrait.
1128 int maxw = (int)(realdh/1.3f);
1129 if (maxw < realdw) {
1130 dw = maxw;
1131 }
1132 } else {
1133 // Turn portrait into landscape.
1134 int maxh = (int)(realdw/1.3f);
1135 if (maxh < realdh) {
1136 dh = maxh;
1137 }
1138 }
1139 }
1140
1141 // Update application display metrics.
1142 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1143 mDisplayId);
1144 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1145 mDisplayId);
1146 mDisplayInfo.rotation = mRotation;
1147 mDisplayInfo.logicalWidth = dw;
1148 mDisplayInfo.logicalHeight = dh;
1149 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1150 mDisplayInfo.appWidth = appWidth;
1151 mDisplayInfo.appHeight = appHeight;
1152 if (isDefaultDisplay) {
1153 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1154 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1155 }
1156 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1157 if (mDisplayScalingDisabled) {
1158 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1159 } else {
1160 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1161 }
1162
1163 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1164 mDisplayInfo);
1165
1166 mBaseDisplayRect.set(0, 0, dw, dh);
1167
1168 if (isDefaultDisplay) {
1169 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1170 mCompatDisplayMetrics);
1171 }
1172 return mDisplayInfo;
1173 }
1174
1175 /**
1176 * Compute display configuration based on display properties and policy settings.
1177 * Do not call if mDisplayReady == false.
1178 */
1179 void computeScreenConfiguration(Configuration config) {
1180 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1181
1182 final int dw = displayInfo.logicalWidth;
1183 final int dh = displayInfo.logicalHeight;
1184 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1185 Configuration.ORIENTATION_LANDSCAPE;
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001186 config.setRotation(displayInfo.rotation);
1187
Andrii Kulian06d07d62017-03-14 11:11:47 -07001188 config.screenWidthDp =
1189 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1190 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1191 config.screenHeightDp =
1192 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1193 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001194
1195 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1196 final int leftInset = mTmpRect.left;
1197 final int topInset = mTmpRect.top;
1198 // appBounds at the root level should mirror the app screen size.
1199 config.setAppBounds(leftInset /*left*/, topInset /*top*/, leftInset + displayInfo.appWidth /*right*/,
1200 topInset + displayInfo.appHeight /*bottom*/);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001201 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1202 || displayInfo.rotation == Surface.ROTATION_270);
1203
1204 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1205 mDisplayMetrics.density, config);
1206
1207 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1208 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1209 ? Configuration.SCREENLAYOUT_ROUND_YES
1210 : Configuration.SCREENLAYOUT_ROUND_NO);
1211
1212 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1213 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1214 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1215 dh, mDisplayId);
1216 config.densityDpi = displayInfo.logicalDensityDpi;
1217
1218 config.colorMode =
1219 (displayInfo.isHdr()
1220 ? Configuration.COLOR_MODE_HDR_YES
1221 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001222 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001223 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1224 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1225
1226 // Update the configuration based on available input devices, lid switch,
1227 // and platform configuration.
1228 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1229 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1230 config.navigation = Configuration.NAVIGATION_NONAV;
1231
1232 int keyboardPresence = 0;
1233 int navigationPresence = 0;
1234 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1235 final int len = devices != null ? devices.length : 0;
1236 for (int i = 0; i < len; i++) {
1237 InputDevice device = devices[i];
1238 if (!device.isVirtual()) {
1239 final int sources = device.getSources();
1240 final int presenceFlag = device.isExternal() ?
1241 WindowManagerPolicy.PRESENCE_EXTERNAL :
1242 WindowManagerPolicy.PRESENCE_INTERNAL;
1243
1244 // TODO(multi-display): Configure on per-display basis.
1245 if (mService.mIsTouchDevice) {
1246 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1247 InputDevice.SOURCE_TOUCHSCREEN) {
1248 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1249 }
1250 } else {
1251 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1252 }
1253
1254 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1255 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1256 navigationPresence |= presenceFlag;
1257 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1258 && config.navigation == Configuration.NAVIGATION_NONAV) {
1259 config.navigation = Configuration.NAVIGATION_DPAD;
1260 navigationPresence |= presenceFlag;
1261 }
1262
1263 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1264 config.keyboard = Configuration.KEYBOARD_QWERTY;
1265 keyboardPresence |= presenceFlag;
1266 }
1267 }
1268 }
1269
1270 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1271 config.navigation = Configuration.NAVIGATION_DPAD;
1272 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1273 }
1274
1275 // Determine whether a hard keyboard is available and enabled.
1276 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1277 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1278 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1279 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1280 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1281 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1282 }
1283
1284 // Let the policy update hidden states.
1285 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1286 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1287 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1288 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1289 }
1290
1291 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1292 int displayId) {
1293 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1294 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1295 final int unrotDw, unrotDh;
1296 if (rotated) {
1297 unrotDw = dh;
1298 unrotDh = dw;
1299 } else {
1300 unrotDw = dw;
1301 unrotDh = dh;
1302 }
1303 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1304 displayId);
1305 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1306 displayId);
1307 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1308 displayId);
1309 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1310 displayId);
1311 return sw;
1312 }
1313
1314 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1315 DisplayMetrics dm, int dw, int dh, int displayId) {
1316 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1317 displayId);
1318 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1319 uiMode, displayId);
1320 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1321 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1322 if (curSize == 0 || size < curSize) {
1323 curSize = size;
1324 }
1325 return curSize;
1326 }
1327
1328 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1329 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1330
1331 // We need to determine the smallest width that will occur under normal
1332 // operation. To this, start with the base screen size and compute the
1333 // width under the different possible rotations. We need to un-rotate
1334 // the current screen dimensions before doing this.
1335 int unrotDw, unrotDh;
1336 if (rotated) {
1337 unrotDw = dh;
1338 unrotDh = dw;
1339 } else {
1340 unrotDw = dw;
1341 unrotDh = dh;
1342 }
1343 displayInfo.smallestNominalAppWidth = 1<<30;
1344 displayInfo.smallestNominalAppHeight = 1<<30;
1345 displayInfo.largestNominalAppWidth = 0;
1346 displayInfo.largestNominalAppHeight = 0;
1347 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1348 unrotDh);
1349 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1350 unrotDw);
1351 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1352 unrotDh);
1353 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1354 unrotDw);
1355 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1356 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1357 displayId);
1358 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1359 displayId);
1360 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1361 displayId);
1362 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1363 displayId);
1364 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1365 outConfig.screenLayout = sl;
1366 }
1367
1368 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1369 int uiMode, int displayId) {
1370 // Get the app screen size at this rotation.
1371 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1372 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1373
1374 // Compute the screen layout size class for this rotation.
1375 int longSize = w;
1376 int shortSize = h;
1377 if (longSize < shortSize) {
1378 int tmp = longSize;
1379 longSize = shortSize;
1380 shortSize = tmp;
1381 }
1382 longSize = (int)(longSize/density);
1383 shortSize = (int)(shortSize/density);
1384 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1385 }
1386
1387 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1388 int uiMode, int dw, int dh) {
1389 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1390 displayId);
1391 if (width < displayInfo.smallestNominalAppWidth) {
1392 displayInfo.smallestNominalAppWidth = width;
1393 }
1394 if (width > displayInfo.largestNominalAppWidth) {
1395 displayInfo.largestNominalAppWidth = width;
1396 }
1397 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1398 displayId);
1399 if (height < displayInfo.smallestNominalAppHeight) {
1400 displayInfo.smallestNominalAppHeight = height;
1401 }
1402 if (height > displayInfo.largestNominalAppHeight) {
1403 displayInfo.largestNominalAppHeight = height;
1404 }
1405 }
1406
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001407 DockedStackDividerController getDockedDividerController() {
1408 return mDividerControllerLocked;
1409 }
1410
Winson Chung655332c2016-10-31 13:14:28 -07001411 PinnedStackController getPinnedStackController() {
1412 return mPinnedStackControllerLocked;
1413 }
1414
Jeff Browna506a6e2013-06-04 00:02:38 -07001415 /**
1416 * Returns true if the specified UID has access to this display.
1417 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001418 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001419 return mDisplay.hasAccess(uid);
1420 }
1421
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001422 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001423 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001424 }
1425
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001426 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001427 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001428 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001429 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001430 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001431 }
1432
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001433 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001434 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1435 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001436 if (stack.mStackId == stackId) {
1437 return stack;
1438 }
1439 }
1440 return null;
1441 }
1442
Bryce Lee48f4b572017-04-10 10:54:15 -07001443 @VisibleForTesting
1444 int getStackCount() {
1445 return mTaskStackContainers.size();
1446 }
1447
1448 @VisibleForTesting
1449 int getStaskPosById(int stackId) {
1450 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1451 final TaskStack stack = mTaskStackContainers.get(i);
1452 if (stack.mStackId == stackId) {
1453 return i;
1454 }
1455 }
1456 return -1;
1457 }
1458
Andrii Kulian441e4492016-09-29 15:25:00 -07001459 @Override
1460 void onConfigurationChanged(Configuration newParentConfig) {
1461 super.onConfigurationChanged(newParentConfig);
1462
Andrii Kulian3a507b52016-09-19 18:14:12 -07001463 // The display size information is heavily dependent on the resources in the current
1464 // configuration, so we need to reconfigure it every time the configuration changes.
1465 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1466 mService.reconfigureDisplayLocked(this);
1467
1468 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001469 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001470 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001471
Andrii Kulian441e4492016-09-29 15:25:00 -07001472 /**
1473 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1474 * bounds were updated.
1475 */
1476 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001477 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1478 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001479 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001480 changedStackList.add(stack.mStackId);
1481 }
1482 }
Winson Chung32c566f2017-04-11 18:31:21 -07001483
1484 // If there was no pinned stack, we still need to notify the controller of the display info
1485 // update as a result of the config change. We do this here to consolidate the flow between
1486 // changes when there is and is not a stack.
1487 if (getStackById(PINNED_STACK_ID) == null) {
1488 mPinnedStackControllerLocked.onDisplayInfoChanged();
1489 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001490 }
1491
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001492 @Override
1493 boolean fillsParent() {
1494 return true;
1495 }
1496
1497 @Override
1498 boolean isVisible() {
1499 return true;
1500 }
1501
1502 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001503 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001504 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001505 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001506 }
1507
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001508 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001509 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1510 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1511 // target, or here in order if there isn't an IME target.
1512 if (traverseTopToBottom) {
1513 for (int i = mChildren.size() - 1; i >= 0; --i) {
1514 final DisplayChildWindowContainer child = mChildren.get(i);
1515 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1516 // In this case the Ime windows will be processed above their target so we skip
1517 // here.
1518 continue;
1519 }
1520 if (child.forAllWindows(callback, traverseTopToBottom)) {
1521 return true;
1522 }
1523 }
1524 } else {
1525 final int count = mChildren.size();
1526 for (int i = 0; i < count; i++) {
1527 final DisplayChildWindowContainer child = mChildren.get(i);
1528 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1529 // In this case the Ime windows will be processed above their target so we skip
1530 // here.
1531 continue;
1532 }
1533 if (child.forAllWindows(callback, traverseTopToBottom)) {
1534 return true;
1535 }
1536 }
1537 }
1538 return false;
1539 }
1540
1541 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1542 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1543 }
1544
Wale Ogunwale399c8692017-05-08 14:22:42 -07001545 @Override
1546 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001547 final WindowManagerPolicy policy = mService.mPolicy;
1548
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001549 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001550 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001551 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001552 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001553 // If the display is frozen, some activities may be in the middle of restarting, and
1554 // thus have removed their old window. If the window has the flag to hide the lock
1555 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1556 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001557 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001558 } else if (policy.isKeyguardLocked()) {
1559 // Use the last orientation the while the display is frozen with the keyguard
1560 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1561 // window. We don't want to check the show when locked window directly though as
1562 // things aren't stable while the display is frozen, for example the window could be
1563 // momentarily unavailable due to activity relaunch.
1564 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001565 + "return " + mLastOrientation);
1566 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001567 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001568 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001569 final int orientation = mAboveAppWindowsContainers.getOrientation();
1570 if (orientation != SCREEN_ORIENTATION_UNSET) {
1571 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001572 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001573 }
1574
Wale Ogunwale399c8692017-05-08 14:22:42 -07001575 // Top system windows are not requesting an orientation. Start searching from apps.
1576 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001577 }
1578
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001579 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001580 // Check if display metrics changed and update base values if needed.
1581 updateBaseDisplayMetricsIfNeeded();
1582
Craig Mautner722285e2012-09-07 13:55:58 -07001583 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001584 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001585
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001586 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1587 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001588 }
Craig Mautner722285e2012-09-07 13:55:58 -07001589 }
1590
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001591 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001592 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1593 if (displayManagerInternal != null) {
1594 // Bootstrap the default logical display from the display manager.
1595 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1596 if (newDisplayInfo != null) {
1597 mDisplayInfo.copyFrom(newDisplayInfo);
1598 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001599 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001600
Andrii Kuliancd097992017-03-23 18:31:59 -07001601 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1602 mDisplayInfo.logicalDensityDpi);
1603 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1604 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1605 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001606 }
1607
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001608 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001609 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001610 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001611 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001612 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1613 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001614 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001615 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001616 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001617 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001618 out.set(left, top, left + width, top + height);
1619 }
1620
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001621 private void getLogicalDisplayRect(Rect out, int orientation) {
1622 getLogicalDisplayRect(out);
1623
1624 // Rotate the Rect if needed.
1625 final int currentRotation = mDisplayInfo.rotation;
1626 final int rotationDelta = deltaRotation(currentRotation, orientation);
1627 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1628 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1629 mTmpRectF.set(out);
1630 mTmpMatrix.mapRect(mTmpRectF);
1631 mTmpRectF.round(out);
1632 }
1633 }
1634
Andrii Kuliancd097992017-03-23 18:31:59 -07001635 /**
1636 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1637 * values.
1638 */
1639 private void updateBaseDisplayMetricsIfNeeded() {
1640 // Get real display metrics without overrides from WM.
1641 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1642 final int orientation = mDisplayInfo.rotation;
1643 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1644 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1645 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1646 final int newDensity = mDisplayInfo.logicalDensityDpi;
1647
1648 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1649 || mInitialDisplayHeight != newHeight
1650 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1651
1652 if (displayMetricsChanged) {
1653 // Check if display size or density is forced.
1654 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1655 || mBaseDisplayHeight != mInitialDisplayHeight;
1656 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1657
1658 // If there is an override set for base values - use it, otherwise use new values.
1659 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1660 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1661 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1662
1663 // Real display metrics changed, so we should also update initial values.
1664 mInitialDisplayWidth = newWidth;
1665 mInitialDisplayHeight = newHeight;
1666 mInitialDisplayDensity = newDensity;
1667 mService.reconfigureDisplayLocked(this);
1668 }
1669 }
1670
Bryce Lee27cec322017-03-21 09:41:37 -07001671 /** Sets the maximum width the screen resolution can be */
1672 void setMaxUiWidth(int width) {
1673 if (DEBUG_DISPLAY) {
1674 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1675 }
1676
1677 mMaxUiWidth = width;
1678
1679 // Update existing metrics.
1680 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1681 }
1682
1683 /** Update base (override) display metrics. */
1684 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1685 mBaseDisplayWidth = baseWidth;
1686 mBaseDisplayHeight = baseHeight;
1687 mBaseDisplayDensity = baseDensity;
1688
1689 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1690 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1691 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1692 mBaseDisplayWidth = mMaxUiWidth;
1693
1694 if (DEBUG_DISPLAY) {
1695 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1696 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1697 + " on display:" + getDisplayId());
1698 }
1699 }
1700
1701 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1702 }
1703
Chong Zhangf66db432016-01-13 10:39:51 -08001704 void getContentRect(Rect out) {
1705 out.set(mContentRect);
1706 }
1707
Wale Ogunwale1666e312016-12-16 11:27:18 -08001708 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1709 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1710 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001711
Wale Ogunwale1666e312016-12-16 11:27:18 -08001712 TaskStack stack = getStackById(stackId);
1713 if (stack != null) {
1714 // It's already attached to the display...clear mDeferRemoval and move stack to
1715 // appropriate z-order on display as needed.
1716 stack.mDeferRemoval = false;
1717 // We're not moving the display to front when we're adding stacks, only when
1718 // requested to change the position of stack explicitly.
1719 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1720 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001721 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001722 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001723 mTaskStackContainers.addStackToDisplay(stack, onTop);
1724 }
1725
Wale Ogunwale1666e312016-12-16 11:27:18 -08001726 if (stackId == DOCKED_STACK_ID) {
1727 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001728 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001729 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001730 }
1731
Andrii Kulian51c1b672017-04-07 18:39:32 -07001732 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001733 final DisplayContent prevDc = stack.getDisplayContent();
1734 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001735 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1736 + " which is not currently attached to any display");
1737 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001738 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001739 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1740 + " to its current displayId=" + mDisplayId);
1741 }
1742
Wale Ogunwale1666e312016-12-16 11:27:18 -08001743 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001744 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001745 }
1746
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001747 @Override
1748 protected void addChild(DisplayChildWindowContainer child,
1749 Comparator<DisplayChildWindowContainer> comparator) {
1750 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1751 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001752
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001753 @Override
1754 protected void addChild(DisplayChildWindowContainer child, int index) {
1755 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1756 }
1757
1758 @Override
1759 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001760 // Only allow removal of direct children from this display if the display is in the process
1761 // of been removed.
1762 if (mRemovingDisplay) {
1763 super.removeChild(child);
1764 return;
1765 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001766 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001767 }
1768
Andrii Kuliand2765632016-12-12 22:26:34 -08001769 @Override
1770 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1771 // Children of the display are statically ordered, so the real intention here is to perform
1772 // the operation on the display and not the static direct children.
1773 getParent().positionChildAt(position, this, includingParents);
1774 }
1775
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001776 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001777 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1778 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001779 final int taskId = stack.taskIdFromPoint(x, y);
1780 if (taskId != -1) {
1781 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001782 }
1783 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001784 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001785 }
1786
Chong Zhang8e89b312015-09-09 15:09:30 -07001787 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001788 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001789 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001790 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001791 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001792 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001793 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001794 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1795 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001796 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001797 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001798 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001799
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001800 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1801 if (mTmpTaskForResizePointSearchResult.searchDone) {
1802 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001803 }
1804 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001805 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001806 }
1807
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001808 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001809 // The provided task is the task on this display with focus, so if WindowManagerService's
1810 // focused app is not on this display, focusedTask will be null.
1811 if (focusedTask == null) {
1812 mTouchExcludeRegion.setEmpty();
1813 } else {
1814 mTouchExcludeRegion.set(mBaseDisplayRect);
1815 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1816 mTmpRect2.setEmpty();
1817 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1818 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1819 stack.setTouchExcludeRegion(
1820 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1821 }
1822 // If we removed the focused task above, add it back and only leave its
1823 // outside touch area in the exclusion. TapDectector is not interested in
1824 // any touch inside the focused task itself.
1825 if (!mTmpRect2.isEmpty()) {
1826 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1827 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001828 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001829 final WindowState inputMethod = mService.mInputMethodWindow;
1830 if (inputMethod != null && inputMethod.isVisibleLw()) {
1831 // If the input method is visible and the user is typing, we don't want these touch
1832 // events to be intercepted and used to change focus. This would likely cause a
1833 // disappearance of the input method.
1834 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001835 if (inputMethod.getDisplayId() == mDisplayId) {
1836 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1837 } else {
1838 // IME is on a different display, so we need to update its tap detector.
1839 // TODO(multidisplay): Remove when IME will always appear on same display.
1840 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1841 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001842 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001843 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1844 WindowState win = mTapExcludedWindows.get(i);
1845 win.getTouchableRegion(mTmpRegion);
1846 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1847 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001848 // TODO(multi-display): Support docked stacks on secondary displays.
1849 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001850 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001851 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001852 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1853 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001854 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001855 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001856 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001857 }
1858
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001859 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001860 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001861 super.switchUser();
1862 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001863 }
1864
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001865 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001866 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1867 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001868 }
1869 }
1870
1871 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001872 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001873 }
1874
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001875 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001876 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001877 }
1878
1879 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001880 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001881 }
1882
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001883 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001884 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001885 }
1886
Wale Ogunwale10124582016-09-15 20:25:50 -07001887 @Override
1888 void removeIfPossible() {
1889 if (isAnimating()) {
1890 mDeferredRemoval = true;
1891 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001892 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001893 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001894 }
1895
Wale Ogunwale10124582016-09-15 20:25:50 -07001896 @Override
1897 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001898 mRemovingDisplay = true;
1899 try {
1900 super.removeImmediately();
1901 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1902 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001903 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001904 mService.unregisterPointerEventListener(mTapDetector);
1905 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1906 }
1907 } finally {
1908 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001909 }
Craig Mautner95da1082014-02-24 17:54:35 -08001910 }
1911
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001912 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001913 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001914 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001915 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1916
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001917 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001918 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001919 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001920 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001921 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001922 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001923 }
1924
Andrii Kulian0214ed92017-05-16 13:44:05 -07001925 /** @return 'true' if removal of this display content is deferred due to active animation. */
1926 boolean isRemovalDeferred() {
1927 return mDeferredRemoval;
1928 }
1929
Wale Ogunwale10124582016-09-15 20:25:50 -07001930 boolean animateForIme(float interpolatedValue, float animationTarget,
1931 float dividerAnimationTarget) {
1932 boolean updated = false;
1933
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001934 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1935 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001936 if (stack == null || !stack.isAdjustedForIme()) {
1937 continue;
1938 }
1939
1940 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1941 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1942 updated = true;
1943 } else {
1944 mDividerControllerLocked.mLastAnimationProgress =
1945 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1946 mDividerControllerLocked.mLastDividerProgress =
1947 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1948 updated |= stack.updateAdjustForIme(
1949 mDividerControllerLocked.mLastAnimationProgress,
1950 mDividerControllerLocked.mLastDividerProgress,
1951 false /* force */);
1952 }
1953 if (interpolatedValue >= 1f) {
1954 stack.endImeAdjustAnimation();
1955 }
1956 }
1957
1958 return updated;
1959 }
1960
1961 boolean clearImeAdjustAnimation() {
1962 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001963 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1964 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001965 if (stack != null && stack.isAdjustedForIme()) {
1966 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1967 changed = true;
1968 }
1969 }
1970 return changed;
1971 }
1972
1973 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001974 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1975 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001976 if (stack.isVisible() && stack.isAdjustedForIme()) {
1977 stack.beginImeAdjustAnimation();
1978 }
1979 }
1980 }
1981
1982 void adjustForImeIfNeeded() {
1983 final WindowState imeWin = mService.mInputMethodWindow;
1984 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1985 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001986 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001987 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1988 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1989 imeTargetStack.getDockSide() : DOCKED_INVALID;
1990 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1991 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1992 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1993 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1994 final boolean imeHeightChanged = imeVisible &&
1995 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1996
1997 // The divider could be adjusted for IME position, or be thinner than usual,
1998 // or both. There are three possible cases:
1999 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2000 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2001 // - If IME is not visible, divider is not moved and is normal width.
2002
2003 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002004 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2005 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002006 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07002007 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
2008 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002009 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2010 } else {
2011 stack.resetAdjustedForIme(false);
2012 }
2013 }
2014 mDividerControllerLocked.setAdjustedForIme(
2015 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2016 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002017 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2018 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002019 stack.resetAdjustedForIme(!dockVisible);
2020 }
2021 mDividerControllerLocked.setAdjustedForIme(
2022 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2023 }
Winson Chung655332c2016-10-31 13:14:28 -07002024 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002025 }
2026
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002027 void setInputMethodAnimLayerAdjustment(int adj) {
2028 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2029 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002030 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002031 }
2032
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002033 /**
2034 * If a window that has an animation specifying a colored background and the current wallpaper
2035 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2036 * suddenly disappear.
2037 */
2038 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002039 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2040 w -> w.mIsWallpaper && w.isVisibleNow());
2041
2042 if (visibleWallpaper != null) {
2043 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002044 }
2045 return winAnimator.mAnimLayer;
2046 }
2047
Wale Ogunwale10124582016-09-15 20:25:50 -07002048 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002049 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2050 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002051 stack.prepareFreezingTaskBounds();
2052 }
2053 }
2054
Wale Ogunwale94744212015-09-21 19:01:47 -07002055 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002056 getLogicalDisplayRect(mTmpRect, newRotation);
2057
2058 // Compute a transform matrix to undo the coordinate space transformation,
2059 // and present the window at the same physical position it previously occupied.
2060 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2061 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2062
2063 mTmpRectF.set(bounds);
2064 mTmpMatrix.mapRect(mTmpRectF);
2065 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002066 }
2067
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002068 static int deltaRotation(int oldRotation, int newRotation) {
2069 int delta = newRotation - oldRotation;
2070 if (delta < 0) delta += 4;
2071 return delta;
2072 }
2073
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002074 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002075 Matrix outMatrix) {
2076 // For rotations without Z-ordering we don't need the target rectangle's position.
2077 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2078 displayHeight, outMatrix);
2079 }
2080
2081 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2082 float displayWidth, float displayHeight, Matrix outMatrix) {
2083 switch (rotation) {
2084 case ROTATION_0:
2085 outMatrix.reset();
2086 break;
2087 case ROTATION_270:
2088 outMatrix.setRotate(270, 0, 0);
2089 outMatrix.postTranslate(0, displayHeight);
2090 outMatrix.postTranslate(rectTop, 0);
2091 break;
2092 case ROTATION_180:
2093 outMatrix.reset();
2094 break;
2095 case ROTATION_90:
2096 outMatrix.setRotate(90, 0, 0);
2097 outMatrix.postTranslate(displayWidth, 0);
2098 outMatrix.postTranslate(-rectTop, rectLeft);
2099 break;
2100 }
2101 }
2102
Craig Mautnera91f9e22012-09-14 16:22:08 -07002103 public void dump(String prefix, PrintWriter pw) {
2104 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2105 final String subPrefix = " " + prefix;
2106 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2107 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2108 pw.print("dpi");
2109 if (mInitialDisplayWidth != mBaseDisplayWidth
2110 || mInitialDisplayHeight != mBaseDisplayHeight
2111 || mInitialDisplayDensity != mBaseDisplayDensity) {
2112 pw.print(" base=");
2113 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2114 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2115 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002116 if (mDisplayScalingDisabled) {
2117 pw.println(" noscale");
2118 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002119 pw.print(" cur=");
2120 pw.print(mDisplayInfo.logicalWidth);
2121 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2122 pw.print(" app=");
2123 pw.print(mDisplayInfo.appWidth);
2124 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2125 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2126 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2127 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2128 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002129 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2130 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002131
Craig Mautnerdc548482014-02-05 13:35:24 -08002132 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002133 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002134 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2135 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002136 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002137 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002138
Craig Mautnerdc548482014-02-05 13:35:24 -08002139 pw.println();
2140 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002141 pw.println();
2142 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002143 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002144 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002145 pw.print(" Exiting #"); pw.print(i);
2146 pw.print(' '); pw.print(token);
2147 pw.println(':');
2148 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002149 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002150 }
Craig Mautner59c00972012-07-30 12:10:24 -07002151 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002152 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002153 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002154 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002155 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002156 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002157
2158 if (mInputMethodAnimLayerAdjustment != 0) {
2159 pw.println(subPrefix
2160 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2161 }
Craig Mautner59c00972012-07-30 12:10:24 -07002162 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002163
2164 @Override
2165 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002166 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002167 }
2168
2169 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002170 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002171 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002172
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002173 /** Checks if stack with provided id is visible on this display. */
2174 boolean isStackVisible(int stackId) {
2175 final TaskStack stack = getStackById(stackId);
2176 return (stack != null && stack.isVisible());
2177 }
2178
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002179 /**
2180 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2181 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002182 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002183 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002184 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002185 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002186
2187 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002188 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002189 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002190 */
Jorim Jaggife762342016-10-13 14:33:27 +02002191 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002192 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002193 }
2194
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002195 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002196 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002197 final int x = (int) xf;
2198 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002199 final WindowState touchedWin = getWindow(w -> {
2200 final int flags = w.mAttrs.flags;
2201 if (!w.isVisibleLw()) {
2202 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002203 }
2204 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002205 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002206 }
2207
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002208 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002209 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002210 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002211 }
2212
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002213 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002214
2215 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002216 return mTmpRegion.contains(x, y) || touchFlags == 0;
2217 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002218
2219 return touchedWin;
2220 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002221
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002222 boolean canAddToastWindowForUid(int uid) {
2223 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002224 // Also if the app is focused adding more than one toast at
2225 // a time for better backwards compatibility.
2226 final WindowState focusedWindowForUid = getWindow(w ->
2227 w.mOwnerUid == uid && w.isFocused());
2228 if (focusedWindowForUid != null) {
2229 return true;
2230 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002231 final WindowState win = getWindow(w ->
2232 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2233 && !w.mWindowRemovalAllowed);
2234 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002235 }
2236
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002237 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002238 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2239 return;
2240 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002241
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002242 // Used to communicate the old focus to the callback method.
2243 mTmpWindow = oldFocus;
2244
2245 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002246 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002247
2248 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002249 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002250
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002251 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002252
2253 if (mTmpWindow == null) {
2254 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2255 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002256 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002257 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002258 }
2259
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002260 /** Updates the layer assignment of windows on this display. */
2261 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002262 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002263 if (setLayoutNeeded) {
2264 setLayoutNeeded();
2265 }
2266 }
2267
Wale Ogunwale1666e312016-12-16 11:27:18 -08002268 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2269 // moving containers or resizing them. Need to investigate the best way to have it automatically
2270 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002271 void layoutAndAssignWindowLayersIfNeeded() {
2272 mService.mWindowsChanged = true;
2273 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002274
2275 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2276 false /*updateInputWindows*/)) {
2277 assignWindowLayers(false /* setLayoutNeeded */);
2278 }
2279
2280 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2281 mService.mWindowPlacerLocked.performSurfacePlacement();
2282 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2283 }
2284
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002285 /** Returns true if a leaked surface was destroyed */
2286 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002287 // Used to indicate that a surface was leaked.
2288 mTmpWindow = null;
2289 forAllWindows(w -> {
2290 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002291 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002292 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002293 }
2294 if (!mService.mSessions.contains(wsa.mSession)) {
2295 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296 + w + " surface=" + wsa.mSurfaceController
2297 + " token=" + w.mToken
2298 + " pid=" + w.mSession.mPid
2299 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002300 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002301 mService.mForceRemoves.add(w);
2302 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002303 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002304 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002305 + w + " surface=" + wsa.mSurfaceController
2306 + " token=" + w.mAppToken
2307 + " saved=" + w.hasSavedSurface());
2308 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002309 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002310 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002311 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002312 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002313
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002314 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002315 }
2316
2317 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002318 * Determine and return the window that should be the IME target.
2319 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2320 * @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 +00002321 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002322 WindowState computeImeTarget(boolean updateImeTarget) {
2323 if (mService.mInputMethodWindow == null) {
2324 // There isn't an IME so there shouldn't be a target...That was easy!
2325 if (updateImeTarget) {
2326 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2327 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2328 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2329 }
2330 return null;
2331 }
2332
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002333 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2334 // same display. Or even when the current IME/target are not on the same screen as the next
2335 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002336 mUpdateImeTarget = updateImeTarget;
2337 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002338
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002339
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002340 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2341 // to be on top of it, but it is not -really- where input will go. So look down below
2342 // for a real window to target...
2343 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2344 final AppWindowToken token = target.mAppToken;
2345 if (token != null) {
2346 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2347 if (betterTarget != null) {
2348 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002349 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002350 }
2351 }
2352
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002353 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2354 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002355
2356 // Now, a special case -- if the last target's window is in the process of exiting, and is
2357 // above the new target, keep on the last target to avoid flicker. Consider for example a
2358 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2359 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2360 // scrim.
2361 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002362 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2363 && (target == null
2364 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002365 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002366 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002367 }
2368
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002369 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2370 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002371
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002372 if (target == null) {
2373 if (updateImeTarget) {
2374 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2375 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2376 + Debug.getCallers(4) : ""));
2377 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2378 }
2379
2380 return null;
2381 }
2382
2383 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002384 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2385 if (token != null) {
2386
2387 // Now some fun for dealing with window animations that modify the Z order. We need
2388 // to look at all windows below the current target that are in this app, finding the
2389 // highest visible one in layering.
2390 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002391 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002393 }
2394
2395 if (highestTarget != null) {
2396 final AppTransition appTransition = mService.mAppTransition;
2397 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2398 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2399 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002400 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002401
2402 if (appTransition.isTransitionSet()) {
2403 // If we are currently setting up for an animation, hold everything until we
2404 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002405 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2406 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002407 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002408 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002409 // If the window we are currently targeting is involved with an animation,
2410 // and it is on top of the next target we will be over, then hold off on
2411 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002412 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2413 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002414 }
2415 }
2416 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002417
2418 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2419 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2420 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002421 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002422 }
2423
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002424 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002425 }
2426
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002427 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2428 if (target == mService.mInputMethodTarget
2429 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2430 && mInputMethodAnimLayerAdjustment == layerAdj) {
2431 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002432 }
2433
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002434 mService.mInputMethodTarget = target;
2435 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2436 setInputMethodAnimLayerAdjustment(layerAdj);
2437 assignWindowLayers(false /* setLayoutNeeded */);
2438 }
2439
2440 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2441 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2442 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2443 }
2444
2445 // Used to indicate we have reached the first window in the range we are interested in.
2446 mTmpWindow = null;
2447
2448 // TODO: Figure-out a more efficient way to do this.
2449 final WindowState candidate = getWindow(w -> {
2450 if (w == top) {
2451 // Reached the first window in the range we are interested in.
2452 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002453 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002454 if (mTmpWindow == null) {
2455 return false;
2456 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002457
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002458 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2459 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002460 }
2461 // If we reached the bottom of the range of windows we are considering,
2462 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002463 if (w == bottom) {
2464 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002465 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002466 return false;
2467 });
2468
2469 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002470 }
2471
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002472 void setLayoutNeeded() {
2473 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2474 mLayoutNeeded = true;
2475 }
2476
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002477 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002478 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2479 mLayoutNeeded = false;
2480 }
2481
2482 boolean isLayoutNeeded() {
2483 return mLayoutNeeded;
2484 }
2485
Wale Ogunwale02319a62016-09-26 15:21:22 -07002486 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2487 if (mTokenMap.isEmpty()) {
2488 return;
2489 }
2490 pw.println(" Display #" + mDisplayId);
2491 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2492 while (it.hasNext()) {
2493 final WindowToken token = it.next();
2494 pw.print(" ");
2495 pw.print(token);
2496 if (dumpAll) {
2497 pw.println(':');
2498 token.dump(pw, " ");
2499 } else {
2500 pw.println();
2501 }
2502 }
2503 }
2504
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002505 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002506 final int[] index = new int[1];
2507 forAllWindows(w -> {
2508 final WindowStateAnimator wAnim = w.mWinAnimator;
2509 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2510 index[0] = index[0] + 1;
2511 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002512 }
2513
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002514 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002515 forAllWindows(w -> {
2516 w.mWinAnimator.enableSurfaceTrace(fd);
2517 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002518 }
2519
2520 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002521 forAllWindows(w -> {
2522 w.mWinAnimator.disableSurfaceTrace();
2523 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002524 }
2525
Jorim Jaggife762342016-10-13 14:33:27 +02002526 /**
2527 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2528 */
2529 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2530 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002531 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002532 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2533 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002534 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002535 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2536 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002537 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002538 }
2539
Wale Ogunwale494009b82016-10-21 09:01:38 -07002540 boolean checkWaitingForWindows() {
2541
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002542 mHaveBootMsg = false;
2543 mHaveApp = false;
2544 mHaveWallpaper = false;
2545 mHaveKeyguard = true;
2546
2547 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002548 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2549 return true;
2550 }
2551 if (w.isDrawnLw()) {
2552 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002553 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002554 } else if (w.mAttrs.type == TYPE_APPLICATION
2555 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002556 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002557 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002558 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002559 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002560 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002561 }
2562 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002563 return false;
2564 });
2565
2566 if (visibleWindow != null) {
2567 // We have a visible window.
2568 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002569 }
2570
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002571 // if the wallpaper service is disabled on the device, we're never going to have
2572 // wallpaper, don't bother waiting for it
2573 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2574 com.android.internal.R.bool.config_enableWallpaperService)
2575 && !mService.mOnlyCore;
2576
Wale Ogunwale494009b82016-10-21 09:01:38 -07002577 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2578 "******** booted=" + mService.mSystemBooted
2579 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002580 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2581 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2582 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002583
2584 // If we are turning on the screen to show the boot message, don't do it until the boot
2585 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002586 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002587 return true;
2588 }
2589
2590 // If we are turning on the screen after the boot is completed normally, don't do so until
2591 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002592 if (mService.mSystemBooted
2593 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002594 return true;
2595 }
2596
2597 return false;
2598 }
2599
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002600 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002601 mTmpWindowAnimator = animator;
2602 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002603 }
2604
2605 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002606 resetAnimationBackgroundAnimator();
2607
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002608 // Used to indicate a detached wallpaper.
2609 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002610 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002611
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002612 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002613
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002614 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002615 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2617 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002618 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2619 }
2620 }
2621
2622 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002623 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002624 }
2625
Wale Ogunwale494009b82016-10-21 09:01:38 -07002626 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002627 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002628 if (imFocus == null) {
2629 return false;
2630 }
2631
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 if (DEBUG_INPUT_METHOD) {
2633 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2634 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2635 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002636 }
2637
Wale Ogunwale494009b82016-10-21 09:01:38 -07002638 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2639
2640 if (DEBUG_INPUT_METHOD) {
2641 Slog.i(TAG_WM, "IM target client: " + imeClient);
2642 if (imeClient != null) {
2643 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2644 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2645 }
2646 }
2647
2648 return imeClient != null && imeClient.asBinder() == client.asBinder();
2649 }
2650
2651 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002652 final WindowState win = getWindow(
2653 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2654 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002655 }
2656
2657 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002658 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002659 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002660 final int curValue = w.mSystemUiVisibility;
2661 final int diff = (curValue ^ visibility) & globalDiff;
2662 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002663 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002664 w.mSeq++;
2665 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002666 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002667 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2668 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002669 visibility, newValue, diff);
2670 }
2671 } catch (RemoteException e) {
2672 // so sorry
2673 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002674 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002675 }
2676
2677 void onWindowFreezeTimeout() {
2678 Slog.w(TAG_WM, "Window freeze timeout expired.");
2679 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002680
2681 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002682 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002683 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002684 }
Bryce Lee8c3cf382017-07-06 19:47:10 -07002685 w.setOrientationChanging(false);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002686 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2687 - mService.mDisplayFreezeTime);
2688 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002689 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002690 mService.mWindowPlacerLocked.performSurfacePlacement();
2691 }
2692
2693 void waitForAllWindowsDrawn() {
2694 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002695 forAllWindows(w -> {
2696 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2697 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2698 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002699 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002700 w.mLastContentInsets.set(-1, -1, -1, -1);
2701 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002702 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002703 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002704 }
2705
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002706 // TODO: Super crazy long method that should be broken down...
2707 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2708
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002709 final int dw = mDisplayInfo.logicalWidth;
2710 final int dh = mDisplayInfo.logicalHeight;
2711 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2712
2713 mTmpUpdateAllDrawn.clear();
2714
2715 int repeats = 0;
2716 do {
2717 repeats++;
2718 if (repeats > 6) {
2719 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2720 clearLayoutNeeded();
2721 break;
2722 }
2723
2724 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2725 pendingLayoutChanges);
2726
Andrii Kulian839def92016-11-02 10:58:58 -07002727 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2728 // the wallpaper window jumping across displays.
2729 // Remove check for default display when there will be support for multiple wallpaper
2730 // targets (on different displays).
2731 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002732 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002733 }
2734
2735 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2736 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2737 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2738 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002739 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002740 }
2741 }
2742
2743 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2744 setLayoutNeeded();
2745 }
2746
2747 // FIRST LOOP: Perform a layout, if needed.
2748 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2749 performLayout(repeats == 1, false /* updateInputWindows */);
2750 } else {
2751 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2752 }
2753
2754 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2755 pendingLayoutChanges = 0;
2756
2757 if (isDefaultDisplay) {
2758 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002759 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002760 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2761 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2762 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2763 }
2764 } while (pendingLayoutChanges != 0);
2765
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002766 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002767 resetDimming();
2768
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002769 mTmpRecoveringMemory = recoveringMemory;
2770 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002771
2772 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002773 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2774 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2775 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002776 true /* inTraversal, must call performTraversalInTrans... below */);
2777
2778 stopDimmingIfNeeded();
2779
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002780 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2781 if (wallpaperVisible != mLastWallpaperVisible) {
2782 mLastWallpaperVisible = wallpaperVisible;
2783 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2784 }
2785
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002786 while (!mTmpUpdateAllDrawn.isEmpty()) {
2787 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2788 // See if any windows have been drawn, so they (and others associated with them)
2789 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002790 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002791 }
2792
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002793 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002794 }
2795
2796 void performLayout(boolean initial, boolean updateInputWindows) {
2797 if (!isLayoutNeeded()) {
2798 return;
2799 }
2800 clearLayoutNeeded();
2801
2802 final int dw = mDisplayInfo.logicalWidth;
2803 final int dh = mDisplayInfo.logicalHeight;
2804
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002805 if (DEBUG_LAYOUT) {
2806 Slog.v(TAG, "-------------------------------------");
2807 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2808 }
2809
Andrii Kulian8ee72852017-03-10 10:36:45 -08002810 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002811 getConfiguration().uiMode);
2812 if (isDefaultDisplay) {
2813 // Not needed on non-default displays.
2814 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2815 mService.mScreenRect.set(0, 0, dw, dh);
2816 }
2817
2818 mService.mPolicy.getContentRectLw(mContentRect);
2819
2820 int seq = mService.mLayoutSeq + 1;
2821 if (seq < 0) seq = 0;
2822 mService.mLayoutSeq = seq;
2823
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002824 // Used to indicate that we have processed the dream window and all additional windows are
2825 // behind it.
2826 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002827 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002828
2829 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002830 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002831
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002832 // Used to indicate that we have processed the dream window and all additional attached
2833 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002834 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002835 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002836
2837 // Now perform layout of attached windows, which usually depend on the position of the
2838 // window they are attached to. XXX does not deal with windows that are attached to windows
2839 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002840 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002841
2842 // Window frames may have changed. Tell the input dispatcher about it.
2843 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2844 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2845 if (updateInputWindows) {
2846 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2847 }
2848
2849 mService.mPolicy.finishLayoutLw();
2850 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2851 }
2852
2853 /**
2854 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2855 * In portrait mode, it grabs the full screenshot.
2856 *
2857 * @param width the width of the target bitmap
2858 * @param height the height of the target bitmap
2859 * @param includeFullDisplay true if the screen should not be cropped before capture
2860 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2861 * @param config of the output bitmap
2862 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002863 * @param includeDecor whether to include window decors, like the status or navigation bar
2864 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002865 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002866 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002867 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002868 boolean wallpaperOnly, boolean includeDecor) {
2869 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2870 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002871 if (bitmap == null) {
2872 return null;
2873 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002874
2875 if (DEBUG_SCREENSHOT) {
2876 // TEST IF IT's ALL BLACK
2877 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2878 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2879 bitmap.getHeight());
2880 boolean allBlack = true;
2881 final int firstColor = buffer[0];
2882 for (int i = 0; i < buffer.length; i++) {
2883 if (buffer[i] != firstColor) {
2884 allBlack = false;
2885 break;
2886 }
2887 }
2888 if (allBlack) {
2889 final WindowState appWin = mScreenshotApplicationState.appWin;
2890 final int maxLayer = mScreenshotApplicationState.maxLayer;
2891 final int minLayer = mScreenshotApplicationState.minLayer;
2892 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2893 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2894 (appWin != null ?
2895 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2896 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2897 }
2898 }
2899
2900 // Create a copy of the screenshot that is immutable and backed in ashmem.
2901 // This greatly reduces the overhead of passing the bitmap between processes.
2902 Bitmap ret = bitmap.createAshmemBitmap(config);
2903 bitmap.recycle();
2904 return ret;
2905 }
2906
2907 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2908 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2909 boolean includeDecor) {
2910 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2911 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2912 }
2913
2914 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2915 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2916 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002917 int dw = mDisplayInfo.logicalWidth;
2918 int dh = mDisplayInfo.logicalHeight;
2919 if (dw == 0 || dh == 0) {
2920 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2921 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2922 return null;
2923 }
2924
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002925 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002926
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002927 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002928 final Rect frame = new Rect();
2929 final Rect stackBounds = new Rect();
2930
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002931 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002932 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002933 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002934 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002935 if (!mService.mPolicy.isScreenOn()) {
2936 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
2937 + " was off.");
2938 return null;
2939 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002940 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002941 mScreenshotApplicationState.appWin = null;
2942 forAllWindows(w -> {
2943 if (!w.mHasSurface) {
2944 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002945 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002946 if (w.mLayer >= aboveAppLayer) {
2947 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002948 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002949 if (wallpaperOnly && !w.mIsWallpaper) {
2950 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002952 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02002953 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002954 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2956 // then the target window state is this one.
2957 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002958 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002959 }
2960
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002961 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002962 // We have not ran across the target window yet, so it is probably behind
2963 // the wallpaper. This can happen when the keyguard is up and all windows
2964 // are moved behind the wallpaper. We don't want to include the wallpaper
2965 // layer in the screenshot as it will cover-up the layer of the target
2966 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002967 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002968 }
2969 // Fall through. The target window is in front of the wallpaper. For this
2970 // case we want to include the wallpaper layer in the screenshot because
2971 // the target window might have some transparent areas.
2972 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002973 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002974 // This app window is of no interest if it is not associated with the
2975 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002976 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002977 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002978 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002979 }
2980
2981 // Include this window.
2982
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002983 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002984 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002985 if (mScreenshotApplicationState.maxLayer < layer) {
2986 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002987 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002988 if (mScreenshotApplicationState.minLayer > layer) {
2989 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002990 }
2991
2992 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02002993 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
2994 if (includeDecor) {
2995 final TaskStack stack = w.getStack();
2996 if (stack != null) {
2997 stack.getBounds(frame);
2998 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02002999
Jorim Jaggi70f59482017-05-04 14:05:59 +02003000 // We want to screenshot with the exact bounds of the surface of the app. Thus,
3001 // intersect it with the frame.
3002 frame.intersect(w.mFrame);
3003 } else {
3004 final Rect wf = w.mFrame;
3005 final Rect cr = w.mContentInsets;
3006 int left = wf.left + cr.left;
3007 int top = wf.top + cr.top;
3008 int right = wf.right - cr.right;
3009 int bottom = wf.bottom - cr.bottom;
3010 frame.union(left, top, right, bottom);
3011 w.getVisibleBounds(stackBounds);
3012 if (!Rect.intersects(frame, stackBounds)) {
3013 // Set frame empty if there's no intersection.
3014 frame.setEmpty();
3015 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003016 }
3017 }
3018
3019 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003020 (w.mAppToken != null && w.mAppToken.token == appToken)
3021 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01003022 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003023 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003024 }
3025
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003026 if (w.isObscuringDisplay()){
3027 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003028 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003029 return false;
3030 }, true /* traverseTopToBottom */);
3031
3032 final WindowState appWin = mScreenshotApplicationState.appWin;
3033 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3034 final int maxLayer = mScreenshotApplicationState.maxLayer;
3035 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003036
3037 if (appToken != null && appWin == null) {
3038 // Can't find a window to snapshot.
3039 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3040 "Screenshot: Couldn't find a surface matching " + appToken);
3041 return null;
3042 }
3043
3044 if (!screenshotReady) {
3045 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3046 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3047 appWin.mWinAnimator.mDrawState)));
3048 return null;
3049 }
3050
3051 // Screenshot is ready to be taken. Everything from here below will continue
3052 // through the bottom of the loop and return a value. We only stay in the loop
3053 // because we don't want to release the mWindowMap lock until the screenshot is
3054 // taken.
3055
3056 if (maxLayer == 0) {
3057 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3058 + ": returning null maxLayer=" + maxLayer);
3059 return null;
3060 }
3061
Jorim Jaggi02886a82016-12-06 09:10:06 -08003062 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003063 // Constrain frame to the screen size.
3064 if (!frame.intersect(0, 0, dw, dh)) {
3065 frame.setEmpty();
3066 }
3067 } else {
3068 // Caller just wants entire display.
3069 frame.set(0, 0, dw, dh);
3070 }
3071 if (frame.isEmpty()) {
3072 return null;
3073 }
3074
3075 if (width < 0) {
3076 width = (int) (frame.width() * frameScale);
3077 }
3078 if (height < 0) {
3079 height = (int) (frame.height() * frameScale);
3080 }
3081
3082 // Tell surface flinger what part of the image to crop. Take the top
3083 // right part of the application, and crop the larger dimension to fit.
3084 Rect crop = new Rect(frame);
3085 if (width / (float) frame.width() < height / (float) frame.height()) {
3086 int cropWidth = (int)((float)width / (float)height * frame.height());
3087 crop.right = crop.left + cropWidth;
3088 } else {
3089 int cropHeight = (int)((float)height / (float)width * frame.width());
3090 crop.bottom = crop.top + cropHeight;
3091 }
3092
3093 // The screenshot API does not apply the current screen rotation.
3094 int rot = mDisplay.getRotation();
3095
3096 if (rot == ROTATION_90 || rot == ROTATION_270) {
3097 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3098 }
3099
3100 // Surfaceflinger is not aware of orientation, so convert our logical
3101 // crop to surfaceflinger's portrait orientation.
3102 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3103
3104 if (DEBUG_SCREENSHOT) {
3105 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3106 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003107 forAllWindows(w -> {
3108 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3109 Slog.i(TAG_WM, w + ": " + w.mLayer
3110 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003111 + " surfaceLayer=" + ((controller == null)
3112 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003113 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003114 }
3115
3116 final ScreenRotationAnimation screenRotationAnimation =
3117 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3118 final boolean inRotation = screenRotationAnimation != null &&
3119 screenRotationAnimation.isAnimating();
3120 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3121 "Taking screenshot while rotating");
3122
3123 // We force pending transactions to flush before taking
3124 // the screenshot by pushing an empty synchronous transaction.
3125 SurfaceControl.openTransaction();
3126 SurfaceControl.closeTransactionSync();
3127
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003128 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003129 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003130 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003131 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3132 + ") to layer " + maxLayer);
3133 return null;
3134 }
3135 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003136 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003137 }
3138
3139 // TODO: Can this use createRotationMatrix()?
3140 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3141 if (rot == Surface.ROTATION_90) {
3142 final int tmp = crop.top;
3143 crop.top = dw - crop.right;
3144 crop.right = crop.bottom;
3145 crop.bottom = dw - crop.left;
3146 crop.left = tmp;
3147 } else if (rot == Surface.ROTATION_180) {
3148 int tmp = crop.top;
3149 crop.top = dh - crop.bottom;
3150 crop.bottom = dh - tmp;
3151 tmp = crop.right;
3152 crop.right = dw - crop.left;
3153 crop.left = dw - tmp;
3154 } else if (rot == Surface.ROTATION_270) {
3155 final int tmp = crop.top;
3156 crop.top = crop.left;
3157 crop.left = dh - crop.bottom;
3158 crop.bottom = crop.right;
3159 crop.right = dh - tmp;
3160 }
3161 }
3162
3163 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003164 // Used to indicate the layout is needed.
3165 mTmpWindow = null;
3166
3167 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003168 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003169 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003170 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003171 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003172 w.setDisplayLayoutNeeded();
3173 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003174 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003175
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003176 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003177 mService.mWindowPlacerLocked.performSurfacePlacement();
3178 }
3179 }
3180
Wale Ogunwale1666e312016-12-16 11:27:18 -08003181 void setExitingTokensHasVisible(boolean hasVisible) {
3182 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3183 mExitingTokens.get(i).hasVisible = hasVisible;
3184 }
3185
3186 // Initialize state of exiting applications.
3187 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3188 }
3189
3190 void removeExistingTokensIfPossible() {
3191 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3192 final WindowToken token = mExitingTokens.get(i);
3193 if (!token.hasVisible) {
3194 mExitingTokens.remove(i);
3195 }
3196 }
3197
3198 // Time to remove any exiting applications?
3199 mTaskStackContainers.removeExistingAppTokensIfPossible();
3200 }
3201
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003202 @Override
3203 void onDescendantOverrideConfigurationChanged() {
3204 setLayoutNeeded();
3205 mService.requestTraversal();
3206 }
3207
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003208 static final class TaskForResizePointSearchResult {
3209 boolean searchDone;
3210 Task taskForResize;
3211
3212 void reset() {
3213 searchDone = false;
3214 taskForResize = null;
3215 }
3216 }
Robert Carr3b716242016-08-16 16:02:21 -07003217
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003218 private static final class ApplySurfaceChangesTransactionState {
3219 boolean displayHasContent;
3220 boolean obscured;
3221 boolean syswin;
3222 boolean focusDisplayed;
3223 float preferredRefreshRate;
3224 int preferredModeId;
3225
3226 void reset() {
3227 displayHasContent = false;
3228 obscured = false;
3229 syswin = false;
3230 focusDisplayed = false;
3231 preferredRefreshRate = 0;
3232 preferredModeId = 0;
3233 }
3234 }
3235
3236 private static final class ScreenshotApplicationState {
3237 WindowState appWin;
3238 int maxLayer;
3239 int minLayer;
3240 boolean screenshotReady;
3241
3242 void reset(boolean screenshotReady) {
3243 appWin = null;
3244 maxLayer = 0;
3245 minLayer = 0;
3246 this.screenshotReady = screenshotReady;
3247 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3248 }
3249 }
3250
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003251 /**
3252 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3253 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3254 * homogeneous children type which is currently required by sub-classes of
3255 * {@link WindowContainer} class.
3256 */
3257 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3258
3259 int size() {
3260 return mChildren.size();
3261 }
3262
3263 E get(int index) {
3264 return mChildren.get(index);
3265 }
3266
3267 @Override
3268 boolean fillsParent() {
3269 return true;
3270 }
3271
3272 @Override
3273 boolean isVisible() {
3274 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003275 }
3276 }
3277
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003278 /**
3279 * Window container class that contains all containers on this display relating to Apps.
3280 * I.e Activities.
3281 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003282 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003283
Andrii Kulian839def92016-11-02 10:58:58 -07003284 /**
3285 * Adds the stack to this container.
3286 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3287 */
3288 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003289 if (stack.mStackId == HOME_STACK_ID) {
3290 if (mHomeStack != null) {
3291 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3292 }
3293 mHomeStack = stack;
3294 }
3295 addChild(stack, onTop);
3296 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003297 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003298
Andrii Kulian839def92016-11-02 10:58:58 -07003299 /** Removes the stack from its container and prepare for changing the parent. */
3300 void removeStackFromDisplay(TaskStack stack) {
3301 removeChild(stack);
3302 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003303 }
3304
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003305 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003306 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3307 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003308 addChild(stack, addIndex);
3309 setLayoutNeeded();
3310 }
3311
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003312 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003313 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3314 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3315 // This stack is always-on-top, override the default behavior.
3316 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3317
3318 // Moving to its current position, as we must call super but we don't want to
3319 // perform any meaningful action.
3320 final int currentPosition = mChildren.indexOf(child);
3321 super.positionChildAt(currentPosition, child, false /* includingParents */);
3322 return;
3323 }
3324
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003325 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3326 super.positionChildAt(targetPosition, child, includingParents);
3327
3328 setLayoutNeeded();
3329 }
3330
3331 /**
3332 * When stack is added or repositioned, find a proper position for it.
3333 * This will make sure that pinned stack always stays on top.
3334 * @param requestedPosition Position requested by caller.
3335 * @param stack Stack to be added or positioned.
3336 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3337 * @return The proper position for the stack.
3338 */
3339 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3340 final int topChildPosition = mChildren.size() - 1;
3341 boolean toTop = requestedPosition == POSITION_TOP;
3342 toTop |= adding ? requestedPosition >= topChildPosition + 1
3343 : requestedPosition >= topChildPosition;
3344 int targetPosition = requestedPosition;
3345
Bryce Lee48f4b572017-04-10 10:54:15 -07003346 if (toTop && stack.mStackId != PINNED_STACK_ID
3347 && getStackById(PINNED_STACK_ID) != null) {
3348 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003349 TaskStack topStack = mChildren.get(topChildPosition);
3350 if (topStack.mStackId != PINNED_STACK_ID) {
3351 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3352 }
3353
3354 // So, stack is moved just below the pinned stack.
3355 // When we're adding a new stack the target is the current pinned stack position.
3356 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003357 // stack, because WindowContainer#positionAt() first removes element and then adds
3358 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003359 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3360 }
3361
3362 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003363 }
3364
3365 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003366 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3367 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003368 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003369 if (super.forAllWindows(callback, traverseTopToBottom)) {
3370 return true;
3371 }
3372 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3373 return true;
3374 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003375 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003376 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3377 return true;
3378 }
3379 if (super.forAllWindows(callback, traverseTopToBottom)) {
3380 return true;
3381 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003382 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003383 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003384 }
3385
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003386 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003387 boolean traverseTopToBottom) {
3388 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3389 // app tokens.
3390 // TODO: Investigate if we need to continue to do this or if we can just process them
3391 // in-order.
3392 if (traverseTopToBottom) {
3393 for (int i = mChildren.size() - 1; i >= 0; --i) {
3394 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3395 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003396 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3397 traverseTopToBottom)) {
3398 return true;
3399 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003400 }
3401 }
3402 } else {
3403 final int count = mChildren.size();
3404 for (int i = 0; i < count; ++i) {
3405 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3406 final int appTokensCount = appTokens.size();
3407 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003408 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3409 traverseTopToBottom)) {
3410 return true;
3411 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003412 }
3413 }
3414 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003415 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003416 }
3417
Wale Ogunwale1666e312016-12-16 11:27:18 -08003418 void setExitingTokensHasVisible(boolean hasVisible) {
3419 for (int i = mChildren.size() - 1; i >= 0; --i) {
3420 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3421 for (int j = appTokens.size() - 1; j >= 0; --j) {
3422 appTokens.get(j).hasVisible = hasVisible;
3423 }
3424 }
3425 }
3426
3427 void removeExistingAppTokensIfPossible() {
3428 for (int i = mChildren.size() - 1; i >= 0; --i) {
3429 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3430 for (int j = appTokens.size() - 1; j >= 0; --j) {
3431 final AppWindowToken token = appTokens.get(j);
3432 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3433 && (!token.mIsExiting || token.isEmpty())) {
3434 // Make sure there is no animation running on this token, so any windows
3435 // associated with it will be removed as soon as their animations are
3436 // complete.
3437 token.mAppAnimator.clearAnimation();
3438 token.mAppAnimator.animating = false;
3439 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3440 "performLayout: App token exiting now removed" + token);
3441 token.removeIfPossible();
3442 }
3443 }
3444 }
3445 }
3446
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003447 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003448 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003449 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003450 // Apps and their containers are not allowed to specify an orientation while the
3451 // docked or freeform stack is visible...except for the home stack/task if the
3452 // docked stack is minimized and it actually set something.
3453 if (mHomeStack != null && mHomeStack.isVisible()
3454 && mDividerControllerLocked.isMinimizedDock()) {
3455 final int orientation = mHomeStack.getOrientation();
3456 if (orientation != SCREEN_ORIENTATION_UNSET) {
3457 return orientation;
3458 }
3459 }
3460 return SCREEN_ORIENTATION_UNSPECIFIED;
3461 }
3462
3463 final int orientation = super.getOrientation();
3464 if (orientation != SCREEN_ORIENTATION_UNSET
3465 && orientation != SCREEN_ORIENTATION_BEHIND) {
3466 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3467 "App is requesting an orientation, return " + orientation);
3468 return orientation;
3469 }
3470
3471 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003472 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003473 // The next app has not been requested to be visible, so we keep the current orientation
3474 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003475 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003476 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003477 }
3478
3479 /**
3480 * Window container class that contains all containers on this display that are not related to
3481 * Apps. E.g. status bar.
3482 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003483 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3484 /**
3485 * Compares two child window tokens returns -1 if the first is lesser than the second in
3486 * terms of z-order and 1 otherwise.
3487 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003488 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003489 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003490 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3491 token1.mOwnerCanManageAppTokens)
3492 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3493 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003494
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003495 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3496 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3497 return false;
3498 }
3499 final int req = w.mAttrs.screenOrientation;
3500 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3501 || req == SCREEN_ORIENTATION_UNSET) {
3502 return false;
3503 }
3504 return true;
3505 };
3506
Wale Ogunwale3a931692016-11-02 16:49:48 -07003507 private final String mName;
3508 NonAppWindowContainers(String name) {
3509 mName = name;
3510 }
3511
3512 void addChild(WindowToken token) {
3513 addChild(token, mWindowComparator);
3514 }
3515
3516 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003517 int getOrientation() {
3518 final WindowManagerPolicy policy = mService.mPolicy;
3519 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003520 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003521
3522 if (win != null) {
3523 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003524 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003525 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003526 if (mService.mKeyguardGoingAway) {
3527 // Keyguard can't affect the orientation if it is going away...
3528 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3529 return SCREEN_ORIENTATION_UNSET;
3530 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003531 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003532 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003533 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003534 }
3535
Andrii Kulian8ee72852017-03-10 10:36:45 -08003536 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003537
3538 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003539 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003540 }
3541
3542 return SCREEN_ORIENTATION_UNSET;
3543 }
3544
3545 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003546 String getName() {
3547 return mName;
3548 }
Robert Carr3b716242016-08-16 16:02:21 -07003549 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003550
3551 /**
3552 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3553 */
3554 @FunctionalInterface
3555 private interface Screenshoter<E> {
3556 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3557 boolean useIdentityTransform, int rotation);
3558 }
Craig Mautner59c00972012-07-30 12:10:24 -07003559}