DisplayContent.java revision 2a88fc31cf16b419fdd23c9841ffdbfe7af9d966
1/* 2 * Copyright (C) 2012 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 android.app.ActivityManager.StackId.DOCKED_STACK_ID; 20import static android.app.ActivityManager.StackId.HOME_STACK_ID; 21import static android.app.ActivityManager.StackId.PINNED_STACK_ID; 22import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY; 23import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; 24import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP; 25 26import android.app.ActivityManager.StackId; 27import android.graphics.Rect; 28import android.graphics.Region; 29import android.util.DisplayMetrics; 30import android.util.Slog; 31import android.view.Display; 32import android.view.DisplayInfo; 33import android.view.Surface; 34 35import java.io.PrintWriter; 36import java.util.ArrayList; 37 38class DisplayContentList extends ArrayList<DisplayContent> { 39} 40 41/** 42 * Utility class for keeping track of the WindowStates and other pertinent contents of a 43 * particular Display. 44 * 45 * IMPORTANT: No method from this class should ever be used without holding 46 * WindowManagerService.mWindowMap. 47 */ 48class DisplayContent { 49 50 /** Unique identifier of this stack. */ 51 private final int mDisplayId; 52 53 /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element 54 * from mDisplayWindows; */ 55 private final WindowList mWindows = new WindowList(); 56 57 int mInitialDisplayWidth = 0; 58 int mInitialDisplayHeight = 0; 59 int mInitialDisplayDensity = 0; 60 int mBaseDisplayWidth = 0; 61 int mBaseDisplayHeight = 0; 62 int mBaseDisplayDensity = 0; 63 boolean mDisplayScalingDisabled; 64 private final DisplayInfo mDisplayInfo = new DisplayInfo(); 65 private final Display mDisplay; 66 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics(); 67 68 Rect mBaseDisplayRect = new Rect(); 69 Rect mContentRect = new Rect(); 70 71 // Accessed directly by all users. 72 boolean layoutNeeded; 73 int pendingLayoutChanges; 74 final boolean isDefaultDisplay; 75 76 /** Window tokens that are in the process of exiting, but still on screen for animations. */ 77 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>(); 78 79 /** Array containing all TaskStacks on this display. Array 80 * is stored in display order with the current bottom stack at 0. */ 81 private final ArrayList<TaskStack> mStacks = new ArrayList<>(); 82 83 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack 84 * (except a future lockscreen TaskStack) moves to the top. */ 85 private TaskStack mHomeStack = null; 86 87 /** Detect user tapping outside of current focused task bounds .*/ 88 TaskTapPointerEventListener mTapDetector; 89 90 /** Detect user tapping outside of current focused stack bounds .*/ 91 Region mTouchExcludeRegion = new Region(); 92 93 /** Detect user tapping in a non-resizeable task in docked or fullscreen stack .*/ 94 Region mNonResizeableRegion = new Region(); 95 96 /** Save allocating when calculating rects */ 97 private final Rect mTmpRect = new Rect(); 98 private final Rect mTmpRect2 = new Rect(); 99 private final Region mTmpRegion = new Region(); 100 101 /** For gathering Task objects in order. */ 102 final ArrayList<Task> mTmpTaskHistory = new ArrayList<Task>(); 103 104 final WindowManagerService mService; 105 106 /** Remove this display when animation on it has completed. */ 107 boolean mDeferredRemoval; 108 109 final DockedStackDividerController mDividerControllerLocked; 110 111 final DimLayerController mDimLayerController; 112 113 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>(); 114 115 /** 116 * @param display May not be null. 117 * @param service You know. 118 */ 119 DisplayContent(Display display, WindowManagerService service) { 120 mDisplay = display; 121 mDisplayId = display.getDisplayId(); 122 display.getDisplayInfo(mDisplayInfo); 123 display.getMetrics(mDisplayMetrics); 124 isDefaultDisplay = mDisplayId == Display.DEFAULT_DISPLAY; 125 mService = service; 126 initializeDisplayBaseInfo(); 127 mDividerControllerLocked = new DockedStackDividerController(service.mContext, this); 128 mDimLayerController = new DimLayerController(this); 129 } 130 131 int getDisplayId() { 132 return mDisplayId; 133 } 134 135 WindowList getWindowList() { 136 return mWindows; 137 } 138 139 Display getDisplay() { 140 return mDisplay; 141 } 142 143 DisplayInfo getDisplayInfo() { 144 return mDisplayInfo; 145 } 146 147 DisplayMetrics getDisplayMetrics() { 148 return mDisplayMetrics; 149 } 150 151 DockedStackDividerController getDockedDividerController() { 152 return mDividerControllerLocked; 153 } 154 155 /** 156 * Returns true if the specified UID has access to this display. 157 */ 158 public boolean hasAccess(int uid) { 159 return mDisplay.hasAccess(uid); 160 } 161 162 public boolean isPrivate() { 163 return (mDisplay.getFlags() & Display.FLAG_PRIVATE) != 0; 164 } 165 166 ArrayList<TaskStack> getStacks() { 167 return mStacks; 168 } 169 170 /** 171 * Retrieve the tasks on this display in stack order from the bottommost TaskStack up. 172 * @return All the Tasks, in order, on this display. 173 */ 174 ArrayList<Task> getTasks() { 175 mTmpTaskHistory.clear(); 176 final int numStacks = mStacks.size(); 177 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) { 178 mTmpTaskHistory.addAll(mStacks.get(stackNdx).getTasks()); 179 } 180 return mTmpTaskHistory; 181 } 182 183 TaskStack getHomeStack() { 184 if (mHomeStack == null && mDisplayId == Display.DEFAULT_DISPLAY) { 185 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this); 186 } 187 return mHomeStack; 188 } 189 190 void updateDisplayInfo() { 191 mDisplay.getDisplayInfo(mDisplayInfo); 192 mDisplay.getMetrics(mDisplayMetrics); 193 for (int i = mStacks.size() - 1; i >= 0; --i) { 194 mStacks.get(i).updateDisplayInfo(null); 195 } 196 } 197 198 void initializeDisplayBaseInfo() { 199 // Bootstrap the default logical display from the display manager. 200 final DisplayInfo newDisplayInfo = 201 mService.mDisplayManagerInternal.getDisplayInfo(mDisplayId); 202 if (newDisplayInfo != null) { 203 mDisplayInfo.copyFrom(newDisplayInfo); 204 } 205 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth; 206 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight; 207 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi; 208 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight); 209 } 210 211 void getLogicalDisplayRect(Rect out) { 212 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked. 213 final int orientation = mDisplayInfo.rotation; 214 boolean rotated = (orientation == Surface.ROTATION_90 215 || orientation == Surface.ROTATION_270); 216 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth; 217 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight; 218 int width = mDisplayInfo.logicalWidth; 219 int left = (physWidth - width) / 2; 220 int height = mDisplayInfo.logicalHeight; 221 int top = (physHeight - height) / 2; 222 out.set(left, top, left + width, top + height); 223 } 224 225 /** Refer to {@link WindowManagerService#attachStack(int, int, boolean)} */ 226 void attachStack(TaskStack stack, boolean onTop) { 227 if (stack.mStackId == HOME_STACK_ID) { 228 if (mHomeStack != null) { 229 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first."); 230 } 231 mHomeStack = stack; 232 } 233 if (onTop) { 234 mStacks.add(stack); 235 } else { 236 mStacks.add(0, stack); 237 } 238 layoutNeeded = true; 239 } 240 241 void moveStack(TaskStack stack, boolean toTop) { 242 if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) { 243 // This stack is always-on-top silly... 244 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom"); 245 return; 246 } 247 248 if (!mStacks.remove(stack)) { 249 Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable()); 250 } 251 252 int addIndex = toTop ? mStacks.size() : 0; 253 254 if (toTop 255 && mService.isStackVisibleLocked(PINNED_STACK_ID) 256 && stack.mStackId != PINNED_STACK_ID) { 257 // The pinned stack is always the top most stack (always-on-top) when it is visible. 258 // So, stack is moved just below the pinned stack. 259 addIndex--; 260 TaskStack topStack = mStacks.get(addIndex); 261 if (topStack.mStackId != PINNED_STACK_ID) { 262 throw new IllegalStateException("Pinned stack isn't top stack??? " + mStacks); 263 } 264 } 265 mStacks.add(addIndex, stack); 266 } 267 268 void detachStack(TaskStack stack) { 269 mDimLayerController.removeDimLayerUser(stack); 270 mStacks.remove(stack); 271 } 272 273 /** 274 * Propagate the new bounds to all child stacks. 275 * @param contentRect The bounds to apply at the top level. 276 */ 277 void resize(Rect contentRect) { 278 mContentRect.set(contentRect); 279 } 280 281 int taskIdFromPoint(int x, int y) { 282 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 283 TaskStack stack = mStacks.get(stackNdx); 284 stack.getBounds(mTmpRect); 285 if (!mTmpRect.contains(x, y)) { 286 continue; 287 } 288 final ArrayList<Task> tasks = stack.getTasks(); 289 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) { 290 final Task task = tasks.get(taskNdx); 291 final WindowState win = task.getTopVisibleAppMainWindow(); 292 if (win == null) { 293 continue; 294 } 295 // We need to use the task's dim bounds (which is derived from the visible 296 // bounds of its apps windows) for any touch-related tests. Can't use 297 // the task's original bounds because it might be adjusted to fit the 298 // content frame. For example, the presence of the IME adjusting the 299 // windows frames when the app window is the IME target. 300 task.getDimBounds(mTmpRect); 301 if (mTmpRect.contains(x, y)) { 302 return task.mTaskId; 303 } 304 } 305 } 306 return -1; 307 } 308 309 /** 310 * Find the task whose outside touch area (for resizing) (x, y) falls within. 311 * Returns null if the touch doesn't fall into a resizing area. 312 */ 313 Task findTaskForControlPoint(int x, int y) { 314 final int delta = mService.dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics); 315 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 316 TaskStack stack = mStacks.get(stackNdx); 317 if (!StackId.isTaskResizeAllowed(stack.mStackId)) { 318 break; 319 } 320 final ArrayList<Task> tasks = stack.getTasks(); 321 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) { 322 final Task task = tasks.get(taskNdx); 323 if (task.isFullscreen()) { 324 return null; 325 } 326 327 // We need to use the task's dim bounds (which is derived from the visible 328 // bounds of its apps windows) for any touch-related tests. Can't use 329 // the task's original bounds because it might be adjusted to fit the 330 // content frame. One example is when the task is put to top-left quadrant, 331 // the actual visible area would not start at (0,0) after it's adjusted 332 // for the status bar. 333 task.getDimBounds(mTmpRect); 334 mTmpRect.inset(-delta, -delta); 335 if (mTmpRect.contains(x, y)) { 336 mTmpRect.inset(delta, delta); 337 if (!mTmpRect.contains(x, y)) { 338 return task; 339 } 340 // User touched inside the task. No need to look further, 341 // focus transfer will be handled in ACTION_UP. 342 return null; 343 } 344 } 345 } 346 return null; 347 } 348 349 void setTouchExcludeRegion(Task focusedTask) { 350 mTouchExcludeRegion.set(mBaseDisplayRect); 351 final int delta = mService.dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics); 352 boolean addBackFocusedTask = false; 353 mNonResizeableRegion.setEmpty(); 354 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 355 TaskStack stack = mStacks.get(stackNdx); 356 final ArrayList<Task> tasks = stack.getTasks(); 357 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) { 358 final Task task = tasks.get(taskNdx); 359 final WindowState win = task.getTopVisibleAppMainWindow(); 360 if (win == null) { 361 continue; 362 } 363 364 /** 365 * Exclusion region is the region that TapDetector doesn't care about. 366 * Here we want to remove all non-focused tasks from the exclusion region. 367 * We also remove the outside touch area for resizing for all freeform 368 * tasks (including the focused). 369 * 370 * (For freeform focused task, the below logic will first remove the enlarged 371 * area, then add back the inner area.) 372 */ 373 final boolean isFreeformed = task.inFreeformWorkspace(); 374 if (task != focusedTask || isFreeformed) { 375 task.getDimBounds(mTmpRect); 376 if (isFreeformed) { 377 // If we're removing a freeform, focused app from the exclusion region, 378 // we need to add back its touchable frame later. Remember the touchable 379 // frame now. 380 if (task == focusedTask) { 381 addBackFocusedTask = true; 382 mTmpRect2.set(mTmpRect); 383 } 384 // If the task is freeformed, enlarge the area to account for outside 385 // touch area for resize. 386 mTmpRect.inset(-delta, -delta); 387 // Intersect with display content rect. If we have system decor (status bar/ 388 // navigation bar), we want to exclude that from the tap detection. 389 // Otherwise, if the app is partially placed under some system button (eg. 390 // Recents, Home), pressing that button would cause a full series of 391 // unwanted transfer focus/resume/pause, before we could go home. 392 mTmpRect.intersect(mContentRect); 393 } 394 mTouchExcludeRegion.op(mTmpRect, Region.Op.DIFFERENCE); 395 } 396 if (task.isTwoFingerScrollMode()) { 397 stack.getBounds(mTmpRect); 398 mNonResizeableRegion.op(mTmpRect, Region.Op.UNION); 399 break; 400 } 401 } 402 } 403 // If we removed the focused task above, add it back and only leave its 404 // outside touch area in the exclusion. TapDectector is not interested in 405 // any touch inside the focused task itself. 406 if (addBackFocusedTask) { 407 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION); 408 } 409 final WindowState inputMethod = mService.mInputMethodWindow; 410 if (inputMethod != null && inputMethod.isVisibleLw()) { 411 // If the input method is visible and the user is typing, we don't want these touch 412 // events to be intercepted and used to change focus. This would likely cause a 413 // disappearance of the input method. 414 inputMethod.getTouchableRegion(mTmpRegion); 415 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION); 416 } 417 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) { 418 WindowState win = mTapExcludedWindows.get(i); 419 win.getTouchableRegion(mTmpRegion); 420 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION); 421 } 422 if (mTapDetector != null) { 423 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion, mNonResizeableRegion); 424 } 425 } 426 427 void switchUserStacks() { 428 final WindowList windows = getWindowList(); 429 for (int i = 0; i < windows.size(); i++) { 430 final WindowState win = windows.get(i); 431 if (win.isHiddenFromUserLocked()) { 432 if (DEBUG_VISIBILITY) Slog.w(TAG_WM, "user changing, hiding " + win 433 + ", attrs=" + win.mAttrs.type + ", belonging to " + win.mOwnerUid); 434 win.hideLw(false); 435 } 436 } 437 438 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 439 mStacks.get(stackNdx).switchUser(); 440 } 441 } 442 443 void resetAnimationBackgroundAnimator() { 444 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 445 mStacks.get(stackNdx).resetAnimationBackgroundAnimator(); 446 } 447 } 448 449 boolean animateDimLayers() { 450 return mDimLayerController.animateDimLayers(); 451 } 452 453 void resetDimming() { 454 mDimLayerController.resetDimming(); 455 } 456 457 boolean isDimming() { 458 return mDimLayerController.isDimming(); 459 } 460 461 void stopDimmingIfNeeded() { 462 mDimLayerController.stopDimmingIfNeeded(); 463 } 464 465 void close() { 466 mDimLayerController.close(); 467 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 468 mStacks.get(stackNdx).close(); 469 } 470 } 471 472 boolean isAnimating() { 473 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 474 final TaskStack stack = mStacks.get(stackNdx); 475 if (stack.isAnimating()) { 476 return true; 477 } 478 } 479 return false; 480 } 481 482 void checkForDeferredActions() { 483 boolean animating = false; 484 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 485 final TaskStack stack = mStacks.get(stackNdx); 486 if (stack.isAnimating()) { 487 animating = true; 488 } else { 489 if (stack.mDeferDetach) { 490 mService.detachStackLocked(this, stack); 491 } 492 final ArrayList<Task> tasks = stack.getTasks(); 493 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) { 494 final Task task = tasks.get(taskNdx); 495 AppTokenList tokens = task.mAppTokens; 496 for (int tokenNdx = tokens.size() - 1; tokenNdx >= 0; --tokenNdx) { 497 AppWindowToken wtoken = tokens.get(tokenNdx); 498 if (wtoken.mIsExiting) { 499 wtoken.removeAppFromTaskLocked(); 500 } 501 } 502 } 503 } 504 } 505 if (!animating && mDeferredRemoval) { 506 mService.onDisplayRemoved(mDisplayId); 507 } 508 } 509 510 void rotateBounds(int oldRotation, int newRotation, Rect bounds) { 511 final int rotationDelta = DisplayContent.deltaRotation(oldRotation, newRotation); 512 getLogicalDisplayRect(mTmpRect); 513 switch (rotationDelta) { 514 case Surface.ROTATION_0: 515 mTmpRect2.set(bounds); 516 break; 517 case Surface.ROTATION_90: 518 mTmpRect2.top = mTmpRect.bottom - bounds.right; 519 mTmpRect2.left = bounds.top; 520 mTmpRect2.right = mTmpRect2.left + bounds.height(); 521 mTmpRect2.bottom = mTmpRect2.top + bounds.width(); 522 break; 523 case Surface.ROTATION_180: 524 mTmpRect2.top = mTmpRect.bottom - bounds.bottom; 525 mTmpRect2.left = mTmpRect.right - bounds.right; 526 mTmpRect2.right = mTmpRect2.left + bounds.width(); 527 mTmpRect2.bottom = mTmpRect2.top + bounds.height(); 528 break; 529 case Surface.ROTATION_270: 530 mTmpRect2.top = bounds.left; 531 mTmpRect2.left = mTmpRect.right - bounds.bottom; 532 mTmpRect2.right = mTmpRect2.left + bounds.height(); 533 mTmpRect2.bottom = mTmpRect2.top + bounds.width(); 534 break; 535 } 536 bounds.set(mTmpRect2); 537 } 538 539 static int deltaRotation(int oldRotation, int newRotation) { 540 int delta = newRotation - oldRotation; 541 if (delta < 0) delta += 4; 542 return delta; 543 } 544 545 public void dump(String prefix, PrintWriter pw) { 546 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId); 547 final String subPrefix = " " + prefix; 548 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x"); 549 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity); 550 pw.print("dpi"); 551 if (mInitialDisplayWidth != mBaseDisplayWidth 552 || mInitialDisplayHeight != mBaseDisplayHeight 553 || mInitialDisplayDensity != mBaseDisplayDensity) { 554 pw.print(" base="); 555 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight); 556 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi"); 557 } 558 if (mDisplayScalingDisabled) { 559 pw.println(" noscale"); 560 } 561 pw.print(" cur="); 562 pw.print(mDisplayInfo.logicalWidth); 563 pw.print("x"); pw.print(mDisplayInfo.logicalHeight); 564 pw.print(" app="); 565 pw.print(mDisplayInfo.appWidth); 566 pw.print("x"); pw.print(mDisplayInfo.appHeight); 567 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth); 568 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight); 569 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth); 570 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight); 571 pw.print(subPrefix); pw.print("deferred="); pw.print(mDeferredRemoval); 572 pw.print(" layoutNeeded="); pw.println(layoutNeeded); 573 574 pw.println(); 575 pw.println(" Application tokens in top down Z order:"); 576 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 577 final TaskStack stack = mStacks.get(stackNdx); 578 stack.dump(prefix + " ", pw); 579 } 580 581 pw.println(); 582 if (!mExitingTokens.isEmpty()) { 583 pw.println(); 584 pw.println(" Exiting tokens:"); 585 for (int i = mExitingTokens.size() - 1; i >= 0; i--) { 586 WindowToken token = mExitingTokens.get(i); 587 pw.print(" Exiting #"); pw.print(i); 588 pw.print(' '); pw.print(token); 589 pw.println(':'); 590 token.dump(pw, " "); 591 } 592 } 593 pw.println(); 594 mDimLayerController.dump(prefix + " ", pw); 595 } 596 597 @Override 598 public String toString() { 599 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mStacks; 600 } 601 602 TaskStack getDockedStackLocked() { 603 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID); 604 return (stack != null && stack.isVisibleLocked()) ? stack : null; 605 } 606} 607