TaskStack.java revision 04a0ea60ac7e20369e63edc4f3f8cedf8425a439
1/* 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 19import static com.android.server.wm.WindowManagerService.DEBUG_TASK_MOVEMENT; 20import static com.android.server.wm.WindowManagerService.TAG; 21 22import android.graphics.Rect; 23import android.os.Debug; 24import android.util.EventLog; 25import android.util.Slog; 26import android.util.TypedValue; 27import com.android.server.EventLogTags; 28 29import java.io.PrintWriter; 30import java.util.ArrayList; 31 32public class TaskStack { 33 /** Amount of time in milliseconds to animate the dim surface from one value to another, 34 * when no window animation is driving it. */ 35 private static final int DEFAULT_DIM_DURATION = 200; 36 37 /** Unique identifier */ 38 final int mStackId; 39 40 /** The service */ 41 private final WindowManagerService mService; 42 43 /** The display this stack sits under. */ 44 private final DisplayContent mDisplayContent; 45 46 /** The Tasks that define this stack. Oldest Tasks are at the bottom. The ordering must match 47 * mTaskHistory in the ActivityStack with the same mStackId */ 48 private final ArrayList<Task> mTasks = new ArrayList<Task>(); 49 50 /** Content limits relative to the DisplayContent this sits in. Empty indicates fullscreen, 51 * Nonempty is size of this TaskStack but is also used to scale if DisplayContent changes. */ 52 Rect mBounds = new Rect(); 53 54 /** Used to support {@link android.view.WindowManager.LayoutParams#FLAG_DIM_BEHIND} */ 55 final DimLayer mDimLayer; 56 57 /** The particular window with FLAG_DIM_BEHIND set. If null, hide mDimLayer. */ 58 WindowStateAnimator mDimWinAnimator; 59 60 /** Support for non-zero {@link android.view.animation.Animation#getBackgroundColor()} */ 61 final DimLayer mAnimationBackgroundSurface; 62 63 /** The particular window with an Animation with non-zero background color. */ 64 WindowStateAnimator mAnimationBackgroundAnimator; 65 66 /** Set to false at the start of performLayoutAndPlaceSurfaces. If it is still false by the end 67 * then stop any dimming. */ 68 boolean mDimmingTag; 69 70 TaskStack(WindowManagerService service, int stackId, DisplayContent displayContent) { 71 mService = service; 72 mStackId = stackId; 73 mDisplayContent = displayContent; 74 mDimLayer = new DimLayer(service, this); 75 mAnimationBackgroundSurface = new DimLayer(service, this); 76 // TODO: remove bounds from log, they are always 0. 77 EventLog.writeEvent(EventLogTags.WM_STACK_CREATED, stackId, mBounds.left, mBounds.top, 78 mBounds.right, mBounds.bottom); 79 } 80 81 DisplayContent getDisplayContent() { 82 return mDisplayContent; 83 } 84 85 ArrayList<Task> getTasks() { 86 return mTasks; 87 } 88 89 private void resizeWindows() { 90 final boolean underStatusBar = mBounds.top == 0; 91 92 final ArrayList<WindowState> resizingWindows = mService.mResizingWindows; 93 for (int taskNdx = mTasks.size() - 1; taskNdx >= 0; --taskNdx) { 94 final ArrayList<AppWindowToken> activities = mTasks.get(taskNdx).mAppTokens; 95 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { 96 final ArrayList<WindowState> windows = activities.get(activityNdx).allAppWindows; 97 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) { 98 final WindowState win = windows.get(winNdx); 99 if (!resizingWindows.contains(win)) { 100 if (WindowManagerService.DEBUG_RESIZE) Slog.d(TAG, 101 "setBounds: Resizing " + win); 102 resizingWindows.add(win); 103 } 104 win.mUnderStatusBar = underStatusBar; 105 } 106 } 107 } 108 } 109 110 boolean setBounds(Rect bounds) { 111 if (mBounds.equals(bounds)) { 112 return false; 113 } 114 115 mDimLayer.setBounds(bounds); 116 mAnimationBackgroundSurface.setBounds(bounds); 117 mBounds.set(bounds); 118 119 resizeWindows(); 120 return true; 121 } 122 123 void getBounds(Rect out) { 124 if (mBounds.isEmpty()) { 125 mDisplayContent.getLogicalDisplayRect(out); 126 } else { 127 out.set(mBounds); 128 } 129 out.intersect(mDisplayContent.mContentRect); 130 } 131 132 boolean isFullscreen() { 133 return mBounds.isEmpty(); 134 } 135 136 void resizeBounds(float oldWidth, float oldHeight, float newWidth, float newHeight) { 137 if (oldWidth == newWidth && oldHeight == newHeight) { 138 return; 139 } 140 float widthScale = newWidth / oldWidth; 141 float heightScale = newHeight / oldHeight; 142 mBounds.left = (int)(mBounds.left * widthScale + 0.5); 143 mBounds.top = (int)(mBounds.top * heightScale + 0.5); 144 mBounds.right = (int)(mBounds.right * widthScale + 0.5); 145 mBounds.bottom = (int)(mBounds.bottom * heightScale + 0.5); 146 resizeWindows(); 147 } 148 149 /** 150 * Put a Task in this stack. Used for adding and moving. 151 * @param task The task to add. 152 * @param toTop Whether to add it to the top or bottom. 153 */ 154 void addTask(Task task, boolean toTop) { 155 int stackNdx; 156 if (!toTop) { 157 stackNdx = 0; 158 } else { 159 stackNdx = mTasks.size(); 160 final int currentUserId = mService.mCurrentUserId; 161 if (task.mUserId != currentUserId) { 162 // Place the task below all current user tasks. 163 while (--stackNdx >= 0) { 164 if (currentUserId != mTasks.get(stackNdx).mUserId) { 165 break; 166 } 167 } 168 ++stackNdx; 169 } 170 } 171 if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "addTask: task=" + task + " toTop=" + toTop 172 + " pos=" + stackNdx); 173 mTasks.add(stackNdx, task); 174 175 task.mStack = this; 176 mDisplayContent.addTask(task, toTop); 177 mDisplayContent.moveStack(this, true); 178 } 179 180 void moveTaskToTop(Task task) { 181 if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "moveTaskToTop: task=" + task + " Callers=" 182 + Debug.getCallers(6)); 183 mTasks.remove(task); 184 addTask(task, true); 185 } 186 187 void moveTaskToBottom(Task task) { 188 if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "moveTaskToBottom: task=" + task); 189 mTasks.remove(task); 190 addTask(task, false); 191 } 192 193 /** 194 * Delete a Task from this stack. If it is the last Task in the stack, move this stack to the 195 * back. 196 * @param task The Task to delete. 197 */ 198 void removeTask(Task task) { 199 if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "removeTask: task=" + task); 200 mTasks.remove(task); 201 mDisplayContent.removeTask(task); 202 if (mTasks.isEmpty()) { 203 mDisplayContent.moveStack(this, false); 204 } 205 mDisplayContent.layoutNeeded = true; 206 } 207 208 int remove() { 209 mAnimationBackgroundSurface.destroySurface(); 210 mDimLayer.destroySurface(); 211 EventLog.writeEvent(EventLogTags.WM_STACK_REMOVED, mStackId); 212 TaskStack next = mDisplayContent.removeStack(this); 213 if (next != null) { 214 return next.mStackId; 215 } 216 return -1; 217 } 218 219 void resetAnimationBackgroundAnimator() { 220 mAnimationBackgroundAnimator = null; 221 mAnimationBackgroundSurface.hide(); 222 } 223 224 private long getDimBehindFadeDuration(long duration) { 225 TypedValue tv = new TypedValue(); 226 mService.mContext.getResources().getValue( 227 com.android.internal.R.fraction.config_dimBehindFadeDuration, tv, true); 228 if (tv.type == TypedValue.TYPE_FRACTION) { 229 duration = (long)tv.getFraction(duration, duration); 230 } else if (tv.type >= TypedValue.TYPE_FIRST_INT && tv.type <= TypedValue.TYPE_LAST_INT) { 231 duration = tv.data; 232 } 233 return duration; 234 } 235 236 boolean animateDimLayers() { 237 final int dimLayer; 238 final float dimAmount; 239 if (mDimWinAnimator == null) { 240 dimLayer = mDimLayer.getLayer(); 241 dimAmount = 0; 242 } else { 243 dimLayer = mDimWinAnimator.mAnimLayer - WindowManagerService.LAYER_OFFSET_DIM; 244 dimAmount = mDimWinAnimator.mWin.mAttrs.dimAmount; 245 } 246 final float targetAlpha = mDimLayer.getTargetAlpha(); 247 if (targetAlpha != dimAmount) { 248 if (mDimWinAnimator == null) { 249 mDimLayer.hide(DEFAULT_DIM_DURATION); 250 } else { 251 long duration = (mDimWinAnimator.mAnimating && mDimWinAnimator.mAnimation != null) 252 ? mDimWinAnimator.mAnimation.computeDurationHint() 253 : DEFAULT_DIM_DURATION; 254 if (targetAlpha > dimAmount) { 255 duration = getDimBehindFadeDuration(duration); 256 } 257 mDimLayer.show(dimLayer, dimAmount, duration); 258 } 259 } else if (mDimLayer.getLayer() != dimLayer) { 260 mDimLayer.setLayer(dimLayer); 261 } 262 if (mDimLayer.isAnimating()) { 263 if (!mService.okToDisplay()) { 264 // Jump to the end of the animation. 265 mDimLayer.show(); 266 } else { 267 return mDimLayer.stepAnimation(); 268 } 269 } 270 return false; 271 } 272 273 void resetDimmingTag() { 274 mDimmingTag = false; 275 } 276 277 void setDimmingTag() { 278 mDimmingTag = true; 279 } 280 281 boolean testDimmingTag() { 282 return mDimmingTag; 283 } 284 285 boolean isDimming() { 286 return mDimLayer.isDimming(); 287 } 288 289 boolean isDimming(WindowStateAnimator winAnimator) { 290 return mDimWinAnimator == winAnimator && mDimLayer.isDimming(); 291 } 292 293 void startDimmingIfNeeded(WindowStateAnimator newWinAnimator) { 294 // Only set dim params on the highest dimmed layer. 295 final WindowStateAnimator existingDimWinAnimator = mDimWinAnimator; 296 // Don't turn on for an unshown surface, or for any layer but the highest dimmed layer. 297 if (newWinAnimator.mSurfaceShown && (existingDimWinAnimator == null 298 || !existingDimWinAnimator.mSurfaceShown 299 || existingDimWinAnimator.mAnimLayer < newWinAnimator.mAnimLayer)) { 300 mDimWinAnimator = newWinAnimator; 301 } 302 } 303 304 void stopDimmingIfNeeded() { 305 if (!mDimmingTag && isDimming()) { 306 mDimWinAnimator = null; 307 } 308 } 309 310 void setAnimationBackground(WindowStateAnimator winAnimator, int color) { 311 int animLayer = winAnimator.mAnimLayer; 312 if (mAnimationBackgroundAnimator == null 313 || animLayer < mAnimationBackgroundAnimator.mAnimLayer) { 314 mAnimationBackgroundAnimator = winAnimator; 315 animLayer = mService.adjustAnimationBackground(winAnimator); 316 mAnimationBackgroundSurface.show(animLayer - WindowManagerService.LAYER_OFFSET_DIM, 317 ((color >> 24) & 0xff) / 255f, 0); 318 } 319 } 320 321 void switchUser(int userId) { 322 int top = mTasks.size(); 323 for (int taskNdx = 0; taskNdx < top; ++taskNdx) { 324 Task task = mTasks.get(taskNdx); 325 if (task.mUserId == userId) { 326 mTasks.remove(taskNdx); 327 mTasks.add(task); 328 --top; 329 } 330 } 331 } 332 333 void close() { 334 mDimLayer.mDimSurface.destroy(); 335 mAnimationBackgroundSurface.mDimSurface.destroy(); 336 } 337 338 public void dump(String prefix, PrintWriter pw) { 339 pw.print(prefix); pw.print("mStackId="); pw.println(mStackId); 340 for (int taskNdx = 0; taskNdx < mTasks.size(); ++taskNdx) { 341 pw.print(prefix); pw.println(mTasks.get(taskNdx)); 342 } 343 if (mAnimationBackgroundSurface.isDimming()) { 344 pw.print(prefix); pw.println("mWindowAnimationBackgroundSurface:"); 345 mAnimationBackgroundSurface.printTo(prefix + " ", pw); 346 } 347 if (mDimLayer.isDimming()) { 348 pw.print(prefix); pw.println("mDimLayer:"); 349 mDimLayer.printTo(prefix, pw); 350 pw.print(prefix); pw.print("mDimWinAnimator="); pw.println(mDimWinAnimator); 351 } 352 } 353 354 @Override 355 public String toString() { 356 return "{stackId=" + mStackId + " tasks=" + mTasks + "}"; 357 } 358} 359