blob: 04f592c1dd9ee41b8b772fcb35ef5a63d4110640 [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;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080024import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Jorim Jaggid53f0922016-04-06 22:16:23 -070025import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
26import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070027import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Wale Ogunwale99db1862015-10-23 20:08:22 -070028
Wale Ogunwale3797c222015-10-27 14:21:58 -070029import android.app.ActivityManager.StackId;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080030import android.content.pm.ActivityInfo;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070031import android.content.res.Configuration;
32import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080033import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080034import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070035import android.view.DisplayInfo;
36import android.view.Surface;
37
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070038import android.view.SurfaceControl;
Craig Mautnere3119b72015-01-20 15:02:36 -080039import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080040
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070041import java.io.PrintWriter;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070042
Wale Ogunwalef6192862016-09-10 13:42:30 -070043class Task extends WindowContainer<AppWindowToken> implements DimLayer.DimLayerUser {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080044 static final String TAG = TAG_WITH_CLASS_NAME ? "Task" : TAG_WM;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070045 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
46 static final int BOUNDS_CHANGE_NONE = 0;
47 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
48 static final int BOUNDS_CHANGE_POSITION = 1;
49 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
50 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
51
Wale Ogunwalef6192862016-09-10 13:42:30 -070052 // TODO: Track parent marks like this in WindowContainer.
Craig Mautnerc00204b2013-03-05 15:02:14 -080053 TaskStack mStack;
Craig Mautner83162a92015-01-26 14:43:30 -080054 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070055 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080056 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080057 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080058
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070059 // Content limits relative to the DisplayContent this sits in.
60 private Rect mBounds = new Rect();
Jorim Jaggi0429f352015-12-22 16:29:16 +010061 final Rect mPreparedFrozenBounds = new Rect();
Jorim Jaggi26c8c422016-05-09 19:57:25 -070062 final Configuration mPreparedFrozenMergedConfig = new Configuration();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070063
Jorim Jaggidc249c42015-12-15 14:57:31 -080064 // Bounds used to calculate the insets.
65 private final Rect mTempInsetBounds = new Rect();
66
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070067 // Device rotation as of the last time {@link #mBounds} was set.
68 int mRotation;
69
70 // Whether mBounds is fullscreen
71 private boolean mFullscreen = true;
72
Andrii Kulian8072d112016-09-16 11:11:01 -070073 /**
74 * Contains configurations settings that are different from the parent configuration due to
75 * stack specific operations. E.g. {@link #setBounds}.
76 */
Jorim Jaggi26c8c422016-05-09 19:57:25 -070077 Configuration mOverrideConfig = Configuration.EMPTY;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070078
79 // For comparison with DisplayContent bounds.
80 private Rect mTmpRect = new Rect();
81 // For handling display rotations.
82 private Rect mTmpRect2 = new Rect();
83
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080084 // Resize mode of the task. See {@link ActivityInfo#resizeMode}
85 private int mResizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -080086
Chong Zhang3005e752015-09-18 18:46:28 -070087 // Whether the task is currently being drag-resized
88 private boolean mDragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010089 private int mDragResizeMode;
Chong Zhang3005e752015-09-18 18:46:28 -070090
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080091 private boolean mHomeTask;
92
Jiaquan Hedd1e66f2016-06-15 15:15:12 -070093 // Whether this task is an on-top launcher task, which is determined by the root activity.
94 private boolean mIsOnTopLauncher;
95
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070096 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
Andrii Kulian8072d112016-09-16 11:11:01 -070097 Configuration overrideConfig, boolean isOnTopLauncher) {
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;
Jiaquan Hedd1e66f2016-06-15 15:15:12 -0700102 mIsOnTopLauncher = isOnTopLauncher;
Andrii Kulian8072d112016-09-16 11:11:01 -0700103 setBounds(bounds, overrideConfig);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800104 }
105
106 DisplayContent getDisplayContent() {
107 return mStack.getDisplayContent();
108 }
109
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800110 void addAppToken(int addPos, AppWindowToken wtoken, int resizeMode, boolean homeTask) {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700111 final int lastPos = mChildren.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800112 if (addPos >= lastPos) {
113 addPos = lastPos;
114 } else {
115 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700116 if (mChildren.get(pos).removed) {
Craig Mautner83162a92015-01-26 14:43:30 -0800117 // addPos assumes removed tokens are actually gone.
118 ++addPos;
119 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700120 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800121 }
Wale Ogunwalef6192862016-09-10 13:42:30 -0700122
123 if (wtoken.mParent != null) {
124 wtoken.mParent.removeChild(wtoken);
125 }
126 addChild(wtoken, addPos);
Craig Mautner83162a92015-01-26 14:43:30 -0800127 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800128 mDeferRemoval = false;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800129 mResizeMode = resizeMode;
130 mHomeTask = homeTask;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800131 }
132
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700133 private boolean hasWindowsAlive() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700134 for (int i = mChildren.size() - 1; i >= 0; i--) {
135 if (mChildren.get(i).hasWindowsAlive()) {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800136 return true;
137 }
138 }
139 return false;
140 }
141
Wale Ogunwalef6192862016-09-10 13:42:30 -0700142 @Override
143 void removeIfPossible() {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700144 if (hasWindowsAlive() && mStack.isAnimating()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800145 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800146 mDeferRemoval = true;
147 return;
148 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800149 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800150 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800151 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700152 DisplayContent content = getDisplayContent();
153 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800154 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700155 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800156 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800157 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800158 }
159
Wale Ogunwalef6192862016-09-10 13:42:30 -0700160 // Change to use reparenting in WC when TaskStack is switched to use WC.
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800161 void moveTaskToStack(TaskStack stack, boolean toTop) {
162 if (stack == mStack) {
163 return;
164 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800165 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800166 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700167 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800168 if (mStack != null) {
169 mStack.removeTask(this);
170 }
171 stack.addTask(this, toTop);
172 }
173
Wale Ogunwale935e5022015-11-10 12:36:10 -0800174 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700175 if (mStack != null && stack != mStack) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800176 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700177 + " from stack=" + mStack);
178 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
179 mStack.removeTask(this);
180 }
181 stack.positionTask(this, position, showForAllUsers());
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800182 resizeLocked(bounds, config, false /* force */);
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800183
Wale Ogunwalef6192862016-09-10 13:42:30 -0700184 for (int activityNdx = mChildren.size() - 1; activityNdx >= 0; --activityNdx) {
185 mChildren.get(activityNdx).notifyMovedInStack();
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800186 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700187 }
188
Wale Ogunwalef6192862016-09-10 13:42:30 -0700189 void removeChild(AppWindowToken token) {
190 if (!mChildren.contains(token)) {
191 Slog.e(TAG, "removeChild: token=" + this + " not found.");
192 return;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700193 }
194
Wale Ogunwalef6192862016-09-10 13:42:30 -0700195 super.removeChild(token);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700196
Wale Ogunwalef6192862016-09-10 13:42:30 -0700197 if (mChildren.isEmpty()) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100198 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800199 if (mDeferRemoval) {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700200 removeIfPossible();
Craig Mautnere3119b72015-01-20 15:02:36 -0800201 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800202 }
Wale Ogunwalef6192862016-09-10 13:42:30 -0700203 token.mTask = null;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800204 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800205
Craig Mautnercbd84af2014-10-22 13:21:22 -0700206 void setSendingToBottom(boolean toBottom) {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700207 for (int appTokenNdx = 0; appTokenNdx < mChildren.size(); appTokenNdx++) {
208 mChildren.get(appTokenNdx).sendingToBottom = toBottom;
Craig Mautnercbd84af2014-10-22 13:21:22 -0700209 }
210 }
211
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700212 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Andrii Kulian8072d112016-09-16 11:11:01 -0700213 private int setBounds(Rect bounds, Configuration overrideConfig) {
214 if (overrideConfig == null) {
215 overrideConfig = Configuration.EMPTY;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700216 }
Andrii Kulian8072d112016-09-16 11:11:01 -0700217 if (bounds == null && !Configuration.EMPTY.equals(overrideConfig)) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700218 throw new IllegalArgumentException("null bounds but non empty configuration: "
Andrii Kulian8072d112016-09-16 11:11:01 -0700219 + overrideConfig);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700220 }
Andrii Kulian8072d112016-09-16 11:11:01 -0700221 if (bounds != null && Configuration.EMPTY.equals(overrideConfig)) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700222 throw new IllegalArgumentException("non null bounds, but empty configuration");
223 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700224 boolean oldFullscreen = mFullscreen;
225 int rotation = Surface.ROTATION_0;
226 final DisplayContent displayContent = mStack.getDisplayContent();
227 if (displayContent != null) {
228 displayContent.getLogicalDisplayRect(mTmpRect);
229 rotation = displayContent.getDisplayInfo().rotation;
Jorim Jaggi067e8172016-02-03 18:24:12 -0800230 mFullscreen = bounds == null;
231 if (mFullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700232 bounds = mTmpRect;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700233 }
234 }
235
236 if (bounds == null) {
237 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700238 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700239 }
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700240 if (mBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700241 return BOUNDS_CHANGE_NONE;
242 }
243
244 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700245 if (mBounds.left != bounds.left || mBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700246 boundsChange |= BOUNDS_CHANGE_POSITION;
247 }
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700248 if (mBounds.width() != bounds.width() || mBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700249 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700250 }
251
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700252 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800253
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700254 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700255 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800256 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700257 }
Andrii Kulian8072d112016-09-16 11:11:01 -0700258 mOverrideConfig = mFullscreen ? Configuration.EMPTY : overrideConfig;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700259 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700260 }
261
Jorim Jaggidc249c42015-12-15 14:57:31 -0800262 /**
263 * Sets the bounds used to calculate the insets. See
264 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
265 */
266 void setTempInsetBounds(Rect tempInsetBounds) {
267 if (tempInsetBounds != null) {
268 mTempInsetBounds.set(tempInsetBounds);
269 } else {
270 mTempInsetBounds.setEmpty();
271 }
272 }
273
274 /**
275 * Gets the bounds used to calculate the insets. See
276 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
277 */
278 void getTempInsetBounds(Rect out) {
279 out.set(mTempInsetBounds);
280 }
281
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800282 void setResizeable(int resizeMode) {
283 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800284 }
285
286 boolean isResizeable() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800287 return !mHomeTask
288 && (ActivityInfo.isResizeableMode(mResizeMode) || mService.mForceResizableTasks);
289 }
290
Jiaquan Hedd1e66f2016-06-15 15:15:12 -0700291 boolean isOnTopLauncher() {
292 return mIsOnTopLauncher;
293 }
294
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800295 boolean cropWindowsToStackBounds() {
296 return !mHomeTask && (isResizeable() || mResizeMode == RESIZE_MODE_CROP_WINDOWS);
297 }
298
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800299 boolean isHomeTask() {
300 return mHomeTask;
301 }
302
Andrii Kulian8072d112016-09-16 11:11:01 -0700303 boolean resizeLocked(Rect bounds, Configuration overrideConfig, boolean forced) {
304 int boundsChanged = setBounds(bounds, overrideConfig);
Chong Zhang87b21722015-09-21 15:39:51 -0700305 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700306 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700307 }
308 if (boundsChanged == BOUNDS_CHANGE_NONE) {
309 return false;
310 }
311 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700312 onResize();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800313 } else {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700314 onMovedByResize();
Chong Zhang3005e752015-09-18 18:46:28 -0700315 }
316 return true;
317 }
318
Jorim Jaggi0429f352015-12-22 16:29:16 +0100319 /**
320 * Prepares the task bounds to be frozen with the current size. See
321 * {@link AppWindowToken#freezeBounds}.
322 */
323 void prepareFreezingBounds() {
324 mPreparedFrozenBounds.set(mBounds);
Andrii Kulian8072d112016-09-16 11:11:01 -0700325 mPreparedFrozenMergedConfig.setTo(mService.mGlobalConfiguration);
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700326 mPreparedFrozenMergedConfig.updateFrom(mOverrideConfig);
Jorim Jaggi0429f352015-12-22 16:29:16 +0100327 }
328
Chong Zhang5117e272016-05-03 12:47:34 -0700329 /**
330 * Align the task to the adjusted bounds.
331 *
332 * @param adjustedBounds Adjusted bounds to which the task should be aligned.
333 * @param tempInsetBounds Insets bounds for the task.
334 * @param alignBottom True if the task's bottom should be aligned to the adjusted
335 * bounds's bottom; false if the task's top should be aligned
336 * the adjusted bounds's top.
337 */
338 void alignToAdjustedBounds(
339 Rect adjustedBounds, Rect tempInsetBounds, boolean alignBottom) {
340 if (!isResizeable() || mOverrideConfig == Configuration.EMPTY) {
341 return;
342 }
343
344 getBounds(mTmpRect2);
345 if (alignBottom) {
346 int offsetY = adjustedBounds.bottom - mTmpRect2.bottom;
347 mTmpRect2.offset(0, offsetY);
348 } else {
349 mTmpRect2.offsetTo(adjustedBounds.left, adjustedBounds.top);
350 }
351 setTempInsetBounds(tempInsetBounds);
352 resizeLocked(mTmpRect2, mOverrideConfig, false /* forced */);
353 }
354
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700355 /** Return true if the current bound can get outputted to the rest of the system as-is. */
356 private boolean useCurrentBounds() {
357 final DisplayContent displayContent = mStack.getDisplayContent();
Wale Ogunwalef6192862016-09-10 13:42:30 -0700358 return mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700359 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700360 || displayContent == null
Wale Ogunwalef6192862016-09-10 13:42:30 -0700361 || displayContent.getDockedStackVisibleForUserLocked() != null;
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700362 }
363
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800364 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800365 void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700366 if (useCurrentBounds()) {
367 // No need to adjust the output bounds if fullscreen or the docked stack is visible
368 // since it is already what we want to represent to the rest of the system.
369 out.set(mBounds);
370 return;
371 }
372
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800373 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
374 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700375 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700376 }
377
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800378 /**
379 * Calculate the maximum visible area of this task. If the task has only one app,
380 * the result will be visible frame of that app. If the task has more than one apps,
381 * we search from top down if the next app got different visible area.
382 *
383 * This effort is to handle the case where some task (eg. GMail composer) might pop up
384 * a dialog that's different in size from the activity below, in which case we should
385 * be dimming the entire task area behind the dialog.
386 *
387 * @param out Rect containing the max visible bounds.
388 * @return true if the task has some visible app windows; false otherwise.
389 */
390 boolean getMaxVisibleBounds(Rect out) {
391 boolean foundTop = false;
Wale Ogunwalef6192862016-09-10 13:42:30 -0700392 for (int i = mChildren.size() - 1; i >= 0; i--) {
393 final AppWindowToken token = mChildren.get(i);
Chong Zhangd8ceb852015-11-11 14:53:41 -0800394 // skip hidden (or about to hide) apps
395 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
396 continue;
397 }
398 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800399 if (win == null) {
400 continue;
401 }
402 if (!foundTop) {
403 out.set(win.mVisibleFrame);
404 foundTop = true;
405 continue;
406 }
407 if (win.mVisibleFrame.left < out.left) {
408 out.left = win.mVisibleFrame.left;
409 }
410 if (win.mVisibleFrame.top < out.top) {
411 out.top = win.mVisibleFrame.top;
412 }
413 if (win.mVisibleFrame.right > out.right) {
414 out.right = win.mVisibleFrame.right;
415 }
416 if (win.mVisibleFrame.bottom > out.bottom) {
417 out.bottom = win.mVisibleFrame.bottom;
418 }
419 }
420 return foundTop;
421 }
422
423 /** Bounds of the task to be used for dimming, as well as touch related tests. */
424 @Override
425 public void getDimBounds(Rect out) {
Robert Carra86a6bf2016-04-08 17:34:16 -0700426 final DisplayContent displayContent = mStack.getDisplayContent();
427 // It doesn't matter if we in particular are part of the resize, since we couldn't have
428 // a DimLayer anyway if we weren't visible.
Wale Ogunwalef6192862016-09-10 13:42:30 -0700429 final boolean dockedResizing = displayContent != null
430 && displayContent.mDividerControllerLocked.isResizing();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800431 if (useCurrentBounds()) {
432 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
433 return;
434 }
435
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700436 if (!mFullscreen) {
437 // When minimizing the docked stack when going home, we don't adjust the task bounds
438 // so we need to intersect the task bounds with the stack bounds here.
Robert Carra86a6bf2016-04-08 17:34:16 -0700439 //
440 // If we are Docked Resizing with snap points, the task bounds could be smaller than the stack
441 // bounds and so we don't even want to use them. Even if the app should not be resized the Dim
442 // should keep up with the divider.
443 if (dockedResizing) {
444 mStack.getBounds(out);
445 } else {
446 mStack.getBounds(mTmpRect);
447 mTmpRect.intersect(mBounds);
448 }
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700449 out.set(mTmpRect);
450 } else {
451 out.set(mBounds);
452 }
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800453 return;
454 }
455
Wale Ogunwalef6192862016-09-10 13:42:30 -0700456 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
457 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
458 if (displayContent != null) {
459 displayContent.getLogicalDisplayRect(out);
460 }
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800461 }
462
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100463 void setDragResizing(boolean dragResizing, int dragResizeMode) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800464 if (mDragResizing != dragResizing) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100465 if (!DragResizeMode.isModeAllowedForStack(mStack.mStackId, dragResizeMode)) {
466 throw new IllegalArgumentException("Drag resize mode not allow for stack stackId="
467 + mStack.mStackId + " dragResizeMode=" + dragResizeMode);
468 }
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800469 mDragResizing = dragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100470 mDragResizeMode = dragResizeMode;
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800471 resetDragResizingChangeReported();
472 }
473 }
474
Chong Zhang3005e752015-09-18 18:46:28 -0700475 boolean isDragResizing() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700476 return mDragResizing;
Chong Zhang3005e752015-09-18 18:46:28 -0700477 }
478
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100479 int getDragResizeMode() {
480 return mDragResizeMode;
481 }
482
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700483 void updateDisplayInfo(final DisplayContent displayContent) {
484 if (displayContent == null) {
485 return;
486 }
487 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700488 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700489 return;
490 }
491 final int newRotation = displayContent.getDisplayInfo().rotation;
492 if (mRotation == newRotation) {
493 return;
494 }
495
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800496 // Device rotation changed.
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700497 // - We don't want the task to move around on the screen when this happens, so update the
498 // task bounds so it stays in the same place.
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800499 // - Rotate the bounds and notify activity manager if the task can be resized independently
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700500 // from its stack. The stack will take care of task rotation for the other case.
501 mTmpRect2.set(mBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800502
503 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
504 setBounds(mTmpRect2, mOverrideConfig);
505 return;
506 }
507
Wale Ogunwale94744212015-09-21 19:01:47 -0700508 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700509 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800510 // Post message to inform activity manager of the bounds change simulating a one-way
511 // call. We do this to prevent a deadlock between window manager lock and activity
512 // manager lock been held.
513 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700514 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700515 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700516 }
517
Wale Ogunwalef6192862016-09-10 13:42:30 -0700518 /** Cancels any running app transitions associated with the task. */
Winsonc28098f2015-10-30 14:50:19 -0700519 void cancelTaskWindowTransition() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700520 for (int i = mChildren.size() - 1; i >= 0; --i) {
521 mChildren.get(i).mAppAnimator.clearAnimation();
Winsonc28098f2015-10-30 14:50:19 -0700522 }
523 }
524
Wale Ogunwalef6192862016-09-10 13:42:30 -0700525 /** Cancels any running thumbnail transitions associated with the task. */
Winson13d30662015-11-06 15:30:29 -0800526 void cancelTaskThumbnailTransition() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700527 for (int i = mChildren.size() - 1; i >= 0; --i) {
528 mChildren.get(i).mAppAnimator.clearThumbnail();
Winson13d30662015-11-06 15:30:29 -0800529 }
530 }
531
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700532 boolean showForAllUsers() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700533 final int tokensCount = mChildren.size();
534 return (tokensCount != 0) && mChildren.get(tokensCount - 1).showForAllUsers;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700535 }
536
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700537 boolean inHomeStack() {
538 return mStack != null && mStack.mStackId == HOME_STACK_ID;
539 }
540
Chong Zhang09b21ef2015-09-14 10:20:21 -0700541 boolean inFreeformWorkspace() {
542 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
543 }
544
Robert Carr03138452016-05-18 14:53:16 -0700545 boolean inPinnedWorkspace() {
546 return mStack != null && mStack.mStackId == PINNED_STACK_ID;
547 }
548
Robert Carre6275582016-02-29 15:45:45 -0800549 boolean isFloating() {
550 return StackId.tasksAreFloating(mStack.mStackId);
551 }
552
Chong Zhangd8ceb852015-11-11 14:53:41 -0800553 WindowState getTopVisibleAppMainWindow() {
554 final AppWindowToken token = getTopVisibleAppToken();
555 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700556 }
557
Chong Zhangd8ceb852015-11-11 14:53:41 -0800558 AppWindowToken getTopVisibleAppToken() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700559 for (int i = mChildren.size() - 1; i >= 0; i--) {
560 final AppWindowToken token = mChildren.get(i);
Chong Zhangd8ceb852015-11-11 14:53:41 -0800561 // skip hidden (or about to hide) apps
562 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
563 return token;
564 }
565 }
566 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700567 }
568
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800569 @Override
Wale Ogunwale29bfbb82016-05-12 15:13:52 -0700570 public boolean dimFullscreen() {
571 return isHomeTask() || isFullscreen();
572 }
573
574 boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700575 if (useCurrentBounds()) {
576 return mFullscreen;
577 }
578 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
579 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
580 // system.
581 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700582 }
583
584 @Override
585 public DisplayInfo getDisplayInfo() {
586 return mStack.getDisplayContent().getDisplayInfo();
587 }
588
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700589 void forceWindowsScaleable(boolean force) {
590 SurfaceControl.openTransaction();
591 try {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700592 for (int i = mChildren.size() - 1; i >= 0; i--) {
593 mChildren.get(i).forceWindowsScaleableInTransaction(force);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700594 }
595 } finally {
596 SurfaceControl.closeTransaction();
597 }
598 }
599
Wale Ogunwaleec731152016-09-08 20:18:57 -0700600 void getWindowOnDisplayBeforeToken(DisplayContent dc, WindowToken token,
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700601 DisplayContent.GetWindowOnDisplaySearchResult result) {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700602 for (int i = mChildren.size() - 1; i >= 0; --i) {
603 final AppWindowToken current = mChildren.get(i);
Wale Ogunwaleec731152016-09-08 20:18:57 -0700604 if (current == token) {
605 // We have reach the token we are interested in. End search.
606 result.reachedToken = true;
607 return;
608 }
609
610 // We haven't reached the token yet; if this token is not going to the bottom and
611 // has windows on this display, then it is a candidate for what we are looking for.
612 final WindowList tokenWindowList = dc.getTokenWindowsOnDisplay(current);
613 if (!current.sendingToBottom && tokenWindowList.size() > 0) {
614 result.foundWindow = tokenWindowList.get(0);
615 }
616 }
617 }
618
619 void getWindowOnDisplayAfterToken(DisplayContent dc, WindowToken token,
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700620 DisplayContent.GetWindowOnDisplaySearchResult result) {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700621 for (int i = mChildren.size() - 1; i >= 0; --i) {
622 final AppWindowToken current = mChildren.get(i);
Wale Ogunwaleec731152016-09-08 20:18:57 -0700623 if (!result.reachedToken) {
624 if (current == token) {
625 // We have reached the token we are interested in. Get whichever window occurs
626 // after it that is on the same display.
627 result.reachedToken = true;
628 }
629 continue;
630 }
631
632 final WindowList tokenWindowList = dc.getTokenWindowsOnDisplay(current);
633 if (tokenWindowList.size() > 0) {
634 result.foundWindow = tokenWindowList.get(tokenWindowList.size() - 1);
635 return;
636 }
637 }
638 }
639
Wale Ogunwalef6192862016-09-10 13:42:30 -0700640 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700641 boolean fillsParent() {
642 return mFullscreen || !StackId.isTaskResizeAllowed(mStack.mStackId);
643 }
644
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700645 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800646 public String toString() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700647 return "{taskId=" + mTaskId + " appTokens=" + mChildren + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800648 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700649
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700650 String getName() {
651 return toShortString();
652 }
653
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700654 @Override
655 public String toShortString() {
656 return "Task=" + mTaskId;
657 }
658
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800659 public void dump(String prefix, PrintWriter pw) {
660 final String doublePrefix = prefix + " ";
661
662 pw.println(prefix + "taskId=" + mTaskId);
663 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
664 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
665 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
Wale Ogunwalef6192862016-09-10 13:42:30 -0700666 pw.println(doublePrefix + "appTokens=" + mChildren);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800667 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
668
669 final String triplePrefix = doublePrefix + " ";
670
Wale Ogunwalef6192862016-09-10 13:42:30 -0700671 for (int i = mChildren.size() - 1; i >= 0; i--) {
672 final AppWindowToken wtoken = mChildren.get(i);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800673 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
674 wtoken.dump(pw, triplePrefix);
675 }
676
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700677 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800678}