blob: 7ee26a048185a5dc6fb21684f3ab9a1c05a85495 [file] [log] [blame]
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Jorim Jaggid53f0922016-04-06 22:16:23 -070019import static android.app.ActivityManager.RESIZE_MODE_SYSTEM_SCREEN_ROTATION;
Wale Ogunwale3797c222015-10-27 14:21:58 -070020import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
21import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080023import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS;
Jorim Jaggid53f0922016-04-06 22:16:23 -070024import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080025import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_RESIZE;
26import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Jorim Jaggid53f0922016-04-06 22:16:23 -070027import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
28import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070029import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Robert Carr2c17cd22016-04-08 17:52:28 -070030import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Wale Ogunwale99db1862015-10-23 20:08:22 -070031
Wale Ogunwale3797c222015-10-27 14:21:58 -070032import android.app.ActivityManager.StackId;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080033import android.content.pm.ActivityInfo;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070034import android.content.res.Configuration;
35import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080036import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080037import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070038import android.view.DisplayInfo;
39import android.view.Surface;
40
Craig Mautnere3119b72015-01-20 15:02:36 -080041import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080042
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070043import java.io.PrintWriter;
44import java.util.ArrayList;
45
46class Task implements DimLayer.DimLayerUser {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080047 static final String TAG = TAG_WITH_CLASS_NAME ? "Task" : TAG_WM;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070048 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
49 static final int BOUNDS_CHANGE_NONE = 0;
50 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
51 static final int BOUNDS_CHANGE_POSITION = 1;
52 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
53 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
54
Craig Mautnerc00204b2013-03-05 15:02:14 -080055 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080056 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080057 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070058 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080059 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080060 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080061
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070062 // Content limits relative to the DisplayContent this sits in.
63 private Rect mBounds = new Rect();
Jorim Jaggi0429f352015-12-22 16:29:16 +010064 final Rect mPreparedFrozenBounds = new Rect();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070065
Chong Zhangf66db432016-01-13 10:39:51 -080066 private Rect mPreScrollBounds = new Rect();
67 private boolean mScrollValid;
68
Jorim Jaggidc249c42015-12-15 14:57:31 -080069 // Bounds used to calculate the insets.
70 private final Rect mTempInsetBounds = new Rect();
71
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070072 // Device rotation as of the last time {@link #mBounds} was set.
73 int mRotation;
74
75 // Whether mBounds is fullscreen
76 private boolean mFullscreen = true;
77
78 // Contains configurations settings that are different from the global configuration due to
79 // stack specific operations. E.g. {@link #setBounds}.
80 Configuration mOverrideConfig;
81
82 // For comparison with DisplayContent bounds.
83 private Rect mTmpRect = new Rect();
84 // For handling display rotations.
85 private Rect mTmpRect2 = new Rect();
86
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080087 // Resize mode of the task. See {@link ActivityInfo#resizeMode}
88 private int mResizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -080089
Chong Zhang3005e752015-09-18 18:46:28 -070090 // Whether the task is currently being drag-resized
91 private boolean mDragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010092 private int mDragResizeMode;
Chong Zhang3005e752015-09-18 18:46:28 -070093
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080094 private boolean mHomeTask;
95
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070096 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
97 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -080098 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -080099 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -0700100 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -0800101 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700102 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800103 }
104
105 DisplayContent getDisplayContent() {
106 return mStack.getDisplayContent();
107 }
108
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800109 void addAppToken(int addPos, AppWindowToken wtoken, int resizeMode, boolean homeTask) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800110 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800111 if (addPos >= lastPos) {
112 addPos = lastPos;
113 } else {
114 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
115 if (mAppTokens.get(pos).removed) {
116 // addPos assumes removed tokens are actually gone.
117 ++addPos;
118 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700119 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800120 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800121 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800122 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800123 mDeferRemoval = false;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800124 mResizeMode = resizeMode;
125 mHomeTask = homeTask;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800126 }
127
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700128 private boolean hasWindowsAlive() {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800129 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700130 if (mAppTokens.get(i).hasWindowsAlive()) {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800131 return true;
132 }
133 }
134 return false;
135 }
136
Craig Mautnere3119b72015-01-20 15:02:36 -0800137 void removeLocked() {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700138 if (hasWindowsAlive() && mStack.isAnimating()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800139 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800140 mDeferRemoval = true;
141 return;
142 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800143 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800144 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800145 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700146 DisplayContent content = getDisplayContent();
147 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800148 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700149 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800150 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800151 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800152 }
153
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800154 void moveTaskToStack(TaskStack stack, boolean toTop) {
155 if (stack == mStack) {
156 return;
157 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800158 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800159 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700160 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800161 if (mStack != null) {
162 mStack.removeTask(this);
163 }
164 stack.addTask(this, toTop);
165 }
166
Wale Ogunwale935e5022015-11-10 12:36:10 -0800167 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700168 if (mStack != null && stack != mStack) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800169 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700170 + " from stack=" + mStack);
171 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
172 mStack.removeTask(this);
173 }
174 stack.positionTask(this, position, showForAllUsers());
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800175 resizeLocked(bounds, config, false /* force */);
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800176
177 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
178 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
179 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
180 final WindowState win = windows.get(winNdx);
181 win.notifyMovedInStack();
182 }
183 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700184 }
185
Craig Mautnerc00204b2013-03-05 15:02:14 -0800186 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800187 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800188 if (mAppTokens.size() == 0) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100189 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800190 if (mDeferRemoval) {
191 removeLocked();
192 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800193 }
Craig Mautner83162a92015-01-26 14:43:30 -0800194 wtoken.mTask = null;
195 /* Leave mTaskId for now, it might be useful for debug
196 wtoken.mTaskId = -1;
197 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800198 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800199 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800200
Craig Mautnercbd84af2014-10-22 13:21:22 -0700201 void setSendingToBottom(boolean toBottom) {
202 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
203 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
204 }
205 }
206
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700207 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800208 private int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700209 if (config == null) {
210 config = Configuration.EMPTY;
211 }
212 if (bounds == null && !Configuration.EMPTY.equals(config)) {
213 throw new IllegalArgumentException("null bounds but non empty configuration: "
214 + config);
215 }
216 if (bounds != null && Configuration.EMPTY.equals(config)) {
217 throw new IllegalArgumentException("non null bounds, but empty configuration");
218 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700219 boolean oldFullscreen = mFullscreen;
220 int rotation = Surface.ROTATION_0;
221 final DisplayContent displayContent = mStack.getDisplayContent();
222 if (displayContent != null) {
223 displayContent.getLogicalDisplayRect(mTmpRect);
224 rotation = displayContent.getDisplayInfo().rotation;
Jorim Jaggi067e8172016-02-03 18:24:12 -0800225 mFullscreen = bounds == null;
226 if (mFullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700227 bounds = mTmpRect;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700228 }
229 }
230
231 if (bounds == null) {
232 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700233 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700234 }
Chong Zhangf66db432016-01-13 10:39:51 -0800235 if (mPreScrollBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700236 return BOUNDS_CHANGE_NONE;
237 }
238
239 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhangf66db432016-01-13 10:39:51 -0800240 if (mPreScrollBounds.left != bounds.left || mPreScrollBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700241 boundsChange |= BOUNDS_CHANGE_POSITION;
242 }
Chong Zhangf66db432016-01-13 10:39:51 -0800243 if (mPreScrollBounds.width() != bounds.width() || mPreScrollBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700244 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700245 }
246
Chong Zhangf66db432016-01-13 10:39:51 -0800247
248 mPreScrollBounds.set(bounds);
249
250 resetScrollLocked();
251
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700252 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700253 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800254 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700255 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700256 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700257 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700258 }
259
Jorim Jaggidc249c42015-12-15 14:57:31 -0800260 /**
261 * Sets the bounds used to calculate the insets. See
262 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
263 */
264 void setTempInsetBounds(Rect tempInsetBounds) {
265 if (tempInsetBounds != null) {
266 mTempInsetBounds.set(tempInsetBounds);
267 } else {
268 mTempInsetBounds.setEmpty();
269 }
270 }
271
272 /**
273 * Gets the bounds used to calculate the insets. See
274 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
275 */
276 void getTempInsetBounds(Rect out) {
277 out.set(mTempInsetBounds);
278 }
279
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800280 void setResizeable(int resizeMode) {
281 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800282 }
283
284 boolean isResizeable() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800285 return !mHomeTask
286 && (ActivityInfo.isResizeableMode(mResizeMode) || mService.mForceResizableTasks);
287 }
288
289 boolean cropWindowsToStackBounds() {
290 return !mHomeTask && (isResizeable() || mResizeMode == RESIZE_MODE_CROP_WINDOWS);
291 }
292
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800293 boolean isHomeTask() {
294 return mHomeTask;
295 }
296
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800297 private boolean inCropWindowsResizeMode() {
298 return !mHomeTask && !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
Chong Zhangb15758a2015-11-17 12:12:03 -0800299 }
300
Chong Zhang87b21722015-09-21 15:39:51 -0700301 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700302 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700303 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700304 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700305 }
306 if (boundsChanged == BOUNDS_CHANGE_NONE) {
307 return false;
308 }
309 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
310 resizeWindows();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800311 } else {
312 moveWindows();
Chong Zhang3005e752015-09-18 18:46:28 -0700313 }
314 return true;
315 }
316
Jorim Jaggi0429f352015-12-22 16:29:16 +0100317 /**
318 * Prepares the task bounds to be frozen with the current size. See
319 * {@link AppWindowToken#freezeBounds}.
320 */
321 void prepareFreezingBounds() {
322 mPreparedFrozenBounds.set(mBounds);
323 }
324
Chong Zhangf66db432016-01-13 10:39:51 -0800325 void resetScrollLocked() {
326 if (mScrollValid) {
327 mScrollValid = false;
328 applyScrollToAllWindows(0, 0);
329 }
330 mBounds.set(mPreScrollBounds);
331 }
332
333 void applyScrollToAllWindows(final int xOffset, final int yOffset) {
334 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
335 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
336 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
337 final WindowState win = windows.get(winNdx);
338 win.mXOffset = xOffset;
339 win.mYOffset = yOffset;
340 }
341 }
342 }
343
344 void applyScrollToWindowIfNeeded(final WindowState win) {
345 if (mScrollValid) {
346 win.mXOffset = mBounds.left;
347 win.mYOffset = mBounds.top;
348 }
349 }
350
Chong Zhangb15758a2015-11-17 12:12:03 -0800351 boolean scrollLocked(Rect bounds) {
352 // shift the task bound if it doesn't fully cover the stack area
353 mStack.getDimBounds(mTmpRect);
354 if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) {
355 if (bounds.left > mTmpRect.left) {
356 bounds.left = mTmpRect.left;
357 bounds.right = mTmpRect.left + mBounds.width();
358 } else if (bounds.right < mTmpRect.right) {
359 bounds.left = mTmpRect.right - mBounds.width();
360 bounds.right = mTmpRect.right;
361 }
362 } else {
363 if (bounds.top > mTmpRect.top) {
364 bounds.top = mTmpRect.top;
365 bounds.bottom = mTmpRect.top + mBounds.height();
366 } else if (bounds.bottom < mTmpRect.bottom) {
367 bounds.top = mTmpRect.bottom - mBounds.height();
368 bounds.bottom = mTmpRect.bottom;
369 }
370 }
371
Chong Zhangf66db432016-01-13 10:39:51 -0800372 // We can stop here if we're already scrolling and the scrolled bounds not changed.
373 if (mScrollValid && bounds.equals(mBounds)) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800374 return false;
375 }
Chong Zhangf66db432016-01-13 10:39:51 -0800376
Chong Zhangb15758a2015-11-17 12:12:03 -0800377 // Normal setBounds() does not allow non-null bounds for fullscreen apps.
378 // We only change bounds for the scrolling case without change it size,
379 // on resizing path we should still want the validation.
380 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800381 mScrollValid = true;
382 applyScrollToAllWindows(bounds.left, bounds.top);
Chong Zhangb15758a2015-11-17 12:12:03 -0800383 return true;
384 }
385
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700386 /** Return true if the current bound can get outputted to the rest of the system as-is. */
387 private boolean useCurrentBounds() {
388 final DisplayContent displayContent = mStack.getDisplayContent();
389 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700390 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700391 || displayContent == null
Jorim Jaggif4fa8cb2016-04-01 16:05:10 -0700392 || displayContent.getDockedStackVisibleForUserLocked() != null) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700393 return true;
394 }
395 return false;
396 }
397
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800398 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800399 void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700400 if (useCurrentBounds()) {
401 // No need to adjust the output bounds if fullscreen or the docked stack is visible
402 // since it is already what we want to represent to the rest of the system.
403 out.set(mBounds);
404 return;
405 }
406
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800407 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
408 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700409 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700410 }
411
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800412 /**
413 * Calculate the maximum visible area of this task. If the task has only one app,
414 * the result will be visible frame of that app. If the task has more than one apps,
415 * we search from top down if the next app got different visible area.
416 *
417 * This effort is to handle the case where some task (eg. GMail composer) might pop up
418 * a dialog that's different in size from the activity below, in which case we should
419 * be dimming the entire task area behind the dialog.
420 *
421 * @param out Rect containing the max visible bounds.
422 * @return true if the task has some visible app windows; false otherwise.
423 */
424 boolean getMaxVisibleBounds(Rect out) {
425 boolean foundTop = false;
426 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800427 final AppWindowToken token = mAppTokens.get(i);
428 // skip hidden (or about to hide) apps
429 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
430 continue;
431 }
432 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800433 if (win == null) {
434 continue;
435 }
436 if (!foundTop) {
437 out.set(win.mVisibleFrame);
438 foundTop = true;
439 continue;
440 }
441 if (win.mVisibleFrame.left < out.left) {
442 out.left = win.mVisibleFrame.left;
443 }
444 if (win.mVisibleFrame.top < out.top) {
445 out.top = win.mVisibleFrame.top;
446 }
447 if (win.mVisibleFrame.right > out.right) {
448 out.right = win.mVisibleFrame.right;
449 }
450 if (win.mVisibleFrame.bottom > out.bottom) {
451 out.bottom = win.mVisibleFrame.bottom;
452 }
453 }
454 return foundTop;
455 }
456
457 /** Bounds of the task to be used for dimming, as well as touch related tests. */
458 @Override
459 public void getDimBounds(Rect out) {
Robert Carra86a6bf2016-04-08 17:34:16 -0700460 final DisplayContent displayContent = mStack.getDisplayContent();
461 // It doesn't matter if we in particular are part of the resize, since we couldn't have
462 // a DimLayer anyway if we weren't visible.
463 final boolean dockedResizing = displayContent != null ?
464 displayContent.mDividerControllerLocked.isResizing() : false;
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800465 if (useCurrentBounds()) {
466 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
467 return;
468 }
469
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700470 if (!mFullscreen) {
471 // When minimizing the docked stack when going home, we don't adjust the task bounds
472 // so we need to intersect the task bounds with the stack bounds here.
Robert Carra86a6bf2016-04-08 17:34:16 -0700473 //
474 // If we are Docked Resizing with snap points, the task bounds could be smaller than the stack
475 // bounds and so we don't even want to use them. Even if the app should not be resized the Dim
476 // should keep up with the divider.
477 if (dockedResizing) {
478 mStack.getBounds(out);
479 } else {
480 mStack.getBounds(mTmpRect);
481 mTmpRect.intersect(mBounds);
482 }
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700483 out.set(mTmpRect);
484 } else {
485 out.set(mBounds);
486 }
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800487 return;
488 }
489
490 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
491 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
492 // system.
Robert Carra86a6bf2016-04-08 17:34:16 -0700493 displayContent.getLogicalDisplayRect(out);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800494 }
495
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100496 void setDragResizing(boolean dragResizing, int dragResizeMode) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800497 if (mDragResizing != dragResizing) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100498 if (!DragResizeMode.isModeAllowedForStack(mStack.mStackId, dragResizeMode)) {
499 throw new IllegalArgumentException("Drag resize mode not allow for stack stackId="
500 + mStack.mStackId + " dragResizeMode=" + dragResizeMode);
501 }
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800502 mDragResizing = dragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100503 mDragResizeMode = dragResizeMode;
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800504 resetDragResizingChangeReported();
505 }
506 }
507
508 void resetDragResizingChangeReported() {
509 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
510 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
511 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
512 final WindowState win = windows.get(winNdx);
513 win.resetDragResizingChangeReported();
514 }
515 }
Chong Zhang3005e752015-09-18 18:46:28 -0700516 }
517
518 boolean isDragResizing() {
Wale Ogunwalece144522016-02-05 22:51:01 -0800519 return mDragResizing || (mStack != null && mStack.isDragResizing());
Chong Zhang3005e752015-09-18 18:46:28 -0700520 }
521
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100522 int getDragResizeMode() {
523 return mDragResizeMode;
524 }
525
Jorim Jaggiff71d202016-04-14 13:12:36 -0700526 /**
527 * Adds all of the tasks windows to {@link WindowManagerService#mWaitingForDrawn} if drag
528 * resizing state of the window has been changed.
529 */
530 void addWindowsWaitingForDrawnIfResizingChanged() {
531 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
532 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
533 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
534 final WindowState win = windows.get(winNdx);
535 if (win.isDragResizeChanged()) {
536 mService.mWaitingForDrawn.add(win);
537 }
538 }
539 }
540 }
541
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700542 void updateDisplayInfo(final DisplayContent displayContent) {
543 if (displayContent == null) {
544 return;
545 }
546 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700547 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700548 return;
549 }
550 final int newRotation = displayContent.getDisplayInfo().rotation;
551 if (mRotation == newRotation) {
552 return;
553 }
554
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800555 // Device rotation changed.
556 // - Reset the bounds to the pre-scroll bounds as whatever scrolling was done is no longer
557 // valid.
558 // - Rotate the bounds and notify activity manager if the task can be resized independently
559 // from its stack. The stack will take care of task rotation for the other case.
Chong Zhangf66db432016-01-13 10:39:51 -0800560 mTmpRect2.set(mPreScrollBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800561
562 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
563 setBounds(mTmpRect2, mOverrideConfig);
564 return;
565 }
566
Wale Ogunwale94744212015-09-21 19:01:47 -0700567 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700568 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800569 // Post message to inform activity manager of the bounds change simulating a one-way
570 // call. We do this to prevent a deadlock between window manager lock and activity
571 // manager lock been held.
572 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
573 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mPreScrollBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700574 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700575 }
576
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700577 void resizeWindows() {
578 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
579 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800580 final AppWindowToken atoken = mAppTokens.get(activityNdx);
581
582 // Some windows won't go through the resizing process, if they don't have a surface, so
583 // destroy all saved surfaces here.
584 atoken.destroySavedSurfaces();
585 final ArrayList<WindowState> windows = atoken.allAppWindows;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700586 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
587 final WindowState win = windows.get(winNdx);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800588 if (win.mHasSurface && !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);
Jorim Jaggif3df0aa2016-04-06 15:56:33 -0700591
592 // If we are not drag resizing, force recreating of a new surface so updating
593 // the content and positioning that surface will be in sync.
Robert Carr2c17cd22016-04-08 17:52:28 -0700594 //
595 // As we use this flag as a hint to freeze surface boundary updates,
596 // we'd like to only apply this to TYPE_BASE_APPLICATION,
597 // windows of TYPE_APPLICATION like dialogs, could appear
598 // to not be drag resizing while they resize, but we'd
599 // still like to manipulate their frame to update crop, etc...
600 //
601 // Anyway we don't need to synchronize position and content updates for these
602 // windows since they aren't at the base layer and could be moved around anyway.
Robert Carr1ca6a332016-04-11 18:00:43 -0700603 if (!win.computeDragResizing() && win.mAttrs.type == TYPE_BASE_APPLICATION &&
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700604 !mStack.getBoundsAnimating() && !win.isGoneForLayoutLw()) {
Jorim Jaggif3df0aa2016-04-06 15:56:33 -0700605 win.mResizedWhileNotDragResizing = true;
606 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700607 }
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800608 if (win.isGoneForLayoutLw()) {
609 win.mResizedWhileGone = true;
610 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700611 }
612 }
613 }
614
Chong Zhangbd0d9372015-12-28 15:18:29 -0800615 void moveWindows() {
616 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
617 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
618 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
619 final WindowState win = windows.get(winNdx);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800620 if (DEBUG_RESIZE) Slog.d(TAG, "moveWindows: Moving " + win);
Chong Zhangbd0d9372015-12-28 15:18:29 -0800621 win.mMovedByResize = true;
622 }
623 }
624 }
625
Winsonc28098f2015-10-30 14:50:19 -0700626 /**
627 * Cancels any running app transitions associated with the task.
628 */
629 void cancelTaskWindowTransition() {
630 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
631 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
632 }
633 }
634
Winson13d30662015-11-06 15:30:29 -0800635 /**
636 * Cancels any running thumbnail transitions associated with the task.
637 */
638 void cancelTaskThumbnailTransition() {
639 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
640 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
641 }
642 }
643
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700644 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700645 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700646 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700647 }
648
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800649 boolean isVisibleForUser() {
650 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
651 final AppWindowToken appToken = mAppTokens.get(i);
652 for (int j = appToken.allAppWindows.size() - 1; j >= 0; j--) {
653 WindowState window = appToken.allAppWindows.get(j);
654 if (!window.isHiddenFromUserLocked()) {
655 return true;
656 }
657 }
658 }
659 return false;
660 }
661
Jorim Jaggiff71d202016-04-14 13:12:36 -0700662 boolean isVisible() {
663 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
664 final AppWindowToken appToken = mAppTokens.get(i);
665 if (appToken.isVisible()) {
666 return true;
667 }
668 }
669 return false;
670 }
671
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700672 boolean inHomeStack() {
673 return mStack != null && mStack.mStackId == HOME_STACK_ID;
674 }
675
Chong Zhang09b21ef2015-09-14 10:20:21 -0700676 boolean inFreeformWorkspace() {
677 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
678 }
679
Filip Gruszczynski955b2fc2015-10-15 14:46:07 -0700680 boolean inDockedWorkspace() {
681 return mStack != null && mStack.mStackId == DOCKED_STACK_ID;
682 }
683
Chong Zhangb15758a2015-11-17 12:12:03 -0800684 boolean isResizeableByDockedStack() {
Wale Ogunwale5e7409c2016-01-11 12:24:19 -0800685 final DisplayContent displayContent = getDisplayContent();
686 return displayContent != null && displayContent.getDockedStackLocked() != null
687 && mStack != null && StackId.isTaskResizeableByDockedStack(mStack.mStackId);
Chong Zhangb15758a2015-11-17 12:12:03 -0800688 }
689
Robert Carre6275582016-02-29 15:45:45 -0800690 boolean isFloating() {
691 return StackId.tasksAreFloating(mStack.mStackId);
692 }
693
Chong Zhangb15758a2015-11-17 12:12:03 -0800694 /**
695 * Whether the task should be treated as if it's docked. Returns true if the task
696 * is currently in docked workspace, or it's side-by-side to a docked task.
697 */
698 boolean isDockedInEffect() {
699 return inDockedWorkspace() || isResizeableByDockedStack();
700 }
701
Chong Zhang2a88fc32016-01-11 17:14:24 -0800702 boolean isTwoFingerScrollMode() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800703 return inCropWindowsResizeMode() && isDockedInEffect();
Chong Zhang2a88fc32016-01-11 17:14:24 -0800704 }
705
Chong Zhangd8ceb852015-11-11 14:53:41 -0800706 WindowState getTopVisibleAppMainWindow() {
707 final AppWindowToken token = getTopVisibleAppToken();
708 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700709 }
710
Chong Zhangd8ceb852015-11-11 14:53:41 -0800711 AppWindowToken getTopVisibleAppToken() {
712 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
713 final AppWindowToken token = mAppTokens.get(i);
714 // skip hidden (or about to hide) apps
715 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
716 return token;
717 }
718 }
719 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700720 }
721
Jorim Jaggiaf558e12016-04-27 22:56:56 -0700722 AppWindowToken getTopAppToken() {
723 return mAppTokens.size() > 0 ? mAppTokens.get(mAppTokens.size() - 1) : null;
724 }
725
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800726 @Override
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700727 public boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700728 if (useCurrentBounds()) {
729 return mFullscreen;
730 }
731 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
732 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
733 // system.
734 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700735 }
736
737 @Override
738 public DisplayInfo getDisplayInfo() {
739 return mStack.getDisplayContent().getDisplayInfo();
740 }
741
742 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800743 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800744 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800745 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700746
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700747 @Override
748 public String toShortString() {
749 return "Task=" + mTaskId;
750 }
751
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800752 public void dump(String prefix, PrintWriter pw) {
753 final String doublePrefix = prefix + " ";
754
755 pw.println(prefix + "taskId=" + mTaskId);
756 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
757 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
758 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
759 pw.println(doublePrefix + "appTokens=" + mAppTokens);
760 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
761
762 final String triplePrefix = doublePrefix + " ";
763
764 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
765 final AppWindowToken wtoken = mAppTokens.get(i);
766 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
767 wtoken.dump(pw, triplePrefix);
768 }
769
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700770 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800771}