blob: e5b00f3df79a2440aebac40dc65f52ac20a162c0 [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;
Craig Mautner59c00972012-07-30 12:10:24 -0700178
Wale Ogunwale02319a62016-09-26 15:21:22 -0700179 // Mapping from a token IBinder to a WindowToken object on this display.
180 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
181
Craig Mautner59c00972012-07-30 12:10:24 -0700182 int mInitialDisplayWidth = 0;
183 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700184 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -0700185 int mBaseDisplayWidth = 0;
186 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700187 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700188 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700189 private final DisplayInfo mDisplayInfo = new DisplayInfo();
190 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700191 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700192 /**
193 * For default display it contains real metrics, empty for others.
194 * @see WindowManagerService#createWatermarkInTransaction()
195 */
196 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
197 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
198 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
199 /**
200 * Compat metrics computed based on {@link #mDisplayMetrics}.
201 * @see #updateDisplayAndOrientation(int)
202 */
203 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
204
205 /** The desired scaling factor for compatible apps. */
206 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700207
Andrii Kulian8ee72852017-03-10 10:36:45 -0800208 /**
209 * Current rotation of the display.
210 * Constants as per {@link android.view.Surface.Rotation}.
211 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700212 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800213 */
214 private int mRotation = 0;
215 /**
216 * Last applied orientation of the display.
217 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
218 *
219 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
220 */
221 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
222 /**
223 * Flag indicating that the application is receiving an orientation that has different metrics
224 * than it expected. E.g. Portrait instead of Landscape.
225 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700226 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800227 */
228 private boolean mAltOrientation = false;
229 /**
230 * Orientation forced by some window. If there is no visible window that specifies orientation
231 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
232 *
233 * @see NonAppWindowContainers#getOrientation()
234 */
235 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
236 /**
237 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
238 * occluded.
239 *
240 * @see NonAppWindowContainers#getOrientation()
241 */
242 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
243
Andrii Kulian06d07d62017-03-14 11:11:47 -0700244 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700245 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700246
Craig Mautner39834192012-09-02 07:47:24 -0700247 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700248 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700249 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700250 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800251 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700252
Craig Mautnerdc548482014-02-05 13:35:24 -0800253 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700254 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800255
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800256 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
257 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700258 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700259
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700260 /** Detect user tapping outside of current focused task bounds .*/
261 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700262
Craig Mautner6601b7b2013-04-29 10:29:11 -0700263 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700264 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700265
Craig Mautner6601b7b2013-04-29 10:29:11 -0700266 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800267 private final Rect mTmpRect = new Rect();
268 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700269 private final RectF mTmpRectF = new RectF();
270 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800271 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700272
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800273 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700274
Craig Mautner95da1082014-02-24 17:54:35 -0800275 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700276 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800277
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700278 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700279 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700280
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800281 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700282
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800283 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
284
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000285 private boolean mHaveBootMsg = false;
286 private boolean mHaveApp = false;
287 private boolean mHaveWallpaper = false;
288 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700289
290 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
291
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700292 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
293 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000294 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
295 new ApplySurfaceChangesTransactionState();
296 private final ScreenshotApplicationState mScreenshotApplicationState =
297 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700298
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700299 // True if this display is in the process of being removed. Used to determine if the removal of
300 // the display's direct children should be allowed.
301 private boolean mRemovingDisplay = false;
302
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700303 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800304 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700305 int mInputMethodAnimLayerAdjustment;
306
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800307 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
308 WindowStateAnimator winAnimator = w.mWinAnimator;
309 if (winAnimator.hasSurface()) {
310 final boolean wasAnimating = winAnimator.mWasAnimating;
311 final boolean nowAnimating = winAnimator.stepAnimationLocked(
312 mTmpWindowAnimator.mCurrentTime);
313 winAnimator.mWasAnimating = nowAnimating;
314 mTmpWindowAnimator.orAnimating(nowAnimating);
315
316 if (DEBUG_WALLPAPER) Slog.v(TAG,
317 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
318
319 if (wasAnimating && !winAnimator.mAnimating
320 && mWallpaperController.isWallpaperTarget(w)) {
321 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
322 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
323 if (DEBUG_LAYOUT_REPEATS) {
324 mService.mWindowPlacerLocked.debugLayoutRepeats(
325 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
326 }
327 }
328 }
329
330 final AppWindowToken atoken = w.mAppToken;
331 if (winAnimator.mDrawState == READY_TO_SHOW) {
332 if (atoken == null || atoken.allDrawn) {
333 if (w.performShowLocked()) {
334 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
335 if (DEBUG_LAYOUT_REPEATS) {
336 mService.mWindowPlacerLocked.debugLayoutRepeats(
337 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
338 }
339 }
340 }
341 }
342 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
343 if (appAnimator != null && appAnimator.thumbnail != null) {
344 if (appAnimator.thumbnailTransactionSeq
345 != mTmpWindowAnimator.mAnimTransactionSequence) {
346 appAnimator.thumbnailTransactionSeq =
347 mTmpWindowAnimator.mAnimTransactionSequence;
348 appAnimator.thumbnailLayer = 0;
349 }
350 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
351 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
352 }
353 }
354 };
355
356 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
357 final WindowStateAnimator winAnimator = w.mWinAnimator;
358 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
359 return;
360 }
361
362 final int flags = w.mAttrs.flags;
363
364 // If this window is animating, make a note that we have an animating window and take
365 // care of a request to run a detached wallpaper animation.
366 if (winAnimator.mAnimating) {
367 if (winAnimator.mAnimation != null) {
368 if ((flags & FLAG_SHOW_WALLPAPER) != 0
369 && winAnimator.mAnimation.getDetachWallpaper()) {
370 mTmpWindow = w;
371 }
372 final int color = winAnimator.mAnimation.getBackgroundColor();
373 if (color != 0) {
374 final TaskStack stack = w.getStack();
375 if (stack != null) {
376 stack.setAnimationBackground(winAnimator, color);
377 }
378 }
379 }
380 mTmpWindowAnimator.setAnimating(true);
381 }
382
383 // If this window's app token is running a detached wallpaper animation, make a note so
384 // we can ensure the wallpaper is displayed behind it.
385 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
386 if (appAnimator != null && appAnimator.animation != null
387 && appAnimator.animating) {
388 if ((flags & FLAG_SHOW_WALLPAPER) != 0
389 && appAnimator.animation.getDetachWallpaper()) {
390 mTmpWindow = w;
391 }
392
393 final int color = appAnimator.animation.getBackgroundColor();
394 if (color != 0) {
395 final TaskStack stack = w.getStack();
396 if (stack != null) {
397 stack.setAnimationBackground(winAnimator, color);
398 }
399 }
400 }
401 };
402
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800403 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
404 final int lostFocusUid = mTmpWindow.mOwnerUid;
405 final Handler handler = mService.mH;
406 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
407 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
408 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
409 w.mAttrs.hideTimeoutMilliseconds);
410 }
411 }
412 };
413
414 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
415 final AppWindowToken focusedApp = mService.mFocusedApp;
416 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
417 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
418
419 if (!w.canReceiveKeys()) {
420 return false;
421 }
422
423 final AppWindowToken wtoken = w.mAppToken;
424
425 // If this window's application has been removed, just skip it.
426 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
427 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
428 + (wtoken.removed ? "removed" : "sendingToBottom"));
429 return false;
430 }
431
432 if (focusedApp == null) {
433 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
434 + " using new focus @ " + w);
435 mTmpWindow = w;
436 return true;
437 }
438
439 if (!focusedApp.windowsAreFocusable()) {
440 // Current focused app windows aren't focusable...
441 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
442 + " focusable using new focus @ " + w);
443 mTmpWindow = w;
444 return true;
445 }
446
447 // Descend through all of the app tokens and find the first that either matches
448 // win.mAppToken (return win) or mFocusedApp (return null).
449 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
450 if (focusedApp.compareTo(wtoken) > 0) {
451 // App stack below focused app stack. No focus for you!!!
452 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
453 "findFocusedWindow: Reached focused app=" + focusedApp);
454 mTmpWindow = null;
455 return true;
456 }
457 }
458
459 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
460 mTmpWindow = w;
461 return true;
462 };
463
464 private final Consumer<WindowState> mPrepareWindowSurfaces =
465 w -> w.mWinAnimator.prepareSurfaceLocked(true);
466
467 private final Consumer<WindowState> mPerformLayout = w -> {
468 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
469 // wasting time and funky changes while a window is animating away.
470 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
471 || w.isGoneForLayoutLw();
472
473 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
474 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
475 + " mLayoutAttached=" + w.mLayoutAttached
476 + " screen changed=" + w.isConfigChanged());
477 final AppWindowToken atoken = w.mAppToken;
478 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
479 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
480 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
481 + " parentHidden=" + w.isParentWindowHidden());
482 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
483 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
484 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
485 + " parentHidden=" + w.isParentWindowHidden());
486 }
487
488 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
489 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
490 // since that means "perform layout as normal, just don't display").
491 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
492 || ((w.isConfigChanged() || w.setReportResizeHints())
493 && !w.isGoneForLayoutLw() &&
494 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
495 (w.mHasSurface && w.mAppToken != null &&
496 w.mAppToken.layoutConfigChanges)))) {
497 if (!w.mLayoutAttached) {
498 if (mTmpInitial) {
499 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
500 w.mContentChanged = false;
501 }
502 if (w.mAttrs.type == TYPE_DREAM) {
503 // Don't layout windows behind a dream, so that if it does stuff like hide
504 // the status bar we won't get a bad transition when it goes away.
505 mTmpWindow = w;
506 }
507 w.mLayoutNeeded = false;
508 w.prelayout();
509 mService.mPolicy.layoutWindowLw(w, null);
510 w.mLayoutSeq = mService.mLayoutSeq;
511
512 // Window frames may have changed. Update dim layer with the new bounds.
513 final Task task = w.getTask();
514 if (task != null) {
515 mDimLayerController.updateDimLayer(task);
516 }
517
518 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
519 + " mContainingFrame=" + w.mContainingFrame
520 + " mDisplayFrame=" + w.mDisplayFrame);
521 }
522 }
523 };
524
525 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
526 if (w.mLayoutAttached) {
527 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
528 + " mViewVisibility=" + w.mViewVisibility
529 + " mRelayoutCalled=" + w.mRelayoutCalled);
530 // If this view is GONE, then skip it -- keep the current frame, and let the caller
531 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
532 // windows, since that means "perform layout as normal, just don't display").
533 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
534 return;
535 }
536 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
537 || w.mLayoutNeeded) {
538 if (mTmpInitial) {
539 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
540 w.mContentChanged = false;
541 }
542 w.mLayoutNeeded = false;
543 w.prelayout();
544 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
545 w.mLayoutSeq = mService.mLayoutSeq;
546 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
547 + " mContainingFrame=" + w.mContainingFrame
548 + " mDisplayFrame=" + w.mDisplayFrame);
549 }
550 } else if (w.mAttrs.type == TYPE_DREAM) {
551 // Don't layout windows behind a dream, so that if it does stuff like hide the
552 // status bar we won't get a bad transition when it goes away.
553 mTmpWindow = mTmpWindow2;
554 }
555 };
556
557 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
558 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
559 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
560 return w.canBeImeTarget();
561 };
562
563 private final Consumer<WindowState> mApplyPostLayoutPolicy =
564 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
565 mService.mInputMethodTarget);
566
567 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
568 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
569 final boolean obscuredChanged = w.mObscured !=
570 mTmpApplySurfaceChangesTransactionState.obscured;
571 final RootWindowContainer root = mService.mRoot;
572 // Only used if default window
573 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
574
575 // Update effect.
576 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
577 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
578 final boolean isDisplayed = w.isDisplayedLw();
579
580 if (isDisplayed && w.isObscuringDisplay()) {
581 // This window completely covers everything behind it, so we want to leave all
582 // of them as undimmed (for performance reasons).
583 root.mObscuringWindow = w;
584 mTmpApplySurfaceChangesTransactionState.obscured = true;
585 }
586
587 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
588 root.handleNotObscuredLocked(w,
589 mTmpApplySurfaceChangesTransactionState.obscured,
590 mTmpApplySurfaceChangesTransactionState.syswin);
591
592 if (w.mHasSurface && isDisplayed) {
593 final int type = w.mAttrs.type;
594 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
595 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
596 mTmpApplySurfaceChangesTransactionState.syswin = true;
597 }
598 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
599 && w.mAttrs.preferredRefreshRate != 0) {
600 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
601 = w.mAttrs.preferredRefreshRate;
602 }
603 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
604 && w.mAttrs.preferredDisplayModeId != 0) {
605 mTmpApplySurfaceChangesTransactionState.preferredModeId
606 = w.mAttrs.preferredDisplayModeId;
607 }
608 }
609 }
610
611 w.applyDimLayerIfNeeded();
612
613 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
614 && mWallpaperController.isWallpaperTarget(w)) {
615 // This is the wallpaper target and its obscured state changed... make sure the
616 // current wallpaper's visibility has been updated accordingly.
617 mWallpaperController.updateWallpaperVisibility();
618 }
619
620 w.handleWindowMovedIfNeeded();
621
622 final WindowStateAnimator winAnimator = w.mWinAnimator;
623
624 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
625 w.mContentChanged = false;
626
627 // Moved from updateWindowsAndWallpaperLocked().
628 if (w.mHasSurface) {
629 // Take care of the window being ready to display.
630 final boolean committed = winAnimator.commitFinishDrawingLocked();
631 if (isDefaultDisplay && committed) {
632 if (w.mAttrs.type == TYPE_DREAM) {
633 // HACK: When a dream is shown, it may at that point hide the lock screen.
634 // So we need to redo the layout to let the phone window manager make this
635 // happen.
636 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
637 if (DEBUG_LAYOUT_REPEATS) {
638 surfacePlacer.debugLayoutRepeats(
639 "dream and commitFinishDrawingLocked true",
640 pendingLayoutChanges);
641 }
642 }
643 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
644 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
645 "First draw done in potential wallpaper target " + w);
646 root.mWallpaperMayChange = true;
647 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
648 if (DEBUG_LAYOUT_REPEATS) {
649 surfacePlacer.debugLayoutRepeats(
650 "wallpaper and commitFinishDrawingLocked true",
651 pendingLayoutChanges);
652 }
653 }
654 }
655 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
656 // Updates the shown frame before we set up the surface. This is needed
657 // because the resizing could change the top-left position (in addition to
658 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
659 // position the surface.
660 //
661 // If an animation is being started, we can't call this method because the
662 // animation hasn't processed its initial transformation yet, but in general
663 // we do want to update the position if the window is animating.
664 winAnimator.computeShownFrameLocked();
665 }
666 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
667 }
668
669 final AppWindowToken atoken = w.mAppToken;
670 if (atoken != null) {
671 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
672 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
673 mTmpUpdateAllDrawn.add(atoken);
674 }
675 }
676
677 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
678 && w.isDisplayedLw()) {
679 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
680 }
681
682 w.updateResizingWindowIfNeeded();
683 };
684
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800685 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800686 * Create new {@link DisplayContent} instance, add itself to the root window container and
687 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700688 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800689 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700690 * @param layersController window layer controller used to assign layer to the windows on this
691 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700692 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
693 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700694 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700695 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700696 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800697
698 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
699 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
700 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
701 + " new=" + display);
702 }
703
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700704 mDisplay = display;
705 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700706 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700707 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700708 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700709 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700710 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700711 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700712 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800713 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700714 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800715 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700716
717 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700718 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700719 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700720 super.addChild(mAboveAppWindowsContainers, null);
721 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800722
723 // Add itself as a child to the root container.
724 mService.mRoot.addChild(this, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700725 }
726
727 int getDisplayId() {
728 return mDisplayId;
729 }
730
Wale Ogunwale02319a62016-09-26 15:21:22 -0700731 WindowToken getWindowToken(IBinder binder) {
732 return mTokenMap.get(binder);
733 }
734
735 AppWindowToken getAppWindowToken(IBinder binder) {
736 final WindowToken token = getWindowToken(binder);
737 if (token == null) {
738 return null;
739 }
740 return token.asAppWindowToken();
741 }
742
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800743 void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700744 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
745 if (dc != null) {
746 // We currently don't support adding a window token to the display if the display
747 // already has the binder mapped to another token. If there is a use case for supporting
748 // this moving forward we will either need to merge the WindowTokens some how or have
749 // the binder map to a list of window tokens.
750 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
751 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
752 }
753 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700754
755 if (token.asAppWindowToken() == null) {
756 // Add non-app token to container hierarchy on the display. App tokens are added through
757 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700758 switch (token.windowType) {
759 case TYPE_WALLPAPER:
760 mBelowAppWindowsContainers.addChild(token);
761 break;
762 case TYPE_INPUT_METHOD:
763 case TYPE_INPUT_METHOD_DIALOG:
764 mImeWindowsContainers.addChild(token);
765 break;
766 default:
767 mAboveAppWindowsContainers.addChild(token);
768 break;
769 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700770 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700771 }
772
773 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700774 final WindowToken token = mTokenMap.remove(binder);
775 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800776 token.setExiting();
777 }
778 return token;
779 }
780
781 /** Changes the display the input window token is housed on to this one. */
782 void reParentWindowToken(WindowToken token) {
783 final DisplayContent prevDc = token.getDisplayContent();
784 if (prevDc == this) {
785 return;
786 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800787 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
788 && token.asAppWindowToken() == null) {
789 // Removed the token from the map, but made sure it's not an app token before removing
790 // from parent.
791 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700792 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800793
794 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700795 }
796
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700797 void removeAppToken(IBinder binder) {
798 final WindowToken token = removeWindowToken(binder);
799 if (token == null) {
800 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
801 return;
802 }
803
804 final AppWindowToken appToken = token.asAppWindowToken();
805
806 if (appToken == null) {
807 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
808 return;
809 }
810
811 appToken.onRemovedFromDisplay();
812 }
813
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700814 Display getDisplay() {
815 return mDisplay;
816 }
817
Craig Mautner59c00972012-07-30 12:10:24 -0700818 DisplayInfo getDisplayInfo() {
819 return mDisplayInfo;
820 }
821
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700822 DisplayMetrics getDisplayMetrics() {
823 return mDisplayMetrics;
824 }
825
Andrii Kulian8ee72852017-03-10 10:36:45 -0800826 int getRotation() {
827 return mRotation;
828 }
829
830 void setRotation(int newRotation) {
831 mRotation = newRotation;
832 }
833
834 int getLastOrientation() {
835 return mLastOrientation;
836 }
837
838 void setLastOrientation(int orientation) {
839 mLastOrientation = orientation;
840 }
841
842 boolean getAltOrientation() {
843 return mAltOrientation;
844 }
845
846 void setAltOrientation(boolean altOrientation) {
847 mAltOrientation = altOrientation;
848 }
849
850 int getLastWindowForcedOrientation() {
851 return mLastWindowForcedOrientation;
852 }
853
Andrii Kulian06d07d62017-03-14 11:11:47 -0700854 /**
855 * Update rotation of the display.
856 *
857 * Returns true if the rotation has been changed. In this case YOU MUST CALL
858 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
859 */
860 boolean updateRotationUnchecked(boolean inTransaction) {
861 if (mService.mDeferredRotationPauseCount > 0) {
862 // Rotation updates have been paused temporarily. Defer the update until
863 // updates have been resumed.
864 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
865 return false;
866 }
867
868 ScreenRotationAnimation screenRotationAnimation =
869 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
870 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
871 // Rotation updates cannot be performed while the previous rotation change
872 // animation is still in progress. Skip this update. We will try updating
873 // again after the animation is finished and the display is unfrozen.
874 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
875 return false;
876 }
877 if (mService.mDisplayFrozen) {
878 // Even if the screen rotation animation has finished (e.g. isAnimating
879 // returns false), there is still some time where we haven't yet unfrozen
880 // the display. We also need to abort rotation here.
881 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
882 "Deferring rotation, still finishing previous rotation");
883 return false;
884 }
885
886 if (!mService.mDisplayEnabled) {
887 // No point choosing a rotation if the display is not enabled.
888 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
889 return false;
890 }
891
892 final int oldRotation = mRotation;
893 final int lastOrientation = mLastOrientation;
894 final boolean oldAltOrientation = mAltOrientation;
895 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
896 final boolean rotateSeamlessly;
897
898 if (mService.mPolicy.shouldRotateSeamlessly(oldRotation, rotation)) {
899 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
900 if (seamlessRotated != null) {
901 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
902 // to complete (that is, waiting for windows to redraw). It's tempting to check
903 // w.mSeamlessRotationCount but that could be incorrect in the case of
904 // window-removal.
905 return false;
906 }
907
908 final WindowState cantSeamlesslyRotate = getWindow((w) ->
909 w.isChildWindow() && w.isVisibleNow()
910 && !w.mWinAnimator.mSurfaceController.getTransformToDisplayInverse());
911 if (cantSeamlesslyRotate != null) {
912 // In what can only be called an unfortunate workaround we require seamlessly
913 // rotated child windows to have the TRANSFORM_TO_DISPLAY_INVERSE flag. Due to
914 // limitations in the client API, there is no way for the client to set this flag in
915 // a race free fashion. If we seamlessly rotate a window which does not have this
916 // flag, but then gains it, we will get an incorrect visual result
917 // (rotated viewfinder). This means if we want to support seamlessly rotating
918 // windows which could gain this flag, we can't rotate windows without it. This
919 // limits seamless rotation in N to camera framework users, windows without
920 // children, and native code. This is unfortunate but having the camera work is our
921 // primary goal.
922 rotateSeamlessly = false;
923 } else {
924 rotateSeamlessly = true;
925 }
926 } else {
927 rotateSeamlessly = false;
928 }
929
930 // TODO: Implement forced rotation changes.
931 // Set mAltOrientation to indicate that the application is receiving
932 // an orientation that has different metrics than it expected.
933 // eg. Portrait instead of Landscape.
934
935 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
936 lastOrientation, rotation);
937
938 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
939 + ", got rotation " + rotation + " which has "
940 + (altOrientation ? "incompatible" : "compatible") + " metrics");
941
942 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
943 // No change.
944 return false;
945 }
946
947 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
948 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
949 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
950
951 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
952 mService.mWaitingForConfig = true;
953 }
954
955 mRotation = rotation;
956 mAltOrientation = altOrientation;
957 if (isDefaultDisplay) {
958 mService.mPolicy.setRotationLw(rotation);
959 }
960
961 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
962 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
963 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
964 WINDOW_FREEZE_TIMEOUT_DURATION);
965
966 setLayoutNeeded();
967 final int[] anim = new int[2];
968 if (isDimming()) {
969 anim[0] = anim[1] = 0;
970 } else {
971 mService.mPolicy.selectRotationAnimationLw(anim);
972 }
973
974 if (!rotateSeamlessly) {
975 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1]);
976 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
977 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
978 mDisplayId);
979 } else {
980 // The screen rotation animation uses a screenshot to freeze the screen
981 // while windows resize underneath.
982 // When we are rotating seamlessly, we allow the elements to transition
983 // to their rotated state independently and without a freeze required.
984 screenRotationAnimation = null;
985
986 // We have to reset this in case a window was removed before it
987 // finished seamless rotation.
988 mService.mSeamlessRotationCount = 0;
989 }
990
991 // We need to update our screen size information to match the new rotation. If the rotation
992 // has actually changed then this method will return true and, according to the comment at
993 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
994 // By updating the Display info here it will be available to
995 // #computeScreenConfiguration() later.
996 updateDisplayAndOrientation(getConfiguration().uiMode);
997
998 if (!inTransaction) {
999 if (SHOW_TRANSACTIONS) {
1000 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1001 }
1002 mService.openSurfaceTransaction();
1003 }
1004 try {
1005 // NOTE: We disable the rotation in the emulator because
1006 // it doesn't support hardware OpenGL emulation yet.
1007 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1008 && screenRotationAnimation.hasScreenshot()) {
1009 if (screenRotationAnimation.setRotationInTransaction(
1010 rotation, mService.mFxSession,
1011 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1012 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1013 mService.scheduleAnimationLocked();
1014 }
1015 }
1016
1017 if (rotateSeamlessly) {
1018 forAllWindows(w -> {
1019 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1020 }, true /* traverseTopToBottom */);
1021 }
1022
1023 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1024 } finally {
1025 if (!inTransaction) {
1026 mService.closeSurfaceTransaction();
1027 if (SHOW_LIGHT_TRANSACTIONS) {
1028 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1029 }
1030 }
1031 }
1032
1033 forAllWindows(w -> {
1034 // Discard surface after orientation change, these can't be reused.
1035 if (w.mAppToken != null) {
1036 w.mAppToken.destroySavedSurfaces();
1037 }
1038 if (w.mHasSurface && !rotateSeamlessly) {
1039 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
1040 w.mOrientationChanging = true;
1041 mService.mRoot.mOrientationChangeComplete = false;
1042 w.mLastFreezeDuration = 0;
1043 }
1044 w.mReportOrientationChanged = true;
1045 }, true /* traverseTopToBottom */);
1046
1047 if (rotateSeamlessly) {
1048 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1049 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1050 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1051 }
1052
1053 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1054 final WindowManagerService.RotationWatcher rotationWatcher
1055 = mService.mRotationWatchers.get(i);
1056 if (rotationWatcher.mDisplayId == mDisplayId) {
1057 try {
1058 rotationWatcher.mWatcher.onRotationChanged(rotation);
1059 } catch (RemoteException e) {
1060 // Ignore
1061 }
1062 }
1063 }
1064
1065 // TODO (multi-display): Magnification is supported only for the default display.
1066 // Announce rotation only if we will not animate as we already have the
1067 // windows in final state. Otherwise, we make this call at the rotation end.
1068 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1069 && isDefaultDisplay) {
1070 mService.mAccessibilityController.onRotationChangedLocked(this);
1071 }
1072
1073 return true;
1074 }
1075
1076 /**
1077 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1078 * changed.
1079 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1080 */
1081 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1082 // Use the effective "visual" dimensions based on current rotation
1083 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1084 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1085 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1086 int dw = realdw;
1087 int dh = realdh;
1088
1089 if (mAltOrientation) {
1090 if (realdw > realdh) {
1091 // Turn landscape into portrait.
1092 int maxw = (int)(realdh/1.3f);
1093 if (maxw < realdw) {
1094 dw = maxw;
1095 }
1096 } else {
1097 // Turn portrait into landscape.
1098 int maxh = (int)(realdw/1.3f);
1099 if (maxh < realdh) {
1100 dh = maxh;
1101 }
1102 }
1103 }
1104
1105 // Update application display metrics.
1106 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1107 mDisplayId);
1108 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1109 mDisplayId);
1110 mDisplayInfo.rotation = mRotation;
1111 mDisplayInfo.logicalWidth = dw;
1112 mDisplayInfo.logicalHeight = dh;
1113 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1114 mDisplayInfo.appWidth = appWidth;
1115 mDisplayInfo.appHeight = appHeight;
1116 if (isDefaultDisplay) {
1117 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1118 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1119 }
1120 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1121 if (mDisplayScalingDisabled) {
1122 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1123 } else {
1124 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1125 }
1126
1127 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1128 mDisplayInfo);
1129
1130 mBaseDisplayRect.set(0, 0, dw, dh);
1131
1132 if (isDefaultDisplay) {
1133 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1134 mCompatDisplayMetrics);
1135 }
1136 return mDisplayInfo;
1137 }
1138
1139 /**
1140 * Compute display configuration based on display properties and policy settings.
1141 * Do not call if mDisplayReady == false.
1142 */
1143 void computeScreenConfiguration(Configuration config) {
1144 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1145
1146 final int dw = displayInfo.logicalWidth;
1147 final int dh = displayInfo.logicalHeight;
1148 config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
1149 Configuration.ORIENTATION_LANDSCAPE;
1150 config.screenWidthDp =
1151 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1152 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1153 config.screenHeightDp =
1154 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1155 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1156 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1157 || displayInfo.rotation == Surface.ROTATION_270);
1158
1159 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1160 mDisplayMetrics.density, config);
1161
1162 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1163 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1164 ? Configuration.SCREENLAYOUT_ROUND_YES
1165 : Configuration.SCREENLAYOUT_ROUND_NO);
1166
1167 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1168 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1169 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1170 dh, mDisplayId);
1171 config.densityDpi = displayInfo.logicalDensityDpi;
1172
1173 config.colorMode =
1174 (displayInfo.isHdr()
1175 ? Configuration.COLOR_MODE_HDR_YES
1176 : Configuration.COLOR_MODE_HDR_NO)
1177 | (displayInfo.isWideColorGamut()
1178 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1179 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1180
1181 // Update the configuration based on available input devices, lid switch,
1182 // and platform configuration.
1183 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1184 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1185 config.navigation = Configuration.NAVIGATION_NONAV;
1186
1187 int keyboardPresence = 0;
1188 int navigationPresence = 0;
1189 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1190 final int len = devices != null ? devices.length : 0;
1191 for (int i = 0; i < len; i++) {
1192 InputDevice device = devices[i];
1193 if (!device.isVirtual()) {
1194 final int sources = device.getSources();
1195 final int presenceFlag = device.isExternal() ?
1196 WindowManagerPolicy.PRESENCE_EXTERNAL :
1197 WindowManagerPolicy.PRESENCE_INTERNAL;
1198
1199 // TODO(multi-display): Configure on per-display basis.
1200 if (mService.mIsTouchDevice) {
1201 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1202 InputDevice.SOURCE_TOUCHSCREEN) {
1203 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1204 }
1205 } else {
1206 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1207 }
1208
1209 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1210 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1211 navigationPresence |= presenceFlag;
1212 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1213 && config.navigation == Configuration.NAVIGATION_NONAV) {
1214 config.navigation = Configuration.NAVIGATION_DPAD;
1215 navigationPresence |= presenceFlag;
1216 }
1217
1218 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1219 config.keyboard = Configuration.KEYBOARD_QWERTY;
1220 keyboardPresence |= presenceFlag;
1221 }
1222 }
1223 }
1224
1225 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1226 config.navigation = Configuration.NAVIGATION_DPAD;
1227 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1228 }
1229
1230 // Determine whether a hard keyboard is available and enabled.
1231 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1232 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1233 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1234 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1235 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1236 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1237 }
1238
1239 // Let the policy update hidden states.
1240 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1241 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1242 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1243 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1244 }
1245
1246 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1247 int displayId) {
1248 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1249 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1250 final int unrotDw, unrotDh;
1251 if (rotated) {
1252 unrotDw = dh;
1253 unrotDh = dw;
1254 } else {
1255 unrotDw = dw;
1256 unrotDh = dh;
1257 }
1258 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1259 displayId);
1260 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1261 displayId);
1262 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1263 displayId);
1264 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1265 displayId);
1266 return sw;
1267 }
1268
1269 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1270 DisplayMetrics dm, int dw, int dh, int displayId) {
1271 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1272 displayId);
1273 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1274 uiMode, displayId);
1275 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1276 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1277 if (curSize == 0 || size < curSize) {
1278 curSize = size;
1279 }
1280 return curSize;
1281 }
1282
1283 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1284 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1285
1286 // We need to determine the smallest width that will occur under normal
1287 // operation. To this, start with the base screen size and compute the
1288 // width under the different possible rotations. We need to un-rotate
1289 // the current screen dimensions before doing this.
1290 int unrotDw, unrotDh;
1291 if (rotated) {
1292 unrotDw = dh;
1293 unrotDh = dw;
1294 } else {
1295 unrotDw = dw;
1296 unrotDh = dh;
1297 }
1298 displayInfo.smallestNominalAppWidth = 1<<30;
1299 displayInfo.smallestNominalAppHeight = 1<<30;
1300 displayInfo.largestNominalAppWidth = 0;
1301 displayInfo.largestNominalAppHeight = 0;
1302 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1303 unrotDh);
1304 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1305 unrotDw);
1306 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1307 unrotDh);
1308 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1309 unrotDw);
1310 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1311 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1312 displayId);
1313 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1314 displayId);
1315 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1316 displayId);
1317 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1318 displayId);
1319 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1320 outConfig.screenLayout = sl;
1321 }
1322
1323 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1324 int uiMode, int displayId) {
1325 // Get the app screen size at this rotation.
1326 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1327 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1328
1329 // Compute the screen layout size class for this rotation.
1330 int longSize = w;
1331 int shortSize = h;
1332 if (longSize < shortSize) {
1333 int tmp = longSize;
1334 longSize = shortSize;
1335 shortSize = tmp;
1336 }
1337 longSize = (int)(longSize/density);
1338 shortSize = (int)(shortSize/density);
1339 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1340 }
1341
1342 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1343 int uiMode, int dw, int dh) {
1344 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1345 displayId);
1346 if (width < displayInfo.smallestNominalAppWidth) {
1347 displayInfo.smallestNominalAppWidth = width;
1348 }
1349 if (width > displayInfo.largestNominalAppWidth) {
1350 displayInfo.largestNominalAppWidth = width;
1351 }
1352 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1353 displayId);
1354 if (height < displayInfo.smallestNominalAppHeight) {
1355 displayInfo.smallestNominalAppHeight = height;
1356 }
1357 if (height > displayInfo.largestNominalAppHeight) {
1358 displayInfo.largestNominalAppHeight = height;
1359 }
1360 }
1361
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001362 DockedStackDividerController getDockedDividerController() {
1363 return mDividerControllerLocked;
1364 }
1365
Winson Chung655332c2016-10-31 13:14:28 -07001366 PinnedStackController getPinnedStackController() {
1367 return mPinnedStackControllerLocked;
1368 }
1369
Jeff Browna506a6e2013-06-04 00:02:38 -07001370 /**
1371 * Returns true if the specified UID has access to this display.
1372 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001373 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001374 return mDisplay.hasAccess(uid);
1375 }
1376
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001377 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001378 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001379 }
1380
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001381 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001382 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001383 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001384 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001385 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001386 }
1387
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001388 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001389 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1390 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001391 if (stack.mStackId == stackId) {
1392 return stack;
1393 }
1394 }
1395 return null;
1396 }
1397
Andrii Kulian441e4492016-09-29 15:25:00 -07001398 @Override
1399 void onConfigurationChanged(Configuration newParentConfig) {
1400 super.onConfigurationChanged(newParentConfig);
1401
Andrii Kulian3a507b52016-09-19 18:14:12 -07001402 // The display size information is heavily dependent on the resources in the current
1403 // configuration, so we need to reconfigure it every time the configuration changes.
1404 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1405 mService.reconfigureDisplayLocked(this);
1406
1407 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001408 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001409 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001410
Andrii Kulian441e4492016-09-29 15:25:00 -07001411 /**
1412 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1413 * bounds were updated.
1414 */
1415 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001416 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1417 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001418 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001419 changedStackList.add(stack.mStackId);
1420 }
1421 }
1422 }
1423
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001424 @Override
1425 boolean fillsParent() {
1426 return true;
1427 }
1428
1429 @Override
1430 boolean isVisible() {
1431 return true;
1432 }
1433
1434 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001435 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001436 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001437 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001438 }
1439
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001440 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001441 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1442 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1443 // target, or here in order if there isn't an IME target.
1444 if (traverseTopToBottom) {
1445 for (int i = mChildren.size() - 1; i >= 0; --i) {
1446 final DisplayChildWindowContainer child = mChildren.get(i);
1447 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1448 // In this case the Ime windows will be processed above their target so we skip
1449 // here.
1450 continue;
1451 }
1452 if (child.forAllWindows(callback, traverseTopToBottom)) {
1453 return true;
1454 }
1455 }
1456 } else {
1457 final int count = mChildren.size();
1458 for (int i = 0; i < count; i++) {
1459 final DisplayChildWindowContainer child = mChildren.get(i);
1460 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1461 // In this case the Ime windows will be processed above their target so we skip
1462 // here.
1463 continue;
1464 }
1465 if (child.forAllWindows(callback, traverseTopToBottom)) {
1466 return true;
1467 }
1468 }
1469 }
1470 return false;
1471 }
1472
1473 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1474 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1475 }
1476
1477 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001478 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001479 final WindowManagerPolicy policy = mService.mPolicy;
1480
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001481 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001482 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001483 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001484 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001485 // If the display is frozen, some activities may be in the middle of restarting, and
1486 // thus have removed their old window. If the window has the flag to hide the lock
1487 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1488 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001489 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001490 } else if (policy.isKeyguardLocked()) {
1491 // Use the last orientation the while the display is frozen with the keyguard
1492 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1493 // window. We don't want to check the show when locked window directly though as
1494 // things aren't stable while the display is frozen, for example the window could be
1495 // momentarily unavailable due to activity relaunch.
1496 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001497 + "return " + mLastOrientation);
1498 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001499 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001500 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001501 final int orientation = mAboveAppWindowsContainers.getOrientation();
1502 if (orientation != SCREEN_ORIENTATION_UNSET) {
1503 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001504 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001505 }
1506
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001507 // Top system windows are not requesting an orientation. Start searching from apps.
1508 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001509 }
1510
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001511 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -07001512 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001513 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001514 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1515 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001516 }
Craig Mautner722285e2012-09-07 13:55:58 -07001517 }
1518
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001519 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001520 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1521 if (displayManagerInternal != null) {
1522 // Bootstrap the default logical display from the display manager.
1523 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1524 if (newDisplayInfo != null) {
1525 mDisplayInfo.copyFrom(newDisplayInfo);
1526 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001527 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001528
Bryce Lee6addf5d2017-02-21 21:36:55 +00001529 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1530 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1531 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
1532 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001533 }
1534
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001535 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001536 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001537 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001538 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001539 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1540 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001541 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001542 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001543 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001544 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001545 out.set(left, top, left + width, top + height);
1546 }
1547
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001548 private void getLogicalDisplayRect(Rect out, int orientation) {
1549 getLogicalDisplayRect(out);
1550
1551 // Rotate the Rect if needed.
1552 final int currentRotation = mDisplayInfo.rotation;
1553 final int rotationDelta = deltaRotation(currentRotation, orientation);
1554 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1555 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1556 mTmpRectF.set(out);
1557 mTmpMatrix.mapRect(mTmpRectF);
1558 mTmpRectF.round(out);
1559 }
1560 }
1561
Chong Zhangf66db432016-01-13 10:39:51 -08001562 void getContentRect(Rect out) {
1563 out.set(mContentRect);
1564 }
1565
Wale Ogunwale1666e312016-12-16 11:27:18 -08001566 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1567 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1568 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001569
Wale Ogunwale1666e312016-12-16 11:27:18 -08001570 TaskStack stack = getStackById(stackId);
1571 if (stack != null) {
1572 // It's already attached to the display...clear mDeferRemoval and move stack to
1573 // appropriate z-order on display as needed.
1574 stack.mDeferRemoval = false;
1575 // We're not moving the display to front when we're adding stacks, only when
1576 // requested to change the position of stack explicitly.
1577 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1578 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001579 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001580 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001581 mTaskStackContainers.addStackToDisplay(stack, onTop);
1582 }
1583
Wale Ogunwale1666e312016-12-16 11:27:18 -08001584 if (stackId == DOCKED_STACK_ID) {
1585 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001586 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001587 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001588 }
1589
Wale Ogunwale1666e312016-12-16 11:27:18 -08001590 void moveStackToDisplay(TaskStack stack) {
1591 final DisplayContent prevDc = stack.getDisplayContent();
1592 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001593 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1594 + " which is not currently attached to any display");
1595 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001596 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001597 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1598 + " to its current displayId=" + mDisplayId);
1599 }
1600
Wale Ogunwale1666e312016-12-16 11:27:18 -08001601 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
1602 mTaskStackContainers.addStackToDisplay(stack, true /* onTop */);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001603 }
1604
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001605 @Override
1606 protected void addChild(DisplayChildWindowContainer child,
1607 Comparator<DisplayChildWindowContainer> comparator) {
1608 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1609 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001610
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001611 @Override
1612 protected void addChild(DisplayChildWindowContainer child, int index) {
1613 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1614 }
1615
1616 @Override
1617 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001618 // Only allow removal of direct children from this display if the display is in the process
1619 // of been removed.
1620 if (mRemovingDisplay) {
1621 super.removeChild(child);
1622 return;
1623 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001624 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001625 }
1626
Andrii Kuliand2765632016-12-12 22:26:34 -08001627 @Override
1628 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1629 // Children of the display are statically ordered, so the real intention here is to perform
1630 // the operation on the display and not the static direct children.
1631 getParent().positionChildAt(position, this, includingParents);
1632 }
1633
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001634 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001635 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1636 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001637 final int taskId = stack.taskIdFromPoint(x, y);
1638 if (taskId != -1) {
1639 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001640 }
1641 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001642 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001643 }
1644
Chong Zhang8e89b312015-09-09 15:09:30 -07001645 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001646 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001647 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001648 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001649 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001650 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001651 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001652 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1653 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001654 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001655 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001656 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001657
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001658 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1659 if (mTmpTaskForResizePointSearchResult.searchDone) {
1660 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001661 }
1662 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001663 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001664 }
1665
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001666 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001667 // The provided task is the task on this display with focus, so if WindowManagerService's
1668 // focused app is not on this display, focusedTask will be null.
1669 if (focusedTask == null) {
1670 mTouchExcludeRegion.setEmpty();
1671 } else {
1672 mTouchExcludeRegion.set(mBaseDisplayRect);
1673 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1674 mTmpRect2.setEmpty();
1675 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1676 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1677 stack.setTouchExcludeRegion(
1678 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1679 }
1680 // If we removed the focused task above, add it back and only leave its
1681 // outside touch area in the exclusion. TapDectector is not interested in
1682 // any touch inside the focused task itself.
1683 if (!mTmpRect2.isEmpty()) {
1684 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1685 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001686 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001687 final WindowState inputMethod = mService.mInputMethodWindow;
1688 if (inputMethod != null && inputMethod.isVisibleLw()) {
1689 // If the input method is visible and the user is typing, we don't want these touch
1690 // events to be intercepted and used to change focus. This would likely cause a
1691 // disappearance of the input method.
1692 inputMethod.getTouchableRegion(mTmpRegion);
1693 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1694 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001695 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1696 WindowState win = mTapExcludedWindows.get(i);
1697 win.getTouchableRegion(mTmpRegion);
1698 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1699 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001700 // TODO(multi-display): Support docked stacks on secondary displays.
1701 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001702 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001703 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001704 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1705 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001706 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001707 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001708 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001709 }
1710
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001711 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001712 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001713 super.switchUser();
1714 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001715 }
1716
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001717 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001718 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1719 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001720 }
1721 }
1722
1723 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001724 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001725 }
1726
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001727 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001728 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001729 }
1730
1731 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001732 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001733 }
1734
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001735 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001736 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001737 }
1738
Wale Ogunwale10124582016-09-15 20:25:50 -07001739 @Override
1740 void removeIfPossible() {
1741 if (isAnimating()) {
1742 mDeferredRemoval = true;
1743 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001744 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001745 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001746 }
1747
Wale Ogunwale10124582016-09-15 20:25:50 -07001748 @Override
1749 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001750 mRemovingDisplay = true;
1751 try {
1752 super.removeImmediately();
1753 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1754 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001755 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001756 mService.unregisterPointerEventListener(mTapDetector);
1757 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1758 }
1759 } finally {
1760 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001761 }
Craig Mautner95da1082014-02-24 17:54:35 -08001762 }
1763
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001764 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001765 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001766 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001767 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1768
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001769 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001770 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001771 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001772 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001773 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001774 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001775 }
1776
Wale Ogunwale10124582016-09-15 20:25:50 -07001777 boolean animateForIme(float interpolatedValue, float animationTarget,
1778 float dividerAnimationTarget) {
1779 boolean updated = false;
1780
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001781 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1782 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001783 if (stack == null || !stack.isAdjustedForIme()) {
1784 continue;
1785 }
1786
1787 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1788 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1789 updated = true;
1790 } else {
1791 mDividerControllerLocked.mLastAnimationProgress =
1792 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1793 mDividerControllerLocked.mLastDividerProgress =
1794 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1795 updated |= stack.updateAdjustForIme(
1796 mDividerControllerLocked.mLastAnimationProgress,
1797 mDividerControllerLocked.mLastDividerProgress,
1798 false /* force */);
1799 }
1800 if (interpolatedValue >= 1f) {
1801 stack.endImeAdjustAnimation();
1802 }
1803 }
1804
1805 return updated;
1806 }
1807
1808 boolean clearImeAdjustAnimation() {
1809 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001810 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1811 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001812 if (stack != null && stack.isAdjustedForIme()) {
1813 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1814 changed = true;
1815 }
1816 }
1817 return changed;
1818 }
1819
1820 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001821 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1822 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001823 if (stack.isVisible() && stack.isAdjustedForIme()) {
1824 stack.beginImeAdjustAnimation();
1825 }
1826 }
1827 }
1828
1829 void adjustForImeIfNeeded() {
1830 final WindowState imeWin = mService.mInputMethodWindow;
1831 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1832 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001833 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001834 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1835 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1836 imeTargetStack.getDockSide() : DOCKED_INVALID;
1837 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1838 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1839 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1840 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1841 final boolean imeHeightChanged = imeVisible &&
1842 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1843
1844 // The divider could be adjusted for IME position, or be thinner than usual,
1845 // or both. There are three possible cases:
1846 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1847 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1848 // - If IME is not visible, divider is not moved and is normal width.
1849
1850 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001851 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1852 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001853 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
1854 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
1855 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1856 } else {
1857 stack.resetAdjustedForIme(false);
1858 }
1859 }
1860 mDividerControllerLocked.setAdjustedForIme(
1861 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1862 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001863 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1864 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001865 stack.resetAdjustedForIme(!dockVisible);
1866 }
1867 mDividerControllerLocked.setAdjustedForIme(
1868 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
1869 }
Winson Chung655332c2016-10-31 13:14:28 -07001870 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07001871 }
1872
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001873 void setInputMethodAnimLayerAdjustment(int adj) {
1874 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
1875 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08001876 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001877 }
1878
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001879 /**
1880 * If a window that has an animation specifying a colored background and the current wallpaper
1881 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
1882 * suddenly disappear.
1883 */
1884 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001885 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
1886 w -> w.mIsWallpaper && w.isVisibleNow());
1887
1888 if (visibleWallpaper != null) {
1889 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001890 }
1891 return winAnimator.mAnimLayer;
1892 }
1893
Wale Ogunwale10124582016-09-15 20:25:50 -07001894 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001895 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1896 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07001897 stack.prepareFreezingTaskBounds();
1898 }
1899 }
1900
Wale Ogunwale94744212015-09-21 19:01:47 -07001901 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001902 getLogicalDisplayRect(mTmpRect, newRotation);
1903
1904 // Compute a transform matrix to undo the coordinate space transformation,
1905 // and present the window at the same physical position it previously occupied.
1906 final int deltaRotation = deltaRotation(newRotation, oldRotation);
1907 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
1908
1909 mTmpRectF.set(bounds);
1910 mTmpMatrix.mapRect(mTmpRectF);
1911 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07001912 }
1913
Wale Ogunwale4a02d812015-02-12 23:01:38 -08001914 static int deltaRotation(int oldRotation, int newRotation) {
1915 int delta = newRotation - oldRotation;
1916 if (delta < 0) delta += 4;
1917 return delta;
1918 }
1919
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001920 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001921 Matrix outMatrix) {
1922 // For rotations without Z-ordering we don't need the target rectangle's position.
1923 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
1924 displayHeight, outMatrix);
1925 }
1926
1927 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
1928 float displayWidth, float displayHeight, Matrix outMatrix) {
1929 switch (rotation) {
1930 case ROTATION_0:
1931 outMatrix.reset();
1932 break;
1933 case ROTATION_270:
1934 outMatrix.setRotate(270, 0, 0);
1935 outMatrix.postTranslate(0, displayHeight);
1936 outMatrix.postTranslate(rectTop, 0);
1937 break;
1938 case ROTATION_180:
1939 outMatrix.reset();
1940 break;
1941 case ROTATION_90:
1942 outMatrix.setRotate(90, 0, 0);
1943 outMatrix.postTranslate(displayWidth, 0);
1944 outMatrix.postTranslate(-rectTop, rectLeft);
1945 break;
1946 }
1947 }
1948
Craig Mautnera91f9e22012-09-14 16:22:08 -07001949 public void dump(String prefix, PrintWriter pw) {
1950 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
1951 final String subPrefix = " " + prefix;
1952 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
1953 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
1954 pw.print("dpi");
1955 if (mInitialDisplayWidth != mBaseDisplayWidth
1956 || mInitialDisplayHeight != mBaseDisplayHeight
1957 || mInitialDisplayDensity != mBaseDisplayDensity) {
1958 pw.print(" base=");
1959 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
1960 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
1961 }
Jeff Brownd46747a2015-04-15 19:02:36 -07001962 if (mDisplayScalingDisabled) {
1963 pw.println(" noscale");
1964 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07001965 pw.print(" cur=");
1966 pw.print(mDisplayInfo.logicalWidth);
1967 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
1968 pw.print(" app=");
1969 pw.print(mDisplayInfo.appWidth);
1970 pw.print("x"); pw.print(mDisplayInfo.appHeight);
1971 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
1972 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
1973 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
1974 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001975 pw.println(subPrefix + "deferred=" + mDeferredRemoval
1976 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001977
Craig Mautnerdc548482014-02-05 13:35:24 -08001978 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001979 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001980 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1981 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001982 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001983 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001984
Craig Mautnerdc548482014-02-05 13:35:24 -08001985 pw.println();
1986 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001987 pw.println();
1988 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001989 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001990 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001991 pw.print(" Exiting #"); pw.print(i);
1992 pw.print(' '); pw.print(token);
1993 pw.println(':');
1994 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001995 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001996 }
Craig Mautner59c00972012-07-30 12:10:24 -07001997 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001998 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07001999 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002000 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002001 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002002 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002003
2004 if (mInputMethodAnimLayerAdjustment != 0) {
2005 pw.println(subPrefix
2006 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2007 }
Craig Mautner59c00972012-07-30 12:10:24 -07002008 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002009
2010 @Override
2011 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002012 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002013 }
2014
2015 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002016 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002017 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002018
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002019 /** Checks if stack with provided id is visible on this display. */
2020 boolean isStackVisible(int stackId) {
2021 final TaskStack stack = getStackById(stackId);
2022 return (stack != null && stack.isVisible());
2023 }
2024
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002025 /**
2026 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2027 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002028 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002029 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002030 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002031 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002032
2033 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002034 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002035 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002036 */
Jorim Jaggife762342016-10-13 14:33:27 +02002037 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002038 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002039 }
2040
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002041 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002042 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002043 final int x = (int) xf;
2044 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002045 final WindowState touchedWin = getWindow(w -> {
2046 final int flags = w.mAttrs.flags;
2047 if (!w.isVisibleLw()) {
2048 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002049 }
2050 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002051 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002052 }
2053
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002054 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002055 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002056 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002057 }
2058
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002059 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002060
2061 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002062 return mTmpRegion.contains(x, y) || touchFlags == 0;
2063 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002064
2065 return touchedWin;
2066 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002067
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002068 boolean canAddToastWindowForUid(int uid) {
2069 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002070 // Also if the app is focused adding more than one toast at
2071 // a time for better backwards compatibility.
2072 final WindowState focusedWindowForUid = getWindow(w ->
2073 w.mOwnerUid == uid && w.isFocused());
2074 if (focusedWindowForUid != null) {
2075 return true;
2076 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002077 final WindowState win = getWindow(w ->
2078 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2079 && !w.mWindowRemovalAllowed);
2080 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002081 }
2082
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002083 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002084 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2085 return;
2086 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002087
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002088 // Used to communicate the old focus to the callback method.
2089 mTmpWindow = oldFocus;
2090
2091 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002092 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002093
2094 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002095 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002096
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002097 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002098
2099 if (mTmpWindow == null) {
2100 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2101 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002102 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002103 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002104 }
2105
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002106 /** Updates the layer assignment of windows on this display. */
2107 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002108 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002109 if (setLayoutNeeded) {
2110 setLayoutNeeded();
2111 }
2112 }
2113
Wale Ogunwale1666e312016-12-16 11:27:18 -08002114 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2115 // moving containers or resizing them. Need to investigate the best way to have it automatically
2116 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002117 void layoutAndAssignWindowLayersIfNeeded() {
2118 mService.mWindowsChanged = true;
2119 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002120
2121 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2122 false /*updateInputWindows*/)) {
2123 assignWindowLayers(false /* setLayoutNeeded */);
2124 }
2125
2126 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2127 mService.mWindowPlacerLocked.performSurfacePlacement();
2128 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2129 }
2130
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002131 /** Returns true if a leaked surface was destroyed */
2132 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002133 // Used to indicate that a surface was leaked.
2134 mTmpWindow = null;
2135 forAllWindows(w -> {
2136 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002137 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002138 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002139 }
2140 if (!mService.mSessions.contains(wsa.mSession)) {
2141 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002142 + w + " surface=" + wsa.mSurfaceController
2143 + " token=" + w.mToken
2144 + " pid=" + w.mSession.mPid
2145 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002146 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002147 mService.mForceRemoves.add(w);
2148 mTmpWindow = w;
2149 } else if (w.mAppToken != null && w.mAppToken.clientHidden) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002150 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002151 + w + " surface=" + wsa.mSurfaceController
2152 + " token=" + w.mAppToken
2153 + " saved=" + w.hasSavedSurface());
2154 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002155 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002156 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002157 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002158 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002159
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002160 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002161 }
2162
2163 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002164 * Determine and return the window that should be the IME target.
2165 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2166 * @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 +00002167 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002168 WindowState computeImeTarget(boolean updateImeTarget) {
2169 if (mService.mInputMethodWindow == null) {
2170 // There isn't an IME so there shouldn't be a target...That was easy!
2171 if (updateImeTarget) {
2172 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2173 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2174 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2175 }
2176 return null;
2177 }
2178
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002179 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2180 // same display. Or even when the current IME/target are not on the same screen as the next
2181 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002182 mUpdateImeTarget = updateImeTarget;
2183 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002184
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002185
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002186 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2187 // to be on top of it, but it is not -really- where input will go. So look down below
2188 // for a real window to target...
2189 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2190 final AppWindowToken token = target.mAppToken;
2191 if (token != null) {
2192 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2193 if (betterTarget != null) {
2194 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002195 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002196 }
2197 }
2198
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002199 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2200 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002201
2202 // Now, a special case -- if the last target's window is in the process of exiting, and is
2203 // above the new target, keep on the last target to avoid flicker. Consider for example a
2204 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2205 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2206 // scrim.
2207 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002208 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2209 && (target == null
2210 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002211 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002212 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002213 }
2214
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002215 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2216 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002217
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002218 if (target == null) {
2219 if (updateImeTarget) {
2220 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2221 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2222 + Debug.getCallers(4) : ""));
2223 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2224 }
2225
2226 return null;
2227 }
2228
2229 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002230 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2231 if (token != null) {
2232
2233 // Now some fun for dealing with window animations that modify the Z order. We need
2234 // to look at all windows below the current target that are in this app, finding the
2235 // highest visible one in layering.
2236 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002237 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002238 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002239 }
2240
2241 if (highestTarget != null) {
2242 final AppTransition appTransition = mService.mAppTransition;
2243 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2244 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2245 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002246 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002247
2248 if (appTransition.isTransitionSet()) {
2249 // If we are currently setting up for an animation, hold everything until we
2250 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002251 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2252 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002253 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002254 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002255 // If the window we are currently targeting is involved with an animation,
2256 // and it is on top of the next target we will be over, then hold off on
2257 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002258 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2259 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002260 }
2261 }
2262 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002263
2264 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2265 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2266 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002267 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002268 }
2269
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002270 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002271 }
2272
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002273 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2274 if (target == mService.mInputMethodTarget
2275 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2276 && mInputMethodAnimLayerAdjustment == layerAdj) {
2277 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002278 }
2279
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002280 mService.mInputMethodTarget = target;
2281 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2282 setInputMethodAnimLayerAdjustment(layerAdj);
2283 assignWindowLayers(false /* setLayoutNeeded */);
2284 }
2285
2286 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2287 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2288 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2289 }
2290
2291 // Used to indicate we have reached the first window in the range we are interested in.
2292 mTmpWindow = null;
2293
2294 // TODO: Figure-out a more efficient way to do this.
2295 final WindowState candidate = getWindow(w -> {
2296 if (w == top) {
2297 // Reached the first window in the range we are interested in.
2298 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002299 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002300 if (mTmpWindow == null) {
2301 return false;
2302 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002303
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002304 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2305 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002306 }
2307 // If we reached the bottom of the range of windows we are considering,
2308 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002309 if (w == bottom) {
2310 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002311 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002312 return false;
2313 });
2314
2315 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002316 }
2317
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002318 void setLayoutNeeded() {
2319 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2320 mLayoutNeeded = true;
2321 }
2322
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002323 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002324 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2325 mLayoutNeeded = false;
2326 }
2327
2328 boolean isLayoutNeeded() {
2329 return mLayoutNeeded;
2330 }
2331
Wale Ogunwale02319a62016-09-26 15:21:22 -07002332 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2333 if (mTokenMap.isEmpty()) {
2334 return;
2335 }
2336 pw.println(" Display #" + mDisplayId);
2337 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2338 while (it.hasNext()) {
2339 final WindowToken token = it.next();
2340 pw.print(" ");
2341 pw.print(token);
2342 if (dumpAll) {
2343 pw.println(':');
2344 token.dump(pw, " ");
2345 } else {
2346 pw.println();
2347 }
2348 }
2349 }
2350
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002351 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002352 final int[] index = new int[1];
2353 forAllWindows(w -> {
2354 final WindowStateAnimator wAnim = w.mWinAnimator;
2355 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2356 index[0] = index[0] + 1;
2357 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002358 }
2359
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002360 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002361 forAllWindows(w -> {
2362 w.mWinAnimator.enableSurfaceTrace(fd);
2363 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002364 }
2365
2366 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002367 forAllWindows(w -> {
2368 w.mWinAnimator.disableSurfaceTrace();
2369 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002370 }
2371
Jorim Jaggife762342016-10-13 14:33:27 +02002372 /**
2373 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2374 */
2375 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2376 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002377 forAllWindows(w -> {
2378 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
2379 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002380 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2381 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002383 }
2384
Wale Ogunwale494009b82016-10-21 09:01:38 -07002385 boolean checkWaitingForWindows() {
2386
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002387 mHaveBootMsg = false;
2388 mHaveApp = false;
2389 mHaveWallpaper = false;
2390 mHaveKeyguard = true;
2391
2392 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002393 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2394 return true;
2395 }
2396 if (w.isDrawnLw()) {
2397 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002399 } else if (w.mAttrs.type == TYPE_APPLICATION
2400 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002401 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002402 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002403 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002404 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002405 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002406 }
2407 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002408 return false;
2409 });
2410
2411 if (visibleWindow != null) {
2412 // We have a visible window.
2413 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002414 }
2415
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002416 // if the wallpaper service is disabled on the device, we're never going to have
2417 // wallpaper, don't bother waiting for it
2418 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2419 com.android.internal.R.bool.config_enableWallpaperService)
2420 && !mService.mOnlyCore;
2421
Wale Ogunwale494009b82016-10-21 09:01:38 -07002422 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2423 "******** booted=" + mService.mSystemBooted
2424 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002425 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2426 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2427 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002428
2429 // If we are turning on the screen to show the boot message, don't do it until the boot
2430 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002431 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002432 return true;
2433 }
2434
2435 // If we are turning on the screen after the boot is completed normally, don't do so until
2436 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002437 if (mService.mSystemBooted
2438 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002439 return true;
2440 }
2441
2442 return false;
2443 }
2444
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002445 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002446 mTmpWindowAnimator = animator;
2447 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002448 }
2449
2450 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002451 resetAnimationBackgroundAnimator();
2452
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002453 // Used to indicate a detached wallpaper.
2454 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002455 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002456
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002457 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002458
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002460 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002461 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2462 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002463 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2464 }
2465 }
2466
2467 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002468 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002469 }
2470
Wale Ogunwale494009b82016-10-21 09:01:38 -07002471 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002472 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002473 if (imFocus == null) {
2474 return false;
2475 }
2476
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002477 if (DEBUG_INPUT_METHOD) {
2478 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2479 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2480 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002481 }
2482
Wale Ogunwale494009b82016-10-21 09:01:38 -07002483 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2484
2485 if (DEBUG_INPUT_METHOD) {
2486 Slog.i(TAG_WM, "IM target client: " + imeClient);
2487 if (imeClient != null) {
2488 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2489 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2490 }
2491 }
2492
2493 return imeClient != null && imeClient.asBinder() == client.asBinder();
2494 }
2495
2496 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002497 final WindowState win = getWindow(
2498 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2499 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002500 }
2501
2502 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002503 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002504 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002505 final int curValue = w.mSystemUiVisibility;
2506 final int diff = (curValue ^ visibility) & globalDiff;
2507 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002508 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002509 w.mSeq++;
2510 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002511 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002512 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2513 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002514 visibility, newValue, diff);
2515 }
2516 } catch (RemoteException e) {
2517 // so sorry
2518 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002519 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002520 }
2521
2522 void onWindowFreezeTimeout() {
2523 Slog.w(TAG_WM, "Window freeze timeout expired.");
2524 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002525
2526 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002527 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002528 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002529 }
2530 w.mOrientationChanging = false;
2531 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2532 - mService.mDisplayFreezeTime);
2533 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002534 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002535 mService.mWindowPlacerLocked.performSurfacePlacement();
2536 }
2537
2538 void waitForAllWindowsDrawn() {
2539 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002540 forAllWindows(w -> {
2541 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2542 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2543 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002544 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002545 w.mLastContentInsets.set(-1, -1, -1, -1);
2546 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002547 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002548 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002549 }
2550
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002551 // TODO: Super crazy long method that should be broken down...
2552 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2553
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002554 final int dw = mDisplayInfo.logicalWidth;
2555 final int dh = mDisplayInfo.logicalHeight;
2556 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2557
2558 mTmpUpdateAllDrawn.clear();
2559
2560 int repeats = 0;
2561 do {
2562 repeats++;
2563 if (repeats > 6) {
2564 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2565 clearLayoutNeeded();
2566 break;
2567 }
2568
2569 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2570 pendingLayoutChanges);
2571
Andrii Kulian839def92016-11-02 10:58:58 -07002572 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2573 // the wallpaper window jumping across displays.
2574 // Remove check for default display when there will be support for multiple wallpaper
2575 // targets (on different displays).
2576 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002577 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002578 }
2579
2580 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2581 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2582 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2583 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002584 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002585 }
2586 }
2587
2588 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2589 setLayoutNeeded();
2590 }
2591
2592 // FIRST LOOP: Perform a layout, if needed.
2593 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2594 performLayout(repeats == 1, false /* updateInputWindows */);
2595 } else {
2596 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2597 }
2598
2599 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2600 pendingLayoutChanges = 0;
2601
2602 if (isDefaultDisplay) {
2603 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002604 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002605 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2606 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2607 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2608 }
2609 } while (pendingLayoutChanges != 0);
2610
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002612 resetDimming();
2613
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002614 mTmpRecoveringMemory = recoveringMemory;
2615 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002616
2617 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002618 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2619 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2620 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002621 true /* inTraversal, must call performTraversalInTrans... below */);
2622
2623 stopDimmingIfNeeded();
2624
2625 while (!mTmpUpdateAllDrawn.isEmpty()) {
2626 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2627 // See if any windows have been drawn, so they (and others associated with them)
2628 // can now be shown.
2629 atoken.updateAllDrawn(this);
2630 }
2631
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002633 }
2634
2635 void performLayout(boolean initial, boolean updateInputWindows) {
2636 if (!isLayoutNeeded()) {
2637 return;
2638 }
2639 clearLayoutNeeded();
2640
2641 final int dw = mDisplayInfo.logicalWidth;
2642 final int dh = mDisplayInfo.logicalHeight;
2643
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002644 if (DEBUG_LAYOUT) {
2645 Slog.v(TAG, "-------------------------------------");
2646 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2647 }
2648
Andrii Kulian8ee72852017-03-10 10:36:45 -08002649 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002650 getConfiguration().uiMode);
2651 if (isDefaultDisplay) {
2652 // Not needed on non-default displays.
2653 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2654 mService.mScreenRect.set(0, 0, dw, dh);
2655 }
2656
2657 mService.mPolicy.getContentRectLw(mContentRect);
2658
2659 int seq = mService.mLayoutSeq + 1;
2660 if (seq < 0) seq = 0;
2661 mService.mLayoutSeq = seq;
2662
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002663 // Used to indicate that we have processed the dream window and all additional windows are
2664 // behind it.
2665 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002666 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002667
2668 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002669 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002670
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002671 // Used to indicate that we have processed the dream window and all additional attached
2672 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002673 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002674 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002675
2676 // Now perform layout of attached windows, which usually depend on the position of the
2677 // window they are attached to. XXX does not deal with windows that are attached to windows
2678 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002679 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002680
2681 // Window frames may have changed. Tell the input dispatcher about it.
2682 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2683 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2684 if (updateInputWindows) {
2685 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2686 }
2687
2688 mService.mPolicy.finishLayoutLw();
2689 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2690 }
2691
2692 /**
2693 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2694 * In portrait mode, it grabs the full screenshot.
2695 *
2696 * @param width the width of the target bitmap
2697 * @param height the height of the target bitmap
2698 * @param includeFullDisplay true if the screen should not be cropped before capture
2699 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2700 * @param config of the output bitmap
2701 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002702 * @param includeDecor whether to include window decors, like the status or navigation bar
2703 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002704 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002705 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002706 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002707 boolean wallpaperOnly, boolean includeDecor) {
2708 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2709 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002710 if (bitmap == null) {
2711 return null;
2712 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002713
2714 if (DEBUG_SCREENSHOT) {
2715 // TEST IF IT's ALL BLACK
2716 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2717 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2718 bitmap.getHeight());
2719 boolean allBlack = true;
2720 final int firstColor = buffer[0];
2721 for (int i = 0; i < buffer.length; i++) {
2722 if (buffer[i] != firstColor) {
2723 allBlack = false;
2724 break;
2725 }
2726 }
2727 if (allBlack) {
2728 final WindowState appWin = mScreenshotApplicationState.appWin;
2729 final int maxLayer = mScreenshotApplicationState.maxLayer;
2730 final int minLayer = mScreenshotApplicationState.minLayer;
2731 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2732 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2733 (appWin != null ?
2734 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2735 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2736 }
2737 }
2738
2739 // Create a copy of the screenshot that is immutable and backed in ashmem.
2740 // This greatly reduces the overhead of passing the bitmap between processes.
2741 Bitmap ret = bitmap.createAshmemBitmap(config);
2742 bitmap.recycle();
2743 return ret;
2744 }
2745
2746 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2747 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2748 boolean includeDecor) {
2749 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2750 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2751 }
2752
2753 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2754 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2755 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002756 int dw = mDisplayInfo.logicalWidth;
2757 int dh = mDisplayInfo.logicalHeight;
2758 if (dw == 0 || dh == 0) {
2759 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2760 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2761 return null;
2762 }
2763
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002764 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002765
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002766 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002767 final Rect frame = new Rect();
2768 final Rect stackBounds = new Rect();
2769
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002770 boolean includeImeInScreenshot;
2771 synchronized(mService.mWindowMap) {
2772 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2773 ? mService.mInputMethodTarget.mAppToken : null;
2774 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2775 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2776 // mode because the frame of the IME might not overlap with that of the app.
2777 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2778 // overlapping with the bottom app.
2779 includeImeInScreenshot = imeTargetAppToken != null
2780 && imeTargetAppToken.appToken != null
2781 && imeTargetAppToken.appToken.asBinder() == appToken
2782 && !mService.mInputMethodTarget.isInMultiWindowMode();
2783 }
2784
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002785 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002786 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002787 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002788 synchronized(mService.mWindowMap) {
2789 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002790 mScreenshotApplicationState.appWin = null;
2791 forAllWindows(w -> {
2792 if (!w.mHasSurface) {
2793 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002794 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002795 if (w.mLayer >= aboveAppLayer) {
2796 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002797 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002798 if (wallpaperOnly && !w.mIsWallpaper) {
2799 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002800 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002801 if (w.mIsImWindow) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002802 if (!includeImeInScreenshot) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002803 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002804 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002805 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002806 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2807 // then the target window state is this one.
2808 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002809 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002810 }
2811
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002812 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002813 // We have not ran across the target window yet, so it is probably behind
2814 // the wallpaper. This can happen when the keyguard is up and all windows
2815 // are moved behind the wallpaper. We don't want to include the wallpaper
2816 // layer in the screenshot as it will cover-up the layer of the target
2817 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002818 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002819 }
2820 // Fall through. The target window is in front of the wallpaper. For this
2821 // case we want to include the wallpaper layer in the screenshot because
2822 // the target window might have some transparent areas.
2823 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002824 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002825 // This app window is of no interest if it is not associated with the
2826 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002827 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002828 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002829 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002830 }
2831
2832 // Include this window.
2833
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002834 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002835 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002836 if (mScreenshotApplicationState.maxLayer < layer) {
2837 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002838 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002839 if (mScreenshotApplicationState.minLayer > layer) {
2840 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002841 }
2842
2843 // Don't include wallpaper in bounds calculation
Jorim Jaggibfbd9dc2017-01-20 18:13:01 +01002844 if (!mutableIncludeFullDisplay.value && includeDecor) {
2845 final TaskStack stack = w.getStack();
2846 if (stack != null) {
2847 stack.getBounds(frame);
2848 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08002849 } else if (!mutableIncludeFullDisplay.value && !w.mIsWallpaper) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002850 final Rect wf = w.mFrame;
2851 final Rect cr = w.mContentInsets;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002852 int left = wf.left + cr.left;
2853 int top = wf.top + cr.top;
2854 int right = wf.right - cr.right;
2855 int bottom = wf.bottom - cr.bottom;
2856 frame.union(left, top, right, bottom);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002857 w.getVisibleBounds(stackBounds);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002858 if (!Rect.intersects(frame, stackBounds)) {
2859 // Set frame empty if there's no intersection.
2860 frame.setEmpty();
2861 }
2862 }
2863
2864 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002865 (w.mAppToken != null && w.mAppToken.token == appToken)
2866 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002867 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002868 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002869 }
2870
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002871 if (w.isObscuringDisplay()){
2872 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002873 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002874 return false;
2875 }, true /* traverseTopToBottom */);
2876
2877 final WindowState appWin = mScreenshotApplicationState.appWin;
2878 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2879 final int maxLayer = mScreenshotApplicationState.maxLayer;
2880 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002881
2882 if (appToken != null && appWin == null) {
2883 // Can't find a window to snapshot.
2884 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2885 "Screenshot: Couldn't find a surface matching " + appToken);
2886 return null;
2887 }
2888
2889 if (!screenshotReady) {
2890 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2891 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2892 appWin.mWinAnimator.mDrawState)));
2893 return null;
2894 }
2895
2896 // Screenshot is ready to be taken. Everything from here below will continue
2897 // through the bottom of the loop and return a value. We only stay in the loop
2898 // because we don't want to release the mWindowMap lock until the screenshot is
2899 // taken.
2900
2901 if (maxLayer == 0) {
2902 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2903 + ": returning null maxLayer=" + maxLayer);
2904 return null;
2905 }
2906
Jorim Jaggi02886a82016-12-06 09:10:06 -08002907 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002908 // Constrain frame to the screen size.
2909 if (!frame.intersect(0, 0, dw, dh)) {
2910 frame.setEmpty();
2911 }
2912 } else {
2913 // Caller just wants entire display.
2914 frame.set(0, 0, dw, dh);
2915 }
2916 if (frame.isEmpty()) {
2917 return null;
2918 }
2919
2920 if (width < 0) {
2921 width = (int) (frame.width() * frameScale);
2922 }
2923 if (height < 0) {
2924 height = (int) (frame.height() * frameScale);
2925 }
2926
2927 // Tell surface flinger what part of the image to crop. Take the top
2928 // right part of the application, and crop the larger dimension to fit.
2929 Rect crop = new Rect(frame);
2930 if (width / (float) frame.width() < height / (float) frame.height()) {
2931 int cropWidth = (int)((float)width / (float)height * frame.height());
2932 crop.right = crop.left + cropWidth;
2933 } else {
2934 int cropHeight = (int)((float)height / (float)width * frame.width());
2935 crop.bottom = crop.top + cropHeight;
2936 }
2937
2938 // The screenshot API does not apply the current screen rotation.
2939 int rot = mDisplay.getRotation();
2940
2941 if (rot == ROTATION_90 || rot == ROTATION_270) {
2942 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2943 }
2944
2945 // Surfaceflinger is not aware of orientation, so convert our logical
2946 // crop to surfaceflinger's portrait orientation.
2947 convertCropForSurfaceFlinger(crop, rot, dw, dh);
2948
2949 if (DEBUG_SCREENSHOT) {
2950 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
2951 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002952 forAllWindows(w -> {
2953 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
2954 Slog.i(TAG_WM, w + ": " + w.mLayer
2955 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002956 + " surfaceLayer=" + ((controller == null)
2957 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002958 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002959 }
2960
2961 final ScreenRotationAnimation screenRotationAnimation =
2962 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2963 final boolean inRotation = screenRotationAnimation != null &&
2964 screenRotationAnimation.isAnimating();
2965 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
2966 "Taking screenshot while rotating");
2967
2968 // We force pending transactions to flush before taking
2969 // the screenshot by pushing an empty synchronous transaction.
2970 SurfaceControl.openTransaction();
2971 SurfaceControl.closeTransactionSync();
2972
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002973 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002974 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002975 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002976 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
2977 + ") to layer " + maxLayer);
2978 return null;
2979 }
2980 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002981 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002982 }
2983
2984 // TODO: Can this use createRotationMatrix()?
2985 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
2986 if (rot == Surface.ROTATION_90) {
2987 final int tmp = crop.top;
2988 crop.top = dw - crop.right;
2989 crop.right = crop.bottom;
2990 crop.bottom = dw - crop.left;
2991 crop.left = tmp;
2992 } else if (rot == Surface.ROTATION_180) {
2993 int tmp = crop.top;
2994 crop.top = dh - crop.bottom;
2995 crop.bottom = dh - tmp;
2996 tmp = crop.right;
2997 crop.right = dw - crop.left;
2998 crop.left = dw - tmp;
2999 } else if (rot == Surface.ROTATION_270) {
3000 final int tmp = crop.top;
3001 crop.top = crop.left;
3002 crop.left = dh - crop.bottom;
3003 crop.bottom = crop.right;
3004 crop.right = dh - tmp;
3005 }
3006 }
3007
3008 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003009 // Used to indicate the layout is needed.
3010 mTmpWindow = null;
3011
3012 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003013 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003014 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003015 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003016 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003017 w.setDisplayLayoutNeeded();
3018 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003019 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003020
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003021 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003022 mService.mWindowPlacerLocked.performSurfacePlacement();
3023 }
3024 }
3025
Wale Ogunwale1666e312016-12-16 11:27:18 -08003026 void setExitingTokensHasVisible(boolean hasVisible) {
3027 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3028 mExitingTokens.get(i).hasVisible = hasVisible;
3029 }
3030
3031 // Initialize state of exiting applications.
3032 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3033 }
3034
3035 void removeExistingTokensIfPossible() {
3036 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3037 final WindowToken token = mExitingTokens.get(i);
3038 if (!token.hasVisible) {
3039 mExitingTokens.remove(i);
3040 }
3041 }
3042
3043 // Time to remove any exiting applications?
3044 mTaskStackContainers.removeExistingAppTokensIfPossible();
3045 }
3046
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003047 static final class TaskForResizePointSearchResult {
3048 boolean searchDone;
3049 Task taskForResize;
3050
3051 void reset() {
3052 searchDone = false;
3053 taskForResize = null;
3054 }
3055 }
Robert Carr3b716242016-08-16 16:02:21 -07003056
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003057 private static final class ApplySurfaceChangesTransactionState {
3058 boolean displayHasContent;
3059 boolean obscured;
3060 boolean syswin;
3061 boolean focusDisplayed;
3062 float preferredRefreshRate;
3063 int preferredModeId;
3064
3065 void reset() {
3066 displayHasContent = false;
3067 obscured = false;
3068 syswin = false;
3069 focusDisplayed = false;
3070 preferredRefreshRate = 0;
3071 preferredModeId = 0;
3072 }
3073 }
3074
3075 private static final class ScreenshotApplicationState {
3076 WindowState appWin;
3077 int maxLayer;
3078 int minLayer;
3079 boolean screenshotReady;
3080
3081 void reset(boolean screenshotReady) {
3082 appWin = null;
3083 maxLayer = 0;
3084 minLayer = 0;
3085 this.screenshotReady = screenshotReady;
3086 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3087 }
3088 }
3089
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003090 /**
3091 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3092 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3093 * homogeneous children type which is currently required by sub-classes of
3094 * {@link WindowContainer} class.
3095 */
3096 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3097
3098 int size() {
3099 return mChildren.size();
3100 }
3101
3102 E get(int index) {
3103 return mChildren.get(index);
3104 }
3105
3106 @Override
3107 boolean fillsParent() {
3108 return true;
3109 }
3110
3111 @Override
3112 boolean isVisible() {
3113 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003114 }
3115 }
3116
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003117 /**
3118 * Window container class that contains all containers on this display relating to Apps.
3119 * I.e Activities.
3120 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003121 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003122
Andrii Kulian839def92016-11-02 10:58:58 -07003123 /**
3124 * Adds the stack to this container.
3125 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3126 */
3127 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003128 if (stack.mStackId == HOME_STACK_ID) {
3129 if (mHomeStack != null) {
3130 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3131 }
3132 mHomeStack = stack;
3133 }
3134 addChild(stack, onTop);
3135 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003136 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003137
Andrii Kulian839def92016-11-02 10:58:58 -07003138 /** Removes the stack from its container and prepare for changing the parent. */
3139 void removeStackFromDisplay(TaskStack stack) {
3140 removeChild(stack);
3141 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003142 }
3143
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003144 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003145 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3146 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003147 addChild(stack, addIndex);
3148 setLayoutNeeded();
3149 }
3150
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003151 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003152 void positionChildAt(int position, TaskStack child, boolean includingParents) {
3153 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
3154 // This stack is always-on-top, override the default behavior.
3155 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3156
3157 // Moving to its current position, as we must call super but we don't want to
3158 // perform any meaningful action.
3159 final int currentPosition = mChildren.indexOf(child);
3160 super.positionChildAt(currentPosition, child, false /* includingParents */);
3161 return;
3162 }
3163
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003164 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3165 super.positionChildAt(targetPosition, child, includingParents);
3166
3167 setLayoutNeeded();
3168 }
3169
3170 /**
3171 * When stack is added or repositioned, find a proper position for it.
3172 * This will make sure that pinned stack always stays on top.
3173 * @param requestedPosition Position requested by caller.
3174 * @param stack Stack to be added or positioned.
3175 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3176 * @return The proper position for the stack.
3177 */
3178 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3179 final int topChildPosition = mChildren.size() - 1;
3180 boolean toTop = requestedPosition == POSITION_TOP;
3181 toTop |= adding ? requestedPosition >= topChildPosition + 1
3182 : requestedPosition >= topChildPosition;
3183 int targetPosition = requestedPosition;
3184
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003185 if (toTop && isStackVisible(PINNED_STACK_ID) && stack.mStackId != PINNED_STACK_ID) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003186 // The pinned stack is always the top most stack (always-on-top) when it is visible.
3187 TaskStack topStack = mChildren.get(topChildPosition);
3188 if (topStack.mStackId != PINNED_STACK_ID) {
3189 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3190 }
3191
3192 // So, stack is moved just below the pinned stack.
3193 // When we're adding a new stack the target is the current pinned stack position.
3194 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003195 // stack, because WindowContainer#positionAt() first removes element and then adds
3196 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003197 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3198 }
3199
3200 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003201 }
3202
3203 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003204 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3205 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003206 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003207 if (super.forAllWindows(callback, traverseTopToBottom)) {
3208 return true;
3209 }
3210 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3211 return true;
3212 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003213 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003214 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3215 return true;
3216 }
3217 if (super.forAllWindows(callback, traverseTopToBottom)) {
3218 return true;
3219 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003220 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003221 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003222 }
3223
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003224 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003225 boolean traverseTopToBottom) {
3226 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3227 // app tokens.
3228 // TODO: Investigate if we need to continue to do this or if we can just process them
3229 // in-order.
3230 if (traverseTopToBottom) {
3231 for (int i = mChildren.size() - 1; i >= 0; --i) {
3232 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3233 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003234 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3235 traverseTopToBottom)) {
3236 return true;
3237 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003238 }
3239 }
3240 } else {
3241 final int count = mChildren.size();
3242 for (int i = 0; i < count; ++i) {
3243 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3244 final int appTokensCount = appTokens.size();
3245 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003246 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3247 traverseTopToBottom)) {
3248 return true;
3249 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003250 }
3251 }
3252 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003253 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003254 }
3255
Wale Ogunwale1666e312016-12-16 11:27:18 -08003256 void setExitingTokensHasVisible(boolean hasVisible) {
3257 for (int i = mChildren.size() - 1; i >= 0; --i) {
3258 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3259 for (int j = appTokens.size() - 1; j >= 0; --j) {
3260 appTokens.get(j).hasVisible = hasVisible;
3261 }
3262 }
3263 }
3264
3265 void removeExistingAppTokensIfPossible() {
3266 for (int i = mChildren.size() - 1; i >= 0; --i) {
3267 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3268 for (int j = appTokens.size() - 1; j >= 0; --j) {
3269 final AppWindowToken token = appTokens.get(j);
3270 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3271 && (!token.mIsExiting || token.isEmpty())) {
3272 // Make sure there is no animation running on this token, so any windows
3273 // associated with it will be removed as soon as their animations are
3274 // complete.
3275 token.mAppAnimator.clearAnimation();
3276 token.mAppAnimator.animating = false;
3277 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3278 "performLayout: App token exiting now removed" + token);
3279 token.removeIfPossible();
3280 }
3281 }
3282 }
3283 }
3284
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003285 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003286 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003287 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003288 // Apps and their containers are not allowed to specify an orientation while the
3289 // docked or freeform stack is visible...except for the home stack/task if the
3290 // docked stack is minimized and it actually set something.
3291 if (mHomeStack != null && mHomeStack.isVisible()
3292 && mDividerControllerLocked.isMinimizedDock()) {
3293 final int orientation = mHomeStack.getOrientation();
3294 if (orientation != SCREEN_ORIENTATION_UNSET) {
3295 return orientation;
3296 }
3297 }
3298 return SCREEN_ORIENTATION_UNSPECIFIED;
3299 }
3300
3301 final int orientation = super.getOrientation();
3302 if (orientation != SCREEN_ORIENTATION_UNSET
3303 && orientation != SCREEN_ORIENTATION_BEHIND) {
3304 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3305 "App is requesting an orientation, return " + orientation);
3306 return orientation;
3307 }
3308
3309 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003310 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003311 // The next app has not been requested to be visible, so we keep the current orientation
3312 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003313 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003314 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003315 }
3316
3317 /**
3318 * Window container class that contains all containers on this display that are not related to
3319 * Apps. E.g. status bar.
3320 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003321 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3322 /**
3323 * Compares two child window tokens returns -1 if the first is lesser than the second in
3324 * terms of z-order and 1 otherwise.
3325 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003326 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003327 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003328 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3329 token1.mOwnerCanManageAppTokens)
3330 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3331 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003332
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003333 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3334 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3335 return false;
3336 }
3337 final int req = w.mAttrs.screenOrientation;
3338 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3339 || req == SCREEN_ORIENTATION_UNSET) {
3340 return false;
3341 }
3342 return true;
3343 };
3344
Wale Ogunwale3a931692016-11-02 16:49:48 -07003345 private final String mName;
3346 NonAppWindowContainers(String name) {
3347 mName = name;
3348 }
3349
3350 void addChild(WindowToken token) {
3351 addChild(token, mWindowComparator);
3352 }
3353
3354 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003355 int getOrientation() {
3356 final WindowManagerPolicy policy = mService.mPolicy;
3357 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003358 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003359
3360 if (win != null) {
3361 final int req = win.mAttrs.screenOrientation;
3362 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
3363 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003364 mLastKeyguardForcedOrientation = req;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003365 }
Andrii Kulian8ee72852017-03-10 10:36:45 -08003366 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003367 }
3368
Andrii Kulian8ee72852017-03-10 10:36:45 -08003369 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003370
3371 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003372 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003373 }
3374
3375 return SCREEN_ORIENTATION_UNSET;
3376 }
3377
3378 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003379 String getName() {
3380 return mName;
3381 }
Robert Carr3b716242016-08-16 16:02:21 -07003382 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003383
3384 /**
3385 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3386 */
3387 @FunctionalInterface
3388 private interface Screenshoter<E> {
3389 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3390 boolean useIdentityTransform, int rotation);
3391 }
Craig Mautner59c00972012-07-30 12:10:24 -07003392}