blob: 423b1341c94d5c38b8872f63595485845af77d92 [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;
Robert Carr03138452016-05-18 14:53:16 -070021import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070022import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
23import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080024import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS;
Jorim Jaggid53f0922016-04-06 22:16:23 -070025import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080026import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_RESIZE;
27import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Jorim Jaggid53f0922016-04-06 22:16:23 -070028import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
29import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070030import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Robert Carr2c17cd22016-04-08 17:52:28 -070031import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Wale Ogunwale99db1862015-10-23 20:08:22 -070032
Wale Ogunwale3797c222015-10-27 14:21:58 -070033import android.app.ActivityManager.StackId;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080034import android.content.pm.ActivityInfo;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070035import android.content.res.Configuration;
36import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080037import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080038import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070039import android.view.DisplayInfo;
40import android.view.Surface;
41
Craig Mautnere3119b72015-01-20 15:02:36 -080042import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080043
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070044import java.io.PrintWriter;
45import java.util.ArrayList;
46
47class Task implements DimLayer.DimLayerUser {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080048 static final String TAG = TAG_WITH_CLASS_NAME ? "Task" : TAG_WM;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070049 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
50 static final int BOUNDS_CHANGE_NONE = 0;
51 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
52 static final int BOUNDS_CHANGE_POSITION = 1;
53 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
54 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
55
Craig Mautnerc00204b2013-03-05 15:02:14 -080056 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080057 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080058 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070059 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080060 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080061 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080062
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070063 // Content limits relative to the DisplayContent this sits in.
64 private Rect mBounds = new Rect();
Jorim Jaggi0429f352015-12-22 16:29:16 +010065 final Rect mPreparedFrozenBounds = new Rect();
Jorim Jaggi26c8c422016-05-09 19:57:25 -070066 final Configuration mPreparedFrozenMergedConfig = new Configuration();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070067
Jorim Jaggidc249c42015-12-15 14:57:31 -080068 // Bounds used to calculate the insets.
69 private final Rect mTempInsetBounds = new Rect();
70
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070071 // Device rotation as of the last time {@link #mBounds} was set.
72 int mRotation;
73
74 // Whether mBounds is fullscreen
75 private boolean mFullscreen = true;
76
77 // Contains configurations settings that are different from the global configuration due to
78 // stack specific operations. E.g. {@link #setBounds}.
Jorim Jaggi26c8c422016-05-09 19:57:25 -070079 Configuration mOverrideConfig = Configuration.EMPTY;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070080
81 // For comparison with DisplayContent bounds.
82 private Rect mTmpRect = new Rect();
83 // For handling display rotations.
84 private Rect mTmpRect2 = new Rect();
85
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080086 // Resize mode of the task. See {@link ActivityInfo#resizeMode}
87 private int mResizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -080088
Chong Zhang3005e752015-09-18 18:46:28 -070089 // Whether the task is currently being drag-resized
90 private boolean mDragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010091 private int mDragResizeMode;
Chong Zhang3005e752015-09-18 18:46:28 -070092
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080093 private boolean mHomeTask;
94
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070095 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
96 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -080097 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -080098 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -070099 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -0800100 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700101 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800102 }
103
104 DisplayContent getDisplayContent() {
105 return mStack.getDisplayContent();
106 }
107
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800108 void addAppToken(int addPos, AppWindowToken wtoken, int resizeMode, boolean homeTask) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800109 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800110 if (addPos >= lastPos) {
111 addPos = lastPos;
112 } else {
113 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
114 if (mAppTokens.get(pos).removed) {
115 // addPos assumes removed tokens are actually gone.
116 ++addPos;
117 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700118 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800119 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800120 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800121 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800122 mDeferRemoval = false;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800123 mResizeMode = resizeMode;
124 mHomeTask = homeTask;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800125 }
126
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700127 private boolean hasWindowsAlive() {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800128 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700129 if (mAppTokens.get(i).hasWindowsAlive()) {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800130 return true;
131 }
132 }
133 return false;
134 }
135
Craig Mautnere3119b72015-01-20 15:02:36 -0800136 void removeLocked() {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700137 if (hasWindowsAlive() && mStack.isAnimating()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800138 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800139 mDeferRemoval = true;
140 return;
141 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800142 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800143 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800144 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700145 DisplayContent content = getDisplayContent();
146 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800147 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700148 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800149 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800150 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800151 }
152
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800153 void moveTaskToStack(TaskStack stack, boolean toTop) {
154 if (stack == mStack) {
155 return;
156 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800157 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800158 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700159 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800160 if (mStack != null) {
161 mStack.removeTask(this);
162 }
163 stack.addTask(this, toTop);
164 }
165
Wale Ogunwale935e5022015-11-10 12:36:10 -0800166 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700167 if (mStack != null && stack != mStack) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800168 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700169 + " from stack=" + mStack);
170 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
171 mStack.removeTask(this);
172 }
173 stack.positionTask(this, position, showForAllUsers());
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800174 resizeLocked(bounds, config, false /* force */);
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800175
176 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
177 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
178 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
179 final WindowState win = windows.get(winNdx);
180 win.notifyMovedInStack();
181 }
182 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700183 }
184
Craig Mautnerc00204b2013-03-05 15:02:14 -0800185 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800186 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800187 if (mAppTokens.size() == 0) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100188 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800189 if (mDeferRemoval) {
190 removeLocked();
191 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800192 }
Craig Mautner83162a92015-01-26 14:43:30 -0800193 wtoken.mTask = null;
194 /* Leave mTaskId for now, it might be useful for debug
195 wtoken.mTaskId = -1;
196 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800197 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800198 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800199
Craig Mautnercbd84af2014-10-22 13:21:22 -0700200 void setSendingToBottom(boolean toBottom) {
201 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
202 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
203 }
204 }
205
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700206 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800207 private int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700208 if (config == null) {
209 config = Configuration.EMPTY;
210 }
211 if (bounds == null && !Configuration.EMPTY.equals(config)) {
212 throw new IllegalArgumentException("null bounds but non empty configuration: "
213 + config);
214 }
215 if (bounds != null && Configuration.EMPTY.equals(config)) {
216 throw new IllegalArgumentException("non null bounds, but empty configuration");
217 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700218 boolean oldFullscreen = mFullscreen;
219 int rotation = Surface.ROTATION_0;
220 final DisplayContent displayContent = mStack.getDisplayContent();
221 if (displayContent != null) {
222 displayContent.getLogicalDisplayRect(mTmpRect);
223 rotation = displayContent.getDisplayInfo().rotation;
Jorim Jaggi067e8172016-02-03 18:24:12 -0800224 mFullscreen = bounds == null;
225 if (mFullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700226 bounds = mTmpRect;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700227 }
228 }
229
230 if (bounds == null) {
231 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700232 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700233 }
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700234 if (mBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700235 return BOUNDS_CHANGE_NONE;
236 }
237
238 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700239 if (mBounds.left != bounds.left || mBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700240 boundsChange |= BOUNDS_CHANGE_POSITION;
241 }
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700242 if (mBounds.width() != bounds.width() || mBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700243 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700244 }
245
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700246 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800247
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700248 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700249 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800250 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700251 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700252 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700253 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700254 }
255
Jorim Jaggidc249c42015-12-15 14:57:31 -0800256 /**
257 * Sets the bounds used to calculate the insets. See
258 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
259 */
260 void setTempInsetBounds(Rect tempInsetBounds) {
261 if (tempInsetBounds != null) {
262 mTempInsetBounds.set(tempInsetBounds);
263 } else {
264 mTempInsetBounds.setEmpty();
265 }
266 }
267
268 /**
269 * Gets the bounds used to calculate the insets. See
270 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
271 */
272 void getTempInsetBounds(Rect out) {
273 out.set(mTempInsetBounds);
274 }
275
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800276 void setResizeable(int resizeMode) {
277 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800278 }
279
280 boolean isResizeable() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800281 return !mHomeTask
282 && (ActivityInfo.isResizeableMode(mResizeMode) || mService.mForceResizableTasks);
283 }
284
285 boolean cropWindowsToStackBounds() {
286 return !mHomeTask && (isResizeable() || mResizeMode == RESIZE_MODE_CROP_WINDOWS);
287 }
288
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800289 boolean isHomeTask() {
290 return mHomeTask;
291 }
292
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800293 private boolean inCropWindowsResizeMode() {
294 return !mHomeTask && !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
Chong Zhangb15758a2015-11-17 12:12:03 -0800295 }
296
Chong Zhang87b21722015-09-21 15:39:51 -0700297 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700298 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700299 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700300 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700301 }
302 if (boundsChanged == BOUNDS_CHANGE_NONE) {
303 return false;
304 }
305 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
306 resizeWindows();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800307 } else {
308 moveWindows();
Chong Zhang3005e752015-09-18 18:46:28 -0700309 }
310 return true;
311 }
312
Jorim Jaggi0429f352015-12-22 16:29:16 +0100313 /**
314 * Prepares the task bounds to be frozen with the current size. See
315 * {@link AppWindowToken#freezeBounds}.
316 */
317 void prepareFreezingBounds() {
318 mPreparedFrozenBounds.set(mBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700319 mPreparedFrozenMergedConfig.setTo(mService.mCurConfiguration);
320 mPreparedFrozenMergedConfig.updateFrom(mOverrideConfig);
Jorim Jaggi0429f352015-12-22 16:29:16 +0100321 }
322
Chong Zhang5117e272016-05-03 12:47:34 -0700323 /**
324 * Align the task to the adjusted bounds.
325 *
326 * @param adjustedBounds Adjusted bounds to which the task should be aligned.
327 * @param tempInsetBounds Insets bounds for the task.
328 * @param alignBottom True if the task's bottom should be aligned to the adjusted
329 * bounds's bottom; false if the task's top should be aligned
330 * the adjusted bounds's top.
331 */
332 void alignToAdjustedBounds(
333 Rect adjustedBounds, Rect tempInsetBounds, boolean alignBottom) {
334 if (!isResizeable() || mOverrideConfig == Configuration.EMPTY) {
335 return;
336 }
337
338 getBounds(mTmpRect2);
339 if (alignBottom) {
340 int offsetY = adjustedBounds.bottom - mTmpRect2.bottom;
341 mTmpRect2.offset(0, offsetY);
342 } else {
343 mTmpRect2.offsetTo(adjustedBounds.left, adjustedBounds.top);
344 }
345 setTempInsetBounds(tempInsetBounds);
346 resizeLocked(mTmpRect2, mOverrideConfig, false /* forced */);
347 }
348
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700349 /** Return true if the current bound can get outputted to the rest of the system as-is. */
350 private boolean useCurrentBounds() {
351 final DisplayContent displayContent = mStack.getDisplayContent();
352 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700353 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700354 || displayContent == null
Jorim Jaggif4fa8cb2016-04-01 16:05:10 -0700355 || displayContent.getDockedStackVisibleForUserLocked() != null) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700356 return true;
357 }
358 return false;
359 }
360
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800361 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800362 void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700363 if (useCurrentBounds()) {
364 // No need to adjust the output bounds if fullscreen or the docked stack is visible
365 // since it is already what we want to represent to the rest of the system.
366 out.set(mBounds);
367 return;
368 }
369
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800370 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
371 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700372 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700373 }
374
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800375 /**
376 * Calculate the maximum visible area of this task. If the task has only one app,
377 * the result will be visible frame of that app. If the task has more than one apps,
378 * we search from top down if the next app got different visible area.
379 *
380 * This effort is to handle the case where some task (eg. GMail composer) might pop up
381 * a dialog that's different in size from the activity below, in which case we should
382 * be dimming the entire task area behind the dialog.
383 *
384 * @param out Rect containing the max visible bounds.
385 * @return true if the task has some visible app windows; false otherwise.
386 */
387 boolean getMaxVisibleBounds(Rect out) {
388 boolean foundTop = false;
389 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800390 final AppWindowToken token = mAppTokens.get(i);
391 // skip hidden (or about to hide) apps
392 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
393 continue;
394 }
395 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800396 if (win == null) {
397 continue;
398 }
399 if (!foundTop) {
400 out.set(win.mVisibleFrame);
401 foundTop = true;
402 continue;
403 }
404 if (win.mVisibleFrame.left < out.left) {
405 out.left = win.mVisibleFrame.left;
406 }
407 if (win.mVisibleFrame.top < out.top) {
408 out.top = win.mVisibleFrame.top;
409 }
410 if (win.mVisibleFrame.right > out.right) {
411 out.right = win.mVisibleFrame.right;
412 }
413 if (win.mVisibleFrame.bottom > out.bottom) {
414 out.bottom = win.mVisibleFrame.bottom;
415 }
416 }
417 return foundTop;
418 }
419
420 /** Bounds of the task to be used for dimming, as well as touch related tests. */
421 @Override
422 public void getDimBounds(Rect out) {
Robert Carra86a6bf2016-04-08 17:34:16 -0700423 final DisplayContent displayContent = mStack.getDisplayContent();
424 // It doesn't matter if we in particular are part of the resize, since we couldn't have
425 // a DimLayer anyway if we weren't visible.
426 final boolean dockedResizing = displayContent != null ?
427 displayContent.mDividerControllerLocked.isResizing() : false;
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800428 if (useCurrentBounds()) {
429 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
430 return;
431 }
432
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700433 if (!mFullscreen) {
434 // When minimizing the docked stack when going home, we don't adjust the task bounds
435 // so we need to intersect the task bounds with the stack bounds here.
Robert Carra86a6bf2016-04-08 17:34:16 -0700436 //
437 // If we are Docked Resizing with snap points, the task bounds could be smaller than the stack
438 // bounds and so we don't even want to use them. Even if the app should not be resized the Dim
439 // should keep up with the divider.
440 if (dockedResizing) {
441 mStack.getBounds(out);
442 } else {
443 mStack.getBounds(mTmpRect);
444 mTmpRect.intersect(mBounds);
445 }
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700446 out.set(mTmpRect);
447 } else {
448 out.set(mBounds);
449 }
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800450 return;
451 }
452
453 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
454 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
455 // system.
Robert Carra86a6bf2016-04-08 17:34:16 -0700456 displayContent.getLogicalDisplayRect(out);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800457 }
458
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100459 void setDragResizing(boolean dragResizing, int dragResizeMode) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800460 if (mDragResizing != dragResizing) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100461 if (!DragResizeMode.isModeAllowedForStack(mStack.mStackId, dragResizeMode)) {
462 throw new IllegalArgumentException("Drag resize mode not allow for stack stackId="
463 + mStack.mStackId + " dragResizeMode=" + dragResizeMode);
464 }
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800465 mDragResizing = dragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100466 mDragResizeMode = dragResizeMode;
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800467 resetDragResizingChangeReported();
468 }
469 }
470
471 void resetDragResizingChangeReported() {
472 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
473 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
474 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
475 final WindowState win = windows.get(winNdx);
476 win.resetDragResizingChangeReported();
477 }
478 }
Chong Zhang3005e752015-09-18 18:46:28 -0700479 }
480
481 boolean isDragResizing() {
Wale Ogunwalece144522016-02-05 22:51:01 -0800482 return mDragResizing || (mStack != null && mStack.isDragResizing());
Chong Zhang3005e752015-09-18 18:46:28 -0700483 }
484
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100485 int getDragResizeMode() {
486 return mDragResizeMode;
487 }
488
Jorim Jaggiff71d202016-04-14 13:12:36 -0700489 /**
490 * Adds all of the tasks windows to {@link WindowManagerService#mWaitingForDrawn} if drag
491 * resizing state of the window has been changed.
492 */
493 void addWindowsWaitingForDrawnIfResizingChanged() {
494 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
495 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
496 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
497 final WindowState win = windows.get(winNdx);
498 if (win.isDragResizeChanged()) {
499 mService.mWaitingForDrawn.add(win);
500 }
501 }
502 }
503 }
504
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700505 void updateDisplayInfo(final DisplayContent displayContent) {
506 if (displayContent == null) {
507 return;
508 }
509 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700510 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700511 return;
512 }
513 final int newRotation = displayContent.getDisplayInfo().rotation;
514 if (mRotation == newRotation) {
515 return;
516 }
517
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800518 // Device rotation changed.
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700519 // - We don't want the task to move around on the screen when this happens, so update the
520 // task bounds so it stays in the same place.
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800521 // - Rotate the bounds and notify activity manager if the task can be resized independently
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700522 // from its stack. The stack will take care of task rotation for the other case.
523 mTmpRect2.set(mBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800524
525 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
526 setBounds(mTmpRect2, mOverrideConfig);
527 return;
528 }
529
Wale Ogunwale94744212015-09-21 19:01:47 -0700530 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700531 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800532 // Post message to inform activity manager of the bounds change simulating a one-way
533 // call. We do this to prevent a deadlock between window manager lock and activity
534 // manager lock been held.
535 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700536 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700537 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700538 }
539
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700540 void resizeWindows() {
541 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
542 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800543 final AppWindowToken atoken = mAppTokens.get(activityNdx);
544
545 // Some windows won't go through the resizing process, if they don't have a surface, so
546 // destroy all saved surfaces here.
547 atoken.destroySavedSurfaces();
548 final ArrayList<WindowState> windows = atoken.allAppWindows;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700549 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
550 final WindowState win = windows.get(winNdx);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800551 if (win.mHasSurface && !resizingWindows.contains(win)) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800552 if (DEBUG_RESIZE) Slog.d(TAG, "resizeWindows: Resizing " + win);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700553 resizingWindows.add(win);
Jorim Jaggif3df0aa2016-04-06 15:56:33 -0700554
555 // If we are not drag resizing, force recreating of a new surface so updating
556 // the content and positioning that surface will be in sync.
Robert Carr2c17cd22016-04-08 17:52:28 -0700557 //
558 // As we use this flag as a hint to freeze surface boundary updates,
559 // we'd like to only apply this to TYPE_BASE_APPLICATION,
560 // windows of TYPE_APPLICATION like dialogs, could appear
561 // to not be drag resizing while they resize, but we'd
562 // still like to manipulate their frame to update crop, etc...
563 //
564 // Anyway we don't need to synchronize position and content updates for these
565 // windows since they aren't at the base layer and could be moved around anyway.
Robert Carr1ca6a332016-04-11 18:00:43 -0700566 if (!win.computeDragResizing() && win.mAttrs.type == TYPE_BASE_APPLICATION &&
Robert Carr03138452016-05-18 14:53:16 -0700567 !mStack.getBoundsAnimating() && !win.isGoneForLayoutLw() &&
568 !inPinnedWorkspace()) {
Andrii Kulianeb1d3222016-05-16 15:17:55 -0700569 win.setResizedWhileNotDragResizing(true);
Jorim Jaggif3df0aa2016-04-06 15:56:33 -0700570 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700571 }
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800572 if (win.isGoneForLayoutLw()) {
573 win.mResizedWhileGone = true;
574 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700575 }
576 }
577 }
578
Chong Zhangbd0d9372015-12-28 15:18:29 -0800579 void moveWindows() {
580 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
581 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
582 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
583 final WindowState win = windows.get(winNdx);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800584 if (DEBUG_RESIZE) Slog.d(TAG, "moveWindows: Moving " + win);
Chong Zhangbd0d9372015-12-28 15:18:29 -0800585 win.mMovedByResize = true;
586 }
587 }
588 }
589
Winsonc28098f2015-10-30 14:50:19 -0700590 /**
591 * Cancels any running app transitions associated with the task.
592 */
593 void cancelTaskWindowTransition() {
594 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
595 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
596 }
597 }
598
Winson13d30662015-11-06 15:30:29 -0800599 /**
600 * Cancels any running thumbnail transitions associated with the task.
601 */
602 void cancelTaskThumbnailTransition() {
603 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
604 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
605 }
606 }
607
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700608 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700609 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700610 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700611 }
612
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800613 boolean isVisibleForUser() {
614 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
615 final AppWindowToken appToken = mAppTokens.get(i);
616 for (int j = appToken.allAppWindows.size() - 1; j >= 0; j--) {
617 WindowState window = appToken.allAppWindows.get(j);
618 if (!window.isHiddenFromUserLocked()) {
619 return true;
620 }
621 }
622 }
623 return false;
624 }
625
Jorim Jaggiff71d202016-04-14 13:12:36 -0700626 boolean isVisible() {
627 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
628 final AppWindowToken appToken = mAppTokens.get(i);
629 if (appToken.isVisible()) {
630 return true;
631 }
632 }
633 return false;
634 }
635
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700636 boolean inHomeStack() {
637 return mStack != null && mStack.mStackId == HOME_STACK_ID;
638 }
639
Chong Zhang09b21ef2015-09-14 10:20:21 -0700640 boolean inFreeformWorkspace() {
641 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
642 }
643
Robert Carr03138452016-05-18 14:53:16 -0700644 boolean inPinnedWorkspace() {
645 return mStack != null && mStack.mStackId == PINNED_STACK_ID;
646 }
647
Robert Carre6275582016-02-29 15:45:45 -0800648 boolean isFloating() {
649 return StackId.tasksAreFloating(mStack.mStackId);
650 }
651
Chong Zhangd8ceb852015-11-11 14:53:41 -0800652 WindowState getTopVisibleAppMainWindow() {
653 final AppWindowToken token = getTopVisibleAppToken();
654 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700655 }
656
Chong Zhangd8ceb852015-11-11 14:53:41 -0800657 AppWindowToken getTopVisibleAppToken() {
658 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
659 final AppWindowToken token = mAppTokens.get(i);
660 // skip hidden (or about to hide) apps
661 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
662 return token;
663 }
664 }
665 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700666 }
667
Jorim Jaggiaf558e12016-04-27 22:56:56 -0700668 AppWindowToken getTopAppToken() {
669 return mAppTokens.size() > 0 ? mAppTokens.get(mAppTokens.size() - 1) : null;
670 }
671
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800672 @Override
Wale Ogunwale29bfbb82016-05-12 15:13:52 -0700673 public boolean dimFullscreen() {
674 return isHomeTask() || isFullscreen();
675 }
676
677 boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700678 if (useCurrentBounds()) {
679 return mFullscreen;
680 }
681 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
682 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
683 // system.
684 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700685 }
686
687 @Override
688 public DisplayInfo getDisplayInfo() {
689 return mStack.getDisplayContent().getDisplayInfo();
690 }
691
692 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800693 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800694 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800695 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700696
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700697 @Override
698 public String toShortString() {
699 return "Task=" + mTaskId;
700 }
701
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800702 public void dump(String prefix, PrintWriter pw) {
703 final String doublePrefix = prefix + " ";
704
705 pw.println(prefix + "taskId=" + mTaskId);
706 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
707 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
708 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
709 pw.println(doublePrefix + "appTokens=" + mAppTokens);
710 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
711
712 final String triplePrefix = doublePrefix + " ";
713
714 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
715 final AppWindowToken wtoken = mAppTokens.get(i);
716 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
717 wtoken.dump(pw, triplePrefix);
718 }
719
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700720 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800721}