blob: 3c68e4ff4e7fde63e47fbb20ca431276646eadc2 [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Wale Ogunwale51362492016-09-08 17:49:17 -070020import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.HOME_STACK_ID;
22import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
25import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070026import static android.view.Display.DEFAULT_DISPLAY;
27import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070028import static android.view.Surface.ROTATION_0;
29import static android.view.Surface.ROTATION_180;
30import static android.view.Surface.ROTATION_270;
31import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070032import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070033import static android.view.WindowManager.DOCKED_BOTTOM;
34import static android.view.WindowManager.DOCKED_INVALID;
35import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080036import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
37import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
38import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070039import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070040import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070041import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
42import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070043import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070045import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070046import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070047import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070049import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
50import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070052import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
53import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070054import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070055import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070056import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
58import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070059import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080060import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070061import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070062import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070063import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
64import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070076import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070077import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070078import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070079import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080080import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070081import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070082import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
83import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070084import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070085import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070086import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
87import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
89import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
90import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070091import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070092import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070093import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070094import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070095import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070096import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -070097import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070098import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070099import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700100
Andrii Kulian3a507b52016-09-19 18:14:12 -0700101import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700102import android.app.ActivityManager.StackId;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700103import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700104import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700105import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100106import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700107import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800108import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700109import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700110import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100111import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700112import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700113import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700114import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700115import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700116import android.os.RemoteException;
117import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700118import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800119import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700120import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700121import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700122import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700123import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700124import android.view.Surface;
125import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700126import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700127
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800128import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700129import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700130
Robert Carr3b716242016-08-16 16:02:21 -0700131import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700132import java.io.PrintWriter;
133import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700134import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700135import java.util.HashMap;
136import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700137import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700138import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800139import java.util.function.Consumer;
140import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700141
Craig Mautner59c00972012-07-30 12:10:24 -0700142/**
143 * Utility class for keeping track of the WindowStates and other pertinent contents of a
144 * particular Display.
145 *
146 * IMPORTANT: No method from this class should ever be used without holding
147 * WindowManagerService.mWindowMap.
148 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700149class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700150 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700151
152 /** Unique identifier of this stack. */
153 private final int mDisplayId;
154
Wale Ogunwale3a931692016-11-02 16:49:48 -0700155 /** The containers below are the only child containers the display can have. */
156 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700157 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700158 // Contains all non-app window containers that should be displayed above the app containers
159 // (e.g. Status bar)
160 private final NonAppWindowContainers mAboveAppWindowsContainers =
161 new NonAppWindowContainers("mAboveAppWindowsContainers");
162 // Contains all non-app window containers that should be displayed below the app containers
163 // (e.g. Wallpaper).
164 private final NonAppWindowContainers mBelowAppWindowsContainers =
165 new NonAppWindowContainers("mBelowAppWindowsContainers");
166 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
167 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
168 // window containers together and move them in-sync if/when needed.
169 private final NonAppWindowContainers mImeWindowsContainers =
170 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700171
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000172 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800173 private WindowState mTmpWindow2;
174 private WindowAnimator mTmpWindowAnimator;
175 private boolean mTmpRecoveringMemory;
176 private boolean mUpdateImeTarget;
177 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700178 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700179
Wale Ogunwale02319a62016-09-26 15:21:22 -0700180 // Mapping from a token IBinder to a WindowToken object on this display.
181 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
182
Craig Mautner59c00972012-07-30 12:10:24 -0700183 int mInitialDisplayWidth = 0;
184 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700185 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -0700186 int mBaseDisplayWidth = 0;
187 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700188 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700189 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700190 private final DisplayInfo mDisplayInfo = new DisplayInfo();
191 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700192 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700193 /**
194 * For default display it contains real metrics, empty for others.
195 * @see WindowManagerService#createWatermarkInTransaction()
196 */
197 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
198 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
199 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
200 /**
201 * Compat metrics computed based on {@link #mDisplayMetrics}.
202 * @see #updateDisplayAndOrientation(int)
203 */
204 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
205
206 /** The desired scaling factor for compatible apps. */
207 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700208
Andrii Kulian8ee72852017-03-10 10:36:45 -0800209 /**
210 * Current rotation of the display.
211 * Constants as per {@link android.view.Surface.Rotation}.
212 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700213 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800214 */
215 private int mRotation = 0;
216 /**
217 * Last applied orientation of the display.
218 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
219 *
220 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
221 */
222 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
223 /**
224 * Flag indicating that the application is receiving an orientation that has different metrics
225 * than it expected. E.g. Portrait instead of Landscape.
226 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700227 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800228 */
229 private boolean mAltOrientation = false;
230 /**
231 * Orientation forced by some window. If there is no visible window that specifies orientation
232 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
233 *
234 * @see NonAppWindowContainers#getOrientation()
235 */
236 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
237 /**
238 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
239 * occluded.
240 *
241 * @see NonAppWindowContainers#getOrientation()
242 */
243 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
244
Andrii Kulian06d07d62017-03-14 11:11:47 -0700245 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700246 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700247
Craig Mautner39834192012-09-02 07:47:24 -0700248 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700249 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700250 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700251 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800252 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700253
Craig Mautnerdc548482014-02-05 13:35:24 -0800254 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700255 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800256
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800257 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
258 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700259 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700260
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700261 /** Detect user tapping outside of current focused task bounds .*/
262 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700263
Craig Mautner6601b7b2013-04-29 10:29:11 -0700264 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700265 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700266
Craig Mautner6601b7b2013-04-29 10:29:11 -0700267 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800268 private final Rect mTmpRect = new Rect();
269 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700270 private final RectF mTmpRectF = new RectF();
271 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800272 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700273
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800274 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700275
Craig Mautner95da1082014-02-24 17:54:35 -0800276 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700277 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800278
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700279 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700280 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700281
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800282 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700283
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800284 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
285
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000286 private boolean mHaveBootMsg = false;
287 private boolean mHaveApp = false;
288 private boolean mHaveWallpaper = false;
289 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700290
291 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
292
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700293 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
294 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000295 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
296 new ApplySurfaceChangesTransactionState();
297 private final ScreenshotApplicationState mScreenshotApplicationState =
298 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700299
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700300 // True if this display is in the process of being removed. Used to determine if the removal of
301 // the display's direct children should be allowed.
302 private boolean mRemovingDisplay = false;
303
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700304 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800305 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700306 int mInputMethodAnimLayerAdjustment;
307
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800308 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
309 WindowStateAnimator winAnimator = w.mWinAnimator;
310 if (winAnimator.hasSurface()) {
311 final boolean wasAnimating = winAnimator.mWasAnimating;
312 final boolean nowAnimating = winAnimator.stepAnimationLocked(
313 mTmpWindowAnimator.mCurrentTime);
314 winAnimator.mWasAnimating = nowAnimating;
315 mTmpWindowAnimator.orAnimating(nowAnimating);
316
317 if (DEBUG_WALLPAPER) Slog.v(TAG,
318 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
319
320 if (wasAnimating && !winAnimator.mAnimating
321 && mWallpaperController.isWallpaperTarget(w)) {
322 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
323 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
324 if (DEBUG_LAYOUT_REPEATS) {
325 mService.mWindowPlacerLocked.debugLayoutRepeats(
326 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
327 }
328 }
329 }
330
331 final AppWindowToken atoken = w.mAppToken;
332 if (winAnimator.mDrawState == READY_TO_SHOW) {
333 if (atoken == null || atoken.allDrawn) {
334 if (w.performShowLocked()) {
335 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
336 if (DEBUG_LAYOUT_REPEATS) {
337 mService.mWindowPlacerLocked.debugLayoutRepeats(
338 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
339 }
340 }
341 }
342 }
343 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
344 if (appAnimator != null && appAnimator.thumbnail != null) {
345 if (appAnimator.thumbnailTransactionSeq
346 != mTmpWindowAnimator.mAnimTransactionSequence) {
347 appAnimator.thumbnailTransactionSeq =
348 mTmpWindowAnimator.mAnimTransactionSequence;
349 appAnimator.thumbnailLayer = 0;
350 }
351 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
352 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
353 }
354 }
355 };
356
357 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
358 final WindowStateAnimator winAnimator = w.mWinAnimator;
359 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
360 return;
361 }
362
363 final int flags = w.mAttrs.flags;
364
365 // If this window is animating, make a note that we have an animating window and take
366 // care of a request to run a detached wallpaper animation.
367 if (winAnimator.mAnimating) {
368 if (winAnimator.mAnimation != null) {
369 if ((flags & FLAG_SHOW_WALLPAPER) != 0
370 && winAnimator.mAnimation.getDetachWallpaper()) {
371 mTmpWindow = w;
372 }
373 final int color = winAnimator.mAnimation.getBackgroundColor();
374 if (color != 0) {
375 final TaskStack stack = w.getStack();
376 if (stack != null) {
377 stack.setAnimationBackground(winAnimator, color);
378 }
379 }
380 }
381 mTmpWindowAnimator.setAnimating(true);
382 }
383
384 // If this window's app token is running a detached wallpaper animation, make a note so
385 // we can ensure the wallpaper is displayed behind it.
386 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
387 if (appAnimator != null && appAnimator.animation != null
388 && appAnimator.animating) {
389 if ((flags & FLAG_SHOW_WALLPAPER) != 0
390 && appAnimator.animation.getDetachWallpaper()) {
391 mTmpWindow = w;
392 }
393
394 final int color = appAnimator.animation.getBackgroundColor();
395 if (color != 0) {
396 final TaskStack stack = w.getStack();
397 if (stack != null) {
398 stack.setAnimationBackground(winAnimator, color);
399 }
400 }
401 }
402 };
403
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800404 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
405 final int lostFocusUid = mTmpWindow.mOwnerUid;
406 final Handler handler = mService.mH;
407 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
408 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
409 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
410 w.mAttrs.hideTimeoutMilliseconds);
411 }
412 }
413 };
414
415 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
416 final AppWindowToken focusedApp = mService.mFocusedApp;
417 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
418 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
419
420 if (!w.canReceiveKeys()) {
421 return false;
422 }
423
424 final AppWindowToken wtoken = w.mAppToken;
425
426 // If this window's application has been removed, just skip it.
427 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
428 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
429 + (wtoken.removed ? "removed" : "sendingToBottom"));
430 return false;
431 }
432
433 if (focusedApp == null) {
434 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
435 + " using new focus @ " + w);
436 mTmpWindow = w;
437 return true;
438 }
439
440 if (!focusedApp.windowsAreFocusable()) {
441 // Current focused app windows aren't focusable...
442 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
443 + " focusable using new focus @ " + w);
444 mTmpWindow = w;
445 return true;
446 }
447
448 // Descend through all of the app tokens and find the first that either matches
449 // win.mAppToken (return win) or mFocusedApp (return null).
450 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
451 if (focusedApp.compareTo(wtoken) > 0) {
452 // App stack below focused app stack. No focus for you!!!
453 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
454 "findFocusedWindow: Reached focused app=" + focusedApp);
455 mTmpWindow = null;
456 return true;
457 }
458 }
459
460 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
461 mTmpWindow = w;
462 return true;
463 };
464
465 private final Consumer<WindowState> mPrepareWindowSurfaces =
466 w -> w.mWinAnimator.prepareSurfaceLocked(true);
467
468 private final Consumer<WindowState> mPerformLayout = w -> {
469 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
470 // wasting time and funky changes while a window is animating away.
471 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
472 || w.isGoneForLayoutLw();
473
474 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
475 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
476 + " mLayoutAttached=" + w.mLayoutAttached
477 + " screen changed=" + w.isConfigChanged());
478 final AppWindowToken atoken = w.mAppToken;
479 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
480 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
481 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
482 + " parentHidden=" + w.isParentWindowHidden());
483 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
484 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
485 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
486 + " parentHidden=" + w.isParentWindowHidden());
487 }
488
489 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
490 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
491 // since that means "perform layout as normal, just don't display").
492 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
493 || ((w.isConfigChanged() || w.setReportResizeHints())
494 && !w.isGoneForLayoutLw() &&
495 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
496 (w.mHasSurface && w.mAppToken != null &&
497 w.mAppToken.layoutConfigChanges)))) {
498 if (!w.mLayoutAttached) {
499 if (mTmpInitial) {
500 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
501 w.mContentChanged = false;
502 }
503 if (w.mAttrs.type == TYPE_DREAM) {
504 // Don't layout windows behind a dream, so that if it does stuff like hide
505 // the status bar we won't get a bad transition when it goes away.
506 mTmpWindow = w;
507 }
508 w.mLayoutNeeded = false;
509 w.prelayout();
510 mService.mPolicy.layoutWindowLw(w, null);
511 w.mLayoutSeq = mService.mLayoutSeq;
512
513 // Window frames may have changed. Update dim layer with the new bounds.
514 final Task task = w.getTask();
515 if (task != null) {
516 mDimLayerController.updateDimLayer(task);
517 }
518
519 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
520 + " mContainingFrame=" + w.mContainingFrame
521 + " mDisplayFrame=" + w.mDisplayFrame);
522 }
523 }
524 };
525
526 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
527 if (w.mLayoutAttached) {
528 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
529 + " mViewVisibility=" + w.mViewVisibility
530 + " mRelayoutCalled=" + w.mRelayoutCalled);
531 // If this view is GONE, then skip it -- keep the current frame, and let the caller
532 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
533 // windows, since that means "perform layout as normal, just don't display").
534 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
535 return;
536 }
537 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
538 || w.mLayoutNeeded) {
539 if (mTmpInitial) {
540 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
541 w.mContentChanged = false;
542 }
543 w.mLayoutNeeded = false;
544 w.prelayout();
545 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
546 w.mLayoutSeq = mService.mLayoutSeq;
547 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
548 + " mContainingFrame=" + w.mContainingFrame
549 + " mDisplayFrame=" + w.mDisplayFrame);
550 }
551 } else if (w.mAttrs.type == TYPE_DREAM) {
552 // Don't layout windows behind a dream, so that if it does stuff like hide the
553 // status bar we won't get a bad transition when it goes away.
554 mTmpWindow = mTmpWindow2;
555 }
556 };
557
558 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
559 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
560 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
561 return w.canBeImeTarget();
562 };
563
564 private final Consumer<WindowState> mApplyPostLayoutPolicy =
565 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
566 mService.mInputMethodTarget);
567
568 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
569 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
570 final boolean obscuredChanged = w.mObscured !=
571 mTmpApplySurfaceChangesTransactionState.obscured;
572 final RootWindowContainer root = mService.mRoot;
573 // Only used if default window
574 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
575
576 // Update effect.
577 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
578 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
579 final boolean isDisplayed = w.isDisplayedLw();
580
581 if (isDisplayed && w.isObscuringDisplay()) {
582 // This window completely covers everything behind it, so we want to leave all
583 // of them as undimmed (for performance reasons).
584 root.mObscuringWindow = w;
585 mTmpApplySurfaceChangesTransactionState.obscured = true;
586 }
587
588 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
589 root.handleNotObscuredLocked(w,
590 mTmpApplySurfaceChangesTransactionState.obscured,
591 mTmpApplySurfaceChangesTransactionState.syswin);
592
593 if (w.mHasSurface && isDisplayed) {
594 final int type = w.mAttrs.type;
595 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
596 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
597 mTmpApplySurfaceChangesTransactionState.syswin = true;
598 }
599 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
600 && w.mAttrs.preferredRefreshRate != 0) {
601 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
602 = w.mAttrs.preferredRefreshRate;
603 }
604 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
605 && w.mAttrs.preferredDisplayModeId != 0) {
606 mTmpApplySurfaceChangesTransactionState.preferredModeId
607 = w.mAttrs.preferredDisplayModeId;
608 }
609 }
610 }
611
612 w.applyDimLayerIfNeeded();
613
614 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
615 && mWallpaperController.isWallpaperTarget(w)) {
616 // This is the wallpaper target and its obscured state changed... make sure the
617 // current wallpaper's visibility has been updated accordingly.
618 mWallpaperController.updateWallpaperVisibility();
619 }
620
621 w.handleWindowMovedIfNeeded();
622
623 final WindowStateAnimator winAnimator = w.mWinAnimator;
624
625 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
626 w.mContentChanged = false;
627
628 // Moved from updateWindowsAndWallpaperLocked().
629 if (w.mHasSurface) {
630 // Take care of the window being ready to display.
631 final boolean committed = winAnimator.commitFinishDrawingLocked();
632 if (isDefaultDisplay && committed) {
633 if (w.mAttrs.type == TYPE_DREAM) {
634 // HACK: When a dream is shown, it may at that point hide the lock screen.
635 // So we need to redo the layout to let the phone window manager make this
636 // happen.
637 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
638 if (DEBUG_LAYOUT_REPEATS) {
639 surfacePlacer.debugLayoutRepeats(
640 "dream and commitFinishDrawingLocked true",
641 pendingLayoutChanges);
642 }
643 }
644 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
645 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
646 "First draw done in potential wallpaper target " + w);
647 root.mWallpaperMayChange = true;
648 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
649 if (DEBUG_LAYOUT_REPEATS) {
650 surfacePlacer.debugLayoutRepeats(
651 "wallpaper and commitFinishDrawingLocked true",
652 pendingLayoutChanges);
653 }
654 }
655 }
656 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
657 // Updates the shown frame before we set up the surface. This is needed
658 // because the resizing could change the top-left position (in addition to
659 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
660 // position the surface.
661 //
662 // If an animation is being started, we can't call this method because the
663 // animation hasn't processed its initial transformation yet, but in general
664 // we do want to update the position if the window is animating.
665 winAnimator.computeShownFrameLocked();
666 }
667 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
668 }
669
670 final AppWindowToken atoken = w.mAppToken;
671 if (atoken != null) {
672 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
673 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
674 mTmpUpdateAllDrawn.add(atoken);
675 }
676 }
677
678 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
679 && w.isDisplayedLw()) {
680 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
681 }
682
683 w.updateResizingWindowIfNeeded();
684 };
685
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800686 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800687 * Create new {@link DisplayContent} instance, add itself to the root window container and
688 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700689 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800690 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700691 * @param layersController window layer controller used to assign layer to the windows on this
692 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700693 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
694 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700695 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700696 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700697 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800698
699 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
700 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
701 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
702 + " new=" + display);
703 }
704
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700705 mDisplay = display;
706 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700707 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700708 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700709 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700710 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700711 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700712 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700713 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800714 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700715 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800716 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700717
718 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700719 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700720 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700721 super.addChild(mAboveAppWindowsContainers, null);
722 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800723
724 // Add itself as a child to the root container.
725 mService.mRoot.addChild(this, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700726 }
727
728 int getDisplayId() {
729 return mDisplayId;
730 }
731
Wale Ogunwale02319a62016-09-26 15:21:22 -0700732 WindowToken getWindowToken(IBinder binder) {
733 return mTokenMap.get(binder);
734 }
735
736 AppWindowToken getAppWindowToken(IBinder binder) {
737 final WindowToken token = getWindowToken(binder);
738 if (token == null) {
739 return null;
740 }
741 return token.asAppWindowToken();
742 }
743
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800744 void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700745 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
746 if (dc != null) {
747 // We currently don't support adding a window token to the display if the display
748 // already has the binder mapped to another token. If there is a use case for supporting
749 // this moving forward we will either need to merge the WindowTokens some how or have
750 // the binder map to a list of window tokens.
751 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
752 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
753 }
754 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700755
756 if (token.asAppWindowToken() == null) {
757 // Add non-app token to container hierarchy on the display. App tokens are added through
758 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700759 switch (token.windowType) {
760 case TYPE_WALLPAPER:
761 mBelowAppWindowsContainers.addChild(token);
762 break;
763 case TYPE_INPUT_METHOD:
764 case TYPE_INPUT_METHOD_DIALOG:
765 mImeWindowsContainers.addChild(token);
766 break;
767 default:
768 mAboveAppWindowsContainers.addChild(token);
769 break;
770 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700771 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700772 }
773
774 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700775 final WindowToken token = mTokenMap.remove(binder);
776 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800777 token.setExiting();
778 }
779 return token;
780 }
781
782 /** Changes the display the input window token is housed on to this one. */
783 void reParentWindowToken(WindowToken token) {
784 final DisplayContent prevDc = token.getDisplayContent();
785 if (prevDc == this) {
786 return;
787 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800788 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
789 && token.asAppWindowToken() == null) {
790 // Removed the token from the map, but made sure it's not an app token before removing
791 // from parent.
792 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700793 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800794
795 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700796 }
797
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700798 void removeAppToken(IBinder binder) {
799 final WindowToken token = removeWindowToken(binder);
800 if (token == null) {
801 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
802 return;
803 }
804
805 final AppWindowToken appToken = token.asAppWindowToken();
806
807 if (appToken == null) {
808 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
809 return;
810 }
811
812 appToken.onRemovedFromDisplay();
813 }
814
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700815 Display getDisplay() {
816 return mDisplay;
817 }
818
Craig Mautner59c00972012-07-30 12:10:24 -0700819 DisplayInfo getDisplayInfo() {
820 return mDisplayInfo;
821 }
822
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700823 DisplayMetrics getDisplayMetrics() {
824 return mDisplayMetrics;
825 }
826
Andrii Kulian8ee72852017-03-10 10:36:45 -0800827 int getRotation() {
828 return mRotation;
829 }
830
831 void setRotation(int newRotation) {
832 mRotation = newRotation;
833 }
834
835 int getLastOrientation() {
836 return mLastOrientation;
837 }
838
839 void setLastOrientation(int orientation) {
840 mLastOrientation = orientation;
841 }
842
843 boolean getAltOrientation() {
844 return mAltOrientation;
845 }
846
847 void setAltOrientation(boolean altOrientation) {
848 mAltOrientation = altOrientation;
849 }
850
851 int getLastWindowForcedOrientation() {
852 return mLastWindowForcedOrientation;
853 }
854
Andrii Kulian06d07d62017-03-14 11:11:47 -0700855 /**
856 * Update rotation of the display.
857 *
858 * Returns true if the rotation has been changed. In this case YOU MUST CALL
859 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
860 */
861 boolean updateRotationUnchecked(boolean inTransaction) {
862 if (mService.mDeferredRotationPauseCount > 0) {
863 // Rotation updates have been paused temporarily. Defer the update until
864 // updates have been resumed.
865 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
866 return false;
867 }
868
869 ScreenRotationAnimation screenRotationAnimation =
870 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
871 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
872 // Rotation updates cannot be performed while the previous rotation change
873 // animation is still in progress. Skip this update. We will try updating
874 // again after the animation is finished and the display is unfrozen.
875 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
876 return false;
877 }
878 if (mService.mDisplayFrozen) {
879 // Even if the screen rotation animation has finished (e.g. isAnimating
880 // returns false), there is still some time where we haven't yet unfrozen
881 // the display. We also need to abort rotation here.
882 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
883 "Deferring rotation, still finishing previous rotation");
884 return false;
885 }
886
887 if (!mService.mDisplayEnabled) {
888 // No point choosing a rotation if the display is not enabled.
889 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
890 return false;
891 }
892
893 final int oldRotation = mRotation;
894 final int lastOrientation = mLastOrientation;
895 final boolean oldAltOrientation = mAltOrientation;
896 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700897 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
898 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700899
Robert Carr897215d2017-03-29 12:25:50 -0700900 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700901 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
902 if (seamlessRotated != null) {
903 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
904 // to complete (that is, waiting for windows to redraw). It's tempting to check
905 // w.mSeamlessRotationCount but that could be incorrect in the case of
906 // window-removal.
907 return false;
908 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700909 }
910
911 // TODO: Implement forced rotation changes.
912 // Set mAltOrientation to indicate that the application is receiving
913 // an orientation that has different metrics than it expected.
914 // eg. Portrait instead of Landscape.
915
916 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
917 lastOrientation, rotation);
918
919 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
920 + ", got rotation " + rotation + " which has "
921 + (altOrientation ? "incompatible" : "compatible") + " metrics");
922
923 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
924 // No change.
925 return false;
926 }
927
928 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
929 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
930 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
931
932 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
933 mService.mWaitingForConfig = true;
934 }
935
936 mRotation = rotation;
937 mAltOrientation = altOrientation;
938 if (isDefaultDisplay) {
939 mService.mPolicy.setRotationLw(rotation);
940 }
941
942 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
943 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
944 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
945 WINDOW_FREEZE_TIMEOUT_DURATION);
946
947 setLayoutNeeded();
948 final int[] anim = new int[2];
949 if (isDimming()) {
950 anim[0] = anim[1] = 0;
951 } else {
952 mService.mPolicy.selectRotationAnimationLw(anim);
953 }
954
955 if (!rotateSeamlessly) {
956 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1]);
957 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
958 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
959 mDisplayId);
960 } else {
961 // The screen rotation animation uses a screenshot to freeze the screen
962 // while windows resize underneath.
963 // When we are rotating seamlessly, we allow the elements to transition
964 // to their rotated state independently and without a freeze required.
965 screenRotationAnimation = null;
966
967 // We have to reset this in case a window was removed before it
968 // finished seamless rotation.
969 mService.mSeamlessRotationCount = 0;
970 }
971
972 // We need to update our screen size information to match the new rotation. If the rotation
973 // has actually changed then this method will return true and, according to the comment at
974 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
975 // By updating the Display info here it will be available to
976 // #computeScreenConfiguration() later.
977 updateDisplayAndOrientation(getConfiguration().uiMode);
978
979 if (!inTransaction) {
980 if (SHOW_TRANSACTIONS) {
981 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
982 }
983 mService.openSurfaceTransaction();
984 }
985 try {
986 // NOTE: We disable the rotation in the emulator because
987 // it doesn't support hardware OpenGL emulation yet.
988 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
989 && screenRotationAnimation.hasScreenshot()) {
990 if (screenRotationAnimation.setRotationInTransaction(
991 rotation, mService.mFxSession,
992 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
993 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
994 mService.scheduleAnimationLocked();
995 }
996 }
997
998 if (rotateSeamlessly) {
999 forAllWindows(w -> {
1000 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1001 }, true /* traverseTopToBottom */);
1002 }
1003
1004 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1005 } finally {
1006 if (!inTransaction) {
1007 mService.closeSurfaceTransaction();
1008 if (SHOW_LIGHT_TRANSACTIONS) {
1009 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1010 }
1011 }
1012 }
1013
1014 forAllWindows(w -> {
1015 // Discard surface after orientation change, these can't be reused.
1016 if (w.mAppToken != null) {
1017 w.mAppToken.destroySavedSurfaces();
1018 }
1019 if (w.mHasSurface && !rotateSeamlessly) {
1020 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
1021 w.mOrientationChanging = true;
1022 mService.mRoot.mOrientationChangeComplete = false;
1023 w.mLastFreezeDuration = 0;
1024 }
1025 w.mReportOrientationChanged = true;
1026 }, true /* traverseTopToBottom */);
1027
1028 if (rotateSeamlessly) {
1029 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1030 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1031 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1032 }
1033
1034 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1035 final WindowManagerService.RotationWatcher rotationWatcher
1036 = mService.mRotationWatchers.get(i);
1037 if (rotationWatcher.mDisplayId == mDisplayId) {
1038 try {
1039 rotationWatcher.mWatcher.onRotationChanged(rotation);
1040 } catch (RemoteException e) {
1041 // Ignore
1042 }
1043 }
1044 }
1045
1046 // TODO (multi-display): Magnification is supported only for the default display.
1047 // Announce rotation only if we will not animate as we already have the
1048 // windows in final state. Otherwise, we make this call at the rotation end.
1049 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1050 && isDefaultDisplay) {
1051 mService.mAccessibilityController.onRotationChangedLocked(this);
1052 }
1053
1054 return true;
1055 }
1056
1057 /**
1058 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1059 * changed.
1060 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1061 */
1062 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1063 // Use the effective "visual" dimensions based on current rotation
1064 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1065 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1066 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1067 int dw = realdw;
1068 int dh = realdh;
1069
1070 if (mAltOrientation) {
1071 if (realdw > realdh) {
1072 // Turn landscape into portrait.
1073 int maxw = (int)(realdh/1.3f);
1074 if (maxw < realdw) {
1075 dw = maxw;
1076 }
1077 } else {
1078 // Turn portrait into landscape.
1079 int maxh = (int)(realdw/1.3f);
1080 if (maxh < realdh) {
1081 dh = maxh;
1082 }
1083 }
1084 }
1085
1086 // Update application display metrics.
1087 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1088 mDisplayId);
1089 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1090 mDisplayId);
1091 mDisplayInfo.rotation = mRotation;
1092 mDisplayInfo.logicalWidth = dw;
1093 mDisplayInfo.logicalHeight = dh;
1094 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1095 mDisplayInfo.appWidth = appWidth;
1096 mDisplayInfo.appHeight = appHeight;
1097 if (isDefaultDisplay) {
1098 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1099 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1100 }
1101 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1102 if (mDisplayScalingDisabled) {
1103 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1104 } else {
1105 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1106 }
1107
1108 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1109 mDisplayInfo);
1110
1111 mBaseDisplayRect.set(0, 0, dw, dh);
1112
1113 if (isDefaultDisplay) {
1114 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1115 mCompatDisplayMetrics);
1116 }
1117 return mDisplayInfo;
1118 }
1119
1120 /**
1121 * Compute display configuration based on display properties and policy settings.
1122 * Do not call if mDisplayReady == false.
1123 */
1124 void computeScreenConfiguration(Configuration config) {
1125 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1126
1127 final int dw = displayInfo.logicalWidth;
1128 final int dh = displayInfo.logicalHeight;
1129 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1130 Configuration.ORIENTATION_LANDSCAPE;
1131 config.screenWidthDp =
1132 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1133 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1134 config.screenHeightDp =
1135 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1136 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001137
1138 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1139 final int leftInset = mTmpRect.left;
1140 final int topInset = mTmpRect.top;
1141 // appBounds at the root level should mirror the app screen size.
1142 config.setAppBounds(leftInset /*left*/, topInset /*top*/, leftInset + displayInfo.appWidth /*right*/,
1143 topInset + displayInfo.appHeight /*bottom*/);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001144 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1145 || displayInfo.rotation == Surface.ROTATION_270);
1146
1147 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1148 mDisplayMetrics.density, config);
1149
1150 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1151 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1152 ? Configuration.SCREENLAYOUT_ROUND_YES
1153 : Configuration.SCREENLAYOUT_ROUND_NO);
1154
1155 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1156 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1157 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1158 dh, mDisplayId);
1159 config.densityDpi = displayInfo.logicalDensityDpi;
1160
1161 config.colorMode =
1162 (displayInfo.isHdr()
1163 ? Configuration.COLOR_MODE_HDR_YES
1164 : Configuration.COLOR_MODE_HDR_NO)
1165 | (displayInfo.isWideColorGamut()
1166 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1167 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1168
1169 // Update the configuration based on available input devices, lid switch,
1170 // and platform configuration.
1171 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1172 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1173 config.navigation = Configuration.NAVIGATION_NONAV;
1174
1175 int keyboardPresence = 0;
1176 int navigationPresence = 0;
1177 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1178 final int len = devices != null ? devices.length : 0;
1179 for (int i = 0; i < len; i++) {
1180 InputDevice device = devices[i];
1181 if (!device.isVirtual()) {
1182 final int sources = device.getSources();
1183 final int presenceFlag = device.isExternal() ?
1184 WindowManagerPolicy.PRESENCE_EXTERNAL :
1185 WindowManagerPolicy.PRESENCE_INTERNAL;
1186
1187 // TODO(multi-display): Configure on per-display basis.
1188 if (mService.mIsTouchDevice) {
1189 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1190 InputDevice.SOURCE_TOUCHSCREEN) {
1191 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1192 }
1193 } else {
1194 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1195 }
1196
1197 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1198 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1199 navigationPresence |= presenceFlag;
1200 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1201 && config.navigation == Configuration.NAVIGATION_NONAV) {
1202 config.navigation = Configuration.NAVIGATION_DPAD;
1203 navigationPresence |= presenceFlag;
1204 }
1205
1206 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1207 config.keyboard = Configuration.KEYBOARD_QWERTY;
1208 keyboardPresence |= presenceFlag;
1209 }
1210 }
1211 }
1212
1213 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1214 config.navigation = Configuration.NAVIGATION_DPAD;
1215 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1216 }
1217
1218 // Determine whether a hard keyboard is available and enabled.
1219 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1220 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1221 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1222 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1223 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1224 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1225 }
1226
1227 // Let the policy update hidden states.
1228 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1229 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1230 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1231 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1232 }
1233
1234 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1235 int displayId) {
1236 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1237 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1238 final int unrotDw, unrotDh;
1239 if (rotated) {
1240 unrotDw = dh;
1241 unrotDh = dw;
1242 } else {
1243 unrotDw = dw;
1244 unrotDh = dh;
1245 }
1246 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1247 displayId);
1248 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1249 displayId);
1250 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1251 displayId);
1252 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1253 displayId);
1254 return sw;
1255 }
1256
1257 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1258 DisplayMetrics dm, int dw, int dh, int displayId) {
1259 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1260 displayId);
1261 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1262 uiMode, displayId);
1263 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1264 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1265 if (curSize == 0 || size < curSize) {
1266 curSize = size;
1267 }
1268 return curSize;
1269 }
1270
1271 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1272 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1273
1274 // We need to determine the smallest width that will occur under normal
1275 // operation. To this, start with the base screen size and compute the
1276 // width under the different possible rotations. We need to un-rotate
1277 // the current screen dimensions before doing this.
1278 int unrotDw, unrotDh;
1279 if (rotated) {
1280 unrotDw = dh;
1281 unrotDh = dw;
1282 } else {
1283 unrotDw = dw;
1284 unrotDh = dh;
1285 }
1286 displayInfo.smallestNominalAppWidth = 1<<30;
1287 displayInfo.smallestNominalAppHeight = 1<<30;
1288 displayInfo.largestNominalAppWidth = 0;
1289 displayInfo.largestNominalAppHeight = 0;
1290 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1291 unrotDh);
1292 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1293 unrotDw);
1294 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1295 unrotDh);
1296 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1297 unrotDw);
1298 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1299 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1300 displayId);
1301 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1302 displayId);
1303 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1304 displayId);
1305 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1306 displayId);
1307 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1308 outConfig.screenLayout = sl;
1309 }
1310
1311 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1312 int uiMode, int displayId) {
1313 // Get the app screen size at this rotation.
1314 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1315 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1316
1317 // Compute the screen layout size class for this rotation.
1318 int longSize = w;
1319 int shortSize = h;
1320 if (longSize < shortSize) {
1321 int tmp = longSize;
1322 longSize = shortSize;
1323 shortSize = tmp;
1324 }
1325 longSize = (int)(longSize/density);
1326 shortSize = (int)(shortSize/density);
1327 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1328 }
1329
1330 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1331 int uiMode, int dw, int dh) {
1332 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1333 displayId);
1334 if (width < displayInfo.smallestNominalAppWidth) {
1335 displayInfo.smallestNominalAppWidth = width;
1336 }
1337 if (width > displayInfo.largestNominalAppWidth) {
1338 displayInfo.largestNominalAppWidth = width;
1339 }
1340 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1341 displayId);
1342 if (height < displayInfo.smallestNominalAppHeight) {
1343 displayInfo.smallestNominalAppHeight = height;
1344 }
1345 if (height > displayInfo.largestNominalAppHeight) {
1346 displayInfo.largestNominalAppHeight = height;
1347 }
1348 }
1349
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001350 DockedStackDividerController getDockedDividerController() {
1351 return mDividerControllerLocked;
1352 }
1353
Winson Chung655332c2016-10-31 13:14:28 -07001354 PinnedStackController getPinnedStackController() {
1355 return mPinnedStackControllerLocked;
1356 }
1357
Jeff Browna506a6e2013-06-04 00:02:38 -07001358 /**
1359 * Returns true if the specified UID has access to this display.
1360 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001361 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001362 return mDisplay.hasAccess(uid);
1363 }
1364
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001365 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001366 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001367 }
1368
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001369 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001370 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001371 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001372 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001373 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001374 }
1375
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001376 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001377 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1378 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001379 if (stack.mStackId == stackId) {
1380 return stack;
1381 }
1382 }
1383 return null;
1384 }
1385
Andrii Kulian441e4492016-09-29 15:25:00 -07001386 @Override
1387 void onConfigurationChanged(Configuration newParentConfig) {
1388 super.onConfigurationChanged(newParentConfig);
1389
Andrii Kulian3a507b52016-09-19 18:14:12 -07001390 // The display size information is heavily dependent on the resources in the current
1391 // configuration, so we need to reconfigure it every time the configuration changes.
1392 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1393 mService.reconfigureDisplayLocked(this);
1394
1395 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001396 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001397 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001398
Andrii Kulian441e4492016-09-29 15:25:00 -07001399 /**
1400 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1401 * bounds were updated.
1402 */
1403 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001404 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1405 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001406 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001407 changedStackList.add(stack.mStackId);
1408 }
1409 }
1410 }
1411
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001412 @Override
1413 boolean fillsParent() {
1414 return true;
1415 }
1416
1417 @Override
1418 boolean isVisible() {
1419 return true;
1420 }
1421
1422 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001423 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001424 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001425 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001426 }
1427
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001428 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001429 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1430 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1431 // target, or here in order if there isn't an IME target.
1432 if (traverseTopToBottom) {
1433 for (int i = mChildren.size() - 1; i >= 0; --i) {
1434 final DisplayChildWindowContainer child = mChildren.get(i);
1435 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1436 // In this case the Ime windows will be processed above their target so we skip
1437 // here.
1438 continue;
1439 }
1440 if (child.forAllWindows(callback, traverseTopToBottom)) {
1441 return true;
1442 }
1443 }
1444 } else {
1445 final int count = mChildren.size();
1446 for (int i = 0; i < count; i++) {
1447 final DisplayChildWindowContainer child = mChildren.get(i);
1448 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1449 // In this case the Ime windows will be processed above their target so we skip
1450 // here.
1451 continue;
1452 }
1453 if (child.forAllWindows(callback, traverseTopToBottom)) {
1454 return true;
1455 }
1456 }
1457 }
1458 return false;
1459 }
1460
1461 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1462 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1463 }
1464
1465 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001466 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001467 final WindowManagerPolicy policy = mService.mPolicy;
1468
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001469 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001470 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001471 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001472 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001473 // If the display is frozen, some activities may be in the middle of restarting, and
1474 // thus have removed their old window. If the window has the flag to hide the lock
1475 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1476 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001477 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001478 } else if (policy.isKeyguardLocked()) {
1479 // Use the last orientation the while the display is frozen with the keyguard
1480 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1481 // window. We don't want to check the show when locked window directly though as
1482 // things aren't stable while the display is frozen, for example the window could be
1483 // momentarily unavailable due to activity relaunch.
1484 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001485 + "return " + mLastOrientation);
1486 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001487 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001488 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001489 final int orientation = mAboveAppWindowsContainers.getOrientation();
1490 if (orientation != SCREEN_ORIENTATION_UNSET) {
1491 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001492 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001493 }
1494
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001495 // Top system windows are not requesting an orientation. Start searching from apps.
1496 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001497 }
1498
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001499 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -07001500 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001501 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001502 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1503 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001504 }
Craig Mautner722285e2012-09-07 13:55:58 -07001505 }
1506
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001507 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001508 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1509 if (displayManagerInternal != null) {
1510 // Bootstrap the default logical display from the display manager.
1511 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1512 if (newDisplayInfo != null) {
1513 mDisplayInfo.copyFrom(newDisplayInfo);
1514 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001515 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001516
Andrii Kulianb6c5c0d2017-03-22 21:50:24 +00001517 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1518 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1519 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
1520 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001521 }
1522
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001523 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001524 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001525 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001526 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001527 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1528 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001529 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001530 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001531 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001532 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001533 out.set(left, top, left + width, top + height);
1534 }
1535
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001536 private void getLogicalDisplayRect(Rect out, int orientation) {
1537 getLogicalDisplayRect(out);
1538
1539 // Rotate the Rect if needed.
1540 final int currentRotation = mDisplayInfo.rotation;
1541 final int rotationDelta = deltaRotation(currentRotation, orientation);
1542 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1543 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1544 mTmpRectF.set(out);
1545 mTmpMatrix.mapRect(mTmpRectF);
1546 mTmpRectF.round(out);
1547 }
1548 }
1549
Bryce Lee27cec322017-03-21 09:41:37 -07001550 /** Sets the maximum width the screen resolution can be */
1551 void setMaxUiWidth(int width) {
1552 if (DEBUG_DISPLAY) {
1553 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1554 }
1555
1556 mMaxUiWidth = width;
1557
1558 // Update existing metrics.
1559 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1560 }
1561
1562 /** Update base (override) display metrics. */
1563 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1564 mBaseDisplayWidth = baseWidth;
1565 mBaseDisplayHeight = baseHeight;
1566 mBaseDisplayDensity = baseDensity;
1567
1568 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1569 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1570 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1571 mBaseDisplayWidth = mMaxUiWidth;
1572
1573 if (DEBUG_DISPLAY) {
1574 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1575 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1576 + " on display:" + getDisplayId());
1577 }
1578 }
1579
1580 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1581 }
1582
Chong Zhangf66db432016-01-13 10:39:51 -08001583 void getContentRect(Rect out) {
1584 out.set(mContentRect);
1585 }
1586
Wale Ogunwale1666e312016-12-16 11:27:18 -08001587 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1588 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1589 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001590
Wale Ogunwale1666e312016-12-16 11:27:18 -08001591 TaskStack stack = getStackById(stackId);
1592 if (stack != null) {
1593 // It's already attached to the display...clear mDeferRemoval and move stack to
1594 // appropriate z-order on display as needed.
1595 stack.mDeferRemoval = false;
1596 // We're not moving the display to front when we're adding stacks, only when
1597 // requested to change the position of stack explicitly.
1598 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1599 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001600 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001601 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001602 mTaskStackContainers.addStackToDisplay(stack, onTop);
1603 }
1604
Wale Ogunwale1666e312016-12-16 11:27:18 -08001605 if (stackId == DOCKED_STACK_ID) {
1606 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001607 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001608 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001609 }
1610
Wale Ogunwale1666e312016-12-16 11:27:18 -08001611 void moveStackToDisplay(TaskStack stack) {
1612 final DisplayContent prevDc = stack.getDisplayContent();
1613 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001614 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1615 + " which is not currently attached to any display");
1616 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001617 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001618 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1619 + " to its current displayId=" + mDisplayId);
1620 }
1621
Wale Ogunwale1666e312016-12-16 11:27:18 -08001622 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
1623 mTaskStackContainers.addStackToDisplay(stack, true /* onTop */);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001624 }
1625
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001626 @Override
1627 protected void addChild(DisplayChildWindowContainer child,
1628 Comparator<DisplayChildWindowContainer> comparator) {
1629 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1630 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001631
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001632 @Override
1633 protected void addChild(DisplayChildWindowContainer child, int index) {
1634 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1635 }
1636
1637 @Override
1638 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001639 // Only allow removal of direct children from this display if the display is in the process
1640 // of been removed.
1641 if (mRemovingDisplay) {
1642 super.removeChild(child);
1643 return;
1644 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001645 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001646 }
1647
Andrii Kuliand2765632016-12-12 22:26:34 -08001648 @Override
1649 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1650 // Children of the display are statically ordered, so the real intention here is to perform
1651 // the operation on the display and not the static direct children.
1652 getParent().positionChildAt(position, this, includingParents);
1653 }
1654
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001655 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001656 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1657 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001658 final int taskId = stack.taskIdFromPoint(x, y);
1659 if (taskId != -1) {
1660 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001661 }
1662 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001663 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001664 }
1665
Chong Zhang8e89b312015-09-09 15:09:30 -07001666 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001667 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001668 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001669 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001670 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001671 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001672 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001673 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1674 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001675 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001676 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001677 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001678
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001679 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1680 if (mTmpTaskForResizePointSearchResult.searchDone) {
1681 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001682 }
1683 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001684 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001685 }
1686
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001687 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001688 // The provided task is the task on this display with focus, so if WindowManagerService's
1689 // focused app is not on this display, focusedTask will be null.
1690 if (focusedTask == null) {
1691 mTouchExcludeRegion.setEmpty();
1692 } else {
1693 mTouchExcludeRegion.set(mBaseDisplayRect);
1694 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1695 mTmpRect2.setEmpty();
1696 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1697 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1698 stack.setTouchExcludeRegion(
1699 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1700 }
1701 // If we removed the focused task above, add it back and only leave its
1702 // outside touch area in the exclusion. TapDectector is not interested in
1703 // any touch inside the focused task itself.
1704 if (!mTmpRect2.isEmpty()) {
1705 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1706 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001707 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001708 final WindowState inputMethod = mService.mInputMethodWindow;
1709 if (inputMethod != null && inputMethod.isVisibleLw()) {
1710 // If the input method is visible and the user is typing, we don't want these touch
1711 // events to be intercepted and used to change focus. This would likely cause a
1712 // disappearance of the input method.
1713 inputMethod.getTouchableRegion(mTmpRegion);
1714 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1715 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001716 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1717 WindowState win = mTapExcludedWindows.get(i);
1718 win.getTouchableRegion(mTmpRegion);
1719 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1720 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001721 // TODO(multi-display): Support docked stacks on secondary displays.
1722 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001723 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001724 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001725 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1726 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001727 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001728 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001729 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001730 }
1731
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001732 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001733 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001734 super.switchUser();
1735 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001736 }
1737
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001738 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001739 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1740 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001741 }
1742 }
1743
1744 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001745 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001746 }
1747
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001748 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001749 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001750 }
1751
1752 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001753 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001754 }
1755
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001756 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001757 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001758 }
1759
Wale Ogunwale10124582016-09-15 20:25:50 -07001760 @Override
1761 void removeIfPossible() {
1762 if (isAnimating()) {
1763 mDeferredRemoval = true;
1764 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001765 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001766 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001767 }
1768
Wale Ogunwale10124582016-09-15 20:25:50 -07001769 @Override
1770 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001771 mRemovingDisplay = true;
1772 try {
1773 super.removeImmediately();
1774 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1775 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001776 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001777 mService.unregisterPointerEventListener(mTapDetector);
1778 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1779 }
1780 } finally {
1781 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001782 }
Craig Mautner95da1082014-02-24 17:54:35 -08001783 }
1784
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001785 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001786 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001787 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001788 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1789
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001790 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001791 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001792 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001793 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001794 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001795 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001796 }
1797
Wale Ogunwale10124582016-09-15 20:25:50 -07001798 boolean animateForIme(float interpolatedValue, float animationTarget,
1799 float dividerAnimationTarget) {
1800 boolean updated = false;
1801
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001802 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1803 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001804 if (stack == null || !stack.isAdjustedForIme()) {
1805 continue;
1806 }
1807
1808 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1809 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1810 updated = true;
1811 } else {
1812 mDividerControllerLocked.mLastAnimationProgress =
1813 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1814 mDividerControllerLocked.mLastDividerProgress =
1815 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1816 updated |= stack.updateAdjustForIme(
1817 mDividerControllerLocked.mLastAnimationProgress,
1818 mDividerControllerLocked.mLastDividerProgress,
1819 false /* force */);
1820 }
1821 if (interpolatedValue >= 1f) {
1822 stack.endImeAdjustAnimation();
1823 }
1824 }
1825
1826 return updated;
1827 }
1828
1829 boolean clearImeAdjustAnimation() {
1830 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001831 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1832 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001833 if (stack != null && stack.isAdjustedForIme()) {
1834 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1835 changed = true;
1836 }
1837 }
1838 return changed;
1839 }
1840
1841 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001842 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1843 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001844 if (stack.isVisible() && stack.isAdjustedForIme()) {
1845 stack.beginImeAdjustAnimation();
1846 }
1847 }
1848 }
1849
1850 void adjustForImeIfNeeded() {
1851 final WindowState imeWin = mService.mInputMethodWindow;
1852 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1853 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001854 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001855 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1856 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1857 imeTargetStack.getDockSide() : DOCKED_INVALID;
1858 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1859 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1860 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1861 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1862 final boolean imeHeightChanged = imeVisible &&
1863 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1864
1865 // The divider could be adjusted for IME position, or be thinner than usual,
1866 // or both. There are three possible cases:
1867 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1868 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1869 // - If IME is not visible, divider is not moved and is normal width.
1870
1871 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001872 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1873 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001874 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
1875 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
1876 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1877 } else {
1878 stack.resetAdjustedForIme(false);
1879 }
1880 }
1881 mDividerControllerLocked.setAdjustedForIme(
1882 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1883 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001884 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1885 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001886 stack.resetAdjustedForIme(!dockVisible);
1887 }
1888 mDividerControllerLocked.setAdjustedForIme(
1889 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
1890 }
Winson Chung655332c2016-10-31 13:14:28 -07001891 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07001892 }
1893
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001894 void setInputMethodAnimLayerAdjustment(int adj) {
1895 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
1896 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08001897 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001898 }
1899
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001900 /**
1901 * If a window that has an animation specifying a colored background and the current wallpaper
1902 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
1903 * suddenly disappear.
1904 */
1905 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001906 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
1907 w -> w.mIsWallpaper && w.isVisibleNow());
1908
1909 if (visibleWallpaper != null) {
1910 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001911 }
1912 return winAnimator.mAnimLayer;
1913 }
1914
Wale Ogunwale10124582016-09-15 20:25:50 -07001915 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001916 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1917 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07001918 stack.prepareFreezingTaskBounds();
1919 }
1920 }
1921
Wale Ogunwale94744212015-09-21 19:01:47 -07001922 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001923 getLogicalDisplayRect(mTmpRect, newRotation);
1924
1925 // Compute a transform matrix to undo the coordinate space transformation,
1926 // and present the window at the same physical position it previously occupied.
1927 final int deltaRotation = deltaRotation(newRotation, oldRotation);
1928 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
1929
1930 mTmpRectF.set(bounds);
1931 mTmpMatrix.mapRect(mTmpRectF);
1932 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07001933 }
1934
Wale Ogunwale4a02d812015-02-12 23:01:38 -08001935 static int deltaRotation(int oldRotation, int newRotation) {
1936 int delta = newRotation - oldRotation;
1937 if (delta < 0) delta += 4;
1938 return delta;
1939 }
1940
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001941 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001942 Matrix outMatrix) {
1943 // For rotations without Z-ordering we don't need the target rectangle's position.
1944 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
1945 displayHeight, outMatrix);
1946 }
1947
1948 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
1949 float displayWidth, float displayHeight, Matrix outMatrix) {
1950 switch (rotation) {
1951 case ROTATION_0:
1952 outMatrix.reset();
1953 break;
1954 case ROTATION_270:
1955 outMatrix.setRotate(270, 0, 0);
1956 outMatrix.postTranslate(0, displayHeight);
1957 outMatrix.postTranslate(rectTop, 0);
1958 break;
1959 case ROTATION_180:
1960 outMatrix.reset();
1961 break;
1962 case ROTATION_90:
1963 outMatrix.setRotate(90, 0, 0);
1964 outMatrix.postTranslate(displayWidth, 0);
1965 outMatrix.postTranslate(-rectTop, rectLeft);
1966 break;
1967 }
1968 }
1969
Craig Mautnera91f9e22012-09-14 16:22:08 -07001970 public void dump(String prefix, PrintWriter pw) {
1971 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
1972 final String subPrefix = " " + prefix;
1973 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
1974 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
1975 pw.print("dpi");
1976 if (mInitialDisplayWidth != mBaseDisplayWidth
1977 || mInitialDisplayHeight != mBaseDisplayHeight
1978 || mInitialDisplayDensity != mBaseDisplayDensity) {
1979 pw.print(" base=");
1980 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
1981 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
1982 }
Jeff Brownd46747a2015-04-15 19:02:36 -07001983 if (mDisplayScalingDisabled) {
1984 pw.println(" noscale");
1985 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07001986 pw.print(" cur=");
1987 pw.print(mDisplayInfo.logicalWidth);
1988 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
1989 pw.print(" app=");
1990 pw.print(mDisplayInfo.appWidth);
1991 pw.print("x"); pw.print(mDisplayInfo.appHeight);
1992 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
1993 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
1994 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
1995 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001996 pw.println(subPrefix + "deferred=" + mDeferredRemoval
1997 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001998
Craig Mautnerdc548482014-02-05 13:35:24 -08001999 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002000 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002001 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2002 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002003 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002004 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002005
Craig Mautnerdc548482014-02-05 13:35:24 -08002006 pw.println();
2007 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002008 pw.println();
2009 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002010 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002011 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002012 pw.print(" Exiting #"); pw.print(i);
2013 pw.print(' '); pw.print(token);
2014 pw.println(':');
2015 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002016 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002017 }
Craig Mautner59c00972012-07-30 12:10:24 -07002018 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002019 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002020 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002021 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002022 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002023 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002024
2025 if (mInputMethodAnimLayerAdjustment != 0) {
2026 pw.println(subPrefix
2027 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2028 }
Craig Mautner59c00972012-07-30 12:10:24 -07002029 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002030
2031 @Override
2032 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002033 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002034 }
2035
2036 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002037 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002038 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002039
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002040 /** Checks if stack with provided id is visible on this display. */
2041 boolean isStackVisible(int stackId) {
2042 final TaskStack stack = getStackById(stackId);
2043 return (stack != null && stack.isVisible());
2044 }
2045
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002046 /**
2047 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2048 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002049 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002050 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002051 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002052 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002053
2054 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002055 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002056 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002057 */
Jorim Jaggife762342016-10-13 14:33:27 +02002058 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002059 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002060 }
2061
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002062 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002063 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002064 final int x = (int) xf;
2065 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002066 final WindowState touchedWin = getWindow(w -> {
2067 final int flags = w.mAttrs.flags;
2068 if (!w.isVisibleLw()) {
2069 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002070 }
2071 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002072 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002073 }
2074
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002075 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002076 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002077 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002078 }
2079
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002080 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002081
2082 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002083 return mTmpRegion.contains(x, y) || touchFlags == 0;
2084 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002085
2086 return touchedWin;
2087 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002088
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002089 boolean canAddToastWindowForUid(int uid) {
2090 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002091 // Also if the app is focused adding more than one toast at
2092 // a time for better backwards compatibility.
2093 final WindowState focusedWindowForUid = getWindow(w ->
2094 w.mOwnerUid == uid && w.isFocused());
2095 if (focusedWindowForUid != null) {
2096 return true;
2097 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002098 final WindowState win = getWindow(w ->
2099 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2100 && !w.mWindowRemovalAllowed);
2101 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002102 }
2103
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002104 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002105 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2106 return;
2107 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002108
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002109 // Used to communicate the old focus to the callback method.
2110 mTmpWindow = oldFocus;
2111
2112 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002113 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002114
2115 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002116 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002117
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002118 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002119
2120 if (mTmpWindow == null) {
2121 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2122 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002123 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002124 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002125 }
2126
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002127 /** Updates the layer assignment of windows on this display. */
2128 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002129 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002130 if (setLayoutNeeded) {
2131 setLayoutNeeded();
2132 }
2133 }
2134
Wale Ogunwale1666e312016-12-16 11:27:18 -08002135 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2136 // moving containers or resizing them. Need to investigate the best way to have it automatically
2137 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002138 void layoutAndAssignWindowLayersIfNeeded() {
2139 mService.mWindowsChanged = true;
2140 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002141
2142 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2143 false /*updateInputWindows*/)) {
2144 assignWindowLayers(false /* setLayoutNeeded */);
2145 }
2146
2147 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2148 mService.mWindowPlacerLocked.performSurfacePlacement();
2149 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2150 }
2151
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002152 /** Returns true if a leaked surface was destroyed */
2153 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002154 // Used to indicate that a surface was leaked.
2155 mTmpWindow = null;
2156 forAllWindows(w -> {
2157 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002158 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002159 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002160 }
2161 if (!mService.mSessions.contains(wsa.mSession)) {
2162 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002163 + w + " surface=" + wsa.mSurfaceController
2164 + " token=" + w.mToken
2165 + " pid=" + w.mSession.mPid
2166 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002167 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002168 mService.mForceRemoves.add(w);
2169 mTmpWindow = w;
2170 } else if (w.mAppToken != null && w.mAppToken.clientHidden) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002171 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002172 + w + " surface=" + wsa.mSurfaceController
2173 + " token=" + w.mAppToken
2174 + " saved=" + w.hasSavedSurface());
2175 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002176 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002177 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002178 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002179 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002180
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002181 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002182 }
2183
2184 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002185 * Determine and return the window that should be the IME target.
2186 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2187 * @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 +00002188 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002189 WindowState computeImeTarget(boolean updateImeTarget) {
2190 if (mService.mInputMethodWindow == null) {
2191 // There isn't an IME so there shouldn't be a target...That was easy!
2192 if (updateImeTarget) {
2193 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2194 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2195 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2196 }
2197 return null;
2198 }
2199
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002200 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2201 // same display. Or even when the current IME/target are not on the same screen as the next
2202 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002203 mUpdateImeTarget = updateImeTarget;
2204 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002205
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002206
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002207 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2208 // to be on top of it, but it is not -really- where input will go. So look down below
2209 // for a real window to target...
2210 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2211 final AppWindowToken token = target.mAppToken;
2212 if (token != null) {
2213 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2214 if (betterTarget != null) {
2215 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002216 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002217 }
2218 }
2219
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002220 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2221 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002222
2223 // Now, a special case -- if the last target's window is in the process of exiting, and is
2224 // above the new target, keep on the last target to avoid flicker. Consider for example a
2225 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2226 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2227 // scrim.
2228 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002229 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2230 && (target == null
2231 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002232 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002233 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002234 }
2235
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002236 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2237 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002238
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002239 if (target == null) {
2240 if (updateImeTarget) {
2241 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2242 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2243 + Debug.getCallers(4) : ""));
2244 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2245 }
2246
2247 return null;
2248 }
2249
2250 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002251 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2252 if (token != null) {
2253
2254 // Now some fun for dealing with window animations that modify the Z order. We need
2255 // to look at all windows below the current target that are in this app, finding the
2256 // highest visible one in layering.
2257 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002258 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002259 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002260 }
2261
2262 if (highestTarget != null) {
2263 final AppTransition appTransition = mService.mAppTransition;
2264 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2265 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2266 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002267 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002268
2269 if (appTransition.isTransitionSet()) {
2270 // If we are currently setting up for an animation, hold everything until we
2271 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002272 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2273 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002274 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002275 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002276 // If the window we are currently targeting is involved with an animation,
2277 // and it is on top of the next target we will be over, then hold off on
2278 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002279 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2280 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002281 }
2282 }
2283 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002284
2285 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2286 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2287 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002288 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002289 }
2290
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002291 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002292 }
2293
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002294 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2295 if (target == mService.mInputMethodTarget
2296 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2297 && mInputMethodAnimLayerAdjustment == layerAdj) {
2298 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002299 }
2300
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002301 mService.mInputMethodTarget = target;
2302 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2303 setInputMethodAnimLayerAdjustment(layerAdj);
2304 assignWindowLayers(false /* setLayoutNeeded */);
2305 }
2306
2307 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2308 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2309 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2310 }
2311
2312 // Used to indicate we have reached the first window in the range we are interested in.
2313 mTmpWindow = null;
2314
2315 // TODO: Figure-out a more efficient way to do this.
2316 final WindowState candidate = getWindow(w -> {
2317 if (w == top) {
2318 // Reached the first window in the range we are interested in.
2319 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002320 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002321 if (mTmpWindow == null) {
2322 return false;
2323 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002324
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002325 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2326 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002327 }
2328 // If we reached the bottom of the range of windows we are considering,
2329 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002330 if (w == bottom) {
2331 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002332 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002333 return false;
2334 });
2335
2336 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002337 }
2338
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002339 void setLayoutNeeded() {
2340 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2341 mLayoutNeeded = true;
2342 }
2343
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002344 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002345 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2346 mLayoutNeeded = false;
2347 }
2348
2349 boolean isLayoutNeeded() {
2350 return mLayoutNeeded;
2351 }
2352
Wale Ogunwale02319a62016-09-26 15:21:22 -07002353 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2354 if (mTokenMap.isEmpty()) {
2355 return;
2356 }
2357 pw.println(" Display #" + mDisplayId);
2358 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2359 while (it.hasNext()) {
2360 final WindowToken token = it.next();
2361 pw.print(" ");
2362 pw.print(token);
2363 if (dumpAll) {
2364 pw.println(':');
2365 token.dump(pw, " ");
2366 } else {
2367 pw.println();
2368 }
2369 }
2370 }
2371
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002372 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002373 final int[] index = new int[1];
2374 forAllWindows(w -> {
2375 final WindowStateAnimator wAnim = w.mWinAnimator;
2376 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2377 index[0] = index[0] + 1;
2378 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002379 }
2380
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002381 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 forAllWindows(w -> {
2383 w.mWinAnimator.enableSurfaceTrace(fd);
2384 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002385 }
2386
2387 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002388 forAllWindows(w -> {
2389 w.mWinAnimator.disableSurfaceTrace();
2390 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002391 }
2392
Jorim Jaggife762342016-10-13 14:33:27 +02002393 /**
2394 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2395 */
2396 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2397 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 forAllWindows(w -> {
2399 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
2400 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002401 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2402 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002403 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002404 }
2405
Wale Ogunwale494009b82016-10-21 09:01:38 -07002406 boolean checkWaitingForWindows() {
2407
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002408 mHaveBootMsg = false;
2409 mHaveApp = false;
2410 mHaveWallpaper = false;
2411 mHaveKeyguard = true;
2412
2413 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002414 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2415 return true;
2416 }
2417 if (w.isDrawnLw()) {
2418 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002419 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002420 } else if (w.mAttrs.type == TYPE_APPLICATION
2421 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002422 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002423 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002424 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002425 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002426 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002427 }
2428 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002429 return false;
2430 });
2431
2432 if (visibleWindow != null) {
2433 // We have a visible window.
2434 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002435 }
2436
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002437 // if the wallpaper service is disabled on the device, we're never going to have
2438 // wallpaper, don't bother waiting for it
2439 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2440 com.android.internal.R.bool.config_enableWallpaperService)
2441 && !mService.mOnlyCore;
2442
Wale Ogunwale494009b82016-10-21 09:01:38 -07002443 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2444 "******** booted=" + mService.mSystemBooted
2445 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002446 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2447 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2448 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002449
2450 // If we are turning on the screen to show the boot message, don't do it until the boot
2451 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002452 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002453 return true;
2454 }
2455
2456 // If we are turning on the screen after the boot is completed normally, don't do so until
2457 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002458 if (mService.mSystemBooted
2459 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002460 return true;
2461 }
2462
2463 return false;
2464 }
2465
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002466 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002467 mTmpWindowAnimator = animator;
2468 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002469 }
2470
2471 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002472 resetAnimationBackgroundAnimator();
2473
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002474 // Used to indicate a detached wallpaper.
2475 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002476 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002477
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002478 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002479
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002480 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002481 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002482 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2483 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002484 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2485 }
2486 }
2487
2488 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002489 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002490 }
2491
Wale Ogunwale494009b82016-10-21 09:01:38 -07002492 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002493 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002494 if (imFocus == null) {
2495 return false;
2496 }
2497
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002498 if (DEBUG_INPUT_METHOD) {
2499 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2500 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2501 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002502 }
2503
Wale Ogunwale494009b82016-10-21 09:01:38 -07002504 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2505
2506 if (DEBUG_INPUT_METHOD) {
2507 Slog.i(TAG_WM, "IM target client: " + imeClient);
2508 if (imeClient != null) {
2509 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2510 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2511 }
2512 }
2513
2514 return imeClient != null && imeClient.asBinder() == client.asBinder();
2515 }
2516
2517 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002518 final WindowState win = getWindow(
2519 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2520 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002521 }
2522
2523 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002524 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002525 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002526 final int curValue = w.mSystemUiVisibility;
2527 final int diff = (curValue ^ visibility) & globalDiff;
2528 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002529 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002530 w.mSeq++;
2531 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002532 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002533 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2534 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002535 visibility, newValue, diff);
2536 }
2537 } catch (RemoteException e) {
2538 // so sorry
2539 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002540 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002541 }
2542
2543 void onWindowFreezeTimeout() {
2544 Slog.w(TAG_WM, "Window freeze timeout expired.");
2545 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002546
2547 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002548 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002549 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002550 }
2551 w.mOrientationChanging = false;
2552 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2553 - mService.mDisplayFreezeTime);
2554 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002555 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002556 mService.mWindowPlacerLocked.performSurfacePlacement();
2557 }
2558
2559 void waitForAllWindowsDrawn() {
2560 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002561 forAllWindows(w -> {
2562 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2563 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2564 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002565 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002566 w.mLastContentInsets.set(-1, -1, -1, -1);
2567 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002568 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002569 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002570 }
2571
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002572 // TODO: Super crazy long method that should be broken down...
2573 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2574
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002575 final int dw = mDisplayInfo.logicalWidth;
2576 final int dh = mDisplayInfo.logicalHeight;
2577 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2578
2579 mTmpUpdateAllDrawn.clear();
2580
2581 int repeats = 0;
2582 do {
2583 repeats++;
2584 if (repeats > 6) {
2585 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2586 clearLayoutNeeded();
2587 break;
2588 }
2589
2590 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2591 pendingLayoutChanges);
2592
Andrii Kulian839def92016-11-02 10:58:58 -07002593 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2594 // the wallpaper window jumping across displays.
2595 // Remove check for default display when there will be support for multiple wallpaper
2596 // targets (on different displays).
2597 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002598 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002599 }
2600
2601 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2602 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2603 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2604 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002605 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002606 }
2607 }
2608
2609 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2610 setLayoutNeeded();
2611 }
2612
2613 // FIRST LOOP: Perform a layout, if needed.
2614 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2615 performLayout(repeats == 1, false /* updateInputWindows */);
2616 } else {
2617 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2618 }
2619
2620 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2621 pendingLayoutChanges = 0;
2622
2623 if (isDefaultDisplay) {
2624 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002625 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002626 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2627 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2628 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2629 }
2630 } while (pendingLayoutChanges != 0);
2631
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002633 resetDimming();
2634
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002635 mTmpRecoveringMemory = recoveringMemory;
2636 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002637
2638 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002639 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2640 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2641 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002642 true /* inTraversal, must call performTraversalInTrans... below */);
2643
2644 stopDimmingIfNeeded();
2645
2646 while (!mTmpUpdateAllDrawn.isEmpty()) {
2647 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2648 // See if any windows have been drawn, so they (and others associated with them)
2649 // can now be shown.
2650 atoken.updateAllDrawn(this);
2651 }
2652
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002653 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002654 }
2655
2656 void performLayout(boolean initial, boolean updateInputWindows) {
2657 if (!isLayoutNeeded()) {
2658 return;
2659 }
2660 clearLayoutNeeded();
2661
2662 final int dw = mDisplayInfo.logicalWidth;
2663 final int dh = mDisplayInfo.logicalHeight;
2664
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002665 if (DEBUG_LAYOUT) {
2666 Slog.v(TAG, "-------------------------------------");
2667 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2668 }
2669
Andrii Kulian8ee72852017-03-10 10:36:45 -08002670 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002671 getConfiguration().uiMode);
2672 if (isDefaultDisplay) {
2673 // Not needed on non-default displays.
2674 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2675 mService.mScreenRect.set(0, 0, dw, dh);
2676 }
2677
2678 mService.mPolicy.getContentRectLw(mContentRect);
2679
2680 int seq = mService.mLayoutSeq + 1;
2681 if (seq < 0) seq = 0;
2682 mService.mLayoutSeq = seq;
2683
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002684 // Used to indicate that we have processed the dream window and all additional windows are
2685 // behind it.
2686 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002687 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002688
2689 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002690 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002691
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002692 // Used to indicate that we have processed the dream window and all additional attached
2693 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002694 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002695 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002696
2697 // Now perform layout of attached windows, which usually depend on the position of the
2698 // window they are attached to. XXX does not deal with windows that are attached to windows
2699 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002700 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002701
2702 // Window frames may have changed. Tell the input dispatcher about it.
2703 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2704 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2705 if (updateInputWindows) {
2706 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2707 }
2708
2709 mService.mPolicy.finishLayoutLw();
2710 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2711 }
2712
2713 /**
2714 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2715 * In portrait mode, it grabs the full screenshot.
2716 *
2717 * @param width the width of the target bitmap
2718 * @param height the height of the target bitmap
2719 * @param includeFullDisplay true if the screen should not be cropped before capture
2720 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2721 * @param config of the output bitmap
2722 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002723 * @param includeDecor whether to include window decors, like the status or navigation bar
2724 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002725 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002726 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002727 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002728 boolean wallpaperOnly, boolean includeDecor) {
2729 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2730 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002731 if (bitmap == null) {
2732 return null;
2733 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002734
2735 if (DEBUG_SCREENSHOT) {
2736 // TEST IF IT's ALL BLACK
2737 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2738 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2739 bitmap.getHeight());
2740 boolean allBlack = true;
2741 final int firstColor = buffer[0];
2742 for (int i = 0; i < buffer.length; i++) {
2743 if (buffer[i] != firstColor) {
2744 allBlack = false;
2745 break;
2746 }
2747 }
2748 if (allBlack) {
2749 final WindowState appWin = mScreenshotApplicationState.appWin;
2750 final int maxLayer = mScreenshotApplicationState.maxLayer;
2751 final int minLayer = mScreenshotApplicationState.minLayer;
2752 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2753 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2754 (appWin != null ?
2755 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2756 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2757 }
2758 }
2759
2760 // Create a copy of the screenshot that is immutable and backed in ashmem.
2761 // This greatly reduces the overhead of passing the bitmap between processes.
2762 Bitmap ret = bitmap.createAshmemBitmap(config);
2763 bitmap.recycle();
2764 return ret;
2765 }
2766
2767 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2768 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2769 boolean includeDecor) {
2770 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2771 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2772 }
2773
2774 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2775 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2776 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002777 int dw = mDisplayInfo.logicalWidth;
2778 int dh = mDisplayInfo.logicalHeight;
2779 if (dw == 0 || dh == 0) {
2780 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2781 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2782 return null;
2783 }
2784
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002785 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002786
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002787 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002788 final Rect frame = new Rect();
2789 final Rect stackBounds = new Rect();
2790
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002791 boolean includeImeInScreenshot;
2792 synchronized(mService.mWindowMap) {
2793 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2794 ? mService.mInputMethodTarget.mAppToken : null;
2795 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2796 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2797 // mode because the frame of the IME might not overlap with that of the app.
2798 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2799 // overlapping with the bottom app.
2800 includeImeInScreenshot = imeTargetAppToken != null
2801 && imeTargetAppToken.appToken != null
2802 && imeTargetAppToken.appToken.asBinder() == appToken
2803 && !mService.mInputMethodTarget.isInMultiWindowMode();
2804 }
2805
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002806 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002807 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002808 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002809 synchronized(mService.mWindowMap) {
2810 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002811 mScreenshotApplicationState.appWin = null;
2812 forAllWindows(w -> {
2813 if (!w.mHasSurface) {
2814 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002815 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002816 if (w.mLayer >= aboveAppLayer) {
2817 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002818 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002819 if (wallpaperOnly && !w.mIsWallpaper) {
2820 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002821 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002822 if (w.mIsImWindow) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002823 if (!includeImeInScreenshot) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002824 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002825 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002826 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002827 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2828 // then the target window state is this one.
2829 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002830 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002831 }
2832
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002833 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002834 // We have not ran across the target window yet, so it is probably behind
2835 // the wallpaper. This can happen when the keyguard is up and all windows
2836 // are moved behind the wallpaper. We don't want to include the wallpaper
2837 // layer in the screenshot as it will cover-up the layer of the target
2838 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002839 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002840 }
2841 // Fall through. The target window is in front of the wallpaper. For this
2842 // case we want to include the wallpaper layer in the screenshot because
2843 // the target window might have some transparent areas.
2844 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002845 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002846 // This app window is of no interest if it is not associated with the
2847 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002848 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002849 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002850 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002851 }
2852
2853 // Include this window.
2854
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002855 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002856 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002857 if (mScreenshotApplicationState.maxLayer < layer) {
2858 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002859 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002860 if (mScreenshotApplicationState.minLayer > layer) {
2861 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002862 }
2863
2864 // Don't include wallpaper in bounds calculation
Jorim Jaggibfbd9dc2017-01-20 18:13:01 +01002865 if (!mutableIncludeFullDisplay.value && includeDecor) {
2866 final TaskStack stack = w.getStack();
2867 if (stack != null) {
2868 stack.getBounds(frame);
2869 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08002870 } else if (!mutableIncludeFullDisplay.value && !w.mIsWallpaper) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002871 final Rect wf = w.mFrame;
2872 final Rect cr = w.mContentInsets;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002873 int left = wf.left + cr.left;
2874 int top = wf.top + cr.top;
2875 int right = wf.right - cr.right;
2876 int bottom = wf.bottom - cr.bottom;
2877 frame.union(left, top, right, bottom);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002878 w.getVisibleBounds(stackBounds);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002879 if (!Rect.intersects(frame, stackBounds)) {
2880 // Set frame empty if there's no intersection.
2881 frame.setEmpty();
2882 }
2883 }
2884
2885 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002886 (w.mAppToken != null && w.mAppToken.token == appToken)
2887 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002888 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002889 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002890 }
2891
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002892 if (w.isObscuringDisplay()){
2893 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002894 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002895 return false;
2896 }, true /* traverseTopToBottom */);
2897
2898 final WindowState appWin = mScreenshotApplicationState.appWin;
2899 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2900 final int maxLayer = mScreenshotApplicationState.maxLayer;
2901 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002902
2903 if (appToken != null && appWin == null) {
2904 // Can't find a window to snapshot.
2905 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2906 "Screenshot: Couldn't find a surface matching " + appToken);
2907 return null;
2908 }
2909
2910 if (!screenshotReady) {
2911 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2912 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2913 appWin.mWinAnimator.mDrawState)));
2914 return null;
2915 }
2916
2917 // Screenshot is ready to be taken. Everything from here below will continue
2918 // through the bottom of the loop and return a value. We only stay in the loop
2919 // because we don't want to release the mWindowMap lock until the screenshot is
2920 // taken.
2921
2922 if (maxLayer == 0) {
2923 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2924 + ": returning null maxLayer=" + maxLayer);
2925 return null;
2926 }
2927
Jorim Jaggi02886a82016-12-06 09:10:06 -08002928 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002929 // Constrain frame to the screen size.
2930 if (!frame.intersect(0, 0, dw, dh)) {
2931 frame.setEmpty();
2932 }
2933 } else {
2934 // Caller just wants entire display.
2935 frame.set(0, 0, dw, dh);
2936 }
2937 if (frame.isEmpty()) {
2938 return null;
2939 }
2940
2941 if (width < 0) {
2942 width = (int) (frame.width() * frameScale);
2943 }
2944 if (height < 0) {
2945 height = (int) (frame.height() * frameScale);
2946 }
2947
2948 // Tell surface flinger what part of the image to crop. Take the top
2949 // right part of the application, and crop the larger dimension to fit.
2950 Rect crop = new Rect(frame);
2951 if (width / (float) frame.width() < height / (float) frame.height()) {
2952 int cropWidth = (int)((float)width / (float)height * frame.height());
2953 crop.right = crop.left + cropWidth;
2954 } else {
2955 int cropHeight = (int)((float)height / (float)width * frame.width());
2956 crop.bottom = crop.top + cropHeight;
2957 }
2958
2959 // The screenshot API does not apply the current screen rotation.
2960 int rot = mDisplay.getRotation();
2961
2962 if (rot == ROTATION_90 || rot == ROTATION_270) {
2963 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2964 }
2965
2966 // Surfaceflinger is not aware of orientation, so convert our logical
2967 // crop to surfaceflinger's portrait orientation.
2968 convertCropForSurfaceFlinger(crop, rot, dw, dh);
2969
2970 if (DEBUG_SCREENSHOT) {
2971 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
2972 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002973 forAllWindows(w -> {
2974 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
2975 Slog.i(TAG_WM, w + ": " + w.mLayer
2976 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002977 + " surfaceLayer=" + ((controller == null)
2978 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002979 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002980 }
2981
2982 final ScreenRotationAnimation screenRotationAnimation =
2983 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2984 final boolean inRotation = screenRotationAnimation != null &&
2985 screenRotationAnimation.isAnimating();
2986 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
2987 "Taking screenshot while rotating");
2988
2989 // We force pending transactions to flush before taking
2990 // the screenshot by pushing an empty synchronous transaction.
2991 SurfaceControl.openTransaction();
2992 SurfaceControl.closeTransactionSync();
2993
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002994 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002995 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002996 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002997 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
2998 + ") to layer " + maxLayer);
2999 return null;
3000 }
3001 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003002 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003003 }
3004
3005 // TODO: Can this use createRotationMatrix()?
3006 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3007 if (rot == Surface.ROTATION_90) {
3008 final int tmp = crop.top;
3009 crop.top = dw - crop.right;
3010 crop.right = crop.bottom;
3011 crop.bottom = dw - crop.left;
3012 crop.left = tmp;
3013 } else if (rot == Surface.ROTATION_180) {
3014 int tmp = crop.top;
3015 crop.top = dh - crop.bottom;
3016 crop.bottom = dh - tmp;
3017 tmp = crop.right;
3018 crop.right = dw - crop.left;
3019 crop.left = dw - tmp;
3020 } else if (rot == Surface.ROTATION_270) {
3021 final int tmp = crop.top;
3022 crop.top = crop.left;
3023 crop.left = dh - crop.bottom;
3024 crop.bottom = crop.right;
3025 crop.right = dh - tmp;
3026 }
3027 }
3028
3029 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003030 // Used to indicate the layout is needed.
3031 mTmpWindow = null;
3032
3033 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003034 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003035 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003036 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003037 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003038 w.setDisplayLayoutNeeded();
3039 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003040 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003041
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003042 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003043 mService.mWindowPlacerLocked.performSurfacePlacement();
3044 }
3045 }
3046
Wale Ogunwale1666e312016-12-16 11:27:18 -08003047 void setExitingTokensHasVisible(boolean hasVisible) {
3048 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3049 mExitingTokens.get(i).hasVisible = hasVisible;
3050 }
3051
3052 // Initialize state of exiting applications.
3053 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3054 }
3055
3056 void removeExistingTokensIfPossible() {
3057 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3058 final WindowToken token = mExitingTokens.get(i);
3059 if (!token.hasVisible) {
3060 mExitingTokens.remove(i);
3061 }
3062 }
3063
3064 // Time to remove any exiting applications?
3065 mTaskStackContainers.removeExistingAppTokensIfPossible();
3066 }
3067
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003068 @Override
3069 void onDescendantOverrideConfigurationChanged() {
3070 setLayoutNeeded();
3071 mService.requestTraversal();
3072 }
3073
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003074 static final class TaskForResizePointSearchResult {
3075 boolean searchDone;
3076 Task taskForResize;
3077
3078 void reset() {
3079 searchDone = false;
3080 taskForResize = null;
3081 }
3082 }
Robert Carr3b716242016-08-16 16:02:21 -07003083
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003084 private static final class ApplySurfaceChangesTransactionState {
3085 boolean displayHasContent;
3086 boolean obscured;
3087 boolean syswin;
3088 boolean focusDisplayed;
3089 float preferredRefreshRate;
3090 int preferredModeId;
3091
3092 void reset() {
3093 displayHasContent = false;
3094 obscured = false;
3095 syswin = false;
3096 focusDisplayed = false;
3097 preferredRefreshRate = 0;
3098 preferredModeId = 0;
3099 }
3100 }
3101
3102 private static final class ScreenshotApplicationState {
3103 WindowState appWin;
3104 int maxLayer;
3105 int minLayer;
3106 boolean screenshotReady;
3107
3108 void reset(boolean screenshotReady) {
3109 appWin = null;
3110 maxLayer = 0;
3111 minLayer = 0;
3112 this.screenshotReady = screenshotReady;
3113 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3114 }
3115 }
3116
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003117 /**
3118 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3119 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3120 * homogeneous children type which is currently required by sub-classes of
3121 * {@link WindowContainer} class.
3122 */
3123 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3124
3125 int size() {
3126 return mChildren.size();
3127 }
3128
3129 E get(int index) {
3130 return mChildren.get(index);
3131 }
3132
3133 @Override
3134 boolean fillsParent() {
3135 return true;
3136 }
3137
3138 @Override
3139 boolean isVisible() {
3140 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003141 }
3142 }
3143
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003144 /**
3145 * Window container class that contains all containers on this display relating to Apps.
3146 * I.e Activities.
3147 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003148 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003149
Andrii Kulian839def92016-11-02 10:58:58 -07003150 /**
3151 * Adds the stack to this container.
3152 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3153 */
3154 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003155 if (stack.mStackId == HOME_STACK_ID) {
3156 if (mHomeStack != null) {
3157 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3158 }
3159 mHomeStack = stack;
3160 }
3161 addChild(stack, onTop);
3162 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003163 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003164
Andrii Kulian839def92016-11-02 10:58:58 -07003165 /** Removes the stack from its container and prepare for changing the parent. */
3166 void removeStackFromDisplay(TaskStack stack) {
3167 removeChild(stack);
3168 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003169 }
3170
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003171 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003172 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3173 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003174 addChild(stack, addIndex);
3175 setLayoutNeeded();
3176 }
3177
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003178 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003179 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3180 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3181 // This stack is always-on-top, override the default behavior.
3182 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3183
3184 // Moving to its current position, as we must call super but we don't want to
3185 // perform any meaningful action.
3186 final int currentPosition = mChildren.indexOf(child);
3187 super.positionChildAt(currentPosition, child, false /* includingParents */);
3188 return;
3189 }
3190
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003191 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3192 super.positionChildAt(targetPosition, child, includingParents);
3193
3194 setLayoutNeeded();
3195 }
3196
3197 /**
3198 * When stack is added or repositioned, find a proper position for it.
3199 * This will make sure that pinned stack always stays on top.
3200 * @param requestedPosition Position requested by caller.
3201 * @param stack Stack to be added or positioned.
3202 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3203 * @return The proper position for the stack.
3204 */
3205 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3206 final int topChildPosition = mChildren.size() - 1;
3207 boolean toTop = requestedPosition == POSITION_TOP;
3208 toTop |= adding ? requestedPosition >= topChildPosition + 1
3209 : requestedPosition >= topChildPosition;
3210 int targetPosition = requestedPosition;
3211
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003212 if (toTop && isStackVisible(PINNED_STACK_ID) && stack.mStackId != PINNED_STACK_ID) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003213 // The pinned stack is always the top most stack (always-on-top) when it is visible.
3214 TaskStack topStack = mChildren.get(topChildPosition);
3215 if (topStack.mStackId != PINNED_STACK_ID) {
3216 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3217 }
3218
3219 // So, stack is moved just below the pinned stack.
3220 // When we're adding a new stack the target is the current pinned stack position.
3221 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003222 // stack, because WindowContainer#positionAt() first removes element and then adds
3223 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003224 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3225 }
3226
3227 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003228 }
3229
3230 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003231 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3232 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003233 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003234 if (super.forAllWindows(callback, traverseTopToBottom)) {
3235 return true;
3236 }
3237 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3238 return true;
3239 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003240 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003241 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3242 return true;
3243 }
3244 if (super.forAllWindows(callback, traverseTopToBottom)) {
3245 return true;
3246 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003247 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003248 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003249 }
3250
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003251 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003252 boolean traverseTopToBottom) {
3253 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3254 // app tokens.
3255 // TODO: Investigate if we need to continue to do this or if we can just process them
3256 // in-order.
3257 if (traverseTopToBottom) {
3258 for (int i = mChildren.size() - 1; i >= 0; --i) {
3259 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3260 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003261 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3262 traverseTopToBottom)) {
3263 return true;
3264 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003265 }
3266 }
3267 } else {
3268 final int count = mChildren.size();
3269 for (int i = 0; i < count; ++i) {
3270 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3271 final int appTokensCount = appTokens.size();
3272 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003273 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3274 traverseTopToBottom)) {
3275 return true;
3276 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003277 }
3278 }
3279 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003280 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003281 }
3282
Wale Ogunwale1666e312016-12-16 11:27:18 -08003283 void setExitingTokensHasVisible(boolean hasVisible) {
3284 for (int i = mChildren.size() - 1; i >= 0; --i) {
3285 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3286 for (int j = appTokens.size() - 1; j >= 0; --j) {
3287 appTokens.get(j).hasVisible = hasVisible;
3288 }
3289 }
3290 }
3291
3292 void removeExistingAppTokensIfPossible() {
3293 for (int i = mChildren.size() - 1; i >= 0; --i) {
3294 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3295 for (int j = appTokens.size() - 1; j >= 0; --j) {
3296 final AppWindowToken token = appTokens.get(j);
3297 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3298 && (!token.mIsExiting || token.isEmpty())) {
3299 // Make sure there is no animation running on this token, so any windows
3300 // associated with it will be removed as soon as their animations are
3301 // complete.
3302 token.mAppAnimator.clearAnimation();
3303 token.mAppAnimator.animating = false;
3304 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3305 "performLayout: App token exiting now removed" + token);
3306 token.removeIfPossible();
3307 }
3308 }
3309 }
3310 }
3311
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003312 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003313 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003314 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003315 // Apps and their containers are not allowed to specify an orientation while the
3316 // docked or freeform stack is visible...except for the home stack/task if the
3317 // docked stack is minimized and it actually set something.
3318 if (mHomeStack != null && mHomeStack.isVisible()
3319 && mDividerControllerLocked.isMinimizedDock()) {
3320 final int orientation = mHomeStack.getOrientation();
3321 if (orientation != SCREEN_ORIENTATION_UNSET) {
3322 return orientation;
3323 }
3324 }
3325 return SCREEN_ORIENTATION_UNSPECIFIED;
3326 }
3327
3328 final int orientation = super.getOrientation();
3329 if (orientation != SCREEN_ORIENTATION_UNSET
3330 && orientation != SCREEN_ORIENTATION_BEHIND) {
3331 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3332 "App is requesting an orientation, return " + orientation);
3333 return orientation;
3334 }
3335
3336 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003337 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003338 // The next app has not been requested to be visible, so we keep the current orientation
3339 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003340 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003341 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003342 }
3343
3344 /**
3345 * Window container class that contains all containers on this display that are not related to
3346 * Apps. E.g. status bar.
3347 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003348 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3349 /**
3350 * Compares two child window tokens returns -1 if the first is lesser than the second in
3351 * terms of z-order and 1 otherwise.
3352 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003353 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003354 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003355 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3356 token1.mOwnerCanManageAppTokens)
3357 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3358 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003359
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003360 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3361 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3362 return false;
3363 }
3364 final int req = w.mAttrs.screenOrientation;
3365 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3366 || req == SCREEN_ORIENTATION_UNSET) {
3367 return false;
3368 }
3369 return true;
3370 };
3371
Wale Ogunwale3a931692016-11-02 16:49:48 -07003372 private final String mName;
3373 NonAppWindowContainers(String name) {
3374 mName = name;
3375 }
3376
3377 void addChild(WindowToken token) {
3378 addChild(token, mWindowComparator);
3379 }
3380
3381 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003382 int getOrientation() {
3383 final WindowManagerPolicy policy = mService.mPolicy;
3384 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003385 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003386
3387 if (win != null) {
3388 final int req = win.mAttrs.screenOrientation;
3389 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
3390 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003391 mLastKeyguardForcedOrientation = req;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003392 }
Andrii Kulian8ee72852017-03-10 10:36:45 -08003393 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003394 }
3395
Andrii Kulian8ee72852017-03-10 10:36:45 -08003396 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003397
3398 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003399 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003400 }
3401
3402 return SCREEN_ORIENTATION_UNSET;
3403 }
3404
3405 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003406 String getName() {
3407 return mName;
3408 }
Robert Carr3b716242016-08-16 16:02:21 -07003409 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003410
3411 /**
3412 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3413 */
3414 @FunctionalInterface
3415 private interface Screenshoter<E> {
3416 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3417 boolean useIdentityTransform, int rotation);
3418 }
Craig Mautner59c00972012-07-30 12:10:24 -07003419}