DisplayContent.java revision b660b9d8cf6b951b85a35599d636c470795e9a1a
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 com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID; 20import static com.android.server.wm.WindowManagerService.DEBUG_VISIBILITY; 21import static com.android.server.wm.WindowManagerService.TAG; 22 23import android.graphics.Rect; 24import android.graphics.Region; 25import android.util.Slog; 26import android.view.Display; 27import android.view.DisplayInfo; 28import android.view.Surface; 29 30import java.io.PrintWriter; 31import java.util.ArrayList; 32 33class DisplayContentList extends ArrayList<DisplayContent> { 34} 35 36/** 37 * Utility class for keeping track of the WindowStates and other pertinent contents of a 38 * particular Display. 39 * 40 * IMPORTANT: No method from this class should ever be used without holding 41 * WindowManagerService.mWindowMap. 42 */ 43class DisplayContent { 44 45 /** Unique identifier of this stack. */ 46 private final int mDisplayId; 47 48 /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element 49 * from mDisplayWindows; */ 50 private final WindowList mWindows = new WindowList(); 51 52 // This protects the following display size properties, so that 53 // getDisplaySize() doesn't need to acquire the global lock. This is 54 // needed because the window manager sometimes needs to use ActivityThread 55 // while it has its global state locked (for example to load animation 56 // resources), but the ActivityThread also needs get the current display 57 // size sometimes when it has its package lock held. 58 // 59 // These will only be modified with both mWindowMap and mDisplaySizeLock 60 // held (in that order) so the window manager doesn't need to acquire this 61 // lock when needing these values in its normal operation. 62 final Object mDisplaySizeLock = new Object(); 63 int mInitialDisplayWidth = 0; 64 int mInitialDisplayHeight = 0; 65 int mInitialDisplayDensity = 0; 66 int mBaseDisplayWidth = 0; 67 int mBaseDisplayHeight = 0; 68 int mBaseDisplayDensity = 0; 69 private final DisplayInfo mDisplayInfo = new DisplayInfo(); 70 private final Display mDisplay; 71 72 Rect mBaseDisplayRect = new Rect(); 73 Rect mContentRect = new Rect(); 74 75 // Accessed directly by all users. 76 boolean layoutNeeded; 77 int pendingLayoutChanges; 78 final boolean isDefaultDisplay; 79 80 /** Window tokens that are in the process of exiting, but still on screen for animations. */ 81 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>(); 82 83 /** Array containing all TaskStacks on this display. Array 84 * is stored in display order with the current bottom stack at 0. */ 85 private final ArrayList<TaskStack> mStacks = new ArrayList<TaskStack>(); 86 87 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack 88 * (except a future lockscreen TaskStack) moves to the top. */ 89 private TaskStack mHomeStack = null; 90 91 /** Detect user tapping outside of current focused stack bounds .*/ 92 StackTapPointerEventListener mTapDetector; 93 94 /** Detect user tapping outside of current focused stack bounds .*/ 95 Region mTouchExcludeRegion = new Region(); 96 97 /** Save allocating when calculating rects */ 98 Rect mTmpRect = new Rect(); 99 100 /** For gathering Task objects in order. */ 101 final ArrayList<Task> mTmpTaskHistory = new ArrayList<Task>(); 102 103 final WindowManagerService mService; 104 105 static final int DEFER_DETACH = 1; 106 static final int DEFER_REMOVAL = 2; 107 int mDeferredActions; 108 109 /** 110 * @param display May not be null. 111 * @param service You know. 112 */ 113 DisplayContent(Display display, WindowManagerService service) { 114 mDisplay = display; 115 mDisplayId = display.getDisplayId(); 116 display.getDisplayInfo(mDisplayInfo); 117 isDefaultDisplay = mDisplayId == Display.DEFAULT_DISPLAY; 118 mService = service; 119 } 120 121 int getDisplayId() { 122 return mDisplayId; 123 } 124 125 WindowList getWindowList() { 126 return mWindows; 127 } 128 129 Display getDisplay() { 130 return mDisplay; 131 } 132 133 DisplayInfo getDisplayInfo() { 134 return mDisplayInfo; 135 } 136 137 /** 138 * Returns true if the specified UID has access to this display. 139 */ 140 public boolean hasAccess(int uid) { 141 return mDisplay.hasAccess(uid); 142 } 143 144 public boolean isPrivate() { 145 return (mDisplay.getFlags() & Display.FLAG_PRIVATE) != 0; 146 } 147 148 ArrayList<TaskStack> getStacks() { 149 return mStacks; 150 } 151 152 /** 153 * Retrieve the tasks on this display in stack order from the bottommost TaskStack up. 154 * @return All the Tasks, in order, on this display. 155 */ 156 ArrayList<Task> getTasks() { 157 mTmpTaskHistory.clear(); 158 final int numStacks = mStacks.size(); 159 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) { 160 mTmpTaskHistory.addAll(mStacks.get(stackNdx).getTasks()); 161 } 162 return mTmpTaskHistory; 163 } 164 165 TaskStack getHomeStack() { 166 if (mHomeStack == null) { 167 Slog.e(TAG, "getHomeStack: Returning null from this=" + this); 168 } 169 return mHomeStack; 170 } 171 172 void updateDisplayInfo() { 173 mDisplay.getDisplayInfo(mDisplayInfo); 174 for (int i = mStacks.size() - 1; i >= 0; --i) { 175 mStacks.get(i).updateDisplayInfo(); 176 } 177 } 178 179 void getLogicalDisplayRect(Rect out) { 180 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked. 181 final int orientation = mDisplayInfo.rotation; 182 boolean rotated = (orientation == Surface.ROTATION_90 183 || orientation == Surface.ROTATION_270); 184 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth; 185 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight; 186 int width = mDisplayInfo.logicalWidth; 187 int left = (physWidth - width) / 2; 188 int height = mDisplayInfo.logicalHeight; 189 int top = (physHeight - height) / 2; 190 out.set(left, top, left + width, top + height); 191 } 192 193 /** Refer to {@link WindowManagerService#attachStack(int, int)} */ 194 void attachStack(TaskStack stack) { 195 if (stack.mStackId == HOME_STACK_ID) { 196 if (mHomeStack != null) { 197 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first."); 198 } 199 mHomeStack = stack; 200 } 201 mStacks.add(stack); 202 layoutNeeded = true; 203 } 204 205 void moveStack(TaskStack stack, boolean toTop) { 206 mStacks.remove(stack); 207 mStacks.add(toTop ? mStacks.size() : 0, stack); 208 } 209 210 void detachStack(TaskStack stack) { 211 mStacks.remove(stack); 212 } 213 214 /** 215 * Propagate the new bounds to all child stacks. 216 * @param contentRect The bounds to apply at the top level. 217 */ 218 void resize(Rect contentRect) { 219 mContentRect.set(contentRect); 220 } 221 222 int stackIdFromPoint(int x, int y) { 223 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 224 final TaskStack stack = mStacks.get(stackNdx); 225 stack.getBounds(mTmpRect); 226 if (mTmpRect.contains(x, y)) { 227 return stack.mStackId; 228 } 229 } 230 return -1; 231 } 232 233 void setTouchExcludeRegion(TaskStack focusedStack) { 234 mTouchExcludeRegion.set(mBaseDisplayRect); 235 WindowList windows = getWindowList(); 236 for (int i = windows.size() - 1; i >= 0; --i) { 237 final WindowState win = windows.get(i); 238 final TaskStack stack = win.getStack(); 239 if (win.isVisibleLw() && stack != null && stack != focusedStack) { 240 mTmpRect.set(win.mVisibleFrame); 241 mTmpRect.intersect(win.mVisibleInsets); 242 mTouchExcludeRegion.op(mTmpRect, Region.Op.DIFFERENCE); 243 } 244 } 245 } 246 247 void switchUserStacks(int newUserId) { 248 final WindowList windows = getWindowList(); 249 for (int i = 0; i < windows.size(); i++) { 250 final WindowState win = windows.get(i); 251 if (win.isHiddenFromUserLocked()) { 252 if (DEBUG_VISIBILITY) Slog.w(TAG, "user changing " + newUserId + " hiding " 253 + win + ", attrs=" + win.mAttrs.type + ", belonging to " 254 + win.mOwnerUid); 255 win.hideLw(false); 256 } 257 } 258 259 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 260 mStacks.get(stackNdx).switchUser(newUserId); 261 } 262 } 263 264 void resetAnimationBackgroundAnimator() { 265 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 266 mStacks.get(stackNdx).resetAnimationBackgroundAnimator(); 267 } 268 } 269 270 boolean animateDimLayers() { 271 boolean result = false; 272 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 273 result |= mStacks.get(stackNdx).animateDimLayers(); 274 } 275 return result; 276 } 277 278 void resetDimming() { 279 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 280 mStacks.get(stackNdx).resetDimmingTag(); 281 } 282 } 283 284 boolean isDimming() { 285 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 286 if (mStacks.get(stackNdx).isDimming()) { 287 return true; 288 } 289 } 290 return false; 291 } 292 293 void stopDimmingIfNeeded() { 294 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 295 mStacks.get(stackNdx).stopDimmingIfNeeded(); 296 } 297 } 298 299 void close() { 300 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 301 mStacks.get(stackNdx).close(); 302 } 303 } 304 305 public void dump(String prefix, PrintWriter pw) { 306 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId); 307 final String subPrefix = " " + prefix; 308 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x"); 309 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity); 310 pw.print("dpi"); 311 if (mInitialDisplayWidth != mBaseDisplayWidth 312 || mInitialDisplayHeight != mBaseDisplayHeight 313 || mInitialDisplayDensity != mBaseDisplayDensity) { 314 pw.print(" base="); 315 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight); 316 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi"); 317 } 318 pw.print(" cur="); 319 pw.print(mDisplayInfo.logicalWidth); 320 pw.print("x"); pw.print(mDisplayInfo.logicalHeight); 321 pw.print(" app="); 322 pw.print(mDisplayInfo.appWidth); 323 pw.print("x"); pw.print(mDisplayInfo.appHeight); 324 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth); 325 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight); 326 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth); 327 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight); 328 pw.print(subPrefix); pw.print("layoutNeeded="); pw.println(layoutNeeded); 329 for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) { 330 final TaskStack stack = mStacks.get(stackNdx); 331 pw.print(prefix); pw.print("mStacks[" + stackNdx + "]"); pw.println(stack.mStackId); 332 stack.dump(prefix + " ", pw); 333 } 334 pw.println(); 335 pw.println(" Application tokens in bottom up Z order:"); 336 int ndx = 0; 337 final int numStacks = mStacks.size(); 338 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) { 339 ArrayList<Task> tasks = mStacks.get(stackNdx).getTasks(); 340 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) { 341 AppTokenList tokens = tasks.get(taskNdx).mAppTokens; 342 for (int tokenNdx = tokens.size() - 1; tokenNdx >= 0; --tokenNdx) { 343 final AppWindowToken wtoken = tokens.get(tokenNdx); 344 pw.print(" App #"); pw.print(ndx++); 345 pw.print(' '); pw.print(wtoken); pw.println(":"); 346 wtoken.dump(pw, " "); 347 } 348 } 349 } 350 if (ndx == 0) { 351 pw.println(" None"); 352 } 353 pw.println(); 354 if (!mExitingTokens.isEmpty()) { 355 pw.println(); 356 pw.println(" Exiting tokens:"); 357 for (int i=mExitingTokens.size()-1; i>=0; i--) { 358 WindowToken token = mExitingTokens.get(i); 359 pw.print(" Exiting #"); pw.print(i); 360 pw.print(' '); pw.print(token); 361 pw.println(':'); 362 token.dump(pw, " "); 363 } 364 } 365 pw.println(); 366 } 367 368 @Override 369 public String toString() { 370 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mStacks; 371 } 372} 373