blob: 6aaf3c72d4fc5b37a287db73f602d8ed9843d08c [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;
22import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070023import static android.app.ActivityManager.RESIZE_MODE_SYSTEM_SCREEN_ROTATION;
Craig Mautner42bf39e2014-02-21 16:46:22 -080024import static com.android.server.wm.WindowManagerService.TAG;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070025import static com.android.server.wm.WindowManagerService.DEBUG_RESIZE;
Craig Mautnere3119b72015-01-20 15:02:36 -080026import static com.android.server.wm.WindowManagerService.DEBUG_STACK;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070027import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Wale Ogunwale99db1862015-10-23 20:08:22 -070028
Wale Ogunwale3797c222015-10-27 14:21:58 -070029import android.app.ActivityManager.StackId;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070030import android.content.res.Configuration;
31import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080032import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080033import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070034import android.view.DisplayInfo;
35import android.view.Surface;
36
Craig Mautnere3119b72015-01-20 15:02:36 -080037import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080038
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070039import java.io.PrintWriter;
40import java.util.ArrayList;
41
42class Task implements DimLayer.DimLayerUser {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070043 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
44 static final int BOUNDS_CHANGE_NONE = 0;
45 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
46 static final int BOUNDS_CHANGE_POSITION = 1;
47 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
48 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
49
Craig Mautnerc00204b2013-03-05 15:02:14 -080050 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080051 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080052 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070053 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080054 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080055 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080056
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070057 // Content limits relative to the DisplayContent this sits in.
58 private Rect mBounds = new Rect();
59
60 // Device rotation as of the last time {@link #mBounds} was set.
61 int mRotation;
62
63 // Whether mBounds is fullscreen
64 private boolean mFullscreen = true;
65
66 // Contains configurations settings that are different from the global configuration due to
67 // stack specific operations. E.g. {@link #setBounds}.
68 Configuration mOverrideConfig;
69
70 // For comparison with DisplayContent bounds.
71 private Rect mTmpRect = new Rect();
72 // For handling display rotations.
73 private Rect mTmpRect2 = new Rect();
74
Chong Zhang3005e752015-09-18 18:46:28 -070075 // Whether the task is currently being drag-resized
76 private boolean mDragResizing;
77
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070078 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
79 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -080080 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -080081 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -070082 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -080083 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070084 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -080085 }
86
87 DisplayContent getDisplayContent() {
88 return mStack.getDisplayContent();
89 }
90
91 void addAppToken(int addPos, AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -080092 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -080093 if (addPos >= lastPos) {
94 addPos = lastPos;
95 } else {
96 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
97 if (mAppTokens.get(pos).removed) {
98 // addPos assumes removed tokens are actually gone.
99 ++addPos;
100 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700101 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800102 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800103 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800104 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800105 mDeferRemoval = false;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800106 }
107
Craig Mautnere3119b72015-01-20 15:02:36 -0800108 void removeLocked() {
109 if (!mAppTokens.isEmpty() && mStack.isAnimating()) {
Craig Mautner83162a92015-01-26 14:43:30 -0800110 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800111 mDeferRemoval = true;
112 return;
113 }
Craig Mautner83162a92015-01-26 14:43:30 -0800114 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
115 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800116 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700117 DisplayContent content = getDisplayContent();
118 if (content != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800119 content.mDimLayerController.removeDimLayerUser(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700120 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800121 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800122 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800123 }
124
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800125 void moveTaskToStack(TaskStack stack, boolean toTop) {
126 if (stack == mStack) {
127 return;
128 }
129 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
130 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700131 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800132 if (mStack != null) {
133 mStack.removeTask(this);
134 }
135 stack.addTask(this, toTop);
136 }
137
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700138 void positionTaskInStack(TaskStack stack, int position) {
139 if (mStack != null && stack != mStack) {
140 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
141 + " from stack=" + mStack);
142 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
143 mStack.removeTask(this);
144 }
145 stack.positionTask(this, position, showForAllUsers());
146 }
147
Craig Mautnerc00204b2013-03-05 15:02:14 -0800148 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800149 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800150 if (mAppTokens.size() == 0) {
Wale Ogunwale000957c2015-04-03 08:19:12 -0700151 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId,
Craig Mautner2c2549c2013-11-12 08:31:15 -0800152 "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800153 if (mDeferRemoval) {
154 removeLocked();
155 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800156 }
Craig Mautner83162a92015-01-26 14:43:30 -0800157 wtoken.mTask = null;
158 /* Leave mTaskId for now, it might be useful for debug
159 wtoken.mTaskId = -1;
160 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800161 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800162 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800163
Craig Mautnercbd84af2014-10-22 13:21:22 -0700164 void setSendingToBottom(boolean toBottom) {
165 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
166 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
167 }
168 }
169
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700170 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700171 int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700172 if (config == null) {
173 config = Configuration.EMPTY;
174 }
175 if (bounds == null && !Configuration.EMPTY.equals(config)) {
176 throw new IllegalArgumentException("null bounds but non empty configuration: "
177 + config);
178 }
179 if (bounds != null && Configuration.EMPTY.equals(config)) {
180 throw new IllegalArgumentException("non null bounds, but empty configuration");
181 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700182 boolean oldFullscreen = mFullscreen;
183 int rotation = Surface.ROTATION_0;
184 final DisplayContent displayContent = mStack.getDisplayContent();
185 if (displayContent != null) {
186 displayContent.getLogicalDisplayRect(mTmpRect);
187 rotation = displayContent.getDisplayInfo().rotation;
188 if (bounds == null) {
189 bounds = mTmpRect;
190 mFullscreen = true;
191 } else {
Wale Ogunwale99db1862015-10-23 20:08:22 -0700192 if ((mStack.mStackId != FREEFORM_WORKSPACE_STACK_ID
193 && mStack.mStackId != PINNED_STACK_ID) || bounds.isEmpty()) {
Stefan Kuhne234dbf82015-08-13 09:44:28 -0700194 // ensure bounds are entirely within the display rect
195 if (!bounds.intersect(mTmpRect)) {
196 // Can't set bounds outside the containing display...Sorry!
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700197 return BOUNDS_CHANGE_NONE;
Stefan Kuhne234dbf82015-08-13 09:44:28 -0700198 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700199 }
200 mFullscreen = mTmpRect.equals(bounds);
201 }
202 }
203
204 if (bounds == null) {
205 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700206 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700207 }
208 if (mBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700209 return BOUNDS_CHANGE_NONE;
210 }
211
212 int boundsChange = BOUNDS_CHANGE_NONE;
213 if (mBounds.left != bounds.left || mBounds.right != bounds.right) {
214 boundsChange |= BOUNDS_CHANGE_POSITION;
215 }
216 if (mBounds.width() != bounds.width() || mBounds.height() != bounds.height()) {
217 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700218 }
219
220 mBounds.set(bounds);
221 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700222 if (displayContent != null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800223 displayContent.mDimLayerController.updateDimLayer(this);
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700224 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700225 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700226 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700227 }
228
Chong Zhang87b21722015-09-21 15:39:51 -0700229 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700230 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700231 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700232 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700233 }
234 if (boundsChanged == BOUNDS_CHANGE_NONE) {
235 return false;
236 }
237 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
238 resizeWindows();
239 }
240 return true;
241 }
242
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700243 /** Return true if the current bound can get outputted to the rest of the system as-is. */
244 private boolean useCurrentBounds() {
245 final DisplayContent displayContent = mStack.getDisplayContent();
246 if (mFullscreen
Wale Ogunwale3797c222015-10-27 14:21:58 -0700247 || !StackId.isTaskResizeableByDockedStack(mStack.mStackId)
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700248 || displayContent == null
249 || displayContent.getDockedStackLocked() != null) {
250 return true;
251 }
252 return false;
253 }
254
255 /** Bounds of the task with other system factors taken into consideration. */
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700256 @Override
257 public void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700258 if (useCurrentBounds()) {
259 // No need to adjust the output bounds if fullscreen or the docked stack is visible
260 // since it is already what we want to represent to the rest of the system.
261 out.set(mBounds);
262 return;
263 }
264
265 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
266 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
267 // system.
268 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700269 }
270
Chong Zhang3005e752015-09-18 18:46:28 -0700271 void setDragResizing(boolean dragResizing) {
Chong Zhang3005e752015-09-18 18:46:28 -0700272 mDragResizing = dragResizing;
273 }
274
275 boolean isDragResizing() {
276 return mDragResizing;
277 }
278
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700279 void updateDisplayInfo(final DisplayContent displayContent) {
280 if (displayContent == null) {
281 return;
282 }
283 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700284 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700285 return;
286 }
287 final int newRotation = displayContent.getDisplayInfo().rotation;
288 if (mRotation == newRotation) {
289 return;
290 }
291
292 // Device rotation changed. We don't want the task to move around on the screen when
293 // this happens, so update the task bounds so it stays in the same place.
Wale Ogunwale94744212015-09-21 19:01:47 -0700294 mTmpRect2.set(mBounds);
295 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700296 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
297 // Post message to inform activity manager of the bounds change simulating
298 // a one-way call. We do this to prevent a deadlock between window manager
Filip Gruszczynski44bc4da2015-10-03 13:59:49 -0700299 // lock and activity manager lock been held. Only tasks within the freeform stack
300 // are resizeable independently of their stack resizing.
301 if (mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
302 mService.mH.sendMessage(mService.mH.obtainMessage(
303 RESIZE_TASK, mTaskId, RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mBounds));
304 }
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700305 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700306 }
307
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700308 void resizeWindows() {
309 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
310 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
311 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
312 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
313 final WindowState win = windows.get(winNdx);
314 if (!resizingWindows.contains(win)) {
315 if (DEBUG_RESIZE) Slog.d(TAG, "setBounds: Resizing " + win);
316 resizingWindows.add(win);
317 }
318 }
319 }
320 }
321
Winsonc28098f2015-10-30 14:50:19 -0700322 /**
323 * Cancels any running app transitions associated with the task.
324 */
325 void cancelTaskWindowTransition() {
326 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
327 mAppTokens.get(activityNdx).mAppAnimator.clearAnimation();
328 }
329 }
330
Winson13d30662015-11-06 15:30:29 -0800331 /**
332 * Cancels any running thumbnail transitions associated with the task.
333 */
334 void cancelTaskThumbnailTransition() {
335 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
336 mAppTokens.get(activityNdx).mAppAnimator.clearThumbnail();
337 }
338 }
339
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700340 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700341 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700342 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700343 }
344
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700345 boolean inHomeStack() {
346 return mStack != null && mStack.mStackId == HOME_STACK_ID;
347 }
348
Chong Zhang09b21ef2015-09-14 10:20:21 -0700349 boolean inFreeformWorkspace() {
350 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
351 }
352
Filip Gruszczynski955b2fc2015-10-15 14:46:07 -0700353 boolean inDockedWorkspace() {
354 return mStack != null && mStack.mStackId == DOCKED_STACK_ID;
355 }
356
Chong Zhang9184ec62015-09-24 12:32:21 -0700357 WindowState getTopAppMainWindow() {
358 final int tokensCount = mAppTokens.size();
359 return tokensCount > 0 ? mAppTokens.get(tokensCount - 1).findMainWindow() : null;
360 }
361
Chong Zhangbef461f2015-10-27 11:38:24 -0700362 AppWindowToken getTopAppWindowToken() {
363 final int tokensCount = mAppTokens.size();
364 return tokensCount > 0 ? mAppTokens.get(tokensCount - 1) : null;
365 }
366
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800367 @Override
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700368 public boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700369 if (useCurrentBounds()) {
370 return mFullscreen;
371 }
372 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
373 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
374 // system.
375 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700376 }
377
378 @Override
379 public DisplayInfo getDisplayInfo() {
380 return mStack.getDisplayContent().getDisplayInfo();
381 }
382
383 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800384 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800385 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800386 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700387
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700388 @Override
389 public String toShortString() {
390 return "Task=" + mTaskId;
391 }
392
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700393 public void printTo(String prefix, PrintWriter pw) {
394 pw.print(prefix); pw.print("taskId="); pw.print(mTaskId);
395 pw.print(prefix); pw.print("appTokens="); pw.print(mAppTokens);
396 pw.print(prefix); pw.print("mdr="); pw.println(mDeferRemoval);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700397 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800398}