blob: 1f03c041b477fd298e49f193a3da5074b47cd2e9 [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;
Jorim Jaggi51605272016-02-24 18:06:49 -050022import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080023import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Chong Zhangb15758a2015-11-17 12:12:03 -080024import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070025import static android.app.ActivityManager.RESIZE_MODE_SYSTEM_SCREEN_ROTATION;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080026import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080027import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080028import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
29import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_RESIZE;
30import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070031import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Chong Zhangc806d902015-11-30 09:44:27 -080032import static com.android.server.wm.WindowManagerService.H.SHOW_NON_RESIZEABLE_DOCK_TOAST;
33import static android.view.WindowManager.DOCKED_INVALID;
34import static android.view.WindowManager.DOCKED_LEFT;
35import static android.view.WindowManager.DOCKED_RIGHT;
36import static android.view.WindowManager.DOCKED_TOP;
Wale Ogunwale99db1862015-10-23 20:08:22 -070037
Wale Ogunwale3797c222015-10-27 14:21:58 -070038import android.app.ActivityManager.StackId;
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;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070044import android.view.DisplayInfo;
45import android.view.Surface;
46
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080047import com.android.internal.R;
Craig Mautnere3119b72015-01-20 15:02:36 -080048import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080049
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070050import java.io.PrintWriter;
51import java.util.ArrayList;
52
53class Task implements DimLayer.DimLayerUser {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080054 static final String TAG = TAG_WITH_CLASS_NAME ? "Task" : TAG_WM;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070055 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
56 static final int BOUNDS_CHANGE_NONE = 0;
57 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
58 static final int BOUNDS_CHANGE_POSITION = 1;
59 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
60 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
61
Craig Mautnerc00204b2013-03-05 15:02:14 -080062 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080063 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080064 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070065 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080066 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080067 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080068
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070069 // Content limits relative to the DisplayContent this sits in.
70 private Rect mBounds = new Rect();
Jorim Jaggi0429f352015-12-22 16:29:16 +010071 final Rect mPreparedFrozenBounds = new Rect();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070072
Chong Zhangf66db432016-01-13 10:39:51 -080073 private Rect mPreScrollBounds = new Rect();
74 private boolean mScrollValid;
75
Jorim Jaggidc249c42015-12-15 14:57:31 -080076 // Bounds used to calculate the insets.
77 private final Rect mTempInsetBounds = new Rect();
78
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070079 // Device rotation as of the last time {@link #mBounds} was set.
80 int mRotation;
81
82 // Whether mBounds is fullscreen
83 private boolean mFullscreen = true;
84
85 // Contains configurations settings that are different from the global configuration due to
86 // stack specific operations. E.g. {@link #setBounds}.
87 Configuration mOverrideConfig;
88
89 // For comparison with DisplayContent bounds.
90 private Rect mTmpRect = new Rect();
91 // For handling display rotations.
92 private Rect mTmpRect2 = new Rect();
93
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080094 // Resize mode of the task. See {@link ActivityInfo#resizeMode}
95 private int mResizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -080096
Chong Zhangc806d902015-11-30 09:44:27 -080097 // Whether we need to show toast about the app being non-resizeable when it becomes visible.
98 // This flag is set when a non-resizeable task is docked (or side-by-side). It's cleared
99 // after we show the toast.
100 private boolean mShowNonResizeableDockToast;
101
Chong Zhang3005e752015-09-18 18:46:28 -0700102 // Whether the task is currently being drag-resized
103 private boolean mDragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100104 private int mDragResizeMode;
Chong Zhang3005e752015-09-18 18:46:28 -0700105
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800106 private boolean mHomeTask;
107
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700108 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
109 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -0800110 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800111 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -0700112 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -0800113 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700114 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800115 }
116
117 DisplayContent getDisplayContent() {
118 return mStack.getDisplayContent();
119 }
120
Chong Zhangc806d902015-11-30 09:44:27 -0800121 void setShowNonResizeableDockToast() {
122 mShowNonResizeableDockToast = true;
123 }
124
125 void scheduleShowNonResizeableDockToastIfNeeded() {
126 if (!mShowNonResizeableDockToast) {
127 return;
128 }
129 final DisplayContent displayContent = mStack.getDisplayContent();
130 // If docked stack is not yet visible, we don't want to show the toast yet,
131 // since we need the visible rect of the docked task to position the toast.
132 if (displayContent == null || displayContent.getDockedStackLocked() == null) {
133 return;
134 }
135
136 mShowNonResizeableDockToast = false;
137
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800138 if (mResizeMode == RESIZE_MODE_UNRESIZEABLE) {
139 final String text =
140 mService.mContext.getString(R.string.dock_non_resizeble_failed_to_dock_text);
141 mService.mH.obtainMessage(SHOW_NON_RESIZEABLE_DOCK_TOAST, 0, 0, text).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800142 }
Chong Zhangc806d902015-11-30 09:44:27 -0800143 }
144
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800145 void addAppToken(int addPos, AppWindowToken wtoken, int resizeMode, boolean homeTask) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800146 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800147 if (addPos >= lastPos) {
148 addPos = lastPos;
149 } else {
150 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
151 if (mAppTokens.get(pos).removed) {
152 // addPos assumes removed tokens are actually gone.
153 ++addPos;
154 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700155 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800156 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800157 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800158 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800159 mDeferRemoval = false;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800160 mResizeMode = resizeMode;
161 mHomeTask = homeTask;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800162 }
163
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800164 private boolean hasAppTokensAlive() {
165 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
166 if (!mAppTokens.get(i).appDied) {
167 return true;
168 }
169 }
170 return false;
171 }
172
Craig Mautnere3119b72015-01-20 15:02:36 -0800173 void removeLocked() {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800174 if (hasAppTokensAlive() && mStack.isAnimating()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800175 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800176 mDeferRemoval = true;
177 return;
178 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800179 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800180 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800181 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700182 DisplayContent content = getDisplayContent();
183 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800184 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700185 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800186 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800187 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800188 }
189
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800190 void moveTaskToStack(TaskStack stack, boolean toTop) {
191 if (stack == mStack) {
192 return;
193 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800194 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800195 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700196 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800197 if (mStack != null) {
198 mStack.removeTask(this);
199 }
200 stack.addTask(this, toTop);
201 }
202
Wale Ogunwale935e5022015-11-10 12:36:10 -0800203 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700204 if (mStack != null && stack != mStack) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800205 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700206 + " from stack=" + mStack);
207 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
208 mStack.removeTask(this);
209 }
210 stack.positionTask(this, position, showForAllUsers());
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800211 resizeLocked(bounds, config, false /* force */);
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800212
213 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
214 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
215 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
216 final WindowState win = windows.get(winNdx);
217 win.notifyMovedInStack();
218 }
219 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700220 }
221
Craig Mautnerc00204b2013-03-05 15:02:14 -0800222 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800223 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800224 if (mAppTokens.size() == 0) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100225 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800226 if (mDeferRemoval) {
227 removeLocked();
228 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800229 }
Craig Mautner83162a92015-01-26 14:43:30 -0800230 wtoken.mTask = null;
231 /* Leave mTaskId for now, it might be useful for debug
232 wtoken.mTaskId = -1;
233 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800234 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800235 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800236
Craig Mautnercbd84af2014-10-22 13:21:22 -0700237 void setSendingToBottom(boolean toBottom) {
238 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
239 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
240 }
241 }
242
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700243 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800244 private int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700245 if (config == null) {
246 config = Configuration.EMPTY;
247 }
248 if (bounds == null && !Configuration.EMPTY.equals(config)) {
249 throw new IllegalArgumentException("null bounds but non empty configuration: "
250 + config);
251 }
252 if (bounds != null && Configuration.EMPTY.equals(config)) {
253 throw new IllegalArgumentException("non null bounds, but empty configuration");
254 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700255 boolean oldFullscreen = mFullscreen;
256 int rotation = Surface.ROTATION_0;
257 final DisplayContent displayContent = mStack.getDisplayContent();
258 if (displayContent != null) {
259 displayContent.getLogicalDisplayRect(mTmpRect);
260 rotation = displayContent.getDisplayInfo().rotation;
Jorim Jaggi067e8172016-02-03 18:24:12 -0800261 mFullscreen = bounds == null;
262 if (mFullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700263 bounds = mTmpRect;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700264 }
265 }
266
267 if (bounds == null) {
268 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700269 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700270 }
Chong Zhangf66db432016-01-13 10:39:51 -0800271 if (mPreScrollBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700272 return BOUNDS_CHANGE_NONE;
273 }
274
275 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhangf66db432016-01-13 10:39:51 -0800276 if (mPreScrollBounds.left != bounds.left || mPreScrollBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700277 boundsChange |= BOUNDS_CHANGE_POSITION;
278 }
Chong Zhangf66db432016-01-13 10:39:51 -0800279 if (mPreScrollBounds.width() != bounds.width() || mPreScrollBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700280 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700281 }
282
Chong Zhangf66db432016-01-13 10:39:51 -0800283
284 mPreScrollBounds.set(bounds);
285
286 resetScrollLocked();
287
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700288 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700289 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800290 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700291 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700292 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700293 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700294 }
295
Jorim Jaggidc249c42015-12-15 14:57:31 -0800296 /**
297 * Sets the bounds used to calculate the insets. See
298 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
299 */
300 void setTempInsetBounds(Rect tempInsetBounds) {
301 if (tempInsetBounds != null) {
302 mTempInsetBounds.set(tempInsetBounds);
303 } else {
304 mTempInsetBounds.setEmpty();
305 }
306 }
307
308 /**
309 * Gets the bounds used to calculate the insets. See
310 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
311 */
312 void getTempInsetBounds(Rect out) {
313 out.set(mTempInsetBounds);
314 }
315
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800316 void setResizeable(int resizeMode) {
317 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800318 }
319
320 boolean isResizeable() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800321 return !mHomeTask
322 && (ActivityInfo.isResizeableMode(mResizeMode) || mService.mForceResizableTasks);
323 }
324
325 boolean cropWindowsToStackBounds() {
326 return !mHomeTask && (isResizeable() || mResizeMode == RESIZE_MODE_CROP_WINDOWS);
327 }
328
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800329 boolean isHomeTask() {
330 return mHomeTask;
331 }
332
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800333 private boolean inCropWindowsResizeMode() {
334 return !mHomeTask && !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
Chong Zhangb15758a2015-11-17 12:12:03 -0800335 }
336
Chong Zhang87b21722015-09-21 15:39:51 -0700337 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700338 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700339 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700340 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700341 }
342 if (boundsChanged == BOUNDS_CHANGE_NONE) {
343 return false;
344 }
345 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
346 resizeWindows();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800347 } else {
348 moveWindows();
Chong Zhang3005e752015-09-18 18:46:28 -0700349 }
350 return true;
351 }
352
Jorim Jaggi0429f352015-12-22 16:29:16 +0100353 /**
354 * Prepares the task bounds to be frozen with the current size. See
355 * {@link AppWindowToken#freezeBounds}.
356 */
357 void prepareFreezingBounds() {
358 mPreparedFrozenBounds.set(mBounds);
359 }
360
Chong Zhangf66db432016-01-13 10:39:51 -0800361 void resetScrollLocked() {
362 if (mScrollValid) {
363 mScrollValid = false;
364 applyScrollToAllWindows(0, 0);
365 }
366 mBounds.set(mPreScrollBounds);
367 }
368
369 void applyScrollToAllWindows(final int xOffset, final int yOffset) {
370 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
371 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
372 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
373 final WindowState win = windows.get(winNdx);
374 win.mXOffset = xOffset;
375 win.mYOffset = yOffset;
376 }
377 }
378 }
379
380 void applyScrollToWindowIfNeeded(final WindowState win) {
381 if (mScrollValid) {
382 win.mXOffset = mBounds.left;
383 win.mYOffset = mBounds.top;
384 }
385 }
386
Chong Zhangb15758a2015-11-17 12:12:03 -0800387 boolean scrollLocked(Rect bounds) {
388 // shift the task bound if it doesn't fully cover the stack area
389 mStack.getDimBounds(mTmpRect);
390 if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) {
391 if (bounds.left > mTmpRect.left) {
392 bounds.left = mTmpRect.left;
393 bounds.right = mTmpRect.left + mBounds.width();
394 } else if (bounds.right < mTmpRect.right) {
395 bounds.left = mTmpRect.right - mBounds.width();
396 bounds.right = mTmpRect.right;
397 }
398 } else {
399 if (bounds.top > mTmpRect.top) {
400 bounds.top = mTmpRect.top;
401 bounds.bottom = mTmpRect.top + mBounds.height();
402 } else if (bounds.bottom < mTmpRect.bottom) {
403 bounds.top = mTmpRect.bottom - mBounds.height();
404 bounds.bottom = mTmpRect.bottom;
405 }
406 }
407
Chong Zhangf66db432016-01-13 10:39:51 -0800408 // We can stop here if we're already scrolling and the scrolled bounds not changed.
409 if (mScrollValid && bounds.equals(mBounds)) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800410 return false;
411 }
Chong Zhangf66db432016-01-13 10:39:51 -0800412
Chong Zhangb15758a2015-11-17 12:12:03 -0800413 // Normal setBounds() does not allow non-null bounds for fullscreen apps.
414 // We only change bounds for the scrolling case without change it size,
415 // on resizing path we should still want the validation.
416 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800417 mScrollValid = true;
418 applyScrollToAllWindows(bounds.left, bounds.top);
Chong Zhangb15758a2015-11-17 12:12:03 -0800419 return true;
420 }
421
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700422 /** Return true if the current bound can get outputted to the rest of the system as-is. */
423 private boolean useCurrentBounds() {
424 final DisplayContent displayContent = mStack.getDisplayContent();
425 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700426 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700427 || displayContent == null
428 || displayContent.getDockedStackLocked() != null) {
429 return true;
430 }
431 return false;
432 }
433
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800434 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800435 void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700436 if (useCurrentBounds()) {
437 // No need to adjust the output bounds if fullscreen or the docked stack is visible
438 // since it is already what we want to represent to the rest of the system.
439 out.set(mBounds);
440 return;
441 }
442
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800443 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
444 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700445 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700446 }
447
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800448 /**
449 * Calculate the maximum visible area of this task. If the task has only one app,
450 * the result will be visible frame of that app. If the task has more than one apps,
451 * we search from top down if the next app got different visible area.
452 *
453 * This effort is to handle the case where some task (eg. GMail composer) might pop up
454 * a dialog that's different in size from the activity below, in which case we should
455 * be dimming the entire task area behind the dialog.
456 *
457 * @param out Rect containing the max visible bounds.
458 * @return true if the task has some visible app windows; false otherwise.
459 */
460 boolean getMaxVisibleBounds(Rect out) {
461 boolean foundTop = false;
462 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800463 final AppWindowToken token = mAppTokens.get(i);
464 // skip hidden (or about to hide) apps
465 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
466 continue;
467 }
468 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800469 if (win == null) {
470 continue;
471 }
472 if (!foundTop) {
473 out.set(win.mVisibleFrame);
474 foundTop = true;
475 continue;
476 }
477 if (win.mVisibleFrame.left < out.left) {
478 out.left = win.mVisibleFrame.left;
479 }
480 if (win.mVisibleFrame.top < out.top) {
481 out.top = win.mVisibleFrame.top;
482 }
483 if (win.mVisibleFrame.right > out.right) {
484 out.right = win.mVisibleFrame.right;
485 }
486 if (win.mVisibleFrame.bottom > out.bottom) {
487 out.bottom = win.mVisibleFrame.bottom;
488 }
489 }
490 return foundTop;
491 }
492
493 /** Bounds of the task to be used for dimming, as well as touch related tests. */
494 @Override
495 public void getDimBounds(Rect out) {
496 if (useCurrentBounds()) {
497 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
498 return;
499 }
500
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700501 if (!mFullscreen) {
502 // When minimizing the docked stack when going home, we don't adjust the task bounds
503 // so we need to intersect the task bounds with the stack bounds here.
504 mStack.getBounds(mTmpRect);
505 mTmpRect.intersect(mBounds);
506 out.set(mTmpRect);
507 } else {
508 out.set(mBounds);
509 }
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800510 return;
511 }
512
513 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
514 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
515 // system.
516 mStack.getDisplayContent().getLogicalDisplayRect(out);
517 }
518
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100519 void setDragResizing(boolean dragResizing, int dragResizeMode) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800520 if (mDragResizing != dragResizing) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100521 if (!DragResizeMode.isModeAllowedForStack(mStack.mStackId, dragResizeMode)) {
522 throw new IllegalArgumentException("Drag resize mode not allow for stack stackId="
523 + mStack.mStackId + " dragResizeMode=" + dragResizeMode);
524 }
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800525 mDragResizing = dragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100526 mDragResizeMode = dragResizeMode;
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800527 resetDragResizingChangeReported();
528 }
529 }
530
531 void resetDragResizingChangeReported() {
532 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
533 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
534 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
535 final WindowState win = windows.get(winNdx);
536 win.resetDragResizingChangeReported();
537 }
538 }
Chong Zhang3005e752015-09-18 18:46:28 -0700539 }
540
541 boolean isDragResizing() {
Wale Ogunwalece144522016-02-05 22:51:01 -0800542 return mDragResizing || (mStack != null && mStack.isDragResizing());
Chong Zhang3005e752015-09-18 18:46:28 -0700543 }
544
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100545 int getDragResizeMode() {
546 return mDragResizeMode;
547 }
548
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700549 void updateDisplayInfo(final DisplayContent displayContent) {
550 if (displayContent == null) {
551 return;
552 }
553 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700554 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700555 return;
556 }
557 final int newRotation = displayContent.getDisplayInfo().rotation;
558 if (mRotation == newRotation) {
559 return;
560 }
561
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800562 // Device rotation changed.
563 // - Reset the bounds to the pre-scroll bounds as whatever scrolling was done is no longer
564 // valid.
565 // - Rotate the bounds and notify activity manager if the task can be resized independently
566 // from its stack. The stack will take care of task rotation for the other case.
Chong Zhangf66db432016-01-13 10:39:51 -0800567 mTmpRect2.set(mPreScrollBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800568
569 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
570 setBounds(mTmpRect2, mOverrideConfig);
571 return;
572 }
573
Wale Ogunwale94744212015-09-21 19:01:47 -0700574 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700575 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800576 // Post message to inform activity manager of the bounds change simulating a one-way
577 // call. We do this to prevent a deadlock between window manager lock and activity
578 // manager lock been held.
579 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
580 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mPreScrollBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700581 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700582 }
583
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700584 void resizeWindows() {
585 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
586 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800587 final AppWindowToken atoken = mAppTokens.get(activityNdx);
588
589 // Some windows won't go through the resizing process, if they don't have a surface, so
590 // destroy all saved surfaces here.
591 atoken.destroySavedSurfaces();
592 final ArrayList<WindowState> windows = atoken.allAppWindows;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700593 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
594 final WindowState win = windows.get(winNdx);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800595 if (win.mHasSurface && !resizingWindows.contains(win)) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800596 if (DEBUG_RESIZE) Slog.d(TAG, "resizeWindows: Resizing " + win);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700597 resizingWindows.add(win);
598 }
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800599 if (win.isGoneForLayoutLw()) {
600 win.mResizedWhileGone = true;
601 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700602 }
603 }
604 }
605
Chong Zhangbd0d9372015-12-28 15:18:29 -0800606 void moveWindows() {
607 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
608 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
609 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
610 final WindowState win = windows.get(winNdx);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800611 if (DEBUG_RESIZE) Slog.d(TAG, "moveWindows: Moving " + win);
Chong Zhangbd0d9372015-12-28 15:18:29 -0800612 win.mMovedByResize = true;
613 }
614 }
615 }
616
Winsonc28098f2015-10-30 14:50:19 -0700617 /**
618 * Cancels any running app transitions associated with the task.
619 */
620 void cancelTaskWindowTransition() {
621 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
622 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
623 }
624 }
625
Winson13d30662015-11-06 15:30:29 -0800626 /**
627 * Cancels any running thumbnail transitions associated with the task.
628 */
629 void cancelTaskThumbnailTransition() {
630 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
631 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
632 }
633 }
634
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700635 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700636 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700637 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700638 }
639
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800640 boolean isVisibleForUser() {
641 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
642 final AppWindowToken appToken = mAppTokens.get(i);
643 for (int j = appToken.allAppWindows.size() - 1; j >= 0; j--) {
644 WindowState window = appToken.allAppWindows.get(j);
645 if (!window.isHiddenFromUserLocked()) {
646 return true;
647 }
648 }
649 }
650 return false;
651 }
652
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700653 boolean inHomeStack() {
654 return mStack != null && mStack.mStackId == HOME_STACK_ID;
655 }
656
Chong Zhang09b21ef2015-09-14 10:20:21 -0700657 boolean inFreeformWorkspace() {
658 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
659 }
660
Filip Gruszczynski955b2fc2015-10-15 14:46:07 -0700661 boolean inDockedWorkspace() {
662 return mStack != null && mStack.mStackId == DOCKED_STACK_ID;
663 }
664
Chong Zhangb15758a2015-11-17 12:12:03 -0800665 boolean isResizeableByDockedStack() {
Wale Ogunwale5e7409c2016-01-11 12:24:19 -0800666 final DisplayContent displayContent = getDisplayContent();
667 return displayContent != null && displayContent.getDockedStackLocked() != null
668 && mStack != null && StackId.isTaskResizeableByDockedStack(mStack.mStackId);
Chong Zhangb15758a2015-11-17 12:12:03 -0800669 }
670
Robert Carre6275582016-02-29 15:45:45 -0800671 boolean isFloating() {
672 return StackId.tasksAreFloating(mStack.mStackId);
673 }
674
Chong Zhangb15758a2015-11-17 12:12:03 -0800675 /**
676 * Whether the task should be treated as if it's docked. Returns true if the task
677 * is currently in docked workspace, or it's side-by-side to a docked task.
678 */
679 boolean isDockedInEffect() {
680 return inDockedWorkspace() || isResizeableByDockedStack();
681 }
682
Chong Zhang2a88fc32016-01-11 17:14:24 -0800683 boolean isTwoFingerScrollMode() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800684 return inCropWindowsResizeMode() && isDockedInEffect();
Chong Zhang2a88fc32016-01-11 17:14:24 -0800685 }
686
Chong Zhangd8ceb852015-11-11 14:53:41 -0800687 WindowState getTopVisibleAppMainWindow() {
688 final AppWindowToken token = getTopVisibleAppToken();
689 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700690 }
691
Chong Zhangd8ceb852015-11-11 14:53:41 -0800692 AppWindowToken getTopVisibleAppToken() {
693 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
694 final AppWindowToken token = mAppTokens.get(i);
695 // skip hidden (or about to hide) apps
696 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
697 return token;
698 }
699 }
700 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700701 }
702
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800703 @Override
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700704 public boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700705 if (useCurrentBounds()) {
706 return mFullscreen;
707 }
708 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
709 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
710 // system.
711 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700712 }
713
714 @Override
715 public DisplayInfo getDisplayInfo() {
716 return mStack.getDisplayContent().getDisplayInfo();
717 }
718
719 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800720 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800721 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800722 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700723
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700724 @Override
725 public String toShortString() {
726 return "Task=" + mTaskId;
727 }
728
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800729 public void dump(String prefix, PrintWriter pw) {
730 final String doublePrefix = prefix + " ";
731
732 pw.println(prefix + "taskId=" + mTaskId);
733 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
734 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
735 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
736 pw.println(doublePrefix + "appTokens=" + mAppTokens);
737 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
738
739 final String triplePrefix = doublePrefix + " ";
740
741 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
742 final AppWindowToken wtoken = mAppTokens.get(i);
743 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
744 wtoken.dump(pw, triplePrefix);
745 }
746
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700747 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800748}