blob: f097eb242bfa71176ee1a2beb7d60087ad91a754 [file] [log] [blame]
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Jorim Jaggic6c89a82016-01-28 17:48:21 -080021import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
Robert Carre6275582016-02-29 15:45:45 -080022import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070023import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Jorim Jaggi51605272016-02-24 18:06:49 -050024import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080025import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Chong Zhangb15758a2015-11-17 12:12:03 -080026import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070027import static android.app.ActivityManager.RESIZE_MODE_SYSTEM_SCREEN_ROTATION;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080028import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080029import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080030import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
31import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_RESIZE;
32import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070033import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Chong Zhangc806d902015-11-30 09:44:27 -080034import static com.android.server.wm.WindowManagerService.H.SHOW_NON_RESIZEABLE_DOCK_TOAST;
35import static android.view.WindowManager.DOCKED_INVALID;
36import static android.view.WindowManager.DOCKED_LEFT;
37import static android.view.WindowManager.DOCKED_RIGHT;
38import static android.view.WindowManager.DOCKED_TOP;
Wale Ogunwale99db1862015-10-23 20:08:22 -070039
Wale Ogunwale3797c222015-10-27 14:21:58 -070040import android.app.ActivityManager.StackId;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080041import android.content.pm.ActivityInfo;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070042import android.content.res.Configuration;
43import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080044import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080045import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070046import android.view.DisplayInfo;
47import android.view.Surface;
48
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080049import com.android.internal.R;
Craig Mautnere3119b72015-01-20 15:02:36 -080050import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080051
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070052import java.io.PrintWriter;
53import java.util.ArrayList;
54
55class Task implements DimLayer.DimLayerUser {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080056 static final String TAG = TAG_WITH_CLASS_NAME ? "Task" : TAG_WM;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070057 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
58 static final int BOUNDS_CHANGE_NONE = 0;
59 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
60 static final int BOUNDS_CHANGE_POSITION = 1;
61 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
62 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
63
Craig Mautnerc00204b2013-03-05 15:02:14 -080064 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080065 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080066 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070067 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080068 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080069 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080070
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070071 // Content limits relative to the DisplayContent this sits in.
72 private Rect mBounds = new Rect();
Jorim Jaggi0429f352015-12-22 16:29:16 +010073 final Rect mPreparedFrozenBounds = new Rect();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070074
Chong Zhangf66db432016-01-13 10:39:51 -080075 private Rect mPreScrollBounds = new Rect();
76 private boolean mScrollValid;
77
Jorim Jaggidc249c42015-12-15 14:57:31 -080078 // Bounds used to calculate the insets.
79 private final Rect mTempInsetBounds = new Rect();
80
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070081 // Device rotation as of the last time {@link #mBounds} was set.
82 int mRotation;
83
84 // Whether mBounds is fullscreen
85 private boolean mFullscreen = true;
86
87 // Contains configurations settings that are different from the global configuration due to
88 // stack specific operations. E.g. {@link #setBounds}.
89 Configuration mOverrideConfig;
90
91 // For comparison with DisplayContent bounds.
92 private Rect mTmpRect = new Rect();
93 // For handling display rotations.
94 private Rect mTmpRect2 = new Rect();
95
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080096 // Resize mode of the task. See {@link ActivityInfo#resizeMode}
97 private int mResizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -080098
Chong Zhangc806d902015-11-30 09:44:27 -080099 // Whether we need to show toast about the app being non-resizeable when it becomes visible.
100 // This flag is set when a non-resizeable task is docked (or side-by-side). It's cleared
101 // after we show the toast.
102 private boolean mShowNonResizeableDockToast;
103
Chong Zhang3005e752015-09-18 18:46:28 -0700104 // Whether the task is currently being drag-resized
105 private boolean mDragResizing;
106
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800107 private boolean mHomeTask;
108
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700109 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
110 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -0800111 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800112 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -0700113 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -0800114 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700115 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800116 }
117
118 DisplayContent getDisplayContent() {
119 return mStack.getDisplayContent();
120 }
121
Chong Zhangc806d902015-11-30 09:44:27 -0800122 void setShowNonResizeableDockToast() {
123 mShowNonResizeableDockToast = true;
124 }
125
126 void scheduleShowNonResizeableDockToastIfNeeded() {
127 if (!mShowNonResizeableDockToast) {
128 return;
129 }
130 final DisplayContent displayContent = mStack.getDisplayContent();
131 // If docked stack is not yet visible, we don't want to show the toast yet,
132 // since we need the visible rect of the docked task to position the toast.
133 if (displayContent == null || displayContent.getDockedStackLocked() == null) {
134 return;
135 }
136
137 mShowNonResizeableDockToast = false;
138
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800139 if (mResizeMode == RESIZE_MODE_UNRESIZEABLE) {
140 final String text =
141 mService.mContext.getString(R.string.dock_non_resizeble_failed_to_dock_text);
142 mService.mH.obtainMessage(SHOW_NON_RESIZEABLE_DOCK_TOAST, 0, 0, text).sendToTarget();
143 return;
144 }
145
Chong Zhangc806d902015-11-30 09:44:27 -0800146 final int dockSide = mStack.getDockSide();
Jorim Jaggi51605272016-02-24 18:06:49 -0500147 if (mResizeMode != RESIZE_MODE_FORCE_RESIZEABLE || dockSide == DOCKED_INVALID) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800148 return;
149 }
150
Chong Zhangc806d902015-11-30 09:44:27 -0800151 int xOffset = 0;
152 int yOffset = 0;
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800153 mStack.getBounds(mTmpRect);
Chong Zhangc806d902015-11-30 09:44:27 -0800154
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800155 if (dockSide == DOCKED_LEFT || dockSide == DOCKED_RIGHT) {
156 // The toast was originally placed at the bottom and centered. To place it at the
157 // bottom-center of the stack, we offset it horizontally by the diff between the center
158 // of the stack bounds vs. the center of the screen.
159 displayContent.getLogicalDisplayRect(mTmpRect2);
160 xOffset = mTmpRect.centerX() - mTmpRect2.centerX();
161 } else if (dockSide == DOCKED_TOP) {
162 // The toast was originally placed at the bottom and centered. To place it at the bottom
163 // center of the top stack, we offset it vertically by the diff between the bottom of
164 // the stack bounds vs. the bottom of the content rect.
165 //
166 // Note here we use the content rect instead of the display rect, as we want the toast's
167 // distance to the dock divider (when it's placed at the top half) to be the same as
168 // it's distance to the top of the navigation bar (when it's placed at the bottom).
Chong Zhangf66db432016-01-13 10:39:51 -0800169
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800170 // We don't adjust for DOCKED_BOTTOM case since it's already at the bottom.
171 displayContent.getContentRect(mTmpRect2);
172 yOffset = mTmpRect2.bottom - mTmpRect.bottom;
Chong Zhangc806d902015-11-30 09:44:27 -0800173 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800174 final String text =
Jorim Jaggi51605272016-02-24 18:06:49 -0500175 mService.mContext.getString(R.string.dock_forced_resizable);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800176 mService.mH.obtainMessage(SHOW_NON_RESIZEABLE_DOCK_TOAST,
177 xOffset, yOffset, text).sendToTarget();
Chong Zhangc806d902015-11-30 09:44:27 -0800178 }
179
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800180 void addAppToken(int addPos, AppWindowToken wtoken, int resizeMode, boolean homeTask) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800181 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800182 if (addPos >= lastPos) {
183 addPos = lastPos;
184 } else {
185 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
186 if (mAppTokens.get(pos).removed) {
187 // addPos assumes removed tokens are actually gone.
188 ++addPos;
189 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700190 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800191 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800192 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800193 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800194 mDeferRemoval = false;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800195 mResizeMode = resizeMode;
196 mHomeTask = homeTask;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800197 }
198
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800199 private boolean hasAppTokensAlive() {
200 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
201 if (!mAppTokens.get(i).appDied) {
202 return true;
203 }
204 }
205 return false;
206 }
207
Craig Mautnere3119b72015-01-20 15:02:36 -0800208 void removeLocked() {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800209 if (hasAppTokensAlive() && mStack.isAnimating()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800210 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800211 mDeferRemoval = true;
212 return;
213 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800214 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800215 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800216 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700217 DisplayContent content = getDisplayContent();
218 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800219 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700220 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800221 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800222 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800223 }
224
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800225 void moveTaskToStack(TaskStack stack, boolean toTop) {
226 if (stack == mStack) {
227 return;
228 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800229 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800230 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700231 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800232 if (mStack != null) {
233 mStack.removeTask(this);
234 }
235 stack.addTask(this, toTop);
236 }
237
Wale Ogunwale935e5022015-11-10 12:36:10 -0800238 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700239 if (mStack != null && stack != mStack) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800240 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700241 + " from stack=" + mStack);
242 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
243 mStack.removeTask(this);
244 }
245 stack.positionTask(this, position, showForAllUsers());
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800246 resizeLocked(bounds, config, false /* force */);
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800247
248 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
249 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
250 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
251 final WindowState win = windows.get(winNdx);
252 win.notifyMovedInStack();
253 }
254 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700255 }
256
Craig Mautnerc00204b2013-03-05 15:02:14 -0800257 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800258 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800259 if (mAppTokens.size() == 0) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100260 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800261 if (mDeferRemoval) {
262 removeLocked();
263 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800264 }
Craig Mautner83162a92015-01-26 14:43:30 -0800265 wtoken.mTask = null;
266 /* Leave mTaskId for now, it might be useful for debug
267 wtoken.mTaskId = -1;
268 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800269 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800270 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800271
Craig Mautnercbd84af2014-10-22 13:21:22 -0700272 void setSendingToBottom(boolean toBottom) {
273 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
274 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
275 }
276 }
277
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700278 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800279 private int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700280 if (config == null) {
281 config = Configuration.EMPTY;
282 }
283 if (bounds == null && !Configuration.EMPTY.equals(config)) {
284 throw new IllegalArgumentException("null bounds but non empty configuration: "
285 + config);
286 }
287 if (bounds != null && Configuration.EMPTY.equals(config)) {
288 throw new IllegalArgumentException("non null bounds, but empty configuration");
289 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700290 boolean oldFullscreen = mFullscreen;
291 int rotation = Surface.ROTATION_0;
292 final DisplayContent displayContent = mStack.getDisplayContent();
293 if (displayContent != null) {
294 displayContent.getLogicalDisplayRect(mTmpRect);
295 rotation = displayContent.getDisplayInfo().rotation;
Jorim Jaggi067e8172016-02-03 18:24:12 -0800296 mFullscreen = bounds == null;
297 if (mFullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700298 bounds = mTmpRect;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700299 }
300 }
301
302 if (bounds == null) {
303 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700304 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700305 }
Chong Zhangf66db432016-01-13 10:39:51 -0800306 if (mPreScrollBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700307 return BOUNDS_CHANGE_NONE;
308 }
309
310 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhangf66db432016-01-13 10:39:51 -0800311 if (mPreScrollBounds.left != bounds.left || mPreScrollBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700312 boundsChange |= BOUNDS_CHANGE_POSITION;
313 }
Chong Zhangf66db432016-01-13 10:39:51 -0800314 if (mPreScrollBounds.width() != bounds.width() || mPreScrollBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700315 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700316 }
317
Chong Zhangf66db432016-01-13 10:39:51 -0800318
319 mPreScrollBounds.set(bounds);
320
321 resetScrollLocked();
322
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700323 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700324 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800325 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700326 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700327 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700328 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700329 }
330
Jorim Jaggidc249c42015-12-15 14:57:31 -0800331 /**
332 * Sets the bounds used to calculate the insets. See
333 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
334 */
335 void setTempInsetBounds(Rect tempInsetBounds) {
336 if (tempInsetBounds != null) {
337 mTempInsetBounds.set(tempInsetBounds);
338 } else {
339 mTempInsetBounds.setEmpty();
340 }
341 }
342
343 /**
344 * Gets the bounds used to calculate the insets. See
345 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
346 */
347 void getTempInsetBounds(Rect out) {
348 out.set(mTempInsetBounds);
349 }
350
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800351 void setResizeable(int resizeMode) {
352 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800353 }
354
355 boolean isResizeable() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800356 return !mHomeTask
357 && (ActivityInfo.isResizeableMode(mResizeMode) || mService.mForceResizableTasks);
358 }
359
360 boolean cropWindowsToStackBounds() {
361 return !mHomeTask && (isResizeable() || mResizeMode == RESIZE_MODE_CROP_WINDOWS);
362 }
363
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800364 boolean isHomeTask() {
365 return mHomeTask;
366 }
367
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800368 private boolean inCropWindowsResizeMode() {
369 return !mHomeTask && !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
Chong Zhangb15758a2015-11-17 12:12:03 -0800370 }
371
Chong Zhang87b21722015-09-21 15:39:51 -0700372 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700373 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700374 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700375 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700376 }
377 if (boundsChanged == BOUNDS_CHANGE_NONE) {
378 return false;
379 }
380 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
381 resizeWindows();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800382 } else {
383 moveWindows();
Chong Zhang3005e752015-09-18 18:46:28 -0700384 }
385 return true;
386 }
387
Jorim Jaggi0429f352015-12-22 16:29:16 +0100388 /**
389 * Prepares the task bounds to be frozen with the current size. See
390 * {@link AppWindowToken#freezeBounds}.
391 */
392 void prepareFreezingBounds() {
393 mPreparedFrozenBounds.set(mBounds);
394 }
395
Chong Zhangf66db432016-01-13 10:39:51 -0800396 void resetScrollLocked() {
397 if (mScrollValid) {
398 mScrollValid = false;
399 applyScrollToAllWindows(0, 0);
400 }
401 mBounds.set(mPreScrollBounds);
402 }
403
404 void applyScrollToAllWindows(final int xOffset, final int yOffset) {
405 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
406 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
407 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
408 final WindowState win = windows.get(winNdx);
409 win.mXOffset = xOffset;
410 win.mYOffset = yOffset;
411 }
412 }
413 }
414
415 void applyScrollToWindowIfNeeded(final WindowState win) {
416 if (mScrollValid) {
417 win.mXOffset = mBounds.left;
418 win.mYOffset = mBounds.top;
419 }
420 }
421
Chong Zhangb15758a2015-11-17 12:12:03 -0800422 boolean scrollLocked(Rect bounds) {
423 // shift the task bound if it doesn't fully cover the stack area
424 mStack.getDimBounds(mTmpRect);
425 if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) {
426 if (bounds.left > mTmpRect.left) {
427 bounds.left = mTmpRect.left;
428 bounds.right = mTmpRect.left + mBounds.width();
429 } else if (bounds.right < mTmpRect.right) {
430 bounds.left = mTmpRect.right - mBounds.width();
431 bounds.right = mTmpRect.right;
432 }
433 } else {
434 if (bounds.top > mTmpRect.top) {
435 bounds.top = mTmpRect.top;
436 bounds.bottom = mTmpRect.top + mBounds.height();
437 } else if (bounds.bottom < mTmpRect.bottom) {
438 bounds.top = mTmpRect.bottom - mBounds.height();
439 bounds.bottom = mTmpRect.bottom;
440 }
441 }
442
Chong Zhangf66db432016-01-13 10:39:51 -0800443 // We can stop here if we're already scrolling and the scrolled bounds not changed.
444 if (mScrollValid && bounds.equals(mBounds)) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800445 return false;
446 }
Chong Zhangf66db432016-01-13 10:39:51 -0800447
Chong Zhangb15758a2015-11-17 12:12:03 -0800448 // Normal setBounds() does not allow non-null bounds for fullscreen apps.
449 // We only change bounds for the scrolling case without change it size,
450 // on resizing path we should still want the validation.
451 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800452 mScrollValid = true;
453 applyScrollToAllWindows(bounds.left, bounds.top);
Chong Zhangb15758a2015-11-17 12:12:03 -0800454 return true;
455 }
456
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700457 /** Return true if the current bound can get outputted to the rest of the system as-is. */
458 private boolean useCurrentBounds() {
459 final DisplayContent displayContent = mStack.getDisplayContent();
460 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700461 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700462 || displayContent == null
463 || displayContent.getDockedStackLocked() != null) {
464 return true;
465 }
466 return false;
467 }
468
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800469 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800470 void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700471 if (useCurrentBounds()) {
472 // No need to adjust the output bounds if fullscreen or the docked stack is visible
473 // since it is already what we want to represent to the rest of the system.
474 out.set(mBounds);
475 return;
476 }
477
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800478 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
479 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700480 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700481 }
482
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800483 /**
484 * Calculate the maximum visible area of this task. If the task has only one app,
485 * the result will be visible frame of that app. If the task has more than one apps,
486 * we search from top down if the next app got different visible area.
487 *
488 * This effort is to handle the case where some task (eg. GMail composer) might pop up
489 * a dialog that's different in size from the activity below, in which case we should
490 * be dimming the entire task area behind the dialog.
491 *
492 * @param out Rect containing the max visible bounds.
493 * @return true if the task has some visible app windows; false otherwise.
494 */
495 boolean getMaxVisibleBounds(Rect out) {
496 boolean foundTop = false;
497 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800498 final AppWindowToken token = mAppTokens.get(i);
499 // skip hidden (or about to hide) apps
500 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
501 continue;
502 }
503 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800504 if (win == null) {
505 continue;
506 }
507 if (!foundTop) {
508 out.set(win.mVisibleFrame);
509 foundTop = true;
510 continue;
511 }
512 if (win.mVisibleFrame.left < out.left) {
513 out.left = win.mVisibleFrame.left;
514 }
515 if (win.mVisibleFrame.top < out.top) {
516 out.top = win.mVisibleFrame.top;
517 }
518 if (win.mVisibleFrame.right > out.right) {
519 out.right = win.mVisibleFrame.right;
520 }
521 if (win.mVisibleFrame.bottom > out.bottom) {
522 out.bottom = win.mVisibleFrame.bottom;
523 }
524 }
525 return foundTop;
526 }
527
528 /** Bounds of the task to be used for dimming, as well as touch related tests. */
529 @Override
530 public void getDimBounds(Rect out) {
531 if (useCurrentBounds()) {
532 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
533 return;
534 }
535
536 out.set(mBounds);
537 return;
538 }
539
540 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
541 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
542 // system.
543 mStack.getDisplayContent().getLogicalDisplayRect(out);
544 }
545
Chong Zhang3005e752015-09-18 18:46:28 -0700546 void setDragResizing(boolean dragResizing) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800547 if (mDragResizing != dragResizing) {
548 mDragResizing = dragResizing;
549 resetDragResizingChangeReported();
550 }
551 }
552
553 void resetDragResizingChangeReported() {
554 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
555 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
556 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
557 final WindowState win = windows.get(winNdx);
558 win.resetDragResizingChangeReported();
559 }
560 }
Chong Zhang3005e752015-09-18 18:46:28 -0700561 }
562
563 boolean isDragResizing() {
Wale Ogunwalece144522016-02-05 22:51:01 -0800564 return mDragResizing || (mStack != null && mStack.isDragResizing());
Chong Zhang3005e752015-09-18 18:46:28 -0700565 }
566
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700567 void updateDisplayInfo(final DisplayContent displayContent) {
568 if (displayContent == null) {
569 return;
570 }
571 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700572 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700573 return;
574 }
575 final int newRotation = displayContent.getDisplayInfo().rotation;
576 if (mRotation == newRotation) {
577 return;
578 }
579
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800580 // Device rotation changed.
581 // - Reset the bounds to the pre-scroll bounds as whatever scrolling was done is no longer
582 // valid.
583 // - Rotate the bounds and notify activity manager if the task can be resized independently
584 // from its stack. The stack will take care of task rotation for the other case.
Chong Zhangf66db432016-01-13 10:39:51 -0800585 mTmpRect2.set(mPreScrollBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800586
587 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
588 setBounds(mTmpRect2, mOverrideConfig);
589 return;
590 }
591
Wale Ogunwale94744212015-09-21 19:01:47 -0700592 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700593 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800594 // Post message to inform activity manager of the bounds change simulating a one-way
595 // call. We do this to prevent a deadlock between window manager lock and activity
596 // manager lock been held.
597 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
598 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mPreScrollBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700599 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700600 }
601
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700602 void resizeWindows() {
603 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
604 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800605 final AppWindowToken atoken = mAppTokens.get(activityNdx);
606
607 // Some windows won't go through the resizing process, if they don't have a surface, so
608 // destroy all saved surfaces here.
609 atoken.destroySavedSurfaces();
610 final ArrayList<WindowState> windows = atoken.allAppWindows;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700611 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
612 final WindowState win = windows.get(winNdx);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800613 if (win.mHasSurface && !resizingWindows.contains(win)) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800614 if (DEBUG_RESIZE) Slog.d(TAG, "resizeWindows: Resizing " + win);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700615 resizingWindows.add(win);
616 }
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800617 if (win.isGoneForLayoutLw()) {
618 win.mResizedWhileGone = true;
619 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700620 }
621 }
622 }
623
Chong Zhangbd0d9372015-12-28 15:18:29 -0800624 void moveWindows() {
625 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
626 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
627 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
628 final WindowState win = windows.get(winNdx);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800629 if (DEBUG_RESIZE) Slog.d(TAG, "moveWindows: Moving " + win);
Chong Zhangbd0d9372015-12-28 15:18:29 -0800630 win.mMovedByResize = true;
631 }
632 }
633 }
634
Winsonc28098f2015-10-30 14:50:19 -0700635 /**
636 * Cancels any running app transitions associated with the task.
637 */
638 void cancelTaskWindowTransition() {
639 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
640 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
641 }
642 }
643
Winson13d30662015-11-06 15:30:29 -0800644 /**
645 * Cancels any running thumbnail transitions associated with the task.
646 */
647 void cancelTaskThumbnailTransition() {
648 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
649 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
650 }
651 }
652
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700653 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700654 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700655 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700656 }
657
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800658 boolean isVisibleForUser() {
659 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
660 final AppWindowToken appToken = mAppTokens.get(i);
661 for (int j = appToken.allAppWindows.size() - 1; j >= 0; j--) {
662 WindowState window = appToken.allAppWindows.get(j);
663 if (!window.isHiddenFromUserLocked()) {
664 return true;
665 }
666 }
667 }
668 return false;
669 }
670
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700671 boolean inHomeStack() {
672 return mStack != null && mStack.mStackId == HOME_STACK_ID;
673 }
674
Chong Zhang09b21ef2015-09-14 10:20:21 -0700675 boolean inFreeformWorkspace() {
676 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
677 }
678
Filip Gruszczynski955b2fc2015-10-15 14:46:07 -0700679 boolean inDockedWorkspace() {
680 return mStack != null && mStack.mStackId == DOCKED_STACK_ID;
681 }
682
Chong Zhangb15758a2015-11-17 12:12:03 -0800683 boolean isResizeableByDockedStack() {
Wale Ogunwale5e7409c2016-01-11 12:24:19 -0800684 final DisplayContent displayContent = getDisplayContent();
685 return displayContent != null && displayContent.getDockedStackLocked() != null
686 && mStack != null && StackId.isTaskResizeableByDockedStack(mStack.mStackId);
Chong Zhangb15758a2015-11-17 12:12:03 -0800687 }
688
Robert Carre6275582016-02-29 15:45:45 -0800689 boolean isFloating() {
690 return StackId.tasksAreFloating(mStack.mStackId);
691 }
692
Chong Zhangb15758a2015-11-17 12:12:03 -0800693 /**
694 * Whether the task should be treated as if it's docked. Returns true if the task
695 * is currently in docked workspace, or it's side-by-side to a docked task.
696 */
697 boolean isDockedInEffect() {
698 return inDockedWorkspace() || isResizeableByDockedStack();
699 }
700
Chong Zhang2a88fc32016-01-11 17:14:24 -0800701 boolean isTwoFingerScrollMode() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800702 return inCropWindowsResizeMode() && isDockedInEffect();
Chong Zhang2a88fc32016-01-11 17:14:24 -0800703 }
704
Chong Zhangd8ceb852015-11-11 14:53:41 -0800705 WindowState getTopVisibleAppMainWindow() {
706 final AppWindowToken token = getTopVisibleAppToken();
707 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700708 }
709
Chong Zhangd8ceb852015-11-11 14:53:41 -0800710 AppWindowToken getTopVisibleAppToken() {
711 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
712 final AppWindowToken token = mAppTokens.get(i);
713 // skip hidden (or about to hide) apps
714 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
715 return token;
716 }
717 }
718 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700719 }
720
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800721 @Override
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700722 public boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700723 if (useCurrentBounds()) {
724 return mFullscreen;
725 }
726 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
727 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
728 // system.
729 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700730 }
731
732 @Override
733 public DisplayInfo getDisplayInfo() {
734 return mStack.getDisplayContent().getDisplayInfo();
735 }
736
737 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800738 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800739 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800740 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700741
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700742 @Override
743 public String toShortString() {
744 return "Task=" + mTaskId;
745 }
746
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800747 public void dump(String prefix, PrintWriter pw) {
748 final String doublePrefix = prefix + " ";
749
750 pw.println(prefix + "taskId=" + mTaskId);
751 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
752 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
753 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
754 pw.println(doublePrefix + "appTokens=" + mAppTokens);
755 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
756
757 final String triplePrefix = doublePrefix + " ";
758
759 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
760 final AppWindowToken wtoken = mAppTokens.get(i);
761 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
762 wtoken.dump(pw, triplePrefix);
763 }
764
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700765 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800766}