blob: 71d616d900d1caf439a9583067d9305bbc33d333 [file] [log] [blame]
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001/*
2 * Copyright (C) 2013 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;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Chong Zhangb15758a2015-11-17 12:12:03 -080022import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070023import static android.app.ActivityManager.RESIZE_MODE_SYSTEM_SCREEN_ROTATION;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080024import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
25import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_RESIZE;
26import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070027import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Chong Zhangc806d902015-11-30 09:44:27 -080028import static com.android.server.wm.WindowManagerService.H.SHOW_NON_RESIZEABLE_DOCK_TOAST;
29import static android.view.WindowManager.DOCKED_INVALID;
30import static android.view.WindowManager.DOCKED_LEFT;
31import static android.view.WindowManager.DOCKED_RIGHT;
32import static android.view.WindowManager.DOCKED_TOP;
Wale Ogunwale99db1862015-10-23 20:08:22 -070033
Wale Ogunwale3797c222015-10-27 14:21:58 -070034import android.app.ActivityManager.StackId;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070035import android.content.res.Configuration;
36import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080037import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080038import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070039import android.view.DisplayInfo;
40import android.view.Surface;
41
Craig Mautnere3119b72015-01-20 15:02:36 -080042import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080043
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070044import java.io.PrintWriter;
45import java.util.ArrayList;
46
47class Task implements DimLayer.DimLayerUser {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070048 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
49 static final int BOUNDS_CHANGE_NONE = 0;
50 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
51 static final int BOUNDS_CHANGE_POSITION = 1;
52 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
53 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
54
Craig Mautnerc00204b2013-03-05 15:02:14 -080055 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080056 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080057 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070058 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080059 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080060 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080061
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070062 // Content limits relative to the DisplayContent this sits in.
63 private Rect mBounds = new Rect();
Jorim Jaggi0429f352015-12-22 16:29:16 +010064 final Rect mPreparedFrozenBounds = new Rect();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070065
Chong Zhangf66db432016-01-13 10:39:51 -080066 private Rect mPreScrollBounds = new Rect();
67 private boolean mScrollValid;
68
Jorim Jaggidc249c42015-12-15 14:57:31 -080069 // Bounds used to calculate the insets.
70 private final Rect mTempInsetBounds = new Rect();
71
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070072 // Device rotation as of the last time {@link #mBounds} was set.
73 int mRotation;
74
75 // Whether mBounds is fullscreen
76 private boolean mFullscreen = true;
77
78 // Contains configurations settings that are different from the global configuration due to
79 // stack specific operations. E.g. {@link #setBounds}.
80 Configuration mOverrideConfig;
81
82 // For comparison with DisplayContent bounds.
83 private Rect mTmpRect = new Rect();
84 // For handling display rotations.
85 private Rect mTmpRect2 = new Rect();
86
Chong Zhangb15758a2015-11-17 12:12:03 -080087 // Whether the task is resizeable
88 private boolean mResizeable;
89
Chong Zhangc806d902015-11-30 09:44:27 -080090 // Whether we need to show toast about the app being non-resizeable when it becomes visible.
91 // This flag is set when a non-resizeable task is docked (or side-by-side). It's cleared
92 // after we show the toast.
93 private boolean mShowNonResizeableDockToast;
94
Chong Zhang3005e752015-09-18 18:46:28 -070095 // Whether the task is currently being drag-resized
96 private boolean mDragResizing;
97
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070098 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
99 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -0800100 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800101 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -0700102 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -0800103 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700104 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800105 }
106
107 DisplayContent getDisplayContent() {
108 return mStack.getDisplayContent();
109 }
110
Chong Zhangc806d902015-11-30 09:44:27 -0800111 void setShowNonResizeableDockToast() {
112 mShowNonResizeableDockToast = true;
113 }
114
115 void scheduleShowNonResizeableDockToastIfNeeded() {
116 if (!mShowNonResizeableDockToast) {
117 return;
118 }
119 final DisplayContent displayContent = mStack.getDisplayContent();
120 // If docked stack is not yet visible, we don't want to show the toast yet,
121 // since we need the visible rect of the docked task to position the toast.
122 if (displayContent == null || displayContent.getDockedStackLocked() == null) {
123 return;
124 }
125
126 mShowNonResizeableDockToast = false;
127
128 final int dockSide = mStack.getDockSide();
129 int xOffset = 0;
130 int yOffset = 0;
131 if (dockSide != DOCKED_INVALID) {
132 mStack.getBounds(mTmpRect);
Chong Zhangc806d902015-11-30 09:44:27 -0800133
134 if (dockSide == DOCKED_LEFT || dockSide == DOCKED_RIGHT) {
Chong Zhangf66db432016-01-13 10:39:51 -0800135 // The toast was originally placed at the bottom and centered. To place it
136 // at the bottom-center of the stack, we offset it horizontally by the diff
137 // between the center of the stack bounds vs. the center of the screen.
138 displayContent.getLogicalDisplayRect(mTmpRect2);
Chong Zhangc806d902015-11-30 09:44:27 -0800139 xOffset = mTmpRect.centerX() - mTmpRect2.centerX();
140 } else if (dockSide == DOCKED_TOP) {
Chong Zhangf66db432016-01-13 10:39:51 -0800141 // The toast was originally placed at the bottom and centered. To place it
142 // at the bottom center of the top stack, we offset it vertically by the diff
143 // between the bottom of the stack bounds vs. the bottom of the content rect.
144 //
145 // Note here we use the content rect instead of the display rect, as we want
146 // the toast's distance to the dock divider (when it's placed at the top half)
147 // to be the same as it's distance to the top of the navigation bar (when it's
148 // placed at the bottom).
149
Chong Zhangc806d902015-11-30 09:44:27 -0800150 // We don't adjust for DOCKED_BOTTOM case since it's already at the bottom.
Chong Zhangf66db432016-01-13 10:39:51 -0800151 displayContent.getContentRect(mTmpRect2);
Chong Zhangc806d902015-11-30 09:44:27 -0800152 yOffset = mTmpRect2.bottom - mTmpRect.bottom;
153 }
154 mService.mH.obtainMessage(
155 SHOW_NON_RESIZEABLE_DOCK_TOAST, xOffset, yOffset).sendToTarget();
156 }
157 }
158
Craig Mautnerc00204b2013-03-05 15:02:14 -0800159 void addAppToken(int addPos, AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800160 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800161 if (addPos >= lastPos) {
162 addPos = lastPos;
163 } else {
164 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
165 if (mAppTokens.get(pos).removed) {
166 // addPos assumes removed tokens are actually gone.
167 ++addPos;
168 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700169 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800170 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800171 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800172 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800173 mDeferRemoval = false;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800174 }
175
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800176 private boolean hasAppTokensAlive() {
177 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
178 if (!mAppTokens.get(i).appDied) {
179 return true;
180 }
181 }
182 return false;
183 }
184
Craig Mautnere3119b72015-01-20 15:02:36 -0800185 void removeLocked() {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800186 if (hasAppTokensAlive() && mStack.isAnimating()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800187 if (DEBUG_STACK) Slog.i(TAG_WM, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800188 mDeferRemoval = true;
189 return;
190 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800191 if (DEBUG_STACK) Slog.i(TAG_WM, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800192 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800193 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700194 DisplayContent content = getDisplayContent();
195 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800196 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700197 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800198 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800199 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800200 }
201
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800202 void moveTaskToStack(TaskStack stack, boolean toTop) {
203 if (stack == mStack) {
204 return;
205 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800206 if (DEBUG_STACK) Slog.i(TAG_WM, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800207 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700208 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800209 if (mStack != null) {
210 mStack.removeTask(this);
211 }
212 stack.addTask(this, toTop);
213 }
214
Wale Ogunwale935e5022015-11-10 12:36:10 -0800215 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700216 if (mStack != null && stack != mStack) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800217 if (DEBUG_STACK) Slog.i(TAG_WM, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700218 + " from stack=" + mStack);
219 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
220 mStack.removeTask(this);
221 }
222 stack.positionTask(this, position, showForAllUsers());
Wale Ogunwale935e5022015-11-10 12:36:10 -0800223 setBounds(bounds, config);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700224 }
225
Craig Mautnerc00204b2013-03-05 15:02:14 -0800226 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800227 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800228 if (mAppTokens.size() == 0) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100229 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800230 if (mDeferRemoval) {
231 removeLocked();
232 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800233 }
Craig Mautner83162a92015-01-26 14:43:30 -0800234 wtoken.mTask = null;
235 /* Leave mTaskId for now, it might be useful for debug
236 wtoken.mTaskId = -1;
237 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800238 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800239 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800240
Craig Mautnercbd84af2014-10-22 13:21:22 -0700241 void setSendingToBottom(boolean toBottom) {
242 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
243 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
244 }
245 }
246
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700247 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700248 int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700249 if (config == null) {
250 config = Configuration.EMPTY;
251 }
252 if (bounds == null && !Configuration.EMPTY.equals(config)) {
253 throw new IllegalArgumentException("null bounds but non empty configuration: "
254 + config);
255 }
256 if (bounds != null && Configuration.EMPTY.equals(config)) {
257 throw new IllegalArgumentException("non null bounds, but empty configuration");
258 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700259 boolean oldFullscreen = mFullscreen;
260 int rotation = Surface.ROTATION_0;
261 final DisplayContent displayContent = mStack.getDisplayContent();
262 if (displayContent != null) {
263 displayContent.getLogicalDisplayRect(mTmpRect);
264 rotation = displayContent.getDisplayInfo().rotation;
265 if (bounds == null) {
266 bounds = mTmpRect;
267 mFullscreen = true;
268 } else {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700269 mFullscreen = mTmpRect.equals(bounds);
270 }
271 }
272
273 if (bounds == null) {
274 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700275 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700276 }
Chong Zhangf66db432016-01-13 10:39:51 -0800277 if (mPreScrollBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700278 return BOUNDS_CHANGE_NONE;
279 }
280
281 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhangf66db432016-01-13 10:39:51 -0800282 if (mPreScrollBounds.left != bounds.left || mPreScrollBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700283 boundsChange |= BOUNDS_CHANGE_POSITION;
284 }
Chong Zhangf66db432016-01-13 10:39:51 -0800285 if (mPreScrollBounds.width() != bounds.width() || mPreScrollBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700286 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700287 }
288
Chong Zhangf66db432016-01-13 10:39:51 -0800289
290 mPreScrollBounds.set(bounds);
291
292 resetScrollLocked();
293
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700294 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700295 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800296 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700297 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700298 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700299 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700300 }
301
Jorim Jaggidc249c42015-12-15 14:57:31 -0800302 /**
303 * Sets the bounds used to calculate the insets. See
304 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
305 */
306 void setTempInsetBounds(Rect tempInsetBounds) {
307 if (tempInsetBounds != null) {
308 mTempInsetBounds.set(tempInsetBounds);
309 } else {
310 mTempInsetBounds.setEmpty();
311 }
312 }
313
314 /**
315 * Gets the bounds used to calculate the insets. See
316 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
317 */
318 void getTempInsetBounds(Rect out) {
319 out.set(mTempInsetBounds);
320 }
321
Chong Zhangb15758a2015-11-17 12:12:03 -0800322 void setResizeable(boolean resizeable) {
323 mResizeable = resizeable;
324 }
325
326 boolean isResizeable() {
327 return mResizeable;
328 }
329
Chong Zhang87b21722015-09-21 15:39:51 -0700330 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700331 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700332 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700333 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700334 }
335 if (boundsChanged == BOUNDS_CHANGE_NONE) {
336 return false;
337 }
338 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
339 resizeWindows();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800340 } else {
341 moveWindows();
Chong Zhang3005e752015-09-18 18:46:28 -0700342 }
343 return true;
344 }
345
Jorim Jaggi0429f352015-12-22 16:29:16 +0100346 /**
347 * Prepares the task bounds to be frozen with the current size. See
348 * {@link AppWindowToken#freezeBounds}.
349 */
350 void prepareFreezingBounds() {
351 mPreparedFrozenBounds.set(mBounds);
352 }
353
Chong Zhangf66db432016-01-13 10:39:51 -0800354 void resetScrollLocked() {
355 if (mScrollValid) {
356 mScrollValid = false;
357 applyScrollToAllWindows(0, 0);
358 }
359 mBounds.set(mPreScrollBounds);
360 }
361
362 void applyScrollToAllWindows(final int xOffset, final int yOffset) {
363 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
364 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
365 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
366 final WindowState win = windows.get(winNdx);
367 win.mXOffset = xOffset;
368 win.mYOffset = yOffset;
369 }
370 }
371 }
372
373 void applyScrollToWindowIfNeeded(final WindowState win) {
374 if (mScrollValid) {
375 win.mXOffset = mBounds.left;
376 win.mYOffset = mBounds.top;
377 }
378 }
379
Chong Zhangb15758a2015-11-17 12:12:03 -0800380 boolean scrollLocked(Rect bounds) {
381 // shift the task bound if it doesn't fully cover the stack area
382 mStack.getDimBounds(mTmpRect);
383 if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) {
384 if (bounds.left > mTmpRect.left) {
385 bounds.left = mTmpRect.left;
386 bounds.right = mTmpRect.left + mBounds.width();
387 } else if (bounds.right < mTmpRect.right) {
388 bounds.left = mTmpRect.right - mBounds.width();
389 bounds.right = mTmpRect.right;
390 }
391 } else {
392 if (bounds.top > mTmpRect.top) {
393 bounds.top = mTmpRect.top;
394 bounds.bottom = mTmpRect.top + mBounds.height();
395 } else if (bounds.bottom < mTmpRect.bottom) {
396 bounds.top = mTmpRect.bottom - mBounds.height();
397 bounds.bottom = mTmpRect.bottom;
398 }
399 }
400
Chong Zhangf66db432016-01-13 10:39:51 -0800401 // We can stop here if we're already scrolling and the scrolled bounds not changed.
402 if (mScrollValid && bounds.equals(mBounds)) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800403 return false;
404 }
Chong Zhangf66db432016-01-13 10:39:51 -0800405
Chong Zhangb15758a2015-11-17 12:12:03 -0800406 // Normal setBounds() does not allow non-null bounds for fullscreen apps.
407 // We only change bounds for the scrolling case without change it size,
408 // on resizing path we should still want the validation.
409 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800410 mScrollValid = true;
411 applyScrollToAllWindows(bounds.left, bounds.top);
Chong Zhangb15758a2015-11-17 12:12:03 -0800412 return true;
413 }
414
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700415 /** Return true if the current bound can get outputted to the rest of the system as-is. */
416 private boolean useCurrentBounds() {
417 final DisplayContent displayContent = mStack.getDisplayContent();
418 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700419 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700420 || displayContent == null
421 || displayContent.getDockedStackLocked() != null) {
422 return true;
423 }
424 return false;
425 }
426
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800427 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700428 public void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700429 if (useCurrentBounds()) {
430 // No need to adjust the output bounds if fullscreen or the docked stack is visible
431 // since it is already what we want to represent to the rest of the system.
432 out.set(mBounds);
433 return;
434 }
435
436 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
437 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
438 // system.
439 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700440 }
441
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800442 /**
443 * Calculate the maximum visible area of this task. If the task has only one app,
444 * the result will be visible frame of that app. If the task has more than one apps,
445 * we search from top down if the next app got different visible area.
446 *
447 * This effort is to handle the case where some task (eg. GMail composer) might pop up
448 * a dialog that's different in size from the activity below, in which case we should
449 * be dimming the entire task area behind the dialog.
450 *
451 * @param out Rect containing the max visible bounds.
452 * @return true if the task has some visible app windows; false otherwise.
453 */
454 boolean getMaxVisibleBounds(Rect out) {
455 boolean foundTop = false;
456 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800457 final AppWindowToken token = mAppTokens.get(i);
458 // skip hidden (or about to hide) apps
459 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
460 continue;
461 }
462 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800463 if (win == null) {
464 continue;
465 }
466 if (!foundTop) {
467 out.set(win.mVisibleFrame);
468 foundTop = true;
469 continue;
470 }
471 if (win.mVisibleFrame.left < out.left) {
472 out.left = win.mVisibleFrame.left;
473 }
474 if (win.mVisibleFrame.top < out.top) {
475 out.top = win.mVisibleFrame.top;
476 }
477 if (win.mVisibleFrame.right > out.right) {
478 out.right = win.mVisibleFrame.right;
479 }
480 if (win.mVisibleFrame.bottom > out.bottom) {
481 out.bottom = win.mVisibleFrame.bottom;
482 }
483 }
484 return foundTop;
485 }
486
487 /** Bounds of the task to be used for dimming, as well as touch related tests. */
488 @Override
489 public void getDimBounds(Rect out) {
490 if (useCurrentBounds()) {
491 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
492 return;
493 }
494
495 out.set(mBounds);
496 return;
497 }
498
499 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
500 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
501 // system.
502 mStack.getDisplayContent().getLogicalDisplayRect(out);
503 }
504
Chong Zhang3005e752015-09-18 18:46:28 -0700505 void setDragResizing(boolean dragResizing) {
Chong Zhang3005e752015-09-18 18:46:28 -0700506 mDragResizing = dragResizing;
507 }
508
509 boolean isDragResizing() {
510 return mDragResizing;
511 }
512
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700513 void updateDisplayInfo(final DisplayContent displayContent) {
514 if (displayContent == null) {
515 return;
516 }
517 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700518 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700519 return;
520 }
521 final int newRotation = displayContent.getDisplayInfo().rotation;
522 if (mRotation == newRotation) {
523 return;
524 }
525
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800526 // Device rotation changed.
527 // - Reset the bounds to the pre-scroll bounds as whatever scrolling was done is no longer
528 // valid.
529 // - Rotate the bounds and notify activity manager if the task can be resized independently
530 // from its stack. The stack will take care of task rotation for the other case.
Chong Zhangf66db432016-01-13 10:39:51 -0800531 mTmpRect2.set(mPreScrollBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800532
533 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
534 setBounds(mTmpRect2, mOverrideConfig);
535 return;
536 }
537
Wale Ogunwale94744212015-09-21 19:01:47 -0700538 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700539 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800540 // Post message to inform activity manager of the bounds change simulating a one-way
541 // call. We do this to prevent a deadlock between window manager lock and activity
542 // manager lock been held.
543 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
544 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mPreScrollBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700545 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700546 }
547
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700548 void resizeWindows() {
549 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
550 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
551 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
552 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
553 final WindowState win = windows.get(winNdx);
554 if (!resizingWindows.contains(win)) {
Chong Zhangbd0d9372015-12-28 15:18:29 -0800555 if (DEBUG_RESIZE) Slog.d(TAG_WM, "resizeWindows: Resizing " + win);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700556 resizingWindows.add(win);
557 }
558 }
559 }
560 }
561
Chong Zhangbd0d9372015-12-28 15:18:29 -0800562 void moveWindows() {
563 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
564 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
565 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
566 final WindowState win = windows.get(winNdx);
567 if (DEBUG_RESIZE) Slog.d(TAG_WM, "moveWindows: Moving " + win);
568 win.mMovedByResize = true;
569 }
570 }
571 }
572
Winsonc28098f2015-10-30 14:50:19 -0700573 /**
574 * Cancels any running app transitions associated with the task.
575 */
576 void cancelTaskWindowTransition() {
577 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
578 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
579 }
580 }
581
Winson13d30662015-11-06 15:30:29 -0800582 /**
583 * Cancels any running thumbnail transitions associated with the task.
584 */
585 void cancelTaskThumbnailTransition() {
586 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
587 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
588 }
589 }
590
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700591 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700592 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700593 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700594 }
595
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700596 boolean inHomeStack() {
597 return mStack != null && mStack.mStackId == HOME_STACK_ID;
598 }
599
Chong Zhang09b21ef2015-09-14 10:20:21 -0700600 boolean inFreeformWorkspace() {
601 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
602 }
603
Filip Gruszczynski955b2fc2015-10-15 14:46:07 -0700604 boolean inDockedWorkspace() {
605 return mStack != null && mStack.mStackId == DOCKED_STACK_ID;
606 }
607
Chong Zhangb15758a2015-11-17 12:12:03 -0800608 boolean isResizeableByDockedStack() {
Wale Ogunwale5e7409c2016-01-11 12:24:19 -0800609 final DisplayContent displayContent = getDisplayContent();
610 return displayContent != null && displayContent.getDockedStackLocked() != null
611 && mStack != null && StackId.isTaskResizeableByDockedStack(mStack.mStackId);
Chong Zhangb15758a2015-11-17 12:12:03 -0800612 }
613
614 /**
615 * Whether the task should be treated as if it's docked. Returns true if the task
616 * is currently in docked workspace, or it's side-by-side to a docked task.
617 */
618 boolean isDockedInEffect() {
619 return inDockedWorkspace() || isResizeableByDockedStack();
620 }
621
Chong Zhang2a88fc32016-01-11 17:14:24 -0800622 boolean isTwoFingerScrollMode() {
623 return isDockedInEffect() && !isResizeable();
624 }
625
Chong Zhangd8ceb852015-11-11 14:53:41 -0800626 WindowState getTopVisibleAppMainWindow() {
627 final AppWindowToken token = getTopVisibleAppToken();
628 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700629 }
630
Chong Zhangd8ceb852015-11-11 14:53:41 -0800631 AppWindowToken getTopVisibleAppToken() {
632 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
633 final AppWindowToken token = mAppTokens.get(i);
634 // skip hidden (or about to hide) apps
635 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
636 return token;
637 }
638 }
639 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700640 }
641
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800642 @Override
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700643 public boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700644 if (useCurrentBounds()) {
645 return mFullscreen;
646 }
647 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
648 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
649 // system.
650 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700651 }
652
653 @Override
654 public DisplayInfo getDisplayInfo() {
655 return mStack.getDisplayContent().getDisplayInfo();
656 }
657
658 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800659 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800660 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800661 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700662
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700663 @Override
664 public String toShortString() {
665 return "Task=" + mTaskId;
666 }
667
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800668 public void dump(String prefix, PrintWriter pw) {
669 final String doublePrefix = prefix + " ";
670
671 pw.println(prefix + "taskId=" + mTaskId);
672 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
673 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
674 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
675 pw.println(doublePrefix + "appTokens=" + mAppTokens);
676 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
677
678 final String triplePrefix = doublePrefix + " ";
679
680 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
681 final AppWindowToken wtoken = mAppTokens.get(i);
682 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
683 wtoken.dump(pw, triplePrefix);
684 }
685
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700686 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800687}