blob: fa3112dbc19d76bc9bb0b3d8a3a359b8afeddf6c [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Wale Ogunwale51362492016-09-08 17:49:17 -070020import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.HOME_STACK_ID;
22import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
25import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070026import static android.view.Display.DEFAULT_DISPLAY;
27import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070028import static android.view.Surface.ROTATION_0;
29import static android.view.Surface.ROTATION_180;
30import static android.view.Surface.ROTATION_270;
31import static android.view.Surface.ROTATION_90;
Wale Ogunwale10124582016-09-15 20:25:50 -070032import static android.view.WindowManager.DOCKED_BOTTOM;
33import static android.view.WindowManager.DOCKED_INVALID;
34import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080035import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
36import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
37import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070038import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwaleec731152016-09-08 20:18:57 -070039import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
40import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070041import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070042import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
43import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale10124582016-09-15 20:25:50 -070044import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070045import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
46import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070047import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwaleec731152016-09-08 20:18:57 -070048import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080049import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
Wale Ogunwale51362492016-09-08 17:49:17 -070050import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080051import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070052import static com.android.server.wm.WindowManagerService.dipToPixel;
53import static com.android.server.wm.WindowManagerService.localLOGV;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070054import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -070055
Andrii Kulian3a507b52016-09-19 18:14:12 -070056import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -070057import android.app.ActivityManager.StackId;
Andrii Kulian441e4492016-09-29 15:25:00 -070058import android.content.res.Configuration;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070059import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -080060import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070061import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -070062import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +010063import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070064import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070065import android.os.Debug;
Wale Ogunwale02319a62016-09-26 15:21:22 -070066import android.os.IBinder;
Chong Zhang8e89b312015-09-09 15:09:30 -070067import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -070068import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -070069import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -070070import android.view.DisplayInfo;
Wale Ogunwaleec731152016-09-08 20:18:57 -070071import android.view.IWindow;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070072
Wale Ogunwale9adfe572016-09-08 20:43:58 -070073import com.android.internal.util.FastPrintWriter;
Craig Mautner59c00972012-07-30 12:10:24 -070074
Robert Carr3b716242016-08-16 16:02:21 -070075import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -070076import java.io.PrintWriter;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070077import java.io.StringWriter;
Craig Mautner59c00972012-07-30 12:10:24 -070078import java.util.ArrayList;
Wale Ogunwale9adfe572016-09-08 20:43:58 -070079import java.util.Arrays;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070080import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -070081import java.util.HashMap;
82import java.util.Iterator;
Andrii Kulian3a507b52016-09-19 18:14:12 -070083import java.util.List;
Craig Mautner59c00972012-07-30 12:10:24 -070084
Craig Mautner59c00972012-07-30 12:10:24 -070085/**
86 * Utility class for keeping track of the WindowStates and other pertinent contents of a
87 * particular Display.
88 *
89 * IMPORTANT: No method from this class should ever be used without holding
90 * WindowManagerService.mWindowMap.
91 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -070092class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Craig Mautner59c00972012-07-30 12:10:24 -070093
94 /** Unique identifier of this stack. */
95 private final int mDisplayId;
96
Wale Ogunwale19e452e2016-10-12 12:36:29 -070097 // The display only has 2 child window containers. mTaskStackContainers which contains all
98 // window containers that are related to apps (Activities) and mNonAppWindowContainers which
99 // contains all window containers not related to apps (e.g. Status bar).
100 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
101 private final NonAppWindowContainers mNonAppWindowContainers = new NonAppWindowContainers();
102
Craig Mautner59c00972012-07-30 12:10:24 -0700103 /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element
104 * from mDisplayWindows; */
Craig Mautnerdc548482014-02-05 13:35:24 -0800105 private final WindowList mWindows = new WindowList();
Craig Mautner59c00972012-07-30 12:10:24 -0700106
Wale Ogunwale02319a62016-09-26 15:21:22 -0700107 // Mapping from a token IBinder to a WindowToken object on this display.
108 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
109
Craig Mautner59c00972012-07-30 12:10:24 -0700110 int mInitialDisplayWidth = 0;
111 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700112 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -0700113 int mBaseDisplayWidth = 0;
114 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700115 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700116 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700117 private final DisplayInfo mDisplayInfo = new DisplayInfo();
118 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700119 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Craig Mautner59c00972012-07-30 12:10:24 -0700120
Craig Mautner6601b7b2013-04-29 10:29:11 -0700121 Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700122 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700123
Craig Mautner39834192012-09-02 07:47:24 -0700124 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700125 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700126 int pendingLayoutChanges;
Craig Mautner69b08182012-09-05 13:07:13 -0700127 final boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700128
Craig Mautnerdc548482014-02-05 13:35:24 -0800129 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700130 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800131
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800132 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
133 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700134 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700135
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700136 /** Detect user tapping outside of current focused task bounds .*/
137 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700138
Craig Mautner6601b7b2013-04-29 10:29:11 -0700139 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700140 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700141
Craig Mautner6601b7b2013-04-29 10:29:11 -0700142 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800143 private final Rect mTmpRect = new Rect();
144 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700145 private final RectF mTmpRectF = new RectF();
146 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800147 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700148
Craig Mautner9d808b12013-08-06 18:00:25 -0700149 final WindowManagerService mService;
150
Craig Mautner95da1082014-02-24 17:54:35 -0800151 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700152 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800153
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700154 final DockedStackDividerController mDividerControllerLocked;
155
Chong Zhang112eb8c2015-11-02 11:17:00 -0800156 final DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700157
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800158 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
159
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700160 /** Used when rebuilding window list to keep track of windows that have been removed. */
161 private WindowState[] mRebuildTmp = new WindowState[20];
162
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700163 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
164 new TaskForResizePointSearchResult();
165 private final GetWindowOnDisplaySearchResult mTmpGetWindowOnDisplaySearchResult =
166 new GetWindowOnDisplaySearchResult();
167
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800168 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700169 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800170 * @param service You know.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700171 */
Craig Mautner9d808b12013-08-06 18:00:25 -0700172 DisplayContent(Display display, WindowManagerService service) {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700173 mDisplay = display;
174 mDisplayId = display.getDisplayId();
175 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700176 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700177 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700178 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700179 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800180 mDividerControllerLocked = new DockedStackDividerController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800181 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700182
183 // These are the only direct children we should ever have and they are permanent.
184 super.addChild(mTaskStackContainers, null);
185 super.addChild(mNonAppWindowContainers, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700186 }
187
188 int getDisplayId() {
189 return mDisplayId;
190 }
191
192 WindowList getWindowList() {
193 return mWindows;
194 }
195
Wale Ogunwale02319a62016-09-26 15:21:22 -0700196 WindowToken getWindowToken(IBinder binder) {
197 return mTokenMap.get(binder);
198 }
199
200 AppWindowToken getAppWindowToken(IBinder binder) {
201 final WindowToken token = getWindowToken(binder);
202 if (token == null) {
203 return null;
204 }
205 return token.asAppWindowToken();
206 }
207
208 void setWindowToken(IBinder binder, WindowToken token) {
209 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
210 if (dc != null) {
211 // We currently don't support adding a window token to the display if the display
212 // already has the binder mapped to another token. If there is a use case for supporting
213 // this moving forward we will either need to merge the WindowTokens some how or have
214 // the binder map to a list of window tokens.
215 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
216 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
217 }
218 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700219
220 if (token.asAppWindowToken() == null) {
221 // Add non-app token to container hierarchy on the display. App tokens are added through
222 // the parent container managing them (e.g. Tasks).
223 mNonAppWindowContainers.addChild(token, null);
224 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700225 }
226
227 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700228 final WindowToken token = mTokenMap.remove(binder);
229 if (token != null && token.asAppWindowToken() == null) {
230 mNonAppWindowContainers.removeChild(token);
231 }
232 return token;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700233 }
234
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700235 Display getDisplay() {
236 return mDisplay;
237 }
238
Craig Mautner59c00972012-07-30 12:10:24 -0700239 DisplayInfo getDisplayInfo() {
240 return mDisplayInfo;
241 }
242
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700243 DisplayMetrics getDisplayMetrics() {
244 return mDisplayMetrics;
245 }
246
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100247 DockedStackDividerController getDockedDividerController() {
248 return mDividerControllerLocked;
249 }
250
Jeff Browna506a6e2013-06-04 00:02:38 -0700251 /**
252 * Returns true if the specified UID has access to this display.
253 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700254 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700255 return mDisplay.hasAccess(uid);
256 }
257
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700258 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700259 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -0700260 }
261
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700262 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700263 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800264 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800265 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700266 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700267 }
268
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700269 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700270 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
271 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700272 if (stack.mStackId == stackId) {
273 return stack;
274 }
275 }
276 return null;
277 }
278
Andrii Kulian441e4492016-09-29 15:25:00 -0700279 @Override
280 void onConfigurationChanged(Configuration newParentConfig) {
281 super.onConfigurationChanged(newParentConfig);
282
Andrii Kulian3a507b52016-09-19 18:14:12 -0700283 // The display size information is heavily dependent on the resources in the current
284 // configuration, so we need to reconfigure it every time the configuration changes.
285 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
286 mService.reconfigureDisplayLocked(this);
287
288 getDockedDividerController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700289 }
Andrii Kulian3a507b52016-09-19 18:14:12 -0700290
Andrii Kulian441e4492016-09-29 15:25:00 -0700291 /**
292 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
293 * bounds were updated.
294 */
295 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700296 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
297 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700298 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -0700299 changedStackList.add(stack.mStackId);
300 }
301 }
302 }
303
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700304 void checkAppWindowsReadyToShow() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700305 super.checkAppWindowsReadyToShow(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700306 }
307
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700308 void stepAppWindowsAnimation(long currentTime) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700309 super.stepAppWindowsAnimation(currentTime, mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700310 }
311
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700312 @Override
313 boolean fillsParent() {
314 return true;
315 }
316
317 @Override
318 boolean isVisible() {
319 return true;
320 }
321
322 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700323 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700324 super.onAppTransitionDone();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700325 rebuildAppWindowList();
326 }
327
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700328 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700329 int getOrientation() {
Wale Ogunwale51362492016-09-08 17:49:17 -0700330 if (mService.isStackVisibleLocked(DOCKED_STACK_ID)
331 || mService.isStackVisibleLocked(FREEFORM_WORKSPACE_STACK_ID)) {
332 // Apps and their containers are not allowed to specify an orientation while the docked
333 // or freeform stack is visible...except for the home stack/task if the docked stack is
334 // minimized and it actually set something.
Andrii Kulian8072d112016-09-16 11:11:01 -0700335 if (mHomeStack != null && mHomeStack.isVisible()
336 && mDividerControllerLocked.isMinimizedDock()) {
Wale Ogunwale51362492016-09-08 17:49:17 -0700337 final int orientation = mHomeStack.getOrientation();
338 if (orientation != SCREEN_ORIENTATION_UNSET) {
339 return orientation;
340 }
341 }
342 return SCREEN_ORIENTATION_UNSPECIFIED;
343 }
344
Wale Ogunwale10124582016-09-15 20:25:50 -0700345 final int orientation = super.getOrientation();
Wale Ogunwalea77e1462016-09-28 10:09:46 -0700346 if (orientation != SCREEN_ORIENTATION_UNSET && orientation != SCREEN_ORIENTATION_BEHIND) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700347 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
348 "App is requesting an orientation, return " + orientation);
349 return orientation;
Wale Ogunwale51362492016-09-08 17:49:17 -0700350 }
351
352 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
353 "No app is requesting an orientation, return " + mService.mLastOrientation);
354 // The next app has not been requested to be visible, so we keep the current orientation
355 // to prevent freezing/unfreezing the display too early.
356 return mService.mLastOrientation;
357 }
358
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700359 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700360 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700361 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700362 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
363 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800364 }
Craig Mautner722285e2012-09-07 13:55:58 -0700365 }
366
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700367 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700368 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
369 if (displayManagerInternal != null) {
370 // Bootstrap the default logical display from the display manager.
371 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
372 if (newDisplayInfo != null) {
373 mDisplayInfo.copyFrom(newDisplayInfo);
374 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700375 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700376
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800377 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
378 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
379 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
380 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700381 }
382
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700383 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700384 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800385 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700386 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800387 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
388 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700389 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800390 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700391 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800392 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700393 out.set(left, top, left + width, top + height);
394 }
395
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700396 private void getLogicalDisplayRect(Rect out, int orientation) {
397 getLogicalDisplayRect(out);
398
399 // Rotate the Rect if needed.
400 final int currentRotation = mDisplayInfo.rotation;
401 final int rotationDelta = deltaRotation(currentRotation, orientation);
402 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
403 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
404 mTmpRectF.set(out);
405 mTmpMatrix.mapRect(mTmpRectF);
406 mTmpRectF.round(out);
407 }
408 }
409
Chong Zhangf66db432016-01-13 10:39:51 -0800410 void getContentRect(Rect out) {
411 out.set(mContentRect);
412 }
413
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700414 /** Refer to {@link WindowManagerService#attachStack(int, int, boolean)} */
415 void attachStack(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700416 mTaskStackContainers.attachStack(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800417 }
418
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800419 void moveStack(TaskStack stack, boolean toTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700420 mTaskStackContainers.moveStack(stack, toTop);
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700421 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700422
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700423 @Override
424 protected void addChild(DisplayChildWindowContainer child,
425 Comparator<DisplayChildWindowContainer> comparator) {
426 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
427 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700428
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700429 @Override
430 protected void addChild(DisplayChildWindowContainer child, int index) {
431 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
432 }
433
434 @Override
435 protected void removeChild(DisplayChildWindowContainer child) {
436 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800437 }
438
439 /**
440 * Propagate the new bounds to all child stacks.
441 * @param contentRect The bounds to apply at the top level.
442 */
443 void resize(Rect contentRect) {
444 mContentRect.set(contentRect);
445 }
446
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700447 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700448 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
449 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700450 final int taskId = stack.taskIdFromPoint(x, y);
451 if (taskId != -1) {
452 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -0700453 }
454 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800455 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -0700456 }
457
Chong Zhang8e89b312015-09-09 15:09:30 -0700458 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -0800459 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -0700460 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -0700461 */
Wale Ogunwale15ead902016-09-02 14:30:11 -0700462 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700463 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700464 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700465 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
466 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -0700467 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700468 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700469 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700470
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700471 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
472 if (mTmpTaskForResizePointSearchResult.searchDone) {
473 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -0700474 }
475 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700476 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700477 }
478
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700479 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -0700480 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700481 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700482 mTmpRect2.setEmpty();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700483 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
484 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700485 stack.setTouchExcludeRegion(
486 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -0700487 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800488 // If we removed the focused task above, add it back and only leave its
489 // outside touch area in the exclusion. TapDectector is not interested in
490 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700491 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800492 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
493 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800494 final WindowState inputMethod = mService.mInputMethodWindow;
495 if (inputMethod != null && inputMethod.isVisibleLw()) {
496 // If the input method is visible and the user is typing, we don't want these touch
497 // events to be intercepted and used to change focus. This would likely cause a
498 // disappearance of the input method.
499 inputMethod.getTouchableRegion(mTmpRegion);
500 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
501 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800502 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
503 WindowState win = mTapExcludedWindows.get(i);
504 win.getTouchableRegion(mTmpRegion);
505 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
506 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100507 if (getDockedStackVisibleForUserLocked() != null) {
508 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -0700509 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100510 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
511 }
Craig Mautner1bef3892015-02-17 15:09:47 -0800512 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700513 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -0800514 }
Craig Mautner6601b7b2013-04-29 10:29:11 -0700515 }
516
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700517 void switchUser() {
Craig Mautner858d8a62013-04-23 17:08:34 -0700518 final WindowList windows = getWindowList();
519 for (int i = 0; i < windows.size(); i++) {
520 final WindowState win = windows.get(i);
521 if (win.isHiddenFromUserLocked()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800522 if (DEBUG_VISIBILITY) Slog.w(TAG_WM, "user changing, hiding " + win
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800523 + ", attrs=" + win.mAttrs.type + ", belonging to " + win.mOwnerUid);
Craig Mautner858d8a62013-04-23 17:08:34 -0700524 win.hideLw(false);
525 }
526 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700527
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700528 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
529 mTaskStackContainers.get(stackNdx).switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -0700530 }
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700531
532 rebuildAppWindowList();
Craig Mautner858d8a62013-04-23 17:08:34 -0700533 }
534
Craig Mautner05d29032013-05-03 13:40:13 -0700535 void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700536 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
537 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -0700538 }
539 }
540
541 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800542 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -0700543 }
544
545 void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800546 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700547 }
548
549 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800550 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700551 }
552
553 void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800554 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -0700555 }
556
Wale Ogunwale10124582016-09-15 20:25:50 -0700557 @Override
558 void removeIfPossible() {
559 if (isAnimating()) {
560 mDeferredRemoval = true;
561 return;
Craig Mautner2eb15342013-08-07 13:13:35 -0700562 }
Wale Ogunwale10124582016-09-15 20:25:50 -0700563 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -0700564 }
565
Wale Ogunwale10124582016-09-15 20:25:50 -0700566 @Override
567 void removeImmediately() {
568 super.removeImmediately();
569 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Wale Ogunwale10124582016-09-15 20:25:50 -0700570 mDimLayerController.close();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700571 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700572 mService.unregisterPointerEventListener(mTapDetector);
573 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
Craig Mautner95da1082014-02-24 17:54:35 -0800574 }
Craig Mautner95da1082014-02-24 17:54:35 -0800575 }
576
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700577 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -0700578 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700579 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700580 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
581
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700582 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700583 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -0800584 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700585 return false;
Craig Mautner95da1082014-02-24 17:54:35 -0800586 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700587 return true;
Craig Mautner95da1082014-02-24 17:54:35 -0800588 }
589
Wale Ogunwale10124582016-09-15 20:25:50 -0700590 boolean animateForIme(float interpolatedValue, float animationTarget,
591 float dividerAnimationTarget) {
592 boolean updated = false;
593
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700594 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
595 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700596 if (stack == null || !stack.isAdjustedForIme()) {
597 continue;
598 }
599
600 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
601 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
602 updated = true;
603 } else {
604 mDividerControllerLocked.mLastAnimationProgress =
605 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
606 mDividerControllerLocked.mLastDividerProgress =
607 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
608 updated |= stack.updateAdjustForIme(
609 mDividerControllerLocked.mLastAnimationProgress,
610 mDividerControllerLocked.mLastDividerProgress,
611 false /* force */);
612 }
613 if (interpolatedValue >= 1f) {
614 stack.endImeAdjustAnimation();
615 }
616 }
617
618 return updated;
619 }
620
621 boolean clearImeAdjustAnimation() {
622 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700623 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
624 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700625 if (stack != null && stack.isAdjustedForIme()) {
626 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
627 changed = true;
628 }
629 }
630 return changed;
631 }
632
633 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700634 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
635 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700636 if (stack.isVisible() && stack.isAdjustedForIme()) {
637 stack.beginImeAdjustAnimation();
638 }
639 }
640 }
641
642 void adjustForImeIfNeeded() {
643 final WindowState imeWin = mService.mInputMethodWindow;
644 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
645 && !mDividerControllerLocked.isImeHideRequested();
646 final boolean dockVisible = mService.isStackVisibleLocked(DOCKED_STACK_ID);
647 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
648 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
649 imeTargetStack.getDockSide() : DOCKED_INVALID;
650 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
651 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
652 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
653 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
654 final boolean imeHeightChanged = imeVisible &&
655 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
656
657 // The divider could be adjusted for IME position, or be thinner than usual,
658 // or both. There are three possible cases:
659 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
660 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
661 // - If IME is not visible, divider is not moved and is normal width.
662
663 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700664 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
665 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700666 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
667 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
668 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
669 } else {
670 stack.resetAdjustedForIme(false);
671 }
672 }
673 mDividerControllerLocked.setAdjustedForIme(
674 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
675 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700676 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
677 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700678 stack.resetAdjustedForIme(!dockVisible);
679 }
680 mDividerControllerLocked.setAdjustedForIme(
681 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
682 }
683 }
684
685 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700686 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
687 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -0700688 stack.prepareFreezingTaskBounds();
689 }
690 }
691
Wale Ogunwale94744212015-09-21 19:01:47 -0700692 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700693 getLogicalDisplayRect(mTmpRect, newRotation);
694
695 // Compute a transform matrix to undo the coordinate space transformation,
696 // and present the window at the same physical position it previously occupied.
697 final int deltaRotation = deltaRotation(newRotation, oldRotation);
698 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
699
700 mTmpRectF.set(bounds);
701 mTmpMatrix.mapRect(mTmpRectF);
702 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -0700703 }
704
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800705 static int deltaRotation(int oldRotation, int newRotation) {
706 int delta = newRotation - oldRotation;
707 if (delta < 0) delta += 4;
708 return delta;
709 }
710
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700711 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700712 Matrix outMatrix) {
713 // For rotations without Z-ordering we don't need the target rectangle's position.
714 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
715 displayHeight, outMatrix);
716 }
717
718 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
719 float displayWidth, float displayHeight, Matrix outMatrix) {
720 switch (rotation) {
721 case ROTATION_0:
722 outMatrix.reset();
723 break;
724 case ROTATION_270:
725 outMatrix.setRotate(270, 0, 0);
726 outMatrix.postTranslate(0, displayHeight);
727 outMatrix.postTranslate(rectTop, 0);
728 break;
729 case ROTATION_180:
730 outMatrix.reset();
731 break;
732 case ROTATION_90:
733 outMatrix.setRotate(90, 0, 0);
734 outMatrix.postTranslate(displayWidth, 0);
735 outMatrix.postTranslate(-rectTop, rectLeft);
736 break;
737 }
738 }
739
Craig Mautnera91f9e22012-09-14 16:22:08 -0700740 public void dump(String prefix, PrintWriter pw) {
741 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
742 final String subPrefix = " " + prefix;
743 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
744 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
745 pw.print("dpi");
746 if (mInitialDisplayWidth != mBaseDisplayWidth
747 || mInitialDisplayHeight != mBaseDisplayHeight
748 || mInitialDisplayDensity != mBaseDisplayDensity) {
749 pw.print(" base=");
750 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
751 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
752 }
Jeff Brownd46747a2015-04-15 19:02:36 -0700753 if (mDisplayScalingDisabled) {
754 pw.println(" noscale");
755 }
Craig Mautnera91f9e22012-09-14 16:22:08 -0700756 pw.print(" cur=");
757 pw.print(mDisplayInfo.logicalWidth);
758 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
759 pw.print(" app=");
760 pw.print(mDisplayInfo.appWidth);
761 pw.print("x"); pw.print(mDisplayInfo.appHeight);
762 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
763 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
764 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
765 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700766 pw.println(subPrefix + "deferred=" + mDeferredRemoval
767 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800768
Craig Mautnerdc548482014-02-05 13:35:24 -0800769 pw.println();
Craig Mautnere8b85fd2014-10-22 09:23:25 -0700770 pw.println(" Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700771 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
772 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800773 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700774 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800775
Craig Mautnerdc548482014-02-05 13:35:24 -0800776 pw.println();
777 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700778 pw.println();
779 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800780 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700781 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700782 pw.print(" Exiting #"); pw.print(i);
783 pw.print(' '); pw.print(token);
784 pw.println(':');
785 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800786 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700787 }
Craig Mautner59c00972012-07-30 12:10:24 -0700788 pw.println();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800789 mDimLayerController.dump(prefix + " ", pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -0700790 pw.println();
791 mDividerControllerLocked.dump(prefix + " ", pw);
Craig Mautner59c00972012-07-30 12:10:24 -0700792 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800793
794 @Override
795 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700796 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700797 }
798
799 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700800 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -0800801 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700802
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800803 /**
804 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
805 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700806 TaskStack getDockedStackLocked() {
Wale Ogunwalee45899a2015-10-01 11:30:34 -0700807 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700808 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700809 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -0800810
811 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800812 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
813 * visible, as long as it's not hidden because the current user doesn't have any tasks there.
814 */
815 TaskStack getDockedStackVisibleForUserLocked() {
816 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700817 return (stack != null && stack.isVisible(true /* ignoreKeyguard */)) ? stack : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800818 }
819
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700820 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -0800821 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
822 WindowState touchedWin = null;
823 final int x = (int) xf;
824 final int y = (int) yf;
825
826 for (int i = mWindows.size() - 1; i >= 0; i--) {
827 WindowState window = mWindows.get(i);
828 final int flags = window.mAttrs.flags;
829 if (!window.isVisibleLw()) {
830 continue;
831 }
832 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
833 continue;
834 }
835
836 window.getVisibleBounds(mTmpRect);
837 if (!mTmpRect.contains(x, y)) {
838 continue;
839 }
840
841 window.getTouchableRegion(mTmpRegion);
842
843 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
844 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
845 touchedWin = window;
846 break;
847 }
848 }
849
850 return touchedWin;
851 }
Jorim Jaggi6626f542016-08-22 13:08:44 -0700852
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700853 boolean canAddToastWindowForUid(int uid) {
854 // We allow one toast window per UID being shown at a time.
855 WindowList windows = getWindowList();
856 final int windowCount = windows.size();
857 for (int i = 0; i < windowCount; i++) {
858 WindowState window = windows.get(i);
859 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == uid
Svet Ganov62a40f82016-09-29 00:43:51 -0700860 && !window.mPermanentlyHidden && !window.mAnimatingExit
861 && !window.mRemoveOnExit) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700862 return false;
863 }
864 }
865 return true;
866 }
867
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700868 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -0700869 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
870 return;
871 }
872 final int lostFocusUid = oldFocus.mOwnerUid;
873 WindowList windows = getWindowList();
874 final int windowCount = windows.size();
875 for (int i = 0; i < windowCount; i++) {
876 WindowState window = windows.get(i);
877 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == lostFocusUid) {
878 if (!mService.mH.hasMessages(WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window)) {
879 mService.mH.sendMessageDelayed(
880 mService.mH.obtainMessage(
881 WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window),
882 window.mAttrs.hideTimeoutMilliseconds);
883 }
884 }
885 }
886 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700887
888 WindowState findFocusedWindow() {
889 final AppWindowToken focusedApp = mService.mFocusedApp;
890
891 for (int i = mWindows.size() - 1; i >= 0; i--) {
892 final WindowState win = mWindows.get(i);
893
894 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + i + " = " + win
895 + ", flags=" + win.mAttrs.flags + ", canReceive=" + win.canReceiveKeys());
896
897 if (!win.canReceiveKeys()) {
898 continue;
899 }
900
901 final AppWindowToken wtoken = win.mAppToken;
902
903 // If this window's application has been removed, just skip it.
904 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
905 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
906 + (wtoken.removed ? "removed" : "sendingToBottom"));
907 continue;
908 }
909
910 if (focusedApp == null) {
911 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
912 + " using new focus @ " + i + " = " + win);
913 return win;
914 }
915
916 if (!focusedApp.windowsAreFocusable()) {
917 // Current focused app windows aren't focusable...
918 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
919 + " focusable using new focus @ " + i + " = " + win);
920 return win;
921 }
922
923 // Descend through all of the app tokens and find the first that either matches
924 // win.mAppToken (return win) or mFocusedApp (return null).
925 if (wtoken != null && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700926 if (focusedApp.compareTo(wtoken) > 0) {
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700927 // App stack below focused app stack. No focus for you!!!
928 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
929 "findFocusedWindow: Reached focused app=" + focusedApp);
930 return null;
931 }
932 }
933
934 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ "
935 + i + " = " + win);
936 return win;
937 }
938
939 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
940 return null;
941 }
Wale Ogunwaleec731152016-09-08 20:18:57 -0700942
943 int addAppWindowToWindowList(final WindowState win) {
944 final IWindow client = win.mClient;
945
946 WindowList tokenWindowList = getTokenWindowsOnDisplay(win.mToken);
947 if (!tokenWindowList.isEmpty()) {
948 return addAppWindowExisting(win, tokenWindowList);
949 }
950
951 // No windows from this token on this display
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700952 if (localLOGV) Slog.v(TAG_WM, "Figuring out where to add app window "
Wale Ogunwaleec731152016-09-08 20:18:57 -0700953 + client.asBinder() + " (token=" + this + ")");
954
955 final WindowToken wToken = win.mToken;
956
957 // Figure out where the window should go, based on the order of applications.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700958 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700959 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
960 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700961 stack.getWindowOnDisplayBeforeToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
962 if (mTmpGetWindowOnDisplaySearchResult.reachedToken) {
Wale Ogunwaleec731152016-09-08 20:18:57 -0700963 // We have reach the token we are interested in. End search.
964 break;
965 }
966 }
967
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700968 WindowState pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700969
970 // We now know the index into the apps. If we found an app window above, that gives us the
971 // position; else we need to look some more.
972 if (pos != null) {
973 // Move behind any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -0700974 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -0700975 if (atoken != null) {
976 tokenWindowList = getTokenWindowsOnDisplay(atoken);
977 final int NC = tokenWindowList.size();
978 if (NC > 0) {
979 WindowState bottom = tokenWindowList.get(0);
980 if (bottom.mSubLayer < 0) {
981 pos = bottom;
982 }
983 }
984 }
985 addWindowToListBefore(win, pos);
986 return 0;
987 }
988
989 // Continue looking down until we find the first token that has windows on this display.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700990 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700991 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
992 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700993 stack.getWindowOnDisplayAfterToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
994 if (mTmpGetWindowOnDisplaySearchResult.foundWindow != null) {
Wale Ogunwaleec731152016-09-08 20:18:57 -0700995 // We have found a window after the token. End search.
996 break;
997 }
998 }
999
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001000 pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001001
1002 if (pos != null) {
1003 // Move in front of any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -07001004 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -07001005 if (atoken != null) {
1006 final WindowState top = atoken.getTopWindow();
1007 if (top != null && top.mSubLayer >= 0) {
1008 pos = top;
1009 }
1010 }
1011 addWindowToListAfter(win, pos);
1012 return 0;
1013 }
1014
1015 // Just search for the start of this layer.
1016 final int myLayer = win.mBaseLayer;
1017 int i;
1018 for (i = mWindows.size() - 1; i >= 0; --i) {
1019 final WindowState w = mWindows.get(i);
1020 // Dock divider shares the base layer with application windows, but we want to always
1021 // keep it above the application windows. The sharing of the base layer is intended
1022 // for window animations, which need to be above the dock divider for the duration
1023 // of the animation.
1024 if (w.mBaseLayer <= myLayer && w.mAttrs.type != TYPE_DOCK_DIVIDER) {
1025 break;
1026 }
1027 }
1028 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1029 "Based on layer: Adding window " + win + " at " + (i + 1) + " of "
1030 + mWindows.size());
1031 mWindows.add(i + 1, win);
1032 mService.mWindowsChanged = true;
1033 return 0;
1034 }
1035
1036 /** Adds this non-app window to the window list. */
1037 void addNonAppWindowToWindowList(WindowState win) {
1038 // Figure out where window should go, based on layer.
1039 int i;
1040 for (i = mWindows.size() - 1; i >= 0; i--) {
1041 final WindowState otherWin = mWindows.get(i);
1042 if (otherWin.getBaseType() != TYPE_WALLPAPER && otherWin.mBaseLayer <= win.mBaseLayer) {
1043 // Wallpaper wanders through the window list, for example to position itself
1044 // directly behind keyguard. Because of this it will break the ordering based on
1045 // WindowState.mBaseLayer. There might windows with higher mBaseLayer behind it and
1046 // we don't want the new window to appear above them. An example of this is adding
1047 // of the docked stack divider. Consider a scenario with the following ordering (top
1048 // to bottom): keyguard, wallpaper, assist preview, apps. We want the dock divider
1049 // to land below the assist preview, so the dock divider must ignore the wallpaper,
1050 // with which it shares the base layer.
1051 break;
1052 }
1053 }
1054
1055 i++;
1056 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1057 "Free window: Adding window " + this + " at " + i + " of " + mWindows.size());
1058 mWindows.add(i, win);
1059 mService.mWindowsChanged = true;
1060 }
1061
Wale Ogunwaleb9a07c32016-10-12 14:55:56 -07001062 void addToWindowList(WindowState win, int index) {
1063 mWindows.add(index, win);
1064 }
1065
Wale Ogunwaleec731152016-09-08 20:18:57 -07001066 void addChildWindowToWindowList(WindowState win) {
1067 final WindowState parentWindow = win.getParentWindow();
1068
1069 WindowList windowsOnSameDisplay = getTokenWindowsOnDisplay(win.mToken);
1070
1071 // Figure out this window's ordering relative to the parent window.
1072 final int wCount = windowsOnSameDisplay.size();
1073 final int sublayer = win.mSubLayer;
1074 int largestSublayer = Integer.MIN_VALUE;
1075 WindowState windowWithLargestSublayer = null;
1076 int i;
1077 for (i = 0; i < wCount; i++) {
1078 WindowState w = windowsOnSameDisplay.get(i);
1079 final int wSublayer = w.mSubLayer;
1080 if (wSublayer >= largestSublayer) {
1081 largestSublayer = wSublayer;
1082 windowWithLargestSublayer = w;
1083 }
1084 if (sublayer < 0) {
1085 // For negative sublayers, we go below all windows in the same sublayer.
1086 if (wSublayer >= sublayer) {
1087 addWindowToListBefore(win, wSublayer >= 0 ? parentWindow : w);
1088 break;
1089 }
1090 } else {
1091 // For positive sublayers, we go above all windows in the same sublayer.
1092 if (wSublayer > sublayer) {
1093 addWindowToListBefore(win, w);
1094 break;
1095 }
1096 }
1097 }
1098 if (i >= wCount) {
1099 if (sublayer < 0) {
1100 addWindowToListBefore(win, parentWindow);
1101 } else {
1102 addWindowToListAfter(win,
1103 largestSublayer >= 0 ? windowWithLargestSublayer : parentWindow);
1104 }
1105 }
1106 }
1107
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001108 /**
1109 * Z-orders the display window list so that:
1110 * <ul>
1111 * <li>Any windows that are currently below the wallpaper window stay below the wallpaper
1112 * window.
1113 * <li>Exiting application windows are at the bottom, but above the wallpaper window.
1114 * <li>All other application windows are above the exiting application windows and ordered based
1115 * on the ordering of their stacks and tasks on the display.
1116 * <li>Non-application windows are at the very top.
1117 * </ul>
1118 * <p>
1119 * NOTE: This isn't a complete picture of what the user see. Further manipulation of the window
1120 * surface layering is done in {@link WindowLayersController}.
1121 */
1122 void rebuildAppWindowList() {
1123 int count = mWindows.size();
1124 int i;
1125 int lastBelow = -1;
1126 int numRemoved = 0;
1127
1128 if (mRebuildTmp.length < count) {
1129 mRebuildTmp = new WindowState[count + 10];
1130 }
1131
1132 // First remove all existing app windows.
1133 i = 0;
1134 while (i < count) {
1135 final WindowState w = mWindows.get(i);
1136 if (w.mAppToken != null) {
1137 final WindowState win = mWindows.remove(i);
1138 win.mRebuilding = true;
1139 mRebuildTmp[numRemoved] = win;
1140 mService.mWindowsChanged = true;
1141 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Rebuild removing window: " + win);
1142 count--;
1143 numRemoved++;
1144 continue;
1145 } else if (lastBelow == i-1) {
1146 if (w.mAttrs.type == TYPE_WALLPAPER) {
1147 lastBelow = i;
1148 }
1149 }
1150 i++;
1151 }
1152
1153 // Keep whatever windows were below the app windows still below, by skipping them.
1154 lastBelow++;
1155 i = lastBelow;
1156
1157 // First add all of the exiting app tokens... these are no longer in the main app list,
1158 // but still have windows shown. We put them in the back because now that the animation is
1159 // over we no longer will care about them.
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001160 final int numStacks = mTaskStackContainers.size();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001161 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001162 AppTokenList exitingAppTokens = mTaskStackContainers.get(stackNdx).mExitingAppTokens;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001163 int NT = exitingAppTokens.size();
1164 for (int j = 0; j < NT; j++) {
Wale Ogunwale360a8bc2016-10-10 13:25:26 -07001165 i = exitingAppTokens.get(j).rebuildWindowListUnchecked(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001166 }
1167 }
1168
1169 // And add in the still active app tokens in Z order.
1170 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001171 i = mTaskStackContainers.get(stackNdx).rebuildWindowList(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001172 }
1173
1174 i -= lastBelow;
1175 if (i != numRemoved) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001176 setLayoutNeeded();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001177 Slog.w(TAG_WM, "On display=" + mDisplayId + " Rebuild removed " + numRemoved
1178 + " windows but added " + i + " rebuildAppWindowListLocked() "
1179 + " callers=" + Debug.getCallers(10));
1180 for (i = 0; i < numRemoved; i++) {
1181 WindowState ws = mRebuildTmp[i];
1182 if (ws.mRebuilding) {
1183 StringWriter sw = new StringWriter();
1184 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1185 ws.dump(pw, "", true);
1186 pw.flush();
1187 Slog.w(TAG_WM, "This window was lost: " + ws);
1188 Slog.w(TAG_WM, sw.toString());
1189 ws.mWinAnimator.destroySurfaceLocked();
1190 }
1191 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001192 Slog.w(TAG_WM, "Current window hierarchy:");
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001193 dumpChildrenNames();
1194 Slog.w(TAG_WM, "Final window list:");
1195 dumpWindows();
1196 }
1197 Arrays.fill(mRebuildTmp, null);
1198 }
1199
Wale Ogunwaleec731152016-09-08 20:18:57 -07001200 /** Return the list of Windows on this display associated with the input token. */
1201 WindowList getTokenWindowsOnDisplay(WindowToken token) {
1202 final WindowList windowList = new WindowList();
1203 final int count = mWindows.size();
1204 for (int i = 0; i < count; i++) {
1205 final WindowState win = mWindows.get(i);
1206 if (win.mToken == token) {
1207 windowList.add(win);
1208 }
1209 }
1210 return windowList;
1211 }
1212
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001213 void setLayoutNeeded() {
1214 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
1215 mLayoutNeeded = true;
1216 }
1217
1218 void clearLayoutNeeded() {
1219 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
1220 mLayoutNeeded = false;
1221 }
1222
1223 boolean isLayoutNeeded() {
1224 return mLayoutNeeded;
1225 }
1226
Wale Ogunwaleec731152016-09-08 20:18:57 -07001227 private int addAppWindowExisting(WindowState win, WindowList tokenWindowList) {
1228
1229 int tokenWindowsPos;
1230 // If this application has existing windows, we simply place the new window on top of
1231 // them... but keep the starting window on top.
1232 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
1233 // Base windows go behind everything else.
1234 final WindowState lowestWindow = tokenWindowList.get(0);
1235 addWindowToListBefore(win, lowestWindow);
1236 tokenWindowsPos = win.mToken.getWindowIndex(lowestWindow);
1237 } else {
1238 final AppWindowToken atoken = win.mAppToken;
1239 final int windowListPos = tokenWindowList.size();
1240 final WindowState lastWindow = tokenWindowList.get(windowListPos - 1);
1241 if (atoken != null && lastWindow == atoken.startingWindow) {
1242 addWindowToListBefore(win, lastWindow);
1243 tokenWindowsPos = win.mToken.getWindowIndex(lastWindow);
1244 } else {
1245 int newIdx = findIdxBasedOnAppTokens(win);
1246 // There is a window above this one associated with the same apptoken note that the
1247 // window could be a floating window that was created later or a window at the top
1248 // of the list of windows associated with this token.
1249 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1250 "not Base app: Adding window " + win + " at " + (newIdx + 1) + " of "
1251 + mWindows.size());
1252 mWindows.add(newIdx + 1, win);
1253 if (newIdx < 0) {
1254 // No window from token found on win's display.
1255 tokenWindowsPos = 0;
1256 } else {
1257 tokenWindowsPos = win.mToken.getWindowIndex(mWindows.get(newIdx)) + 1;
1258 }
1259 mService.mWindowsChanged = true;
1260 }
1261 }
1262 return tokenWindowsPos;
1263 }
1264
1265 /** Places the first input window after the second input window in the window list. */
1266 private void addWindowToListAfter(WindowState first, WindowState second) {
1267 final int i = mWindows.indexOf(second);
1268 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1269 "Adding window " + this + " at " + (i + 1) + " of " + mWindows.size()
1270 + " (after " + second + ")");
1271 mWindows.add(i + 1, first);
1272 mService.mWindowsChanged = true;
1273 }
1274
1275 /** Places the first input window before the second input window in the window list. */
1276 private void addWindowToListBefore(WindowState first, WindowState second) {
1277 int i = mWindows.indexOf(second);
1278 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1279 "Adding window " + this + " at " + i + " of " + mWindows.size()
1280 + " (before " + second + ")");
1281 if (i < 0) {
1282 Slog.w(TAG_WM, "addWindowToListBefore: Unable to find " + second + " in " + mWindows);
1283 i = 0;
1284 }
1285 mWindows.add(i, first);
1286 mService.mWindowsChanged = true;
1287 }
1288
1289 /**
1290 * This method finds out the index of a window that has the same app token as win. used for z
1291 * ordering the windows in mWindows
1292 */
1293 private int findIdxBasedOnAppTokens(WindowState win) {
1294 for(int j = mWindows.size() - 1; j >= 0; j--) {
1295 final WindowState wentry = mWindows.get(j);
1296 if(wentry.mAppToken == win.mAppToken) {
1297 return j;
1298 }
1299 }
1300 return -1;
1301 }
1302
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001303 private void dumpChildrenNames() {
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001304 StringBuilder output = new StringBuilder();
1305 dumpChildrenNames(output, " ");
1306 Slog.v(TAG_WM, output.toString());
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001307 }
1308
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001309 private void dumpWindows() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001310 Slog.v(TAG_WM, " Display #" + mDisplayId);
1311 final WindowList windows = getWindowList();
1312 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
1313 Slog.v(TAG_WM, " #" + winNdx + ": " + windows.get(winNdx));
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001314 }
1315 }
1316
Wale Ogunwale02319a62016-09-26 15:21:22 -07001317 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1318 if (mTokenMap.isEmpty()) {
1319 return;
1320 }
1321 pw.println(" Display #" + mDisplayId);
1322 final Iterator<WindowToken> it = mTokenMap.values().iterator();
1323 while (it.hasNext()) {
1324 final WindowToken token = it.next();
1325 pw.print(" ");
1326 pw.print(token);
1327 if (dumpAll) {
1328 pw.println(':');
1329 token.dump(pw, " ");
1330 } else {
1331 pw.println();
1332 }
1333 }
1334 }
1335
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001336 void enableSurfaceTrace(FileDescriptor fd) {
1337 for (int i = mWindows.size() - 1; i >= 0; i--) {
1338 final WindowState win = mWindows.get(i);
1339 win.mWinAnimator.enableSurfaceTrace(fd);
1340 }
1341 }
1342
1343 void disableSurfaceTrace() {
1344 for (int i = mWindows.size() - 1; i >= 0; i--) {
1345 final WindowState win = mWindows.get(i);
1346 win.mWinAnimator.disableSurfaceTrace();
1347 }
1348 }
1349
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001350 static final class GetWindowOnDisplaySearchResult {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001351 boolean reachedToken;
1352 WindowState foundWindow;
1353
1354 void reset() {
1355 reachedToken = false;
1356 foundWindow = null;
1357 }
1358 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001359
1360 static final class TaskForResizePointSearchResult {
1361 boolean searchDone;
1362 Task taskForResize;
1363
1364 void reset() {
1365 searchDone = false;
1366 taskForResize = null;
1367 }
1368 }
Robert Carr3b716242016-08-16 16:02:21 -07001369
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001370 /**
1371 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
1372 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
1373 * homogeneous children type which is currently required by sub-classes of
1374 * {@link WindowContainer} class.
1375 */
1376 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
1377
1378 int size() {
1379 return mChildren.size();
1380 }
1381
1382 E get(int index) {
1383 return mChildren.get(index);
1384 }
1385
1386 @Override
1387 boolean fillsParent() {
1388 return true;
1389 }
1390
1391 @Override
1392 boolean isVisible() {
1393 return true;
Robert Carr3b716242016-08-16 16:02:21 -07001394 }
1395 }
1396
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001397 /**
1398 * Window container class that contains all containers on this display relating to Apps.
1399 * I.e Activities.
1400 */
1401 private class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
1402
1403 void attachStack(TaskStack stack, boolean onTop) {
1404 if (stack.mStackId == HOME_STACK_ID) {
1405 if (mHomeStack != null) {
1406 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
1407 }
1408 mHomeStack = stack;
1409 }
1410 addChild(stack, onTop);
1411 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07001412 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001413
1414 void moveStack(TaskStack stack, boolean toTop) {
1415 if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) {
1416 // This stack is always-on-top silly...
1417 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom");
1418 return;
1419 }
1420
1421 if (!mChildren.contains(stack)) {
1422 Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable());
1423 }
1424 removeChild(stack);
1425 addChild(stack, toTop);
1426 }
1427
1428 private void addChild(TaskStack stack, boolean toTop) {
1429 int addIndex = toTop ? mChildren.size() : 0;
1430
1431 if (toTop
1432 && mService.isStackVisibleLocked(PINNED_STACK_ID)
1433 && stack.mStackId != PINNED_STACK_ID) {
1434 // The pinned stack is always the top most stack (always-on-top) when it is visible.
1435 // So, stack is moved just below the pinned stack.
1436 addIndex--;
1437 TaskStack topStack = mChildren.get(addIndex);
1438 if (topStack.mStackId != PINNED_STACK_ID) {
1439 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
1440 }
1441 }
1442 addChild(stack, addIndex);
1443 setLayoutNeeded();
1444 }
1445
1446 }
1447
1448 /**
1449 * Window container class that contains all containers on this display that are not related to
1450 * Apps. E.g. status bar.
1451 */
1452 private static class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
1453
Robert Carr3b716242016-08-16 16:02:21 -07001454 }
Craig Mautner59c00972012-07-30 12:10:24 -07001455}