blob: af33880e91b1a25939d86dba8dc6969773b08a4c [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 Ogunwale1ed0d892015-09-28 13:27:44 -070020import static android.app.ActivityManager.RESIZE_MODE_SYSTEM_SCREEN_ROTATION;
Craig Mautner42bf39e2014-02-21 16:46:22 -080021import static com.android.server.wm.WindowManagerService.TAG;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070022import static com.android.server.wm.WindowManagerService.DEBUG_RESIZE;
Craig Mautnere3119b72015-01-20 15:02:36 -080023import static com.android.server.wm.WindowManagerService.DEBUG_STACK;
Wale Ogunwale1ed0d892015-09-28 13:27:44 -070024import static com.android.server.wm.WindowManagerService.H.RESIZE_TASK;
Stefan Kuhne234dbf82015-08-13 09:44:28 -070025import static android.app.ActivityManager.FREEFORM_WORKSPACE_STACK_ID;
Craig Mautner42bf39e2014-02-21 16:46:22 -080026
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070027import android.content.res.Configuration;
28import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080029import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080030import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070031import android.util.SparseArray;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070032import android.view.DisplayInfo;
33import android.view.Surface;
34
Craig Mautnere3119b72015-01-20 15:02:36 -080035import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080036
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070037import java.io.PrintWriter;
38import java.util.ArrayList;
39
40class Task implements DimLayer.DimLayerUser {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -070041 // Return value from {@link setBounds} indicating no change was made to the Task bounds.
42 static final int BOUNDS_CHANGE_NONE = 0;
43 // Return value from {@link setBounds} indicating the position of the Task bounds changed.
44 static final int BOUNDS_CHANGE_POSITION = 1;
45 // Return value from {@link setBounds} indicating the size of the Task bounds changed.
46 static final int BOUNDS_CHANGE_SIZE = 1 << 1;
47
Craig Mautnerc00204b2013-03-05 15:02:14 -080048 TaskStack mStack;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080049 final AppTokenList mAppTokens = new AppTokenList();
Craig Mautner83162a92015-01-26 14:43:30 -080050 final int mTaskId;
Craig Mautnerac6f8432013-07-17 13:24:59 -070051 final int mUserId;
Craig Mautner9ef471f2014-02-07 13:11:47 -080052 boolean mDeferRemoval = false;
Craig Mautnere3119b72015-01-20 15:02:36 -080053 final WindowManagerService mService;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080054
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070055 // Content limits relative to the DisplayContent this sits in.
56 private Rect mBounds = new Rect();
57
58 // Device rotation as of the last time {@link #mBounds} was set.
59 int mRotation;
60
61 // Whether mBounds is fullscreen
62 private boolean mFullscreen = true;
63
64 // Contains configurations settings that are different from the global configuration due to
65 // stack specific operations. E.g. {@link #setBounds}.
66 Configuration mOverrideConfig;
67
68 // For comparison with DisplayContent bounds.
69 private Rect mTmpRect = new Rect();
70 // For handling display rotations.
71 private Rect mTmpRect2 = new Rect();
72
Chong Zhang3005e752015-09-18 18:46:28 -070073 // Whether the task is currently being drag-resized
74 private boolean mDragResizing;
75
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070076 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
77 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -080078 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -080079 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -070080 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -080081 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070082 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -080083 }
84
85 DisplayContent getDisplayContent() {
86 return mStack.getDisplayContent();
87 }
88
89 void addAppToken(int addPos, AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -080090 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -080091 if (addPos >= lastPos) {
92 addPos = lastPos;
93 } else {
94 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
95 if (mAppTokens.get(pos).removed) {
96 // addPos assumes removed tokens are actually gone.
97 ++addPos;
98 }
Craig Mautner01f79cf2014-08-27 09:56:02 -070099 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800100 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800101 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800102 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800103 mDeferRemoval = false;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800104 }
105
Craig Mautnere3119b72015-01-20 15:02:36 -0800106 void removeLocked() {
107 if (!mAppTokens.isEmpty() && mStack.isAnimating()) {
Craig Mautner83162a92015-01-26 14:43:30 -0800108 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800109 mDeferRemoval = true;
110 return;
111 }
Craig Mautner83162a92015-01-26 14:43:30 -0800112 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
113 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800114 mDeferRemoval = false;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700115 DisplayContent content = getDisplayContent();
116 if (content != null) {
117 content.mDimBehindController.removeDimLayerUser(this);
118 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800119 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800120 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800121 }
122
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800123 void moveTaskToStack(TaskStack stack, boolean toTop) {
124 if (stack == mStack) {
125 return;
126 }
127 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
128 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700129 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800130 if (mStack != null) {
131 mStack.removeTask(this);
132 }
133 stack.addTask(this, toTop);
134 }
135
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700136 void positionTaskInStack(TaskStack stack, int position) {
137 if (mStack != null && stack != mStack) {
138 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
139 + " from stack=" + mStack);
140 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
141 mStack.removeTask(this);
142 }
143 stack.positionTask(this, position, showForAllUsers());
144 }
145
Craig Mautnerc00204b2013-03-05 15:02:14 -0800146 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800147 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800148 if (mAppTokens.size() == 0) {
Wale Ogunwale000957c2015-04-03 08:19:12 -0700149 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId,
Craig Mautner2c2549c2013-11-12 08:31:15 -0800150 "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800151 if (mDeferRemoval) {
152 removeLocked();
153 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800154 }
Craig Mautner83162a92015-01-26 14:43:30 -0800155 wtoken.mTask = null;
156 /* Leave mTaskId for now, it might be useful for debug
157 wtoken.mTaskId = -1;
158 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800159 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800160 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800161
Craig Mautnercbd84af2014-10-22 13:21:22 -0700162 void setSendingToBottom(boolean toBottom) {
163 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
164 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
165 }
166 }
167
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700168 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700169 int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700170 if (config == null) {
171 config = Configuration.EMPTY;
172 }
173 if (bounds == null && !Configuration.EMPTY.equals(config)) {
174 throw new IllegalArgumentException("null bounds but non empty configuration: "
175 + config);
176 }
177 if (bounds != null && Configuration.EMPTY.equals(config)) {
178 throw new IllegalArgumentException("non null bounds, but empty configuration");
179 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700180 boolean oldFullscreen = mFullscreen;
181 int rotation = Surface.ROTATION_0;
182 final DisplayContent displayContent = mStack.getDisplayContent();
183 if (displayContent != null) {
184 displayContent.getLogicalDisplayRect(mTmpRect);
185 rotation = displayContent.getDisplayInfo().rotation;
186 if (bounds == null) {
187 bounds = mTmpRect;
188 mFullscreen = true;
189 } else {
Stefan Kuhne234dbf82015-08-13 09:44:28 -0700190 if (mStack.mStackId != FREEFORM_WORKSPACE_STACK_ID || bounds.isEmpty()) {
191 // ensure bounds are entirely within the display rect
192 if (!bounds.intersect(mTmpRect)) {
193 // Can't set bounds outside the containing display...Sorry!
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700194 return BOUNDS_CHANGE_NONE;
Stefan Kuhne234dbf82015-08-13 09:44:28 -0700195 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700196 }
197 mFullscreen = mTmpRect.equals(bounds);
198 }
199 }
200
201 if (bounds == null) {
202 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700203 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700204 }
205 if (mBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700206 return BOUNDS_CHANGE_NONE;
207 }
208
209 int boundsChange = BOUNDS_CHANGE_NONE;
210 if (mBounds.left != bounds.left || mBounds.right != bounds.right) {
211 boundsChange |= BOUNDS_CHANGE_POSITION;
212 }
213 if (mBounds.width() != bounds.width() || mBounds.height() != bounds.height()) {
214 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700215 }
216
217 mBounds.set(bounds);
218 mRotation = rotation;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700219 if (displayContent != null) {
220 displayContent.mDimBehindController.updateDimLayer(this);
221 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700222 mOverrideConfig = mFullscreen ? Configuration.EMPTY : config;
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700223 return boundsChange;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700224 }
225
Chong Zhang87b21722015-09-21 15:39:51 -0700226 boolean resizeLocked(Rect bounds, Configuration configuration, boolean forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700227 int boundsChanged = setBounds(bounds, configuration);
Chong Zhang87b21722015-09-21 15:39:51 -0700228 if (forced) {
Chong Zhang3005e752015-09-18 18:46:28 -0700229 boundsChanged |= BOUNDS_CHANGE_SIZE;
Chong Zhang3005e752015-09-18 18:46:28 -0700230 }
231 if (boundsChanged == BOUNDS_CHANGE_NONE) {
232 return false;
233 }
234 if ((boundsChanged & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) {
235 resizeWindows();
236 }
237 return true;
238 }
239
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700240 /** Return true if the current bound can get outputted to the rest of the system as-is. */
241 private boolean useCurrentBounds() {
242 final DisplayContent displayContent = mStack.getDisplayContent();
243 if (mFullscreen
244 || mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID
245 || mStack.mStackId == DOCKED_STACK_ID
246 || displayContent == null
247 || displayContent.getDockedStackLocked() != null) {
248 return true;
249 }
250 return false;
251 }
252
253 /** Bounds of the task with other system factors taken into consideration. */
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700254 @Override
255 public void getBounds(Rect out) {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700256 if (useCurrentBounds()) {
257 // No need to adjust the output bounds if fullscreen or the docked stack is visible
258 // since it is already what we want to represent to the rest of the system.
259 out.set(mBounds);
260 return;
261 }
262
263 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
264 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
265 // system.
266 mStack.getDisplayContent().getLogicalDisplayRect(out);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700267 }
268
Chong Zhang3005e752015-09-18 18:46:28 -0700269 void setDragResizing(boolean dragResizing) {
Chong Zhang3005e752015-09-18 18:46:28 -0700270 mDragResizing = dragResizing;
271 }
272
273 boolean isDragResizing() {
274 return mDragResizing;
275 }
276
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700277 void updateDisplayInfo(final DisplayContent displayContent) {
278 if (displayContent == null) {
279 return;
280 }
281 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700282 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700283 return;
284 }
285 final int newRotation = displayContent.getDisplayInfo().rotation;
286 if (mRotation == newRotation) {
287 return;
288 }
289
290 // Device rotation changed. We don't want the task to move around on the screen when
291 // this happens, so update the task bounds so it stays in the same place.
Wale Ogunwale94744212015-09-21 19:01:47 -0700292 mTmpRect2.set(mBounds);
293 displayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700294 if (setBounds(mTmpRect2, mOverrideConfig) != BOUNDS_CHANGE_NONE) {
295 // Post message to inform activity manager of the bounds change simulating
296 // a one-way call. We do this to prevent a deadlock between window manager
297 // lock and activity manager lock been held.
298 mService.mH.sendMessage(mService.mH.obtainMessage(
299 RESIZE_TASK, mTaskId, RESIZE_MODE_SYSTEM_SCREEN_ROTATION, mBounds));
300 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700301 }
302
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700303 void resizeWindows() {
304 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
305 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
306 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
307 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
308 final WindowState win = windows.get(winNdx);
309 if (!resizingWindows.contains(win)) {
310 if (DEBUG_RESIZE) Slog.d(TAG, "setBounds: Resizing " + win);
311 resizingWindows.add(win);
312 }
313 }
314 }
315 }
316
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700317 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700318 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700319 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700320 }
321
Chong Zhang09b21ef2015-09-14 10:20:21 -0700322 boolean inFreeformWorkspace() {
323 return mStack != null && mStack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
324 }
325
Chong Zhang9184ec62015-09-24 12:32:21 -0700326 WindowState getTopAppMainWindow() {
327 final int tokensCount = mAppTokens.size();
328 return tokensCount > 0 ? mAppTokens.get(tokensCount - 1).findMainWindow() : null;
329 }
330
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800331 @Override
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700332 public boolean isFullscreen() {
Wale Ogunwalef175e8a2015-09-29 11:07:06 -0700333 if (useCurrentBounds()) {
334 return mFullscreen;
335 }
336 // The bounds has been adjusted to accommodate for a docked stack, but the docked stack
337 // is not currently visible. Go ahead a represent it as fullscreen to the rest of the
338 // system.
339 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700340 }
341
342 @Override
343 public DisplayInfo getDisplayInfo() {
344 return mStack.getDisplayContent().getDisplayInfo();
345 }
346
347 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800348 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800349 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800350 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700351
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700352 @Override
353 public String toShortString() {
354 return "Task=" + mTaskId;
355 }
356
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700357 public void printTo(String prefix, PrintWriter pw) {
358 pw.print(prefix); pw.print("taskId="); pw.print(mTaskId);
359 pw.print(prefix); pw.print("appTokens="); pw.print(mAppTokens);
360 pw.print(prefix); pw.print("mdr="); pw.println(mDeferRemoval);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700361 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800362}