blob: c542ff616d7015b9dfbbe44bff87539aac4a29f5 [file] [log] [blame]
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080022import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Chong Zhangb15758a2015-11-17 12:12:03 -080023import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070024import static android.app.ActivityManager.RESIZE_MODE_SYSTEM_SCREEN_ROTATION;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080025import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080026import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080027import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
28import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_RESIZE;
29import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070030import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Chong Zhangc806d902015-11-30 09:44:27 -080031import static com.android.server.wm.WindowManagerService.H.SHOW_NON_RESIZEABLE_DOCK_TOAST;
32import static android.view.WindowManager.DOCKED_INVALID;
33import static android.view.WindowManager.DOCKED_LEFT;
34import static android.view.WindowManager.DOCKED_RIGHT;
35import static android.view.WindowManager.DOCKED_TOP;
Wale Ogunwale99db1862015-10-23 20:08:22 -070036
Wale Ogunwale3797c222015-10-27 14:21:58 -070037import android.app.ActivityManager.StackId;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080038import android.content.pm.ActivityInfo;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070039import android.content.res.Configuration;
40import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080041import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080042import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070043import android.view.DisplayInfo;
44import android.view.Surface;
45
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080046import com.android.internal.R;
Craig Mautnere3119b72015-01-20 15:02:36 -080047import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080048
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070049import java.io.PrintWriter;
50import java.util.ArrayList;
51
52class Task implements DimLayer.DimLayerUser {
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 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
55 static final int BOUNDS_CHANGE_NONE = 0;
56 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
57 static final int BOUNDS_CHANGE_POSITION = 1;
58 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
59 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
60
Craig Mautnerc00204b2013-03-05 15:02:14 -080061 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080062 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080063 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070064 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080065 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080066 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080067
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070068 // Content limits relative to the DisplayContent this sits in.
69 private Rect mBounds = new Rect();
Jorim Jaggi0429f352015-12-22 16:29:16 +010070 final Rect mPreparedFrozenBounds = new Rect();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070071
Chong Zhangf66db432016-01-13 10:39:51 -080072 private Rect mPreScrollBounds = new Rect();
73 private boolean mScrollValid;
74
Jorim Jaggidc249c42015-12-15 14:57:31 -080075 // Bounds used to calculate the insets.
76 private final Rect mTempInsetBounds = new Rect();
77
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070078 // Device rotation as of the last time {@link #mBounds} was set.
79 int mRotation;
80
81 // Whether mBounds is fullscreen
82 private boolean mFullscreen = true;
83
84 // Contains configurations settings that are different from the global configuration due to
85 // stack specific operations. E.g. {@link #setBounds}.
86 Configuration mOverrideConfig;
87
88 // For comparison with DisplayContent bounds.
89 private Rect mTmpRect = new Rect();
90 // For handling display rotations.
91 private Rect mTmpRect2 = new Rect();
92
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080093 // Resize mode of the task. See {@link ActivityInfo#resizeMode}
94 private int mResizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -080095
Chong Zhangc806d902015-11-30 09:44:27 -080096 // Whether we need to show toast about the app being non-resizeable when it becomes visible.
97 // This flag is set when a non-resizeable task is docked (or side-by-side). It's cleared
98 // after we show the toast.
99 private boolean mShowNonResizeableDockToast;
100
Chong Zhang3005e752015-09-18 18:46:28 -0700101 // Whether the task is currently being drag-resized
102 private boolean mDragResizing;
103
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800104 private boolean mHomeTask;
105
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700106 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
107 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -0800108 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800109 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -0700110 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -0800111 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700112 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800113 }
114
115 DisplayContent getDisplayContent() {
116 return mStack.getDisplayContent();
117 }
118
Chong Zhangc806d902015-11-30 09:44:27 -0800119 void setShowNonResizeableDockToast() {
120 mShowNonResizeableDockToast = true;
121 }
122
123 void scheduleShowNonResizeableDockToastIfNeeded() {
124 if (!mShowNonResizeableDockToast) {
125 return;
126 }
127 final DisplayContent displayContent = mStack.getDisplayContent();
128 // If docked stack is not yet visible, we don't want to show the toast yet,
129 // since we need the visible rect of the docked task to position the toast.
130 if (displayContent == null || displayContent.getDockedStackLocked() == null) {
131 return;
132 }
133
134 mShowNonResizeableDockToast = false;
135
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800136 if (isResizeable()) {
137 Slog.wtf(TAG,
138 "Trying to show non-resizeable toast when task is resizeable task=" + this);
139 return;
140 }
141
142 if (mResizeMode == RESIZE_MODE_UNRESIZEABLE) {
143 final String text =
144 mService.mContext.getString(R.string.dock_non_resizeble_failed_to_dock_text);
145 mService.mH.obtainMessage(SHOW_NON_RESIZEABLE_DOCK_TOAST, 0, 0, text).sendToTarget();
146 return;
147 }
148
Chong Zhangc806d902015-11-30 09:44:27 -0800149 final int dockSide = mStack.getDockSide();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800150 if (!inCropWindowsResizeMode() || dockSide == DOCKED_INVALID) {
151 return;
152 }
153
Chong Zhangc806d902015-11-30 09:44:27 -0800154 int xOffset = 0;
155 int yOffset = 0;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800156 mStack.getBounds(mTmpRect);
Chong Zhangc806d902015-11-30 09:44:27 -0800157
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800158 if (dockSide == DOCKED_LEFT || dockSide == DOCKED_RIGHT) {
159 // The toast was originally placed at the bottom and centered. To place it at the
160 // bottom-center of the stack, we offset it horizontally by the diff between the center
161 // of the stack bounds vs. the center of the screen.
162 displayContent.getLogicalDisplayRect(mTmpRect2);
163 xOffset = mTmpRect.centerX() - mTmpRect2.centerX();
164 } else if (dockSide == DOCKED_TOP) {
165 // The toast was originally placed at the bottom and centered. To place it at the bottom
166 // center of the top stack, we offset it vertically by the diff between the bottom of
167 // the stack bounds vs. the bottom of the content rect.
168 //
169 // Note here we use the content rect instead of the display rect, as we want the toast's
170 // distance to the dock divider (when it's placed at the top half) to be the same as
171 // it's distance to the top of the navigation bar (when it's placed at the bottom).
Chong Zhangf66db432016-01-13 10:39:51 -0800172
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800173 // We don't adjust for DOCKED_BOTTOM case since it's already at the bottom.
174 displayContent.getContentRect(mTmpRect2);
175 yOffset = mTmpRect2.bottom - mTmpRect.bottom;
Chong Zhangc806d902015-11-30 09:44:27 -0800176 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800177 final String text =
178 mService.mContext.getString(R.string.dock_cropped_windows_text);
179 mService.mH.obtainMessage(SHOW_NON_RESIZEABLE_DOCK_TOAST,
180 xOffset, yOffset, text).sendToTarget();
Chong Zhangc806d902015-11-30 09:44:27 -0800181 }
182
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800183 void addAppToken(int addPos, AppWindowToken wtoken, int resizeMode, boolean homeTask) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800184 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800185 if (addPos >= lastPos) {
186 addPos = lastPos;
187 } else {
188 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
189 if (mAppTokens.get(pos).removed) {
190 // addPos assumes removed tokens are actually gone.
191 ++addPos;
192 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700193 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800194 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800195 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800196 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800197 mDeferRemoval = false;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800198 mResizeMode = resizeMode;
199 mHomeTask = homeTask;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800200 }
201
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800202 private boolean hasAppTokensAlive() {
203 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
204 if (!mAppTokens.get(i).appDied) {
205 return true;
206 }
207 }
208 return false;
209 }
210
Craig Mautnere3119b72015-01-20 15:02:36 -0800211 void removeLocked() {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800212 if (hasAppTokensAlive() && mStack.isAnimating()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800213 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800214 mDeferRemoval = true;
215 return;
216 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800217 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800218 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800219 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700220 DisplayContent content = getDisplayContent();
221 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800222 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700223 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800224 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800225 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800226 }
227
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800228 void moveTaskToStack(TaskStack stack, boolean toTop) {
229 if (stack == mStack) {
230 return;
231 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800232 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800233 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700234 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800235 if (mStack != null) {
236 mStack.removeTask(this);
237 }
238 stack.addTask(this, toTop);
239 }
240
Wale Ogunwale935e5022015-11-10 12:36:10 -0800241 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700242 if (mStack != null && stack != mStack) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800243 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700244 + " from stack=" + mStack);
245 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
246 mStack.removeTask(this);
247 }
248 stack.positionTask(this, position, showForAllUsers());
Wale Ogunwale935e5022015-11-10 12:36:10 -0800249 setBounds(bounds, config);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700250 }
251
Craig Mautnerc00204b2013-03-05 15:02:14 -0800252 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800253 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800254 if (mAppTokens.size() == 0) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100255 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800256 if (mDeferRemoval) {
257 removeLocked();
258 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800259 }
Craig Mautner83162a92015-01-26 14:43:30 -0800260 wtoken.mTask = null;
261 /* Leave mTaskId for now, it might be useful for debug
262 wtoken.mTaskId = -1;
263 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800264 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800265 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800266
Craig Mautnercbd84af2014-10-22 13:21:22 -0700267 void setSendingToBottom(boolean toBottom) {
268 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
269 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
270 }
271 }
272
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700273 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700274 int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700275 if (config == null) {
276 config = Configuration.EMPTY;
277 }
278 if (bounds == null && !Configuration.EMPTY.equals(config)) {
279 throw new IllegalArgumentException("null bounds but non empty configuration: "
280 + config);
281 }
282 if (bounds != null && Configuration.EMPTY.equals(config)) {
283 throw new IllegalArgumentException("non null bounds, but empty configuration");
284 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700285 boolean oldFullscreen = mFullscreen;
286 int rotation = Surface.ROTATION_0;
287 final DisplayContent displayContent = mStack.getDisplayContent();
288 if (displayContent != null) {
289 displayContent.getLogicalDisplayRect(mTmpRect);
290 rotation = displayContent.getDisplayInfo().rotation;
291 if (bounds == null) {
292 bounds = mTmpRect;
293 mFullscreen = true;
294 } else {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700295 mFullscreen = mTmpRect.equals(bounds);
296 }
297 }
298
299 if (bounds == null) {
300 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700301 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700302 }
Chong Zhangf66db432016-01-13 10:39:51 -0800303 if (mPreScrollBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700304 return BOUNDS_CHANGE_NONE;
305 }
306
307 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhangf66db432016-01-13 10:39:51 -0800308 if (mPreScrollBounds.left != bounds.left || mPreScrollBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700309 boundsChange |= BOUNDS_CHANGE_POSITION;
310 }
Chong Zhangf66db432016-01-13 10:39:51 -0800311 if (mPreScrollBounds.width() != bounds.width() || mPreScrollBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700312 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700313 }
314
Chong Zhangf66db432016-01-13 10:39:51 -0800315
316 mPreScrollBounds.set(bounds);
317
318 resetScrollLocked();
319
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700320 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700321 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800322 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700323 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700324 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700325 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700326 }
327
Jorim Jaggidc249c42015-12-15 14:57:31 -0800328 /**
329 * Sets the bounds used to calculate the insets. See
330 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
331 */
332 void setTempInsetBounds(Rect tempInsetBounds) {
333 if (tempInsetBounds != null) {
334 mTempInsetBounds.set(tempInsetBounds);
335 } else {
336 mTempInsetBounds.setEmpty();
337 }
338 }
339
340 /**
341 * Gets the bounds used to calculate the insets. See
342 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
343 */
344 void getTempInsetBounds(Rect out) {
345 out.set(mTempInsetBounds);
346 }
347
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800348 void setResizeable(int resizeMode) {
349 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800350 }
351
352 boolean isResizeable() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800353 return !mHomeTask
354 && (ActivityInfo.isResizeableMode(mResizeMode) || mService.mForceResizableTasks);
355 }
356
357 boolean cropWindowsToStackBounds() {
358 return !mHomeTask && (isResizeable() || mResizeMode == RESIZE_MODE_CROP_WINDOWS);
359 }
360
361 private boolean inCropWindowsResizeMode() {
362 return !mHomeTask && !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
Chong Zhangb15758a2015-11-17 12:12:03 -0800363 }
364
Chong Zhang87b21722015-09-21 15:39:51 -0700365 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700366 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700367 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700368 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700369 }
370 if (boundsChanged == BOUNDS_CHANGE_NONE) {
371 return false;
372 }
373 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
374 resizeWindows();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800375 } else {
376 moveWindows();
Chong Zhang3005e752015-09-18 18:46:28 -0700377 }
378 return true;
379 }
380
Jorim Jaggi0429f352015-12-22 16:29:16 +0100381 /**
382 * Prepares the task bounds to be frozen with the current size. See
383 * {@link AppWindowToken#freezeBounds}.
384 */
385 void prepareFreezingBounds() {
386 mPreparedFrozenBounds.set(mBounds);
387 }
388
Chong Zhangf66db432016-01-13 10:39:51 -0800389 void resetScrollLocked() {
390 if (mScrollValid) {
391 mScrollValid = false;
392 applyScrollToAllWindows(0, 0);
393 }
394 mBounds.set(mPreScrollBounds);
395 }
396
397 void applyScrollToAllWindows(final int xOffset, final int yOffset) {
398 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
399 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
400 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
401 final WindowState win = windows.get(winNdx);
402 win.mXOffset = xOffset;
403 win.mYOffset = yOffset;
404 }
405 }
406 }
407
408 void applyScrollToWindowIfNeeded(final WindowState win) {
409 if (mScrollValid) {
410 win.mXOffset = mBounds.left;
411 win.mYOffset = mBounds.top;
412 }
413 }
414
Chong Zhangb15758a2015-11-17 12:12:03 -0800415 boolean scrollLocked(Rect bounds) {
416 // shift the task bound if it doesn't fully cover the stack area
417 mStack.getDimBounds(mTmpRect);
418 if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) {
419 if (bounds.left > mTmpRect.left) {
420 bounds.left = mTmpRect.left;
421 bounds.right = mTmpRect.left + mBounds.width();
422 } else if (bounds.right < mTmpRect.right) {
423 bounds.left = mTmpRect.right - mBounds.width();
424 bounds.right = mTmpRect.right;
425 }
426 } else {
427 if (bounds.top > mTmpRect.top) {
428 bounds.top = mTmpRect.top;
429 bounds.bottom = mTmpRect.top + mBounds.height();
430 } else if (bounds.bottom < mTmpRect.bottom) {
431 bounds.top = mTmpRect.bottom - mBounds.height();
432 bounds.bottom = mTmpRect.bottom;
433 }
434 }
435
Chong Zhangf66db432016-01-13 10:39:51 -0800436 // We can stop here if we're already scrolling and the scrolled bounds not changed.
437 if (mScrollValid && bounds.equals(mBounds)) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800438 return false;
439 }
Chong Zhangf66db432016-01-13 10:39:51 -0800440
Chong Zhangb15758a2015-11-17 12:12:03 -0800441 // Normal setBounds() does not allow non-null bounds for fullscreen apps.
442 // We only change bounds for the scrolling case without change it size,
443 // on resizing path we should still want the validation.
444 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800445 mScrollValid = true;
446 applyScrollToAllWindows(bounds.left, bounds.top);
Chong Zhangb15758a2015-11-17 12:12:03 -0800447 return true;
448 }
449
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700450 /** Return true if the current bound can get outputted to the rest of the system as-is. */
451 private boolean useCurrentBounds() {
452 final DisplayContent displayContent = mStack.getDisplayContent();
453 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700454 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700455 || displayContent == null
456 || displayContent.getDockedStackLocked() != null) {
457 return true;
458 }
459 return false;
460 }
461
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800462 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800463 void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700464 if (useCurrentBounds()) {
465 // No need to adjust the output bounds if fullscreen or the docked stack is visible
466 // since it is already what we want to represent to the rest of the system.
467 out.set(mBounds);
468 return;
469 }
470
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800471 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
472 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700473 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700474 }
475
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800476 /**
477 * Calculate the maximum visible area of this task. If the task has only one app,
478 * the result will be visible frame of that app. If the task has more than one apps,
479 * we search from top down if the next app got different visible area.
480 *
481 * This effort is to handle the case where some task (eg. GMail composer) might pop up
482 * a dialog that's different in size from the activity below, in which case we should
483 * be dimming the entire task area behind the dialog.
484 *
485 * @param out Rect containing the max visible bounds.
486 * @return true if the task has some visible app windows; false otherwise.
487 */
488 boolean getMaxVisibleBounds(Rect out) {
489 boolean foundTop = false;
490 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800491 final AppWindowToken token = mAppTokens.get(i);
492 // skip hidden (or about to hide) apps
493 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
494 continue;
495 }
496 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800497 if (win == null) {
498 continue;
499 }
500 if (!foundTop) {
501 out.set(win.mVisibleFrame);
502 foundTop = true;
503 continue;
504 }
505 if (win.mVisibleFrame.left < out.left) {
506 out.left = win.mVisibleFrame.left;
507 }
508 if (win.mVisibleFrame.top < out.top) {
509 out.top = win.mVisibleFrame.top;
510 }
511 if (win.mVisibleFrame.right > out.right) {
512 out.right = win.mVisibleFrame.right;
513 }
514 if (win.mVisibleFrame.bottom > out.bottom) {
515 out.bottom = win.mVisibleFrame.bottom;
516 }
517 }
518 return foundTop;
519 }
520
521 /** Bounds of the task to be used for dimming, as well as touch related tests. */
522 @Override
523 public void getDimBounds(Rect out) {
524 if (useCurrentBounds()) {
525 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
526 return;
527 }
528
529 out.set(mBounds);
530 return;
531 }
532
533 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
534 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
535 // system.
536 mStack.getDisplayContent().getLogicalDisplayRect(out);
537 }
538
Chong Zhang3005e752015-09-18 18:46:28 -0700539 void setDragResizing(boolean dragResizing) {
Chong Zhang3005e752015-09-18 18:46:28 -0700540 mDragResizing = dragResizing;
541 }
542
543 boolean isDragResizing() {
544 return mDragResizing;
545 }
546
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700547 void updateDisplayInfo(final DisplayContent displayContent) {
548 if (displayContent == null) {
549 return;
550 }
551 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700552 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700553 return;
554 }
555 final int newRotation = displayContent.getDisplayInfo().rotation;
556 if (mRotation == newRotation) {
557 return;
558 }
559
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800560 // Device rotation changed.
561 // - Reset the bounds to the pre-scroll bounds as whatever scrolling was done is no longer
562 // valid.
563 // - Rotate the bounds and notify activity manager if the task can be resized independently
564 // from its stack. The stack will take care of task rotation for the other case.
Chong Zhangf66db432016-01-13 10:39:51 -0800565 mTmpRect2.set(mPreScrollBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800566
567 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
568 setBounds(mTmpRect2, mOverrideConfig);
569 return;
570 }
571
Wale Ogunwale94744212015-09-21 19:01:47 -0700572 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700573 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800574 // Post message to inform activity manager of the bounds change simulating a one-way
575 // call. We do this to prevent a deadlock between window manager lock and activity
576 // manager lock been held.
577 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
578 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mPreScrollBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700579 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700580 }
581
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700582 void resizeWindows() {
583 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
584 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
585 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
586 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
587 final WindowState win = windows.get(winNdx);
588 if (!resizingWindows.contains(win)) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800589 if (DEBUG_RESIZE) Slog.d(TAG, "resizeWindows: Resizing " + win);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700590 resizingWindows.add(win);
591 }
592 }
593 }
594 }
595
Chong Zhangbd0d9372015-12-28 15:18:29 -0800596 void moveWindows() {
597 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
598 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
599 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
600 final WindowState win = windows.get(winNdx);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800601 if (DEBUG_RESIZE) Slog.d(TAG, "moveWindows: Moving " + win);
Chong Zhangbd0d9372015-12-28 15:18:29 -0800602 win.mMovedByResize = true;
603 }
604 }
605 }
606
Winsonc28098f2015-10-30 14:50:19 -0700607 /**
608 * Cancels any running app transitions associated with the task.
609 */
610 void cancelTaskWindowTransition() {
611 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
612 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
613 }
614 }
615
Winson13d30662015-11-06 15:30:29 -0800616 /**
617 * Cancels any running thumbnail transitions associated with the task.
618 */
619 void cancelTaskThumbnailTransition() {
620 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
621 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
622 }
623 }
624
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700625 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700626 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700627 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700628 }
629
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700630 boolean inHomeStack() {
631 return mStack != null && mStack.mStackId == HOME_STACK_ID;
632 }
633
Chong Zhang09b21ef2015-09-14 10:20:21 -0700634 boolean inFreeformWorkspace() {
635 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
636 }
637
Filip Gruszczynski955b2fc2015-10-15 14:46:07 -0700638 boolean inDockedWorkspace() {
639 return mStack != null && mStack.mStackId == DOCKED_STACK_ID;
640 }
641
Chong Zhangb15758a2015-11-17 12:12:03 -0800642 boolean isResizeableByDockedStack() {
Wale Ogunwale5e7409c2016-01-11 12:24:19 -0800643 final DisplayContent displayContent = getDisplayContent();
644 return displayContent != null && displayContent.getDockedStackLocked() != null
645 && mStack != null && StackId.isTaskResizeableByDockedStack(mStack.mStackId);
Chong Zhangb15758a2015-11-17 12:12:03 -0800646 }
647
648 /**
649 * Whether the task should be treated as if it's docked. Returns true if the task
650 * is currently in docked workspace, or it's side-by-side to a docked task.
651 */
652 boolean isDockedInEffect() {
653 return inDockedWorkspace() || isResizeableByDockedStack();
654 }
655
Chong Zhang2a88fc32016-01-11 17:14:24 -0800656 boolean isTwoFingerScrollMode() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800657 return inCropWindowsResizeMode() && isDockedInEffect();
Chong Zhang2a88fc32016-01-11 17:14:24 -0800658 }
659
Chong Zhangd8ceb852015-11-11 14:53:41 -0800660 WindowState getTopVisibleAppMainWindow() {
661 final AppWindowToken token = getTopVisibleAppToken();
662 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700663 }
664
Chong Zhangd8ceb852015-11-11 14:53:41 -0800665 AppWindowToken getTopVisibleAppToken() {
666 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
667 final AppWindowToken token = mAppTokens.get(i);
668 // skip hidden (or about to hide) apps
669 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
670 return token;
671 }
672 }
673 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700674 }
675
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800676 @Override
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700677 public boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700678 if (useCurrentBounds()) {
679 return mFullscreen;
680 }
681 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
682 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
683 // system.
684 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700685 }
686
687 @Override
688 public DisplayInfo getDisplayInfo() {
689 return mStack.getDisplayContent().getDisplayInfo();
690 }
691
692 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800693 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800694 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800695 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700696
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700697 @Override
698 public String toShortString() {
699 return "Task=" + mTaskId;
700 }
701
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800702 public void dump(String prefix, PrintWriter pw) {
703 final String doublePrefix = prefix + " ";
704
705 pw.println(prefix + "taskId=" + mTaskId);
706 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
707 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
708 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
709 pw.println(doublePrefix + "appTokens=" + mAppTokens);
710 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
711
712 final String triplePrefix = doublePrefix + " ";
713
714 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
715 final AppWindowToken wtoken = mAppTokens.get(i);
716 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
717 wtoken.dump(pw, triplePrefix);
718 }
719
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700720 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800721}