blob: 4f59c62b5c409387afce0e3b9c75dba348ec8640 [file] [log] [blame]
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Chong Zhangb15758a2015-11-17 12:12:03 -080022import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070023import static android.app.ActivityManager.RESIZE_MODE_SYSTEM_SCREEN_ROTATION;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080024import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080025import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
26import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_RESIZE;
27import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070028import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Chong Zhangc806d902015-11-30 09:44:27 -080029import static com.android.server.wm.WindowManagerService.H.SHOW_NON_RESIZEABLE_DOCK_TOAST;
30import static android.view.WindowManager.DOCKED_INVALID;
31import static android.view.WindowManager.DOCKED_LEFT;
32import static android.view.WindowManager.DOCKED_RIGHT;
33import static android.view.WindowManager.DOCKED_TOP;
Wale Ogunwale99db1862015-10-23 20:08:22 -070034
Wale Ogunwale3797c222015-10-27 14:21:58 -070035import android.app.ActivityManager.StackId;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080036import android.content.pm.ActivityInfo;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070037import android.content.res.Configuration;
38import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080039import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080040import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070041import android.view.DisplayInfo;
42import android.view.Surface;
43
Craig Mautnere3119b72015-01-20 15:02:36 -080044import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080045
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070046import java.io.PrintWriter;
47import java.util.ArrayList;
48
49class Task implements DimLayer.DimLayerUser {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070050 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
51 static final int BOUNDS_CHANGE_NONE = 0;
52 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
53 static final int BOUNDS_CHANGE_POSITION = 1;
54 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
55 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
56
Craig Mautnerc00204b2013-03-05 15:02:14 -080057 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080058 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080059 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070060 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080061 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080062 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080063
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070064 // Content limits relative to the DisplayContent this sits in.
65 private Rect mBounds = new Rect();
Jorim Jaggi0429f352015-12-22 16:29:16 +010066 final Rect mPreparedFrozenBounds = new Rect();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070067
Chong Zhangf66db432016-01-13 10:39:51 -080068 private Rect mPreScrollBounds = new Rect();
69 private boolean mScrollValid;
70
Jorim Jaggidc249c42015-12-15 14:57:31 -080071 // Bounds used to calculate the insets.
72 private final Rect mTempInsetBounds = new Rect();
73
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070074 // Device rotation as of the last time {@link #mBounds} was set.
75 int mRotation;
76
77 // Whether mBounds is fullscreen
78 private boolean mFullscreen = true;
79
80 // Contains configurations settings that are different from the global configuration due to
81 // stack specific operations. E.g. {@link #setBounds}.
82 Configuration mOverrideConfig;
83
84 // For comparison with DisplayContent bounds.
85 private Rect mTmpRect = new Rect();
86 // For handling display rotations.
87 private Rect mTmpRect2 = new Rect();
88
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080089 // Resize mode of the task. See {@link ActivityInfo#resizeMode}
90 private int mResizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -080091
Chong Zhangc806d902015-11-30 09:44:27 -080092 // Whether we need to show toast about the app being non-resizeable when it becomes visible.
93 // This flag is set when a non-resizeable task is docked (or side-by-side). It's cleared
94 // after we show the toast.
95 private boolean mShowNonResizeableDockToast;
96
Chong Zhang3005e752015-09-18 18:46:28 -070097 // Whether the task is currently being drag-resized
98 private boolean mDragResizing;
99
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800100 private boolean mHomeTask;
101
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700102 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
103 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -0800104 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800105 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -0700106 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -0800107 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700108 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800109 }
110
111 DisplayContent getDisplayContent() {
112 return mStack.getDisplayContent();
113 }
114
Chong Zhangc806d902015-11-30 09:44:27 -0800115 void setShowNonResizeableDockToast() {
116 mShowNonResizeableDockToast = true;
117 }
118
119 void scheduleShowNonResizeableDockToastIfNeeded() {
120 if (!mShowNonResizeableDockToast) {
121 return;
122 }
123 final DisplayContent displayContent = mStack.getDisplayContent();
124 // If docked stack is not yet visible, we don't want to show the toast yet,
125 // since we need the visible rect of the docked task to position the toast.
126 if (displayContent == null || displayContent.getDockedStackLocked() == null) {
127 return;
128 }
129
130 mShowNonResizeableDockToast = false;
131
132 final int dockSide = mStack.getDockSide();
133 int xOffset = 0;
134 int yOffset = 0;
135 if (dockSide != DOCKED_INVALID) {
136 mStack.getBounds(mTmpRect);
Chong Zhangc806d902015-11-30 09:44:27 -0800137
138 if (dockSide == DOCKED_LEFT || dockSide == DOCKED_RIGHT) {
Chong Zhangf66db432016-01-13 10:39:51 -0800139 // The toast was originally placed at the bottom and centered. To place it
140 // at the bottom-center of the stack, we offset it horizontally by the diff
141 // between the center of the stack bounds vs. the center of the screen.
142 displayContent.getLogicalDisplayRect(mTmpRect2);
Chong Zhangc806d902015-11-30 09:44:27 -0800143 xOffset = mTmpRect.centerX() - mTmpRect2.centerX();
144 } else if (dockSide == DOCKED_TOP) {
Chong Zhangf66db432016-01-13 10:39:51 -0800145 // The toast was originally placed at the bottom and centered. To place it
146 // at the bottom center of the top stack, we offset it vertically by the diff
147 // between the bottom of the stack bounds vs. the bottom of the content rect.
148 //
149 // Note here we use the content rect instead of the display rect, as we want
150 // the toast's distance to the dock divider (when it's placed at the top half)
151 // to be the same as it's distance to the top of the navigation bar (when it's
152 // placed at the bottom).
153
Chong Zhangc806d902015-11-30 09:44:27 -0800154 // We don't adjust for DOCKED_BOTTOM case since it's already at the bottom.
Chong Zhangf66db432016-01-13 10:39:51 -0800155 displayContent.getContentRect(mTmpRect2);
Chong Zhangc806d902015-11-30 09:44:27 -0800156 yOffset = mTmpRect2.bottom - mTmpRect.bottom;
157 }
158 mService.mH.obtainMessage(
159 SHOW_NON_RESIZEABLE_DOCK_TOAST, xOffset, yOffset).sendToTarget();
160 }
161 }
162
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800163 void addAppToken(int addPos, AppWindowToken wtoken, int resizeMode, boolean homeTask) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800164 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800165 if (addPos >= lastPos) {
166 addPos = lastPos;
167 } else {
168 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
169 if (mAppTokens.get(pos).removed) {
170 // addPos assumes removed tokens are actually gone.
171 ++addPos;
172 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700173 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800174 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800175 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800176 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800177 mDeferRemoval = false;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800178 mResizeMode = resizeMode;
179 mHomeTask = homeTask;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800180 }
181
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800182 private boolean hasAppTokensAlive() {
183 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
184 if (!mAppTokens.get(i).appDied) {
185 return true;
186 }
187 }
188 return false;
189 }
190
Craig Mautnere3119b72015-01-20 15:02:36 -0800191 void removeLocked() {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800192 if (hasAppTokensAlive() && mStack.isAnimating()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800193 if (DEBUG_STACK) Slog.i(TAG_WM, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800194 mDeferRemoval = true;
195 return;
196 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800197 if (DEBUG_STACK) Slog.i(TAG_WM, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800198 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800199 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700200 DisplayContent content = getDisplayContent();
201 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800202 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700203 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800204 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800205 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800206 }
207
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800208 void moveTaskToStack(TaskStack stack, boolean toTop) {
209 if (stack == mStack) {
210 return;
211 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800212 if (DEBUG_STACK) Slog.i(TAG_WM, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800213 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700214 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800215 if (mStack != null) {
216 mStack.removeTask(this);
217 }
218 stack.addTask(this, toTop);
219 }
220
Wale Ogunwale935e5022015-11-10 12:36:10 -0800221 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700222 if (mStack != null && stack != mStack) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800223 if (DEBUG_STACK) Slog.i(TAG_WM, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700224 + " from stack=" + mStack);
225 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
226 mStack.removeTask(this);
227 }
228 stack.positionTask(this, position, showForAllUsers());
Wale Ogunwale935e5022015-11-10 12:36:10 -0800229 setBounds(bounds, config);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700230 }
231
Craig Mautnerc00204b2013-03-05 15:02:14 -0800232 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800233 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800234 if (mAppTokens.size() == 0) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100235 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800236 if (mDeferRemoval) {
237 removeLocked();
238 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800239 }
Craig Mautner83162a92015-01-26 14:43:30 -0800240 wtoken.mTask = null;
241 /* Leave mTaskId for now, it might be useful for debug
242 wtoken.mTaskId = -1;
243 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800244 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800245 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800246
Craig Mautnercbd84af2014-10-22 13:21:22 -0700247 void setSendingToBottom(boolean toBottom) {
248 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
249 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
250 }
251 }
252
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700253 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700254 int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700255 if (config == null) {
256 config = Configuration.EMPTY;
257 }
258 if (bounds == null && !Configuration.EMPTY.equals(config)) {
259 throw new IllegalArgumentException("null bounds but non empty configuration: "
260 + config);
261 }
262 if (bounds != null && Configuration.EMPTY.equals(config)) {
263 throw new IllegalArgumentException("non null bounds, but empty configuration");
264 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700265 boolean oldFullscreen = mFullscreen;
266 int rotation = Surface.ROTATION_0;
267 final DisplayContent displayContent = mStack.getDisplayContent();
268 if (displayContent != null) {
269 displayContent.getLogicalDisplayRect(mTmpRect);
270 rotation = displayContent.getDisplayInfo().rotation;
271 if (bounds == null) {
272 bounds = mTmpRect;
273 mFullscreen = true;
274 } else {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700275 mFullscreen = mTmpRect.equals(bounds);
276 }
277 }
278
279 if (bounds == null) {
280 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700281 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700282 }
Chong Zhangf66db432016-01-13 10:39:51 -0800283 if (mPreScrollBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700284 return BOUNDS_CHANGE_NONE;
285 }
286
287 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhangf66db432016-01-13 10:39:51 -0800288 if (mPreScrollBounds.left != bounds.left || mPreScrollBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700289 boundsChange |= BOUNDS_CHANGE_POSITION;
290 }
Chong Zhangf66db432016-01-13 10:39:51 -0800291 if (mPreScrollBounds.width() != bounds.width() || mPreScrollBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700292 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700293 }
294
Chong Zhangf66db432016-01-13 10:39:51 -0800295
296 mPreScrollBounds.set(bounds);
297
298 resetScrollLocked();
299
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700300 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700301 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800302 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700303 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700304 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700305 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700306 }
307
Jorim Jaggidc249c42015-12-15 14:57:31 -0800308 /**
309 * Sets the bounds used to calculate the insets. See
310 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
311 */
312 void setTempInsetBounds(Rect tempInsetBounds) {
313 if (tempInsetBounds != null) {
314 mTempInsetBounds.set(tempInsetBounds);
315 } else {
316 mTempInsetBounds.setEmpty();
317 }
318 }
319
320 /**
321 * Gets the bounds used to calculate the insets. See
322 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
323 */
324 void getTempInsetBounds(Rect out) {
325 out.set(mTempInsetBounds);
326 }
327
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800328 void setResizeable(int resizeMode) {
329 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800330 }
331
332 boolean isResizeable() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800333 return !mHomeTask
334 && (ActivityInfo.isResizeableMode(mResizeMode) || mService.mForceResizableTasks);
335 }
336
337 boolean cropWindowsToStackBounds() {
338 return !mHomeTask && (isResizeable() || mResizeMode == RESIZE_MODE_CROP_WINDOWS);
339 }
340
341 private boolean inCropWindowsResizeMode() {
342 return !mHomeTask && !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
Chong Zhangb15758a2015-11-17 12:12:03 -0800343 }
344
Chong Zhang87b21722015-09-21 15:39:51 -0700345 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700346 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700347 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700348 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700349 }
350 if (boundsChanged == BOUNDS_CHANGE_NONE) {
351 return false;
352 }
353 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
354 resizeWindows();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800355 } else {
356 moveWindows();
Chong Zhang3005e752015-09-18 18:46:28 -0700357 }
358 return true;
359 }
360
Jorim Jaggi0429f352015-12-22 16:29:16 +0100361 /**
362 * Prepares the task bounds to be frozen with the current size. See
363 * {@link AppWindowToken#freezeBounds}.
364 */
365 void prepareFreezingBounds() {
366 mPreparedFrozenBounds.set(mBounds);
367 }
368
Chong Zhangf66db432016-01-13 10:39:51 -0800369 void resetScrollLocked() {
370 if (mScrollValid) {
371 mScrollValid = false;
372 applyScrollToAllWindows(0, 0);
373 }
374 mBounds.set(mPreScrollBounds);
375 }
376
377 void applyScrollToAllWindows(final int xOffset, final int yOffset) {
378 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
379 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
380 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
381 final WindowState win = windows.get(winNdx);
382 win.mXOffset = xOffset;
383 win.mYOffset = yOffset;
384 }
385 }
386 }
387
388 void applyScrollToWindowIfNeeded(final WindowState win) {
389 if (mScrollValid) {
390 win.mXOffset = mBounds.left;
391 win.mYOffset = mBounds.top;
392 }
393 }
394
Chong Zhangb15758a2015-11-17 12:12:03 -0800395 boolean scrollLocked(Rect bounds) {
396 // shift the task bound if it doesn't fully cover the stack area
397 mStack.getDimBounds(mTmpRect);
398 if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) {
399 if (bounds.left > mTmpRect.left) {
400 bounds.left = mTmpRect.left;
401 bounds.right = mTmpRect.left + mBounds.width();
402 } else if (bounds.right < mTmpRect.right) {
403 bounds.left = mTmpRect.right - mBounds.width();
404 bounds.right = mTmpRect.right;
405 }
406 } else {
407 if (bounds.top > mTmpRect.top) {
408 bounds.top = mTmpRect.top;
409 bounds.bottom = mTmpRect.top + mBounds.height();
410 } else if (bounds.bottom < mTmpRect.bottom) {
411 bounds.top = mTmpRect.bottom - mBounds.height();
412 bounds.bottom = mTmpRect.bottom;
413 }
414 }
415
Chong Zhangf66db432016-01-13 10:39:51 -0800416 // We can stop here if we're already scrolling and the scrolled bounds not changed.
417 if (mScrollValid && bounds.equals(mBounds)) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800418 return false;
419 }
Chong Zhangf66db432016-01-13 10:39:51 -0800420
Chong Zhangb15758a2015-11-17 12:12:03 -0800421 // Normal setBounds() does not allow non-null bounds for fullscreen apps.
422 // We only change bounds for the scrolling case without change it size,
423 // on resizing path we should still want the validation.
424 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800425 mScrollValid = true;
426 applyScrollToAllWindows(bounds.left, bounds.top);
Chong Zhangb15758a2015-11-17 12:12:03 -0800427 return true;
428 }
429
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700430 /** Return true if the current bound can get outputted to the rest of the system as-is. */
431 private boolean useCurrentBounds() {
432 final DisplayContent displayContent = mStack.getDisplayContent();
433 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700434 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700435 || displayContent == null
436 || displayContent.getDockedStackLocked() != null) {
437 return true;
438 }
439 return false;
440 }
441
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800442 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800443 void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700444 if (useCurrentBounds()) {
445 // No need to adjust the output bounds if fullscreen or the docked stack is visible
446 // since it is already what we want to represent to the rest of the system.
447 out.set(mBounds);
448 return;
449 }
450
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800451 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
452 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700453 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700454 }
455
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800456 /**
457 * Calculate the maximum visible area of this task. If the task has only one app,
458 * the result will be visible frame of that app. If the task has more than one apps,
459 * we search from top down if the next app got different visible area.
460 *
461 * This effort is to handle the case where some task (eg. GMail composer) might pop up
462 * a dialog that's different in size from the activity below, in which case we should
463 * be dimming the entire task area behind the dialog.
464 *
465 * @param out Rect containing the max visible bounds.
466 * @return true if the task has some visible app windows; false otherwise.
467 */
468 boolean getMaxVisibleBounds(Rect out) {
469 boolean foundTop = false;
470 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800471 final AppWindowToken token = mAppTokens.get(i);
472 // skip hidden (or about to hide) apps
473 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
474 continue;
475 }
476 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800477 if (win == null) {
478 continue;
479 }
480 if (!foundTop) {
481 out.set(win.mVisibleFrame);
482 foundTop = true;
483 continue;
484 }
485 if (win.mVisibleFrame.left < out.left) {
486 out.left = win.mVisibleFrame.left;
487 }
488 if (win.mVisibleFrame.top < out.top) {
489 out.top = win.mVisibleFrame.top;
490 }
491 if (win.mVisibleFrame.right > out.right) {
492 out.right = win.mVisibleFrame.right;
493 }
494 if (win.mVisibleFrame.bottom > out.bottom) {
495 out.bottom = win.mVisibleFrame.bottom;
496 }
497 }
498 return foundTop;
499 }
500
501 /** Bounds of the task to be used for dimming, as well as touch related tests. */
502 @Override
503 public void getDimBounds(Rect out) {
504 if (useCurrentBounds()) {
505 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
506 return;
507 }
508
509 out.set(mBounds);
510 return;
511 }
512
513 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
514 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
515 // system.
516 mStack.getDisplayContent().getLogicalDisplayRect(out);
517 }
518
Chong Zhang3005e752015-09-18 18:46:28 -0700519 void setDragResizing(boolean dragResizing) {
Chong Zhang3005e752015-09-18 18:46:28 -0700520 mDragResizing = dragResizing;
521 }
522
523 boolean isDragResizing() {
524 return mDragResizing;
525 }
526
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700527 void updateDisplayInfo(final DisplayContent displayContent) {
528 if (displayContent == null) {
529 return;
530 }
531 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700532 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700533 return;
534 }
535 final int newRotation = displayContent.getDisplayInfo().rotation;
536 if (mRotation == newRotation) {
537 return;
538 }
539
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800540 // Device rotation changed.
541 // - Reset the bounds to the pre-scroll bounds as whatever scrolling was done is no longer
542 // valid.
543 // - Rotate the bounds and notify activity manager if the task can be resized independently
544 // from its stack. The stack will take care of task rotation for the other case.
Chong Zhangf66db432016-01-13 10:39:51 -0800545 mTmpRect2.set(mPreScrollBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800546
547 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
548 setBounds(mTmpRect2, mOverrideConfig);
549 return;
550 }
551
Wale Ogunwale94744212015-09-21 19:01:47 -0700552 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700553 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800554 // Post message to inform activity manager of the bounds change simulating a one-way
555 // call. We do this to prevent a deadlock between window manager lock and activity
556 // manager lock been held.
557 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
558 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mPreScrollBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700559 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700560 }
561
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700562 void resizeWindows() {
563 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
564 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
565 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
566 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
567 final WindowState win = windows.get(winNdx);
568 if (!resizingWindows.contains(win)) {
Chong Zhangbd0d9372015-12-28 15:18:29 -0800569 if (DEBUG_RESIZE) Slog.d(TAG_WM, "resizeWindows: Resizing " + win);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700570 resizingWindows.add(win);
571 }
572 }
573 }
574 }
575
Chong Zhangbd0d9372015-12-28 15:18:29 -0800576 void moveWindows() {
577 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
578 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
579 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
580 final WindowState win = windows.get(winNdx);
581 if (DEBUG_RESIZE) Slog.d(TAG_WM, "moveWindows: Moving " + win);
582 win.mMovedByResize = true;
583 }
584 }
585 }
586
Winsonc28098f2015-10-30 14:50:19 -0700587 /**
588 * Cancels any running app transitions associated with the task.
589 */
590 void cancelTaskWindowTransition() {
591 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
592 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
593 }
594 }
595
Winson13d30662015-11-06 15:30:29 -0800596 /**
597 * Cancels any running thumbnail transitions associated with the task.
598 */
599 void cancelTaskThumbnailTransition() {
600 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
601 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
602 }
603 }
604
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700605 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700606 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700607 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700608 }
609
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700610 boolean inHomeStack() {
611 return mStack != null && mStack.mStackId == HOME_STACK_ID;
612 }
613
Chong Zhang09b21ef2015-09-14 10:20:21 -0700614 boolean inFreeformWorkspace() {
615 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
616 }
617
Filip Gruszczynski955b2fc2015-10-15 14:46:07 -0700618 boolean inDockedWorkspace() {
619 return mStack != null && mStack.mStackId == DOCKED_STACK_ID;
620 }
621
Chong Zhangb15758a2015-11-17 12:12:03 -0800622 boolean isResizeableByDockedStack() {
Wale Ogunwale5e7409c2016-01-11 12:24:19 -0800623 final DisplayContent displayContent = getDisplayContent();
624 return displayContent != null && displayContent.getDockedStackLocked() != null
625 && mStack != null && StackId.isTaskResizeableByDockedStack(mStack.mStackId);
Chong Zhangb15758a2015-11-17 12:12:03 -0800626 }
627
628 /**
629 * Whether the task should be treated as if it's docked. Returns true if the task
630 * is currently in docked workspace, or it's side-by-side to a docked task.
631 */
632 boolean isDockedInEffect() {
633 return inDockedWorkspace() || isResizeableByDockedStack();
634 }
635
Chong Zhang2a88fc32016-01-11 17:14:24 -0800636 boolean isTwoFingerScrollMode() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800637 return inCropWindowsResizeMode() && isDockedInEffect();
Chong Zhang2a88fc32016-01-11 17:14:24 -0800638 }
639
Chong Zhangd8ceb852015-11-11 14:53:41 -0800640 WindowState getTopVisibleAppMainWindow() {
641 final AppWindowToken token = getTopVisibleAppToken();
642 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700643 }
644
Chong Zhangd8ceb852015-11-11 14:53:41 -0800645 AppWindowToken getTopVisibleAppToken() {
646 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
647 final AppWindowToken token = mAppTokens.get(i);
648 // skip hidden (or about to hide) apps
649 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
650 return token;
651 }
652 }
653 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700654 }
655
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800656 @Override
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700657 public boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700658 if (useCurrentBounds()) {
659 return mFullscreen;
660 }
661 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
662 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
663 // system.
664 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700665 }
666
667 @Override
668 public DisplayInfo getDisplayInfo() {
669 return mStack.getDisplayContent().getDisplayInfo();
670 }
671
672 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800673 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800674 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800675 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700676
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700677 @Override
678 public String toShortString() {
679 return "Task=" + mTaskId;
680 }
681
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800682 public void dump(String prefix, PrintWriter pw) {
683 final String doublePrefix = prefix + " ";
684
685 pw.println(prefix + "taskId=" + mTaskId);
686 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
687 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
688 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
689 pw.println(doublePrefix + "appTokens=" + mAppTokens);
690 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
691
692 final String triplePrefix = doublePrefix + " ";
693
694 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
695 final AppWindowToken wtoken = mAppTokens.get(i);
696 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
697 wtoken.dump(pw, triplePrefix);
698 }
699
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700700 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800701}