blob: aa8557490f256ef1745a14c75138944625afefac [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);
897 final boolean rotateSeamlessly;
898
899 if (mService.mPolicy.shouldRotateSeamlessly(oldRotation, rotation)) {
900 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
901 if (seamlessRotated != null) {
902 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
903 // to complete (that is, waiting for windows to redraw). It's tempting to check
904 // w.mSeamlessRotationCount but that could be incorrect in the case of
905 // window-removal.
906 return false;
907 }
908
909 final WindowState cantSeamlesslyRotate = getWindow((w) ->
910 w.isChildWindow() && w.isVisibleNow()
911 && !w.mWinAnimator.mSurfaceController.getTransformToDisplayInverse());
912 if (cantSeamlesslyRotate != null) {
913 // In what can only be called an unfortunate workaround we require seamlessly
914 // rotated child windows to have the TRANSFORM_TO_DISPLAY_INVERSE flag. Due to
915 // limitations in the client API, there is no way for the client to set this flag in
916 // a race free fashion. If we seamlessly rotate a window which does not have this
917 // flag, but then gains it, we will get an incorrect visual result
918 // (rotated viewfinder). This means if we want to support seamlessly rotating
919 // windows which could gain this flag, we can't rotate windows without it. This
920 // limits seamless rotation in N to camera framework users, windows without
921 // children, and native code. This is unfortunate but having the camera work is our
922 // primary goal.
923 rotateSeamlessly = false;
924 } else {
925 rotateSeamlessly = true;
926 }
927 } else {
928 rotateSeamlessly = false;
929 }
930
931 // TODO: Implement forced rotation changes.
932 // Set mAltOrientation to indicate that the application is receiving
933 // an orientation that has different metrics than it expected.
934 // eg. Portrait instead of Landscape.
935
936 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
937 lastOrientation, rotation);
938
939 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
940 + ", got rotation " + rotation + " which has "
941 + (altOrientation ? "incompatible" : "compatible") + " metrics");
942
943 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
944 // No change.
945 return false;
946 }
947
948 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
949 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
950 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
951
952 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
953 mService.mWaitingForConfig = true;
954 }
955
956 mRotation = rotation;
957 mAltOrientation = altOrientation;
958 if (isDefaultDisplay) {
959 mService.mPolicy.setRotationLw(rotation);
960 }
961
962 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
963 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
964 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
965 WINDOW_FREEZE_TIMEOUT_DURATION);
966
967 setLayoutNeeded();
968 final int[] anim = new int[2];
969 if (isDimming()) {
970 anim[0] = anim[1] = 0;
971 } else {
972 mService.mPolicy.selectRotationAnimationLw(anim);
973 }
974
975 if (!rotateSeamlessly) {
976 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1]);
977 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
978 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
979 mDisplayId);
980 } else {
981 // The screen rotation animation uses a screenshot to freeze the screen
982 // while windows resize underneath.
983 // When we are rotating seamlessly, we allow the elements to transition
984 // to their rotated state independently and without a freeze required.
985 screenRotationAnimation = null;
986
987 // We have to reset this in case a window was removed before it
988 // finished seamless rotation.
989 mService.mSeamlessRotationCount = 0;
990 }
991
992 // We need to update our screen size information to match the new rotation. If the rotation
993 // has actually changed then this method will return true and, according to the comment at
994 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
995 // By updating the Display info here it will be available to
996 // #computeScreenConfiguration() later.
997 updateDisplayAndOrientation(getConfiguration().uiMode);
998
999 if (!inTransaction) {
1000 if (SHOW_TRANSACTIONS) {
1001 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1002 }
1003 mService.openSurfaceTransaction();
1004 }
1005 try {
1006 // NOTE: We disable the rotation in the emulator because
1007 // it doesn't support hardware OpenGL emulation yet.
1008 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1009 && screenRotationAnimation.hasScreenshot()) {
1010 if (screenRotationAnimation.setRotationInTransaction(
1011 rotation, mService.mFxSession,
1012 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1013 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1014 mService.scheduleAnimationLocked();
1015 }
1016 }
1017
1018 if (rotateSeamlessly) {
1019 forAllWindows(w -> {
1020 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1021 }, true /* traverseTopToBottom */);
1022 }
1023
1024 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1025 } finally {
1026 if (!inTransaction) {
1027 mService.closeSurfaceTransaction();
1028 if (SHOW_LIGHT_TRANSACTIONS) {
1029 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1030 }
1031 }
1032 }
1033
1034 forAllWindows(w -> {
1035 // Discard surface after orientation change, these can't be reused.
1036 if (w.mAppToken != null) {
1037 w.mAppToken.destroySavedSurfaces();
1038 }
1039 if (w.mHasSurface && !rotateSeamlessly) {
1040 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
1041 w.mOrientationChanging = true;
1042 mService.mRoot.mOrientationChangeComplete = false;
1043 w.mLastFreezeDuration = 0;
1044 }
1045 w.mReportOrientationChanged = true;
1046 }, true /* traverseTopToBottom */);
1047
1048 if (rotateSeamlessly) {
1049 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1050 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1051 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1052 }
1053
1054 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1055 final WindowManagerService.RotationWatcher rotationWatcher
1056 = mService.mRotationWatchers.get(i);
1057 if (rotationWatcher.mDisplayId == mDisplayId) {
1058 try {
1059 rotationWatcher.mWatcher.onRotationChanged(rotation);
1060 } catch (RemoteException e) {
1061 // Ignore
1062 }
1063 }
1064 }
1065
1066 // TODO (multi-display): Magnification is supported only for the default display.
1067 // Announce rotation only if we will not animate as we already have the
1068 // windows in final state. Otherwise, we make this call at the rotation end.
1069 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1070 && isDefaultDisplay) {
1071 mService.mAccessibilityController.onRotationChangedLocked(this);
1072 }
1073
1074 return true;
1075 }
1076
1077 /**
1078 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1079 * changed.
1080 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1081 */
1082 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1083 // Use the effective "visual" dimensions based on current rotation
1084 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1085 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1086 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1087 int dw = realdw;
1088 int dh = realdh;
1089
1090 if (mAltOrientation) {
1091 if (realdw > realdh) {
1092 // Turn landscape into portrait.
1093 int maxw = (int)(realdh/1.3f);
1094 if (maxw < realdw) {
1095 dw = maxw;
1096 }
1097 } else {
1098 // Turn portrait into landscape.
1099 int maxh = (int)(realdw/1.3f);
1100 if (maxh < realdh) {
1101 dh = maxh;
1102 }
1103 }
1104 }
1105
1106 // Update application display metrics.
1107 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1108 mDisplayId);
1109 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1110 mDisplayId);
1111 mDisplayInfo.rotation = mRotation;
1112 mDisplayInfo.logicalWidth = dw;
1113 mDisplayInfo.logicalHeight = dh;
1114 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1115 mDisplayInfo.appWidth = appWidth;
1116 mDisplayInfo.appHeight = appHeight;
1117 if (isDefaultDisplay) {
1118 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1119 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1120 }
1121 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1122 if (mDisplayScalingDisabled) {
1123 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1124 } else {
1125 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1126 }
1127
1128 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1129 mDisplayInfo);
1130
1131 mBaseDisplayRect.set(0, 0, dw, dh);
1132
1133 if (isDefaultDisplay) {
1134 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1135 mCompatDisplayMetrics);
1136 }
1137 return mDisplayInfo;
1138 }
1139
1140 /**
1141 * Compute display configuration based on display properties and policy settings.
1142 * Do not call if mDisplayReady == false.
1143 */
1144 void computeScreenConfiguration(Configuration config) {
1145 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1146
1147 final int dw = displayInfo.logicalWidth;
1148 final int dh = displayInfo.logicalHeight;
1149 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1150 Configuration.ORIENTATION_LANDSCAPE;
1151 config.screenWidthDp =
1152 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1153 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1154 config.screenHeightDp =
1155 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1156 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1157 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1158 || displayInfo.rotation == Surface.ROTATION_270);
1159
1160 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1161 mDisplayMetrics.density, config);
1162
1163 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1164 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1165 ? Configuration.SCREENLAYOUT_ROUND_YES
1166 : Configuration.SCREENLAYOUT_ROUND_NO);
1167
1168 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1169 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1170 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1171 dh, mDisplayId);
1172 config.densityDpi = displayInfo.logicalDensityDpi;
1173
1174 config.colorMode =
1175 (displayInfo.isHdr()
1176 ? Configuration.COLOR_MODE_HDR_YES
1177 : Configuration.COLOR_MODE_HDR_NO)
1178 | (displayInfo.isWideColorGamut()
1179 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1180 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1181
1182 // Update the configuration based on available input devices, lid switch,
1183 // and platform configuration.
1184 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1185 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1186 config.navigation = Configuration.NAVIGATION_NONAV;
1187
1188 int keyboardPresence = 0;
1189 int navigationPresence = 0;
1190 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1191 final int len = devices != null ? devices.length : 0;
1192 for (int i = 0; i < len; i++) {
1193 InputDevice device = devices[i];
1194 if (!device.isVirtual()) {
1195 final int sources = device.getSources();
1196 final int presenceFlag = device.isExternal() ?
1197 WindowManagerPolicy.PRESENCE_EXTERNAL :
1198 WindowManagerPolicy.PRESENCE_INTERNAL;
1199
1200 // TODO(multi-display): Configure on per-display basis.
1201 if (mService.mIsTouchDevice) {
1202 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1203 InputDevice.SOURCE_TOUCHSCREEN) {
1204 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1205 }
1206 } else {
1207 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1208 }
1209
1210 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1211 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1212 navigationPresence |= presenceFlag;
1213 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1214 && config.navigation == Configuration.NAVIGATION_NONAV) {
1215 config.navigation = Configuration.NAVIGATION_DPAD;
1216 navigationPresence |= presenceFlag;
1217 }
1218
1219 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1220 config.keyboard = Configuration.KEYBOARD_QWERTY;
1221 keyboardPresence |= presenceFlag;
1222 }
1223 }
1224 }
1225
1226 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1227 config.navigation = Configuration.NAVIGATION_DPAD;
1228 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1229 }
1230
1231 // Determine whether a hard keyboard is available and enabled.
1232 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1233 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1234 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1235 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1236 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1237 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1238 }
1239
1240 // Let the policy update hidden states.
1241 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1242 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1243 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1244 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1245 }
1246
1247 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1248 int displayId) {
1249 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1250 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1251 final int unrotDw, unrotDh;
1252 if (rotated) {
1253 unrotDw = dh;
1254 unrotDh = dw;
1255 } else {
1256 unrotDw = dw;
1257 unrotDh = dh;
1258 }
1259 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1260 displayId);
1261 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1262 displayId);
1263 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1264 displayId);
1265 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1266 displayId);
1267 return sw;
1268 }
1269
1270 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1271 DisplayMetrics dm, int dw, int dh, int displayId) {
1272 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1273 displayId);
1274 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1275 uiMode, displayId);
1276 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1277 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1278 if (curSize == 0 || size < curSize) {
1279 curSize = size;
1280 }
1281 return curSize;
1282 }
1283
1284 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1285 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1286
1287 // We need to determine the smallest width that will occur under normal
1288 // operation. To this, start with the base screen size and compute the
1289 // width under the different possible rotations. We need to un-rotate
1290 // the current screen dimensions before doing this.
1291 int unrotDw, unrotDh;
1292 if (rotated) {
1293 unrotDw = dh;
1294 unrotDh = dw;
1295 } else {
1296 unrotDw = dw;
1297 unrotDh = dh;
1298 }
1299 displayInfo.smallestNominalAppWidth = 1<<30;
1300 displayInfo.smallestNominalAppHeight = 1<<30;
1301 displayInfo.largestNominalAppWidth = 0;
1302 displayInfo.largestNominalAppHeight = 0;
1303 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1304 unrotDh);
1305 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1306 unrotDw);
1307 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1308 unrotDh);
1309 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1310 unrotDw);
1311 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1312 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1313 displayId);
1314 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1315 displayId);
1316 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1317 displayId);
1318 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1319 displayId);
1320 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1321 outConfig.screenLayout = sl;
1322 }
1323
1324 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1325 int uiMode, int displayId) {
1326 // Get the app screen size at this rotation.
1327 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1328 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1329
1330 // Compute the screen layout size class for this rotation.
1331 int longSize = w;
1332 int shortSize = h;
1333 if (longSize < shortSize) {
1334 int tmp = longSize;
1335 longSize = shortSize;
1336 shortSize = tmp;
1337 }
1338 longSize = (int)(longSize/density);
1339 shortSize = (int)(shortSize/density);
1340 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1341 }
1342
1343 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1344 int uiMode, int dw, int dh) {
1345 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1346 displayId);
1347 if (width < displayInfo.smallestNominalAppWidth) {
1348 displayInfo.smallestNominalAppWidth = width;
1349 }
1350 if (width > displayInfo.largestNominalAppWidth) {
1351 displayInfo.largestNominalAppWidth = width;
1352 }
1353 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1354 displayId);
1355 if (height < displayInfo.smallestNominalAppHeight) {
1356 displayInfo.smallestNominalAppHeight = height;
1357 }
1358 if (height > displayInfo.largestNominalAppHeight) {
1359 displayInfo.largestNominalAppHeight = height;
1360 }
1361 }
1362
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001363 DockedStackDividerController getDockedDividerController() {
1364 return mDividerControllerLocked;
1365 }
1366
Winson Chung655332c2016-10-31 13:14:28 -07001367 PinnedStackController getPinnedStackController() {
1368 return mPinnedStackControllerLocked;
1369 }
1370
Jeff Browna506a6e2013-06-04 00:02:38 -07001371 /**
1372 * Returns true if the specified UID has access to this display.
1373 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001374 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001375 return mDisplay.hasAccess(uid);
1376 }
1377
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001378 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001379 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001380 }
1381
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001382 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001383 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001384 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001385 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001386 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001387 }
1388
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001389 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001390 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1391 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001392 if (stack.mStackId == stackId) {
1393 return stack;
1394 }
1395 }
1396 return null;
1397 }
1398
Andrii Kulian441e4492016-09-29 15:25:00 -07001399 @Override
1400 void onConfigurationChanged(Configuration newParentConfig) {
1401 super.onConfigurationChanged(newParentConfig);
1402
Andrii Kulian3a507b52016-09-19 18:14:12 -07001403 // The display size information is heavily dependent on the resources in the current
1404 // configuration, so we need to reconfigure it every time the configuration changes.
1405 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1406 mService.reconfigureDisplayLocked(this);
1407
1408 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001409 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001410 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001411
Andrii Kulian441e4492016-09-29 15:25:00 -07001412 /**
1413 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1414 * bounds were updated.
1415 */
1416 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001417 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1418 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001419 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001420 changedStackList.add(stack.mStackId);
1421 }
1422 }
1423 }
1424
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001425 @Override
1426 boolean fillsParent() {
1427 return true;
1428 }
1429
1430 @Override
1431 boolean isVisible() {
1432 return true;
1433 }
1434
1435 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001436 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001437 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001438 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001439 }
1440
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001441 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001442 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1443 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1444 // target, or here in order if there isn't an IME target.
1445 if (traverseTopToBottom) {
1446 for (int i = mChildren.size() - 1; i >= 0; --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 } else {
1458 final int count = mChildren.size();
1459 for (int i = 0; i < count; i++) {
1460 final DisplayChildWindowContainer child = mChildren.get(i);
1461 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1462 // In this case the Ime windows will be processed above their target so we skip
1463 // here.
1464 continue;
1465 }
1466 if (child.forAllWindows(callback, traverseTopToBottom)) {
1467 return true;
1468 }
1469 }
1470 }
1471 return false;
1472 }
1473
1474 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1475 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1476 }
1477
1478 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001479 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001480 final WindowManagerPolicy policy = mService.mPolicy;
1481
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001482 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001483 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001484 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001485 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001486 // If the display is frozen, some activities may be in the middle of restarting, and
1487 // thus have removed their old window. If the window has the flag to hide the lock
1488 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1489 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001490 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001491 } else if (policy.isKeyguardLocked()) {
1492 // Use the last orientation the while the display is frozen with the keyguard
1493 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1494 // window. We don't want to check the show when locked window directly though as
1495 // things aren't stable while the display is frozen, for example the window could be
1496 // momentarily unavailable due to activity relaunch.
1497 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001498 + "return " + mLastOrientation);
1499 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001500 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001501 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001502 final int orientation = mAboveAppWindowsContainers.getOrientation();
1503 if (orientation != SCREEN_ORIENTATION_UNSET) {
1504 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001505 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001506 }
1507
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001508 // Top system windows are not requesting an orientation. Start searching from apps.
1509 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001510 }
1511
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001512 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -07001513 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001514 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001515 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1516 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001517 }
Craig Mautner722285e2012-09-07 13:55:58 -07001518 }
1519
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001520 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001521 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1522 if (displayManagerInternal != null) {
1523 // Bootstrap the default logical display from the display manager.
1524 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1525 if (newDisplayInfo != null) {
1526 mDisplayInfo.copyFrom(newDisplayInfo);
1527 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001528 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001529
Andrii Kulianb6c5c0d2017-03-22 21:50:24 +00001530 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1531 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1532 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
1533 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001534 }
1535
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001536 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001537 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001538 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001539 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001540 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1541 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001542 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001543 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001544 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001545 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001546 out.set(left, top, left + width, top + height);
1547 }
1548
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001549 private void getLogicalDisplayRect(Rect out, int orientation) {
1550 getLogicalDisplayRect(out);
1551
1552 // Rotate the Rect if needed.
1553 final int currentRotation = mDisplayInfo.rotation;
1554 final int rotationDelta = deltaRotation(currentRotation, orientation);
1555 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1556 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1557 mTmpRectF.set(out);
1558 mTmpMatrix.mapRect(mTmpRectF);
1559 mTmpRectF.round(out);
1560 }
1561 }
1562
Bryce Lee27cec322017-03-21 09:41:37 -07001563 /** Sets the maximum width the screen resolution can be */
1564 void setMaxUiWidth(int width) {
1565 if (DEBUG_DISPLAY) {
1566 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1567 }
1568
1569 mMaxUiWidth = width;
1570
1571 // Update existing metrics.
1572 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1573 }
1574
1575 /** Update base (override) display metrics. */
1576 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1577 mBaseDisplayWidth = baseWidth;
1578 mBaseDisplayHeight = baseHeight;
1579 mBaseDisplayDensity = baseDensity;
1580
1581 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1582 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1583 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1584 mBaseDisplayWidth = mMaxUiWidth;
1585
1586 if (DEBUG_DISPLAY) {
1587 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1588 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1589 + " on display:" + getDisplayId());
1590 }
1591 }
1592
1593 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1594 }
1595
Chong Zhangf66db432016-01-13 10:39:51 -08001596 void getContentRect(Rect out) {
1597 out.set(mContentRect);
1598 }
1599
Wale Ogunwale1666e312016-12-16 11:27:18 -08001600 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1601 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1602 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001603
Wale Ogunwale1666e312016-12-16 11:27:18 -08001604 TaskStack stack = getStackById(stackId);
1605 if (stack != null) {
1606 // It's already attached to the display...clear mDeferRemoval and move stack to
1607 // appropriate z-order on display as needed.
1608 stack.mDeferRemoval = false;
1609 // We're not moving the display to front when we're adding stacks, only when
1610 // requested to change the position of stack explicitly.
1611 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1612 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001613 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001614 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001615 mTaskStackContainers.addStackToDisplay(stack, onTop);
1616 }
1617
Wale Ogunwale1666e312016-12-16 11:27:18 -08001618 if (stackId == DOCKED_STACK_ID) {
1619 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001620 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001621 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001622 }
1623
Wale Ogunwale1666e312016-12-16 11:27:18 -08001624 void moveStackToDisplay(TaskStack stack) {
1625 final DisplayContent prevDc = stack.getDisplayContent();
1626 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001627 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1628 + " which is not currently attached to any display");
1629 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001630 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001631 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1632 + " to its current displayId=" + mDisplayId);
1633 }
1634
Wale Ogunwale1666e312016-12-16 11:27:18 -08001635 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
1636 mTaskStackContainers.addStackToDisplay(stack, true /* onTop */);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001637 }
1638
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001639 @Override
1640 protected void addChild(DisplayChildWindowContainer child,
1641 Comparator<DisplayChildWindowContainer> comparator) {
1642 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1643 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001644
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001645 @Override
1646 protected void addChild(DisplayChildWindowContainer child, int index) {
1647 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1648 }
1649
1650 @Override
1651 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001652 // Only allow removal of direct children from this display if the display is in the process
1653 // of been removed.
1654 if (mRemovingDisplay) {
1655 super.removeChild(child);
1656 return;
1657 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001658 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001659 }
1660
Andrii Kuliand2765632016-12-12 22:26:34 -08001661 @Override
1662 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1663 // Children of the display are statically ordered, so the real intention here is to perform
1664 // the operation on the display and not the static direct children.
1665 getParent().positionChildAt(position, this, includingParents);
1666 }
1667
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001668 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001669 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1670 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001671 final int taskId = stack.taskIdFromPoint(x, y);
1672 if (taskId != -1) {
1673 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001674 }
1675 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001676 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001677 }
1678
Chong Zhang8e89b312015-09-09 15:09:30 -07001679 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001680 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001681 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001682 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001683 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001684 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001685 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001686 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1687 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001688 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001689 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001690 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001691
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001692 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1693 if (mTmpTaskForResizePointSearchResult.searchDone) {
1694 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001695 }
1696 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001697 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001698 }
1699
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001700 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001701 // The provided task is the task on this display with focus, so if WindowManagerService's
1702 // focused app is not on this display, focusedTask will be null.
1703 if (focusedTask == null) {
1704 mTouchExcludeRegion.setEmpty();
1705 } else {
1706 mTouchExcludeRegion.set(mBaseDisplayRect);
1707 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1708 mTmpRect2.setEmpty();
1709 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1710 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1711 stack.setTouchExcludeRegion(
1712 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1713 }
1714 // If we removed the focused task above, add it back and only leave its
1715 // outside touch area in the exclusion. TapDectector is not interested in
1716 // any touch inside the focused task itself.
1717 if (!mTmpRect2.isEmpty()) {
1718 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1719 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001720 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001721 final WindowState inputMethod = mService.mInputMethodWindow;
1722 if (inputMethod != null && inputMethod.isVisibleLw()) {
1723 // If the input method is visible and the user is typing, we don't want these touch
1724 // events to be intercepted and used to change focus. This would likely cause a
1725 // disappearance of the input method.
1726 inputMethod.getTouchableRegion(mTmpRegion);
1727 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1728 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001729 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1730 WindowState win = mTapExcludedWindows.get(i);
1731 win.getTouchableRegion(mTmpRegion);
1732 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1733 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001734 // TODO(multi-display): Support docked stacks on secondary displays.
1735 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001736 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001737 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001738 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1739 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001740 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001741 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001742 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001743 }
1744
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001745 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001746 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001747 super.switchUser();
1748 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001749 }
1750
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001751 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001752 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1753 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001754 }
1755 }
1756
1757 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001758 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001759 }
1760
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001761 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001762 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001763 }
1764
1765 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001766 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001767 }
1768
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001769 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001770 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001771 }
1772
Wale Ogunwale10124582016-09-15 20:25:50 -07001773 @Override
1774 void removeIfPossible() {
1775 if (isAnimating()) {
1776 mDeferredRemoval = true;
1777 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001778 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001779 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001780 }
1781
Wale Ogunwale10124582016-09-15 20:25:50 -07001782 @Override
1783 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001784 mRemovingDisplay = true;
1785 try {
1786 super.removeImmediately();
1787 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1788 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001789 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001790 mService.unregisterPointerEventListener(mTapDetector);
1791 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1792 }
1793 } finally {
1794 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001795 }
Craig Mautner95da1082014-02-24 17:54:35 -08001796 }
1797
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001798 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001799 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001800 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001801 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1802
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001803 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001804 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001805 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001806 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001807 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001808 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001809 }
1810
Wale Ogunwale10124582016-09-15 20:25:50 -07001811 boolean animateForIme(float interpolatedValue, float animationTarget,
1812 float dividerAnimationTarget) {
1813 boolean updated = false;
1814
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001815 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1816 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001817 if (stack == null || !stack.isAdjustedForIme()) {
1818 continue;
1819 }
1820
1821 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1822 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1823 updated = true;
1824 } else {
1825 mDividerControllerLocked.mLastAnimationProgress =
1826 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1827 mDividerControllerLocked.mLastDividerProgress =
1828 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1829 updated |= stack.updateAdjustForIme(
1830 mDividerControllerLocked.mLastAnimationProgress,
1831 mDividerControllerLocked.mLastDividerProgress,
1832 false /* force */);
1833 }
1834 if (interpolatedValue >= 1f) {
1835 stack.endImeAdjustAnimation();
1836 }
1837 }
1838
1839 return updated;
1840 }
1841
1842 boolean clearImeAdjustAnimation() {
1843 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001844 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1845 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001846 if (stack != null && stack.isAdjustedForIme()) {
1847 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1848 changed = true;
1849 }
1850 }
1851 return changed;
1852 }
1853
1854 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001855 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1856 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001857 if (stack.isVisible() && stack.isAdjustedForIme()) {
1858 stack.beginImeAdjustAnimation();
1859 }
1860 }
1861 }
1862
1863 void adjustForImeIfNeeded() {
1864 final WindowState imeWin = mService.mInputMethodWindow;
1865 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1866 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001867 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001868 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1869 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1870 imeTargetStack.getDockSide() : DOCKED_INVALID;
1871 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1872 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1873 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1874 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1875 final boolean imeHeightChanged = imeVisible &&
1876 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1877
1878 // The divider could be adjusted for IME position, or be thinner than usual,
1879 // or both. There are three possible cases:
1880 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1881 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1882 // - If IME is not visible, divider is not moved and is normal width.
1883
1884 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001885 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1886 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001887 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
1888 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
1889 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1890 } else {
1891 stack.resetAdjustedForIme(false);
1892 }
1893 }
1894 mDividerControllerLocked.setAdjustedForIme(
1895 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1896 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001897 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1898 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001899 stack.resetAdjustedForIme(!dockVisible);
1900 }
1901 mDividerControllerLocked.setAdjustedForIme(
1902 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
1903 }
Winson Chung655332c2016-10-31 13:14:28 -07001904 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07001905 }
1906
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001907 void setInputMethodAnimLayerAdjustment(int adj) {
1908 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
1909 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08001910 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001911 }
1912
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001913 /**
1914 * If a window that has an animation specifying a colored background and the current wallpaper
1915 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
1916 * suddenly disappear.
1917 */
1918 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001919 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
1920 w -> w.mIsWallpaper && w.isVisibleNow());
1921
1922 if (visibleWallpaper != null) {
1923 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001924 }
1925 return winAnimator.mAnimLayer;
1926 }
1927
Wale Ogunwale10124582016-09-15 20:25:50 -07001928 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001929 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1930 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07001931 stack.prepareFreezingTaskBounds();
1932 }
1933 }
1934
Wale Ogunwale94744212015-09-21 19:01:47 -07001935 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001936 getLogicalDisplayRect(mTmpRect, newRotation);
1937
1938 // Compute a transform matrix to undo the coordinate space transformation,
1939 // and present the window at the same physical position it previously occupied.
1940 final int deltaRotation = deltaRotation(newRotation, oldRotation);
1941 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
1942
1943 mTmpRectF.set(bounds);
1944 mTmpMatrix.mapRect(mTmpRectF);
1945 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07001946 }
1947
Wale Ogunwale4a02d812015-02-12 23:01:38 -08001948 static int deltaRotation(int oldRotation, int newRotation) {
1949 int delta = newRotation - oldRotation;
1950 if (delta < 0) delta += 4;
1951 return delta;
1952 }
1953
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001954 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001955 Matrix outMatrix) {
1956 // For rotations without Z-ordering we don't need the target rectangle's position.
1957 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
1958 displayHeight, outMatrix);
1959 }
1960
1961 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
1962 float displayWidth, float displayHeight, Matrix outMatrix) {
1963 switch (rotation) {
1964 case ROTATION_0:
1965 outMatrix.reset();
1966 break;
1967 case ROTATION_270:
1968 outMatrix.setRotate(270, 0, 0);
1969 outMatrix.postTranslate(0, displayHeight);
1970 outMatrix.postTranslate(rectTop, 0);
1971 break;
1972 case ROTATION_180:
1973 outMatrix.reset();
1974 break;
1975 case ROTATION_90:
1976 outMatrix.setRotate(90, 0, 0);
1977 outMatrix.postTranslate(displayWidth, 0);
1978 outMatrix.postTranslate(-rectTop, rectLeft);
1979 break;
1980 }
1981 }
1982
Craig Mautnera91f9e22012-09-14 16:22:08 -07001983 public void dump(String prefix, PrintWriter pw) {
1984 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
1985 final String subPrefix = " " + prefix;
1986 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
1987 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
1988 pw.print("dpi");
1989 if (mInitialDisplayWidth != mBaseDisplayWidth
1990 || mInitialDisplayHeight != mBaseDisplayHeight
1991 || mInitialDisplayDensity != mBaseDisplayDensity) {
1992 pw.print(" base=");
1993 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
1994 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
1995 }
Jeff Brownd46747a2015-04-15 19:02:36 -07001996 if (mDisplayScalingDisabled) {
1997 pw.println(" noscale");
1998 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07001999 pw.print(" cur=");
2000 pw.print(mDisplayInfo.logicalWidth);
2001 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2002 pw.print(" app=");
2003 pw.print(mDisplayInfo.appWidth);
2004 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2005 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2006 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2007 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2008 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002009 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2010 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002011
Craig Mautnerdc548482014-02-05 13:35:24 -08002012 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002013 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002014 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2015 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002016 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002017 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002018
Craig Mautnerdc548482014-02-05 13:35:24 -08002019 pw.println();
2020 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002021 pw.println();
2022 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002023 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002024 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002025 pw.print(" Exiting #"); pw.print(i);
2026 pw.print(' '); pw.print(token);
2027 pw.println(':');
2028 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002029 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002030 }
Craig Mautner59c00972012-07-30 12:10:24 -07002031 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002032 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002033 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002034 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002035 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002036 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002037
2038 if (mInputMethodAnimLayerAdjustment != 0) {
2039 pw.println(subPrefix
2040 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2041 }
Craig Mautner59c00972012-07-30 12:10:24 -07002042 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002043
2044 @Override
2045 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002046 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002047 }
2048
2049 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002050 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002051 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002052
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002053 /** Checks if stack with provided id is visible on this display. */
2054 boolean isStackVisible(int stackId) {
2055 final TaskStack stack = getStackById(stackId);
2056 return (stack != null && stack.isVisible());
2057 }
2058
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002059 /**
2060 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2061 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002062 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002063 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002064 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002065 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002066
2067 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002068 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002069 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002070 */
Jorim Jaggife762342016-10-13 14:33:27 +02002071 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002072 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002073 }
2074
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002075 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002076 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002077 final int x = (int) xf;
2078 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002079 final WindowState touchedWin = getWindow(w -> {
2080 final int flags = w.mAttrs.flags;
2081 if (!w.isVisibleLw()) {
2082 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002083 }
2084 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002085 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002086 }
2087
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002088 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002089 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002090 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002091 }
2092
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002093 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002094
2095 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002096 return mTmpRegion.contains(x, y) || touchFlags == 0;
2097 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002098
2099 return touchedWin;
2100 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002101
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002102 boolean canAddToastWindowForUid(int uid) {
2103 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002104 // Also if the app is focused adding more than one toast at
2105 // a time for better backwards compatibility.
2106 final WindowState focusedWindowForUid = getWindow(w ->
2107 w.mOwnerUid == uid && w.isFocused());
2108 if (focusedWindowForUid != null) {
2109 return true;
2110 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002111 final WindowState win = getWindow(w ->
2112 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2113 && !w.mWindowRemovalAllowed);
2114 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002115 }
2116
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002117 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002118 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2119 return;
2120 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002121
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002122 // Used to communicate the old focus to the callback method.
2123 mTmpWindow = oldFocus;
2124
2125 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002126 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002127
2128 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002129 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002130
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002131 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002132
2133 if (mTmpWindow == null) {
2134 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2135 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002136 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002137 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002138 }
2139
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002140 /** Updates the layer assignment of windows on this display. */
2141 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002142 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002143 if (setLayoutNeeded) {
2144 setLayoutNeeded();
2145 }
2146 }
2147
Wale Ogunwale1666e312016-12-16 11:27:18 -08002148 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2149 // moving containers or resizing them. Need to investigate the best way to have it automatically
2150 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002151 void layoutAndAssignWindowLayersIfNeeded() {
2152 mService.mWindowsChanged = true;
2153 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002154
2155 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2156 false /*updateInputWindows*/)) {
2157 assignWindowLayers(false /* setLayoutNeeded */);
2158 }
2159
2160 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2161 mService.mWindowPlacerLocked.performSurfacePlacement();
2162 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2163 }
2164
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002165 /** Returns true if a leaked surface was destroyed */
2166 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002167 // Used to indicate that a surface was leaked.
2168 mTmpWindow = null;
2169 forAllWindows(w -> {
2170 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002171 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002172 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002173 }
2174 if (!mService.mSessions.contains(wsa.mSession)) {
2175 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002176 + w + " surface=" + wsa.mSurfaceController
2177 + " token=" + w.mToken
2178 + " pid=" + w.mSession.mPid
2179 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002180 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002181 mService.mForceRemoves.add(w);
2182 mTmpWindow = w;
2183 } else if (w.mAppToken != null && w.mAppToken.clientHidden) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002184 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002185 + w + " surface=" + wsa.mSurfaceController
2186 + " token=" + w.mAppToken
2187 + " saved=" + w.hasSavedSurface());
2188 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002189 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002190 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002191 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002192 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002193
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002194 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002195 }
2196
2197 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002198 * Determine and return the window that should be the IME target.
2199 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2200 * @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 +00002201 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002202 WindowState computeImeTarget(boolean updateImeTarget) {
2203 if (mService.mInputMethodWindow == null) {
2204 // There isn't an IME so there shouldn't be a target...That was easy!
2205 if (updateImeTarget) {
2206 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2207 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2208 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2209 }
2210 return null;
2211 }
2212
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002213 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2214 // same display. Or even when the current IME/target are not on the same screen as the next
2215 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002216 mUpdateImeTarget = updateImeTarget;
2217 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002218
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002219
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002220 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2221 // to be on top of it, but it is not -really- where input will go. So look down below
2222 // for a real window to target...
2223 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2224 final AppWindowToken token = target.mAppToken;
2225 if (token != null) {
2226 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2227 if (betterTarget != null) {
2228 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002229 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002230 }
2231 }
2232
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002233 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2234 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002235
2236 // Now, a special case -- if the last target's window is in the process of exiting, and is
2237 // above the new target, keep on the last target to avoid flicker. Consider for example a
2238 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2239 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2240 // scrim.
2241 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002242 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2243 && (target == null
2244 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002245 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002246 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002247 }
2248
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002249 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2250 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002251
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002252 if (target == null) {
2253 if (updateImeTarget) {
2254 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2255 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2256 + Debug.getCallers(4) : ""));
2257 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2258 }
2259
2260 return null;
2261 }
2262
2263 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002264 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2265 if (token != null) {
2266
2267 // Now some fun for dealing with window animations that modify the Z order. We need
2268 // to look at all windows below the current target that are in this app, finding the
2269 // highest visible one in layering.
2270 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002271 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002272 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002273 }
2274
2275 if (highestTarget != null) {
2276 final AppTransition appTransition = mService.mAppTransition;
2277 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2278 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2279 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002280 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002281
2282 if (appTransition.isTransitionSet()) {
2283 // If we are currently setting up for an animation, hold everything until we
2284 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002285 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2286 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002287 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002288 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002289 // If the window we are currently targeting is involved with an animation,
2290 // and it is on top of the next target we will be over, then hold off on
2291 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002292 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2293 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002294 }
2295 }
2296 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002297
2298 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2299 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2300 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002301 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002302 }
2303
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002304 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002305 }
2306
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002307 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2308 if (target == mService.mInputMethodTarget
2309 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2310 && mInputMethodAnimLayerAdjustment == layerAdj) {
2311 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002312 }
2313
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002314 mService.mInputMethodTarget = target;
2315 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2316 setInputMethodAnimLayerAdjustment(layerAdj);
2317 assignWindowLayers(false /* setLayoutNeeded */);
2318 }
2319
2320 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2321 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2322 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2323 }
2324
2325 // Used to indicate we have reached the first window in the range we are interested in.
2326 mTmpWindow = null;
2327
2328 // TODO: Figure-out a more efficient way to do this.
2329 final WindowState candidate = getWindow(w -> {
2330 if (w == top) {
2331 // Reached the first window in the range we are interested in.
2332 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002333 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002334 if (mTmpWindow == null) {
2335 return false;
2336 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002337
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002338 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2339 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002340 }
2341 // If we reached the bottom of the range of windows we are considering,
2342 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002343 if (w == bottom) {
2344 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002345 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002346 return false;
2347 });
2348
2349 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002350 }
2351
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002352 void setLayoutNeeded() {
2353 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2354 mLayoutNeeded = true;
2355 }
2356
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002357 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002358 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2359 mLayoutNeeded = false;
2360 }
2361
2362 boolean isLayoutNeeded() {
2363 return mLayoutNeeded;
2364 }
2365
Wale Ogunwale02319a62016-09-26 15:21:22 -07002366 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2367 if (mTokenMap.isEmpty()) {
2368 return;
2369 }
2370 pw.println(" Display #" + mDisplayId);
2371 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2372 while (it.hasNext()) {
2373 final WindowToken token = it.next();
2374 pw.print(" ");
2375 pw.print(token);
2376 if (dumpAll) {
2377 pw.println(':');
2378 token.dump(pw, " ");
2379 } else {
2380 pw.println();
2381 }
2382 }
2383 }
2384
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002385 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002386 final int[] index = new int[1];
2387 forAllWindows(w -> {
2388 final WindowStateAnimator wAnim = w.mWinAnimator;
2389 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2390 index[0] = index[0] + 1;
2391 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002392 }
2393
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002394 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002395 forAllWindows(w -> {
2396 w.mWinAnimator.enableSurfaceTrace(fd);
2397 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002398 }
2399
2400 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002401 forAllWindows(w -> {
2402 w.mWinAnimator.disableSurfaceTrace();
2403 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002404 }
2405
Jorim Jaggife762342016-10-13 14:33:27 +02002406 /**
2407 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2408 */
2409 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2410 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002411 forAllWindows(w -> {
2412 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
2413 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002414 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2415 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002416 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002417 }
2418
Wale Ogunwale494009b82016-10-21 09:01:38 -07002419 boolean checkWaitingForWindows() {
2420
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002421 mHaveBootMsg = false;
2422 mHaveApp = false;
2423 mHaveWallpaper = false;
2424 mHaveKeyguard = true;
2425
2426 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002427 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2428 return true;
2429 }
2430 if (w.isDrawnLw()) {
2431 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002432 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002433 } else if (w.mAttrs.type == TYPE_APPLICATION
2434 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002435 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002436 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002437 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002438 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002439 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002440 }
2441 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002442 return false;
2443 });
2444
2445 if (visibleWindow != null) {
2446 // We have a visible window.
2447 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002448 }
2449
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002450 // if the wallpaper service is disabled on the device, we're never going to have
2451 // wallpaper, don't bother waiting for it
2452 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2453 com.android.internal.R.bool.config_enableWallpaperService)
2454 && !mService.mOnlyCore;
2455
Wale Ogunwale494009b82016-10-21 09:01:38 -07002456 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2457 "******** booted=" + mService.mSystemBooted
2458 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2460 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2461 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002462
2463 // If we are turning on the screen to show the boot message, don't do it until the boot
2464 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002465 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002466 return true;
2467 }
2468
2469 // If we are turning on the screen after the boot is completed normally, don't do so until
2470 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002471 if (mService.mSystemBooted
2472 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002473 return true;
2474 }
2475
2476 return false;
2477 }
2478
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002479 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002480 mTmpWindowAnimator = animator;
2481 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002482 }
2483
2484 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002485 resetAnimationBackgroundAnimator();
2486
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487 // Used to indicate a detached wallpaper.
2488 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002489 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002490
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002491 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002492
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002493 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002494 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002495 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2496 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002497 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2498 }
2499 }
2500
2501 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002502 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002503 }
2504
Wale Ogunwale494009b82016-10-21 09:01:38 -07002505 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002506 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002507 if (imFocus == null) {
2508 return false;
2509 }
2510
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002511 if (DEBUG_INPUT_METHOD) {
2512 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2513 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2514 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002515 }
2516
Wale Ogunwale494009b82016-10-21 09:01:38 -07002517 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2518
2519 if (DEBUG_INPUT_METHOD) {
2520 Slog.i(TAG_WM, "IM target client: " + imeClient);
2521 if (imeClient != null) {
2522 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2523 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2524 }
2525 }
2526
2527 return imeClient != null && imeClient.asBinder() == client.asBinder();
2528 }
2529
2530 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002531 final WindowState win = getWindow(
2532 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2533 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002534 }
2535
2536 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002537 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002538 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002539 final int curValue = w.mSystemUiVisibility;
2540 final int diff = (curValue ^ visibility) & globalDiff;
2541 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002542 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002543 w.mSeq++;
2544 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002545 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002546 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2547 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002548 visibility, newValue, diff);
2549 }
2550 } catch (RemoteException e) {
2551 // so sorry
2552 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002553 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002554 }
2555
2556 void onWindowFreezeTimeout() {
2557 Slog.w(TAG_WM, "Window freeze timeout expired.");
2558 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002559
2560 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002561 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002562 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002563 }
2564 w.mOrientationChanging = false;
2565 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2566 - mService.mDisplayFreezeTime);
2567 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002568 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002569 mService.mWindowPlacerLocked.performSurfacePlacement();
2570 }
2571
2572 void waitForAllWindowsDrawn() {
2573 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002574 forAllWindows(w -> {
2575 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2576 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2577 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002578 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002579 w.mLastContentInsets.set(-1, -1, -1, -1);
2580 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002581 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002582 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002583 }
2584
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002585 // TODO: Super crazy long method that should be broken down...
2586 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2587
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002588 final int dw = mDisplayInfo.logicalWidth;
2589 final int dh = mDisplayInfo.logicalHeight;
2590 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2591
2592 mTmpUpdateAllDrawn.clear();
2593
2594 int repeats = 0;
2595 do {
2596 repeats++;
2597 if (repeats > 6) {
2598 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2599 clearLayoutNeeded();
2600 break;
2601 }
2602
2603 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2604 pendingLayoutChanges);
2605
Andrii Kulian839def92016-11-02 10:58:58 -07002606 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2607 // the wallpaper window jumping across displays.
2608 // Remove check for default display when there will be support for multiple wallpaper
2609 // targets (on different displays).
2610 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002612 }
2613
2614 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2615 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2616 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2617 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002618 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002619 }
2620 }
2621
2622 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2623 setLayoutNeeded();
2624 }
2625
2626 // FIRST LOOP: Perform a layout, if needed.
2627 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2628 performLayout(repeats == 1, false /* updateInputWindows */);
2629 } else {
2630 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2631 }
2632
2633 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2634 pendingLayoutChanges = 0;
2635
2636 if (isDefaultDisplay) {
2637 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002638 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002639 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2640 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2641 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2642 }
2643 } while (pendingLayoutChanges != 0);
2644
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002645 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002646 resetDimming();
2647
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002648 mTmpRecoveringMemory = recoveringMemory;
2649 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002650
2651 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002652 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2653 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2654 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002655 true /* inTraversal, must call performTraversalInTrans... below */);
2656
2657 stopDimmingIfNeeded();
2658
2659 while (!mTmpUpdateAllDrawn.isEmpty()) {
2660 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2661 // See if any windows have been drawn, so they (and others associated with them)
2662 // can now be shown.
2663 atoken.updateAllDrawn(this);
2664 }
2665
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002667 }
2668
2669 void performLayout(boolean initial, boolean updateInputWindows) {
2670 if (!isLayoutNeeded()) {
2671 return;
2672 }
2673 clearLayoutNeeded();
2674
2675 final int dw = mDisplayInfo.logicalWidth;
2676 final int dh = mDisplayInfo.logicalHeight;
2677
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002678 if (DEBUG_LAYOUT) {
2679 Slog.v(TAG, "-------------------------------------");
2680 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2681 }
2682
Andrii Kulian8ee72852017-03-10 10:36:45 -08002683 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002684 getConfiguration().uiMode);
2685 if (isDefaultDisplay) {
2686 // Not needed on non-default displays.
2687 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2688 mService.mScreenRect.set(0, 0, dw, dh);
2689 }
2690
2691 mService.mPolicy.getContentRectLw(mContentRect);
2692
2693 int seq = mService.mLayoutSeq + 1;
2694 if (seq < 0) seq = 0;
2695 mService.mLayoutSeq = seq;
2696
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002697 // Used to indicate that we have processed the dream window and all additional windows are
2698 // behind it.
2699 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002700 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002701
2702 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002703 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002704
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002705 // Used to indicate that we have processed the dream window and all additional attached
2706 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002707 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002708 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002709
2710 // Now perform layout of attached windows, which usually depend on the position of the
2711 // window they are attached to. XXX does not deal with windows that are attached to windows
2712 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002713 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002714
2715 // Window frames may have changed. Tell the input dispatcher about it.
2716 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2717 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2718 if (updateInputWindows) {
2719 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2720 }
2721
2722 mService.mPolicy.finishLayoutLw();
2723 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2724 }
2725
2726 /**
2727 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2728 * In portrait mode, it grabs the full screenshot.
2729 *
2730 * @param width the width of the target bitmap
2731 * @param height the height of the target bitmap
2732 * @param includeFullDisplay true if the screen should not be cropped before capture
2733 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2734 * @param config of the output bitmap
2735 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002736 * @param includeDecor whether to include window decors, like the status or navigation bar
2737 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002738 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002739 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002740 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002741 boolean wallpaperOnly, boolean includeDecor) {
2742 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2743 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002744 if (bitmap == null) {
2745 return null;
2746 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002747
2748 if (DEBUG_SCREENSHOT) {
2749 // TEST IF IT's ALL BLACK
2750 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2751 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2752 bitmap.getHeight());
2753 boolean allBlack = true;
2754 final int firstColor = buffer[0];
2755 for (int i = 0; i < buffer.length; i++) {
2756 if (buffer[i] != firstColor) {
2757 allBlack = false;
2758 break;
2759 }
2760 }
2761 if (allBlack) {
2762 final WindowState appWin = mScreenshotApplicationState.appWin;
2763 final int maxLayer = mScreenshotApplicationState.maxLayer;
2764 final int minLayer = mScreenshotApplicationState.minLayer;
2765 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2766 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2767 (appWin != null ?
2768 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2769 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2770 }
2771 }
2772
2773 // Create a copy of the screenshot that is immutable and backed in ashmem.
2774 // This greatly reduces the overhead of passing the bitmap between processes.
2775 Bitmap ret = bitmap.createAshmemBitmap(config);
2776 bitmap.recycle();
2777 return ret;
2778 }
2779
2780 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2781 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2782 boolean includeDecor) {
2783 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2784 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2785 }
2786
2787 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2788 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2789 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002790 int dw = mDisplayInfo.logicalWidth;
2791 int dh = mDisplayInfo.logicalHeight;
2792 if (dw == 0 || dh == 0) {
2793 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2794 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2795 return null;
2796 }
2797
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002798 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002799
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002800 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002801 final Rect frame = new Rect();
2802 final Rect stackBounds = new Rect();
2803
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002804 boolean includeImeInScreenshot;
2805 synchronized(mService.mWindowMap) {
2806 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2807 ? mService.mInputMethodTarget.mAppToken : null;
2808 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2809 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2810 // mode because the frame of the IME might not overlap with that of the app.
2811 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2812 // overlapping with the bottom app.
2813 includeImeInScreenshot = imeTargetAppToken != null
2814 && imeTargetAppToken.appToken != null
2815 && imeTargetAppToken.appToken.asBinder() == appToken
2816 && !mService.mInputMethodTarget.isInMultiWindowMode();
2817 }
2818
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002819 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002820 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002821 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002822 synchronized(mService.mWindowMap) {
2823 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002824 mScreenshotApplicationState.appWin = null;
2825 forAllWindows(w -> {
2826 if (!w.mHasSurface) {
2827 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002828 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002829 if (w.mLayer >= aboveAppLayer) {
2830 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002831 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002832 if (wallpaperOnly && !w.mIsWallpaper) {
2833 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002834 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002835 if (w.mIsImWindow) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002836 if (!includeImeInScreenshot) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002837 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002838 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002839 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002840 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2841 // then the target window state is this one.
2842 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002843 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002844 }
2845
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002846 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002847 // We have not ran across the target window yet, so it is probably behind
2848 // the wallpaper. This can happen when the keyguard is up and all windows
2849 // are moved behind the wallpaper. We don't want to include the wallpaper
2850 // layer in the screenshot as it will cover-up the layer of the target
2851 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002852 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002853 }
2854 // Fall through. The target window is in front of the wallpaper. For this
2855 // case we want to include the wallpaper layer in the screenshot because
2856 // the target window might have some transparent areas.
2857 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002858 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002859 // This app window is of no interest if it is not associated with the
2860 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002861 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002862 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002863 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002864 }
2865
2866 // Include this window.
2867
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002868 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002869 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002870 if (mScreenshotApplicationState.maxLayer < layer) {
2871 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002872 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002873 if (mScreenshotApplicationState.minLayer > layer) {
2874 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002875 }
2876
2877 // Don't include wallpaper in bounds calculation
Jorim Jaggibfbd9dc2017-01-20 18:13:01 +01002878 if (!mutableIncludeFullDisplay.value && includeDecor) {
2879 final TaskStack stack = w.getStack();
2880 if (stack != null) {
2881 stack.getBounds(frame);
2882 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08002883 } else if (!mutableIncludeFullDisplay.value && !w.mIsWallpaper) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002884 final Rect wf = w.mFrame;
2885 final Rect cr = w.mContentInsets;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002886 int left = wf.left + cr.left;
2887 int top = wf.top + cr.top;
2888 int right = wf.right - cr.right;
2889 int bottom = wf.bottom - cr.bottom;
2890 frame.union(left, top, right, bottom);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002891 w.getVisibleBounds(stackBounds);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002892 if (!Rect.intersects(frame, stackBounds)) {
2893 // Set frame empty if there's no intersection.
2894 frame.setEmpty();
2895 }
2896 }
2897
2898 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002899 (w.mAppToken != null && w.mAppToken.token == appToken)
2900 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002901 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002902 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002903 }
2904
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002905 if (w.isObscuringDisplay()){
2906 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002907 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002908 return false;
2909 }, true /* traverseTopToBottom */);
2910
2911 final WindowState appWin = mScreenshotApplicationState.appWin;
2912 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2913 final int maxLayer = mScreenshotApplicationState.maxLayer;
2914 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002915
2916 if (appToken != null && appWin == null) {
2917 // Can't find a window to snapshot.
2918 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2919 "Screenshot: Couldn't find a surface matching " + appToken);
2920 return null;
2921 }
2922
2923 if (!screenshotReady) {
2924 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2925 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2926 appWin.mWinAnimator.mDrawState)));
2927 return null;
2928 }
2929
2930 // Screenshot is ready to be taken. Everything from here below will continue
2931 // through the bottom of the loop and return a value. We only stay in the loop
2932 // because we don't want to release the mWindowMap lock until the screenshot is
2933 // taken.
2934
2935 if (maxLayer == 0) {
2936 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2937 + ": returning null maxLayer=" + maxLayer);
2938 return null;
2939 }
2940
Jorim Jaggi02886a82016-12-06 09:10:06 -08002941 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002942 // Constrain frame to the screen size.
2943 if (!frame.intersect(0, 0, dw, dh)) {
2944 frame.setEmpty();
2945 }
2946 } else {
2947 // Caller just wants entire display.
2948 frame.set(0, 0, dw, dh);
2949 }
2950 if (frame.isEmpty()) {
2951 return null;
2952 }
2953
2954 if (width < 0) {
2955 width = (int) (frame.width() * frameScale);
2956 }
2957 if (height < 0) {
2958 height = (int) (frame.height() * frameScale);
2959 }
2960
2961 // Tell surface flinger what part of the image to crop. Take the top
2962 // right part of the application, and crop the larger dimension to fit.
2963 Rect crop = new Rect(frame);
2964 if (width / (float) frame.width() < height / (float) frame.height()) {
2965 int cropWidth = (int)((float)width / (float)height * frame.height());
2966 crop.right = crop.left + cropWidth;
2967 } else {
2968 int cropHeight = (int)((float)height / (float)width * frame.width());
2969 crop.bottom = crop.top + cropHeight;
2970 }
2971
2972 // The screenshot API does not apply the current screen rotation.
2973 int rot = mDisplay.getRotation();
2974
2975 if (rot == ROTATION_90 || rot == ROTATION_270) {
2976 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2977 }
2978
2979 // Surfaceflinger is not aware of orientation, so convert our logical
2980 // crop to surfaceflinger's portrait orientation.
2981 convertCropForSurfaceFlinger(crop, rot, dw, dh);
2982
2983 if (DEBUG_SCREENSHOT) {
2984 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
2985 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002986 forAllWindows(w -> {
2987 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
2988 Slog.i(TAG_WM, w + ": " + w.mLayer
2989 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002990 + " surfaceLayer=" + ((controller == null)
2991 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002992 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002993 }
2994
2995 final ScreenRotationAnimation screenRotationAnimation =
2996 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2997 final boolean inRotation = screenRotationAnimation != null &&
2998 screenRotationAnimation.isAnimating();
2999 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3000 "Taking screenshot while rotating");
3001
3002 // We force pending transactions to flush before taking
3003 // the screenshot by pushing an empty synchronous transaction.
3004 SurfaceControl.openTransaction();
3005 SurfaceControl.closeTransactionSync();
3006
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003007 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003008 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003009 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003010 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3011 + ") to layer " + maxLayer);
3012 return null;
3013 }
3014 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003015 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003016 }
3017
3018 // TODO: Can this use createRotationMatrix()?
3019 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3020 if (rot == Surface.ROTATION_90) {
3021 final int tmp = crop.top;
3022 crop.top = dw - crop.right;
3023 crop.right = crop.bottom;
3024 crop.bottom = dw - crop.left;
3025 crop.left = tmp;
3026 } else if (rot == Surface.ROTATION_180) {
3027 int tmp = crop.top;
3028 crop.top = dh - crop.bottom;
3029 crop.bottom = dh - tmp;
3030 tmp = crop.right;
3031 crop.right = dw - crop.left;
3032 crop.left = dw - tmp;
3033 } else if (rot == Surface.ROTATION_270) {
3034 final int tmp = crop.top;
3035 crop.top = crop.left;
3036 crop.left = dh - crop.bottom;
3037 crop.bottom = crop.right;
3038 crop.right = dh - tmp;
3039 }
3040 }
3041
3042 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003043 // Used to indicate the layout is needed.
3044 mTmpWindow = null;
3045
3046 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003047 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003048 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003049 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003050 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003051 w.setDisplayLayoutNeeded();
3052 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003053 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003054
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003055 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003056 mService.mWindowPlacerLocked.performSurfacePlacement();
3057 }
3058 }
3059
Wale Ogunwale1666e312016-12-16 11:27:18 -08003060 void setExitingTokensHasVisible(boolean hasVisible) {
3061 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3062 mExitingTokens.get(i).hasVisible = hasVisible;
3063 }
3064
3065 // Initialize state of exiting applications.
3066 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3067 }
3068
3069 void removeExistingTokensIfPossible() {
3070 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3071 final WindowToken token = mExitingTokens.get(i);
3072 if (!token.hasVisible) {
3073 mExitingTokens.remove(i);
3074 }
3075 }
3076
3077 // Time to remove any exiting applications?
3078 mTaskStackContainers.removeExistingAppTokensIfPossible();
3079 }
3080
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003081 @Override
3082 void onDescendantOverrideConfigurationChanged() {
3083 setLayoutNeeded();
3084 mService.requestTraversal();
3085 }
3086
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003087 static final class TaskForResizePointSearchResult {
3088 boolean searchDone;
3089 Task taskForResize;
3090
3091 void reset() {
3092 searchDone = false;
3093 taskForResize = null;
3094 }
3095 }
Robert Carr3b716242016-08-16 16:02:21 -07003096
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003097 private static final class ApplySurfaceChangesTransactionState {
3098 boolean displayHasContent;
3099 boolean obscured;
3100 boolean syswin;
3101 boolean focusDisplayed;
3102 float preferredRefreshRate;
3103 int preferredModeId;
3104
3105 void reset() {
3106 displayHasContent = false;
3107 obscured = false;
3108 syswin = false;
3109 focusDisplayed = false;
3110 preferredRefreshRate = 0;
3111 preferredModeId = 0;
3112 }
3113 }
3114
3115 private static final class ScreenshotApplicationState {
3116 WindowState appWin;
3117 int maxLayer;
3118 int minLayer;
3119 boolean screenshotReady;
3120
3121 void reset(boolean screenshotReady) {
3122 appWin = null;
3123 maxLayer = 0;
3124 minLayer = 0;
3125 this.screenshotReady = screenshotReady;
3126 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3127 }
3128 }
3129
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003130 /**
3131 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3132 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3133 * homogeneous children type which is currently required by sub-classes of
3134 * {@link WindowContainer} class.
3135 */
3136 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3137
3138 int size() {
3139 return mChildren.size();
3140 }
3141
3142 E get(int index) {
3143 return mChildren.get(index);
3144 }
3145
3146 @Override
3147 boolean fillsParent() {
3148 return true;
3149 }
3150
3151 @Override
3152 boolean isVisible() {
3153 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003154 }
3155 }
3156
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003157 /**
3158 * Window container class that contains all containers on this display relating to Apps.
3159 * I.e Activities.
3160 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003161 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003162
Andrii Kulian839def92016-11-02 10:58:58 -07003163 /**
3164 * Adds the stack to this container.
3165 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3166 */
3167 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003168 if (stack.mStackId == HOME_STACK_ID) {
3169 if (mHomeStack != null) {
3170 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3171 }
3172 mHomeStack = stack;
3173 }
3174 addChild(stack, onTop);
3175 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003176 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003177
Andrii Kulian839def92016-11-02 10:58:58 -07003178 /** Removes the stack from its container and prepare for changing the parent. */
3179 void removeStackFromDisplay(TaskStack stack) {
3180 removeChild(stack);
3181 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003182 }
3183
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003184 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003185 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3186 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003187 addChild(stack, addIndex);
3188 setLayoutNeeded();
3189 }
3190
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003191 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003192 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3193 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3194 // This stack is always-on-top, override the default behavior.
3195 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3196
3197 // Moving to its current position, as we must call super but we don't want to
3198 // perform any meaningful action.
3199 final int currentPosition = mChildren.indexOf(child);
3200 super.positionChildAt(currentPosition, child, false /* includingParents */);
3201 return;
3202 }
3203
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003204 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3205 super.positionChildAt(targetPosition, child, includingParents);
3206
3207 setLayoutNeeded();
3208 }
3209
3210 /**
3211 * When stack is added or repositioned, find a proper position for it.
3212 * This will make sure that pinned stack always stays on top.
3213 * @param requestedPosition Position requested by caller.
3214 * @param stack Stack to be added or positioned.
3215 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3216 * @return The proper position for the stack.
3217 */
3218 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3219 final int topChildPosition = mChildren.size() - 1;
3220 boolean toTop = requestedPosition == POSITION_TOP;
3221 toTop |= adding ? requestedPosition >= topChildPosition + 1
3222 : requestedPosition >= topChildPosition;
3223 int targetPosition = requestedPosition;
3224
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003225 if (toTop && isStackVisible(PINNED_STACK_ID) && stack.mStackId != PINNED_STACK_ID) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003226 // The pinned stack is always the top most stack (always-on-top) when it is visible.
3227 TaskStack topStack = mChildren.get(topChildPosition);
3228 if (topStack.mStackId != PINNED_STACK_ID) {
3229 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3230 }
3231
3232 // So, stack is moved just below the pinned stack.
3233 // When we're adding a new stack the target is the current pinned stack position.
3234 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003235 // stack, because WindowContainer#positionAt() first removes element and then adds
3236 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003237 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3238 }
3239
3240 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003241 }
3242
3243 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003244 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3245 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003246 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003247 if (super.forAllWindows(callback, traverseTopToBottom)) {
3248 return true;
3249 }
3250 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3251 return true;
3252 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003253 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003254 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3255 return true;
3256 }
3257 if (super.forAllWindows(callback, traverseTopToBottom)) {
3258 return true;
3259 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003260 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003261 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003262 }
3263
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003264 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003265 boolean traverseTopToBottom) {
3266 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3267 // app tokens.
3268 // TODO: Investigate if we need to continue to do this or if we can just process them
3269 // in-order.
3270 if (traverseTopToBottom) {
3271 for (int i = mChildren.size() - 1; i >= 0; --i) {
3272 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3273 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003274 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3275 traverseTopToBottom)) {
3276 return true;
3277 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003278 }
3279 }
3280 } else {
3281 final int count = mChildren.size();
3282 for (int i = 0; i < count; ++i) {
3283 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3284 final int appTokensCount = appTokens.size();
3285 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003286 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3287 traverseTopToBottom)) {
3288 return true;
3289 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003290 }
3291 }
3292 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003293 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003294 }
3295
Wale Ogunwale1666e312016-12-16 11:27:18 -08003296 void setExitingTokensHasVisible(boolean hasVisible) {
3297 for (int i = mChildren.size() - 1; i >= 0; --i) {
3298 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3299 for (int j = appTokens.size() - 1; j >= 0; --j) {
3300 appTokens.get(j).hasVisible = hasVisible;
3301 }
3302 }
3303 }
3304
3305 void removeExistingAppTokensIfPossible() {
3306 for (int i = mChildren.size() - 1; i >= 0; --i) {
3307 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3308 for (int j = appTokens.size() - 1; j >= 0; --j) {
3309 final AppWindowToken token = appTokens.get(j);
3310 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3311 && (!token.mIsExiting || token.isEmpty())) {
3312 // Make sure there is no animation running on this token, so any windows
3313 // associated with it will be removed as soon as their animations are
3314 // complete.
3315 token.mAppAnimator.clearAnimation();
3316 token.mAppAnimator.animating = false;
3317 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3318 "performLayout: App token exiting now removed" + token);
3319 token.removeIfPossible();
3320 }
3321 }
3322 }
3323 }
3324
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003325 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003326 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003327 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003328 // Apps and their containers are not allowed to specify an orientation while the
3329 // docked or freeform stack is visible...except for the home stack/task if the
3330 // docked stack is minimized and it actually set something.
3331 if (mHomeStack != null && mHomeStack.isVisible()
3332 && mDividerControllerLocked.isMinimizedDock()) {
3333 final int orientation = mHomeStack.getOrientation();
3334 if (orientation != SCREEN_ORIENTATION_UNSET) {
3335 return orientation;
3336 }
3337 }
3338 return SCREEN_ORIENTATION_UNSPECIFIED;
3339 }
3340
3341 final int orientation = super.getOrientation();
3342 if (orientation != SCREEN_ORIENTATION_UNSET
3343 && orientation != SCREEN_ORIENTATION_BEHIND) {
3344 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3345 "App is requesting an orientation, return " + orientation);
3346 return orientation;
3347 }
3348
3349 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003350 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003351 // The next app has not been requested to be visible, so we keep the current orientation
3352 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003353 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003354 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003355 }
3356
3357 /**
3358 * Window container class that contains all containers on this display that are not related to
3359 * Apps. E.g. status bar.
3360 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003361 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3362 /**
3363 * Compares two child window tokens returns -1 if the first is lesser than the second in
3364 * terms of z-order and 1 otherwise.
3365 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003366 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003367 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003368 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3369 token1.mOwnerCanManageAppTokens)
3370 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3371 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003372
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003373 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3374 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3375 return false;
3376 }
3377 final int req = w.mAttrs.screenOrientation;
3378 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3379 || req == SCREEN_ORIENTATION_UNSET) {
3380 return false;
3381 }
3382 return true;
3383 };
3384
Wale Ogunwale3a931692016-11-02 16:49:48 -07003385 private final String mName;
3386 NonAppWindowContainers(String name) {
3387 mName = name;
3388 }
3389
3390 void addChild(WindowToken token) {
3391 addChild(token, mWindowComparator);
3392 }
3393
3394 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003395 int getOrientation() {
3396 final WindowManagerPolicy policy = mService.mPolicy;
3397 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003398 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003399
3400 if (win != null) {
3401 final int req = win.mAttrs.screenOrientation;
3402 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
3403 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003404 mLastKeyguardForcedOrientation = req;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003405 }
Andrii Kulian8ee72852017-03-10 10:36:45 -08003406 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003407 }
3408
Andrii Kulian8ee72852017-03-10 10:36:45 -08003409 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003410
3411 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003412 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003413 }
3414
3415 return SCREEN_ORIENTATION_UNSET;
3416 }
3417
3418 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003419 String getName() {
3420 return mName;
3421 }
Robert Carr3b716242016-08-16 16:02:21 -07003422 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003423
3424 /**
3425 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3426 */
3427 @FunctionalInterface
3428 private interface Screenshoter<E> {
3429 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3430 boolean useIdentityTransform, int rotation);
3431 }
Craig Mautner59c00972012-07-30 12:10:24 -07003432}