blob: 10f919cbe1a87e326c0262be5019bdfcea53208e [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 Ogunwale10124582016-09-15 20:25:50 -070032import static android.view.WindowManager.DOCKED_BOTTOM;
33import static android.view.WindowManager.DOCKED_INVALID;
34import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080035import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
36import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
37import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070038import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwaleec731152016-09-08 20:18:57 -070039import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
40import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070041import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070042import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
43import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale10124582016-09-15 20:25:50 -070044import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070045import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
46import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070047import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwaleec731152016-09-08 20:18:57 -070048import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080049import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
Wale Ogunwale51362492016-09-08 17:49:17 -070050import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080051import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070052import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070053import static com.android.server.wm.WindowManagerService.dipToPixel;
54import static com.android.server.wm.WindowManagerService.localLOGV;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070055import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -070056
Andrii Kulian3a507b52016-09-19 18:14:12 -070057import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -070058import android.app.ActivityManager.StackId;
Andrii Kulian441e4492016-09-29 15:25:00 -070059import android.content.res.Configuration;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070060import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -080061import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070062import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -070063import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +010064import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070065import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070066import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070067import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -070068import android.os.IBinder;
Chong Zhang8e89b312015-09-09 15:09:30 -070069import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -070070import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -070071import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -070072import android.view.DisplayInfo;
Wale Ogunwaleec731152016-09-08 20:18:57 -070073import android.view.IWindow;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070074
Wale Ogunwale9adfe572016-09-08 20:43:58 -070075import com.android.internal.util.FastPrintWriter;
Craig Mautner59c00972012-07-30 12:10:24 -070076
Robert Carr3b716242016-08-16 16:02:21 -070077import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -070078import java.io.PrintWriter;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070079import java.io.StringWriter;
Craig Mautner59c00972012-07-30 12:10:24 -070080import java.util.ArrayList;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070081import java.util.Arrays;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070082import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -070083import java.util.HashMap;
84import java.util.Iterator;
Andrii Kulian3a507b52016-09-19 18:14:12 -070085import java.util.List;
Craig Mautner59c00972012-07-30 12:10:24 -070086
Craig Mautner59c00972012-07-30 12:10:24 -070087/**
88 * Utility class for keeping track of the WindowStates and other pertinent contents of a
89 * particular Display.
90 *
91 * IMPORTANT: No method from this class should ever be used without holding
92 * WindowManagerService.mWindowMap.
93 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -070094class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Craig Mautner59c00972012-07-30 12:10:24 -070095
96 /** Unique identifier of this stack. */
97 private final int mDisplayId;
98
Wale Ogunwale19e452e2016-10-12 12:36:29 -070099 // The display only has 2 child window containers. mTaskStackContainers which contains all
100 // window containers that are related to apps (Activities) and mNonAppWindowContainers which
101 // contains all window containers not related to apps (e.g. Status bar).
102 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
103 private final NonAppWindowContainers mNonAppWindowContainers = new NonAppWindowContainers();
104
Craig Mautner59c00972012-07-30 12:10:24 -0700105 /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element
106 * from mDisplayWindows; */
Craig Mautnerdc548482014-02-05 13:35:24 -0800107 private final WindowList mWindows = new WindowList();
Craig Mautner59c00972012-07-30 12:10:24 -0700108
Wale Ogunwale02319a62016-09-26 15:21:22 -0700109 // Mapping from a token IBinder to a WindowToken object on this display.
110 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
111
Craig Mautner59c00972012-07-30 12:10:24 -0700112 int mInitialDisplayWidth = 0;
113 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700114 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -0700115 int mBaseDisplayWidth = 0;
116 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700117 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700118 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700119 private final DisplayInfo mDisplayInfo = new DisplayInfo();
120 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700121 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Craig Mautner59c00972012-07-30 12:10:24 -0700122
Craig Mautner6601b7b2013-04-29 10:29:11 -0700123 Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700124 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700125
Craig Mautner39834192012-09-02 07:47:24 -0700126 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700127 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700128 int pendingLayoutChanges;
Craig Mautner69b08182012-09-05 13:07:13 -0700129 final boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700130
Craig Mautnerdc548482014-02-05 13:35:24 -0800131 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700132 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800133
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800134 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
135 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700136 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700137
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700138 /** Detect user tapping outside of current focused task bounds .*/
139 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700140
Craig Mautner6601b7b2013-04-29 10:29:11 -0700141 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700142 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700143
Craig Mautner6601b7b2013-04-29 10:29:11 -0700144 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800145 private final Rect mTmpRect = new Rect();
146 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700147 private final RectF mTmpRectF = new RectF();
148 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800149 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700150
Craig Mautner9d808b12013-08-06 18:00:25 -0700151 final WindowManagerService mService;
152
Craig Mautner95da1082014-02-24 17:54:35 -0800153 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700154 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800155
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700156 final DockedStackDividerController mDividerControllerLocked;
157
Chong Zhang112eb8c2015-11-02 11:17:00 -0800158 final DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700159
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800160 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
161
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700162 /** Used when rebuilding window list to keep track of windows that have been removed. */
163 private WindowState[] mRebuildTmp = new WindowState[20];
164
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700165 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
166 new TaskForResizePointSearchResult();
167 private final GetWindowOnDisplaySearchResult mTmpGetWindowOnDisplaySearchResult =
168 new GetWindowOnDisplaySearchResult();
169
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800170 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700171 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800172 * @param service You know.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700173 */
Craig Mautner9d808b12013-08-06 18:00:25 -0700174 DisplayContent(Display display, WindowManagerService service) {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700175 mDisplay = display;
176 mDisplayId = display.getDisplayId();
177 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700178 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700179 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700180 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700181 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800182 mDividerControllerLocked = new DockedStackDividerController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800183 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700184
185 // These are the only direct children we should ever have and they are permanent.
186 super.addChild(mTaskStackContainers, null);
187 super.addChild(mNonAppWindowContainers, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700188 }
189
190 int getDisplayId() {
191 return mDisplayId;
192 }
193
194 WindowList getWindowList() {
195 return mWindows;
196 }
197
Wale Ogunwale02319a62016-09-26 15:21:22 -0700198 WindowToken getWindowToken(IBinder binder) {
199 return mTokenMap.get(binder);
200 }
201
202 AppWindowToken getAppWindowToken(IBinder binder) {
203 final WindowToken token = getWindowToken(binder);
204 if (token == null) {
205 return null;
206 }
207 return token.asAppWindowToken();
208 }
209
210 void setWindowToken(IBinder binder, WindowToken token) {
211 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
212 if (dc != null) {
213 // We currently don't support adding a window token to the display if the display
214 // already has the binder mapped to another token. If there is a use case for supporting
215 // this moving forward we will either need to merge the WindowTokens some how or have
216 // the binder map to a list of window tokens.
217 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
218 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
219 }
220 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700221
222 if (token.asAppWindowToken() == null) {
223 // Add non-app token to container hierarchy on the display. App tokens are added through
224 // the parent container managing them (e.g. Tasks).
225 mNonAppWindowContainers.addChild(token, null);
226 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700227 }
228
229 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700230 final WindowToken token = mTokenMap.remove(binder);
231 if (token != null && token.asAppWindowToken() == null) {
232 mNonAppWindowContainers.removeChild(token);
233 }
234 return token;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700235 }
236
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700237 Display getDisplay() {
238 return mDisplay;
239 }
240
Craig Mautner59c00972012-07-30 12:10:24 -0700241 DisplayInfo getDisplayInfo() {
242 return mDisplayInfo;
243 }
244
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700245 DisplayMetrics getDisplayMetrics() {
246 return mDisplayMetrics;
247 }
248
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100249 DockedStackDividerController getDockedDividerController() {
250 return mDividerControllerLocked;
251 }
252
Jeff Browna506a6e2013-06-04 00:02:38 -0700253 /**
254 * Returns true if the specified UID has access to this display.
255 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700256 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700257 return mDisplay.hasAccess(uid);
258 }
259
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700260 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700261 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -0700262 }
263
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700264 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700265 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800266 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800267 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700268 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700269 }
270
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700271 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700272 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
273 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700274 if (stack.mStackId == stackId) {
275 return stack;
276 }
277 }
278 return null;
279 }
280
Andrii Kulian441e4492016-09-29 15:25:00 -0700281 @Override
282 void onConfigurationChanged(Configuration newParentConfig) {
283 super.onConfigurationChanged(newParentConfig);
284
Andrii Kulian3a507b52016-09-19 18:14:12 -0700285 // The display size information is heavily dependent on the resources in the current
286 // configuration, so we need to reconfigure it every time the configuration changes.
287 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
288 mService.reconfigureDisplayLocked(this);
289
290 getDockedDividerController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700291 }
Andrii Kulian3a507b52016-09-19 18:14:12 -0700292
Andrii Kulian441e4492016-09-29 15:25:00 -0700293 /**
294 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
295 * bounds were updated.
296 */
297 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700298 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
299 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700300 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -0700301 changedStackList.add(stack.mStackId);
302 }
303 }
304 }
305
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700306 @Override
307 boolean fillsParent() {
308 return true;
309 }
310
311 @Override
312 boolean isVisible() {
313 return true;
314 }
315
316 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700317 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700318 super.onAppTransitionDone();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700319 rebuildAppWindowList();
320 }
321
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700322 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700323 int getOrientation() {
Wale Ogunwale51362492016-09-08 17:49:17 -0700324 if (mService.isStackVisibleLocked(DOCKED_STACK_ID)
325 || mService.isStackVisibleLocked(FREEFORM_WORKSPACE_STACK_ID)) {
326 // Apps and their containers are not allowed to specify an orientation while the docked
327 // or freeform stack is visible...except for the home stack/task if the docked stack is
328 // minimized and it actually set something.
Andrii Kulian8072d112016-09-16 11:11:01 -0700329 if (mHomeStack != null && mHomeStack.isVisible()
330 && mDividerControllerLocked.isMinimizedDock()) {
Wale Ogunwale51362492016-09-08 17:49:17 -0700331 final int orientation = mHomeStack.getOrientation();
332 if (orientation != SCREEN_ORIENTATION_UNSET) {
333 return orientation;
334 }
335 }
336 return SCREEN_ORIENTATION_UNSPECIFIED;
337 }
338
Wale Ogunwale10124582016-09-15 20:25:50 -0700339 final int orientation = super.getOrientation();
Wale Ogunwalea77e1462016-09-28 10:09:46 -0700340 if (orientation != SCREEN_ORIENTATION_UNSET && orientation != SCREEN_ORIENTATION_BEHIND) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700341 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
342 "App is requesting an orientation, return " + orientation);
343 return orientation;
Wale Ogunwale51362492016-09-08 17:49:17 -0700344 }
345
346 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
347 "No app is requesting an orientation, return " + mService.mLastOrientation);
348 // The next app has not been requested to be visible, so we keep the current orientation
349 // to prevent freezing/unfreezing the display too early.
350 return mService.mLastOrientation;
351 }
352
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700353 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700354 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700355 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700356 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
357 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800358 }
Craig Mautner722285e2012-09-07 13:55:58 -0700359 }
360
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700361 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700362 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
363 if (displayManagerInternal != null) {
364 // Bootstrap the default logical display from the display manager.
365 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
366 if (newDisplayInfo != null) {
367 mDisplayInfo.copyFrom(newDisplayInfo);
368 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700369 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700370
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800371 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
372 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
373 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
374 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700375 }
376
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700377 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700378 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700380 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
382 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700383 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800384 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700385 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800386 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700387 out.set(left, top, left + width, top + height);
388 }
389
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700390 private void getLogicalDisplayRect(Rect out, int orientation) {
391 getLogicalDisplayRect(out);
392
393 // Rotate the Rect if needed.
394 final int currentRotation = mDisplayInfo.rotation;
395 final int rotationDelta = deltaRotation(currentRotation, orientation);
396 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
397 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
398 mTmpRectF.set(out);
399 mTmpMatrix.mapRect(mTmpRectF);
400 mTmpRectF.round(out);
401 }
402 }
403
Chong Zhangf66db432016-01-13 10:39:51 -0800404 void getContentRect(Rect out) {
405 out.set(mContentRect);
406 }
407
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700408 /** Refer to {@link WindowManagerService#attachStack(int, int, boolean)} */
409 void attachStack(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700410 mTaskStackContainers.attachStack(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800411 }
412
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800413 void moveStack(TaskStack stack, boolean toTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700414 mTaskStackContainers.moveStack(stack, toTop);
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700415 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700416
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700417 @Override
418 protected void addChild(DisplayChildWindowContainer child,
419 Comparator<DisplayChildWindowContainer> comparator) {
420 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
421 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700422
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700423 @Override
424 protected void addChild(DisplayChildWindowContainer child, int index) {
425 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
426 }
427
428 @Override
429 protected void removeChild(DisplayChildWindowContainer child) {
430 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800431 }
432
433 /**
434 * Propagate the new bounds to all child stacks.
435 * @param contentRect The bounds to apply at the top level.
436 */
437 void resize(Rect contentRect) {
438 mContentRect.set(contentRect);
439 }
440
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700441 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700442 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
443 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700444 final int taskId = stack.taskIdFromPoint(x, y);
445 if (taskId != -1) {
446 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -0700447 }
448 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800449 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -0700450 }
451
Chong Zhang8e89b312015-09-09 15:09:30 -0700452 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -0800453 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -0700454 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -0700455 */
Wale Ogunwale15ead902016-09-02 14:30:11 -0700456 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700457 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700458 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700459 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
460 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -0700461 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700462 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700463 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700464
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700465 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
466 if (mTmpTaskForResizePointSearchResult.searchDone) {
467 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -0700468 }
469 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700470 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700471 }
472
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700473 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -0700474 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700475 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700476 mTmpRect2.setEmpty();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700477 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
478 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700479 stack.setTouchExcludeRegion(
480 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -0700481 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800482 // If we removed the focused task above, add it back and only leave its
483 // outside touch area in the exclusion. TapDectector is not interested in
484 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700485 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800486 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
487 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800488 final WindowState inputMethod = mService.mInputMethodWindow;
489 if (inputMethod != null && inputMethod.isVisibleLw()) {
490 // If the input method is visible and the user is typing, we don't want these touch
491 // events to be intercepted and used to change focus. This would likely cause a
492 // disappearance of the input method.
493 inputMethod.getTouchableRegion(mTmpRegion);
494 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
495 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800496 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
497 WindowState win = mTapExcludedWindows.get(i);
498 win.getTouchableRegion(mTmpRegion);
499 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
500 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100501 if (getDockedStackVisibleForUserLocked() != null) {
502 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -0700503 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100504 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
505 }
Craig Mautner1bef3892015-02-17 15:09:47 -0800506 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700507 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -0800508 }
Craig Mautner6601b7b2013-04-29 10:29:11 -0700509 }
510
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700511 void switchUser() {
Craig Mautner858d8a62013-04-23 17:08:34 -0700512 final WindowList windows = getWindowList();
513 for (int i = 0; i < windows.size(); i++) {
514 final WindowState win = windows.get(i);
515 if (win.isHiddenFromUserLocked()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800516 if (DEBUG_VISIBILITY) Slog.w(TAG_WM, "user changing, hiding " + win
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800517 + ", attrs=" + win.mAttrs.type + ", belonging to " + win.mOwnerUid);
Craig Mautner858d8a62013-04-23 17:08:34 -0700518 win.hideLw(false);
519 }
520 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700521
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700522 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
523 mTaskStackContainers.get(stackNdx).switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -0700524 }
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700525
526 rebuildAppWindowList();
Craig Mautner858d8a62013-04-23 17:08:34 -0700527 }
528
Craig Mautner05d29032013-05-03 13:40:13 -0700529 void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700530 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
531 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -0700532 }
533 }
534
535 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800536 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -0700537 }
538
539 void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800540 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700541 }
542
543 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800544 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700545 }
546
547 void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800548 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -0700549 }
550
Wale Ogunwale10124582016-09-15 20:25:50 -0700551 @Override
552 void removeIfPossible() {
553 if (isAnimating()) {
554 mDeferredRemoval = true;
555 return;
Craig Mautner2eb15342013-08-07 13:13:35 -0700556 }
Wale Ogunwale10124582016-09-15 20:25:50 -0700557 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -0700558 }
559
Wale Ogunwale10124582016-09-15 20:25:50 -0700560 @Override
561 void removeImmediately() {
562 super.removeImmediately();
563 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Wale Ogunwale10124582016-09-15 20:25:50 -0700564 mDimLayerController.close();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700565 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700566 mService.unregisterPointerEventListener(mTapDetector);
567 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
Craig Mautner95da1082014-02-24 17:54:35 -0800568 }
Craig Mautner95da1082014-02-24 17:54:35 -0800569 }
570
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700571 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -0700572 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700573 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700574 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
575
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700576 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700577 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -0800578 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700579 return false;
Craig Mautner95da1082014-02-24 17:54:35 -0800580 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700581 return true;
Craig Mautner95da1082014-02-24 17:54:35 -0800582 }
583
Wale Ogunwale10124582016-09-15 20:25:50 -0700584 boolean animateForIme(float interpolatedValue, float animationTarget,
585 float dividerAnimationTarget) {
586 boolean updated = false;
587
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700588 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
589 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700590 if (stack == null || !stack.isAdjustedForIme()) {
591 continue;
592 }
593
594 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
595 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
596 updated = true;
597 } else {
598 mDividerControllerLocked.mLastAnimationProgress =
599 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
600 mDividerControllerLocked.mLastDividerProgress =
601 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
602 updated |= stack.updateAdjustForIme(
603 mDividerControllerLocked.mLastAnimationProgress,
604 mDividerControllerLocked.mLastDividerProgress,
605 false /* force */);
606 }
607 if (interpolatedValue >= 1f) {
608 stack.endImeAdjustAnimation();
609 }
610 }
611
612 return updated;
613 }
614
615 boolean clearImeAdjustAnimation() {
616 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700617 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
618 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700619 if (stack != null && stack.isAdjustedForIme()) {
620 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
621 changed = true;
622 }
623 }
624 return changed;
625 }
626
627 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700628 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
629 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700630 if (stack.isVisible() && stack.isAdjustedForIme()) {
631 stack.beginImeAdjustAnimation();
632 }
633 }
634 }
635
636 void adjustForImeIfNeeded() {
637 final WindowState imeWin = mService.mInputMethodWindow;
638 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
639 && !mDividerControllerLocked.isImeHideRequested();
640 final boolean dockVisible = mService.isStackVisibleLocked(DOCKED_STACK_ID);
641 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
642 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
643 imeTargetStack.getDockSide() : DOCKED_INVALID;
644 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
645 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
646 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
647 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
648 final boolean imeHeightChanged = imeVisible &&
649 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
650
651 // The divider could be adjusted for IME position, or be thinner than usual,
652 // or both. There are three possible cases:
653 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
654 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
655 // - If IME is not visible, divider is not moved and is normal width.
656
657 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700658 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
659 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700660 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
661 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
662 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
663 } else {
664 stack.resetAdjustedForIme(false);
665 }
666 }
667 mDividerControllerLocked.setAdjustedForIme(
668 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
669 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700670 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
671 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700672 stack.resetAdjustedForIme(!dockVisible);
673 }
674 mDividerControllerLocked.setAdjustedForIme(
675 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
676 }
677 }
678
679 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700680 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
681 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -0700682 stack.prepareFreezingTaskBounds();
683 }
684 }
685
Wale Ogunwale94744212015-09-21 19:01:47 -0700686 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700687 getLogicalDisplayRect(mTmpRect, newRotation);
688
689 // Compute a transform matrix to undo the coordinate space transformation,
690 // and present the window at the same physical position it previously occupied.
691 final int deltaRotation = deltaRotation(newRotation, oldRotation);
692 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
693
694 mTmpRectF.set(bounds);
695 mTmpMatrix.mapRect(mTmpRectF);
696 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -0700697 }
698
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800699 static int deltaRotation(int oldRotation, int newRotation) {
700 int delta = newRotation - oldRotation;
701 if (delta < 0) delta += 4;
702 return delta;
703 }
704
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700705 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700706 Matrix outMatrix) {
707 // For rotations without Z-ordering we don't need the target rectangle's position.
708 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
709 displayHeight, outMatrix);
710 }
711
712 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
713 float displayWidth, float displayHeight, Matrix outMatrix) {
714 switch (rotation) {
715 case ROTATION_0:
716 outMatrix.reset();
717 break;
718 case ROTATION_270:
719 outMatrix.setRotate(270, 0, 0);
720 outMatrix.postTranslate(0, displayHeight);
721 outMatrix.postTranslate(rectTop, 0);
722 break;
723 case ROTATION_180:
724 outMatrix.reset();
725 break;
726 case ROTATION_90:
727 outMatrix.setRotate(90, 0, 0);
728 outMatrix.postTranslate(displayWidth, 0);
729 outMatrix.postTranslate(-rectTop, rectLeft);
730 break;
731 }
732 }
733
Craig Mautnera91f9e22012-09-14 16:22:08 -0700734 public void dump(String prefix, PrintWriter pw) {
735 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
736 final String subPrefix = " " + prefix;
737 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
738 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
739 pw.print("dpi");
740 if (mInitialDisplayWidth != mBaseDisplayWidth
741 || mInitialDisplayHeight != mBaseDisplayHeight
742 || mInitialDisplayDensity != mBaseDisplayDensity) {
743 pw.print(" base=");
744 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
745 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
746 }
Jeff Brownd46747a2015-04-15 19:02:36 -0700747 if (mDisplayScalingDisabled) {
748 pw.println(" noscale");
749 }
Craig Mautnera91f9e22012-09-14 16:22:08 -0700750 pw.print(" cur=");
751 pw.print(mDisplayInfo.logicalWidth);
752 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
753 pw.print(" app=");
754 pw.print(mDisplayInfo.appWidth);
755 pw.print("x"); pw.print(mDisplayInfo.appHeight);
756 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
757 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
758 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
759 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700760 pw.println(subPrefix + "deferred=" + mDeferredRemoval
761 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800762
Craig Mautnerdc548482014-02-05 13:35:24 -0800763 pw.println();
Craig Mautnere8b85fd2014-10-22 09:23:25 -0700764 pw.println(" Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700765 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
766 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800767 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700768 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800769
Craig Mautnerdc548482014-02-05 13:35:24 -0800770 pw.println();
771 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700772 pw.println();
773 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800774 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700775 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700776 pw.print(" Exiting #"); pw.print(i);
777 pw.print(' '); pw.print(token);
778 pw.println(':');
779 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800780 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700781 }
Craig Mautner59c00972012-07-30 12:10:24 -0700782 pw.println();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800783 mDimLayerController.dump(prefix + " ", pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -0700784 pw.println();
785 mDividerControllerLocked.dump(prefix + " ", pw);
Craig Mautner59c00972012-07-30 12:10:24 -0700786 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800787
788 @Override
789 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700790 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700791 }
792
793 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700794 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -0800795 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700796
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800797 /**
798 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
799 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700800 TaskStack getDockedStackLocked() {
Wale Ogunwalee45899a2015-10-01 11:30:34 -0700801 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700802 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700803 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -0800804
805 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800806 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
807 * visible, as long as it's not hidden because the current user doesn't have any tasks there.
808 */
809 TaskStack getDockedStackVisibleForUserLocked() {
810 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700811 return (stack != null && stack.isVisible(true /* ignoreKeyguard */)) ? stack : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800812 }
813
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700814 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -0800815 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
816 WindowState touchedWin = null;
817 final int x = (int) xf;
818 final int y = (int) yf;
819
820 for (int i = mWindows.size() - 1; i >= 0; i--) {
821 WindowState window = mWindows.get(i);
822 final int flags = window.mAttrs.flags;
823 if (!window.isVisibleLw()) {
824 continue;
825 }
826 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
827 continue;
828 }
829
830 window.getVisibleBounds(mTmpRect);
831 if (!mTmpRect.contains(x, y)) {
832 continue;
833 }
834
835 window.getTouchableRegion(mTmpRegion);
836
837 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
838 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
839 touchedWin = window;
840 break;
841 }
842 }
843
844 return touchedWin;
845 }
Jorim Jaggi6626f542016-08-22 13:08:44 -0700846
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700847 boolean canAddToastWindowForUid(int uid) {
848 // We allow one toast window per UID being shown at a time.
849 WindowList windows = getWindowList();
850 final int windowCount = windows.size();
851 for (int i = 0; i < windowCount; i++) {
852 WindowState window = windows.get(i);
853 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == uid
Svet Ganov62a40f82016-09-29 00:43:51 -0700854 && !window.mPermanentlyHidden && !window.mAnimatingExit
855 && !window.mRemoveOnExit) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700856 return false;
857 }
858 }
859 return true;
860 }
861
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700862 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700863 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
864 return;
865 }
866 final int lostFocusUid = oldFocus.mOwnerUid;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700867 final WindowList windows = getWindowList();
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700868 final int windowCount = windows.size();
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700869 final Handler handler = mService.mH;
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700870 for (int i = 0; i < windowCount; i++) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700871 final WindowState window = windows.get(i);
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700872 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == lostFocusUid) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700873 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, window)) {
874 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, window),
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700875 window.mAttrs.hideTimeoutMilliseconds);
876 }
877 }
878 }
879 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700880
881 WindowState findFocusedWindow() {
882 final AppWindowToken focusedApp = mService.mFocusedApp;
883
884 for (int i = mWindows.size() - 1; i >= 0; i--) {
885 final WindowState win = mWindows.get(i);
886
887 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + i + " = " + win
888 + ", flags=" + win.mAttrs.flags + ", canReceive=" + win.canReceiveKeys());
889
890 if (!win.canReceiveKeys()) {
891 continue;
892 }
893
894 final AppWindowToken wtoken = win.mAppToken;
895
896 // If this window's application has been removed, just skip it.
897 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
898 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
899 + (wtoken.removed ? "removed" : "sendingToBottom"));
900 continue;
901 }
902
903 if (focusedApp == null) {
904 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
905 + " using new focus @ " + i + " = " + win);
906 return win;
907 }
908
909 if (!focusedApp.windowsAreFocusable()) {
910 // Current focused app windows aren't focusable...
911 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
912 + " focusable using new focus @ " + i + " = " + win);
913 return win;
914 }
915
916 // Descend through all of the app tokens and find the first that either matches
917 // win.mAppToken (return win) or mFocusedApp (return null).
918 if (wtoken != null && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700919 if (focusedApp.compareTo(wtoken) > 0) {
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700920 // App stack below focused app stack. No focus for you!!!
921 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
922 "findFocusedWindow: Reached focused app=" + focusedApp);
923 return null;
924 }
925 }
926
927 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ "
928 + i + " = " + win);
929 return win;
930 }
931
932 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
933 return null;
934 }
Wale Ogunwaleec731152016-09-08 20:18:57 -0700935
936 int addAppWindowToWindowList(final WindowState win) {
937 final IWindow client = win.mClient;
938
939 WindowList tokenWindowList = getTokenWindowsOnDisplay(win.mToken);
940 if (!tokenWindowList.isEmpty()) {
941 return addAppWindowExisting(win, tokenWindowList);
942 }
943
944 // No windows from this token on this display
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700945 if (localLOGV) Slog.v(TAG_WM, "Figuring out where to add app window "
Wale Ogunwaleec731152016-09-08 20:18:57 -0700946 + client.asBinder() + " (token=" + this + ")");
947
948 final WindowToken wToken = win.mToken;
949
950 // Figure out where the window should go, based on the order of applications.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700951 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700952 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
953 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700954 stack.getWindowOnDisplayBeforeToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
955 if (mTmpGetWindowOnDisplaySearchResult.reachedToken) {
Wale Ogunwaleec731152016-09-08 20:18:57 -0700956 // We have reach the token we are interested in. End search.
957 break;
958 }
959 }
960
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700961 WindowState pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700962
963 // We now know the index into the apps. If we found an app window above, that gives us the
964 // position; else we need to look some more.
965 if (pos != null) {
966 // Move behind any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -0700967 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -0700968 if (atoken != null) {
969 tokenWindowList = getTokenWindowsOnDisplay(atoken);
970 final int NC = tokenWindowList.size();
971 if (NC > 0) {
972 WindowState bottom = tokenWindowList.get(0);
973 if (bottom.mSubLayer < 0) {
974 pos = bottom;
975 }
976 }
977 }
978 addWindowToListBefore(win, pos);
979 return 0;
980 }
981
982 // Continue looking down until we find the first token that has windows on this display.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700983 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700984 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
985 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700986 stack.getWindowOnDisplayAfterToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
987 if (mTmpGetWindowOnDisplaySearchResult.foundWindow != null) {
Wale Ogunwaleec731152016-09-08 20:18:57 -0700988 // We have found a window after the token. End search.
989 break;
990 }
991 }
992
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700993 pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700994
995 if (pos != null) {
996 // Move in front of any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -0700997 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -0700998 if (atoken != null) {
999 final WindowState top = atoken.getTopWindow();
1000 if (top != null && top.mSubLayer >= 0) {
1001 pos = top;
1002 }
1003 }
1004 addWindowToListAfter(win, pos);
1005 return 0;
1006 }
1007
1008 // Just search for the start of this layer.
1009 final int myLayer = win.mBaseLayer;
1010 int i;
1011 for (i = mWindows.size() - 1; i >= 0; --i) {
1012 final WindowState w = mWindows.get(i);
1013 // Dock divider shares the base layer with application windows, but we want to always
1014 // keep it above the application windows. The sharing of the base layer is intended
1015 // for window animations, which need to be above the dock divider for the duration
1016 // of the animation.
1017 if (w.mBaseLayer <= myLayer && w.mAttrs.type != TYPE_DOCK_DIVIDER) {
1018 break;
1019 }
1020 }
1021 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1022 "Based on layer: Adding window " + win + " at " + (i + 1) + " of "
1023 + mWindows.size());
1024 mWindows.add(i + 1, win);
1025 mService.mWindowsChanged = true;
1026 return 0;
1027 }
1028
1029 /** Adds this non-app window to the window list. */
1030 void addNonAppWindowToWindowList(WindowState win) {
1031 // Figure out where window should go, based on layer.
1032 int i;
1033 for (i = mWindows.size() - 1; i >= 0; i--) {
1034 final WindowState otherWin = mWindows.get(i);
1035 if (otherWin.getBaseType() != TYPE_WALLPAPER && otherWin.mBaseLayer <= win.mBaseLayer) {
1036 // Wallpaper wanders through the window list, for example to position itself
1037 // directly behind keyguard. Because of this it will break the ordering based on
1038 // WindowState.mBaseLayer. There might windows with higher mBaseLayer behind it and
1039 // we don't want the new window to appear above them. An example of this is adding
1040 // of the docked stack divider. Consider a scenario with the following ordering (top
1041 // to bottom): keyguard, wallpaper, assist preview, apps. We want the dock divider
1042 // to land below the assist preview, so the dock divider must ignore the wallpaper,
1043 // with which it shares the base layer.
1044 break;
1045 }
1046 }
1047
1048 i++;
1049 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1050 "Free window: Adding window " + this + " at " + i + " of " + mWindows.size());
1051 mWindows.add(i, win);
1052 mService.mWindowsChanged = true;
1053 }
1054
Wale Ogunwaleb9a07c32016-10-12 14:55:56 -07001055 void addToWindowList(WindowState win, int index) {
1056 mWindows.add(index, win);
1057 }
1058
Wale Ogunwaleec731152016-09-08 20:18:57 -07001059 void addChildWindowToWindowList(WindowState win) {
1060 final WindowState parentWindow = win.getParentWindow();
1061
1062 WindowList windowsOnSameDisplay = getTokenWindowsOnDisplay(win.mToken);
1063
1064 // Figure out this window's ordering relative to the parent window.
1065 final int wCount = windowsOnSameDisplay.size();
1066 final int sublayer = win.mSubLayer;
1067 int largestSublayer = Integer.MIN_VALUE;
1068 WindowState windowWithLargestSublayer = null;
1069 int i;
1070 for (i = 0; i < wCount; i++) {
1071 WindowState w = windowsOnSameDisplay.get(i);
1072 final int wSublayer = w.mSubLayer;
1073 if (wSublayer >= largestSublayer) {
1074 largestSublayer = wSublayer;
1075 windowWithLargestSublayer = w;
1076 }
1077 if (sublayer < 0) {
1078 // For negative sublayers, we go below all windows in the same sublayer.
1079 if (wSublayer >= sublayer) {
1080 addWindowToListBefore(win, wSublayer >= 0 ? parentWindow : w);
1081 break;
1082 }
1083 } else {
1084 // For positive sublayers, we go above all windows in the same sublayer.
1085 if (wSublayer > sublayer) {
1086 addWindowToListBefore(win, w);
1087 break;
1088 }
1089 }
1090 }
1091 if (i >= wCount) {
1092 if (sublayer < 0) {
1093 addWindowToListBefore(win, parentWindow);
1094 } else {
1095 addWindowToListAfter(win,
1096 largestSublayer >= 0 ? windowWithLargestSublayer : parentWindow);
1097 }
1098 }
1099 }
1100
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001101 /**
1102 * Z-orders the display window list so that:
1103 * <ul>
1104 * <li>Any windows that are currently below the wallpaper window stay below the wallpaper
1105 * window.
1106 * <li>Exiting application windows are at the bottom, but above the wallpaper window.
1107 * <li>All other application windows are above the exiting application windows and ordered based
1108 * on the ordering of their stacks and tasks on the display.
1109 * <li>Non-application windows are at the very top.
1110 * </ul>
1111 * <p>
1112 * NOTE: This isn't a complete picture of what the user see. Further manipulation of the window
1113 * surface layering is done in {@link WindowLayersController}.
1114 */
1115 void rebuildAppWindowList() {
1116 int count = mWindows.size();
1117 int i;
1118 int lastBelow = -1;
1119 int numRemoved = 0;
1120
1121 if (mRebuildTmp.length < count) {
1122 mRebuildTmp = new WindowState[count + 10];
1123 }
1124
1125 // First remove all existing app windows.
1126 i = 0;
1127 while (i < count) {
1128 final WindowState w = mWindows.get(i);
1129 if (w.mAppToken != null) {
1130 final WindowState win = mWindows.remove(i);
1131 win.mRebuilding = true;
1132 mRebuildTmp[numRemoved] = win;
1133 mService.mWindowsChanged = true;
1134 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Rebuild removing window: " + win);
1135 count--;
1136 numRemoved++;
1137 continue;
1138 } else if (lastBelow == i-1) {
1139 if (w.mAttrs.type == TYPE_WALLPAPER) {
1140 lastBelow = i;
1141 }
1142 }
1143 i++;
1144 }
1145
1146 // Keep whatever windows were below the app windows still below, by skipping them.
1147 lastBelow++;
1148 i = lastBelow;
1149
1150 // First add all of the exiting app tokens... these are no longer in the main app list,
1151 // but still have windows shown. We put them in the back because now that the animation is
1152 // over we no longer will care about them.
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001153 final int numStacks = mTaskStackContainers.size();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001154 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001155 AppTokenList exitingAppTokens = mTaskStackContainers.get(stackNdx).mExitingAppTokens;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001156 int NT = exitingAppTokens.size();
1157 for (int j = 0; j < NT; j++) {
Wale Ogunwale360a8bc2016-10-10 13:25:26 -07001158 i = exitingAppTokens.get(j).rebuildWindowListUnchecked(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001159 }
1160 }
1161
1162 // And add in the still active app tokens in Z order.
1163 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001164 i = mTaskStackContainers.get(stackNdx).rebuildWindowList(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001165 }
1166
1167 i -= lastBelow;
1168 if (i != numRemoved) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001169 setLayoutNeeded();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001170 Slog.w(TAG_WM, "On display=" + mDisplayId + " Rebuild removed " + numRemoved
1171 + " windows but added " + i + " rebuildAppWindowListLocked() "
1172 + " callers=" + Debug.getCallers(10));
1173 for (i = 0; i < numRemoved; i++) {
1174 WindowState ws = mRebuildTmp[i];
1175 if (ws.mRebuilding) {
1176 StringWriter sw = new StringWriter();
1177 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1178 ws.dump(pw, "", true);
1179 pw.flush();
1180 Slog.w(TAG_WM, "This window was lost: " + ws);
1181 Slog.w(TAG_WM, sw.toString());
1182 ws.mWinAnimator.destroySurfaceLocked();
1183 }
1184 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001185 Slog.w(TAG_WM, "Current window hierarchy:");
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001186 dumpChildrenNames();
1187 Slog.w(TAG_WM, "Final window list:");
1188 dumpWindows();
1189 }
1190 Arrays.fill(mRebuildTmp, null);
1191 }
1192
Wale Ogunwaleec731152016-09-08 20:18:57 -07001193 /** Return the list of Windows on this display associated with the input token. */
1194 WindowList getTokenWindowsOnDisplay(WindowToken token) {
1195 final WindowList windowList = new WindowList();
1196 final int count = mWindows.size();
1197 for (int i = 0; i < count; i++) {
1198 final WindowState win = mWindows.get(i);
1199 if (win.mToken == token) {
1200 windowList.add(win);
1201 }
1202 }
1203 return windowList;
1204 }
1205
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001206 void setLayoutNeeded() {
1207 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
1208 mLayoutNeeded = true;
1209 }
1210
1211 void clearLayoutNeeded() {
1212 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
1213 mLayoutNeeded = false;
1214 }
1215
1216 boolean isLayoutNeeded() {
1217 return mLayoutNeeded;
1218 }
1219
Wale Ogunwaleec731152016-09-08 20:18:57 -07001220 private int addAppWindowExisting(WindowState win, WindowList tokenWindowList) {
1221
1222 int tokenWindowsPos;
1223 // If this application has existing windows, we simply place the new window on top of
1224 // them... but keep the starting window on top.
1225 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
1226 // Base windows go behind everything else.
1227 final WindowState lowestWindow = tokenWindowList.get(0);
1228 addWindowToListBefore(win, lowestWindow);
1229 tokenWindowsPos = win.mToken.getWindowIndex(lowestWindow);
1230 } else {
1231 final AppWindowToken atoken = win.mAppToken;
1232 final int windowListPos = tokenWindowList.size();
1233 final WindowState lastWindow = tokenWindowList.get(windowListPos - 1);
1234 if (atoken != null && lastWindow == atoken.startingWindow) {
1235 addWindowToListBefore(win, lastWindow);
1236 tokenWindowsPos = win.mToken.getWindowIndex(lastWindow);
1237 } else {
1238 int newIdx = findIdxBasedOnAppTokens(win);
1239 // There is a window above this one associated with the same apptoken note that the
1240 // window could be a floating window that was created later or a window at the top
1241 // of the list of windows associated with this token.
1242 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1243 "not Base app: Adding window " + win + " at " + (newIdx + 1) + " of "
1244 + mWindows.size());
1245 mWindows.add(newIdx + 1, win);
1246 if (newIdx < 0) {
1247 // No window from token found on win's display.
1248 tokenWindowsPos = 0;
1249 } else {
1250 tokenWindowsPos = win.mToken.getWindowIndex(mWindows.get(newIdx)) + 1;
1251 }
1252 mService.mWindowsChanged = true;
1253 }
1254 }
1255 return tokenWindowsPos;
1256 }
1257
1258 /** Places the first input window after the second input window in the window list. */
1259 private void addWindowToListAfter(WindowState first, WindowState second) {
1260 final int i = mWindows.indexOf(second);
1261 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1262 "Adding window " + this + " at " + (i + 1) + " of " + mWindows.size()
1263 + " (after " + second + ")");
1264 mWindows.add(i + 1, first);
1265 mService.mWindowsChanged = true;
1266 }
1267
1268 /** Places the first input window before the second input window in the window list. */
1269 private void addWindowToListBefore(WindowState first, WindowState second) {
1270 int i = mWindows.indexOf(second);
1271 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1272 "Adding window " + this + " at " + i + " of " + mWindows.size()
1273 + " (before " + second + ")");
1274 if (i < 0) {
1275 Slog.w(TAG_WM, "addWindowToListBefore: Unable to find " + second + " in " + mWindows);
1276 i = 0;
1277 }
1278 mWindows.add(i, first);
1279 mService.mWindowsChanged = true;
1280 }
1281
1282 /**
1283 * This method finds out the index of a window that has the same app token as win. used for z
1284 * ordering the windows in mWindows
1285 */
1286 private int findIdxBasedOnAppTokens(WindowState win) {
1287 for(int j = mWindows.size() - 1; j >= 0; j--) {
1288 final WindowState wentry = mWindows.get(j);
1289 if(wentry.mAppToken == win.mAppToken) {
1290 return j;
1291 }
1292 }
1293 return -1;
1294 }
1295
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001296 private void dumpChildrenNames() {
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001297 StringBuilder output = new StringBuilder();
1298 dumpChildrenNames(output, " ");
1299 Slog.v(TAG_WM, output.toString());
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001300 }
1301
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001302 private void dumpWindows() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001303 Slog.v(TAG_WM, " Display #" + mDisplayId);
1304 final WindowList windows = getWindowList();
1305 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
1306 Slog.v(TAG_WM, " #" + winNdx + ": " + windows.get(winNdx));
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001307 }
1308 }
1309
Wale Ogunwale02319a62016-09-26 15:21:22 -07001310 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1311 if (mTokenMap.isEmpty()) {
1312 return;
1313 }
1314 pw.println(" Display #" + mDisplayId);
1315 final Iterator<WindowToken> it = mTokenMap.values().iterator();
1316 while (it.hasNext()) {
1317 final WindowToken token = it.next();
1318 pw.print(" ");
1319 pw.print(token);
1320 if (dumpAll) {
1321 pw.println(':');
1322 token.dump(pw, " ");
1323 } else {
1324 pw.println();
1325 }
1326 }
1327 }
1328
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001329 void enableSurfaceTrace(FileDescriptor fd) {
1330 for (int i = mWindows.size() - 1; i >= 0; i--) {
1331 final WindowState win = mWindows.get(i);
1332 win.mWinAnimator.enableSurfaceTrace(fd);
1333 }
1334 }
1335
1336 void disableSurfaceTrace() {
1337 for (int i = mWindows.size() - 1; i >= 0; i--) {
1338 final WindowState win = mWindows.get(i);
1339 win.mWinAnimator.disableSurfaceTrace();
1340 }
1341 }
1342
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001343 static final class GetWindowOnDisplaySearchResult {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001344 boolean reachedToken;
1345 WindowState foundWindow;
1346
1347 void reset() {
1348 reachedToken = false;
1349 foundWindow = null;
1350 }
1351 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001352
1353 static final class TaskForResizePointSearchResult {
1354 boolean searchDone;
1355 Task taskForResize;
1356
1357 void reset() {
1358 searchDone = false;
1359 taskForResize = null;
1360 }
1361 }
Robert Carr3b716242016-08-16 16:02:21 -07001362
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001363 /**
1364 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
1365 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
1366 * homogeneous children type which is currently required by sub-classes of
1367 * {@link WindowContainer} class.
1368 */
1369 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
1370
1371 int size() {
1372 return mChildren.size();
1373 }
1374
1375 E get(int index) {
1376 return mChildren.get(index);
1377 }
1378
1379 @Override
1380 boolean fillsParent() {
1381 return true;
1382 }
1383
1384 @Override
1385 boolean isVisible() {
1386 return true;
Robert Carr3b716242016-08-16 16:02:21 -07001387 }
1388 }
1389
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001390 /**
1391 * Window container class that contains all containers on this display relating to Apps.
1392 * I.e Activities.
1393 */
1394 private class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
1395
1396 void attachStack(TaskStack stack, boolean onTop) {
1397 if (stack.mStackId == HOME_STACK_ID) {
1398 if (mHomeStack != null) {
1399 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
1400 }
1401 mHomeStack = stack;
1402 }
1403 addChild(stack, onTop);
1404 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07001405 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001406
1407 void moveStack(TaskStack stack, boolean toTop) {
1408 if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) {
1409 // This stack is always-on-top silly...
1410 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom");
1411 return;
1412 }
1413
1414 if (!mChildren.contains(stack)) {
1415 Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable());
1416 }
1417 removeChild(stack);
1418 addChild(stack, toTop);
1419 }
1420
1421 private void addChild(TaskStack stack, boolean toTop) {
1422 int addIndex = toTop ? mChildren.size() : 0;
1423
1424 if (toTop
1425 && mService.isStackVisibleLocked(PINNED_STACK_ID)
1426 && stack.mStackId != PINNED_STACK_ID) {
1427 // The pinned stack is always the top most stack (always-on-top) when it is visible.
1428 // So, stack is moved just below the pinned stack.
1429 addIndex--;
1430 TaskStack topStack = mChildren.get(addIndex);
1431 if (topStack.mStackId != PINNED_STACK_ID) {
1432 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
1433 }
1434 }
1435 addChild(stack, addIndex);
1436 setLayoutNeeded();
1437 }
1438
1439 }
1440
1441 /**
1442 * Window container class that contains all containers on this display that are not related to
1443 * Apps. E.g. status bar.
1444 */
1445 private static class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
1446
Robert Carr3b716242016-08-16 16:02:21 -07001447 }
Craig Mautner59c00972012-07-30 12:10:24 -07001448}