blob: 07a850bfe17c434eae46c4bfbafef3ce12aea1db [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
Craig Mautner42bf39e2014-02-21 16:46:22 -080019import static com.android.server.wm.WindowManagerService.TAG;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070020import static com.android.server.wm.WindowManagerService.DEBUG_RESIZE;
Craig Mautnere3119b72015-01-20 15:02:36 -080021import static com.android.server.wm.WindowManagerService.DEBUG_STACK;
Stefan Kuhne234dbf82015-08-13 09:44:28 -070022import static android.app.ActivityManager.FREEFORM_WORKSPACE_STACK_ID;
Craig Mautner42bf39e2014-02-21 16:46:22 -080023
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070024import android.content.res.Configuration;
25import android.graphics.Rect;
Craig Mautner2c2549c2013-11-12 08:31:15 -080026import android.util.EventLog;
Craig Mautner42bf39e2014-02-21 16:46:22 -080027import android.util.Slog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070028import android.util.SparseArray;
29import android.util.TypedValue;
30import android.view.DisplayInfo;
31import android.view.Surface;
32
Craig Mautnere3119b72015-01-20 15:02:36 -080033import com.android.server.EventLogTags;
Craig Mautner2c2549c2013-11-12 08:31:15 -080034
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070035import java.io.PrintWriter;
36import java.util.ArrayList;
37
38class Task implements DimLayer.DimLayerUser {
39 /** Amount of time in milliseconds to animate the dim surface from one value to another,
40 * when no window animation is driving it. */
41 private static final int DEFAULT_DIM_DURATION = 200;
42
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
75 // The particular window with FLAG_DIM_BEHIND set. If null, hide mDimLayer.
76 WindowStateAnimator mDimWinAnimator;
77 // Used to support {@link android.view.WindowManager.LayoutParams#FLAG_DIM_BEHIND}
78 private DimLayer mDimLayer;
79 // Set to false at the start of performLayoutAndPlaceSurfaces. If it is still false by the end
80 // then stop any dimming.
81 private boolean mContinueDimming;
82 // Shared dim layer for fullscreen tasks. {@link #mDimLayer} will point to this instead
83 // of creating a new object per fullscreen task on a display.
84 private static final SparseArray<DimLayer> sSharedFullscreenDimLayers = new SparseArray<>();
85
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070086 Task(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
87 Configuration config) {
Craig Mautner83162a92015-01-26 14:43:30 -080088 mTaskId = taskId;
Craig Mautnerc00204b2013-03-05 15:02:14 -080089 mStack = stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -070090 mUserId = userId;
Craig Mautnere3119b72015-01-20 15:02:36 -080091 mService = service;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070092 setBounds(bounds, config);
Craig Mautnerc00204b2013-03-05 15:02:14 -080093 }
94
95 DisplayContent getDisplayContent() {
96 return mStack.getDisplayContent();
97 }
98
99 void addAppToken(int addPos, AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800100 final int lastPos = mAppTokens.size();
Craig Mautner83162a92015-01-26 14:43:30 -0800101 if (addPos >= lastPos) {
102 addPos = lastPos;
103 } else {
104 for (int pos = 0; pos < lastPos && pos < addPos; ++pos) {
105 if (mAppTokens.get(pos).removed) {
106 // addPos assumes removed tokens are actually gone.
107 ++addPos;
108 }
Craig Mautner01f79cf2014-08-27 09:56:02 -0700109 }
Craig Mautner42bf39e2014-02-21 16:46:22 -0800110 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800111 mAppTokens.add(addPos, wtoken);
Craig Mautner83162a92015-01-26 14:43:30 -0800112 wtoken.mTask = this;
Craig Mautner42bf39e2014-02-21 16:46:22 -0800113 mDeferRemoval = false;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800114 }
115
Craig Mautnere3119b72015-01-20 15:02:36 -0800116 void removeLocked() {
117 if (!mAppTokens.isEmpty() && mStack.isAnimating()) {
Craig Mautner83162a92015-01-26 14:43:30 -0800118 if (DEBUG_STACK) Slog.i(TAG, "removeTask: deferring removing taskId=" + mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800119 mDeferRemoval = true;
120 return;
121 }
Craig Mautner83162a92015-01-26 14:43:30 -0800122 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing taskId=" + mTaskId);
123 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "removeTask");
Craig Mautnere3119b72015-01-20 15:02:36 -0800124 mDeferRemoval = false;
125 mStack.removeTask(this);
Craig Mautner83162a92015-01-26 14:43:30 -0800126 mService.mTaskIdToTask.delete(mTaskId);
Craig Mautnere3119b72015-01-20 15:02:36 -0800127 }
128
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800129 void moveTaskToStack(TaskStack stack, boolean toTop) {
130 if (stack == mStack) {
131 return;
132 }
133 if (DEBUG_STACK) Slog.i(TAG, "moveTaskToStack: removing taskId=" + mTaskId
134 + " from stack=" + mStack);
Wale Ogunwale000957c2015-04-03 08:19:12 -0700135 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800136 if (mStack != null) {
137 mStack.removeTask(this);
138 }
139 stack.addTask(this, toTop);
140 }
141
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700142 void positionTaskInStack(TaskStack stack, int position) {
143 if (mStack != null && stack != mStack) {
144 if (DEBUG_STACK) Slog.i(TAG, "positionTaskInStack: removing taskId=" + mTaskId
145 + " from stack=" + mStack);
146 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId, "moveTask");
147 mStack.removeTask(this);
148 }
149 stack.positionTask(this, position, showForAllUsers());
150 }
151
Craig Mautnerc00204b2013-03-05 15:02:14 -0800152 boolean removeAppToken(AppWindowToken wtoken) {
Craig Mautner42bf39e2014-02-21 16:46:22 -0800153 boolean removed = mAppTokens.remove(wtoken);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800154 if (mAppTokens.size() == 0) {
Wale Ogunwale000957c2015-04-03 08:19:12 -0700155 EventLog.writeEvent(EventLogTags.WM_TASK_REMOVED, mTaskId,
Craig Mautner2c2549c2013-11-12 08:31:15 -0800156 "removeAppToken: last token");
Craig Mautnere3119b72015-01-20 15:02:36 -0800157 if (mDeferRemoval) {
158 removeLocked();
159 }
Craig Mautnerc00204b2013-03-05 15:02:14 -0800160 }
Craig Mautner83162a92015-01-26 14:43:30 -0800161 wtoken.mTask = null;
162 /* Leave mTaskId for now, it might be useful for debug
163 wtoken.mTaskId = -1;
164 */
Craig Mautner42bf39e2014-02-21 16:46:22 -0800165 return removed;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800166 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800167
Craig Mautnercbd84af2014-10-22 13:21:22 -0700168 void setSendingToBottom(boolean toBottom) {
169 for (int appTokenNdx = 0; appTokenNdx < mAppTokens.size(); appTokenNdx++) {
170 mAppTokens.get(appTokenNdx).sendingToBottom = toBottom;
171 }
172 }
173
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700174 /** Set the task bounds. Passing in null sets the bounds to fullscreen. */
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700175 int setBounds(Rect bounds, Configuration config) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700176 if (config == null) {
177 config = Configuration.EMPTY;
178 }
179 if (bounds == null && !Configuration.EMPTY.equals(config)) {
180 throw new IllegalArgumentException("null bounds but non empty configuration: "
181 + config);
182 }
183 if (bounds != null && Configuration.EMPTY.equals(config)) {
184 throw new IllegalArgumentException("non null bounds, but empty configuration");
185 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700186 boolean oldFullscreen = mFullscreen;
187 int rotation = Surface.ROTATION_0;
188 final DisplayContent displayContent = mStack.getDisplayContent();
189 if (displayContent != null) {
190 displayContent.getLogicalDisplayRect(mTmpRect);
191 rotation = displayContent.getDisplayInfo().rotation;
192 if (bounds == null) {
193 bounds = mTmpRect;
194 mFullscreen = true;
195 } else {
Stefan Kuhne234dbf82015-08-13 09:44:28 -0700196 if (mStack.mStackId != FREEFORM_WORKSPACE_STACK_ID || bounds.isEmpty()) {
197 // ensure bounds are entirely within the display rect
198 if (!bounds.intersect(mTmpRect)) {
199 // Can't set bounds outside the containing display...Sorry!
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700200 return BOUNDS_CHANGE_NONE;
Stefan Kuhne234dbf82015-08-13 09:44:28 -0700201 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700202 }
203 mFullscreen = mTmpRect.equals(bounds);
204 }
205 }
206
207 if (bounds == null) {
208 // Can't set to fullscreen if we don't have a display to get bounds from...
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700209 return BOUNDS_CHANGE_NONE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700210 }
211 if (mBounds.equals(bounds) && oldFullscreen == mFullscreen && mRotation == rotation) {
Wale Ogunwale2cc92f52015-09-09 13:12:10 -0700212 return BOUNDS_CHANGE_NONE;
213 }
214
215 int boundsChange = BOUNDS_CHANGE_NONE;
216 if (mBounds.left != bounds.left || mBounds.right != bounds.right) {
217 boundsChange |= BOUNDS_CHANGE_POSITION;
218 }
219 if (mBounds.width() != bounds.width() || mBounds.height() != bounds.height()) {
220 boundsChange |= BOUNDS_CHANGE_SIZE;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700221 }
222
223 mBounds.set(bounds);
224 mRotation = rotation;
225 updateDimLayer();
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
230 void getBounds(Rect out) {
231 out.set(mBounds);
232 }
233
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700234 void updateDisplayInfo(final DisplayContent displayContent) {
235 if (displayContent == null) {
236 return;
237 }
238 if (mFullscreen) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700239 setBounds(null, Configuration.EMPTY);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700240 return;
241 }
242 final int newRotation = displayContent.getDisplayInfo().rotation;
243 if (mRotation == newRotation) {
244 return;
245 }
246
247 // Device rotation changed. We don't want the task to move around on the screen when
248 // this happens, so update the task bounds so it stays in the same place.
249 final int rotationDelta = DisplayContent.deltaRotation(mRotation, newRotation);
250 displayContent.getLogicalDisplayRect(mTmpRect);
251 switch (rotationDelta) {
252 case Surface.ROTATION_0:
253 mTmpRect2.set(mBounds);
254 break;
255 case Surface.ROTATION_90:
256 mTmpRect2.top = mTmpRect.bottom - mBounds.right;
257 mTmpRect2.left = mBounds.top;
258 mTmpRect2.right = mTmpRect2.left + mBounds.height();
259 mTmpRect2.bottom = mTmpRect2.top + mBounds.width();
260 break;
261 case Surface.ROTATION_180:
262 mTmpRect2.top = mTmpRect.bottom - mBounds.bottom;
263 mTmpRect2.left = mTmpRect.right - mBounds.right;
264 mTmpRect2.right = mTmpRect2.left + mBounds.width();
265 mTmpRect2.bottom = mTmpRect2.top + mBounds.height();
266 break;
267 case Surface.ROTATION_270:
268 mTmpRect2.top = mBounds.left;
269 mTmpRect2.left = mTmpRect.right - mBounds.bottom;
270 mTmpRect2.right = mTmpRect2.left + mBounds.height();
271 mTmpRect2.bottom = mTmpRect2.top + mBounds.width();
272 break;
273 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700274 setBounds(mTmpRect2, mOverrideConfig);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700275 }
276
277 /** Updates the dim layer bounds, recreating it if needed. */
278 private void updateDimLayer() {
279 DimLayer newDimLayer;
280 final boolean previousFullscreen =
281 mDimLayer != null && sSharedFullscreenDimLayers.indexOfValue(mDimLayer) > -1;
282 final int displayId = mStack.getDisplayContent().getDisplayId();
283 if (mFullscreen) {
284 if (previousFullscreen) {
285 // Nothing to do here...
286 return;
287 }
288 // Use shared fullscreen dim layer
289 newDimLayer = sSharedFullscreenDimLayers.get(displayId);
290 if (newDimLayer == null) {
291 if (mDimLayer != null) {
292 // Re-purpose the previous dim layer.
293 newDimLayer = mDimLayer;
294 } else {
295 // Create new full screen dim layer.
296 newDimLayer = new DimLayer(mService, this, displayId);
297 }
298 newDimLayer.setBounds(mBounds);
299 sSharedFullscreenDimLayers.put(displayId, newDimLayer);
300 } else if (mDimLayer != null) {
301 mDimLayer.destroySurface();
302 }
303 } else {
304 newDimLayer = (mDimLayer == null || previousFullscreen)
305 ? new DimLayer(mService, this, displayId) : mDimLayer;
306 newDimLayer.setBounds(mBounds);
307 }
308 mDimLayer = newDimLayer;
309 }
310
311 boolean animateDimLayers() {
312 final int dimLayer;
313 final float dimAmount;
314 if (mDimWinAnimator == null) {
315 dimLayer = mDimLayer.getLayer();
316 dimAmount = 0;
317 } else {
318 dimLayer = mDimWinAnimator.mAnimLayer - WindowManagerService.LAYER_OFFSET_DIM;
319 dimAmount = mDimWinAnimator.mWin.mAttrs.dimAmount;
320 }
321 final float targetAlpha = mDimLayer.getTargetAlpha();
322 if (targetAlpha != dimAmount) {
323 if (mDimWinAnimator == null) {
324 mDimLayer.hide(DEFAULT_DIM_DURATION);
325 } else {
326 long duration = (mDimWinAnimator.mAnimating && mDimWinAnimator.mAnimation != null)
327 ? mDimWinAnimator.mAnimation.computeDurationHint()
328 : DEFAULT_DIM_DURATION;
329 if (targetAlpha > dimAmount) {
330 duration = getDimBehindFadeDuration(duration);
331 }
332 mDimLayer.show(dimLayer, dimAmount, duration);
333 }
334 } else if (mDimLayer.getLayer() != dimLayer) {
335 mDimLayer.setLayer(dimLayer);
336 }
337 if (mDimLayer.isAnimating()) {
338 if (!mService.okToDisplay()) {
339 // Jump to the end of the animation.
340 mDimLayer.show();
341 } else {
342 return mDimLayer.stepAnimation();
343 }
344 }
345 return false;
346 }
347
348 private long getDimBehindFadeDuration(long duration) {
349 TypedValue tv = new TypedValue();
350 mService.mContext.getResources().getValue(
351 com.android.internal.R.fraction.config_dimBehindFadeDuration, tv, true);
352 if (tv.type == TypedValue.TYPE_FRACTION) {
353 duration = (long)tv.getFraction(duration, duration);
354 } else if (tv.type >= TypedValue.TYPE_FIRST_INT && tv.type <= TypedValue.TYPE_LAST_INT) {
355 duration = tv.data;
356 }
357 return duration;
358 }
359
360 void clearContinueDimming() {
361 mContinueDimming = false;
362 }
363
364 void setContinueDimming() {
365 mContinueDimming = true;
366 }
367
368 boolean getContinueDimming() {
369 return mContinueDimming;
370 }
371
372 boolean isDimming() {
373 return mDimLayer.isDimming();
374 }
375
376 boolean isDimming(WindowStateAnimator winAnimator) {
377 return mDimWinAnimator == winAnimator && isDimming();
378 }
379
380 void startDimmingIfNeeded(WindowStateAnimator newWinAnimator) {
381 // Only set dim params on the highest dimmed layer.
382 // Don't turn on for an unshown surface, or for any layer but the highest dimmed layer.
383 if (newWinAnimator.mSurfaceShown && (mDimWinAnimator == null
384 || !mDimWinAnimator.mSurfaceShown
385 || mDimWinAnimator.mAnimLayer < newWinAnimator.mAnimLayer)) {
386 mDimWinAnimator = newWinAnimator;
387 if (mDimWinAnimator.mWin.mAppToken == null
388 && !mFullscreen && mStack.getDisplayContent() != null) {
389 // Dim should cover the entire screen for system windows.
390 mStack.getDisplayContent().getLogicalDisplayRect(mTmpRect);
391 mDimLayer.setBounds(mTmpRect);
392 }
393 }
394 }
395
396 void stopDimmingIfNeeded() {
397 if (!mContinueDimming && isDimming()) {
398 mDimWinAnimator = null;
399 mDimLayer.setBounds(mBounds);
400 }
401 }
402
403 void close() {
404 if (mDimLayer != null) {
405 mDimLayer.destroySurface();
406 mDimLayer = null;
407 }
408 }
409
410 void resizeWindows() {
411 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
412 for (int activityNdx = mAppTokens.size() - 1; activityNdx >= 0; --activityNdx) {
413 final ArrayList<WindowState> windows = mAppTokens.get(activityNdx).allAppWindows;
414 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
415 final WindowState win = windows.get(winNdx);
416 if (!resizingWindows.contains(win)) {
417 if (DEBUG_RESIZE) Slog.d(TAG, "setBounds: Resizing " + win);
418 resizingWindows.add(win);
419 }
420 }
421 }
422 }
423
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700424 boolean showForAllUsers() {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700425 final int tokensCount = mAppTokens.size();
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700426 return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700427 }
428
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800429 @Override
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700430 public boolean isFullscreen() {
431 return mFullscreen;
432 }
433
434 @Override
435 public DisplayInfo getDisplayInfo() {
436 return mStack.getDisplayContent().getDisplayInfo();
437 }
438
439 @Override
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800440 public String toString() {
Craig Mautner83162a92015-01-26 14:43:30 -0800441 return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800442 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700443
444 public void printTo(String prefix, PrintWriter pw) {
445 pw.print(prefix); pw.print("taskId="); pw.print(mTaskId);
446 pw.print(prefix); pw.print("appTokens="); pw.print(mAppTokens);
447 pw.print(prefix); pw.print("mdr="); pw.println(mDeferRemoval);
448 if (mDimLayer.isDimming()) {
449 pw.print(prefix); pw.println("mDimLayer:");
450 mDimLayer.printTo(prefix + " ", pw);
451 pw.print(prefix); pw.print("mDimWinAnimator="); pw.println(mDimWinAnimator);
452 } else {
453 pw.println();
454 }
455 }
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800456}