blob: f2f24748761e9744eb00e7a202703299a0987e34 [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;
Robert Carr03138452016-05-18 14:53:16 -070020import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070021import 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;
Wale Ogunwale51362492016-09-08 17:49:17 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
25import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
26import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080027import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Jorim Jaggid53f0922016-04-06 22:16:23 -070028import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
29import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070030import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
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;
Jorim Jaggi6626f542016-08-22 13:08:44 -070040import android.view.animation.Animation;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070041
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070042import android.view.SurfaceControl;
Craig Mautnere3119b72015-01-20 15:02:36 -080043import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080044
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070045import java.io.PrintWriter;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070046
47class Task implements DimLayer.DimLayerUser {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080048 static final String TAG = TAG_WITH_CLASS_NAME ? "Task" : TAG_WM;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070049 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
50 static final int BOUNDS_CHANGE_NONE = 0;
51 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
52 static final int BOUNDS_CHANGE_POSITION = 1;
53 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
54 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
55
Craig Mautnerc00204b2013-03-05 15:02:14 -080056 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080057 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080058 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070059 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080060 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080061 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080062
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070063 // Content limits relative to the DisplayContent this sits in.
64 private Rect mBounds = new Rect();
Jorim Jaggi0429f352015-12-22 16:29:16 +010065 final Rect mPreparedFrozenBounds = new Rect();
Jorim Jaggi26c8c422016-05-09 19:57:25 -070066 final Configuration mPreparedFrozenMergedConfig = new Configuration();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070067
Jorim Jaggidc249c42015-12-15 14:57:31 -080068 // Bounds used to calculate the insets.
69 private final Rect mTempInsetBounds = new Rect();
70
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070071 // Device rotation as of the last time {@link #mBounds} was set.
72 int mRotation;
73
74 // Whether mBounds is fullscreen
75 private boolean mFullscreen = true;
76
77 // Contains configurations settings that are different from the global configuration due to
78 // stack specific operations. E.g. {@link #setBounds}.
Jorim Jaggi26c8c422016-05-09 19:57:25 -070079 Configuration mOverrideConfig = Configuration.EMPTY;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070080
81 // For comparison with DisplayContent bounds.
82 private Rect mTmpRect = new Rect();
83 // For handling display rotations.
84 private Rect mTmpRect2 = new Rect();
85
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080086 // Resize mode of the task. See {@link ActivityInfo#resizeMode}
87 private int mResizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -080088
Chong Zhang3005e752015-09-18 18:46:28 -070089 // Whether the task is currently being drag-resized
90 private boolean mDragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010091 private int mDragResizeMode;
Chong Zhang3005e752015-09-18 18:46:28 -070092
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080093 private boolean mHomeTask;
94
Jiaquan Hedd1e66f2016-06-15 15:15:12 -070095 // Whether this task is an on-top launcher task, which is determined by the root activity.
96 private boolean mIsOnTopLauncher;
97
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070098 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
Jiaquan Hedd1e66f2016-06-15 15:15:12 -070099 Configuration config, boolean isOnTopLauncher) {
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;
Jiaquan Hedd1e66f2016-06-15 15:15:12 -0700104 mIsOnTopLauncher = isOnTopLauncher;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700105 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800106 }
107
108 DisplayContent getDisplayContent() {
109 return mStack.getDisplayContent();
110 }
111
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800112 void addAppToken(int addPos, AppWindowToken wtoken, int resizeMode, boolean homeTask) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800113 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800114 if (addPos >= lastPos) {
115 addPos = lastPos;
116 } else {
117 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
118 if (mAppTokens.get(pos).removed) {
119 // addPos assumes removed tokens are actually gone.
120 ++addPos;
121 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700122 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800123 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800124 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800125 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800126 mDeferRemoval = false;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800127 mResizeMode = resizeMode;
128 mHomeTask = homeTask;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800129 }
130
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700131 private boolean hasWindowsAlive() {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800132 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700133 if (mAppTokens.get(i).hasWindowsAlive()) {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800134 return true;
135 }
136 }
137 return false;
138 }
139
Craig Mautnere3119b72015-01-20 15:02:36 -0800140 void removeLocked() {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700141 if (hasWindowsAlive() && mStack.isAnimating()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800142 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800143 mDeferRemoval = true;
144 return;
145 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800146 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800147 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800148 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700149 DisplayContent content = getDisplayContent();
150 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800151 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700152 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800153 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800154 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800155 }
156
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800157 void moveTaskToStack(TaskStack stack, boolean toTop) {
158 if (stack == mStack) {
159 return;
160 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800161 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800162 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700163 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800164 if (mStack != null) {
165 mStack.removeTask(this);
166 }
167 stack.addTask(this, toTop);
168 }
169
Wale Ogunwale935e5022015-11-10 12:36:10 -0800170 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700171 if (mStack != null && stack != mStack) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800172 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700173 + " from stack=" + mStack);
174 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
175 mStack.removeTask(this);
176 }
177 stack.positionTask(this, position, showForAllUsers());
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800178 resizeLocked(bounds, config, false /* force */);
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800179
180 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700181 mAppTokens.get(activityNdx).notifyMovedInStack();
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800182 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700183 }
184
Wale Ogunwale571771c2016-08-26 13:18:50 -0700185 // TODO: Don't forget to switch to WC.detachChild
186 void detachChild(AppWindowToken wtoken) {
187 if (!removeAppToken(wtoken)) {
188 Slog.e(TAG, "detachChild: token=" + this + " not found.");
189 }
190 mStack.mExitingAppTokens.remove(wtoken);
191 }
192
Craig Mautnerc00204b2013-03-05 15:02:14 -0800193 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800194 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800195 if (mAppTokens.size() == 0) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100196 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800197 if (mDeferRemoval) {
198 removeLocked();
199 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800200 }
Craig Mautner83162a92015-01-26 14:43:30 -0800201 wtoken.mTask = null;
202 /* Leave mTaskId for now, it might be useful for debug
203 wtoken.mTaskId = -1;
204 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800205 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800206 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800207
Craig Mautnercbd84af2014-10-22 13:21:22 -0700208 void setSendingToBottom(boolean toBottom) {
209 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
210 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
211 }
212 }
213
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700214 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800215 private int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700216 if (config == null) {
217 config = Configuration.EMPTY;
218 }
219 if (bounds == null && !Configuration.EMPTY.equals(config)) {
220 throw new IllegalArgumentException("null bounds but non empty configuration: "
221 + config);
222 }
223 if (bounds != null && Configuration.EMPTY.equals(config)) {
224 throw new IllegalArgumentException("non null bounds, but empty configuration");
225 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700226 boolean oldFullscreen = mFullscreen;
227 int rotation = Surface.ROTATION_0;
228 final DisplayContent displayContent = mStack.getDisplayContent();
229 if (displayContent != null) {
230 displayContent.getLogicalDisplayRect(mTmpRect);
231 rotation = displayContent.getDisplayInfo().rotation;
Jorim Jaggi067e8172016-02-03 18:24:12 -0800232 mFullscreen = bounds == null;
233 if (mFullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700234 bounds = mTmpRect;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700235 }
236 }
237
238 if (bounds == null) {
239 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700240 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700241 }
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700242 if (mBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700243 return BOUNDS_CHANGE_NONE;
244 }
245
246 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700247 if (mBounds.left != bounds.left || mBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700248 boundsChange |= BOUNDS_CHANGE_POSITION;
249 }
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700250 if (mBounds.width() != bounds.width() || mBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700251 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700252 }
253
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700254 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800255
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700256 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700257 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800258 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700259 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700260 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700261 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700262 }
263
Jorim Jaggidc249c42015-12-15 14:57:31 -0800264 /**
265 * Sets the bounds used to calculate the insets. See
266 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
267 */
268 void setTempInsetBounds(Rect tempInsetBounds) {
269 if (tempInsetBounds != null) {
270 mTempInsetBounds.set(tempInsetBounds);
271 } else {
272 mTempInsetBounds.setEmpty();
273 }
274 }
275
276 /**
277 * Gets the bounds used to calculate the insets. See
278 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
279 */
280 void getTempInsetBounds(Rect out) {
281 out.set(mTempInsetBounds);
282 }
283
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800284 void setResizeable(int resizeMode) {
285 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800286 }
287
288 boolean isResizeable() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800289 return !mHomeTask
290 && (ActivityInfo.isResizeableMode(mResizeMode) || mService.mForceResizableTasks);
291 }
292
Jiaquan Hedd1e66f2016-06-15 15:15:12 -0700293 boolean isOnTopLauncher() {
294 return mIsOnTopLauncher;
295 }
296
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800297 boolean cropWindowsToStackBounds() {
298 return !mHomeTask && (isResizeable() || mResizeMode == RESIZE_MODE_CROP_WINDOWS);
299 }
300
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800301 boolean isHomeTask() {
302 return mHomeTask;
303 }
304
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800305 private boolean inCropWindowsResizeMode() {
306 return !mHomeTask && !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
Chong Zhangb15758a2015-11-17 12:12:03 -0800307 }
308
Chong Zhang87b21722015-09-21 15:39:51 -0700309 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700310 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700311 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700312 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700313 }
314 if (boundsChanged == BOUNDS_CHANGE_NONE) {
315 return false;
316 }
317 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700318 onResize();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800319 } else {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700320 onMovedByResize();
Chong Zhang3005e752015-09-18 18:46:28 -0700321 }
322 return true;
323 }
324
Jorim Jaggi0429f352015-12-22 16:29:16 +0100325 /**
326 * Prepares the task bounds to be frozen with the current size. See
327 * {@link AppWindowToken#freezeBounds}.
328 */
329 void prepareFreezingBounds() {
330 mPreparedFrozenBounds.set(mBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700331 mPreparedFrozenMergedConfig.setTo(mService.mCurConfiguration);
332 mPreparedFrozenMergedConfig.updateFrom(mOverrideConfig);
Jorim Jaggi0429f352015-12-22 16:29:16 +0100333 }
334
Chong Zhang5117e272016-05-03 12:47:34 -0700335 /**
336 * Align the task to the adjusted bounds.
337 *
338 * @param adjustedBounds Adjusted bounds to which the task should be aligned.
339 * @param tempInsetBounds Insets bounds for the task.
340 * @param alignBottom True if the task's bottom should be aligned to the adjusted
341 * bounds's bottom; false if the task's top should be aligned
342 * the adjusted bounds's top.
343 */
344 void alignToAdjustedBounds(
345 Rect adjustedBounds, Rect tempInsetBounds, boolean alignBottom) {
346 if (!isResizeable() || mOverrideConfig == Configuration.EMPTY) {
347 return;
348 }
349
350 getBounds(mTmpRect2);
351 if (alignBottom) {
352 int offsetY = adjustedBounds.bottom - mTmpRect2.bottom;
353 mTmpRect2.offset(0, offsetY);
354 } else {
355 mTmpRect2.offsetTo(adjustedBounds.left, adjustedBounds.top);
356 }
357 setTempInsetBounds(tempInsetBounds);
358 resizeLocked(mTmpRect2, mOverrideConfig, false /* forced */);
359 }
360
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700361 /** Return true if the current bound can get outputted to the rest of the system as-is. */
362 private boolean useCurrentBounds() {
363 final DisplayContent displayContent = mStack.getDisplayContent();
364 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700365 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700366 || displayContent == null
Jorim Jaggif4fa8cb2016-04-01 16:05:10 -0700367 || displayContent.getDockedStackVisibleForUserLocked() != null) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700368 return true;
369 }
370 return false;
371 }
372
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800373 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800374 void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700375 if (useCurrentBounds()) {
376 // No need to adjust the output bounds if fullscreen or the docked stack is visible
377 // since it is already what we want to represent to the rest of the system.
378 out.set(mBounds);
379 return;
380 }
381
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800382 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
383 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700384 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700385 }
386
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800387 /**
388 * Calculate the maximum visible area of this task. If the task has only one app,
389 * the result will be visible frame of that app. If the task has more than one apps,
390 * we search from top down if the next app got different visible area.
391 *
392 * This effort is to handle the case where some task (eg. GMail composer) might pop up
393 * a dialog that's different in size from the activity below, in which case we should
394 * be dimming the entire task area behind the dialog.
395 *
396 * @param out Rect containing the max visible bounds.
397 * @return true if the task has some visible app windows; false otherwise.
398 */
399 boolean getMaxVisibleBounds(Rect out) {
400 boolean foundTop = false;
401 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800402 final AppWindowToken token = mAppTokens.get(i);
403 // skip hidden (or about to hide) apps
404 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
405 continue;
406 }
407 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800408 if (win == null) {
409 continue;
410 }
411 if (!foundTop) {
412 out.set(win.mVisibleFrame);
413 foundTop = true;
414 continue;
415 }
416 if (win.mVisibleFrame.left < out.left) {
417 out.left = win.mVisibleFrame.left;
418 }
419 if (win.mVisibleFrame.top < out.top) {
420 out.top = win.mVisibleFrame.top;
421 }
422 if (win.mVisibleFrame.right > out.right) {
423 out.right = win.mVisibleFrame.right;
424 }
425 if (win.mVisibleFrame.bottom > out.bottom) {
426 out.bottom = win.mVisibleFrame.bottom;
427 }
428 }
429 return foundTop;
430 }
431
432 /** Bounds of the task to be used for dimming, as well as touch related tests. */
433 @Override
434 public void getDimBounds(Rect out) {
Robert Carra86a6bf2016-04-08 17:34:16 -0700435 final DisplayContent displayContent = mStack.getDisplayContent();
436 // It doesn't matter if we in particular are part of the resize, since we couldn't have
437 // a DimLayer anyway if we weren't visible.
438 final boolean dockedResizing = displayContent != null ?
439 displayContent.mDividerControllerLocked.isResizing() : false;
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800440 if (useCurrentBounds()) {
441 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
442 return;
443 }
444
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700445 if (!mFullscreen) {
446 // When minimizing the docked stack when going home, we don't adjust the task bounds
447 // so we need to intersect the task bounds with the stack bounds here.
Robert Carra86a6bf2016-04-08 17:34:16 -0700448 //
449 // If we are Docked Resizing with snap points, the task bounds could be smaller than the stack
450 // bounds and so we don't even want to use them. Even if the app should not be resized the Dim
451 // should keep up with the divider.
452 if (dockedResizing) {
453 mStack.getBounds(out);
454 } else {
455 mStack.getBounds(mTmpRect);
456 mTmpRect.intersect(mBounds);
457 }
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700458 out.set(mTmpRect);
459 } else {
460 out.set(mBounds);
461 }
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800462 return;
463 }
464
465 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
466 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
467 // system.
Robert Carra86a6bf2016-04-08 17:34:16 -0700468 displayContent.getLogicalDisplayRect(out);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800469 }
470
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100471 void setDragResizing(boolean dragResizing, int dragResizeMode) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800472 if (mDragResizing != dragResizing) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100473 if (!DragResizeMode.isModeAllowedForStack(mStack.mStackId, dragResizeMode)) {
474 throw new IllegalArgumentException("Drag resize mode not allow for stack stackId="
475 + mStack.mStackId + " dragResizeMode=" + dragResizeMode);
476 }
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800477 mDragResizing = dragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100478 mDragResizeMode = dragResizeMode;
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800479 resetDragResizingChangeReported();
480 }
481 }
482
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700483 private void resetDragResizingChangeReported() {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800484 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700485 mAppTokens.get(activityNdx).resetDragResizingChangeReported();
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800486 }
Chong Zhang3005e752015-09-18 18:46:28 -0700487 }
488
489 boolean isDragResizing() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700490 return mDragResizing;
Chong Zhang3005e752015-09-18 18:46:28 -0700491 }
492
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100493 int getDragResizeMode() {
494 return mDragResizeMode;
495 }
496
Jorim Jaggiff71d202016-04-14 13:12:36 -0700497 /**
498 * Adds all of the tasks windows to {@link WindowManagerService#mWaitingForDrawn} if drag
499 * resizing state of the window has been changed.
500 */
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700501 void setWaitingForDrawnIfResizingChanged() {
502 for (int i = mAppTokens.size() - 1; i >= 0; --i) {
503 mAppTokens.get(i).setWaitingForDrawnIfResizingChanged();
504 }
505 }
506
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700507 boolean detachFromDisplay() {
508 boolean didSomething = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700509 for (int i = mAppTokens.size() - 1; i >= 0; --i) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700510 didSomething |= mAppTokens.get(i).detachFromDisplay();
Jorim Jaggiff71d202016-04-14 13:12:36 -0700511 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700512 return didSomething;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700513 }
514
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700515 void updateDisplayInfo(final DisplayContent displayContent) {
516 if (displayContent == null) {
517 return;
518 }
519 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700520 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700521 return;
522 }
523 final int newRotation = displayContent.getDisplayInfo().rotation;
524 if (mRotation == newRotation) {
525 return;
526 }
527
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800528 // Device rotation changed.
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700529 // - We don't want the task to move around on the screen when this happens, so update the
530 // task bounds so it stays in the same place.
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800531 // - Rotate the bounds and notify activity manager if the task can be resized independently
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700532 // from its stack. The stack will take care of task rotation for the other case.
533 mTmpRect2.set(mBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800534
535 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
536 setBounds(mTmpRect2, mOverrideConfig);
537 return;
538 }
539
Wale Ogunwale94744212015-09-21 19:01:47 -0700540 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700541 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800542 // Post message to inform activity manager of the bounds change simulating a one-way
543 // call. We do this to prevent a deadlock between window manager lock and activity
544 // manager lock been held.
545 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700546 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700547 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700548 }
549
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700550 private void onResize() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700551 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700552 mAppTokens.get(activityNdx).onResize();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700553 }
554 }
555
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700556 private void onMovedByResize() {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700557 for (int i = mAppTokens.size() - 1; i >= 0; --i) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700558 mAppTokens.get(i).onMovedByResize();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800559 }
560 }
561
Winsonc28098f2015-10-30 14:50:19 -0700562 /**
563 * Cancels any running app transitions associated with the task.
564 */
565 void cancelTaskWindowTransition() {
566 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
567 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
568 }
569 }
570
Winson13d30662015-11-06 15:30:29 -0800571 /**
572 * Cancels any running thumbnail transitions associated with the task.
573 */
574 void cancelTaskThumbnailTransition() {
575 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
576 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
577 }
578 }
579
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700580 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700581 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700582 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700583 }
584
Wale Ogunwale44f21802016-09-02 12:49:48 -0700585 boolean hasContentToDisplay() {
586 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
587 final AppWindowToken appToken = mAppTokens.get(i);
588 if (appToken.hasContentToDisplay()) {
589 return true;
590 }
591 }
592 return false;
593 }
594
Jorim Jaggiff71d202016-04-14 13:12:36 -0700595 boolean isVisible() {
596 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
597 final AppWindowToken appToken = mAppTokens.get(i);
598 if (appToken.isVisible()) {
599 return true;
600 }
601 }
602 return false;
603 }
604
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700605 boolean inHomeStack() {
606 return mStack != null && mStack.mStackId == HOME_STACK_ID;
607 }
608
Chong Zhang09b21ef2015-09-14 10:20:21 -0700609 boolean inFreeformWorkspace() {
610 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
611 }
612
Robert Carr03138452016-05-18 14:53:16 -0700613 boolean inPinnedWorkspace() {
614 return mStack != null && mStack.mStackId == PINNED_STACK_ID;
615 }
616
Robert Carre6275582016-02-29 15:45:45 -0800617 boolean isFloating() {
618 return StackId.tasksAreFloating(mStack.mStackId);
619 }
620
Chong Zhangd8ceb852015-11-11 14:53:41 -0800621 WindowState getTopVisibleAppMainWindow() {
622 final AppWindowToken token = getTopVisibleAppToken();
623 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700624 }
625
Chong Zhangd8ceb852015-11-11 14:53:41 -0800626 AppWindowToken getTopVisibleAppToken() {
627 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
628 final AppWindowToken token = mAppTokens.get(i);
629 // skip hidden (or about to hide) apps
630 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
631 return token;
632 }
633 }
634 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700635 }
636
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800637 @Override
Wale Ogunwale29bfbb82016-05-12 15:13:52 -0700638 public boolean dimFullscreen() {
639 return isHomeTask() || isFullscreen();
640 }
641
642 boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700643 if (useCurrentBounds()) {
644 return mFullscreen;
645 }
646 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
647 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
648 // system.
649 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700650 }
651
652 @Override
653 public DisplayInfo getDisplayInfo() {
654 return mStack.getDisplayContent().getDisplayInfo();
655 }
656
Jorim Jaggi6626f542016-08-22 13:08:44 -0700657 /**
658 * See {@link WindowManagerService#overridePlayingAppAnimationsLw}
659 */
660 void overridePlayingAppAnimations(Animation a) {
661 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
662 mAppTokens.get(i).overridePlayingAppAnimations(a);
663 }
664 }
665
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700666 void forceWindowsScaleable(boolean force) {
667 SurfaceControl.openTransaction();
668 try {
669 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
670 mAppTokens.get(i).forceWindowsScaleableInTransaction(force);
671 }
672 } finally {
673 SurfaceControl.closeTransaction();
674 }
675 }
676
677 boolean isAnimating() {
678 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
679 final AppWindowToken aToken = mAppTokens.get(i);
680 if (aToken.isAnimating()) {
681 return true;
682 }
683 }
684 return false;
685 }
686
Wale Ogunwale51362492016-09-08 17:49:17 -0700687 boolean fillsParent() {
688 return mFullscreen || !StackId.isTaskResizeAllowed(mStack.mStackId);
689 }
690
691 // TODO: Remove once switched to use WindowContainer
692 int getOrientation() {
693 int candidate = SCREEN_ORIENTATION_UNSET;
694
695 for (int i = mAppTokens.size() - 1; i >= 0; --i) {
696 final AppWindowToken token = mAppTokens.get(i);
697 final int orientation = token.getOrientation();
698
699 if (orientation == SCREEN_ORIENTATION_BEHIND) {
700 candidate = orientation;
701 continue;
702 }
703
704 if (orientation != SCREEN_ORIENTATION_UNSET) {
705 if (token.fillsParent() || orientation != SCREEN_ORIENTATION_UNSPECIFIED) {
706 return orientation;
707 }
708 }
709 }
710
711 return candidate;
712 }
713
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700714 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800715 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800716 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800717 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700718
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700719 @Override
720 public String toShortString() {
721 return "Task=" + mTaskId;
722 }
723
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800724 public void dump(String prefix, PrintWriter pw) {
725 final String doublePrefix = prefix + " ";
726
727 pw.println(prefix + "taskId=" + mTaskId);
728 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
729 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
730 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
731 pw.println(doublePrefix + "appTokens=" + mAppTokens);
732 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
733
734 final String triplePrefix = doublePrefix + " ";
735
736 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
737 final AppWindowToken wtoken = mAppTokens.get(i);
738 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
739 wtoken.dump(pw, triplePrefix);
740 }
741
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700742 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800743}