blob: fe55e80d76f7137af8c80947aad2b2514115140a [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;
Jorim Jaggic6c89a82016-01-28 17:48:21 -080021import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070022import static android.app.ActivityManager.StackId.HOME_STACK_ID;
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;
104
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800105 private boolean mHomeTask;
106
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700107 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
108 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -0800109 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800110 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -0700111 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -0800112 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700113 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800114 }
115
116 DisplayContent getDisplayContent() {
117 return mStack.getDisplayContent();
118 }
119
Chong Zhangc806d902015-11-30 09:44:27 -0800120 void setShowNonResizeableDockToast() {
121 mShowNonResizeableDockToast = true;
122 }
123
124 void scheduleShowNonResizeableDockToastIfNeeded() {
125 if (!mShowNonResizeableDockToast) {
126 return;
127 }
128 final DisplayContent displayContent = mStack.getDisplayContent();
129 // If docked stack is not yet visible, we don't want to show the toast yet,
130 // since we need the visible rect of the docked task to position the toast.
131 if (displayContent == null || displayContent.getDockedStackLocked() == null) {
132 return;
133 }
134
135 mShowNonResizeableDockToast = false;
136
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800137 if (isResizeable()) {
138 Slog.wtf(TAG,
139 "Trying to show non-resizeable toast when task is resizeable task=" + this);
140 return;
141 }
142
143 if (mResizeMode == RESIZE_MODE_UNRESIZEABLE) {
144 final String text =
145 mService.mContext.getString(R.string.dock_non_resizeble_failed_to_dock_text);
146 mService.mH.obtainMessage(SHOW_NON_RESIZEABLE_DOCK_TOAST, 0, 0, text).sendToTarget();
147 return;
148 }
149
Chong Zhangc806d902015-11-30 09:44:27 -0800150 final int dockSide = mStack.getDockSide();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800151 if (!inCropWindowsResizeMode() || dockSide == DOCKED_INVALID) {
152 return;
153 }
154
Chong Zhangc806d902015-11-30 09:44:27 -0800155 int xOffset = 0;
156 int yOffset = 0;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800157 mStack.getBounds(mTmpRect);
Chong Zhangc806d902015-11-30 09:44:27 -0800158
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800159 if (dockSide == DOCKED_LEFT || dockSide == DOCKED_RIGHT) {
160 // The toast was originally placed at the bottom and centered. To place it at the
161 // bottom-center of the stack, we offset it horizontally by the diff between the center
162 // of the stack bounds vs. the center of the screen.
163 displayContent.getLogicalDisplayRect(mTmpRect2);
164 xOffset = mTmpRect.centerX() - mTmpRect2.centerX();
165 } else if (dockSide == DOCKED_TOP) {
166 // The toast was originally placed at the bottom and centered. To place it at the bottom
167 // center of the top stack, we offset it vertically by the diff between the bottom of
168 // the stack bounds vs. the bottom of the content rect.
169 //
170 // Note here we use the content rect instead of the display rect, as we want the toast's
171 // distance to the dock divider (when it's placed at the top half) to be the same as
172 // it's distance to the top of the navigation bar (when it's placed at the bottom).
Chong Zhangf66db432016-01-13 10:39:51 -0800173
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800174 // We don't adjust for DOCKED_BOTTOM case since it's already at the bottom.
175 displayContent.getContentRect(mTmpRect2);
176 yOffset = mTmpRect2.bottom - mTmpRect.bottom;
Chong Zhangc806d902015-11-30 09:44:27 -0800177 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800178 final String text =
179 mService.mContext.getString(R.string.dock_cropped_windows_text);
180 mService.mH.obtainMessage(SHOW_NON_RESIZEABLE_DOCK_TOAST,
181 xOffset, yOffset, text).sendToTarget();
Chong Zhangc806d902015-11-30 09:44:27 -0800182 }
183
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800184 void addAppToken(int addPos, AppWindowToken wtoken, int resizeMode, boolean homeTask) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800185 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800186 if (addPos >= lastPos) {
187 addPos = lastPos;
188 } else {
189 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
190 if (mAppTokens.get(pos).removed) {
191 // addPos assumes removed tokens are actually gone.
192 ++addPos;
193 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700194 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800195 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800196 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800197 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800198 mDeferRemoval = false;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800199 mResizeMode = resizeMode;
200 mHomeTask = homeTask;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800201 }
202
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800203 private boolean hasAppTokensAlive() {
204 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
205 if (!mAppTokens.get(i).appDied) {
206 return true;
207 }
208 }
209 return false;
210 }
211
Craig Mautnere3119b72015-01-20 15:02:36 -0800212 void removeLocked() {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800213 if (hasAppTokensAlive() && mStack.isAnimating()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800214 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800215 mDeferRemoval = true;
216 return;
217 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800218 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800219 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800220 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700221 DisplayContent content = getDisplayContent();
222 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800223 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700224 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800225 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800226 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800227 }
228
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800229 void moveTaskToStack(TaskStack stack, boolean toTop) {
230 if (stack == mStack) {
231 return;
232 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800233 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800234 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700235 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800236 if (mStack != null) {
237 mStack.removeTask(this);
238 }
239 stack.addTask(this, toTop);
240 }
241
Wale Ogunwale935e5022015-11-10 12:36:10 -0800242 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700243 if (mStack != null && stack != mStack) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800244 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700245 + " from stack=" + mStack);
246 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
247 mStack.removeTask(this);
248 }
249 stack.positionTask(this, position, showForAllUsers());
Wale Ogunwale935e5022015-11-10 12:36:10 -0800250 setBounds(bounds, config);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700251 }
252
Craig Mautnerc00204b2013-03-05 15:02:14 -0800253 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800254 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800255 if (mAppTokens.size() == 0) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100256 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800257 if (mDeferRemoval) {
258 removeLocked();
259 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800260 }
Craig Mautner83162a92015-01-26 14:43:30 -0800261 wtoken.mTask = null;
262 /* Leave mTaskId for now, it might be useful for debug
263 wtoken.mTaskId = -1;
264 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800265 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800266 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800267
Craig Mautnercbd84af2014-10-22 13:21:22 -0700268 void setSendingToBottom(boolean toBottom) {
269 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
270 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
271 }
272 }
273
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700274 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700275 int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700276 if (config == null) {
277 config = Configuration.EMPTY;
278 }
279 if (bounds == null && !Configuration.EMPTY.equals(config)) {
280 throw new IllegalArgumentException("null bounds but non empty configuration: "
281 + config);
282 }
283 if (bounds != null && Configuration.EMPTY.equals(config)) {
284 throw new IllegalArgumentException("non null bounds, but empty configuration");
285 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700286 boolean oldFullscreen = mFullscreen;
287 int rotation = Surface.ROTATION_0;
288 final DisplayContent displayContent = mStack.getDisplayContent();
289 if (displayContent != null) {
290 displayContent.getLogicalDisplayRect(mTmpRect);
291 rotation = displayContent.getDisplayInfo().rotation;
Jorim Jaggi067e8172016-02-03 18:24:12 -0800292 mFullscreen = bounds == null;
293 if (mFullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700294 bounds = mTmpRect;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700295 }
296 }
297
298 if (bounds == null) {
299 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700300 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700301 }
Chong Zhangf66db432016-01-13 10:39:51 -0800302 if (mPreScrollBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700303 return BOUNDS_CHANGE_NONE;
304 }
305
306 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhangf66db432016-01-13 10:39:51 -0800307 if (mPreScrollBounds.left != bounds.left || mPreScrollBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700308 boundsChange |= BOUNDS_CHANGE_POSITION;
309 }
Chong Zhangf66db432016-01-13 10:39:51 -0800310 if (mPreScrollBounds.width() != bounds.width() || mPreScrollBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700311 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700312 }
313
Chong Zhangf66db432016-01-13 10:39:51 -0800314
315 mPreScrollBounds.set(bounds);
316
317 resetScrollLocked();
318
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700319 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700320 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800321 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700322 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700323 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700324 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700325 }
326
Jorim Jaggidc249c42015-12-15 14:57:31 -0800327 /**
328 * Sets the bounds used to calculate the insets. See
329 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
330 */
331 void setTempInsetBounds(Rect tempInsetBounds) {
332 if (tempInsetBounds != null) {
333 mTempInsetBounds.set(tempInsetBounds);
334 } else {
335 mTempInsetBounds.setEmpty();
336 }
337 }
338
339 /**
340 * Gets the bounds used to calculate the insets. See
341 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
342 */
343 void getTempInsetBounds(Rect out) {
344 out.set(mTempInsetBounds);
345 }
346
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800347 void setResizeable(int resizeMode) {
348 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800349 }
350
351 boolean isResizeable() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800352 return !mHomeTask
353 && (ActivityInfo.isResizeableMode(mResizeMode) || mService.mForceResizableTasks);
354 }
355
356 boolean cropWindowsToStackBounds() {
357 return !mHomeTask && (isResizeable() || mResizeMode == RESIZE_MODE_CROP_WINDOWS);
358 }
359
360 private boolean inCropWindowsResizeMode() {
361 return !mHomeTask && !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
Chong Zhangb15758a2015-11-17 12:12:03 -0800362 }
363
Chong Zhang87b21722015-09-21 15:39:51 -0700364 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700365 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700366 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700367 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700368 }
369 if (boundsChanged == BOUNDS_CHANGE_NONE) {
370 return false;
371 }
372 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
373 resizeWindows();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800374 } else {
375 moveWindows();
Chong Zhang3005e752015-09-18 18:46:28 -0700376 }
377 return true;
378 }
379
Jorim Jaggi0429f352015-12-22 16:29:16 +0100380 /**
381 * Prepares the task bounds to be frozen with the current size. See
382 * {@link AppWindowToken#freezeBounds}.
383 */
384 void prepareFreezingBounds() {
385 mPreparedFrozenBounds.set(mBounds);
386 }
387
Chong Zhangf66db432016-01-13 10:39:51 -0800388 void resetScrollLocked() {
389 if (mScrollValid) {
390 mScrollValid = false;
391 applyScrollToAllWindows(0, 0);
392 }
393 mBounds.set(mPreScrollBounds);
394 }
395
396 void applyScrollToAllWindows(final int xOffset, final int yOffset) {
397 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
398 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
399 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
400 final WindowState win = windows.get(winNdx);
401 win.mXOffset = xOffset;
402 win.mYOffset = yOffset;
403 }
404 }
405 }
406
407 void applyScrollToWindowIfNeeded(final WindowState win) {
408 if (mScrollValid) {
409 win.mXOffset = mBounds.left;
410 win.mYOffset = mBounds.top;
411 }
412 }
413
Chong Zhangb15758a2015-11-17 12:12:03 -0800414 boolean scrollLocked(Rect bounds) {
415 // shift the task bound if it doesn't fully cover the stack area
416 mStack.getDimBounds(mTmpRect);
417 if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) {
418 if (bounds.left > mTmpRect.left) {
419 bounds.left = mTmpRect.left;
420 bounds.right = mTmpRect.left + mBounds.width();
421 } else if (bounds.right < mTmpRect.right) {
422 bounds.left = mTmpRect.right - mBounds.width();
423 bounds.right = mTmpRect.right;
424 }
425 } else {
426 if (bounds.top > mTmpRect.top) {
427 bounds.top = mTmpRect.top;
428 bounds.bottom = mTmpRect.top + mBounds.height();
429 } else if (bounds.bottom < mTmpRect.bottom) {
430 bounds.top = mTmpRect.bottom - mBounds.height();
431 bounds.bottom = mTmpRect.bottom;
432 }
433 }
434
Chong Zhangf66db432016-01-13 10:39:51 -0800435 // We can stop here if we're already scrolling and the scrolled bounds not changed.
436 if (mScrollValid && bounds.equals(mBounds)) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800437 return false;
438 }
Chong Zhangf66db432016-01-13 10:39:51 -0800439
Chong Zhangb15758a2015-11-17 12:12:03 -0800440 // Normal setBounds() does not allow non-null bounds for fullscreen apps.
441 // We only change bounds for the scrolling case without change it size,
442 // on resizing path we should still want the validation.
443 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800444 mScrollValid = true;
445 applyScrollToAllWindows(bounds.left, bounds.top);
Chong Zhangb15758a2015-11-17 12:12:03 -0800446 return true;
447 }
448
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700449 /** Return true if the current bound can get outputted to the rest of the system as-is. */
450 private boolean useCurrentBounds() {
451 final DisplayContent displayContent = mStack.getDisplayContent();
452 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700453 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700454 || displayContent == null
455 || displayContent.getDockedStackLocked() != null) {
456 return true;
457 }
458 return false;
459 }
460
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800461 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800462 void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700463 if (useCurrentBounds()) {
464 // No need to adjust the output bounds if fullscreen or the docked stack is visible
465 // since it is already what we want to represent to the rest of the system.
466 out.set(mBounds);
467 return;
468 }
469
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800470 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
471 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700472 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700473 }
474
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800475 /**
476 * Calculate the maximum visible area of this task. If the task has only one app,
477 * the result will be visible frame of that app. If the task has more than one apps,
478 * we search from top down if the next app got different visible area.
479 *
480 * This effort is to handle the case where some task (eg. GMail composer) might pop up
481 * a dialog that's different in size from the activity below, in which case we should
482 * be dimming the entire task area behind the dialog.
483 *
484 * @param out Rect containing the max visible bounds.
485 * @return true if the task has some visible app windows; false otherwise.
486 */
487 boolean getMaxVisibleBounds(Rect out) {
488 boolean foundTop = false;
489 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800490 final AppWindowToken token = mAppTokens.get(i);
491 // skip hidden (or about to hide) apps
492 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
493 continue;
494 }
495 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800496 if (win == null) {
497 continue;
498 }
499 if (!foundTop) {
500 out.set(win.mVisibleFrame);
501 foundTop = true;
502 continue;
503 }
504 if (win.mVisibleFrame.left < out.left) {
505 out.left = win.mVisibleFrame.left;
506 }
507 if (win.mVisibleFrame.top < out.top) {
508 out.top = win.mVisibleFrame.top;
509 }
510 if (win.mVisibleFrame.right > out.right) {
511 out.right = win.mVisibleFrame.right;
512 }
513 if (win.mVisibleFrame.bottom > out.bottom) {
514 out.bottom = win.mVisibleFrame.bottom;
515 }
516 }
517 return foundTop;
518 }
519
520 /** Bounds of the task to be used for dimming, as well as touch related tests. */
521 @Override
522 public void getDimBounds(Rect out) {
523 if (useCurrentBounds()) {
524 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
525 return;
526 }
527
528 out.set(mBounds);
529 return;
530 }
531
532 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
533 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
534 // system.
535 mStack.getDisplayContent().getLogicalDisplayRect(out);
536 }
537
Chong Zhang3005e752015-09-18 18:46:28 -0700538 void setDragResizing(boolean dragResizing) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800539 if (mDragResizing != dragResizing) {
540 mDragResizing = dragResizing;
541 resetDragResizingChangeReported();
542 }
543 }
544
545 void resetDragResizingChangeReported() {
546 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
547 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
548 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
549 final WindowState win = windows.get(winNdx);
550 win.resetDragResizingChangeReported();
551 }
552 }
Chong Zhang3005e752015-09-18 18:46:28 -0700553 }
554
555 boolean isDragResizing() {
Wale Ogunwalece144522016-02-05 22:51:01 -0800556 return mDragResizing || (mStack != null && mStack.isDragResizing());
Chong Zhang3005e752015-09-18 18:46:28 -0700557 }
558
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700559 void updateDisplayInfo(final DisplayContent displayContent) {
560 if (displayContent == null) {
561 return;
562 }
563 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700564 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700565 return;
566 }
567 final int newRotation = displayContent.getDisplayInfo().rotation;
568 if (mRotation == newRotation) {
569 return;
570 }
571
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800572 // Device rotation changed.
573 // - Reset the bounds to the pre-scroll bounds as whatever scrolling was done is no longer
574 // valid.
575 // - Rotate the bounds and notify activity manager if the task can be resized independently
576 // from its stack. The stack will take care of task rotation for the other case.
Chong Zhangf66db432016-01-13 10:39:51 -0800577 mTmpRect2.set(mPreScrollBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800578
579 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
580 setBounds(mTmpRect2, mOverrideConfig);
581 return;
582 }
583
Wale Ogunwale94744212015-09-21 19:01:47 -0700584 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700585 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800586 // Post message to inform activity manager of the bounds change simulating a one-way
587 // call. We do this to prevent a deadlock between window manager lock and activity
588 // manager lock been held.
589 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
590 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mPreScrollBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700591 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700592 }
593
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700594 void resizeWindows() {
595 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
596 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
597 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
598 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
599 final WindowState win = windows.get(winNdx);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800600 if (win.mHasSurface && !resizingWindows.contains(win)) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800601 if (DEBUG_RESIZE) Slog.d(TAG, "resizeWindows: Resizing " + win);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700602 resizingWindows.add(win);
603 }
604 }
605 }
606 }
607
Chong Zhangbd0d9372015-12-28 15:18:29 -0800608 void moveWindows() {
609 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
610 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
611 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
612 final WindowState win = windows.get(winNdx);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800613 if (DEBUG_RESIZE) Slog.d(TAG, "moveWindows: Moving " + win);
Chong Zhangbd0d9372015-12-28 15:18:29 -0800614 win.mMovedByResize = true;
615 }
616 }
617 }
618
Winsonc28098f2015-10-30 14:50:19 -0700619 /**
620 * Cancels any running app transitions associated with the task.
621 */
622 void cancelTaskWindowTransition() {
623 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
624 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
625 }
626 }
627
Winson13d30662015-11-06 15:30:29 -0800628 /**
629 * Cancels any running thumbnail transitions associated with the task.
630 */
631 void cancelTaskThumbnailTransition() {
632 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
633 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
634 }
635 }
636
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700637 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700638 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700639 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700640 }
641
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700642 boolean inHomeStack() {
643 return mStack != null && mStack.mStackId == HOME_STACK_ID;
644 }
645
Chong Zhang09b21ef2015-09-14 10:20:21 -0700646 boolean inFreeformWorkspace() {
647 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
648 }
649
Filip Gruszczynski955b2fc2015-10-15 14:46:07 -0700650 boolean inDockedWorkspace() {
651 return mStack != null && mStack.mStackId == DOCKED_STACK_ID;
652 }
653
Chong Zhangb15758a2015-11-17 12:12:03 -0800654 boolean isResizeableByDockedStack() {
Wale Ogunwale5e7409c2016-01-11 12:24:19 -0800655 final DisplayContent displayContent = getDisplayContent();
656 return displayContent != null && displayContent.getDockedStackLocked() != null
657 && mStack != null && StackId.isTaskResizeableByDockedStack(mStack.mStackId);
Chong Zhangb15758a2015-11-17 12:12:03 -0800658 }
659
660 /**
661 * Whether the task should be treated as if it's docked. Returns true if the task
662 * is currently in docked workspace, or it's side-by-side to a docked task.
663 */
664 boolean isDockedInEffect() {
665 return inDockedWorkspace() || isResizeableByDockedStack();
666 }
667
Chong Zhang2a88fc32016-01-11 17:14:24 -0800668 boolean isTwoFingerScrollMode() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800669 return inCropWindowsResizeMode() && isDockedInEffect();
Chong Zhang2a88fc32016-01-11 17:14:24 -0800670 }
671
Chong Zhangd8ceb852015-11-11 14:53:41 -0800672 WindowState getTopVisibleAppMainWindow() {
673 final AppWindowToken token = getTopVisibleAppToken();
674 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700675 }
676
Chong Zhangd8ceb852015-11-11 14:53:41 -0800677 AppWindowToken getTopVisibleAppToken() {
678 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
679 final AppWindowToken token = mAppTokens.get(i);
680 // skip hidden (or about to hide) apps
681 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
682 return token;
683 }
684 }
685 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700686 }
687
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800688 @Override
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700689 public boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700690 if (useCurrentBounds()) {
691 return mFullscreen;
692 }
693 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
694 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
695 // system.
696 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700697 }
698
699 @Override
700 public DisplayInfo getDisplayInfo() {
701 return mStack.getDisplayContent().getDisplayInfo();
702 }
703
704 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800705 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800706 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800707 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700708
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700709 @Override
710 public String toShortString() {
711 return "Task=" + mTaskId;
712 }
713
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800714 public void dump(String prefix, PrintWriter pw) {
715 final String doublePrefix = prefix + " ";
716
717 pw.println(prefix + "taskId=" + mTaskId);
718 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
719 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
720 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
721 pw.println(doublePrefix + "appTokens=" + mAppTokens);
722 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
723
724 final String triplePrefix = doublePrefix + " ";
725
726 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
727 final AppWindowToken wtoken = mAppTokens.get(i);
728 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
729 wtoken.dump(pw, triplePrefix);
730 }
731
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700732 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800733}