blob: 8f4197f06f1b9c226a56885a1072fd4af49ff1e6 [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
Jorim Jaggid53f0922016-04-06 22:16:23 -070019import static android.app.ActivityManager.RESIZE_MODE_SYSTEM_SCREEN_ROTATION;
Wale Ogunwale3797c222015-10-27 14:21:58 -070020import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
21import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080023import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS;
Jorim Jaggid53f0922016-04-06 22:16:23 -070024import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080025import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_RESIZE;
26import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Jorim Jaggid53f0922016-04-06 22:16:23 -070027import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
28import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070029import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Robert Carr2c17cd22016-04-08 17:52:28 -070030import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Wale Ogunwale99db1862015-10-23 20:08:22 -070031
Wale Ogunwale3797c222015-10-27 14:21:58 -070032import android.app.ActivityManager.StackId;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080033import android.content.pm.ActivityInfo;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070034import android.content.res.Configuration;
35import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080036import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080037import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070038import android.view.DisplayInfo;
39import android.view.Surface;
40
Craig Mautnere3119b72015-01-20 15:02:36 -080041import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080042
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070043import java.io.PrintWriter;
44import java.util.ArrayList;
45
46class Task implements DimLayer.DimLayerUser {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080047 static final String TAG = TAG_WITH_CLASS_NAME ? "Task" : TAG_WM;
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
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080087 // Resize mode of the task. See {@link ActivityInfo#resizeMode}
88 private int mResizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -080089
Chong Zhang3005e752015-09-18 18:46:28 -070090 // Whether the task is currently being drag-resized
91 private boolean mDragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010092 private int mDragResizeMode;
Chong Zhang3005e752015-09-18 18:46:28 -070093
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080094 private boolean mHomeTask;
95
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070096 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
97 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -080098 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -080099 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -0700100 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -0800101 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700102 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800103 }
104
105 DisplayContent getDisplayContent() {
106 return mStack.getDisplayContent();
107 }
108
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800109 void addAppToken(int addPos, AppWindowToken wtoken, int resizeMode, boolean homeTask) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800110 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800111 if (addPos >= lastPos) {
112 addPos = lastPos;
113 } else {
114 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
115 if (mAppTokens.get(pos).removed) {
116 // addPos assumes removed tokens are actually gone.
117 ++addPos;
118 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700119 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800120 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800121 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800122 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800123 mDeferRemoval = false;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800124 mResizeMode = resizeMode;
125 mHomeTask = homeTask;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800126 }
127
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700128 private boolean hasWindowsAlive() {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800129 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700130 if (mAppTokens.get(i).hasWindowsAlive()) {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800131 return true;
132 }
133 }
134 return false;
135 }
136
Craig Mautnere3119b72015-01-20 15:02:36 -0800137 void removeLocked() {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700138 if (hasWindowsAlive() && mStack.isAnimating()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800139 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800140 mDeferRemoval = true;
141 return;
142 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800143 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800144 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800145 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700146 DisplayContent content = getDisplayContent();
147 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800148 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700149 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800150 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800151 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800152 }
153
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800154 void moveTaskToStack(TaskStack stack, boolean toTop) {
155 if (stack == mStack) {
156 return;
157 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800158 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800159 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700160 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800161 if (mStack != null) {
162 mStack.removeTask(this);
163 }
164 stack.addTask(this, toTop);
165 }
166
Wale Ogunwale935e5022015-11-10 12:36:10 -0800167 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700168 if (mStack != null && stack != mStack) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800169 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700170 + " from stack=" + mStack);
171 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
172 mStack.removeTask(this);
173 }
174 stack.positionTask(this, position, showForAllUsers());
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800175 resizeLocked(bounds, config, false /* force */);
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800176
177 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
178 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
179 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
180 final WindowState win = windows.get(winNdx);
181 win.notifyMovedInStack();
182 }
183 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700184 }
185
Craig Mautnerc00204b2013-03-05 15:02:14 -0800186 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800187 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800188 if (mAppTokens.size() == 0) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100189 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800190 if (mDeferRemoval) {
191 removeLocked();
192 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800193 }
Craig Mautner83162a92015-01-26 14:43:30 -0800194 wtoken.mTask = null;
195 /* Leave mTaskId for now, it might be useful for debug
196 wtoken.mTaskId = -1;
197 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800198 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800199 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800200
Craig Mautnercbd84af2014-10-22 13:21:22 -0700201 void setSendingToBottom(boolean toBottom) {
202 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
203 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
204 }
205 }
206
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700207 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800208 private int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700209 if (config == null) {
210 config = Configuration.EMPTY;
211 }
212 if (bounds == null && !Configuration.EMPTY.equals(config)) {
213 throw new IllegalArgumentException("null bounds but non empty configuration: "
214 + config);
215 }
216 if (bounds != null && Configuration.EMPTY.equals(config)) {
217 throw new IllegalArgumentException("non null bounds, but empty configuration");
218 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700219 boolean oldFullscreen = mFullscreen;
220 int rotation = Surface.ROTATION_0;
221 final DisplayContent displayContent = mStack.getDisplayContent();
222 if (displayContent != null) {
223 displayContent.getLogicalDisplayRect(mTmpRect);
224 rotation = displayContent.getDisplayInfo().rotation;
Jorim Jaggi067e8172016-02-03 18:24:12 -0800225 mFullscreen = bounds == null;
226 if (mFullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700227 bounds = mTmpRect;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700228 }
229 }
230
231 if (bounds == null) {
232 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700233 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700234 }
Chong Zhangf66db432016-01-13 10:39:51 -0800235 if (mPreScrollBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700236 return BOUNDS_CHANGE_NONE;
237 }
238
239 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhangf66db432016-01-13 10:39:51 -0800240 if (mPreScrollBounds.left != bounds.left || mPreScrollBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700241 boundsChange |= BOUNDS_CHANGE_POSITION;
242 }
Chong Zhangf66db432016-01-13 10:39:51 -0800243 if (mPreScrollBounds.width() != bounds.width() || mPreScrollBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700244 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700245 }
246
Chong Zhangf66db432016-01-13 10:39:51 -0800247
248 mPreScrollBounds.set(bounds);
249
250 resetScrollLocked();
251
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700252 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700253 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800254 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700255 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700256 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700257 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700258 }
259
Jorim Jaggidc249c42015-12-15 14:57:31 -0800260 /**
261 * Sets the bounds used to calculate the insets. See
262 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
263 */
264 void setTempInsetBounds(Rect tempInsetBounds) {
265 if (tempInsetBounds != null) {
266 mTempInsetBounds.set(tempInsetBounds);
267 } else {
268 mTempInsetBounds.setEmpty();
269 }
270 }
271
272 /**
273 * Gets the bounds used to calculate the insets. See
274 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
275 */
276 void getTempInsetBounds(Rect out) {
277 out.set(mTempInsetBounds);
278 }
279
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800280 void setResizeable(int resizeMode) {
281 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800282 }
283
284 boolean isResizeable() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800285 return !mHomeTask
286 && (ActivityInfo.isResizeableMode(mResizeMode) || mService.mForceResizableTasks);
287 }
288
289 boolean cropWindowsToStackBounds() {
290 return !mHomeTask && (isResizeable() || mResizeMode == RESIZE_MODE_CROP_WINDOWS);
291 }
292
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800293 boolean isHomeTask() {
294 return mHomeTask;
295 }
296
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800297 private boolean inCropWindowsResizeMode() {
298 return !mHomeTask && !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
Chong Zhangb15758a2015-11-17 12:12:03 -0800299 }
300
Chong Zhang87b21722015-09-21 15:39:51 -0700301 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700302 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700303 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700304 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700305 }
306 if (boundsChanged == BOUNDS_CHANGE_NONE) {
307 return false;
308 }
309 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
310 resizeWindows();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800311 } else {
312 moveWindows();
Chong Zhang3005e752015-09-18 18:46:28 -0700313 }
314 return true;
315 }
316
Jorim Jaggi0429f352015-12-22 16:29:16 +0100317 /**
318 * Prepares the task bounds to be frozen with the current size. See
319 * {@link AppWindowToken#freezeBounds}.
320 */
321 void prepareFreezingBounds() {
322 mPreparedFrozenBounds.set(mBounds);
323 }
324
Chong Zhang5117e272016-05-03 12:47:34 -0700325 /**
326 * Align the task to the adjusted bounds.
327 *
328 * @param adjustedBounds Adjusted bounds to which the task should be aligned.
329 * @param tempInsetBounds Insets bounds for the task.
330 * @param alignBottom True if the task's bottom should be aligned to the adjusted
331 * bounds's bottom; false if the task's top should be aligned
332 * the adjusted bounds's top.
333 */
334 void alignToAdjustedBounds(
335 Rect adjustedBounds, Rect tempInsetBounds, boolean alignBottom) {
336 if (!isResizeable() || mOverrideConfig == Configuration.EMPTY) {
337 return;
338 }
339
340 getBounds(mTmpRect2);
341 if (alignBottom) {
342 int offsetY = adjustedBounds.bottom - mTmpRect2.bottom;
343 mTmpRect2.offset(0, offsetY);
344 } else {
345 mTmpRect2.offsetTo(adjustedBounds.left, adjustedBounds.top);
346 }
347 setTempInsetBounds(tempInsetBounds);
348 resizeLocked(mTmpRect2, mOverrideConfig, false /* forced */);
349 }
350
Chong Zhangf66db432016-01-13 10:39:51 -0800351 void resetScrollLocked() {
352 if (mScrollValid) {
353 mScrollValid = false;
354 applyScrollToAllWindows(0, 0);
355 }
356 mBounds.set(mPreScrollBounds);
357 }
358
359 void applyScrollToAllWindows(final int xOffset, final int yOffset) {
360 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
361 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
362 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
363 final WindowState win = windows.get(winNdx);
364 win.mXOffset = xOffset;
365 win.mYOffset = yOffset;
366 }
367 }
368 }
369
370 void applyScrollToWindowIfNeeded(final WindowState win) {
371 if (mScrollValid) {
372 win.mXOffset = mBounds.left;
373 win.mYOffset = mBounds.top;
374 }
375 }
376
Chong Zhangb15758a2015-11-17 12:12:03 -0800377 boolean scrollLocked(Rect bounds) {
378 // shift the task bound if it doesn't fully cover the stack area
379 mStack.getDimBounds(mTmpRect);
380 if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) {
381 if (bounds.left > mTmpRect.left) {
382 bounds.left = mTmpRect.left;
383 bounds.right = mTmpRect.left + mBounds.width();
384 } else if (bounds.right < mTmpRect.right) {
385 bounds.left = mTmpRect.right - mBounds.width();
386 bounds.right = mTmpRect.right;
387 }
388 } else {
389 if (bounds.top > mTmpRect.top) {
390 bounds.top = mTmpRect.top;
391 bounds.bottom = mTmpRect.top + mBounds.height();
392 } else if (bounds.bottom < mTmpRect.bottom) {
393 bounds.top = mTmpRect.bottom - mBounds.height();
394 bounds.bottom = mTmpRect.bottom;
395 }
396 }
397
Chong Zhangf66db432016-01-13 10:39:51 -0800398 // We can stop here if we're already scrolling and the scrolled bounds not changed.
399 if (mScrollValid && bounds.equals(mBounds)) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800400 return false;
401 }
Chong Zhangf66db432016-01-13 10:39:51 -0800402
Chong Zhangb15758a2015-11-17 12:12:03 -0800403 // Normal setBounds() does not allow non-null bounds for fullscreen apps.
404 // We only change bounds for the scrolling case without change it size,
405 // on resizing path we should still want the validation.
406 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800407 mScrollValid = true;
408 applyScrollToAllWindows(bounds.left, bounds.top);
Chong Zhangb15758a2015-11-17 12:12:03 -0800409 return true;
410 }
411
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700412 /** Return true if the current bound can get outputted to the rest of the system as-is. */
413 private boolean useCurrentBounds() {
414 final DisplayContent displayContent = mStack.getDisplayContent();
415 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700416 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700417 || displayContent == null
Jorim Jaggif4fa8cb2016-04-01 16:05:10 -0700418 || displayContent.getDockedStackVisibleForUserLocked() != null) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700419 return true;
420 }
421 return false;
422 }
423
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800424 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800425 void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700426 if (useCurrentBounds()) {
427 // No need to adjust the output bounds if fullscreen or the docked stack is visible
428 // since it is already what we want to represent to the rest of the system.
429 out.set(mBounds);
430 return;
431 }
432
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800433 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
434 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700435 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700436 }
437
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800438 /**
439 * Calculate the maximum visible area of this task. If the task has only one app,
440 * the result will be visible frame of that app. If the task has more than one apps,
441 * we search from top down if the next app got different visible area.
442 *
443 * This effort is to handle the case where some task (eg. GMail composer) might pop up
444 * a dialog that's different in size from the activity below, in which case we should
445 * be dimming the entire task area behind the dialog.
446 *
447 * @param out Rect containing the max visible bounds.
448 * @return true if the task has some visible app windows; false otherwise.
449 */
450 boolean getMaxVisibleBounds(Rect out) {
451 boolean foundTop = false;
452 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800453 final AppWindowToken token = mAppTokens.get(i);
454 // skip hidden (or about to hide) apps
455 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
456 continue;
457 }
458 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800459 if (win == null) {
460 continue;
461 }
462 if (!foundTop) {
463 out.set(win.mVisibleFrame);
464 foundTop = true;
465 continue;
466 }
467 if (win.mVisibleFrame.left < out.left) {
468 out.left = win.mVisibleFrame.left;
469 }
470 if (win.mVisibleFrame.top < out.top) {
471 out.top = win.mVisibleFrame.top;
472 }
473 if (win.mVisibleFrame.right > out.right) {
474 out.right = win.mVisibleFrame.right;
475 }
476 if (win.mVisibleFrame.bottom > out.bottom) {
477 out.bottom = win.mVisibleFrame.bottom;
478 }
479 }
480 return foundTop;
481 }
482
483 /** Bounds of the task to be used for dimming, as well as touch related tests. */
484 @Override
485 public void getDimBounds(Rect out) {
Robert Carra86a6bf2016-04-08 17:34:16 -0700486 final DisplayContent displayContent = mStack.getDisplayContent();
487 // It doesn't matter if we in particular are part of the resize, since we couldn't have
488 // a DimLayer anyway if we weren't visible.
489 final boolean dockedResizing = displayContent != null ?
490 displayContent.mDividerControllerLocked.isResizing() : false;
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800491 if (useCurrentBounds()) {
492 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
493 return;
494 }
495
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700496 if (!mFullscreen) {
497 // When minimizing the docked stack when going home, we don't adjust the task bounds
498 // so we need to intersect the task bounds with the stack bounds here.
Robert Carra86a6bf2016-04-08 17:34:16 -0700499 //
500 // If we are Docked Resizing with snap points, the task bounds could be smaller than the stack
501 // bounds and so we don't even want to use them. Even if the app should not be resized the Dim
502 // should keep up with the divider.
503 if (dockedResizing) {
504 mStack.getBounds(out);
505 } else {
506 mStack.getBounds(mTmpRect);
507 mTmpRect.intersect(mBounds);
508 }
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700509 out.set(mTmpRect);
510 } else {
511 out.set(mBounds);
512 }
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800513 return;
514 }
515
516 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
517 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
518 // system.
Robert Carra86a6bf2016-04-08 17:34:16 -0700519 displayContent.getLogicalDisplayRect(out);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800520 }
521
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100522 void setDragResizing(boolean dragResizing, int dragResizeMode) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800523 if (mDragResizing != dragResizing) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100524 if (!DragResizeMode.isModeAllowedForStack(mStack.mStackId, dragResizeMode)) {
525 throw new IllegalArgumentException("Drag resize mode not allow for stack stackId="
526 + mStack.mStackId + " dragResizeMode=" + dragResizeMode);
527 }
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800528 mDragResizing = dragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100529 mDragResizeMode = dragResizeMode;
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800530 resetDragResizingChangeReported();
531 }
532 }
533
534 void resetDragResizingChangeReported() {
535 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
536 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
537 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
538 final WindowState win = windows.get(winNdx);
539 win.resetDragResizingChangeReported();
540 }
541 }
Chong Zhang3005e752015-09-18 18:46:28 -0700542 }
543
544 boolean isDragResizing() {
Wale Ogunwalece144522016-02-05 22:51:01 -0800545 return mDragResizing || (mStack != null && mStack.isDragResizing());
Chong Zhang3005e752015-09-18 18:46:28 -0700546 }
547
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100548 int getDragResizeMode() {
549 return mDragResizeMode;
550 }
551
Jorim Jaggiff71d202016-04-14 13:12:36 -0700552 /**
553 * Adds all of the tasks windows to {@link WindowManagerService#mWaitingForDrawn} if drag
554 * resizing state of the window has been changed.
555 */
556 void addWindowsWaitingForDrawnIfResizingChanged() {
557 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
558 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
559 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
560 final WindowState win = windows.get(winNdx);
561 if (win.isDragResizeChanged()) {
562 mService.mWaitingForDrawn.add(win);
563 }
564 }
565 }
566 }
567
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700568 void updateDisplayInfo(final DisplayContent displayContent) {
569 if (displayContent == null) {
570 return;
571 }
572 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700573 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700574 return;
575 }
576 final int newRotation = displayContent.getDisplayInfo().rotation;
577 if (mRotation == newRotation) {
578 return;
579 }
580
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800581 // Device rotation changed.
582 // - Reset the bounds to the pre-scroll bounds as whatever scrolling was done is no longer
583 // valid.
584 // - Rotate the bounds and notify activity manager if the task can be resized independently
585 // from its stack. The stack will take care of task rotation for the other case.
Chong Zhangf66db432016-01-13 10:39:51 -0800586 mTmpRect2.set(mPreScrollBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800587
588 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
589 setBounds(mTmpRect2, mOverrideConfig);
590 return;
591 }
592
Wale Ogunwale94744212015-09-21 19:01:47 -0700593 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700594 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800595 // Post message to inform activity manager of the bounds change simulating a one-way
596 // call. We do this to prevent a deadlock between window manager lock and activity
597 // manager lock been held.
598 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
599 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mPreScrollBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700600 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700601 }
602
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700603 void resizeWindows() {
604 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
605 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800606 final AppWindowToken atoken = mAppTokens.get(activityNdx);
607
608 // Some windows won't go through the resizing process, if they don't have a surface, so
609 // destroy all saved surfaces here.
610 atoken.destroySavedSurfaces();
611 final ArrayList<WindowState> windows = atoken.allAppWindows;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700612 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
613 final WindowState win = windows.get(winNdx);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800614 if (win.mHasSurface && !resizingWindows.contains(win)) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800615 if (DEBUG_RESIZE) Slog.d(TAG, "resizeWindows: Resizing " + win);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700616 resizingWindows.add(win);
Jorim Jaggif3df0aa2016-04-06 15:56:33 -0700617
618 // If we are not drag resizing, force recreating of a new surface so updating
619 // the content and positioning that surface will be in sync.
Robert Carr2c17cd22016-04-08 17:52:28 -0700620 //
621 // As we use this flag as a hint to freeze surface boundary updates,
622 // we'd like to only apply this to TYPE_BASE_APPLICATION,
623 // windows of TYPE_APPLICATION like dialogs, could appear
624 // to not be drag resizing while they resize, but we'd
625 // still like to manipulate their frame to update crop, etc...
626 //
627 // Anyway we don't need to synchronize position and content updates for these
628 // windows since they aren't at the base layer and could be moved around anyway.
Robert Carr1ca6a332016-04-11 18:00:43 -0700629 if (!win.computeDragResizing() && win.mAttrs.type == TYPE_BASE_APPLICATION &&
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700630 !mStack.getBoundsAnimating() && !win.isGoneForLayoutLw()) {
Jorim Jaggif3df0aa2016-04-06 15:56:33 -0700631 win.mResizedWhileNotDragResizing = true;
632 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700633 }
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800634 if (win.isGoneForLayoutLw()) {
635 win.mResizedWhileGone = true;
636 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700637 }
638 }
639 }
640
Chong Zhangbd0d9372015-12-28 15:18:29 -0800641 void moveWindows() {
642 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
643 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
644 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
645 final WindowState win = windows.get(winNdx);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800646 if (DEBUG_RESIZE) Slog.d(TAG, "moveWindows: Moving " + win);
Chong Zhangbd0d9372015-12-28 15:18:29 -0800647 win.mMovedByResize = true;
648 }
649 }
650 }
651
Winsonc28098f2015-10-30 14:50:19 -0700652 /**
653 * Cancels any running app transitions associated with the task.
654 */
655 void cancelTaskWindowTransition() {
656 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
657 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
658 }
659 }
660
Winson13d30662015-11-06 15:30:29 -0800661 /**
662 * Cancels any running thumbnail transitions associated with the task.
663 */
664 void cancelTaskThumbnailTransition() {
665 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
666 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
667 }
668 }
669
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700670 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700671 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700672 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700673 }
674
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800675 boolean isVisibleForUser() {
676 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
677 final AppWindowToken appToken = mAppTokens.get(i);
678 for (int j = appToken.allAppWindows.size() - 1; j >= 0; j--) {
679 WindowState window = appToken.allAppWindows.get(j);
680 if (!window.isHiddenFromUserLocked()) {
681 return true;
682 }
683 }
684 }
685 return false;
686 }
687
Jorim Jaggiff71d202016-04-14 13:12:36 -0700688 boolean isVisible() {
689 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
690 final AppWindowToken appToken = mAppTokens.get(i);
691 if (appToken.isVisible()) {
692 return true;
693 }
694 }
695 return false;
696 }
697
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700698 boolean inHomeStack() {
699 return mStack != null && mStack.mStackId == HOME_STACK_ID;
700 }
701
Chong Zhang09b21ef2015-09-14 10:20:21 -0700702 boolean inFreeformWorkspace() {
703 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
704 }
705
Filip Gruszczynski955b2fc2015-10-15 14:46:07 -0700706 boolean inDockedWorkspace() {
707 return mStack != null && mStack.mStackId == DOCKED_STACK_ID;
708 }
709
Chong Zhangb15758a2015-11-17 12:12:03 -0800710 boolean isResizeableByDockedStack() {
Wale Ogunwale5e7409c2016-01-11 12:24:19 -0800711 final DisplayContent displayContent = getDisplayContent();
712 return displayContent != null && displayContent.getDockedStackLocked() != null
713 && mStack != null && StackId.isTaskResizeableByDockedStack(mStack.mStackId);
Chong Zhangb15758a2015-11-17 12:12:03 -0800714 }
715
Robert Carre6275582016-02-29 15:45:45 -0800716 boolean isFloating() {
717 return StackId.tasksAreFloating(mStack.mStackId);
718 }
719
Chong Zhangb15758a2015-11-17 12:12:03 -0800720 /**
721 * Whether the task should be treated as if it's docked. Returns true if the task
722 * is currently in docked workspace, or it's side-by-side to a docked task.
723 */
724 boolean isDockedInEffect() {
725 return inDockedWorkspace() || isResizeableByDockedStack();
726 }
727
Chong Zhang2a88fc32016-01-11 17:14:24 -0800728 boolean isTwoFingerScrollMode() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800729 return inCropWindowsResizeMode() && isDockedInEffect();
Chong Zhang2a88fc32016-01-11 17:14:24 -0800730 }
731
Chong Zhangd8ceb852015-11-11 14:53:41 -0800732 WindowState getTopVisibleAppMainWindow() {
733 final AppWindowToken token = getTopVisibleAppToken();
734 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700735 }
736
Chong Zhangd8ceb852015-11-11 14:53:41 -0800737 AppWindowToken getTopVisibleAppToken() {
738 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
739 final AppWindowToken token = mAppTokens.get(i);
740 // skip hidden (or about to hide) apps
741 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
742 return token;
743 }
744 }
745 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700746 }
747
Jorim Jaggiaf558e12016-04-27 22:56:56 -0700748 AppWindowToken getTopAppToken() {
749 return mAppTokens.size() > 0 ? mAppTokens.get(mAppTokens.size() - 1) : null;
750 }
751
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800752 @Override
Wale Ogunwale29bfbb82016-05-12 15:13:52 -0700753 public boolean dimFullscreen() {
754 return isHomeTask() || isFullscreen();
755 }
756
757 boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700758 if (useCurrentBounds()) {
759 return mFullscreen;
760 }
761 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
762 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
763 // system.
764 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700765 }
766
767 @Override
768 public DisplayInfo getDisplayInfo() {
769 return mStack.getDisplayContent().getDisplayInfo();
770 }
771
772 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800773 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800774 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800775 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700776
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700777 @Override
778 public String toShortString() {
779 return "Task=" + mTaskId;
780 }
781
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800782 public void dump(String prefix, PrintWriter pw) {
783 final String doublePrefix = prefix + " ";
784
785 pw.println(prefix + "taskId=" + mTaskId);
786 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
787 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
788 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
789 pw.println(doublePrefix + "appTokens=" + mAppTokens);
790 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
791
792 final String triplePrefix = doublePrefix + " ";
793
794 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
795 final AppWindowToken wtoken = mAppTokens.get(i);
796 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
797 wtoken.dump(pw, triplePrefix);
798 }
799
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700800 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800801}