blob: 0d3535495aff8af5797dc3c374f6ee48c73239d3 [file] [log] [blame]
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Jorim Jaggid53f0922016-04-06 22:16:23 -070019import static android.app.ActivityManager.RESIZE_MODE_SYSTEM_SCREEN_ROTATION;
Wale Ogunwale3797c222015-10-27 14:21:58 -070020import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
21import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080023import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS;
Jorim Jaggid53f0922016-04-06 22:16:23 -070024import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080025import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_RESIZE;
26import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Jorim Jaggid53f0922016-04-06 22:16:23 -070027import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
28import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070029import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Robert Carr2c17cd22016-04-08 17:52:28 -070030import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Wale Ogunwale99db1862015-10-23 20:08:22 -070031
Wale Ogunwale3797c222015-10-27 14:21:58 -070032import android.app.ActivityManager.StackId;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080033import android.content.pm.ActivityInfo;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070034import android.content.res.Configuration;
35import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080036import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080037import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070038import android.view.DisplayInfo;
39import android.view.Surface;
40
Craig Mautnere3119b72015-01-20 15:02:36 -080041import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080042
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070043import java.io.PrintWriter;
44import java.util.ArrayList;
45
46class Task implements DimLayer.DimLayerUser {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -080047 static final String TAG = TAG_WITH_CLASS_NAME ? "Task" : TAG_WM;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070048 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
49 static final int BOUNDS_CHANGE_NONE = 0;
50 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
51 static final int BOUNDS_CHANGE_POSITION = 1;
52 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
53 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
54
Craig Mautnerc00204b2013-03-05 15:02:14 -080055 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080056 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080057 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070058 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080059 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080060 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080061
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070062 // Content limits relative to the DisplayContent this sits in.
63 private Rect mBounds = new Rect();
Jorim Jaggi0429f352015-12-22 16:29:16 +010064 final Rect mPreparedFrozenBounds = new Rect();
Jorim Jaggi26c8c422016-05-09 19:57:25 -070065 final Configuration mPreparedFrozenMergedConfig = new Configuration();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070066
Chong Zhangf66db432016-01-13 10:39:51 -080067 private Rect mPreScrollBounds = new Rect();
68 private boolean mScrollValid;
69
Jorim Jaggidc249c42015-12-15 14:57:31 -080070 // Bounds used to calculate the insets.
71 private final Rect mTempInsetBounds = new Rect();
72
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070073 // Device rotation as of the last time {@link #mBounds} was set.
74 int mRotation;
75
76 // Whether mBounds is fullscreen
77 private boolean mFullscreen = true;
78
79 // Contains configurations settings that are different from the global configuration due to
80 // stack specific operations. E.g. {@link #setBounds}.
Jorim Jaggi26c8c422016-05-09 19:57:25 -070081 Configuration mOverrideConfig = Configuration.EMPTY;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070082
83 // For comparison with DisplayContent bounds.
84 private Rect mTmpRect = new Rect();
85 // For handling display rotations.
86 private Rect mTmpRect2 = new Rect();
87
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080088 // Resize mode of the task. See {@link ActivityInfo#resizeMode}
89 private int mResizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -080090
Chong Zhang3005e752015-09-18 18:46:28 -070091 // Whether the task is currently being drag-resized
92 private boolean mDragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010093 private int mDragResizeMode;
Chong Zhang3005e752015-09-18 18:46:28 -070094
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080095 private boolean mHomeTask;
96
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070097 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
98 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -080099 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800100 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -0700101 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -0800102 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700103 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800104 }
105
106 DisplayContent getDisplayContent() {
107 return mStack.getDisplayContent();
108 }
109
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800110 void addAppToken(int addPos, AppWindowToken wtoken, int resizeMode, boolean homeTask) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800111 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800112 if (addPos >= lastPos) {
113 addPos = lastPos;
114 } else {
115 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
116 if (mAppTokens.get(pos).removed) {
117 // addPos assumes removed tokens are actually gone.
118 ++addPos;
119 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700120 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800121 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800122 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800123 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800124 mDeferRemoval = false;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800125 mResizeMode = resizeMode;
126 mHomeTask = homeTask;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800127 }
128
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700129 private boolean hasWindowsAlive() {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800130 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700131 if (mAppTokens.get(i).hasWindowsAlive()) {
Chong Zhang7e8eeb72016-01-06 19:14:47 -0800132 return true;
133 }
134 }
135 return false;
136 }
137
Craig Mautnere3119b72015-01-20 15:02:36 -0800138 void removeLocked() {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700139 if (hasWindowsAlive() && mStack.isAnimating()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800140 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800141 mDeferRemoval = true;
142 return;
143 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800144 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
Craig Mautner83162a92015-01-26 14:43:30 -0800145 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800146 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700147 DisplayContent content = getDisplayContent();
148 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800149 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700150 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800151 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800152 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800153 }
154
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800155 void moveTaskToStack(TaskStack stack, boolean toTop) {
156 if (stack == mStack) {
157 return;
158 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800159 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800160 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700161 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800162 if (mStack != null) {
163 mStack.removeTask(this);
164 }
165 stack.addTask(this, toTop);
166 }
167
Wale Ogunwale935e5022015-11-10 12:36:10 -0800168 void positionTaskInStack(TaskStack stack, int position, Rect bounds, Configuration config) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700169 if (mStack != null && stack != mStack) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800170 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700171 + " from stack=" + mStack);
172 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
173 mStack.removeTask(this);
174 }
175 stack.positionTask(this, position, showForAllUsers());
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800176 resizeLocked(bounds, config, false /* force */);
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800177
178 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
179 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
180 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
181 final WindowState win = windows.get(winNdx);
182 win.notifyMovedInStack();
183 }
184 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700185 }
186
Craig Mautnerc00204b2013-03-05 15:02:14 -0800187 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800188 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800189 if (mAppTokens.size() == 0) {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100190 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800191 if (mDeferRemoval) {
192 removeLocked();
193 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800194 }
Craig Mautner83162a92015-01-26 14:43:30 -0800195 wtoken.mTask = null;
196 /* Leave mTaskId for now, it might be useful for debug
197 wtoken.mTaskId = -1;
198 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800199 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800200 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800201
Craig Mautnercbd84af2014-10-22 13:21:22 -0700202 void setSendingToBottom(boolean toBottom) {
203 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
204 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
205 }
206 }
207
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700208 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800209 private int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700210 if (config == null) {
211 config = Configuration.EMPTY;
212 }
213 if (bounds == null && !Configuration.EMPTY.equals(config)) {
214 throw new IllegalArgumentException("null bounds but non empty configuration: "
215 + config);
216 }
217 if (bounds != null && Configuration.EMPTY.equals(config)) {
218 throw new IllegalArgumentException("non null bounds, but empty configuration");
219 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700220 boolean oldFullscreen = mFullscreen;
221 int rotation = Surface.ROTATION_0;
222 final DisplayContent displayContent = mStack.getDisplayContent();
223 if (displayContent != null) {
224 displayContent.getLogicalDisplayRect(mTmpRect);
225 rotation = displayContent.getDisplayInfo().rotation;
Jorim Jaggi067e8172016-02-03 18:24:12 -0800226 mFullscreen = bounds == null;
227 if (mFullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700228 bounds = mTmpRect;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700229 }
230 }
231
232 if (bounds == null) {
233 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700234 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700235 }
Chong Zhangf66db432016-01-13 10:39:51 -0800236 if (mPreScrollBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700237 return BOUNDS_CHANGE_NONE;
238 }
239
240 int boundsChange = BOUNDS_CHANGE_NONE;
Chong Zhangf66db432016-01-13 10:39:51 -0800241 if (mPreScrollBounds.left != bounds.left || mPreScrollBounds.top != bounds.top) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700242 boundsChange |= BOUNDS_CHANGE_POSITION;
243 }
Chong Zhangf66db432016-01-13 10:39:51 -0800244 if (mPreScrollBounds.width() != bounds.width() || mPreScrollBounds.height() != bounds.height()) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700245 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700246 }
247
Chong Zhangf66db432016-01-13 10:39:51 -0800248
249 mPreScrollBounds.set(bounds);
250
251 resetScrollLocked();
252
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700253 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700254 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800255 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700256 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700257 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700258 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700259 }
260
Jorim Jaggidc249c42015-12-15 14:57:31 -0800261 /**
262 * Sets the bounds used to calculate the insets. See
263 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
264 */
265 void setTempInsetBounds(Rect tempInsetBounds) {
266 if (tempInsetBounds != null) {
267 mTempInsetBounds.set(tempInsetBounds);
268 } else {
269 mTempInsetBounds.setEmpty();
270 }
271 }
272
273 /**
274 * Gets the bounds used to calculate the insets. See
275 * {@link android.app.IActivityManager#resizeDockedStack} why this is needed.
276 */
277 void getTempInsetBounds(Rect out) {
278 out.set(mTempInsetBounds);
279 }
280
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800281 void setResizeable(int resizeMode) {
282 mResizeMode = resizeMode;
Chong Zhangb15758a2015-11-17 12:12:03 -0800283 }
284
285 boolean isResizeable() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800286 return !mHomeTask
287 && (ActivityInfo.isResizeableMode(mResizeMode) || mService.mForceResizableTasks);
288 }
289
290 boolean cropWindowsToStackBounds() {
291 return !mHomeTask && (isResizeable() || mResizeMode == RESIZE_MODE_CROP_WINDOWS);
292 }
293
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800294 boolean isHomeTask() {
295 return mHomeTask;
296 }
297
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800298 private boolean inCropWindowsResizeMode() {
299 return !mHomeTask && !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
Chong Zhangb15758a2015-11-17 12:12:03 -0800300 }
301
Chong Zhang87b21722015-09-21 15:39:51 -0700302 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700303 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700304 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700305 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700306 }
307 if (boundsChanged == BOUNDS_CHANGE_NONE) {
308 return false;
309 }
310 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
311 resizeWindows();
Chong Zhangbd0d9372015-12-28 15:18:29 -0800312 } else {
313 moveWindows();
Chong Zhang3005e752015-09-18 18:46:28 -0700314 }
315 return true;
316 }
317
Jorim Jaggi0429f352015-12-22 16:29:16 +0100318 /**
319 * Prepares the task bounds to be frozen with the current size. See
320 * {@link AppWindowToken#freezeBounds}.
321 */
322 void prepareFreezingBounds() {
323 mPreparedFrozenBounds.set(mBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700324 mPreparedFrozenMergedConfig.setTo(mService.mCurConfiguration);
325 mPreparedFrozenMergedConfig.updateFrom(mOverrideConfig);
Jorim Jaggi0429f352015-12-22 16:29:16 +0100326 }
327
Chong Zhang5117e272016-05-03 12:47:34 -0700328 /**
329 * Align the task to the adjusted bounds.
330 *
331 * @param adjustedBounds Adjusted bounds to which the task should be aligned.
332 * @param tempInsetBounds Insets bounds for the task.
333 * @param alignBottom True if the task's bottom should be aligned to the adjusted
334 * bounds's bottom; false if the task's top should be aligned
335 * the adjusted bounds's top.
336 */
337 void alignToAdjustedBounds(
338 Rect adjustedBounds, Rect tempInsetBounds, boolean alignBottom) {
339 if (!isResizeable() || mOverrideConfig == Configuration.EMPTY) {
340 return;
341 }
342
343 getBounds(mTmpRect2);
344 if (alignBottom) {
345 int offsetY = adjustedBounds.bottom - mTmpRect2.bottom;
346 mTmpRect2.offset(0, offsetY);
347 } else {
348 mTmpRect2.offsetTo(adjustedBounds.left, adjustedBounds.top);
349 }
350 setTempInsetBounds(tempInsetBounds);
351 resizeLocked(mTmpRect2, mOverrideConfig, false /* forced */);
352 }
353
Chong Zhangf66db432016-01-13 10:39:51 -0800354 void resetScrollLocked() {
355 if (mScrollValid) {
356 mScrollValid = false;
357 applyScrollToAllWindows(0, 0);
358 }
359 mBounds.set(mPreScrollBounds);
360 }
361
362 void applyScrollToAllWindows(final int xOffset, final int yOffset) {
363 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
364 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
365 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
366 final WindowState win = windows.get(winNdx);
367 win.mXOffset = xOffset;
368 win.mYOffset = yOffset;
369 }
370 }
371 }
372
373 void applyScrollToWindowIfNeeded(final WindowState win) {
374 if (mScrollValid) {
375 win.mXOffset = mBounds.left;
376 win.mYOffset = mBounds.top;
377 }
378 }
379
Chong Zhangb15758a2015-11-17 12:12:03 -0800380 boolean scrollLocked(Rect bounds) {
381 // shift the task bound if it doesn't fully cover the stack area
382 mStack.getDimBounds(mTmpRect);
383 if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) {
384 if (bounds.left > mTmpRect.left) {
385 bounds.left = mTmpRect.left;
386 bounds.right = mTmpRect.left + mBounds.width();
387 } else if (bounds.right < mTmpRect.right) {
388 bounds.left = mTmpRect.right - mBounds.width();
389 bounds.right = mTmpRect.right;
390 }
391 } else {
392 if (bounds.top > mTmpRect.top) {
393 bounds.top = mTmpRect.top;
394 bounds.bottom = mTmpRect.top + mBounds.height();
395 } else if (bounds.bottom < mTmpRect.bottom) {
396 bounds.top = mTmpRect.bottom - mBounds.height();
397 bounds.bottom = mTmpRect.bottom;
398 }
399 }
400
Chong Zhangf66db432016-01-13 10:39:51 -0800401 // We can stop here if we're already scrolling and the scrolled bounds not changed.
402 if (mScrollValid && bounds.equals(mBounds)) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800403 return false;
404 }
Chong Zhangf66db432016-01-13 10:39:51 -0800405
Chong Zhangb15758a2015-11-17 12:12:03 -0800406 // Normal setBounds() does not allow non-null bounds for fullscreen apps.
407 // We only change bounds for the scrolling case without change it size,
408 // on resizing path we should still want the validation.
409 mBounds.set(bounds);
Chong Zhangf66db432016-01-13 10:39:51 -0800410 mScrollValid = true;
411 applyScrollToAllWindows(bounds.left, bounds.top);
Chong Zhangb15758a2015-11-17 12:12:03 -0800412 return true;
413 }
414
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700415 /** Return true if the current bound can get outputted to the rest of the system as-is. */
416 private boolean useCurrentBounds() {
417 final DisplayContent displayContent = mStack.getDisplayContent();
418 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700419 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700420 || displayContent == null
Jorim Jaggif4fa8cb2016-04-01 16:05:10 -0700421 || displayContent.getDockedStackVisibleForUserLocked() != null) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700422 return true;
423 }
424 return false;
425 }
426
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800427 /** Original bounds of the task if applicable, otherwise fullscreen rect. */
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800428 void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700429 if (useCurrentBounds()) {
430 // No need to adjust the output bounds if fullscreen or the docked stack is visible
431 // since it is already what we want to represent to the rest of the system.
432 out.set(mBounds);
433 return;
434 }
435
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -0800436 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack is
437 // not currently visible. Go ahead a represent it as fullscreen to the rest of the system.
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700438 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700439 }
440
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800441 /**
442 * Calculate the maximum visible area of this task. If the task has only one app,
443 * the result will be visible frame of that app. If the task has more than one apps,
444 * we search from top down if the next app got different visible area.
445 *
446 * This effort is to handle the case where some task (eg. GMail composer) might pop up
447 * a dialog that's different in size from the activity below, in which case we should
448 * be dimming the entire task area behind the dialog.
449 *
450 * @param out Rect containing the max visible bounds.
451 * @return true if the task has some visible app windows; false otherwise.
452 */
453 boolean getMaxVisibleBounds(Rect out) {
454 boolean foundTop = false;
455 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800456 final AppWindowToken token = mAppTokens.get(i);
457 // skip hidden (or about to hide) apps
458 if (token.mIsExiting || token.clientHidden || token.hiddenRequested) {
459 continue;
460 }
461 final WindowState win = token.findMainWindow();
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800462 if (win == null) {
463 continue;
464 }
465 if (!foundTop) {
466 out.set(win.mVisibleFrame);
467 foundTop = true;
468 continue;
469 }
470 if (win.mVisibleFrame.left < out.left) {
471 out.left = win.mVisibleFrame.left;
472 }
473 if (win.mVisibleFrame.top < out.top) {
474 out.top = win.mVisibleFrame.top;
475 }
476 if (win.mVisibleFrame.right > out.right) {
477 out.right = win.mVisibleFrame.right;
478 }
479 if (win.mVisibleFrame.bottom > out.bottom) {
480 out.bottom = win.mVisibleFrame.bottom;
481 }
482 }
483 return foundTop;
484 }
485
486 /** Bounds of the task to be used for dimming, as well as touch related tests. */
487 @Override
488 public void getDimBounds(Rect out) {
Robert Carra86a6bf2016-04-08 17:34:16 -0700489 final DisplayContent displayContent = mStack.getDisplayContent();
490 // It doesn't matter if we in particular are part of the resize, since we couldn't have
491 // a DimLayer anyway if we weren't visible.
492 final boolean dockedResizing = displayContent != null ?
493 displayContent.mDividerControllerLocked.isResizing() : false;
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800494 if (useCurrentBounds()) {
495 if (inFreeformWorkspace() && getMaxVisibleBounds(out)) {
496 return;
497 }
498
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700499 if (!mFullscreen) {
500 // When minimizing the docked stack when going home, we don't adjust the task bounds
501 // so we need to intersect the task bounds with the stack bounds here.
Robert Carra86a6bf2016-04-08 17:34:16 -0700502 //
503 // If we are Docked Resizing with snap points, the task bounds could be smaller than the stack
504 // bounds and so we don't even want to use them. Even if the app should not be resized the Dim
505 // should keep up with the divider.
506 if (dockedResizing) {
507 mStack.getBounds(out);
508 } else {
509 mStack.getBounds(mTmpRect);
510 mTmpRect.intersect(mBounds);
511 }
Jorim Jaggi22a869f2016-03-25 23:33:21 -0700512 out.set(mTmpRect);
513 } else {
514 out.set(mBounds);
515 }
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800516 return;
517 }
518
519 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
520 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
521 // system.
Robert Carra86a6bf2016-04-08 17:34:16 -0700522 displayContent.getLogicalDisplayRect(out);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800523 }
524
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100525 void setDragResizing(boolean dragResizing, int dragResizeMode) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800526 if (mDragResizing != dragResizing) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100527 if (!DragResizeMode.isModeAllowedForStack(mStack.mStackId, dragResizeMode)) {
528 throw new IllegalArgumentException("Drag resize mode not allow for stack stackId="
529 + mStack.mStackId + " dragResizeMode=" + dragResizeMode);
530 }
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800531 mDragResizing = dragResizing;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100532 mDragResizeMode = dragResizeMode;
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800533 resetDragResizingChangeReported();
534 }
535 }
536
537 void resetDragResizingChangeReported() {
538 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
539 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
540 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
541 final WindowState win = windows.get(winNdx);
542 win.resetDragResizingChangeReported();
543 }
544 }
Chong Zhang3005e752015-09-18 18:46:28 -0700545 }
546
547 boolean isDragResizing() {
Wale Ogunwalece144522016-02-05 22:51:01 -0800548 return mDragResizing || (mStack != null && mStack.isDragResizing());
Chong Zhang3005e752015-09-18 18:46:28 -0700549 }
550
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100551 int getDragResizeMode() {
552 return mDragResizeMode;
553 }
554
Jorim Jaggiff71d202016-04-14 13:12:36 -0700555 /**
556 * Adds all of the tasks windows to {@link WindowManagerService#mWaitingForDrawn} if drag
557 * resizing state of the window has been changed.
558 */
559 void addWindowsWaitingForDrawnIfResizingChanged() {
560 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
561 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
562 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
563 final WindowState win = windows.get(winNdx);
564 if (win.isDragResizeChanged()) {
565 mService.mWaitingForDrawn.add(win);
566 }
567 }
568 }
569 }
570
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700571 void updateDisplayInfo(final DisplayContent displayContent) {
572 if (displayContent == null) {
573 return;
574 }
575 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700576 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700577 return;
578 }
579 final int newRotation = displayContent.getDisplayInfo().rotation;
580 if (mRotation == newRotation) {
581 return;
582 }
583
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800584 // Device rotation changed.
585 // - Reset the bounds to the pre-scroll bounds as whatever scrolling was done is no longer
586 // valid.
587 // - Rotate the bounds and notify activity manager if the task can be resized independently
588 // from its stack. The stack will take care of task rotation for the other case.
Chong Zhangf66db432016-01-13 10:39:51 -0800589 mTmpRect2.set(mPreScrollBounds);
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800590
591 if (!StackId.isTaskResizeAllowed(mStack.mStackId)) {
592 setBounds(mTmpRect2, mOverrideConfig);
593 return;
594 }
595
Wale Ogunwale94744212015-09-21 19:01:47 -0700596 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700597 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
Wale Ogunwalee1fe4d12016-01-14 08:52:30 -0800598 // Post message to inform activity manager of the bounds change simulating a one-way
599 // call. We do this to prevent a deadlock between window manager lock and activity
600 // manager lock been held.
601 mService.mH.obtainMessage(RESIZE_TASK, mTaskId,
602 RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mPreScrollBounds).sendToTarget();
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700603 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700604 }
605
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700606 void resizeWindows() {
607 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
608 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800609 final AppWindowToken atoken = mAppTokens.get(activityNdx);
610
611 // Some windows won't go through the resizing process, if they don't have a surface, so
612 // destroy all saved surfaces here.
613 atoken.destroySavedSurfaces();
614 final ArrayList<WindowState> windows = atoken.allAppWindows;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700615 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
616 final WindowState win = windows.get(winNdx);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800617 if (win.mHasSurface && !resizingWindows.contains(win)) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800618 if (DEBUG_RESIZE) Slog.d(TAG, "resizeWindows: Resizing " + win);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700619 resizingWindows.add(win);
Jorim Jaggif3df0aa2016-04-06 15:56:33 -0700620
621 // If we are not drag resizing, force recreating of a new surface so updating
622 // the content and positioning that surface will be in sync.
Robert Carr2c17cd22016-04-08 17:52:28 -0700623 //
624 // As we use this flag as a hint to freeze surface boundary updates,
625 // we'd like to only apply this to TYPE_BASE_APPLICATION,
626 // windows of TYPE_APPLICATION like dialogs, could appear
627 // to not be drag resizing while they resize, but we'd
628 // still like to manipulate their frame to update crop, etc...
629 //
630 // Anyway we don't need to synchronize position and content updates for these
631 // windows since they aren't at the base layer and could be moved around anyway.
Robert Carr1ca6a332016-04-11 18:00:43 -0700632 if (!win.computeDragResizing() && win.mAttrs.type == TYPE_BASE_APPLICATION &&
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700633 !mStack.getBoundsAnimating() && !win.isGoneForLayoutLw()) {
Jorim Jaggif3df0aa2016-04-06 15:56:33 -0700634 win.mResizedWhileNotDragResizing = true;
635 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700636 }
Jorim Jaggi8fa45222016-02-19 19:54:39 -0800637 if (win.isGoneForLayoutLw()) {
638 win.mResizedWhileGone = true;
639 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700640 }
641 }
642 }
643
Chong Zhangbd0d9372015-12-28 15:18:29 -0800644 void moveWindows() {
645 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
646 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
647 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
648 final WindowState win = windows.get(winNdx);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -0800649 if (DEBUG_RESIZE) Slog.d(TAG, "moveWindows: Moving " + win);
Chong Zhangbd0d9372015-12-28 15:18:29 -0800650 win.mMovedByResize = true;
651 }
652 }
653 }
654
Winsonc28098f2015-10-30 14:50:19 -0700655 /**
656 * Cancels any running app transitions associated with the task.
657 */
658 void cancelTaskWindowTransition() {
659 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
660 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
661 }
662 }
663
Winson13d30662015-11-06 15:30:29 -0800664 /**
665 * Cancels any running thumbnail transitions associated with the task.
666 */
667 void cancelTaskThumbnailTransition() {
668 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
669 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
670 }
671 }
672
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700673 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700674 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700675 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700676 }
677
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800678 boolean isVisibleForUser() {
679 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
680 final AppWindowToken appToken = mAppTokens.get(i);
681 for (int j = appToken.allAppWindows.size() - 1; j >= 0; j--) {
682 WindowState window = appToken.allAppWindows.get(j);
683 if (!window.isHiddenFromUserLocked()) {
684 return true;
685 }
686 }
687 }
688 return false;
689 }
690
Jorim Jaggiff71d202016-04-14 13:12:36 -0700691 boolean isVisible() {
692 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
693 final AppWindowToken appToken = mAppTokens.get(i);
694 if (appToken.isVisible()) {
695 return true;
696 }
697 }
698 return false;
699 }
700
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700701 boolean inHomeStack() {
702 return mStack != null && mStack.mStackId == HOME_STACK_ID;
703 }
704
Chong Zhang09b21ef2015-09-14 10:20:21 -0700705 boolean inFreeformWorkspace() {
706 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
707 }
708
Filip Gruszczynski955b2fc2015-10-15 14:46:07 -0700709 boolean inDockedWorkspace() {
710 return mStack != null && mStack.mStackId == DOCKED_STACK_ID;
711 }
712
Chong Zhangb15758a2015-11-17 12:12:03 -0800713 boolean isResizeableByDockedStack() {
Wale Ogunwale5e7409c2016-01-11 12:24:19 -0800714 final DisplayContent displayContent = getDisplayContent();
715 return displayContent != null && displayContent.getDockedStackLocked() != null
716 && mStack != null && StackId.isTaskResizeableByDockedStack(mStack.mStackId);
Chong Zhangb15758a2015-11-17 12:12:03 -0800717 }
718
Robert Carre6275582016-02-29 15:45:45 -0800719 boolean isFloating() {
720 return StackId.tasksAreFloating(mStack.mStackId);
721 }
722
Chong Zhangb15758a2015-11-17 12:12:03 -0800723 /**
724 * Whether the task should be treated as if it's docked. Returns true if the task
725 * is currently in docked workspace, or it's side-by-side to a docked task.
726 */
727 boolean isDockedInEffect() {
728 return inDockedWorkspace() || isResizeableByDockedStack();
729 }
730
Chong Zhang2a88fc32016-01-11 17:14:24 -0800731 boolean isTwoFingerScrollMode() {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800732 return inCropWindowsResizeMode() && isDockedInEffect();
Chong Zhang2a88fc32016-01-11 17:14:24 -0800733 }
734
Chong Zhangd8ceb852015-11-11 14:53:41 -0800735 WindowState getTopVisibleAppMainWindow() {
736 final AppWindowToken token = getTopVisibleAppToken();
737 return token != null ? token.findMainWindow() : null;
Chong Zhang9184ec62015-09-24 12:32:21 -0700738 }
739
Chong Zhangd8ceb852015-11-11 14:53:41 -0800740 AppWindowToken getTopVisibleAppToken() {
741 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
742 final AppWindowToken token = mAppTokens.get(i);
743 // skip hidden (or about to hide) apps
744 if (!token.mIsExiting && !token.clientHidden && !token.hiddenRequested) {
745 return token;
746 }
747 }
748 return null;
Chong Zhangbef461f2015-10-27 11:38:24 -0700749 }
750
Jorim Jaggiaf558e12016-04-27 22:56:56 -0700751 AppWindowToken getTopAppToken() {
752 return mAppTokens.size() > 0 ? mAppTokens.get(mAppTokens.size() - 1) : null;
753 }
754
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800755 @Override
Wale Ogunwale29bfbb82016-05-12 15:13:52 -0700756 public boolean dimFullscreen() {
757 return isHomeTask() || isFullscreen();
758 }
759
760 boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700761 if (useCurrentBounds()) {
762 return mFullscreen;
763 }
764 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
765 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
766 // system.
767 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700768 }
769
770 @Override
771 public DisplayInfo getDisplayInfo() {
772 return mStack.getDisplayContent().getDisplayInfo();
773 }
774
775 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800776 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800777 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800778 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700779
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700780 @Override
781 public String toShortString() {
782 return "Task=" + mTaskId;
783 }
784
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800785 public void dump(String prefix, PrintWriter pw) {
786 final String doublePrefix = prefix + " ";
787
788 pw.println(prefix + "taskId=" + mTaskId);
789 pw.println(doublePrefix + "mFullscreen=" + mFullscreen);
790 pw.println(doublePrefix + "mBounds=" + mBounds.toShortString());
791 pw.println(doublePrefix + "mdr=" + mDeferRemoval);
792 pw.println(doublePrefix + "appTokens=" + mAppTokens);
793 pw.println(doublePrefix + "mTempInsetBounds=" + mTempInsetBounds.toShortString());
794
795 final String triplePrefix = doublePrefix + " ";
796
797 for (int i = mAppTokens.size() - 1; i >= 0; i--) {
798 final AppWindowToken wtoken = mAppTokens.get(i);
799 pw.println(triplePrefix + "Activity #" + i + " " + wtoken);
800 wtoken.dump(pw, triplePrefix);
801 }
802
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700803 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800804}