blob: ad44196048964b17a5c028f2f85e0c5a0cc491f0 [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
Filip Gruszczynski466f3212015-09-21 17:57:57 -070019import static android.app.ActivityManager.DOCKED_STACK_ID;
Wale Ogunwale99db1862015-10-23 20:08:22 -070020import static android.app.ActivityManager.FREEFORM_WORKSPACE_STACK_ID;
Chong Zhangdb20b5f2015-10-23 14:01:43 -070021import static android.app.ActivityManager.HOME_STACK_ID;
Wale Ogunwale99db1862015-10-23 20:08:22 -070022import static android.app.ActivityManager.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
Craig Mautner42bf39e2014-02-21 16:46:22 -080029
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.util.SparseArray;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070035import android.view.DisplayInfo;
36import android.view.Surface;
37
Craig Mautnere3119b72015-01-20 15:02:36 -080038import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080039
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070040import java.io.PrintWriter;
41import java.util.ArrayList;
42
43class Task implements DimLayer.DimLayerUser {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070044 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
45 static final int BOUNDS_CHANGE_NONE = 0;
46 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
47 static final int BOUNDS_CHANGE_POSITION = 1;
48 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
49 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
50
Craig Mautnerc00204b2013-03-05 15:02:14 -080051 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080052 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080053 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070054 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080055 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080056 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080057
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070058 // Content limits relative to the DisplayContent this sits in.
59 private Rect mBounds = new Rect();
60
61 // Device rotation as of the last time {@link #mBounds} was set.
62 int mRotation;
63
64 // Whether mBounds is fullscreen
65 private boolean mFullscreen = true;
66
67 // Contains configurations settings that are different from the global configuration due to
68 // stack specific operations. E.g. {@link #setBounds}.
69 Configuration mOverrideConfig;
70
71 // For comparison with DisplayContent bounds.
72 private Rect mTmpRect = new Rect();
73 // For handling display rotations.
74 private Rect mTmpRect2 = new Rect();
75
Chong Zhang3005e752015-09-18 18:46:28 -070076 // Whether the task is currently being drag-resized
77 private boolean mDragResizing;
78
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070079 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
80 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -080081 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -080082 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -070083 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -080084 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070085 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -080086 }
87
88 DisplayContent getDisplayContent() {
89 return mStack.getDisplayContent();
90 }
91
92 void addAppToken(int addPos, AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -080093 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -080094 if (addPos >= lastPos) {
95 addPos = lastPos;
96 } else {
97 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
98 if (mAppTokens.get(pos).removed) {
99 // addPos assumes removed tokens are actually gone.
100 ++addPos;
101 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700102 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800103 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800104 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800105 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800106 mDeferRemoval = false;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800107 }
108
Craig Mautnere3119b72015-01-20 15:02:36 -0800109 void removeLocked() {
110 if (!mAppTokens.isEmpty() && mStack.isAnimating()) {
Craig Mautner83162a92015-01-26 14:43:30 -0800111 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800112 mDeferRemoval = true;
113 return;
114 }
Craig Mautner83162a92015-01-26 14:43:30 -0800115 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
116 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800117 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700118 DisplayContent content = getDisplayContent();
119 if (content != null) {
120 content.mDimBehindController.removeDimLayerUser(this);
121 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800122 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800123 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800124 }
125
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800126 void moveTaskToStack(TaskStack stack, boolean toTop) {
127 if (stack == mStack) {
128 return;
129 }
130 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
131 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700132 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800133 if (mStack != null) {
134 mStack.removeTask(this);
135 }
136 stack.addTask(this, toTop);
137 }
138
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700139 void positionTaskInStack(TaskStack stack, int position) {
140 if (mStack != null && stack != mStack) {
141 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
142 + " from stack=" + mStack);
143 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
144 mStack.removeTask(this);
145 }
146 stack.positionTask(this, position, showForAllUsers());
147 }
148
Craig Mautnerc00204b2013-03-05 15:02:14 -0800149 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800150 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800151 if (mAppTokens.size() == 0) {
Wale Ogunwale000957c2015-04-03 08:19:12 -0700152 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId,
Craig Mautner2c2549c2013-11-12 08:31:15 -0800153 "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800154 if (mDeferRemoval) {
155 removeLocked();
156 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800157 }
Craig Mautner83162a92015-01-26 14:43:30 -0800158 wtoken.mTask = null;
159 /* Leave mTaskId for now, it might be useful for debug
160 wtoken.mTaskId = -1;
161 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800162 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800163 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800164
Craig Mautnercbd84af2014-10-22 13:21:22 -0700165 void setSendingToBottom(boolean toBottom) {
166 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
167 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
168 }
169 }
170
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700171 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700172 int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700173 if (config == null) {
174 config = Configuration.EMPTY;
175 }
176 if (bounds == null && !Configuration.EMPTY.equals(config)) {
177 throw new IllegalArgumentException("null bounds but non empty configuration: "
178 + config);
179 }
180 if (bounds != null && Configuration.EMPTY.equals(config)) {
181 throw new IllegalArgumentException("non null bounds, but empty configuration");
182 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700183 boolean oldFullscreen = mFullscreen;
184 int rotation = Surface.ROTATION_0;
185 final DisplayContent displayContent = mStack.getDisplayContent();
186 if (displayContent != null) {
187 displayContent.getLogicalDisplayRect(mTmpRect);
188 rotation = displayContent.getDisplayInfo().rotation;
189 if (bounds == null) {
190 bounds = mTmpRect;
191 mFullscreen = true;
192 } else {
Wale Ogunwale99db1862015-10-23 20:08:22 -0700193 if ((mStack.mStackId != FREEFORM_WORKSPACE_STACK_ID
194 && mStack.mStackId != PINNED_STACK_ID) || bounds.isEmpty()) {
Stefan Kuhne234dbf82015-08-13 09:44:28 -0700195 // ensure bounds are entirely within the display rect
196 if (!bounds.intersect(mTmpRect)) {
197 // Can't set bounds outside the containing display...Sorry!
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700198 return BOUNDS_CHANGE_NONE;
Stefan Kuhne234dbf82015-08-13 09:44:28 -0700199 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700200 }
201 mFullscreen = mTmpRect.equals(bounds);
202 }
203 }
204
205 if (bounds == null) {
206 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700207 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700208 }
209 if (mBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700210 return BOUNDS_CHANGE_NONE;
211 }
212
213 int boundsChange = BOUNDS_CHANGE_NONE;
214 if (mBounds.left != bounds.left || mBounds.right != bounds.right) {
215 boundsChange |= BOUNDS_CHANGE_POSITION;
216 }
217 if (mBounds.width() != bounds.width() || mBounds.height() != bounds.height()) {
218 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700219 }
220
221 mBounds.set(bounds);
222 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700223 if (displayContent != null) {
224 displayContent.mDimBehindController.updateDimLayer(this);
225 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700226 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700227 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700228 }
229
Chong Zhang87b21722015-09-21 15:39:51 -0700230 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700231 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700232 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700233 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700234 }
235 if (boundsChanged == BOUNDS_CHANGE_NONE) {
236 return false;
237 }
238 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
239 resizeWindows();
240 }
241 return true;
242 }
243
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700244 /** Return true if the current bound can get outputted to the rest of the system as-is. */
245 private boolean useCurrentBounds() {
246 final DisplayContent displayContent = mStack.getDisplayContent();
247 if (mFullscreen
Wale Ogunwale99db1862015-10-23 20:08:22 -0700248 || mStack.allowTaskResize()
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700249 || displayContent == null
250 || displayContent.getDockedStackLocked() != null) {
251 return true;
252 }
253 return false;
254 }
255
256 /** Bounds of the task with other system factors taken into consideration. */
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700257 @Override
258 public void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700259 if (useCurrentBounds()) {
260 // No need to adjust the output bounds if fullscreen or the docked stack is visible
261 // since it is already what we want to represent to the rest of the system.
262 out.set(mBounds);
263 return;
264 }
265
266 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
267 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
268 // system.
269 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700270 }
271
Chong Zhang3005e752015-09-18 18:46:28 -0700272 void setDragResizing(boolean dragResizing) {
Chong Zhang3005e752015-09-18 18:46:28 -0700273 mDragResizing = dragResizing;
274 }
275
276 boolean isDragResizing() {
277 return mDragResizing;
278 }
279
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700280 void updateDisplayInfo(final DisplayContent displayContent) {
281 if (displayContent == null) {
282 return;
283 }
284 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700285 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700286 return;
287 }
288 final int newRotation = displayContent.getDisplayInfo().rotation;
289 if (mRotation == newRotation) {
290 return;
291 }
292
293 // Device rotation changed. We don't want the task to move around on the screen when
294 // this happens, so update the task bounds so it stays in the same place.
Wale Ogunwale94744212015-09-21 19:01:47 -0700295 mTmpRect2.set(mBounds);
296 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700297 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
298 // Post message to inform activity manager of the bounds change simulating
299 // a one-way call. We do this to prevent a deadlock between window manager
Filip Gruszczynski44bc4da2015-10-03 13:59:49 -0700300 // lock and activity manager lock been held. Only tasks within the freeform stack
301 // are resizeable independently of their stack resizing.
302 if (mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
303 mService.mH.sendMessage(mService.mH.obtainMessage(
304 RESIZE_TASK, mTaskId, RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mBounds));
305 }
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700306 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700307 }
308
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700309 void resizeWindows() {
310 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
311 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
312 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
313 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
314 final WindowState win = windows.get(winNdx);
315 if (!resizingWindows.contains(win)) {
316 if (DEBUG_RESIZE) Slog.d(TAG, "setBounds: Resizing " + win);
317 resizingWindows.add(win);
318 }
319 }
320 }
321 }
322
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700323 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700324 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700325 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700326 }
327
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700328 boolean inHomeStack() {
329 return mStack != null && mStack.mStackId == HOME_STACK_ID;
330 }
331
Chong Zhang09b21ef2015-09-14 10:20:21 -0700332 boolean inFreeformWorkspace() {
333 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
334 }
335
Filip Gruszczynski955b2fc2015-10-15 14:46:07 -0700336 boolean inDockedWorkspace() {
337 return mStack != null && mStack.mStackId == DOCKED_STACK_ID;
338 }
339
Chong Zhang9184ec62015-09-24 12:32:21 -0700340 WindowState getTopAppMainWindow() {
341 final int tokensCount = mAppTokens.size();
342 return tokensCount > 0 ? mAppTokens.get(tokensCount - 1).findMainWindow() : null;
343 }
344
Chong Zhangbef461f2015-10-27 11:38:24 -0700345 AppWindowToken getTopAppWindowToken() {
346 final int tokensCount = mAppTokens.size();
347 return tokensCount > 0 ? mAppTokens.get(tokensCount - 1) : null;
348 }
349
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800350 @Override
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700351 public boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700352 if (useCurrentBounds()) {
353 return mFullscreen;
354 }
355 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
356 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
357 // system.
358 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700359 }
360
361 @Override
362 public DisplayInfo getDisplayInfo() {
363 return mStack.getDisplayContent().getDisplayInfo();
364 }
365
366 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800367 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800368 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800369 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700370
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700371 @Override
372 public String toShortString() {
373 return "Task=" + mTaskId;
374 }
375
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700376 public void printTo(String prefix, PrintWriter pw) {
377 pw.print(prefix); pw.print("taskId="); pw.print(mTaskId);
378 pw.print(prefix); pw.print("appTokens="); pw.print(mAppTokens);
379 pw.print(prefix); pw.print("mdr="); pw.println(mDeferRemoval);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700380 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800381}