blob: e1aa98d4b79ee332451988d06c959574c1b257bf [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 Ogunwale51362492016-09-08 17:49:17 -070023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
24import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
25import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080026import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
27import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
28import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070029import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwaleec731152016-09-08 20:18:57 -070030import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
31import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070032import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070033import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
34import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070035import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
36import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleec731152016-09-08 20:18:57 -070037import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080038import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
Wale Ogunwale51362492016-09-08 17:49:17 -070039import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080040import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070041import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -070042
Wale Ogunwale3797c222015-10-27 14:21:58 -070043import android.app.ActivityManager.StackId;
Craig Mautnerc00204b2013-03-05 15:02:14 -080044import android.graphics.Rect;
Craig Mautner6601b7b2013-04-29 10:29:11 -070045import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +010046import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070047import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070048import android.os.Debug;
Chong Zhang8e89b312015-09-09 15:09:30 -070049import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -070050import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -070051import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -070052import android.view.DisplayInfo;
Wale Ogunwaleec731152016-09-08 20:18:57 -070053import android.view.IWindow;
Craig Mautner4a1cb222013-12-04 16:14:06 -080054import android.view.Surface;
Jorim Jaggi6626f542016-08-22 13:08:44 -070055import android.view.animation.Animation;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070056import com.android.internal.util.FastPrintWriter;
Craig Mautner59c00972012-07-30 12:10:24 -070057
Robert Carr3b716242016-08-16 16:02:21 -070058import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -070059import java.io.PrintWriter;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070060import java.io.StringWriter;
Craig Mautner59c00972012-07-30 12:10:24 -070061import java.util.ArrayList;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070062import java.util.Arrays;
Craig Mautner59c00972012-07-30 12:10:24 -070063
64class DisplayContentList extends ArrayList<DisplayContent> {
65}
66
67/**
68 * Utility class for keeping track of the WindowStates and other pertinent contents of a
69 * particular Display.
70 *
71 * IMPORTANT: No method from this class should ever be used without holding
72 * WindowManagerService.mWindowMap.
73 */
74class DisplayContent {
75
76 /** Unique identifier of this stack. */
77 private final int mDisplayId;
78
79 /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element
80 * from mDisplayWindows; */
Craig Mautnerdc548482014-02-05 13:35:24 -080081 private final WindowList mWindows = new WindowList();
Craig Mautner59c00972012-07-30 12:10:24 -070082
Craig Mautner59c00972012-07-30 12:10:24 -070083 int mInitialDisplayWidth = 0;
84 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -070085 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -070086 int mBaseDisplayWidth = 0;
87 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -070088 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -070089 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -070090 private final DisplayInfo mDisplayInfo = new DisplayInfo();
91 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070092 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Craig Mautner59c00972012-07-30 12:10:24 -070093
Craig Mautner6601b7b2013-04-29 10:29:11 -070094 Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -070095 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -070096
Craig Mautner39834192012-09-02 07:47:24 -070097 // Accessed directly by all users.
98 boolean layoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -070099 int pendingLayoutChanges;
Craig Mautner69b08182012-09-05 13:07:13 -0700100 final boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700101
Craig Mautnerdc548482014-02-05 13:35:24 -0800102 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700103 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800104
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800105 /** Array containing all TaskStacks on this display. Array
Craig Mautnercf910b02013-04-23 11:23:27 -0700106 * is stored in display order with the current bottom stack at 0. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700107 private final ArrayList<TaskStack> mStacks = new ArrayList<>();
Craig Mautnerc00204b2013-03-05 15:02:14 -0800108
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800109 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
110 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700111 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700112
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700113 /** Detect user tapping outside of current focused task bounds .*/
114 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700115
Craig Mautner6601b7b2013-04-29 10:29:11 -0700116 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700117 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700118
Craig Mautner6601b7b2013-04-29 10:29:11 -0700119 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800120 private final Rect mTmpRect = new Rect();
121 private final Rect mTmpRect2 = new Rect();
122 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700123
Craig Mautner9d808b12013-08-06 18:00:25 -0700124 final WindowManagerService mService;
125
Craig Mautner95da1082014-02-24 17:54:35 -0800126 /** Remove this display when animation on it has completed. */
127 boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800128
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700129 final DockedStackDividerController mDividerControllerLocked;
130
Chong Zhang112eb8c2015-11-02 11:17:00 -0800131 final DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700132
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800133 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
134
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700135 /** Used when rebuilding window list to keep track of windows that have been removed. */
136 private WindowState[] mRebuildTmp = new WindowState[20];
137
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700138 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
139 new TaskForResizePointSearchResult();
140 private final GetWindowOnDisplaySearchResult mTmpGetWindowOnDisplaySearchResult =
141 new GetWindowOnDisplaySearchResult();
142
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800143 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700144 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800145 * @param service You know.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700146 */
Craig Mautner9d808b12013-08-06 18:00:25 -0700147 DisplayContent(Display display, WindowManagerService service) {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700148 mDisplay = display;
149 mDisplayId = display.getDisplayId();
150 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700151 display.getMetrics(mDisplayMetrics);
Craig Mautner69b08182012-09-05 13:07:13 -0700152 isDefaultDisplay = mDisplayId == Display.DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700153 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700154 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800155 mDividerControllerLocked = new DockedStackDividerController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800156 mDimLayerController = new DimLayerController(this);
Craig Mautner59c00972012-07-30 12:10:24 -0700157 }
158
159 int getDisplayId() {
160 return mDisplayId;
161 }
162
163 WindowList getWindowList() {
164 return mWindows;
165 }
166
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700167 Display getDisplay() {
168 return mDisplay;
169 }
170
Craig Mautner59c00972012-07-30 12:10:24 -0700171 DisplayInfo getDisplayInfo() {
172 return mDisplayInfo;
173 }
174
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700175 DisplayMetrics getDisplayMetrics() {
176 return mDisplayMetrics;
177 }
178
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100179 DockedStackDividerController getDockedDividerController() {
180 return mDividerControllerLocked;
181 }
182
Jeff Browna506a6e2013-06-04 00:02:38 -0700183 /**
184 * Returns true if the specified UID has access to this display.
185 */
186 public boolean hasAccess(int uid) {
187 return mDisplay.hasAccess(uid);
188 }
189
keunyounga446bf02013-06-21 19:07:57 -0700190 public boolean isPrivate() {
191 return (mDisplay.getFlags() & Display.FLAG_PRIVATE) != 0;
192 }
193
Craig Mautnerdc548482014-02-05 13:35:24 -0800194 ArrayList<TaskStack> getStacks() {
195 return mStacks;
196 }
197
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700198 TaskStack getHomeStack() {
Craig Mautner333c2ec2014-10-02 12:24:02 -0700199 if (mHomeStack == null && mDisplayId == Display.DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800200 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800201 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700202 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700203 }
204
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700205 TaskStack getStackById(int stackId) {
206 for (int i = mStacks.size() - 1; i >= 0; --i) {
207 final TaskStack stack = mStacks.get(i);
208 if (stack.mStackId == stackId) {
209 return stack;
210 }
211 }
212 return null;
213 }
214
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700215 void checkAppWindowsReadyToShow() {
216 for (int i = mStacks.size() - 1; i >= 0; --i) {
217 final TaskStack stack = mStacks.get(i);
218 stack.checkAppWindowsReadyToShow(mDisplayId);
219 }
220 }
221
222 void updateAllDrawn() {
223 for (int i = mStacks.size() - 1; i >= 0; --i) {
224 final TaskStack stack = mStacks.get(i);
225 stack.updateAllDrawn(mDisplayId);
226 }
227 }
228
229 void stepAppWindowsAnimation(long currentTime) {
230 for (int i = mStacks.size() - 1; i >= 0; --i) {
231 final TaskStack stack = mStacks.get(i);
232 stack.stepAppWindowsAnimation(currentTime, mDisplayId);
233 }
234 }
235
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700236 void onAppTransitionDone() {
237 for (int i = mStacks.size() - 1; i >= 0; --i) {
238 final TaskStack stack = mStacks.get(i);
239 stack.onAppTransitionDone();
240 }
241
242 rebuildAppWindowList();
243 }
244
Wale Ogunwale51362492016-09-08 17:49:17 -0700245 int getOrientation() {
246 // TODO: Most of the logic here can be removed once this class is converted to use
247 // WindowContainer which has an abstract implementation of getOrientation that
248 // should cover this.
249 if (mService.isStackVisibleLocked(DOCKED_STACK_ID)
250 || mService.isStackVisibleLocked(FREEFORM_WORKSPACE_STACK_ID)) {
251 // Apps and their containers are not allowed to specify an orientation while the docked
252 // or freeform stack is visible...except for the home stack/task if the docked stack is
253 // minimized and it actually set something.
Andrii Kulian8072d112016-09-16 11:11:01 -0700254 if (mHomeStack != null && mHomeStack.isVisible()
255 && mDividerControllerLocked.isMinimizedDock()) {
Wale Ogunwale51362492016-09-08 17:49:17 -0700256 final int orientation = mHomeStack.getOrientation();
257 if (orientation != SCREEN_ORIENTATION_UNSET) {
258 return orientation;
259 }
260 }
261 return SCREEN_ORIENTATION_UNSPECIFIED;
262 }
263
264 for (int i = mStacks.size() - 1; i >= 0; --i) {
265 final TaskStack stack = mStacks.get(i);
266 if (!stack.isVisible()) {
267 continue;
268 }
269
270 final int orientation = stack.getOrientation();
271
272 if (orientation == SCREEN_ORIENTATION_BEHIND) {
273 continue;
274 }
275
276 if (orientation != SCREEN_ORIENTATION_UNSET) {
277 if (stack.fillsParent() || orientation != SCREEN_ORIENTATION_UNSPECIFIED) {
278 return orientation;
279 }
280 }
281 }
282
283 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
284 "No app is requesting an orientation, return " + mService.mLastOrientation);
285 // The next app has not been requested to be visible, so we keep the current orientation
286 // to prevent freezing/unfreezing the display too early.
287 return mService.mLastOrientation;
288 }
289
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700290 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700291 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700292 mDisplay.getMetrics(mDisplayMetrics);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800293 for (int i = mStacks.size() - 1; i >= 0; --i) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700294 mStacks.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800295 }
Craig Mautner722285e2012-09-07 13:55:58 -0700296 }
297
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700298 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700299 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
300 if (displayManagerInternal != null) {
301 // Bootstrap the default logical display from the display manager.
302 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
303 if (newDisplayInfo != null) {
304 mDisplayInfo.copyFrom(newDisplayInfo);
305 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700306 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700307
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800308 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
309 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
310 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
311 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700312 }
313
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700314 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700315 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800316 final int orientation = mDisplayInfo.rotation;
317 boolean rotated = (orientation == Surface.ROTATION_90
318 || orientation == Surface.ROTATION_270);
319 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
320 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700321 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700323 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800324 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700325 out.set(left, top, left + width, top + height);
326 }
327
Chong Zhangf66db432016-01-13 10:39:51 -0800328 void getContentRect(Rect out) {
329 out.set(mContentRect);
330 }
331
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700332 /** Refer to {@link WindowManagerService#attachStack(int, int, boolean)} */
333 void attachStack(TaskStack stack, boolean onTop) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800334 if (stack.mStackId == HOME_STACK_ID) {
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800335 if (mHomeStack != null) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800336 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700337 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800338 mHomeStack = stack;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800339 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700340 if (onTop) {
341 mStacks.add(stack);
342 } else {
343 mStacks.add(0, stack);
344 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800345 layoutNeeded = true;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800346 }
347
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800348 void moveStack(TaskStack stack, boolean toTop) {
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800349 if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) {
350 // This stack is always-on-top silly...
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800351 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom");
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700352 return;
353 }
354
Filip Gruszczynski26ed2652015-08-10 11:02:53 -0700355 if (!mStacks.remove(stack)) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800356 Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable());
Filip Gruszczynski26ed2652015-08-10 11:02:53 -0700357 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700358
359 int addIndex = toTop ? mStacks.size() : 0;
360
361 if (toTop
362 && mService.isStackVisibleLocked(PINNED_STACK_ID)
363 && stack.mStackId != PINNED_STACK_ID) {
364 // The pinned stack is always the top most stack (always-on-top) when it is visible.
365 // So, stack is moved just below the pinned stack.
366 addIndex--;
367 TaskStack topStack = mStacks.get(addIndex);
368 if (topStack.mStackId != PINNED_STACK_ID) {
369 throw new IllegalStateException("Pinned stack isn't top stack??? " + mStacks);
370 }
371 }
372 mStacks.add(addIndex, stack);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800373 }
374
Wale Ogunwalef6192862016-09-10 13:42:30 -0700375 // TODO: Don't forget to switch to WC.removeChild
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700376 void detachChild(TaskStack stack) {
377 detachStack(stack);
378 if (stack.detachFromDisplay()) {
379 mService.mWindowPlacerLocked.requestTraversal();
380 }
381 if (stack.mStackId == DOCKED_STACK_ID) {
382 mService.getDefaultDisplayContentLocked().mDividerControllerLocked
383 .notifyDockedStackExistsChanged(false);
384 }
385 }
386
387 // TODO: See about removing this by untangling the use case in WMS.attachStack()
Craig Mautnerdf88d732014-01-27 09:21:32 -0800388 void detachStack(TaskStack stack) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800389 mDimLayerController.removeDimLayerUser(stack);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800390 mStacks.remove(stack);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800391 }
392
393 /**
394 * Propagate the new bounds to all child stacks.
395 * @param contentRect The bounds to apply at the top level.
396 */
397 void resize(Rect contentRect) {
398 mContentRect.set(contentRect);
399 }
400
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700401 int taskIdFromPoint(int x, int y) {
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800402 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700403 final TaskStack stack = mStacks.get(stackNdx);
404 final int taskId = stack.taskIdFromPoint(x, y);
405 if (taskId != -1) {
406 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -0700407 }
408 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800409 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -0700410 }
411
Chong Zhang8e89b312015-09-09 15:09:30 -0700412 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -0800413 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -0700414 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -0700415 */
Wale Ogunwale15ead902016-09-02 14:30:11 -0700416 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700417 final int delta = mService.dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700418 mTmpTaskForResizePointSearchResult.reset();
Chong Zhang8e89b312015-09-09 15:09:30 -0700419 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
420 TaskStack stack = mStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -0700421 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700422 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700423 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700424
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700425 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
426 if (mTmpTaskForResizePointSearchResult.searchDone) {
427 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -0700428 }
429 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700430 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700431 }
432
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700433 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -0700434 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700435 final int delta = mService.dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700436 mTmpRect2.setEmpty();
Chong Zhangd8ceb852015-11-11 14:53:41 -0800437 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700438 final TaskStack stack = mStacks.get(stackNdx);
439 stack.setTouchExcludeRegion(
440 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -0700441 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800442 // If we removed the focused task above, add it back and only leave its
443 // outside touch area in the exclusion. TapDectector is not interested in
444 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700445 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800446 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
447 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800448 final WindowState inputMethod = mService.mInputMethodWindow;
449 if (inputMethod != null && inputMethod.isVisibleLw()) {
450 // If the input method is visible and the user is typing, we don't want these touch
451 // events to be intercepted and used to change focus. This would likely cause a
452 // disappearance of the input method.
453 inputMethod.getTouchableRegion(mTmpRegion);
454 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
455 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800456 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
457 WindowState win = mTapExcludedWindows.get(i);
458 win.getTouchableRegion(mTmpRegion);
459 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
460 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100461 if (getDockedStackVisibleForUserLocked() != null) {
462 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -0700463 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100464 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
465 }
Craig Mautner1bef3892015-02-17 15:09:47 -0800466 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700467 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -0800468 }
Craig Mautner6601b7b2013-04-29 10:29:11 -0700469 }
470
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800471 void switchUserStacks() {
Craig Mautner858d8a62013-04-23 17:08:34 -0700472 final WindowList windows = getWindowList();
473 for (int i = 0; i < windows.size(); i++) {
474 final WindowState win = windows.get(i);
475 if (win.isHiddenFromUserLocked()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800476 if (DEBUG_VISIBILITY) Slog.w(TAG_WM, "user changing, hiding " + win
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800477 + ", attrs=" + win.mAttrs.type + ", belonging to " + win.mOwnerUid);
Craig Mautner858d8a62013-04-23 17:08:34 -0700478 win.hideLw(false);
479 }
480 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700481
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800482 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800483 mStacks.get(stackNdx).switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -0700484 }
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700485
486 rebuildAppWindowList();
Craig Mautner858d8a62013-04-23 17:08:34 -0700487 }
488
Craig Mautner05d29032013-05-03 13:40:13 -0700489 void resetAnimationBackgroundAnimator() {
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800490 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
491 mStacks.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -0700492 }
493 }
494
495 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800496 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -0700497 }
498
499 void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800500 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700501 }
502
503 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800504 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700505 }
506
507 void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800508 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -0700509 }
510
Craig Mautner2eb15342013-08-07 13:13:35 -0700511 void close() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800512 mDimLayerController.close();
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800513 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
514 mStacks.get(stackNdx).close();
Craig Mautner2eb15342013-08-07 13:13:35 -0700515 }
516 }
517
Craig Mautner95da1082014-02-24 17:54:35 -0800518 boolean isAnimating() {
519 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
520 final TaskStack stack = mStacks.get(stackNdx);
521 if (stack.isAnimating()) {
522 return true;
523 }
524 }
525 return false;
526 }
527
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700528 /** Returns true if a removal action is still being deferred. */
529 boolean checkCompleteDeferredRemoval() {
530 boolean stillDeferringRemoval = false;
Craig Mautner95da1082014-02-24 17:54:35 -0800531 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
532 final TaskStack stack = mStacks.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700533 stillDeferringRemoval |= stack.checkCompleteDeferredRemoval();
Craig Mautner95da1082014-02-24 17:54:35 -0800534 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700535 if (!stillDeferringRemoval && mDeferredRemoval) {
Craig Mautner95da1082014-02-24 17:54:35 -0800536 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700537 return false;
Craig Mautner95da1082014-02-24 17:54:35 -0800538 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700539 return true;
Craig Mautner95da1082014-02-24 17:54:35 -0800540 }
541
Wale Ogunwale94744212015-09-21 19:01:47 -0700542 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
543 final int rotationDelta = DisplayContent.deltaRotation(oldRotation, newRotation);
544 getLogicalDisplayRect(mTmpRect);
545 switch (rotationDelta) {
546 case Surface.ROTATION_0:
547 mTmpRect2.set(bounds);
548 break;
549 case Surface.ROTATION_90:
550 mTmpRect2.top = mTmpRect.bottom - bounds.right;
551 mTmpRect2.left = bounds.top;
552 mTmpRect2.right = mTmpRect2.left + bounds.height();
553 mTmpRect2.bottom = mTmpRect2.top + bounds.width();
554 break;
555 case Surface.ROTATION_180:
556 mTmpRect2.top = mTmpRect.bottom - bounds.bottom;
557 mTmpRect2.left = mTmpRect.right - bounds.right;
558 mTmpRect2.right = mTmpRect2.left + bounds.width();
559 mTmpRect2.bottom = mTmpRect2.top + bounds.height();
560 break;
561 case Surface.ROTATION_270:
562 mTmpRect2.top = bounds.left;
563 mTmpRect2.left = mTmpRect.right - bounds.bottom;
564 mTmpRect2.right = mTmpRect2.left + bounds.height();
565 mTmpRect2.bottom = mTmpRect2.top + bounds.width();
566 break;
567 }
568 bounds.set(mTmpRect2);
569 }
570
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800571 static int deltaRotation(int oldRotation, int newRotation) {
572 int delta = newRotation - oldRotation;
573 if (delta < 0) delta += 4;
574 return delta;
575 }
576
Craig Mautnera91f9e22012-09-14 16:22:08 -0700577 public void dump(String prefix, PrintWriter pw) {
578 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
579 final String subPrefix = " " + prefix;
580 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
581 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
582 pw.print("dpi");
583 if (mInitialDisplayWidth != mBaseDisplayWidth
584 || mInitialDisplayHeight != mBaseDisplayHeight
585 || mInitialDisplayDensity != mBaseDisplayDensity) {
586 pw.print(" base=");
587 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
588 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
589 }
Jeff Brownd46747a2015-04-15 19:02:36 -0700590 if (mDisplayScalingDisabled) {
591 pw.println(" noscale");
592 }
Craig Mautnera91f9e22012-09-14 16:22:08 -0700593 pw.print(" cur=");
594 pw.print(mDisplayInfo.logicalWidth);
595 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
596 pw.print(" app=");
597 pw.print(mDisplayInfo.appWidth);
598 pw.print("x"); pw.print(mDisplayInfo.appHeight);
599 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
600 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
601 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
602 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Craig Mautner95da1082014-02-24 17:54:35 -0800603 pw.print(subPrefix); pw.print("deferred="); pw.print(mDeferredRemoval);
604 pw.print(" layoutNeeded="); pw.println(layoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800605
Craig Mautnerdc548482014-02-05 13:35:24 -0800606 pw.println();
Craig Mautnere8b85fd2014-10-22 09:23:25 -0700607 pw.println(" Application tokens in top down Z order:");
Craig Mautnere8b85fd2014-10-22 09:23:25 -0700608 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnerbcb6eb92015-01-13 13:09:22 -0800609 final TaskStack stack = mStacks.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800610 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700611 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800612
Craig Mautnerdc548482014-02-05 13:35:24 -0800613 pw.println();
614 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700615 pw.println();
616 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800617 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700618 WindowToken token = mExitingTokens.get(i);
619 pw.print(" Exiting #"); pw.print(i);
620 pw.print(' '); pw.print(token);
621 pw.println(':');
622 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800623 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700624 }
Craig Mautner59c00972012-07-30 12:10:24 -0700625 pw.println();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800626 mDimLayerController.dump(prefix + " ", pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -0700627 pw.println();
628 mDividerControllerLocked.dump(prefix + " ", pw);
Craig Mautner59c00972012-07-30 12:10:24 -0700629 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800630
631 @Override
632 public String toString() {
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700633 return getName() + " stacks=" + mStacks;
634 }
635
636 String getName() {
637 return "Display " + mDisplayId + " info=" + mDisplayInfo;
Craig Mautnere0a38842013-12-16 16:14:02 -0800638 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700639
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800640 /**
641 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
642 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700643 TaskStack getDockedStackLocked() {
Wale Ogunwalee45899a2015-10-01 11:30:34 -0700644 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700645 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700646 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -0800647
648 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800649 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
650 * visible, as long as it's not hidden because the current user doesn't have any tasks there.
651 */
652 TaskStack getDockedStackVisibleForUserLocked() {
653 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700654 return (stack != null && stack.isVisible(true /* ignoreKeyguard */)) ? stack : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800655 }
656
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700657 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -0800658 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
659 WindowState touchedWin = null;
660 final int x = (int) xf;
661 final int y = (int) yf;
662
663 for (int i = mWindows.size() - 1; i >= 0; i--) {
664 WindowState window = mWindows.get(i);
665 final int flags = window.mAttrs.flags;
666 if (!window.isVisibleLw()) {
667 continue;
668 }
669 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
670 continue;
671 }
672
673 window.getVisibleBounds(mTmpRect);
674 if (!mTmpRect.contains(x, y)) {
675 continue;
676 }
677
678 window.getTouchableRegion(mTmpRegion);
679
680 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
681 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
682 touchedWin = window;
683 break;
684 }
685 }
686
687 return touchedWin;
688 }
Jorim Jaggi6626f542016-08-22 13:08:44 -0700689
690 /**
691 * See {@link WindowManagerService#overridePlayingAppAnimationsLw}.
692 */
693 void overridePlayingAppAnimationsLw(Animation a) {
694 for (int i = mStacks.size() - 1; i >= 0; i--) {
695 mStacks.get(i).overridePlayingAppAnimations(a);
696 }
697 }
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700698
699 boolean canAddToastWindowForUid(int uid) {
700 // We allow one toast window per UID being shown at a time.
701 WindowList windows = getWindowList();
702 final int windowCount = windows.size();
703 for (int i = 0; i < windowCount; i++) {
704 WindowState window = windows.get(i);
705 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == uid
706 && !window.mPermanentlyHidden && !window.mAnimatingExit) {
707 return false;
708 }
709 }
710 return true;
711 }
712
713 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus,
714 WindowState newFocus) {
715 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
716 return;
717 }
718 final int lostFocusUid = oldFocus.mOwnerUid;
719 WindowList windows = getWindowList();
720 final int windowCount = windows.size();
721 for (int i = 0; i < windowCount; i++) {
722 WindowState window = windows.get(i);
723 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == lostFocusUid) {
724 if (!mService.mH.hasMessages(WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window)) {
725 mService.mH.sendMessageDelayed(
726 mService.mH.obtainMessage(
727 WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window),
728 window.mAttrs.hideTimeoutMilliseconds);
729 }
730 }
731 }
732 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700733
734 WindowState findFocusedWindow() {
735 final AppWindowToken focusedApp = mService.mFocusedApp;
736
737 for (int i = mWindows.size() - 1; i >= 0; i--) {
738 final WindowState win = mWindows.get(i);
739
740 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + i + " = " + win
741 + ", flags=" + win.mAttrs.flags + ", canReceive=" + win.canReceiveKeys());
742
743 if (!win.canReceiveKeys()) {
744 continue;
745 }
746
747 final AppWindowToken wtoken = win.mAppToken;
748
749 // If this window's application has been removed, just skip it.
750 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
751 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
752 + (wtoken.removed ? "removed" : "sendingToBottom"));
753 continue;
754 }
755
756 if (focusedApp == null) {
757 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
758 + " using new focus @ " + i + " = " + win);
759 return win;
760 }
761
762 if (!focusedApp.windowsAreFocusable()) {
763 // Current focused app windows aren't focusable...
764 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
765 + " focusable using new focus @ " + i + " = " + win);
766 return win;
767 }
768
769 // Descend through all of the app tokens and find the first that either matches
770 // win.mAppToken (return win) or mFocusedApp (return null).
771 if (wtoken != null && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
772 final TaskStack focusedAppStack = focusedApp.mTask.mStack;
773 final TaskStack appStack = wtoken.mTask.mStack;
774
775 // TODO: Use WindowContainer.compareTo() once everything is using WindowContainer
Wale Ogunwale14a3fb92016-09-11 15:19:05 -0700776 if ((focusedAppStack == appStack && focusedApp.compareTo(wtoken) > 0)
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700777 || mStacks.indexOf(focusedAppStack) > mStacks.indexOf(appStack)) {
778 // App stack below focused app stack. No focus for you!!!
779 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
780 "findFocusedWindow: Reached focused app=" + focusedApp);
781 return null;
782 }
783 }
784
785 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ "
786 + i + " = " + win);
787 return win;
788 }
789
790 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
791 return null;
792 }
Wale Ogunwaleec731152016-09-08 20:18:57 -0700793
794 int addAppWindowToWindowList(final WindowState win) {
795 final IWindow client = win.mClient;
796
797 WindowList tokenWindowList = getTokenWindowsOnDisplay(win.mToken);
798 if (!tokenWindowList.isEmpty()) {
799 return addAppWindowExisting(win, tokenWindowList);
800 }
801
802 // No windows from this token on this display
803 if (mService.localLOGV) Slog.v(TAG_WM, "Figuring out where to add app window "
804 + client.asBinder() + " (token=" + this + ")");
805
806 final WindowToken wToken = win.mToken;
807
808 // Figure out where the window should go, based on the order of applications.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700809 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwaleec731152016-09-08 20:18:57 -0700810 for (int i = mStacks.size() - 1; i >= 0; --i) {
811 final TaskStack stack = mStacks.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700812 stack.getWindowOnDisplayBeforeToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
813 if (mTmpGetWindowOnDisplaySearchResult.reachedToken) {
Wale Ogunwaleec731152016-09-08 20:18:57 -0700814 // We have reach the token we are interested in. End search.
815 break;
816 }
817 }
818
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700819 WindowState pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700820
821 // We now know the index into the apps. If we found an app window above, that gives us the
822 // position; else we need to look some more.
823 if (pos != null) {
824 // Move behind any windows attached to this one.
825 final WindowToken atoken = mService.mTokenMap.get(pos.mClient.asBinder());
826 if (atoken != null) {
827 tokenWindowList = getTokenWindowsOnDisplay(atoken);
828 final int NC = tokenWindowList.size();
829 if (NC > 0) {
830 WindowState bottom = tokenWindowList.get(0);
831 if (bottom.mSubLayer < 0) {
832 pos = bottom;
833 }
834 }
835 }
836 addWindowToListBefore(win, pos);
837 return 0;
838 }
839
840 // Continue looking down until we find the first token that has windows on this display.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700841 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwaleec731152016-09-08 20:18:57 -0700842 for (int i = mStacks.size() - 1; i >= 0; --i) {
843 final TaskStack stack = mStacks.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700844 stack.getWindowOnDisplayAfterToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
845 if (mTmpGetWindowOnDisplaySearchResult.foundWindow != null) {
Wale Ogunwaleec731152016-09-08 20:18:57 -0700846 // We have found a window after the token. End search.
847 break;
848 }
849 }
850
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700851 pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700852
853 if (pos != null) {
854 // Move in front of any windows attached to this one.
855 final WindowToken atoken = mService.mTokenMap.get(pos.mClient.asBinder());
856 if (atoken != null) {
857 final WindowState top = atoken.getTopWindow();
858 if (top != null && top.mSubLayer >= 0) {
859 pos = top;
860 }
861 }
862 addWindowToListAfter(win, pos);
863 return 0;
864 }
865
866 // Just search for the start of this layer.
867 final int myLayer = win.mBaseLayer;
868 int i;
869 for (i = mWindows.size() - 1; i >= 0; --i) {
870 final WindowState w = mWindows.get(i);
871 // Dock divider shares the base layer with application windows, but we want to always
872 // keep it above the application windows. The sharing of the base layer is intended
873 // for window animations, which need to be above the dock divider for the duration
874 // of the animation.
875 if (w.mBaseLayer <= myLayer && w.mAttrs.type != TYPE_DOCK_DIVIDER) {
876 break;
877 }
878 }
879 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
880 "Based on layer: Adding window " + win + " at " + (i + 1) + " of "
881 + mWindows.size());
882 mWindows.add(i + 1, win);
883 mService.mWindowsChanged = true;
884 return 0;
885 }
886
887 /** Adds this non-app window to the window list. */
888 void addNonAppWindowToWindowList(WindowState win) {
889 // Figure out where window should go, based on layer.
890 int i;
891 for (i = mWindows.size() - 1; i >= 0; i--) {
892 final WindowState otherWin = mWindows.get(i);
893 if (otherWin.getBaseType() != TYPE_WALLPAPER && otherWin.mBaseLayer <= win.mBaseLayer) {
894 // Wallpaper wanders through the window list, for example to position itself
895 // directly behind keyguard. Because of this it will break the ordering based on
896 // WindowState.mBaseLayer. There might windows with higher mBaseLayer behind it and
897 // we don't want the new window to appear above them. An example of this is adding
898 // of the docked stack divider. Consider a scenario with the following ordering (top
899 // to bottom): keyguard, wallpaper, assist preview, apps. We want the dock divider
900 // to land below the assist preview, so the dock divider must ignore the wallpaper,
901 // with which it shares the base layer.
902 break;
903 }
904 }
905
906 i++;
907 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
908 "Free window: Adding window " + this + " at " + i + " of " + mWindows.size());
909 mWindows.add(i, win);
910 mService.mWindowsChanged = true;
911 }
912
913 void addChildWindowToWindowList(WindowState win) {
914 final WindowState parentWindow = win.getParentWindow();
915
916 WindowList windowsOnSameDisplay = getTokenWindowsOnDisplay(win.mToken);
917
918 // Figure out this window's ordering relative to the parent window.
919 final int wCount = windowsOnSameDisplay.size();
920 final int sublayer = win.mSubLayer;
921 int largestSublayer = Integer.MIN_VALUE;
922 WindowState windowWithLargestSublayer = null;
923 int i;
924 for (i = 0; i < wCount; i++) {
925 WindowState w = windowsOnSameDisplay.get(i);
926 final int wSublayer = w.mSubLayer;
927 if (wSublayer >= largestSublayer) {
928 largestSublayer = wSublayer;
929 windowWithLargestSublayer = w;
930 }
931 if (sublayer < 0) {
932 // For negative sublayers, we go below all windows in the same sublayer.
933 if (wSublayer >= sublayer) {
934 addWindowToListBefore(win, wSublayer >= 0 ? parentWindow : w);
935 break;
936 }
937 } else {
938 // For positive sublayers, we go above all windows in the same sublayer.
939 if (wSublayer > sublayer) {
940 addWindowToListBefore(win, w);
941 break;
942 }
943 }
944 }
945 if (i >= wCount) {
946 if (sublayer < 0) {
947 addWindowToListBefore(win, parentWindow);
948 } else {
949 addWindowToListAfter(win,
950 largestSublayer >= 0 ? windowWithLargestSublayer : parentWindow);
951 }
952 }
953 }
954
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700955 /**
956 * Z-orders the display window list so that:
957 * <ul>
958 * <li>Any windows that are currently below the wallpaper window stay below the wallpaper
959 * window.
960 * <li>Exiting application windows are at the bottom, but above the wallpaper window.
961 * <li>All other application windows are above the exiting application windows and ordered based
962 * on the ordering of their stacks and tasks on the display.
963 * <li>Non-application windows are at the very top.
964 * </ul>
965 * <p>
966 * NOTE: This isn't a complete picture of what the user see. Further manipulation of the window
967 * surface layering is done in {@link WindowLayersController}.
968 */
969 void rebuildAppWindowList() {
970 int count = mWindows.size();
971 int i;
972 int lastBelow = -1;
973 int numRemoved = 0;
974
975 if (mRebuildTmp.length < count) {
976 mRebuildTmp = new WindowState[count + 10];
977 }
978
979 // First remove all existing app windows.
980 i = 0;
981 while (i < count) {
982 final WindowState w = mWindows.get(i);
983 if (w.mAppToken != null) {
984 final WindowState win = mWindows.remove(i);
985 win.mRebuilding = true;
986 mRebuildTmp[numRemoved] = win;
987 mService.mWindowsChanged = true;
988 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Rebuild removing window: " + win);
989 count--;
990 numRemoved++;
991 continue;
992 } else if (lastBelow == i-1) {
993 if (w.mAttrs.type == TYPE_WALLPAPER) {
994 lastBelow = i;
995 }
996 }
997 i++;
998 }
999
1000 // Keep whatever windows were below the app windows still below, by skipping them.
1001 lastBelow++;
1002 i = lastBelow;
1003
1004 // First add all of the exiting app tokens... these are no longer in the main app list,
1005 // but still have windows shown. We put them in the back because now that the animation is
1006 // over we no longer will care about them.
1007 final int numStacks = mStacks.size();
1008 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1009 AppTokenList exitingAppTokens = mStacks.get(stackNdx).mExitingAppTokens;
1010 int NT = exitingAppTokens.size();
1011 for (int j = 0; j < NT; j++) {
1012 i = exitingAppTokens.get(j).rebuildWindowList(this, i);
1013 }
1014 }
1015
1016 // And add in the still active app tokens in Z order.
1017 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1018 i = mStacks.get(stackNdx).rebuildWindowList(this, i);
1019 }
1020
1021 i -= lastBelow;
1022 if (i != numRemoved) {
1023 layoutNeeded = true;
1024 Slog.w(TAG_WM, "On display=" + mDisplayId + " Rebuild removed " + numRemoved
1025 + " windows but added " + i + " rebuildAppWindowListLocked() "
1026 + " callers=" + Debug.getCallers(10));
1027 for (i = 0; i < numRemoved; i++) {
1028 WindowState ws = mRebuildTmp[i];
1029 if (ws.mRebuilding) {
1030 StringWriter sw = new StringWriter();
1031 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1032 ws.dump(pw, "", true);
1033 pw.flush();
1034 Slog.w(TAG_WM, "This window was lost: " + ws);
1035 Slog.w(TAG_WM, sw.toString());
1036 ws.mWinAnimator.destroySurfaceLocked();
1037 }
1038 }
1039 Slog.w(TAG_WM, "Current app token list:");
1040 dumpChildrenNames();
1041 Slog.w(TAG_WM, "Final window list:");
1042 dumpWindows();
1043 }
1044 Arrays.fill(mRebuildTmp, null);
1045 }
1046
Wale Ogunwaleec731152016-09-08 20:18:57 -07001047 /** Return the list of Windows on this display associated with the input token. */
1048 WindowList getTokenWindowsOnDisplay(WindowToken token) {
1049 final WindowList windowList = new WindowList();
1050 final int count = mWindows.size();
1051 for (int i = 0; i < count; i++) {
1052 final WindowState win = mWindows.get(i);
1053 if (win.mToken == token) {
1054 windowList.add(win);
1055 }
1056 }
1057 return windowList;
1058 }
1059
1060 private int addAppWindowExisting(WindowState win, WindowList tokenWindowList) {
1061
1062 int tokenWindowsPos;
1063 // If this application has existing windows, we simply place the new window on top of
1064 // them... but keep the starting window on top.
1065 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
1066 // Base windows go behind everything else.
1067 final WindowState lowestWindow = tokenWindowList.get(0);
1068 addWindowToListBefore(win, lowestWindow);
1069 tokenWindowsPos = win.mToken.getWindowIndex(lowestWindow);
1070 } else {
1071 final AppWindowToken atoken = win.mAppToken;
1072 final int windowListPos = tokenWindowList.size();
1073 final WindowState lastWindow = tokenWindowList.get(windowListPos - 1);
1074 if (atoken != null && lastWindow == atoken.startingWindow) {
1075 addWindowToListBefore(win, lastWindow);
1076 tokenWindowsPos = win.mToken.getWindowIndex(lastWindow);
1077 } else {
1078 int newIdx = findIdxBasedOnAppTokens(win);
1079 // There is a window above this one associated with the same apptoken note that the
1080 // window could be a floating window that was created later or a window at the top
1081 // of the list of windows associated with this token.
1082 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1083 "not Base app: Adding window " + win + " at " + (newIdx + 1) + " of "
1084 + mWindows.size());
1085 mWindows.add(newIdx + 1, win);
1086 if (newIdx < 0) {
1087 // No window from token found on win's display.
1088 tokenWindowsPos = 0;
1089 } else {
1090 tokenWindowsPos = win.mToken.getWindowIndex(mWindows.get(newIdx)) + 1;
1091 }
1092 mService.mWindowsChanged = true;
1093 }
1094 }
1095 return tokenWindowsPos;
1096 }
1097
1098 /** Places the first input window after the second input window in the window list. */
1099 private void addWindowToListAfter(WindowState first, WindowState second) {
1100 final int i = mWindows.indexOf(second);
1101 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1102 "Adding window " + this + " at " + (i + 1) + " of " + mWindows.size()
1103 + " (after " + second + ")");
1104 mWindows.add(i + 1, first);
1105 mService.mWindowsChanged = true;
1106 }
1107
1108 /** Places the first input window before the second input window in the window list. */
1109 private void addWindowToListBefore(WindowState first, WindowState second) {
1110 int i = mWindows.indexOf(second);
1111 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1112 "Adding window " + this + " at " + i + " of " + mWindows.size()
1113 + " (before " + second + ")");
1114 if (i < 0) {
1115 Slog.w(TAG_WM, "addWindowToListBefore: Unable to find " + second + " in " + mWindows);
1116 i = 0;
1117 }
1118 mWindows.add(i, first);
1119 mService.mWindowsChanged = true;
1120 }
1121
1122 /**
1123 * This method finds out the index of a window that has the same app token as win. used for z
1124 * ordering the windows in mWindows
1125 */
1126 private int findIdxBasedOnAppTokens(WindowState win) {
1127 for(int j = mWindows.size() - 1; j >= 0; j--) {
1128 final WindowState wentry = mWindows.get(j);
1129 if(wentry.mAppToken == win.mAppToken) {
1130 return j;
1131 }
1132 }
1133 return -1;
1134 }
1135
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001136 private void dumpChildrenNames() {
1137 StringWriter sw = new StringWriter();
1138 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1139 dumpChildrenNames(pw, " ");
1140 }
1141
1142 private void dumpChildrenNames(PrintWriter pw, String prefix) {
1143 final String childPrefix = prefix + prefix;
1144 for (int j = mStacks.size() - 1; j >= 0; j--) {
1145 final TaskStack stack = mStacks.get(j);
1146 pw.println("#" + j + " " + getName());
1147 stack.dumpChildrenNames(pw, childPrefix);
1148 }
1149 }
1150
1151 private void dumpWindows() {
1152 final int numDisplays = mService.mDisplayContents.size();
1153 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
1154 final DisplayContent displayContent = mService.mDisplayContents.valueAt(displayNdx);
1155 Slog.v(TAG_WM, " Display #" + displayContent.getDisplayId());
1156 final WindowList windows = displayContent.getWindowList();
1157 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
1158 Slog.v(TAG_WM, " #" + winNdx + ": " + windows.get(winNdx));
1159 }
1160 }
1161 }
1162
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001163 static final class GetWindowOnDisplaySearchResult {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001164 boolean reachedToken;
1165 WindowState foundWindow;
1166
1167 void reset() {
1168 reachedToken = false;
1169 foundWindow = null;
1170 }
1171 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001172
1173 static final class TaskForResizePointSearchResult {
1174 boolean searchDone;
1175 Task taskForResize;
1176
1177 void reset() {
1178 searchDone = false;
1179 taskForResize = null;
1180 }
1181 }
Robert Carr3b716242016-08-16 16:02:21 -07001182
1183 void enableSurfaceTrace(FileDescriptor fd) {
1184 for (int i = mWindows.size() - 1; i >= 0; i--) {
1185 final WindowState win = mWindows.get(i);
1186 win.mWinAnimator.enableSurfaceTrace(fd);
1187 }
1188 }
1189
1190 void disableSurfaceTrace() {
1191 for (int i = mWindows.size() - 1; i >= 0; i--) {
1192 final WindowState win = mWindows.get(i);
1193 win.mWinAnimator.disableSurfaceTrace();
1194 }
1195 }
Craig Mautner59c00972012-07-30 12:10:24 -07001196}