blob: 4fc3ab0ec5033cf7d23ff234eae12dd0f91bf609 [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
Andrii Kulian3a507b52016-09-19 18:14:12 -070043import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -070044import android.app.ActivityManager.StackId;
Craig Mautnerc00204b2013-03-05 15:02:14 -080045import android.graphics.Rect;
Craig Mautner6601b7b2013-04-29 10:29:11 -070046import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +010047import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070048import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070049import android.os.Debug;
Chong Zhang8e89b312015-09-09 15:09:30 -070050import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -070051import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -070052import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -070053import android.view.DisplayInfo;
Wale Ogunwaleec731152016-09-08 20:18:57 -070054import android.view.IWindow;
Craig Mautner4a1cb222013-12-04 16:14:06 -080055import android.view.Surface;
Jorim Jaggi6626f542016-08-22 13:08:44 -070056import android.view.animation.Animation;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070057import com.android.internal.util.FastPrintWriter;
Craig Mautner59c00972012-07-30 12:10:24 -070058
Robert Carr3b716242016-08-16 16:02:21 -070059import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -070060import java.io.PrintWriter;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070061import java.io.StringWriter;
Craig Mautner59c00972012-07-30 12:10:24 -070062import java.util.ArrayList;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070063import java.util.Arrays;
Andrii Kulian3a507b52016-09-19 18:14:12 -070064import java.util.List;
Craig Mautner59c00972012-07-30 12:10:24 -070065
66class DisplayContentList extends ArrayList<DisplayContent> {
67}
68
69/**
70 * Utility class for keeping track of the WindowStates and other pertinent contents of a
71 * particular Display.
72 *
73 * IMPORTANT: No method from this class should ever be used without holding
74 * WindowManagerService.mWindowMap.
75 */
76class DisplayContent {
77
78 /** Unique identifier of this stack. */
79 private final int mDisplayId;
80
81 /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element
82 * from mDisplayWindows; */
Craig Mautnerdc548482014-02-05 13:35:24 -080083 private final WindowList mWindows = new WindowList();
Craig Mautner59c00972012-07-30 12:10:24 -070084
Craig Mautner59c00972012-07-30 12:10:24 -070085 int mInitialDisplayWidth = 0;
86 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -070087 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -070088 int mBaseDisplayWidth = 0;
89 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -070090 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -070091 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -070092 private final DisplayInfo mDisplayInfo = new DisplayInfo();
93 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070094 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Craig Mautner59c00972012-07-30 12:10:24 -070095
Craig Mautner6601b7b2013-04-29 10:29:11 -070096 Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -070097 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -070098
Craig Mautner39834192012-09-02 07:47:24 -070099 // Accessed directly by all users.
100 boolean layoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700101 int pendingLayoutChanges;
Craig Mautner69b08182012-09-05 13:07:13 -0700102 final boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700103
Craig Mautnerdc548482014-02-05 13:35:24 -0800104 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700105 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800106
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800107 /** Array containing all TaskStacks on this display. Array
Craig Mautnercf910b02013-04-23 11:23:27 -0700108 * is stored in display order with the current bottom stack at 0. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700109 private final ArrayList<TaskStack> mStacks = new ArrayList<>();
Craig Mautnerc00204b2013-03-05 15:02:14 -0800110
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800111 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
112 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700113 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700114
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700115 /** Detect user tapping outside of current focused task bounds .*/
116 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700117
Craig Mautner6601b7b2013-04-29 10:29:11 -0700118 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700119 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700120
Craig Mautner6601b7b2013-04-29 10:29:11 -0700121 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800122 private final Rect mTmpRect = new Rect();
123 private final Rect mTmpRect2 = new Rect();
124 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700125
Craig Mautner9d808b12013-08-06 18:00:25 -0700126 final WindowManagerService mService;
127
Craig Mautner95da1082014-02-24 17:54:35 -0800128 /** Remove this display when animation on it has completed. */
129 boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800130
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700131 final DockedStackDividerController mDividerControllerLocked;
132
Chong Zhang112eb8c2015-11-02 11:17:00 -0800133 final DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700134
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800135 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
136
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700137 /** Used when rebuilding window list to keep track of windows that have been removed. */
138 private WindowState[] mRebuildTmp = new WindowState[20];
139
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700140 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
141 new TaskForResizePointSearchResult();
142 private final GetWindowOnDisplaySearchResult mTmpGetWindowOnDisplaySearchResult =
143 new GetWindowOnDisplaySearchResult();
144
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800145 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700146 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800147 * @param service You know.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700148 */
Craig Mautner9d808b12013-08-06 18:00:25 -0700149 DisplayContent(Display display, WindowManagerService service) {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700150 mDisplay = display;
151 mDisplayId = display.getDisplayId();
152 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700153 display.getMetrics(mDisplayMetrics);
Craig Mautner69b08182012-09-05 13:07:13 -0700154 isDefaultDisplay = mDisplayId == Display.DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700155 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700156 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800157 mDividerControllerLocked = new DockedStackDividerController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800158 mDimLayerController = new DimLayerController(this);
Craig Mautner59c00972012-07-30 12:10:24 -0700159 }
160
161 int getDisplayId() {
162 return mDisplayId;
163 }
164
165 WindowList getWindowList() {
166 return mWindows;
167 }
168
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700169 Display getDisplay() {
170 return mDisplay;
171 }
172
Craig Mautner59c00972012-07-30 12:10:24 -0700173 DisplayInfo getDisplayInfo() {
174 return mDisplayInfo;
175 }
176
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700177 DisplayMetrics getDisplayMetrics() {
178 return mDisplayMetrics;
179 }
180
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100181 DockedStackDividerController getDockedDividerController() {
182 return mDividerControllerLocked;
183 }
184
Jeff Browna506a6e2013-06-04 00:02:38 -0700185 /**
186 * Returns true if the specified UID has access to this display.
187 */
188 public boolean hasAccess(int uid) {
189 return mDisplay.hasAccess(uid);
190 }
191
keunyounga446bf02013-06-21 19:07:57 -0700192 public boolean isPrivate() {
193 return (mDisplay.getFlags() & Display.FLAG_PRIVATE) != 0;
194 }
195
Craig Mautnerdc548482014-02-05 13:35:24 -0800196 ArrayList<TaskStack> getStacks() {
197 return mStacks;
198 }
199
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700200 TaskStack getHomeStack() {
Craig Mautner333c2ec2014-10-02 12:24:02 -0700201 if (mHomeStack == null && mDisplayId == Display.DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800202 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800203 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700204 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700205 }
206
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700207 TaskStack getStackById(int stackId) {
208 for (int i = mStacks.size() - 1; i >= 0; --i) {
209 final TaskStack stack = mStacks.get(i);
210 if (stack.mStackId == stackId) {
211 return stack;
212 }
213 }
214 return null;
215 }
216
Andrii Kulian3a507b52016-09-19 18:14:12 -0700217 /** Callback used to notify about configuration changes. */
218 void onConfigurationChanged(@NonNull List<Integer> changedStackList) {
219 // The display size information is heavily dependent on the resources in the current
220 // configuration, so we need to reconfigure it every time the configuration changes.
221 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
222 mService.reconfigureDisplayLocked(this);
223
224 getDockedDividerController().onConfigurationChanged();
225
226 for (int i = 0; i < mStacks.size(); i++) {
227 final TaskStack stack = mStacks.get(i);
228 if (stack.onConfigurationChanged()) {
229 changedStackList.add(stack.mStackId);
230 }
231 }
232 }
233
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700234 void checkAppWindowsReadyToShow() {
235 for (int i = mStacks.size() - 1; i >= 0; --i) {
236 final TaskStack stack = mStacks.get(i);
237 stack.checkAppWindowsReadyToShow(mDisplayId);
238 }
239 }
240
241 void updateAllDrawn() {
242 for (int i = mStacks.size() - 1; i >= 0; --i) {
243 final TaskStack stack = mStacks.get(i);
244 stack.updateAllDrawn(mDisplayId);
245 }
246 }
247
248 void stepAppWindowsAnimation(long currentTime) {
249 for (int i = mStacks.size() - 1; i >= 0; --i) {
250 final TaskStack stack = mStacks.get(i);
251 stack.stepAppWindowsAnimation(currentTime, mDisplayId);
252 }
253 }
254
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700255 void onAppTransitionDone() {
256 for (int i = mStacks.size() - 1; i >= 0; --i) {
257 final TaskStack stack = mStacks.get(i);
258 stack.onAppTransitionDone();
259 }
260
261 rebuildAppWindowList();
262 }
263
Wale Ogunwale51362492016-09-08 17:49:17 -0700264 int getOrientation() {
265 // TODO: Most of the logic here can be removed once this class is converted to use
266 // WindowContainer which has an abstract implementation of getOrientation that
267 // should cover this.
268 if (mService.isStackVisibleLocked(DOCKED_STACK_ID)
269 || mService.isStackVisibleLocked(FREEFORM_WORKSPACE_STACK_ID)) {
270 // Apps and their containers are not allowed to specify an orientation while the docked
271 // or freeform stack is visible...except for the home stack/task if the docked stack is
272 // minimized and it actually set something.
Andrii Kulian8072d112016-09-16 11:11:01 -0700273 if (mHomeStack != null && mHomeStack.isVisible()
274 && mDividerControllerLocked.isMinimizedDock()) {
Wale Ogunwale51362492016-09-08 17:49:17 -0700275 final int orientation = mHomeStack.getOrientation();
276 if (orientation != SCREEN_ORIENTATION_UNSET) {
277 return orientation;
278 }
279 }
280 return SCREEN_ORIENTATION_UNSPECIFIED;
281 }
282
283 for (int i = mStacks.size() - 1; i >= 0; --i) {
284 final TaskStack stack = mStacks.get(i);
285 if (!stack.isVisible()) {
286 continue;
287 }
288
289 final int orientation = stack.getOrientation();
290
291 if (orientation == SCREEN_ORIENTATION_BEHIND) {
292 continue;
293 }
294
295 if (orientation != SCREEN_ORIENTATION_UNSET) {
296 if (stack.fillsParent() || orientation != SCREEN_ORIENTATION_UNSPECIFIED) {
297 return orientation;
298 }
299 }
300 }
301
302 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
303 "No app is requesting an orientation, return " + mService.mLastOrientation);
304 // The next app has not been requested to be visible, so we keep the current orientation
305 // to prevent freezing/unfreezing the display too early.
306 return mService.mLastOrientation;
307 }
308
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700309 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700310 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700311 mDisplay.getMetrics(mDisplayMetrics);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800312 for (int i = mStacks.size() - 1; i >= 0; --i) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700313 mStacks.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800314 }
Craig Mautner722285e2012-09-07 13:55:58 -0700315 }
316
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700317 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700318 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
319 if (displayManagerInternal != null) {
320 // Bootstrap the default logical display from the display manager.
321 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
322 if (newDisplayInfo != null) {
323 mDisplayInfo.copyFrom(newDisplayInfo);
324 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700325 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700326
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800327 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
328 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
329 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
330 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700331 }
332
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700333 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700334 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800335 final int orientation = mDisplayInfo.rotation;
336 boolean rotated = (orientation == Surface.ROTATION_90
337 || orientation == Surface.ROTATION_270);
338 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
339 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700340 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800341 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700342 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800343 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700344 out.set(left, top, left + width, top + height);
345 }
346
Chong Zhangf66db432016-01-13 10:39:51 -0800347 void getContentRect(Rect out) {
348 out.set(mContentRect);
349 }
350
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700351 /** Refer to {@link WindowManagerService#attachStack(int, int, boolean)} */
352 void attachStack(TaskStack stack, boolean onTop) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800353 if (stack.mStackId == HOME_STACK_ID) {
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800354 if (mHomeStack != null) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800355 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700356 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800357 mHomeStack = stack;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800358 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700359 if (onTop) {
360 mStacks.add(stack);
361 } else {
362 mStacks.add(0, stack);
363 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800364 layoutNeeded = true;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800365 }
366
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800367 void moveStack(TaskStack stack, boolean toTop) {
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800368 if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) {
369 // This stack is always-on-top silly...
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800370 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom");
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700371 return;
372 }
373
Filip Gruszczynski26ed2652015-08-10 11:02:53 -0700374 if (!mStacks.remove(stack)) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800375 Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable());
Filip Gruszczynski26ed2652015-08-10 11:02:53 -0700376 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700377
378 int addIndex = toTop ? mStacks.size() : 0;
379
380 if (toTop
381 && mService.isStackVisibleLocked(PINNED_STACK_ID)
382 && stack.mStackId != PINNED_STACK_ID) {
383 // The pinned stack is always the top most stack (always-on-top) when it is visible.
384 // So, stack is moved just below the pinned stack.
385 addIndex--;
386 TaskStack topStack = mStacks.get(addIndex);
387 if (topStack.mStackId != PINNED_STACK_ID) {
388 throw new IllegalStateException("Pinned stack isn't top stack??? " + mStacks);
389 }
390 }
391 mStacks.add(addIndex, stack);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800392 }
393
Wale Ogunwalef6192862016-09-10 13:42:30 -0700394 // TODO: Don't forget to switch to WC.removeChild
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700395 void detachChild(TaskStack stack) {
396 detachStack(stack);
397 if (stack.detachFromDisplay()) {
398 mService.mWindowPlacerLocked.requestTraversal();
399 }
400 if (stack.mStackId == DOCKED_STACK_ID) {
401 mService.getDefaultDisplayContentLocked().mDividerControllerLocked
402 .notifyDockedStackExistsChanged(false);
403 }
404 }
405
406 // TODO: See about removing this by untangling the use case in WMS.attachStack()
Craig Mautnerdf88d732014-01-27 09:21:32 -0800407 void detachStack(TaskStack stack) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800408 mDimLayerController.removeDimLayerUser(stack);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800409 mStacks.remove(stack);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800410 }
411
412 /**
413 * Propagate the new bounds to all child stacks.
414 * @param contentRect The bounds to apply at the top level.
415 */
416 void resize(Rect contentRect) {
417 mContentRect.set(contentRect);
418 }
419
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700420 int taskIdFromPoint(int x, int y) {
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800421 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700422 final TaskStack stack = mStacks.get(stackNdx);
423 final int taskId = stack.taskIdFromPoint(x, y);
424 if (taskId != -1) {
425 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -0700426 }
427 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800428 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -0700429 }
430
Chong Zhang8e89b312015-09-09 15:09:30 -0700431 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -0800432 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -0700433 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -0700434 */
Wale Ogunwale15ead902016-09-02 14:30:11 -0700435 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700436 final int delta = mService.dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700437 mTmpTaskForResizePointSearchResult.reset();
Chong Zhang8e89b312015-09-09 15:09:30 -0700438 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
439 TaskStack stack = mStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -0700440 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700441 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700442 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700443
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700444 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
445 if (mTmpTaskForResizePointSearchResult.searchDone) {
446 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -0700447 }
448 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700449 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700450 }
451
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700452 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -0700453 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700454 final int delta = mService.dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700455 mTmpRect2.setEmpty();
Chong Zhangd8ceb852015-11-11 14:53:41 -0800456 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700457 final TaskStack stack = mStacks.get(stackNdx);
458 stack.setTouchExcludeRegion(
459 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -0700460 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800461 // If we removed the focused task above, add it back and only leave its
462 // outside touch area in the exclusion. TapDectector is not interested in
463 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700464 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800465 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
466 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800467 final WindowState inputMethod = mService.mInputMethodWindow;
468 if (inputMethod != null && inputMethod.isVisibleLw()) {
469 // If the input method is visible and the user is typing, we don't want these touch
470 // events to be intercepted and used to change focus. This would likely cause a
471 // disappearance of the input method.
472 inputMethod.getTouchableRegion(mTmpRegion);
473 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
474 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800475 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
476 WindowState win = mTapExcludedWindows.get(i);
477 win.getTouchableRegion(mTmpRegion);
478 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
479 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100480 if (getDockedStackVisibleForUserLocked() != null) {
481 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -0700482 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100483 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
484 }
Craig Mautner1bef3892015-02-17 15:09:47 -0800485 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700486 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -0800487 }
Craig Mautner6601b7b2013-04-29 10:29:11 -0700488 }
489
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800490 void switchUserStacks() {
Craig Mautner858d8a62013-04-23 17:08:34 -0700491 final WindowList windows = getWindowList();
492 for (int i = 0; i < windows.size(); i++) {
493 final WindowState win = windows.get(i);
494 if (win.isHiddenFromUserLocked()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800495 if (DEBUG_VISIBILITY) Slog.w(TAG_WM, "user changing, hiding " + win
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800496 + ", attrs=" + win.mAttrs.type + ", belonging to " + win.mOwnerUid);
Craig Mautner858d8a62013-04-23 17:08:34 -0700497 win.hideLw(false);
498 }
499 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700500
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800501 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800502 mStacks.get(stackNdx).switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -0700503 }
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700504
505 rebuildAppWindowList();
Craig Mautner858d8a62013-04-23 17:08:34 -0700506 }
507
Craig Mautner05d29032013-05-03 13:40:13 -0700508 void resetAnimationBackgroundAnimator() {
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800509 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
510 mStacks.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -0700511 }
512 }
513
514 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800515 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -0700516 }
517
518 void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800519 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700520 }
521
522 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800523 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700524 }
525
526 void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800527 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -0700528 }
529
Craig Mautner2eb15342013-08-07 13:13:35 -0700530 void close() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800531 mDimLayerController.close();
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800532 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
533 mStacks.get(stackNdx).close();
Craig Mautner2eb15342013-08-07 13:13:35 -0700534 }
535 }
536
Craig Mautner95da1082014-02-24 17:54:35 -0800537 boolean isAnimating() {
538 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
539 final TaskStack stack = mStacks.get(stackNdx);
540 if (stack.isAnimating()) {
541 return true;
542 }
543 }
544 return false;
545 }
546
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700547 /** Returns true if a removal action is still being deferred. */
548 boolean checkCompleteDeferredRemoval() {
549 boolean stillDeferringRemoval = false;
Craig Mautner95da1082014-02-24 17:54:35 -0800550 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
551 final TaskStack stack = mStacks.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700552 stillDeferringRemoval |= stack.checkCompleteDeferredRemoval();
Craig Mautner95da1082014-02-24 17:54:35 -0800553 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700554 if (!stillDeferringRemoval && mDeferredRemoval) {
Craig Mautner95da1082014-02-24 17:54:35 -0800555 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700556 return false;
Craig Mautner95da1082014-02-24 17:54:35 -0800557 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700558 return true;
Craig Mautner95da1082014-02-24 17:54:35 -0800559 }
560
Wale Ogunwale94744212015-09-21 19:01:47 -0700561 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
562 final int rotationDelta = DisplayContent.deltaRotation(oldRotation, newRotation);
563 getLogicalDisplayRect(mTmpRect);
564 switch (rotationDelta) {
565 case Surface.ROTATION_0:
566 mTmpRect2.set(bounds);
567 break;
568 case Surface.ROTATION_90:
569 mTmpRect2.top = mTmpRect.bottom - bounds.right;
570 mTmpRect2.left = bounds.top;
571 mTmpRect2.right = mTmpRect2.left + bounds.height();
572 mTmpRect2.bottom = mTmpRect2.top + bounds.width();
573 break;
574 case Surface.ROTATION_180:
575 mTmpRect2.top = mTmpRect.bottom - bounds.bottom;
576 mTmpRect2.left = mTmpRect.right - bounds.right;
577 mTmpRect2.right = mTmpRect2.left + bounds.width();
578 mTmpRect2.bottom = mTmpRect2.top + bounds.height();
579 break;
580 case Surface.ROTATION_270:
581 mTmpRect2.top = bounds.left;
582 mTmpRect2.left = mTmpRect.right - bounds.bottom;
583 mTmpRect2.right = mTmpRect2.left + bounds.height();
584 mTmpRect2.bottom = mTmpRect2.top + bounds.width();
585 break;
586 }
587 bounds.set(mTmpRect2);
588 }
589
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800590 static int deltaRotation(int oldRotation, int newRotation) {
591 int delta = newRotation - oldRotation;
592 if (delta < 0) delta += 4;
593 return delta;
594 }
595
Craig Mautnera91f9e22012-09-14 16:22:08 -0700596 public void dump(String prefix, PrintWriter pw) {
597 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
598 final String subPrefix = " " + prefix;
599 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
600 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
601 pw.print("dpi");
602 if (mInitialDisplayWidth != mBaseDisplayWidth
603 || mInitialDisplayHeight != mBaseDisplayHeight
604 || mInitialDisplayDensity != mBaseDisplayDensity) {
605 pw.print(" base=");
606 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
607 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
608 }
Jeff Brownd46747a2015-04-15 19:02:36 -0700609 if (mDisplayScalingDisabled) {
610 pw.println(" noscale");
611 }
Craig Mautnera91f9e22012-09-14 16:22:08 -0700612 pw.print(" cur=");
613 pw.print(mDisplayInfo.logicalWidth);
614 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
615 pw.print(" app=");
616 pw.print(mDisplayInfo.appWidth);
617 pw.print("x"); pw.print(mDisplayInfo.appHeight);
618 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
619 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
620 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
621 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Craig Mautner95da1082014-02-24 17:54:35 -0800622 pw.print(subPrefix); pw.print("deferred="); pw.print(mDeferredRemoval);
623 pw.print(" layoutNeeded="); pw.println(layoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800624
Craig Mautnerdc548482014-02-05 13:35:24 -0800625 pw.println();
Craig Mautnere8b85fd2014-10-22 09:23:25 -0700626 pw.println(" Application tokens in top down Z order:");
Craig Mautnere8b85fd2014-10-22 09:23:25 -0700627 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnerbcb6eb92015-01-13 13:09:22 -0800628 final TaskStack stack = mStacks.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800629 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700630 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800631
Craig Mautnerdc548482014-02-05 13:35:24 -0800632 pw.println();
633 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700634 pw.println();
635 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800636 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700637 WindowToken token = mExitingTokens.get(i);
638 pw.print(" Exiting #"); pw.print(i);
639 pw.print(' '); pw.print(token);
640 pw.println(':');
641 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800642 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700643 }
Craig Mautner59c00972012-07-30 12:10:24 -0700644 pw.println();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800645 mDimLayerController.dump(prefix + " ", pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -0700646 pw.println();
647 mDividerControllerLocked.dump(prefix + " ", pw);
Craig Mautner59c00972012-07-30 12:10:24 -0700648 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800649
650 @Override
651 public String toString() {
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700652 return getName() + " stacks=" + mStacks;
653 }
654
655 String getName() {
656 return "Display " + mDisplayId + " info=" + mDisplayInfo;
Craig Mautnere0a38842013-12-16 16:14:02 -0800657 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700658
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800659 /**
660 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
661 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700662 TaskStack getDockedStackLocked() {
Wale Ogunwalee45899a2015-10-01 11:30:34 -0700663 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700664 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700665 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -0800666
667 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800668 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
669 * visible, as long as it's not hidden because the current user doesn't have any tasks there.
670 */
671 TaskStack getDockedStackVisibleForUserLocked() {
672 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700673 return (stack != null && stack.isVisible(true /* ignoreKeyguard */)) ? stack : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800674 }
675
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700676 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -0800677 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
678 WindowState touchedWin = null;
679 final int x = (int) xf;
680 final int y = (int) yf;
681
682 for (int i = mWindows.size() - 1; i >= 0; i--) {
683 WindowState window = mWindows.get(i);
684 final int flags = window.mAttrs.flags;
685 if (!window.isVisibleLw()) {
686 continue;
687 }
688 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
689 continue;
690 }
691
692 window.getVisibleBounds(mTmpRect);
693 if (!mTmpRect.contains(x, y)) {
694 continue;
695 }
696
697 window.getTouchableRegion(mTmpRegion);
698
699 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
700 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
701 touchedWin = window;
702 break;
703 }
704 }
705
706 return touchedWin;
707 }
Jorim Jaggi6626f542016-08-22 13:08:44 -0700708
709 /**
710 * See {@link WindowManagerService#overridePlayingAppAnimationsLw}.
711 */
712 void overridePlayingAppAnimationsLw(Animation a) {
713 for (int i = mStacks.size() - 1; i >= 0; i--) {
714 mStacks.get(i).overridePlayingAppAnimations(a);
715 }
716 }
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700717
718 boolean canAddToastWindowForUid(int uid) {
719 // We allow one toast window per UID being shown at a time.
720 WindowList windows = getWindowList();
721 final int windowCount = windows.size();
722 for (int i = 0; i < windowCount; i++) {
723 WindowState window = windows.get(i);
724 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == uid
725 && !window.mPermanentlyHidden && !window.mAnimatingExit) {
726 return false;
727 }
728 }
729 return true;
730 }
731
732 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus,
733 WindowState newFocus) {
734 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
735 return;
736 }
737 final int lostFocusUid = oldFocus.mOwnerUid;
738 WindowList windows = getWindowList();
739 final int windowCount = windows.size();
740 for (int i = 0; i < windowCount; i++) {
741 WindowState window = windows.get(i);
742 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == lostFocusUid) {
743 if (!mService.mH.hasMessages(WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window)) {
744 mService.mH.sendMessageDelayed(
745 mService.mH.obtainMessage(
746 WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window),
747 window.mAttrs.hideTimeoutMilliseconds);
748 }
749 }
750 }
751 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700752
753 WindowState findFocusedWindow() {
754 final AppWindowToken focusedApp = mService.mFocusedApp;
755
756 for (int i = mWindows.size() - 1; i >= 0; i--) {
757 final WindowState win = mWindows.get(i);
758
759 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + i + " = " + win
760 + ", flags=" + win.mAttrs.flags + ", canReceive=" + win.canReceiveKeys());
761
762 if (!win.canReceiveKeys()) {
763 continue;
764 }
765
766 final AppWindowToken wtoken = win.mAppToken;
767
768 // If this window's application has been removed, just skip it.
769 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
770 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
771 + (wtoken.removed ? "removed" : "sendingToBottom"));
772 continue;
773 }
774
775 if (focusedApp == null) {
776 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
777 + " using new focus @ " + i + " = " + win);
778 return win;
779 }
780
781 if (!focusedApp.windowsAreFocusable()) {
782 // Current focused app windows aren't focusable...
783 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
784 + " focusable using new focus @ " + i + " = " + win);
785 return win;
786 }
787
788 // Descend through all of the app tokens and find the first that either matches
789 // win.mAppToken (return win) or mFocusedApp (return null).
790 if (wtoken != null && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
791 final TaskStack focusedAppStack = focusedApp.mTask.mStack;
792 final TaskStack appStack = wtoken.mTask.mStack;
793
794 // TODO: Use WindowContainer.compareTo() once everything is using WindowContainer
Wale Ogunwale14a3fb92016-09-11 15:19:05 -0700795 if ((focusedAppStack == appStack && focusedApp.compareTo(wtoken) > 0)
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700796 || mStacks.indexOf(focusedAppStack) > mStacks.indexOf(appStack)) {
797 // App stack below focused app stack. No focus for you!!!
798 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
799 "findFocusedWindow: Reached focused app=" + focusedApp);
800 return null;
801 }
802 }
803
804 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ "
805 + i + " = " + win);
806 return win;
807 }
808
809 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
810 return null;
811 }
Wale Ogunwaleec731152016-09-08 20:18:57 -0700812
813 int addAppWindowToWindowList(final WindowState win) {
814 final IWindow client = win.mClient;
815
816 WindowList tokenWindowList = getTokenWindowsOnDisplay(win.mToken);
817 if (!tokenWindowList.isEmpty()) {
818 return addAppWindowExisting(win, tokenWindowList);
819 }
820
821 // No windows from this token on this display
822 if (mService.localLOGV) Slog.v(TAG_WM, "Figuring out where to add app window "
823 + client.asBinder() + " (token=" + this + ")");
824
825 final WindowToken wToken = win.mToken;
826
827 // Figure out where the window should go, based on the order of applications.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700828 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwaleec731152016-09-08 20:18:57 -0700829 for (int i = mStacks.size() - 1; i >= 0; --i) {
830 final TaskStack stack = mStacks.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700831 stack.getWindowOnDisplayBeforeToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
832 if (mTmpGetWindowOnDisplaySearchResult.reachedToken) {
Wale Ogunwaleec731152016-09-08 20:18:57 -0700833 // We have reach the token we are interested in. End search.
834 break;
835 }
836 }
837
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700838 WindowState pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700839
840 // We now know the index into the apps. If we found an app window above, that gives us the
841 // position; else we need to look some more.
842 if (pos != null) {
843 // Move behind any windows attached to this one.
844 final WindowToken atoken = mService.mTokenMap.get(pos.mClient.asBinder());
845 if (atoken != null) {
846 tokenWindowList = getTokenWindowsOnDisplay(atoken);
847 final int NC = tokenWindowList.size();
848 if (NC > 0) {
849 WindowState bottom = tokenWindowList.get(0);
850 if (bottom.mSubLayer < 0) {
851 pos = bottom;
852 }
853 }
854 }
855 addWindowToListBefore(win, pos);
856 return 0;
857 }
858
859 // Continue looking down until we find the first token that has windows on this display.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700860 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwaleec731152016-09-08 20:18:57 -0700861 for (int i = mStacks.size() - 1; i >= 0; --i) {
862 final TaskStack stack = mStacks.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700863 stack.getWindowOnDisplayAfterToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
864 if (mTmpGetWindowOnDisplaySearchResult.foundWindow != null) {
Wale Ogunwaleec731152016-09-08 20:18:57 -0700865 // We have found a window after the token. End search.
866 break;
867 }
868 }
869
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700870 pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700871
872 if (pos != null) {
873 // Move in front of any windows attached to this one.
874 final WindowToken atoken = mService.mTokenMap.get(pos.mClient.asBinder());
875 if (atoken != null) {
876 final WindowState top = atoken.getTopWindow();
877 if (top != null && top.mSubLayer >= 0) {
878 pos = top;
879 }
880 }
881 addWindowToListAfter(win, pos);
882 return 0;
883 }
884
885 // Just search for the start of this layer.
886 final int myLayer = win.mBaseLayer;
887 int i;
888 for (i = mWindows.size() - 1; i >= 0; --i) {
889 final WindowState w = mWindows.get(i);
890 // Dock divider shares the base layer with application windows, but we want to always
891 // keep it above the application windows. The sharing of the base layer is intended
892 // for window animations, which need to be above the dock divider for the duration
893 // of the animation.
894 if (w.mBaseLayer <= myLayer && w.mAttrs.type != TYPE_DOCK_DIVIDER) {
895 break;
896 }
897 }
898 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
899 "Based on layer: Adding window " + win + " at " + (i + 1) + " of "
900 + mWindows.size());
901 mWindows.add(i + 1, win);
902 mService.mWindowsChanged = true;
903 return 0;
904 }
905
906 /** Adds this non-app window to the window list. */
907 void addNonAppWindowToWindowList(WindowState win) {
908 // Figure out where window should go, based on layer.
909 int i;
910 for (i = mWindows.size() - 1; i >= 0; i--) {
911 final WindowState otherWin = mWindows.get(i);
912 if (otherWin.getBaseType() != TYPE_WALLPAPER && otherWin.mBaseLayer <= win.mBaseLayer) {
913 // Wallpaper wanders through the window list, for example to position itself
914 // directly behind keyguard. Because of this it will break the ordering based on
915 // WindowState.mBaseLayer. There might windows with higher mBaseLayer behind it and
916 // we don't want the new window to appear above them. An example of this is adding
917 // of the docked stack divider. Consider a scenario with the following ordering (top
918 // to bottom): keyguard, wallpaper, assist preview, apps. We want the dock divider
919 // to land below the assist preview, so the dock divider must ignore the wallpaper,
920 // with which it shares the base layer.
921 break;
922 }
923 }
924
925 i++;
926 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
927 "Free window: Adding window " + this + " at " + i + " of " + mWindows.size());
928 mWindows.add(i, win);
929 mService.mWindowsChanged = true;
930 }
931
932 void addChildWindowToWindowList(WindowState win) {
933 final WindowState parentWindow = win.getParentWindow();
934
935 WindowList windowsOnSameDisplay = getTokenWindowsOnDisplay(win.mToken);
936
937 // Figure out this window's ordering relative to the parent window.
938 final int wCount = windowsOnSameDisplay.size();
939 final int sublayer = win.mSubLayer;
940 int largestSublayer = Integer.MIN_VALUE;
941 WindowState windowWithLargestSublayer = null;
942 int i;
943 for (i = 0; i < wCount; i++) {
944 WindowState w = windowsOnSameDisplay.get(i);
945 final int wSublayer = w.mSubLayer;
946 if (wSublayer >= largestSublayer) {
947 largestSublayer = wSublayer;
948 windowWithLargestSublayer = w;
949 }
950 if (sublayer < 0) {
951 // For negative sublayers, we go below all windows in the same sublayer.
952 if (wSublayer >= sublayer) {
953 addWindowToListBefore(win, wSublayer >= 0 ? parentWindow : w);
954 break;
955 }
956 } else {
957 // For positive sublayers, we go above all windows in the same sublayer.
958 if (wSublayer > sublayer) {
959 addWindowToListBefore(win, w);
960 break;
961 }
962 }
963 }
964 if (i >= wCount) {
965 if (sublayer < 0) {
966 addWindowToListBefore(win, parentWindow);
967 } else {
968 addWindowToListAfter(win,
969 largestSublayer >= 0 ? windowWithLargestSublayer : parentWindow);
970 }
971 }
972 }
973
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700974 /**
975 * Z-orders the display window list so that:
976 * <ul>
977 * <li>Any windows that are currently below the wallpaper window stay below the wallpaper
978 * window.
979 * <li>Exiting application windows are at the bottom, but above the wallpaper window.
980 * <li>All other application windows are above the exiting application windows and ordered based
981 * on the ordering of their stacks and tasks on the display.
982 * <li>Non-application windows are at the very top.
983 * </ul>
984 * <p>
985 * NOTE: This isn't a complete picture of what the user see. Further manipulation of the window
986 * surface layering is done in {@link WindowLayersController}.
987 */
988 void rebuildAppWindowList() {
989 int count = mWindows.size();
990 int i;
991 int lastBelow = -1;
992 int numRemoved = 0;
993
994 if (mRebuildTmp.length < count) {
995 mRebuildTmp = new WindowState[count + 10];
996 }
997
998 // First remove all existing app windows.
999 i = 0;
1000 while (i < count) {
1001 final WindowState w = mWindows.get(i);
1002 if (w.mAppToken != null) {
1003 final WindowState win = mWindows.remove(i);
1004 win.mRebuilding = true;
1005 mRebuildTmp[numRemoved] = win;
1006 mService.mWindowsChanged = true;
1007 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Rebuild removing window: " + win);
1008 count--;
1009 numRemoved++;
1010 continue;
1011 } else if (lastBelow == i-1) {
1012 if (w.mAttrs.type == TYPE_WALLPAPER) {
1013 lastBelow = i;
1014 }
1015 }
1016 i++;
1017 }
1018
1019 // Keep whatever windows were below the app windows still below, by skipping them.
1020 lastBelow++;
1021 i = lastBelow;
1022
1023 // First add all of the exiting app tokens... these are no longer in the main app list,
1024 // but still have windows shown. We put them in the back because now that the animation is
1025 // over we no longer will care about them.
1026 final int numStacks = mStacks.size();
1027 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1028 AppTokenList exitingAppTokens = mStacks.get(stackNdx).mExitingAppTokens;
1029 int NT = exitingAppTokens.size();
1030 for (int j = 0; j < NT; j++) {
1031 i = exitingAppTokens.get(j).rebuildWindowList(this, i);
1032 }
1033 }
1034
1035 // And add in the still active app tokens in Z order.
1036 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1037 i = mStacks.get(stackNdx).rebuildWindowList(this, i);
1038 }
1039
1040 i -= lastBelow;
1041 if (i != numRemoved) {
1042 layoutNeeded = true;
1043 Slog.w(TAG_WM, "On display=" + mDisplayId + " Rebuild removed " + numRemoved
1044 + " windows but added " + i + " rebuildAppWindowListLocked() "
1045 + " callers=" + Debug.getCallers(10));
1046 for (i = 0; i < numRemoved; i++) {
1047 WindowState ws = mRebuildTmp[i];
1048 if (ws.mRebuilding) {
1049 StringWriter sw = new StringWriter();
1050 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1051 ws.dump(pw, "", true);
1052 pw.flush();
1053 Slog.w(TAG_WM, "This window was lost: " + ws);
1054 Slog.w(TAG_WM, sw.toString());
1055 ws.mWinAnimator.destroySurfaceLocked();
1056 }
1057 }
1058 Slog.w(TAG_WM, "Current app token list:");
1059 dumpChildrenNames();
1060 Slog.w(TAG_WM, "Final window list:");
1061 dumpWindows();
1062 }
1063 Arrays.fill(mRebuildTmp, null);
1064 }
1065
Wale Ogunwaleec731152016-09-08 20:18:57 -07001066 /** Return the list of Windows on this display associated with the input token. */
1067 WindowList getTokenWindowsOnDisplay(WindowToken token) {
1068 final WindowList windowList = new WindowList();
1069 final int count = mWindows.size();
1070 for (int i = 0; i < count; i++) {
1071 final WindowState win = mWindows.get(i);
1072 if (win.mToken == token) {
1073 windowList.add(win);
1074 }
1075 }
1076 return windowList;
1077 }
1078
1079 private int addAppWindowExisting(WindowState win, WindowList tokenWindowList) {
1080
1081 int tokenWindowsPos;
1082 // If this application has existing windows, we simply place the new window on top of
1083 // them... but keep the starting window on top.
1084 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
1085 // Base windows go behind everything else.
1086 final WindowState lowestWindow = tokenWindowList.get(0);
1087 addWindowToListBefore(win, lowestWindow);
1088 tokenWindowsPos = win.mToken.getWindowIndex(lowestWindow);
1089 } else {
1090 final AppWindowToken atoken = win.mAppToken;
1091 final int windowListPos = tokenWindowList.size();
1092 final WindowState lastWindow = tokenWindowList.get(windowListPos - 1);
1093 if (atoken != null && lastWindow == atoken.startingWindow) {
1094 addWindowToListBefore(win, lastWindow);
1095 tokenWindowsPos = win.mToken.getWindowIndex(lastWindow);
1096 } else {
1097 int newIdx = findIdxBasedOnAppTokens(win);
1098 // There is a window above this one associated with the same apptoken note that the
1099 // window could be a floating window that was created later or a window at the top
1100 // of the list of windows associated with this token.
1101 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1102 "not Base app: Adding window " + win + " at " + (newIdx + 1) + " of "
1103 + mWindows.size());
1104 mWindows.add(newIdx + 1, win);
1105 if (newIdx < 0) {
1106 // No window from token found on win's display.
1107 tokenWindowsPos = 0;
1108 } else {
1109 tokenWindowsPos = win.mToken.getWindowIndex(mWindows.get(newIdx)) + 1;
1110 }
1111 mService.mWindowsChanged = true;
1112 }
1113 }
1114 return tokenWindowsPos;
1115 }
1116
1117 /** Places the first input window after the second input window in the window list. */
1118 private void addWindowToListAfter(WindowState first, WindowState second) {
1119 final int i = mWindows.indexOf(second);
1120 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1121 "Adding window " + this + " at " + (i + 1) + " of " + mWindows.size()
1122 + " (after " + second + ")");
1123 mWindows.add(i + 1, first);
1124 mService.mWindowsChanged = true;
1125 }
1126
1127 /** Places the first input window before the second input window in the window list. */
1128 private void addWindowToListBefore(WindowState first, WindowState second) {
1129 int i = mWindows.indexOf(second);
1130 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1131 "Adding window " + this + " at " + i + " of " + mWindows.size()
1132 + " (before " + second + ")");
1133 if (i < 0) {
1134 Slog.w(TAG_WM, "addWindowToListBefore: Unable to find " + second + " in " + mWindows);
1135 i = 0;
1136 }
1137 mWindows.add(i, first);
1138 mService.mWindowsChanged = true;
1139 }
1140
1141 /**
1142 * This method finds out the index of a window that has the same app token as win. used for z
1143 * ordering the windows in mWindows
1144 */
1145 private int findIdxBasedOnAppTokens(WindowState win) {
1146 for(int j = mWindows.size() - 1; j >= 0; j--) {
1147 final WindowState wentry = mWindows.get(j);
1148 if(wentry.mAppToken == win.mAppToken) {
1149 return j;
1150 }
1151 }
1152 return -1;
1153 }
1154
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001155 private void dumpChildrenNames() {
1156 StringWriter sw = new StringWriter();
1157 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1158 dumpChildrenNames(pw, " ");
1159 }
1160
1161 private void dumpChildrenNames(PrintWriter pw, String prefix) {
1162 final String childPrefix = prefix + prefix;
1163 for (int j = mStacks.size() - 1; j >= 0; j--) {
1164 final TaskStack stack = mStacks.get(j);
1165 pw.println("#" + j + " " + getName());
1166 stack.dumpChildrenNames(pw, childPrefix);
1167 }
1168 }
1169
1170 private void dumpWindows() {
1171 final int numDisplays = mService.mDisplayContents.size();
1172 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
1173 final DisplayContent displayContent = mService.mDisplayContents.valueAt(displayNdx);
1174 Slog.v(TAG_WM, " Display #" + displayContent.getDisplayId());
1175 final WindowList windows = displayContent.getWindowList();
1176 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
1177 Slog.v(TAG_WM, " #" + winNdx + ": " + windows.get(winNdx));
1178 }
1179 }
1180 }
1181
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001182 static final class GetWindowOnDisplaySearchResult {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001183 boolean reachedToken;
1184 WindowState foundWindow;
1185
1186 void reset() {
1187 reachedToken = false;
1188 foundWindow = null;
1189 }
1190 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001191
1192 static final class TaskForResizePointSearchResult {
1193 boolean searchDone;
1194 Task taskForResize;
1195
1196 void reset() {
1197 searchDone = false;
1198 taskForResize = null;
1199 }
1200 }
Robert Carr3b716242016-08-16 16:02:21 -07001201
1202 void enableSurfaceTrace(FileDescriptor fd) {
1203 for (int i = mWindows.size() - 1; i >= 0; i--) {
1204 final WindowState win = mWindows.get(i);
1205 win.mWinAnimator.enableSurfaceTrace(fd);
1206 }
1207 }
1208
1209 void disableSurfaceTrace() {
1210 for (int i = mWindows.size() - 1; i >= 0; i--) {
1211 final WindowState win = mWindows.get(i);
1212 win.mWinAnimator.disableSurfaceTrace();
1213 }
1214 }
Craig Mautner59c00972012-07-30 12:10:24 -07001215}