blob: 244eb66a7ef07ac018ab68688987a1d5570ced4a [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;
skuhne@google.com322347b2016-12-02 12:54:03 -080020import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY;
Jason Monkba53d8a2017-04-06 18:28:19 +000021import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020022import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION;
Bryce Lee61fbcbc2017-03-10 14:14:03 -080023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
Wale Ogunwale68278562017-09-23 17:13:55 -070024import static android.content.res.Configuration.EMPTY;
Winson Chungbe9be7f2017-06-28 10:55:22 -070025
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080026import static com.android.server.EventLogTags.WM_TASK_REMOVED;
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;
Steven Timotiusaf03df62017-07-18 16:56:43 -070030import static com.android.server.wm.proto.TaskProto.APP_WINDOW_TOKENS;
31import static com.android.server.wm.proto.TaskProto.BOUNDS;
32import static com.android.server.wm.proto.TaskProto.FILLS_PARENT;
33import static com.android.server.wm.proto.TaskProto.ID;
34import static com.android.server.wm.proto.TaskProto.TEMP_INSET_BOUNDS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070035import static com.android.server.wm.proto.TaskProto.WINDOW_CONTAINER;
Wale Ogunwale99db1862015-10-23 20:08:22 -070036
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070037import android.annotation.CallSuper;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010038import android.app.ActivityManager.TaskDescription;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080039import android.content.pm.ActivityInfo;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070040import android.content.res.Configuration;
41import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080042import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080043import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -070044import android.util.proto.ProtoOutputStream;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070045import android.view.Surface;
46
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080047import com.android.internal.annotations.VisibleForTesting;
Craig Mautner2c2549c2013-11-12 08:31:15 -080048
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070049import java.io.PrintWriter;
Jorim Jaggi51304d72017-05-17 17:25:32 +020050import java.util.function.Consumer;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070051
Robert Carrf59b8dd2017-10-02 18:58:36 -070052class Task extends WindowContainer<AppWindowToken> {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080053 static final String TAG = TAG_WITH_CLASS_NAME ? "Task" : TAG_WM;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070054
Wale Ogunwalef6192862016-09-10 13:42:30 -070055 // TODO: Track parent marks like this in WindowContainer.
Craig Mautnerc00204b2013-03-05 15:02:14 -080056 TaskStack mStack;
Craig Mautner83162a92015-01-26 14:43:30 -080057 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070058 final int mUserId;
Wale Ogunwale3eadad72016-10-13 09:16:59 -070059 private boolean mDeferRemoval = false;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080060
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.
Wale Ogunwale3eadad72016-10-13 09:16:59 -070068 private int mRotation;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070069
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070070 // For comparison with DisplayContent bounds.
71 private Rect mTmpRect = new Rect();
72 // For handling display rotations.
73 private Rect mTmpRect2 = new Rect();
Bryce Leef3c6a472017-11-14 14:53:06 -080074 // For retrieving dim bounds
75 private Rect mTmpRect3 = new Rect();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070076
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080077 // Resize mode of the task. See {@link ActivityInfo#resizeMode}
78 private int mResizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -080079
Winson Chungd3395382016-12-13 11:49:09 -080080 // Whether the task supports picture-in-picture.
81 // See {@link ActivityInfo#FLAG_SUPPORTS_PICTURE_IN_PICTURE}
82 private boolean mSupportsPictureInPicture;
83
Chong Zhang3005e752015-09-18 18:46:28 -070084 // Whether the task is currently being drag-resized
85 private boolean mDragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010086 private int mDragResizeMode;
Chong Zhang3005e752015-09-18 18:46:28 -070087
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010088 private TaskDescription mTaskDescription;
89
Robert Carr18f622f2017-05-08 11:20:43 -070090 // If set to true, the task will report that it is not in the floating
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070091 // state regardless of it's stack affiliation. As the floating state drives
Robert Carr18f622f2017-05-08 11:20:43 -070092 // production of content insets this can be used to preserve them across
93 // stack moves and we in fact do so when moving from full screen to pinned.
94 private boolean mPreserveNonFloatingState = false;
95
Robert Carrf59b8dd2017-10-02 18:58:36 -070096 private Dimmer mDimmer = new Dimmer(this);
97 private final Rect mTmpDimBoundsRect = new Rect();
98
Bryce Leef3c6a472017-11-14 14:53:06 -080099 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, int resizeMode,
100 boolean supportsPictureInPicture, TaskDescription taskDescription,
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700101 TaskWindowContainerController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100102 super(service);
Craig Mautner83162a92015-01-26 14:43:30 -0800103 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800104 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -0700105 mUserId = userId;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800106 mResizeMode = resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800107 mSupportsPictureInPicture = supportsPictureInPicture;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800108 setController(controller);
Bryce Leef3c6a472017-11-14 14:53:06 -0800109 setBounds(getOverrideBounds());
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100110 mTaskDescription = taskDescription;
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800111
112 // Tasks have no set orientation value (including SCREEN_ORIENTATION_UNSPECIFIED).
113 setOrientation(SCREEN_ORIENTATION_UNSET);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800114 }
115
116 DisplayContent getDisplayContent() {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800117 return mStack != null ? mStack.getDisplayContent() : null;
118 }
119
Wale Ogunwale069bbd32017-02-03 07:58:14 -0800120 private int getAdjustedAddPosition(int suggestedPosition) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800121 final int size = mChildren.size();
122 if (suggestedPosition >= size) {
123 return Math.min(size, suggestedPosition);
124 }
125
126 for (int pos = 0; pos < size && pos < suggestedPosition; ++pos) {
127 // TODO: Confirm that this is the behavior we want long term.
128 if (mChildren.get(pos).removed) {
129 // suggestedPosition assumes removed tokens are actually gone.
130 ++suggestedPosition;
131 }
132 }
133 return Math.min(size, suggestedPosition);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800134 }
135
Wale Ogunwale72919d22016-12-08 18:58:50 -0800136 @Override
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800137 void addChild(AppWindowToken wtoken, int position) {
138 position = getAdjustedAddPosition(position);
139 super.addChild(wtoken, position);
Craig Mautner42bf39e2014-02-21 16:46:22 -0800140 mDeferRemoval = false;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800141 }
142
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800143 @Override
144 void positionChildAt(int position, AppWindowToken child, boolean includingParents) {
145 position = getAdjustedAddPosition(position);
146 super.positionChildAt(position, child, includingParents);
147 mDeferRemoval = false;
148 }
149
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700150 private boolean hasWindowsAlive() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700151 for (int i = mChildren.size() - 1; i >= 0; i--) {
152 if (mChildren.get(i).hasWindowsAlive()) {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800153 return true;
154 }
155 }
156 return false;
157 }
158
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800159 @VisibleForTesting
160 boolean shouldDeferRemoval() {
Wale Ogunwale2c9f2032017-06-02 06:50:50 -0700161 // TODO: This should probably return false if mChildren.isEmpty() regardless if the stack
162 // is animating...
Jorim Jaggia5e10572017-11-15 14:36:26 +0100163 return hasWindowsAlive() && mStack.isSelfOrChildAnimating();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800164 }
165
Wale Ogunwalef6192862016-09-10 13:42:30 -0700166 @Override
167 void removeIfPossible() {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800168 if (shouldDeferRemoval()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800169 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800170 mDeferRemoval = true;
171 return;
172 }
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800173 removeImmediately();
174 }
175
176 @Override
177 void removeImmediately() {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800178 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800179 EventLog.writeEvent(WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800180 mDeferRemoval = false;
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800181
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800182 super.removeImmediately();
Craig Mautnere3119b72015-01-20 15:02:36 -0800183 }
184
Wale Ogunwale2719cc12017-04-14 09:45:27 -0700185 void reparent(TaskStack stack, int position, boolean moveParents) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800186 if (stack == mStack) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800187 throw new IllegalArgumentException(
188 "task=" + this + " already child of stack=" + mStack);
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800189 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800190 if (DEBUG_STACK) Slog.i(TAG, "reParentTask: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800191 + " from stack=" + mStack);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800192 EventLog.writeEvent(WM_TASK_REMOVED, mTaskId, "reParentTask");
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -0800193 final DisplayContent prevDisplayContent = getDisplayContent();
194
Robert Carr18f622f2017-05-08 11:20:43 -0700195 // If we are moving from the fullscreen stack to the pinned stack
196 // then we want to preserve our insets so that there will not
197 // be a jump in the area covered by system decorations. We rely
198 // on the pinned animation to later unset this value.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700199 if (stack.inPinnedWindowingMode()) {
Robert Carr18f622f2017-05-08 11:20:43 -0700200 mPreserveNonFloatingState = true;
201 } else {
202 mPreserveNonFloatingState = false;
203 }
204
Andrii Kulian441e4492016-09-29 15:25:00 -0700205 getParent().removeChild(this);
Wale Ogunwale2719cc12017-04-14 09:45:27 -0700206 stack.addTask(this, position, showForAllUsers(), moveParents);
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -0800207
208 // Relayout display(s).
209 final DisplayContent displayContent = stack.getDisplayContent();
210 displayContent.setLayoutNeeded();
211 if (prevDisplayContent != displayContent) {
212 onDisplayChanged(displayContent);
213 prevDisplayContent.setLayoutNeeded();
214 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800215 }
216
Andrii Kuliand2765632016-12-12 22:26:34 -0800217 /** @see com.android.server.am.ActivityManagerService#positionTaskInStack(int, int, int). */
Bryce Leef3c6a472017-11-14 14:53:06 -0800218 void positionAt(int position) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800219 mStack.positionChildAt(position, this, false /* includingParents */);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700220 }
221
Wale Ogunwale14a3fb92016-09-11 15:19:05 -0700222 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -0800223 void onParentSet() {
Robert Carrb1579c82017-09-05 14:54:47 -0700224 super.onParentSet();
225
Andrii Kuliand2765632016-12-12 22:26:34 -0800226 // Update task bounds if needed.
227 updateDisplayInfo(getDisplayContent());
228
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700229 if (getWindowConfiguration().windowsAreScaleable()) {
Andrii Kuliand2765632016-12-12 22:26:34 -0800230 // We force windows out of SCALING_MODE_FREEZE so that we can continue to animate them
231 // while a resize is pending.
232 forceWindowsScaleable(true /* force */);
233 } else {
234 forceWindowsScaleable(false /* force */);
235 }
236 }
237
238 @Override
Wale Ogunwalef6192862016-09-10 13:42:30 -0700239 void removeChild(AppWindowToken token) {
240 if (!mChildren.contains(token)) {
241 Slog.e(TAG, "removeChild: token=" + this + " not found.");
242 return;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700243 }
244
Wale Ogunwalef6192862016-09-10 13:42:30 -0700245 super.removeChild(token);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700246
Wale Ogunwalef6192862016-09-10 13:42:30 -0700247 if (mChildren.isEmpty()) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800248 EventLog.writeEvent(WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800249 if (mDeferRemoval) {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700250 removeIfPossible();
Craig Mautnere3119b72015-01-20 15:02:36 -0800251 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800252 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800253 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800254
Craig Mautnercbd84af2014-10-22 13:21:22 -0700255 void setSendingToBottom(boolean toBottom) {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700256 for (int appTokenNdx = 0; appTokenNdx < mChildren.size(); appTokenNdx++) {
257 mChildren.get(appTokenNdx).sendingToBottom = toBottom;
Craig Mautnercbd84af2014-10-22 13:21:22 -0700258 }
259 }
260
Bryce Leef3c6a472017-11-14 14:53:06 -0800261 public int setBounds(Rect bounds, boolean forceResize) {
262 final int boundsChanged = setBounds(bounds);
263
264 if (forceResize && (boundsChanged & BOUNDS_CHANGE_SIZE) != BOUNDS_CHANGE_SIZE) {
265 onResize();
266 return BOUNDS_CHANGE_SIZE | boundsChanged;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700267 }
Wale Ogunwale68278562017-09-23 17:13:55 -0700268
Bryce Leef3c6a472017-11-14 14:53:06 -0800269 return boundsChanged;
270 }
271
272 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
273 @Override
274 public int setBounds(Rect bounds) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700275 int rotation = Surface.ROTATION_0;
276 final DisplayContent displayContent = mStack.getDisplayContent();
277 if (displayContent != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700278 rotation = displayContent.getDisplayInfo().rotation;
Bryce Leef3c6a472017-11-14 14:53:06 -0800279 } else if (bounds == null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700280 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700281 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700282 }
Bryce Leef3c6a472017-11-14 14:53:06 -0800283
284 if (equivalentOverrideBounds(bounds)) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700285 return BOUNDS_CHANGE_NONE;
286 }
287
Bryce Leef3c6a472017-11-14 14:53:06 -0800288 final int boundsChange = super.setBounds(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800289
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700290 mRotation = rotation;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700291
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700292 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700293 }
294
Jorim Jaggidc249c42015-12-15 14:57:31 -0800295 /**
296 * Sets the bounds used to calculate the insets. See
297 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
298 */
299 void setTempInsetBounds(Rect tempInsetBounds) {
300 if (tempInsetBounds != null) {
301 mTempInsetBounds.set(tempInsetBounds);
302 } else {
303 mTempInsetBounds.setEmpty();
304 }
305 }
306
307 /**
308 * Gets the bounds used to calculate the insets. See
309 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
310 */
311 void getTempInsetBounds(Rect out) {
312 out.set(mTempInsetBounds);
313 }
314
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800315 void setResizeable(int resizeMode) {
316 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800317 }
318
319 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -0800320 return ActivityInfo.isResizeableMode(mResizeMode) || mSupportsPictureInPicture
321 || mService.mForceResizableTasks;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800322 }
323
skuhne@google.com322347b2016-12-02 12:54:03 -0800324 /**
325 * Tests if the orientation should be preserved upon user interactive resizig operations.
326
327 * @return true if orientation should not get changed upon resizing operation.
328 */
329 boolean preserveOrientationOnResize() {
330 return mResizeMode == RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY
331 || mResizeMode == RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY
332 || mResizeMode == RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION;
333 }
334
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800335 boolean cropWindowsToStackBounds() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700336 return isResizeable();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800337 }
338
Jorim Jaggi0429f352015-12-22 16:29:16 +0100339 /**
340 * Prepares the task bounds to be frozen with the current size. See
341 * {@link AppWindowToken#freezeBounds}.
342 */
343 void prepareFreezingBounds() {
Bryce Leef3c6a472017-11-14 14:53:06 -0800344 mPreparedFrozenBounds.set(getBounds());
Andrii Kulian441e4492016-09-29 15:25:00 -0700345 mPreparedFrozenMergedConfig.setTo(getConfiguration());
Jorim Jaggi0429f352015-12-22 16:29:16 +0100346 }
347
Chong Zhang5117e272016-05-03 12:47:34 -0700348 /**
349 * Align the task to the adjusted bounds.
350 *
351 * @param adjustedBounds Adjusted bounds to which the task should be aligned.
352 * @param tempInsetBounds Insets bounds for the task.
353 * @param alignBottom True if the task's bottom should be aligned to the adjusted
354 * bounds's bottom; false if the task's top should be aligned
355 * the adjusted bounds's top.
356 */
Andrii Kulian441e4492016-09-29 15:25:00 -0700357 void alignToAdjustedBounds(Rect adjustedBounds, Rect tempInsetBounds, boolean alignBottom) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700358 if (!isResizeable() || EMPTY.equals(getOverrideConfiguration())) {
Chong Zhang5117e272016-05-03 12:47:34 -0700359 return;
360 }
361
362 getBounds(mTmpRect2);
363 if (alignBottom) {
364 int offsetY = adjustedBounds.bottom - mTmpRect2.bottom;
365 mTmpRect2.offset(0, offsetY);
366 } else {
367 mTmpRect2.offsetTo(adjustedBounds.left, adjustedBounds.top);
368 }
369 setTempInsetBounds(tempInsetBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -0800370 setBounds(mTmpRect2, false /* forced */);
Chong Zhang5117e272016-05-03 12:47:34 -0700371 }
372
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700373 /** Return true if the current bound can get outputted to the rest of the system as-is. */
374 private boolean useCurrentBounds() {
Wale Ogunwale926aade2017-08-29 11:24:37 -0700375 final DisplayContent displayContent = getDisplayContent();
Bryce Leef3c6a472017-11-14 14:53:06 -0800376 return matchParentBounds()
Wale Ogunwale926aade2017-08-29 11:24:37 -0700377 || !inSplitScreenSecondaryWindowingMode()
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700378 || displayContent == null
Matthew Ng64e77cf2017-10-31 14:01:31 -0700379 || displayContent.getSplitScreenPrimaryStackIgnoringVisibility() != null;
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700380 }
381
Bryce Leef3c6a472017-11-14 14:53:06 -0800382 @Override
383 public void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700384 if (useCurrentBounds()) {
385 // No need to adjust the output bounds if fullscreen or the docked stack is visible
386 // since it is already what we want to represent to the rest of the system.
Bryce Leef3c6a472017-11-14 14:53:06 -0800387 super.getBounds(out);
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700388 return;
389 }
390
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800391 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
392 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Bryce Leef3c6a472017-11-14 14:53:06 -0800393 mStack.getDisplayContent().getBounds(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700394 }
395
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800396 /**
397 * Calculate the maximum visible area of this task. If the task has only one app,
398 * the result will be visible frame of that app. If the task has more than one apps,
399 * we search from top down if the next app got different visible area.
400 *
401 * This effort is to handle the case where some task (eg. GMail composer) might pop up
402 * a dialog that's different in size from the activity below, in which case we should
403 * be dimming the entire task area behind the dialog.
404 *
405 * @param out Rect containing the max visible bounds.
406 * @return true if the task has some visible app windows; false otherwise.
407 */
408 boolean getMaxVisibleBounds(Rect out) {
409 boolean foundTop = false;
Wale Ogunwalef6192862016-09-10 13:42:30 -0700410 for (int i = mChildren.size() - 1; i >= 0; i--) {
411 final AppWindowToken token = mChildren.get(i);
Chong Zhangd8ceb852015-11-11 14:53:41 -0800412 // skip hidden (or about to hide) apps
Wale Ogunwale89973222017-04-23 18:39:45 -0700413 if (token.mIsExiting || token.isClientHidden() || token.hiddenRequested) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800414 continue;
415 }
416 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800417 if (win == null) {
418 continue;
419 }
420 if (!foundTop) {
421 out.set(win.mVisibleFrame);
422 foundTop = true;
423 continue;
424 }
425 if (win.mVisibleFrame.left < out.left) {
426 out.left = win.mVisibleFrame.left;
427 }
428 if (win.mVisibleFrame.top < out.top) {
429 out.top = win.mVisibleFrame.top;
430 }
431 if (win.mVisibleFrame.right > out.right) {
432 out.right = win.mVisibleFrame.right;
433 }
434 if (win.mVisibleFrame.bottom > out.bottom) {
435 out.bottom = win.mVisibleFrame.bottom;
436 }
437 }
438 return foundTop;
439 }
440
441 /** Bounds of the task to be used for dimming, as well as touch related tests. */
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800442 public void getDimBounds(Rect out) {
Robert Carra86a6bf2016-04-08 17:34:16 -0700443 final DisplayContent displayContent = mStack.getDisplayContent();
444 // It doesn't matter if we in particular are part of the resize, since we couldn't have
445 // a DimLayer anyway if we weren't visible.
Wale Ogunwalef6192862016-09-10 13:42:30 -0700446 final boolean dockedResizing = displayContent != null
447 && displayContent.mDividerControllerLocked.isResizing();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800448 if (useCurrentBounds()) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700449 if (inFreeformWindowingMode() && getMaxVisibleBounds(out)) {
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800450 return;
451 }
452
Bryce Leef3c6a472017-11-14 14:53:06 -0800453 if (!matchParentBounds()) {
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700454 // When minimizing the docked stack when going home, we don't adjust the task bounds
455 // so we need to intersect the task bounds with the stack bounds here.
Robert Carra86a6bf2016-04-08 17:34:16 -0700456 //
457 // If we are Docked Resizing with snap points, the task bounds could be smaller than the stack
458 // bounds and so we don't even want to use them. Even if the app should not be resized the Dim
459 // should keep up with the divider.
460 if (dockedResizing) {
461 mStack.getBounds(out);
462 } else {
463 mStack.getBounds(mTmpRect);
Bryce Leef3c6a472017-11-14 14:53:06 -0800464 mTmpRect.intersect(getBounds());
Robert Carra86a6bf2016-04-08 17:34:16 -0700465 }
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700466 out.set(mTmpRect);
467 } else {
Bryce Leef3c6a472017-11-14 14:53:06 -0800468 out.set(getBounds());
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700469 }
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800470 return;
471 }
472
Wale Ogunwalef6192862016-09-10 13:42:30 -0700473 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
474 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
475 if (displayContent != null) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800476 displayContent.getBounds(out);
Wale Ogunwalef6192862016-09-10 13:42:30 -0700477 }
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800478 }
479
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100480 void setDragResizing(boolean dragResizing, int dragResizeMode) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800481 if (mDragResizing != dragResizing) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700482 if (!DragResizeMode.isModeAllowedForStack(mStack, dragResizeMode)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100483 throw new IllegalArgumentException("Drag resize mode not allow for stack stackId="
484 + mStack.mStackId + " dragResizeMode=" + dragResizeMode);
485 }
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800486 mDragResizing = dragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100487 mDragResizeMode = dragResizeMode;
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800488 resetDragResizingChangeReported();
489 }
490 }
491
Chong Zhang3005e752015-09-18 18:46:28 -0700492 boolean isDragResizing() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700493 return mDragResizing;
Chong Zhang3005e752015-09-18 18:46:28 -0700494 }
495
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100496 int getDragResizeMode() {
497 return mDragResizeMode;
498 }
499
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700500 void updateDisplayInfo(final DisplayContent displayContent) {
501 if (displayContent == null) {
502 return;
503 }
Bryce Leef3c6a472017-11-14 14:53:06 -0800504 if (matchParentBounds()) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700505 // TODO: Yeah...not sure if this works with WindowConfiguration, but shouldn't be a
506 // problem once we move mBounds into WindowConfiguration.
Bryce Leef3c6a472017-11-14 14:53:06 -0800507 setBounds(null);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700508 return;
509 }
510 final int newRotation = displayContent.getDisplayInfo().rotation;
511 if (mRotation == newRotation) {
512 return;
513 }
514
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800515 // Device rotation changed.
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700516 // - We don't want the task to move around on the screen when this happens, so update the
517 // task bounds so it stays in the same place.
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800518 // - Rotate the bounds and notify activity manager if the task can be resized independently
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700519 // from its stack. The stack will take care of task rotation for the other case.
Bryce Leef3c6a472017-11-14 14:53:06 -0800520 mTmpRect2.set(getBounds());
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800521
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700522 if (!getWindowConfiguration().canResizeTask()) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800523 setBounds(mTmpRect2);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800524 return;
525 }
526
Wale Ogunwale94744212015-09-21 19:01:47 -0700527 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Bryce Leef3c6a472017-11-14 14:53:06 -0800528 if (setBounds(mTmpRect2) != BOUNDS_CHANGE_NONE) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800529 final TaskWindowContainerController controller = getController();
530 if (controller != null) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800531 controller.requestResize(getBounds(), RESIZE_MODE_SYSTEM_SCREEN_ROTATION);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800532 }
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700533 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700534 }
535
Wale Ogunwalef6192862016-09-10 13:42:30 -0700536 /** Cancels any running app transitions associated with the task. */
Winsonc28098f2015-10-30 14:50:19 -0700537 void cancelTaskWindowTransition() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700538 for (int i = mChildren.size() - 1; i >= 0; --i) {
539 mChildren.get(i).mAppAnimator.clearAnimation();
Winsonc28098f2015-10-30 14:50:19 -0700540 }
541 }
542
Wale Ogunwalef6192862016-09-10 13:42:30 -0700543 /** Cancels any running thumbnail transitions associated with the task. */
Winson13d30662015-11-06 15:30:29 -0800544 void cancelTaskThumbnailTransition() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700545 for (int i = mChildren.size() - 1; i >= 0; --i) {
546 mChildren.get(i).mAppAnimator.clearThumbnail();
Winson13d30662015-11-06 15:30:29 -0800547 }
548 }
549
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700550 boolean showForAllUsers() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700551 final int tokensCount = mChildren.size();
Wale Ogunwale72919d22016-12-08 18:58:50 -0800552 return (tokensCount != 0) && mChildren.get(tokensCount - 1).mShowForAllUsers;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700553 }
554
Robert Carr7e4c90e2017-02-15 19:52:38 -0800555 /**
556 * When we are in a floating stack (Freeform, Pinned, ...) we calculate
557 * insets differently. However if we are animating to the fullscreen stack
558 * we need to begin calculating insets as if we were fullscreen, otherwise
559 * we will have a jump at the end.
560 */
Robert Carre6275582016-02-29 15:45:45 -0800561 boolean isFloating() {
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700562 return getWindowConfiguration().tasksAreFloating()
Robert Carr18f622f2017-05-08 11:20:43 -0700563 && !mStack.isAnimatingBoundsToFullscreen() && !mPreserveNonFloatingState;
Robert Carre6275582016-02-29 15:45:45 -0800564 }
565
Chong Zhangd8ceb852015-11-11 14:53:41 -0800566 WindowState getTopVisibleAppMainWindow() {
567 final AppWindowToken token = getTopVisibleAppToken();
568 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700569 }
570
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200571 AppWindowToken getTopFullscreenAppToken() {
572 for (int i = mChildren.size() - 1; i >= 0; i--) {
573 final AppWindowToken token = mChildren.get(i);
574 final WindowState win = token.findMainWindow();
575 if (win != null && win.mAttrs.isFullscreen()) {
576 return token;
577 }
578 }
579 return null;
580 }
581
Chong Zhangd8ceb852015-11-11 14:53:41 -0800582 AppWindowToken getTopVisibleAppToken() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700583 for (int i = mChildren.size() - 1; i >= 0; i--) {
584 final AppWindowToken token = mChildren.get(i);
Chong Zhangd8ceb852015-11-11 14:53:41 -0800585 // skip hidden (or about to hide) apps
Wale Ogunwale89973222017-04-23 18:39:45 -0700586 if (!token.mIsExiting && !token.isClientHidden() && !token.hiddenRequested) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800587 return token;
588 }
589 }
590 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700591 }
592
Wale Ogunwale29bfbb82016-05-12 15:13:52 -0700593 boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700594 if (useCurrentBounds()) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800595 return matchParentBounds();
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700596 }
597 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
598 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
599 // system.
600 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700601 }
602
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700603 void forceWindowsScaleable(boolean force) {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700604 mService.openSurfaceTransaction();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700605 try {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700606 for (int i = mChildren.size() - 1; i >= 0; i--) {
607 mChildren.get(i).forceWindowsScaleableInTransaction(force);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700608 }
609 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200610 mService.closeSurfaceTransaction("forceWindowsScaleable");
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700611 }
612 }
613
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100614 void setTaskDescription(TaskDescription taskDescription) {
615 mTaskDescription = taskDescription;
616 }
617
618 TaskDescription getTaskDescription() {
619 return mTaskDescription;
620 }
621
Wale Ogunwalef6192862016-09-10 13:42:30 -0700622 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700623 boolean fillsParent() {
Bryce Leef3c6a472017-11-14 14:53:06 -0800624 return matchParentBounds() || !getWindowConfiguration().canResizeTask();
Wale Ogunwale51362492016-09-08 17:49:17 -0700625 }
626
Jorim Jaggi329a5832017-01-05 18:57:12 +0100627 @Override
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100628 TaskWindowContainerController getController() {
629 return (TaskWindowContainerController) super.getController();
630 }
631
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700632 @Override
Jorim Jaggi51304d72017-05-17 17:25:32 +0200633 void forAllTasks(Consumer<Task> callback) {
634 callback.accept(this);
635 }
636
637 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800638 public String toString() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700639 return "{taskId=" + mTaskId + " appTokens=" + mChildren + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800640 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700641
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700642 String getName() {
643 return toShortString();
644 }
645
Robert Carr18f622f2017-05-08 11:20:43 -0700646 void clearPreserveNonFloatingState() {
647 mPreserveNonFloatingState = false;
648 }
649
Robert Carrf59b8dd2017-10-02 18:58:36 -0700650 Dimmer getDimmer() {
651 return mDimmer;
652 }
653
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700654 @Override
Robert Carrf59b8dd2017-10-02 18:58:36 -0700655 void prepareSurfaces() {
656 mDimmer.resetDimStates();
657 super.prepareSurfaces();
658 getDimBounds(mTmpDimBoundsRect);
659 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
660 scheduleAnimation();
661 }
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700662 }
663
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700664 @CallSuper
665 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +0200666 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -0700667 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +0200668 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -0700669 proto.write(ID, mTaskId);
670 for (int i = mChildren.size() - 1; i >= 0; i--) {
671 final AppWindowToken appWindowToken = mChildren.get(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +0200672 appWindowToken.writeToProto(proto, APP_WINDOW_TOKENS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -0700673 }
Bryce Leef3c6a472017-11-14 14:53:06 -0800674 proto.write(FILLS_PARENT, matchParentBounds());
675 getBounds().writeToProto(proto, BOUNDS);
Steven Timotiusaf03df62017-07-18 16:56:43 -0700676 mTempInsetBounds.writeToProto(proto, TEMP_INSET_BOUNDS);
677 proto.end(token);
678 }
679
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800680 public void dump(String prefix, PrintWriter pw) {
681 final String doublePrefix = prefix + " ";
682
683 pw.println(prefix + "taskId=" + mTaskId);
Bryce Leef3c6a472017-11-14 14:53:06 -0800684 pw.println(doublePrefix + "mBounds=" + getBounds().toShortString());
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800685 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
Wale Ogunwalef6192862016-09-10 13:42:30 -0700686 pw.println(doublePrefix + "appTokens=" + mChildren);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800687 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
688
689 final String triplePrefix = doublePrefix + " ";
690
Wale Ogunwalef6192862016-09-10 13:42:30 -0700691 for (int i = mChildren.size() - 1; i >= 0; i--) {
692 final AppWindowToken wtoken = mChildren.get(i);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800693 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
694 wtoken.dump(pw, triplePrefix);
695 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700696 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700697
698 String toShortString() {
699 return "Task=" + mTaskId;
700 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800701}