blob: 90353876d735b481ddfc9d7294f12d43ca4370a9 [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
58import java.io.PrintWriter;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070059import java.io.StringWriter;
Craig Mautner59c00972012-07-30 12:10:24 -070060import java.util.ArrayList;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070061import java.util.Arrays;
Craig Mautner59c00972012-07-30 12:10:24 -070062
63class DisplayContentList extends ArrayList<DisplayContent> {
64}
65
66/**
67 * Utility class for keeping track of the WindowStates and other pertinent contents of a
68 * particular Display.
69 *
70 * IMPORTANT: No method from this class should ever be used without holding
71 * WindowManagerService.mWindowMap.
72 */
73class DisplayContent {
74
75 /** Unique identifier of this stack. */
76 private final int mDisplayId;
77
78 /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element
79 * from mDisplayWindows; */
Craig Mautnerdc548482014-02-05 13:35:24 -080080 private final WindowList mWindows = new WindowList();
Craig Mautner59c00972012-07-30 12:10:24 -070081
Craig Mautner59c00972012-07-30 12:10:24 -070082 int mInitialDisplayWidth = 0;
83 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -070084 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -070085 int mBaseDisplayWidth = 0;
86 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -070087 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -070088 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -070089 private final DisplayInfo mDisplayInfo = new DisplayInfo();
90 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070091 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Craig Mautner59c00972012-07-30 12:10:24 -070092
Craig Mautner6601b7b2013-04-29 10:29:11 -070093 Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -070094 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -070095
Craig Mautner39834192012-09-02 07:47:24 -070096 // Accessed directly by all users.
97 boolean layoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -070098 int pendingLayoutChanges;
Craig Mautner69b08182012-09-05 13:07:13 -070099 final boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700100
Craig Mautnerdc548482014-02-05 13:35:24 -0800101 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700102 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800103
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800104 /** Array containing all TaskStacks on this display. Array
Craig Mautnercf910b02013-04-23 11:23:27 -0700105 * is stored in display order with the current bottom stack at 0. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700106 private final ArrayList<TaskStack> mStacks = new ArrayList<>();
Craig Mautnerc00204b2013-03-05 15:02:14 -0800107
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800108 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
109 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700110 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700111
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700112 /** Detect user tapping outside of current focused task bounds .*/
113 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700114
Craig Mautner6601b7b2013-04-29 10:29:11 -0700115 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700116 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700117
Craig Mautner6601b7b2013-04-29 10:29:11 -0700118 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800119 private final Rect mTmpRect = new Rect();
120 private final Rect mTmpRect2 = new Rect();
121 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700122
Craig Mautner9d808b12013-08-06 18:00:25 -0700123 final WindowManagerService mService;
124
Craig Mautner95da1082014-02-24 17:54:35 -0800125 /** Remove this display when animation on it has completed. */
126 boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800127
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700128 final DockedStackDividerController mDividerControllerLocked;
129
Chong Zhang112eb8c2015-11-02 11:17:00 -0800130 final DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700131
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800132 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
133
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700134 /** Used when rebuilding window list to keep track of windows that have been removed. */
135 private WindowState[] mRebuildTmp = new WindowState[20];
136
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700137 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
138 new TaskForResizePointSearchResult();
139 private final GetWindowOnDisplaySearchResult mTmpGetWindowOnDisplaySearchResult =
140 new GetWindowOnDisplaySearchResult();
141
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800142 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700143 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800144 * @param service You know.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700145 */
Craig Mautner9d808b12013-08-06 18:00:25 -0700146 DisplayContent(Display display, WindowManagerService service) {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700147 mDisplay = display;
148 mDisplayId = display.getDisplayId();
149 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700150 display.getMetrics(mDisplayMetrics);
Craig Mautner69b08182012-09-05 13:07:13 -0700151 isDefaultDisplay = mDisplayId == Display.DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700152 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700153 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800154 mDividerControllerLocked = new DockedStackDividerController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800155 mDimLayerController = new DimLayerController(this);
Craig Mautner59c00972012-07-30 12:10:24 -0700156 }
157
158 int getDisplayId() {
159 return mDisplayId;
160 }
161
162 WindowList getWindowList() {
163 return mWindows;
164 }
165
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700166 Display getDisplay() {
167 return mDisplay;
168 }
169
Craig Mautner59c00972012-07-30 12:10:24 -0700170 DisplayInfo getDisplayInfo() {
171 return mDisplayInfo;
172 }
173
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700174 DisplayMetrics getDisplayMetrics() {
175 return mDisplayMetrics;
176 }
177
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100178 DockedStackDividerController getDockedDividerController() {
179 return mDividerControllerLocked;
180 }
181
Jeff Browna506a6e2013-06-04 00:02:38 -0700182 /**
183 * Returns true if the specified UID has access to this display.
184 */
185 public boolean hasAccess(int uid) {
186 return mDisplay.hasAccess(uid);
187 }
188
keunyounga446bf02013-06-21 19:07:57 -0700189 public boolean isPrivate() {
190 return (mDisplay.getFlags() & Display.FLAG_PRIVATE) != 0;
191 }
192
Craig Mautnerdc548482014-02-05 13:35:24 -0800193 ArrayList<TaskStack> getStacks() {
194 return mStacks;
195 }
196
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700197 TaskStack getHomeStack() {
Craig Mautner333c2ec2014-10-02 12:24:02 -0700198 if (mHomeStack == null && mDisplayId == Display.DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800199 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800200 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700201 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700202 }
203
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700204 TaskStack getStackById(int stackId) {
205 for (int i = mStacks.size() - 1; i >= 0; --i) {
206 final TaskStack stack = mStacks.get(i);
207 if (stack.mStackId == stackId) {
208 return stack;
209 }
210 }
211 return null;
212 }
213
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700214 void checkAppWindowsReadyToShow() {
215 for (int i = mStacks.size() - 1; i >= 0; --i) {
216 final TaskStack stack = mStacks.get(i);
217 stack.checkAppWindowsReadyToShow(mDisplayId);
218 }
219 }
220
221 void updateAllDrawn() {
222 for (int i = mStacks.size() - 1; i >= 0; --i) {
223 final TaskStack stack = mStacks.get(i);
224 stack.updateAllDrawn(mDisplayId);
225 }
226 }
227
228 void stepAppWindowsAnimation(long currentTime) {
229 for (int i = mStacks.size() - 1; i >= 0; --i) {
230 final TaskStack stack = mStacks.get(i);
231 stack.stepAppWindowsAnimation(currentTime, mDisplayId);
232 }
233 }
234
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700235 void onAppTransitionDone() {
236 for (int i = mStacks.size() - 1; i >= 0; --i) {
237 final TaskStack stack = mStacks.get(i);
238 stack.onAppTransitionDone();
239 }
240
241 rebuildAppWindowList();
242 }
243
Wale Ogunwale51362492016-09-08 17:49:17 -0700244 int getOrientation() {
245 // TODO: Most of the logic here can be removed once this class is converted to use
246 // WindowContainer which has an abstract implementation of getOrientation that
247 // should cover this.
248 if (mService.isStackVisibleLocked(DOCKED_STACK_ID)
249 || mService.isStackVisibleLocked(FREEFORM_WORKSPACE_STACK_ID)) {
250 // Apps and their containers are not allowed to specify an orientation while the docked
251 // or freeform stack is visible...except for the home stack/task if the docked stack is
252 // minimized and it actually set something.
Andrii Kulian8072d112016-09-16 11:11:01 -0700253 if (mHomeStack != null && mHomeStack.isVisible()
254 && mDividerControllerLocked.isMinimizedDock()) {
Wale Ogunwale51362492016-09-08 17:49:17 -0700255 final int orientation = mHomeStack.getOrientation();
256 if (orientation != SCREEN_ORIENTATION_UNSET) {
257 return orientation;
258 }
259 }
260 return SCREEN_ORIENTATION_UNSPECIFIED;
261 }
262
263 for (int i = mStacks.size() - 1; i >= 0; --i) {
264 final TaskStack stack = mStacks.get(i);
265 if (!stack.isVisible()) {
266 continue;
267 }
268
269 final int orientation = stack.getOrientation();
270
271 if (orientation == SCREEN_ORIENTATION_BEHIND) {
272 continue;
273 }
274
275 if (orientation != SCREEN_ORIENTATION_UNSET) {
276 if (stack.fillsParent() || orientation != SCREEN_ORIENTATION_UNSPECIFIED) {
277 return orientation;
278 }
279 }
280 }
281
282 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
283 "No app is requesting an orientation, return " + mService.mLastOrientation);
284 // The next app has not been requested to be visible, so we keep the current orientation
285 // to prevent freezing/unfreezing the display too early.
286 return mService.mLastOrientation;
287 }
288
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700289 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700290 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700291 mDisplay.getMetrics(mDisplayMetrics);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800292 for (int i = mStacks.size() - 1; i >= 0; --i) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700293 mStacks.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800294 }
Craig Mautner722285e2012-09-07 13:55:58 -0700295 }
296
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700297 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700298 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
299 if (displayManagerInternal != null) {
300 // Bootstrap the default logical display from the display manager.
301 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
302 if (newDisplayInfo != null) {
303 mDisplayInfo.copyFrom(newDisplayInfo);
304 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700305 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700306
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800307 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
308 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
309 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
310 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700311 }
312
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700313 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700314 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800315 final int orientation = mDisplayInfo.rotation;
316 boolean rotated = (orientation == Surface.ROTATION_90
317 || orientation == Surface.ROTATION_270);
318 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
319 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700320 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800321 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700322 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800323 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700324 out.set(left, top, left + width, top + height);
325 }
326
Chong Zhangf66db432016-01-13 10:39:51 -0800327 void getContentRect(Rect out) {
328 out.set(mContentRect);
329 }
330
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700331 /** Refer to {@link WindowManagerService#attachStack(int, int, boolean)} */
332 void attachStack(TaskStack stack, boolean onTop) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800333 if (stack.mStackId == HOME_STACK_ID) {
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800334 if (mHomeStack != null) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800335 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700336 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800337 mHomeStack = stack;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800338 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700339 if (onTop) {
340 mStacks.add(stack);
341 } else {
342 mStacks.add(0, stack);
343 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800344 layoutNeeded = true;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800345 }
346
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800347 void moveStack(TaskStack stack, boolean toTop) {
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800348 if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) {
349 // This stack is always-on-top silly...
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800350 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom");
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700351 return;
352 }
353
Filip Gruszczynski26ed2652015-08-10 11:02:53 -0700354 if (!mStacks.remove(stack)) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800355 Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable());
Filip Gruszczynski26ed2652015-08-10 11:02:53 -0700356 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700357
358 int addIndex = toTop ? mStacks.size() : 0;
359
360 if (toTop
361 && mService.isStackVisibleLocked(PINNED_STACK_ID)
362 && stack.mStackId != PINNED_STACK_ID) {
363 // The pinned stack is always the top most stack (always-on-top) when it is visible.
364 // So, stack is moved just below the pinned stack.
365 addIndex--;
366 TaskStack topStack = mStacks.get(addIndex);
367 if (topStack.mStackId != PINNED_STACK_ID) {
368 throw new IllegalStateException("Pinned stack isn't top stack??? " + mStacks);
369 }
370 }
371 mStacks.add(addIndex, stack);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800372 }
373
Wale Ogunwalef6192862016-09-10 13:42:30 -0700374 // TODO: Don't forget to switch to WC.removeChild
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700375 void detachChild(TaskStack stack) {
376 detachStack(stack);
377 if (stack.detachFromDisplay()) {
378 mService.mWindowPlacerLocked.requestTraversal();
379 }
380 if (stack.mStackId == DOCKED_STACK_ID) {
381 mService.getDefaultDisplayContentLocked().mDividerControllerLocked
382 .notifyDockedStackExistsChanged(false);
383 }
384 }
385
386 // TODO: See about removing this by untangling the use case in WMS.attachStack()
Craig Mautnerdf88d732014-01-27 09:21:32 -0800387 void detachStack(TaskStack stack) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800388 mDimLayerController.removeDimLayerUser(stack);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800389 mStacks.remove(stack);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800390 }
391
392 /**
393 * Propagate the new bounds to all child stacks.
394 * @param contentRect The bounds to apply at the top level.
395 */
396 void resize(Rect contentRect) {
397 mContentRect.set(contentRect);
398 }
399
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700400 int taskIdFromPoint(int x, int y) {
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800401 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700402 final TaskStack stack = mStacks.get(stackNdx);
403 final int taskId = stack.taskIdFromPoint(x, y);
404 if (taskId != -1) {
405 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -0700406 }
407 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800408 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -0700409 }
410
Chong Zhang8e89b312015-09-09 15:09:30 -0700411 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -0800412 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -0700413 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -0700414 */
Wale Ogunwale15ead902016-09-02 14:30:11 -0700415 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700416 final int delta = mService.dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700417 mTmpTaskForResizePointSearchResult.reset();
Chong Zhang8e89b312015-09-09 15:09:30 -0700418 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
419 TaskStack stack = mStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -0700420 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700421 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700422 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700423
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700424 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
425 if (mTmpTaskForResizePointSearchResult.searchDone) {
426 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -0700427 }
428 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700429 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700430 }
431
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700432 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -0700433 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700434 final int delta = mService.dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700435 mTmpRect2.setEmpty();
Chong Zhangd8ceb852015-11-11 14:53:41 -0800436 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700437 final TaskStack stack = mStacks.get(stackNdx);
438 stack.setTouchExcludeRegion(
439 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -0700440 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800441 // If we removed the focused task above, add it back and only leave its
442 // outside touch area in the exclusion. TapDectector is not interested in
443 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700444 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800445 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
446 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800447 final WindowState inputMethod = mService.mInputMethodWindow;
448 if (inputMethod != null && inputMethod.isVisibleLw()) {
449 // If the input method is visible and the user is typing, we don't want these touch
450 // events to be intercepted and used to change focus. This would likely cause a
451 // disappearance of the input method.
452 inputMethod.getTouchableRegion(mTmpRegion);
453 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
454 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800455 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
456 WindowState win = mTapExcludedWindows.get(i);
457 win.getTouchableRegion(mTmpRegion);
458 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
459 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100460 if (getDockedStackVisibleForUserLocked() != null) {
461 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -0700462 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100463 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
464 }
Craig Mautner1bef3892015-02-17 15:09:47 -0800465 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700466 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -0800467 }
Craig Mautner6601b7b2013-04-29 10:29:11 -0700468 }
469
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800470 void switchUserStacks() {
Craig Mautner858d8a62013-04-23 17:08:34 -0700471 final WindowList windows = getWindowList();
472 for (int i = 0; i < windows.size(); i++) {
473 final WindowState win = windows.get(i);
474 if (win.isHiddenFromUserLocked()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800475 if (DEBUG_VISIBILITY) Slog.w(TAG_WM, "user changing, hiding " + win
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800476 + ", attrs=" + win.mAttrs.type + ", belonging to " + win.mOwnerUid);
Craig Mautner858d8a62013-04-23 17:08:34 -0700477 win.hideLw(false);
478 }
479 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700480
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800481 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800482 mStacks.get(stackNdx).switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -0700483 }
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700484
485 rebuildAppWindowList();
Craig Mautner858d8a62013-04-23 17:08:34 -0700486 }
487
Craig Mautner05d29032013-05-03 13:40:13 -0700488 void resetAnimationBackgroundAnimator() {
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800489 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
490 mStacks.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -0700491 }
492 }
493
494 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800495 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -0700496 }
497
498 void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800499 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700500 }
501
502 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800503 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700504 }
505
506 void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800507 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -0700508 }
509
Craig Mautner2eb15342013-08-07 13:13:35 -0700510 void close() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800511 mDimLayerController.close();
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800512 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
513 mStacks.get(stackNdx).close();
Craig Mautner2eb15342013-08-07 13:13:35 -0700514 }
515 }
516
Craig Mautner95da1082014-02-24 17:54:35 -0800517 boolean isAnimating() {
518 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
519 final TaskStack stack = mStacks.get(stackNdx);
520 if (stack.isAnimating()) {
521 return true;
522 }
523 }
524 return false;
525 }
526
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700527 /** Returns true if a removal action is still being deferred. */
528 boolean checkCompleteDeferredRemoval() {
529 boolean stillDeferringRemoval = false;
Craig Mautner95da1082014-02-24 17:54:35 -0800530 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
531 final TaskStack stack = mStacks.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700532 stillDeferringRemoval |= stack.checkCompleteDeferredRemoval();
Craig Mautner95da1082014-02-24 17:54:35 -0800533 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700534 if (!stillDeferringRemoval && mDeferredRemoval) {
Craig Mautner95da1082014-02-24 17:54:35 -0800535 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700536 return false;
Craig Mautner95da1082014-02-24 17:54:35 -0800537 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700538 return true;
Craig Mautner95da1082014-02-24 17:54:35 -0800539 }
540
Wale Ogunwale94744212015-09-21 19:01:47 -0700541 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
542 final int rotationDelta = DisplayContent.deltaRotation(oldRotation, newRotation);
543 getLogicalDisplayRect(mTmpRect);
544 switch (rotationDelta) {
545 case Surface.ROTATION_0:
546 mTmpRect2.set(bounds);
547 break;
548 case Surface.ROTATION_90:
549 mTmpRect2.top = mTmpRect.bottom - bounds.right;
550 mTmpRect2.left = bounds.top;
551 mTmpRect2.right = mTmpRect2.left + bounds.height();
552 mTmpRect2.bottom = mTmpRect2.top + bounds.width();
553 break;
554 case Surface.ROTATION_180:
555 mTmpRect2.top = mTmpRect.bottom - bounds.bottom;
556 mTmpRect2.left = mTmpRect.right - bounds.right;
557 mTmpRect2.right = mTmpRect2.left + bounds.width();
558 mTmpRect2.bottom = mTmpRect2.top + bounds.height();
559 break;
560 case Surface.ROTATION_270:
561 mTmpRect2.top = bounds.left;
562 mTmpRect2.left = mTmpRect.right - bounds.bottom;
563 mTmpRect2.right = mTmpRect2.left + bounds.height();
564 mTmpRect2.bottom = mTmpRect2.top + bounds.width();
565 break;
566 }
567 bounds.set(mTmpRect2);
568 }
569
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800570 static int deltaRotation(int oldRotation, int newRotation) {
571 int delta = newRotation - oldRotation;
572 if (delta < 0) delta += 4;
573 return delta;
574 }
575
Craig Mautnera91f9e22012-09-14 16:22:08 -0700576 public void dump(String prefix, PrintWriter pw) {
577 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
578 final String subPrefix = " " + prefix;
579 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
580 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
581 pw.print("dpi");
582 if (mInitialDisplayWidth != mBaseDisplayWidth
583 || mInitialDisplayHeight != mBaseDisplayHeight
584 || mInitialDisplayDensity != mBaseDisplayDensity) {
585 pw.print(" base=");
586 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
587 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
588 }
Jeff Brownd46747a2015-04-15 19:02:36 -0700589 if (mDisplayScalingDisabled) {
590 pw.println(" noscale");
591 }
Craig Mautnera91f9e22012-09-14 16:22:08 -0700592 pw.print(" cur=");
593 pw.print(mDisplayInfo.logicalWidth);
594 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
595 pw.print(" app=");
596 pw.print(mDisplayInfo.appWidth);
597 pw.print("x"); pw.print(mDisplayInfo.appHeight);
598 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
599 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
600 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
601 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Craig Mautner95da1082014-02-24 17:54:35 -0800602 pw.print(subPrefix); pw.print("deferred="); pw.print(mDeferredRemoval);
603 pw.print(" layoutNeeded="); pw.println(layoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800604
Craig Mautnerdc548482014-02-05 13:35:24 -0800605 pw.println();
Craig Mautnere8b85fd2014-10-22 09:23:25 -0700606 pw.println(" Application tokens in top down Z order:");
Craig Mautnere8b85fd2014-10-22 09:23:25 -0700607 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnerbcb6eb92015-01-13 13:09:22 -0800608 final TaskStack stack = mStacks.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800609 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700610 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800611
Craig Mautnerdc548482014-02-05 13:35:24 -0800612 pw.println();
613 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700614 pw.println();
615 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800616 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700617 WindowToken token = mExitingTokens.get(i);
618 pw.print(" Exiting #"); pw.print(i);
619 pw.print(' '); pw.print(token);
620 pw.println(':');
621 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800622 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700623 }
Craig Mautner59c00972012-07-30 12:10:24 -0700624 pw.println();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800625 mDimLayerController.dump(prefix + " ", pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -0700626 pw.println();
627 mDividerControllerLocked.dump(prefix + " ", pw);
Craig Mautner59c00972012-07-30 12:10:24 -0700628 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800629
630 @Override
631 public String toString() {
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700632 return getName() + " stacks=" + mStacks;
633 }
634
635 String getName() {
636 return "Display " + mDisplayId + " info=" + mDisplayInfo;
Craig Mautnere0a38842013-12-16 16:14:02 -0800637 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700638
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800639 /**
640 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
641 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700642 TaskStack getDockedStackLocked() {
Wale Ogunwalee45899a2015-10-01 11:30:34 -0700643 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700644 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700645 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -0800646
647 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800648 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
649 * visible, as long as it's not hidden because the current user doesn't have any tasks there.
650 */
651 TaskStack getDockedStackVisibleForUserLocked() {
652 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700653 return (stack != null && stack.isVisible(true /* ignoreKeyguard */)) ? stack : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800654 }
655
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700656 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -0800657 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
658 WindowState touchedWin = null;
659 final int x = (int) xf;
660 final int y = (int) yf;
661
662 for (int i = mWindows.size() - 1; i >= 0; i--) {
663 WindowState window = mWindows.get(i);
664 final int flags = window.mAttrs.flags;
665 if (!window.isVisibleLw()) {
666 continue;
667 }
668 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
669 continue;
670 }
671
672 window.getVisibleBounds(mTmpRect);
673 if (!mTmpRect.contains(x, y)) {
674 continue;
675 }
676
677 window.getTouchableRegion(mTmpRegion);
678
679 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
680 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
681 touchedWin = window;
682 break;
683 }
684 }
685
686 return touchedWin;
687 }
Jorim Jaggi6626f542016-08-22 13:08:44 -0700688
689 /**
690 * See {@link WindowManagerService#overridePlayingAppAnimationsLw}.
691 */
692 void overridePlayingAppAnimationsLw(Animation a) {
693 for (int i = mStacks.size() - 1; i >= 0; i--) {
694 mStacks.get(i).overridePlayingAppAnimations(a);
695 }
696 }
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700697
698 boolean canAddToastWindowForUid(int uid) {
699 // We allow one toast window per UID being shown at a time.
700 WindowList windows = getWindowList();
701 final int windowCount = windows.size();
702 for (int i = 0; i < windowCount; i++) {
703 WindowState window = windows.get(i);
704 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == uid
705 && !window.mPermanentlyHidden && !window.mAnimatingExit) {
706 return false;
707 }
708 }
709 return true;
710 }
711
712 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus,
713 WindowState newFocus) {
714 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
715 return;
716 }
717 final int lostFocusUid = oldFocus.mOwnerUid;
718 WindowList windows = getWindowList();
719 final int windowCount = windows.size();
720 for (int i = 0; i < windowCount; i++) {
721 WindowState window = windows.get(i);
722 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == lostFocusUid) {
723 if (!mService.mH.hasMessages(WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window)) {
724 mService.mH.sendMessageDelayed(
725 mService.mH.obtainMessage(
726 WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window),
727 window.mAttrs.hideTimeoutMilliseconds);
728 }
729 }
730 }
731 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700732
733 WindowState findFocusedWindow() {
734 final AppWindowToken focusedApp = mService.mFocusedApp;
735
736 for (int i = mWindows.size() - 1; i >= 0; i--) {
737 final WindowState win = mWindows.get(i);
738
739 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + i + " = " + win
740 + ", flags=" + win.mAttrs.flags + ", canReceive=" + win.canReceiveKeys());
741
742 if (!win.canReceiveKeys()) {
743 continue;
744 }
745
746 final AppWindowToken wtoken = win.mAppToken;
747
748 // If this window's application has been removed, just skip it.
749 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
750 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
751 + (wtoken.removed ? "removed" : "sendingToBottom"));
752 continue;
753 }
754
755 if (focusedApp == null) {
756 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
757 + " using new focus @ " + i + " = " + win);
758 return win;
759 }
760
761 if (!focusedApp.windowsAreFocusable()) {
762 // Current focused app windows aren't focusable...
763 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
764 + " focusable using new focus @ " + i + " = " + win);
765 return win;
766 }
767
768 // Descend through all of the app tokens and find the first that either matches
769 // win.mAppToken (return win) or mFocusedApp (return null).
770 if (wtoken != null && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
771 final TaskStack focusedAppStack = focusedApp.mTask.mStack;
772 final TaskStack appStack = wtoken.mTask.mStack;
773
774 // TODO: Use WindowContainer.compareTo() once everything is using WindowContainer
Wale Ogunwale14a3fb92016-09-11 15:19:05 -0700775 if ((focusedAppStack == appStack && focusedApp.compareTo(wtoken) > 0)
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700776 || mStacks.indexOf(focusedAppStack) > mStacks.indexOf(appStack)) {
777 // App stack below focused app stack. No focus for you!!!
778 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
779 "findFocusedWindow: Reached focused app=" + focusedApp);
780 return null;
781 }
782 }
783
784 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ "
785 + i + " = " + win);
786 return win;
787 }
788
789 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
790 return null;
791 }
Wale Ogunwaleec731152016-09-08 20:18:57 -0700792
793 int addAppWindowToWindowList(final WindowState win) {
794 final IWindow client = win.mClient;
795
796 WindowList tokenWindowList = getTokenWindowsOnDisplay(win.mToken);
797 if (!tokenWindowList.isEmpty()) {
798 return addAppWindowExisting(win, tokenWindowList);
799 }
800
801 // No windows from this token on this display
802 if (mService.localLOGV) Slog.v(TAG_WM, "Figuring out where to add app window "
803 + client.asBinder() + " (token=" + this + ")");
804
805 final WindowToken wToken = win.mToken;
806
807 // Figure out where the window should go, based on the order of applications.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700808 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwaleec731152016-09-08 20:18:57 -0700809 for (int i = mStacks.size() - 1; i >= 0; --i) {
810 final TaskStack stack = mStacks.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700811 stack.getWindowOnDisplayBeforeToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
812 if (mTmpGetWindowOnDisplaySearchResult.reachedToken) {
Wale Ogunwaleec731152016-09-08 20:18:57 -0700813 // We have reach the token we are interested in. End search.
814 break;
815 }
816 }
817
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700818 WindowState pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700819
820 // We now know the index into the apps. If we found an app window above, that gives us the
821 // position; else we need to look some more.
822 if (pos != null) {
823 // Move behind any windows attached to this one.
824 final WindowToken atoken = mService.mTokenMap.get(pos.mClient.asBinder());
825 if (atoken != null) {
826 tokenWindowList = getTokenWindowsOnDisplay(atoken);
827 final int NC = tokenWindowList.size();
828 if (NC > 0) {
829 WindowState bottom = tokenWindowList.get(0);
830 if (bottom.mSubLayer < 0) {
831 pos = bottom;
832 }
833 }
834 }
835 addWindowToListBefore(win, pos);
836 return 0;
837 }
838
839 // Continue looking down until we find the first token that has windows on this display.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700840 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwaleec731152016-09-08 20:18:57 -0700841 for (int i = mStacks.size() - 1; i >= 0; --i) {
842 final TaskStack stack = mStacks.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700843 stack.getWindowOnDisplayAfterToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
844 if (mTmpGetWindowOnDisplaySearchResult.foundWindow != null) {
Wale Ogunwaleec731152016-09-08 20:18:57 -0700845 // We have found a window after the token. End search.
846 break;
847 }
848 }
849
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700850 pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700851
852 if (pos != null) {
853 // Move in front of any windows attached to this one.
854 final WindowToken atoken = mService.mTokenMap.get(pos.mClient.asBinder());
855 if (atoken != null) {
856 final WindowState top = atoken.getTopWindow();
857 if (top != null && top.mSubLayer >= 0) {
858 pos = top;
859 }
860 }
861 addWindowToListAfter(win, pos);
862 return 0;
863 }
864
865 // Just search for the start of this layer.
866 final int myLayer = win.mBaseLayer;
867 int i;
868 for (i = mWindows.size() - 1; i >= 0; --i) {
869 final WindowState w = mWindows.get(i);
870 // Dock divider shares the base layer with application windows, but we want to always
871 // keep it above the application windows. The sharing of the base layer is intended
872 // for window animations, which need to be above the dock divider for the duration
873 // of the animation.
874 if (w.mBaseLayer <= myLayer && w.mAttrs.type != TYPE_DOCK_DIVIDER) {
875 break;
876 }
877 }
878 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
879 "Based on layer: Adding window " + win + " at " + (i + 1) + " of "
880 + mWindows.size());
881 mWindows.add(i + 1, win);
882 mService.mWindowsChanged = true;
883 return 0;
884 }
885
886 /** Adds this non-app window to the window list. */
887 void addNonAppWindowToWindowList(WindowState win) {
888 // Figure out where window should go, based on layer.
889 int i;
890 for (i = mWindows.size() - 1; i >= 0; i--) {
891 final WindowState otherWin = mWindows.get(i);
892 if (otherWin.getBaseType() != TYPE_WALLPAPER && otherWin.mBaseLayer <= win.mBaseLayer) {
893 // Wallpaper wanders through the window list, for example to position itself
894 // directly behind keyguard. Because of this it will break the ordering based on
895 // WindowState.mBaseLayer. There might windows with higher mBaseLayer behind it and
896 // we don't want the new window to appear above them. An example of this is adding
897 // of the docked stack divider. Consider a scenario with the following ordering (top
898 // to bottom): keyguard, wallpaper, assist preview, apps. We want the dock divider
899 // to land below the assist preview, so the dock divider must ignore the wallpaper,
900 // with which it shares the base layer.
901 break;
902 }
903 }
904
905 i++;
906 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
907 "Free window: Adding window " + this + " at " + i + " of " + mWindows.size());
908 mWindows.add(i, win);
909 mService.mWindowsChanged = true;
910 }
911
912 void addChildWindowToWindowList(WindowState win) {
913 final WindowState parentWindow = win.getParentWindow();
914
915 WindowList windowsOnSameDisplay = getTokenWindowsOnDisplay(win.mToken);
916
917 // Figure out this window's ordering relative to the parent window.
918 final int wCount = windowsOnSameDisplay.size();
919 final int sublayer = win.mSubLayer;
920 int largestSublayer = Integer.MIN_VALUE;
921 WindowState windowWithLargestSublayer = null;
922 int i;
923 for (i = 0; i < wCount; i++) {
924 WindowState w = windowsOnSameDisplay.get(i);
925 final int wSublayer = w.mSubLayer;
926 if (wSublayer >= largestSublayer) {
927 largestSublayer = wSublayer;
928 windowWithLargestSublayer = w;
929 }
930 if (sublayer < 0) {
931 // For negative sublayers, we go below all windows in the same sublayer.
932 if (wSublayer >= sublayer) {
933 addWindowToListBefore(win, wSublayer >= 0 ? parentWindow : w);
934 break;
935 }
936 } else {
937 // For positive sublayers, we go above all windows in the same sublayer.
938 if (wSublayer > sublayer) {
939 addWindowToListBefore(win, w);
940 break;
941 }
942 }
943 }
944 if (i >= wCount) {
945 if (sublayer < 0) {
946 addWindowToListBefore(win, parentWindow);
947 } else {
948 addWindowToListAfter(win,
949 largestSublayer >= 0 ? windowWithLargestSublayer : parentWindow);
950 }
951 }
952 }
953
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700954 /**
955 * Z-orders the display window list so that:
956 * <ul>
957 * <li>Any windows that are currently below the wallpaper window stay below the wallpaper
958 * window.
959 * <li>Exiting application windows are at the bottom, but above the wallpaper window.
960 * <li>All other application windows are above the exiting application windows and ordered based
961 * on the ordering of their stacks and tasks on the display.
962 * <li>Non-application windows are at the very top.
963 * </ul>
964 * <p>
965 * NOTE: This isn't a complete picture of what the user see. Further manipulation of the window
966 * surface layering is done in {@link WindowLayersController}.
967 */
968 void rebuildAppWindowList() {
969 int count = mWindows.size();
970 int i;
971 int lastBelow = -1;
972 int numRemoved = 0;
973
974 if (mRebuildTmp.length < count) {
975 mRebuildTmp = new WindowState[count + 10];
976 }
977
978 // First remove all existing app windows.
979 i = 0;
980 while (i < count) {
981 final WindowState w = mWindows.get(i);
982 if (w.mAppToken != null) {
983 final WindowState win = mWindows.remove(i);
984 win.mRebuilding = true;
985 mRebuildTmp[numRemoved] = win;
986 mService.mWindowsChanged = true;
987 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Rebuild removing window: " + win);
988 count--;
989 numRemoved++;
990 continue;
991 } else if (lastBelow == i-1) {
992 if (w.mAttrs.type == TYPE_WALLPAPER) {
993 lastBelow = i;
994 }
995 }
996 i++;
997 }
998
999 // Keep whatever windows were below the app windows still below, by skipping them.
1000 lastBelow++;
1001 i = lastBelow;
1002
1003 // First add all of the exiting app tokens... these are no longer in the main app list,
1004 // but still have windows shown. We put them in the back because now that the animation is
1005 // over we no longer will care about them.
1006 final int numStacks = mStacks.size();
1007 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1008 AppTokenList exitingAppTokens = mStacks.get(stackNdx).mExitingAppTokens;
1009 int NT = exitingAppTokens.size();
1010 for (int j = 0; j < NT; j++) {
1011 i = exitingAppTokens.get(j).rebuildWindowList(this, i);
1012 }
1013 }
1014
1015 // And add in the still active app tokens in Z order.
1016 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1017 i = mStacks.get(stackNdx).rebuildWindowList(this, i);
1018 }
1019
1020 i -= lastBelow;
1021 if (i != numRemoved) {
1022 layoutNeeded = true;
1023 Slog.w(TAG_WM, "On display=" + mDisplayId + " Rebuild removed " + numRemoved
1024 + " windows but added " + i + " rebuildAppWindowListLocked() "
1025 + " callers=" + Debug.getCallers(10));
1026 for (i = 0; i < numRemoved; i++) {
1027 WindowState ws = mRebuildTmp[i];
1028 if (ws.mRebuilding) {
1029 StringWriter sw = new StringWriter();
1030 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1031 ws.dump(pw, "", true);
1032 pw.flush();
1033 Slog.w(TAG_WM, "This window was lost: " + ws);
1034 Slog.w(TAG_WM, sw.toString());
1035 ws.mWinAnimator.destroySurfaceLocked();
1036 }
1037 }
1038 Slog.w(TAG_WM, "Current app token list:");
1039 dumpChildrenNames();
1040 Slog.w(TAG_WM, "Final window list:");
1041 dumpWindows();
1042 }
1043 Arrays.fill(mRebuildTmp, null);
1044 }
1045
Wale Ogunwaleec731152016-09-08 20:18:57 -07001046 /** Return the list of Windows on this display associated with the input token. */
1047 WindowList getTokenWindowsOnDisplay(WindowToken token) {
1048 final WindowList windowList = new WindowList();
1049 final int count = mWindows.size();
1050 for (int i = 0; i < count; i++) {
1051 final WindowState win = mWindows.get(i);
1052 if (win.mToken == token) {
1053 windowList.add(win);
1054 }
1055 }
1056 return windowList;
1057 }
1058
1059 private int addAppWindowExisting(WindowState win, WindowList tokenWindowList) {
1060
1061 int tokenWindowsPos;
1062 // If this application has existing windows, we simply place the new window on top of
1063 // them... but keep the starting window on top.
1064 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
1065 // Base windows go behind everything else.
1066 final WindowState lowestWindow = tokenWindowList.get(0);
1067 addWindowToListBefore(win, lowestWindow);
1068 tokenWindowsPos = win.mToken.getWindowIndex(lowestWindow);
1069 } else {
1070 final AppWindowToken atoken = win.mAppToken;
1071 final int windowListPos = tokenWindowList.size();
1072 final WindowState lastWindow = tokenWindowList.get(windowListPos - 1);
1073 if (atoken != null && lastWindow == atoken.startingWindow) {
1074 addWindowToListBefore(win, lastWindow);
1075 tokenWindowsPos = win.mToken.getWindowIndex(lastWindow);
1076 } else {
1077 int newIdx = findIdxBasedOnAppTokens(win);
1078 // There is a window above this one associated with the same apptoken note that the
1079 // window could be a floating window that was created later or a window at the top
1080 // of the list of windows associated with this token.
1081 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1082 "not Base app: Adding window " + win + " at " + (newIdx + 1) + " of "
1083 + mWindows.size());
1084 mWindows.add(newIdx + 1, win);
1085 if (newIdx < 0) {
1086 // No window from token found on win's display.
1087 tokenWindowsPos = 0;
1088 } else {
1089 tokenWindowsPos = win.mToken.getWindowIndex(mWindows.get(newIdx)) + 1;
1090 }
1091 mService.mWindowsChanged = true;
1092 }
1093 }
1094 return tokenWindowsPos;
1095 }
1096
1097 /** Places the first input window after the second input window in the window list. */
1098 private void addWindowToListAfter(WindowState first, WindowState second) {
1099 final int i = mWindows.indexOf(second);
1100 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1101 "Adding window " + this + " at " + (i + 1) + " of " + mWindows.size()
1102 + " (after " + second + ")");
1103 mWindows.add(i + 1, first);
1104 mService.mWindowsChanged = true;
1105 }
1106
1107 /** Places the first input window before the second input window in the window list. */
1108 private void addWindowToListBefore(WindowState first, WindowState second) {
1109 int i = mWindows.indexOf(second);
1110 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1111 "Adding window " + this + " at " + i + " of " + mWindows.size()
1112 + " (before " + second + ")");
1113 if (i < 0) {
1114 Slog.w(TAG_WM, "addWindowToListBefore: Unable to find " + second + " in " + mWindows);
1115 i = 0;
1116 }
1117 mWindows.add(i, first);
1118 mService.mWindowsChanged = true;
1119 }
1120
1121 /**
1122 * This method finds out the index of a window that has the same app token as win. used for z
1123 * ordering the windows in mWindows
1124 */
1125 private int findIdxBasedOnAppTokens(WindowState win) {
1126 for(int j = mWindows.size() - 1; j >= 0; j--) {
1127 final WindowState wentry = mWindows.get(j);
1128 if(wentry.mAppToken == win.mAppToken) {
1129 return j;
1130 }
1131 }
1132 return -1;
1133 }
1134
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001135 private void dumpChildrenNames() {
1136 StringWriter sw = new StringWriter();
1137 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1138 dumpChildrenNames(pw, " ");
1139 }
1140
1141 private void dumpChildrenNames(PrintWriter pw, String prefix) {
1142 final String childPrefix = prefix + prefix;
1143 for (int j = mStacks.size() - 1; j >= 0; j--) {
1144 final TaskStack stack = mStacks.get(j);
1145 pw.println("#" + j + " " + getName());
1146 stack.dumpChildrenNames(pw, childPrefix);
1147 }
1148 }
1149
1150 private void dumpWindows() {
1151 final int numDisplays = mService.mDisplayContents.size();
1152 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
1153 final DisplayContent displayContent = mService.mDisplayContents.valueAt(displayNdx);
1154 Slog.v(TAG_WM, " Display #" + displayContent.getDisplayId());
1155 final WindowList windows = displayContent.getWindowList();
1156 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
1157 Slog.v(TAG_WM, " #" + winNdx + ": " + windows.get(winNdx));
1158 }
1159 }
1160 }
1161
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001162 static final class GetWindowOnDisplaySearchResult {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001163 boolean reachedToken;
1164 WindowState foundWindow;
1165
1166 void reset() {
1167 reachedToken = false;
1168 foundWindow = null;
1169 }
1170 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001171
1172 static final class TaskForResizePointSearchResult {
1173 boolean searchDone;
1174 Task taskForResize;
1175
1176 void reset() {
1177 searchDone = false;
1178 taskForResize = null;
1179 }
1180 }
Craig Mautner59c00972012-07-30 12:10:24 -07001181}