WallpaperController.java revision 21fdd916a77e0ec64ca1c9b7fcb454283d6f58c3
1/* 2 * Copyright (C) 2015 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.FREEFORM_WORKSPACE_STACK_ID; 20import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER; 21import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD; 22import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING; 23import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM; 24import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER; 25import static com.android.server.wm.WindowManagerService.DEBUG_ADD_REMOVE; 26import static com.android.server.wm.WindowManagerService.DEBUG_APP_TRANSITIONS; 27import static com.android.server.wm.WindowManagerService.DEBUG_LAYERS; 28import static com.android.server.wm.WindowManagerService.DEBUG_WINDOW_MOVEMENT; 29import static com.android.server.wm.WindowManagerService.DEBUG_VISIBILITY; 30import static com.android.server.wm.WindowManagerService.DEBUG_WALLPAPER; 31import static com.android.server.wm.WindowManagerService.DEBUG_WALLPAPER_LIGHT; 32import static com.android.server.wm.WindowManagerService.H.WALLPAPER_DRAW_PENDING_TIMEOUT; 33import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER; 34import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET; 35 36import android.os.Bundle; 37import android.os.Debug; 38import android.os.IBinder; 39import android.os.RemoteException; 40import android.os.SystemClock; 41import android.util.Slog; 42import android.view.DisplayInfo; 43import android.view.WindowManager; 44import android.view.WindowManagerPolicy; 45 46import java.io.PrintWriter; 47import java.util.ArrayList; 48 49/** 50 * Controls wallpaper windows visibility, ordering, and so on. 51 * NOTE: All methods in this class must be called with the window manager service lock held. 52 */ 53class WallpaperController { 54 private static final String TAG = com.android.server.wm.WindowManagerService.TAG; 55 final private WindowManagerService mService; 56 57 private final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<>(); 58 59 // If non-null, this is the currently visible window that is associated 60 // with the wallpaper. 61 private WindowState mWallpaperTarget = null; 62 // If non-null, we are in the middle of animating from one wallpaper target 63 // to another, and this is the lower one in Z-order. 64 private WindowState mLowerWallpaperTarget = null; 65 // If non-null, we are in the middle of animating from one wallpaper target 66 // to another, and this is the higher one in Z-order. 67 private WindowState mUpperWallpaperTarget = null; 68 69 private int mWallpaperAnimLayerAdjustment; 70 71 private float mLastWallpaperX = -1; 72 private float mLastWallpaperY = -1; 73 private float mLastWallpaperXStep = -1; 74 private float mLastWallpaperYStep = -1; 75 private int mLastWallpaperDisplayOffsetX = Integer.MIN_VALUE; 76 private int mLastWallpaperDisplayOffsetY = Integer.MIN_VALUE; 77 78 // This is set when we are waiting for a wallpaper to tell us it is done 79 // changing its scroll position. 80 WindowState mWaitingOnWallpaper; 81 82 // The last time we had a timeout when waiting for a wallpaper. 83 private long mLastWallpaperTimeoutTime; 84 // We give a wallpaper up to 150ms to finish scrolling. 85 private static final long WALLPAPER_TIMEOUT = 150; 86 // Time we wait after a timeout before trying to wait again. 87 private static final long WALLPAPER_TIMEOUT_RECOVERY = 10000; 88 89 // Set to the wallpaper window we would like to hide once the transition animations are done. 90 // This is useful in cases where we don't want the wallpaper to be hidden when the close app 91 // is a wallpaper target and is done animating out, but the opening app isn't a wallpaper 92 // target and isn't done animating in. 93 private WindowState mDeferredHideWallpaper = null; 94 95 // We give a wallpaper up to 500ms to finish drawing before playing app transitions. 96 private static final long WALLPAPER_DRAW_PENDING_TIMEOUT_DURATION = 500; 97 private static final int WALLPAPER_DRAW_NORMAL = 0; 98 private static final int WALLPAPER_DRAW_PENDING = 1; 99 private static final int WALLPAPER_DRAW_TIMEOUT = 2; 100 private int mWallpaperDrawState = WALLPAPER_DRAW_NORMAL; 101 102 private final FindWallpaperTargetResult mFindResults = new FindWallpaperTargetResult(); 103 104 public WallpaperController(WindowManagerService service) { 105 mService = service; 106 } 107 108 WindowState getWallpaperTarget() { 109 return mWallpaperTarget; 110 } 111 112 WindowState getLowerWallpaperTarget() { 113 return mLowerWallpaperTarget; 114 } 115 116 WindowState getUpperWallpaperTarget() { 117 return mUpperWallpaperTarget; 118 } 119 120 boolean isWallpaperTarget(WindowState win) { 121 return win == mWallpaperTarget; 122 } 123 124 boolean isBelowWallpaperTarget(WindowState win) { 125 return mWallpaperTarget != null && mWallpaperTarget.mLayer >= win.mBaseLayer; 126 } 127 128 boolean isWallpaperVisible() { 129 return isWallpaperVisible(mWallpaperTarget); 130 } 131 132 private boolean isWallpaperVisible(WindowState wallpaperTarget) { 133 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper vis: target " + wallpaperTarget + ", obscured=" 134 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??") 135 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null) 136 ? wallpaperTarget.mAppToken.mAppAnimator.animation : null) 137 + " upper=" + mUpperWallpaperTarget 138 + " lower=" + mLowerWallpaperTarget); 139 return (wallpaperTarget != null 140 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null 141 && wallpaperTarget.mAppToken.mAppAnimator.animation != null))) 142 || mUpperWallpaperTarget != null 143 || mLowerWallpaperTarget != null; 144 } 145 146 boolean isWallpaperTargetAnimating() { 147 return mWallpaperTarget != null && mWallpaperTarget.mWinAnimator.isAnimating() 148 && !mWallpaperTarget.mWinAnimator.isDummyAnimation(); 149 } 150 151 void updateWallpaperVisibility() { 152 final boolean visible = isWallpaperVisible(mWallpaperTarget); 153 final DisplayContent displayContent = mWallpaperTarget.getDisplayContent(); 154 if (displayContent == null) { 155 return; 156 } 157 final DisplayInfo displayInfo = displayContent.getDisplayInfo(); 158 final int dw = displayInfo.logicalWidth; 159 final int dh = displayInfo.logicalHeight; 160 161 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) { 162 WindowToken token = mWallpaperTokens.get(curTokenNdx); 163 if (token.hidden == visible) { 164 token.hidden = !visible; 165 // Need to do a layout to ensure the wallpaper now has the 166 // correct size. 167 displayContent.layoutNeeded = true; 168 } 169 170 final WindowList windows = token.windows; 171 for (int wallpaperNdx = windows.size() - 1; wallpaperNdx >= 0; wallpaperNdx--) { 172 WindowState wallpaper = windows.get(wallpaperNdx); 173 if (visible) { 174 updateWallpaperOffset(wallpaper, dw, dh, false); 175 } 176 177 dispatchWallpaperVisibility(wallpaper, visible); 178 } 179 } 180 } 181 182 void hideDeferredWallpapersIfNeeded() { 183 if (mDeferredHideWallpaper != null) { 184 hideWallpapers(mDeferredHideWallpaper); 185 mDeferredHideWallpaper = null; 186 } 187 } 188 189 void hideWallpapers(final WindowState winGoingAway) { 190 if (mWallpaperTarget != null 191 && (mWallpaperTarget != winGoingAway || mLowerWallpaperTarget != null)) { 192 return; 193 } 194 if (mService.mAppTransition.isRunning()) { 195 // Defer hiding the wallpaper when app transition is running until the animations 196 // are done. 197 mDeferredHideWallpaper = winGoingAway; 198 return; 199 } 200 201 final boolean wasDeferred = (mDeferredHideWallpaper == winGoingAway); 202 for (int i = mWallpaperTokens.size() - 1; i >= 0; i--) { 203 final WindowToken token = mWallpaperTokens.get(i); 204 for (int j = token.windows.size() - 1; j >= 0; j--) { 205 final WindowState wallpaper = token.windows.get(j); 206 final WindowStateAnimator winAnimator = wallpaper.mWinAnimator; 207 if (!winAnimator.mLastHidden || wasDeferred) { 208 winAnimator.hide(); 209 dispatchWallpaperVisibility(wallpaper, false); 210 final DisplayContent displayContent = wallpaper.getDisplayContent(); 211 if (displayContent != null) { 212 displayContent.pendingLayoutChanges |= 213 WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER; 214 } 215 } 216 } 217 if (DEBUG_WALLPAPER_LIGHT && !token.hidden) Slog.d(TAG, "Hiding wallpaper " + token 218 + " from " + winGoingAway + " target=" + mWallpaperTarget + " lower=" 219 + mLowerWallpaperTarget + "\n" + Debug.getCallers(5, " ")); 220 token.hidden = true; 221 } 222 } 223 224 /** 225 * Check wallpaper for visibility change and notify window if so. 226 * @param wallpaper The wallpaper to test and notify. 227 * @param visible Current visibility. 228 */ 229 void dispatchWallpaperVisibility(final WindowState wallpaper, final boolean visible) { 230 // Only send notification if the visibility actually changed and we are not trying to hide 231 // the wallpaper when we are deferring hiding of the wallpaper. 232 if (wallpaper.mWallpaperVisible != visible 233 && (mDeferredHideWallpaper == null || visible)) { 234 wallpaper.mWallpaperVisible = visible; 235 try { 236 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, 237 "Updating vis of wallpaper " + wallpaper 238 + ": " + visible + " from:\n" + Debug.getCallers(4, " ")); 239 wallpaper.mClient.dispatchAppVisibility(visible); 240 } catch (RemoteException e) { 241 } 242 } 243 } 244 245 boolean updateWallpaperOffset(WindowState wallpaperWin, int dw, int dh, boolean sync) { 246 boolean rawChanged = false; 247 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f; 248 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f; 249 int availw = wallpaperWin.mFrame.right - wallpaperWin.mFrame.left - dw; 250 int offset = availw > 0 ? -(int)(availw * wpx + .5f) : 0; 251 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE) { 252 offset += mLastWallpaperDisplayOffsetX; 253 } 254 boolean changed = wallpaperWin.mXOffset != offset; 255 if (changed) { 256 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper " + wallpaperWin + " x: " + offset); 257 wallpaperWin.mXOffset = offset; 258 } 259 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) { 260 wallpaperWin.mWallpaperX = wpx; 261 wallpaperWin.mWallpaperXStep = wpxs; 262 rawChanged = true; 263 } 264 265 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f; 266 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f; 267 int availh = wallpaperWin.mFrame.bottom - wallpaperWin.mFrame.top - dh; 268 offset = availh > 0 ? -(int)(availh * wpy + .5f) : 0; 269 if (mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) { 270 offset += mLastWallpaperDisplayOffsetY; 271 } 272 if (wallpaperWin.mYOffset != offset) { 273 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper " + wallpaperWin + " y: " + offset); 274 changed = true; 275 wallpaperWin.mYOffset = offset; 276 } 277 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) { 278 wallpaperWin.mWallpaperY = wpy; 279 wallpaperWin.mWallpaperYStep = wpys; 280 rawChanged = true; 281 } 282 283 if (rawChanged && (wallpaperWin.mAttrs.privateFlags & 284 WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS) != 0) { 285 try { 286 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset " 287 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX 288 + " y=" + wallpaperWin.mWallpaperY); 289 if (sync) { 290 mWaitingOnWallpaper = wallpaperWin; 291 } 292 wallpaperWin.mClient.dispatchWallpaperOffsets( 293 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY, 294 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync); 295 if (sync) { 296 if (mWaitingOnWallpaper != null) { 297 long start = SystemClock.uptimeMillis(); 298 if ((mLastWallpaperTimeoutTime + WALLPAPER_TIMEOUT_RECOVERY) 299 < start) { 300 try { 301 if (DEBUG_WALLPAPER) Slog.v(TAG, 302 "Waiting for offset complete..."); 303 mService.mWindowMap.wait(WALLPAPER_TIMEOUT); 304 } catch (InterruptedException e) { 305 } 306 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!"); 307 if ((start + WALLPAPER_TIMEOUT) < SystemClock.uptimeMillis()) { 308 Slog.i(TAG, "Timeout waiting for wallpaper to offset: " 309 + wallpaperWin); 310 mLastWallpaperTimeoutTime = start; 311 } 312 } 313 mWaitingOnWallpaper = null; 314 } 315 } 316 } catch (RemoteException e) { 317 } 318 } 319 320 return changed; 321 } 322 323 void setWindowWallpaperPosition( 324 WindowState window, float x, float y, float xStep, float yStep) { 325 if (window.mWallpaperX != x || window.mWallpaperY != y) { 326 window.mWallpaperX = x; 327 window.mWallpaperY = y; 328 window.mWallpaperXStep = xStep; 329 window.mWallpaperYStep = yStep; 330 updateWallpaperOffsetLocked(window, true); 331 } 332 } 333 334 void setWindowWallpaperDisplayOffset(WindowState window, int x, int y) { 335 if (window.mWallpaperDisplayOffsetX != x || window.mWallpaperDisplayOffsetY != y) { 336 window.mWallpaperDisplayOffsetX = x; 337 window.mWallpaperDisplayOffsetY = y; 338 updateWallpaperOffsetLocked(window, true); 339 } 340 } 341 342 Bundle sendWindowWallpaperCommand( 343 WindowState window, String action, int x, int y, int z, Bundle extras, boolean sync) { 344 if (window == mWallpaperTarget 345 || window == mLowerWallpaperTarget 346 || window == mUpperWallpaperTarget) { 347 boolean doWait = sync; 348 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) { 349 final WindowList windows = mWallpaperTokens.get(curTokenNdx).windows; 350 for (int wallpaperNdx = windows.size() - 1; wallpaperNdx >= 0; wallpaperNdx--) { 351 WindowState wallpaper = windows.get(wallpaperNdx); 352 try { 353 wallpaper.mClient.dispatchWallpaperCommand(action, 354 x, y, z, extras, sync); 355 // We only want to be synchronous with one wallpaper. 356 sync = false; 357 } catch (RemoteException e) { 358 } 359 } 360 } 361 362 if (doWait) { 363 // TODO: Need to wait for result. 364 } 365 } 366 367 return null; 368 } 369 370 void updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) { 371 final DisplayContent displayContent = changingTarget.getDisplayContent(); 372 if (displayContent == null) { 373 return; 374 } 375 final DisplayInfo displayInfo = displayContent.getDisplayInfo(); 376 final int dw = displayInfo.logicalWidth; 377 final int dh = displayInfo.logicalHeight; 378 379 WindowState target = mWallpaperTarget; 380 if (target != null) { 381 if (target.mWallpaperX >= 0) { 382 mLastWallpaperX = target.mWallpaperX; 383 } else if (changingTarget.mWallpaperX >= 0) { 384 mLastWallpaperX = changingTarget.mWallpaperX; 385 } 386 if (target.mWallpaperY >= 0) { 387 mLastWallpaperY = target.mWallpaperY; 388 } else if (changingTarget.mWallpaperY >= 0) { 389 mLastWallpaperY = changingTarget.mWallpaperY; 390 } 391 if (target.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) { 392 mLastWallpaperDisplayOffsetX = target.mWallpaperDisplayOffsetX; 393 } else if (changingTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) { 394 mLastWallpaperDisplayOffsetX = changingTarget.mWallpaperDisplayOffsetX; 395 } 396 if (target.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) { 397 mLastWallpaperDisplayOffsetY = target.mWallpaperDisplayOffsetY; 398 } else if (changingTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) { 399 mLastWallpaperDisplayOffsetY = changingTarget.mWallpaperDisplayOffsetY; 400 } 401 if (target.mWallpaperXStep >= 0) { 402 mLastWallpaperXStep = target.mWallpaperXStep; 403 } else if (changingTarget.mWallpaperXStep >= 0) { 404 mLastWallpaperXStep = changingTarget.mWallpaperXStep; 405 } 406 if (target.mWallpaperYStep >= 0) { 407 mLastWallpaperYStep = target.mWallpaperYStep; 408 } else if (changingTarget.mWallpaperYStep >= 0) { 409 mLastWallpaperYStep = changingTarget.mWallpaperYStep; 410 } 411 } 412 413 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) { 414 WindowList windows = mWallpaperTokens.get(curTokenNdx).windows; 415 for (int wallpaperNdx = windows.size() - 1; wallpaperNdx >= 0; wallpaperNdx--) { 416 WindowState wallpaper = windows.get(wallpaperNdx); 417 if (updateWallpaperOffset(wallpaper, dw, dh, sync)) { 418 WindowStateAnimator winAnimator = wallpaper.mWinAnimator; 419 winAnimator.computeShownFrameLocked(); 420 // No need to lay out the windows - we can just set the wallpaper position 421 // directly. 422 winAnimator.setWallpaperOffset(wallpaper.mShownFrame); 423 // We only want to be synchronous with one wallpaper. 424 sync = false; 425 } 426 } 427 } 428 } 429 430 void clearLastWallpaperTimeoutTime() { 431 mLastWallpaperTimeoutTime = 0; 432 } 433 434 void wallpaperCommandComplete(IBinder window) { 435 if (mWaitingOnWallpaper != null && 436 mWaitingOnWallpaper.mClient.asBinder() == window) { 437 mWaitingOnWallpaper = null; 438 mService.mWindowMap.notifyAll(); 439 } 440 } 441 442 void wallpaperOffsetsComplete(IBinder window) { 443 if (mWaitingOnWallpaper != null && 444 mWaitingOnWallpaper.mClient.asBinder() == window) { 445 mWaitingOnWallpaper = null; 446 mService.mWindowMap.notifyAll(); 447 } 448 } 449 450 int getAnimLayerAdjustment() { 451 return mWallpaperAnimLayerAdjustment; 452 } 453 454 void setAnimLayerAdjustment(WindowState win, int adj) { 455 if (win != mWallpaperTarget || mLowerWallpaperTarget != null) { 456 return; 457 } 458 459 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Setting wallpaper layer adj to " + adj); 460 mWallpaperAnimLayerAdjustment = adj; 461 for (int i = mWallpaperTokens.size() - 1; i >= 0; i--) { 462 WindowList windows = mWallpaperTokens.get(i).windows; 463 for (int j = windows.size() - 1; j >= 0; j--) { 464 WindowState wallpaper = windows.get(j); 465 wallpaper.mWinAnimator.mAnimLayer = wallpaper.mLayer + adj; 466 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "setWallpaper win " 467 + wallpaper + " anim layer: " + wallpaper.mWinAnimator.mAnimLayer); 468 } 469 } 470 } 471 472 private void findWallpaperTarget(WindowList windows, FindWallpaperTargetResult result) { 473 474 final WindowAnimator winAnimator = mService.mAnimator; 475 result.reset(); 476 WindowState w = null; 477 int windowDetachedI = -1; 478 boolean resetTopWallpaper = false; 479 boolean inFreeformSpace = false; 480 for (int i = windows.size() - 1; i >= 0; i--) { 481 w = windows.get(i); 482 if ((w.mAttrs.type == TYPE_WALLPAPER)) { 483 if (result.topWallpaper == null || resetTopWallpaper) { 484 result.setTopWallpaper(w, i); 485 resetTopWallpaper = false; 486 } 487 continue; 488 } 489 resetTopWallpaper = true; 490 if (w != winAnimator.mWindowDetachedWallpaper && w.mAppToken != null) { 491 // If this window's app token is hidden and not animating, 492 // it is of no interest to us. 493 if (w.mAppToken.hidden && w.mAppToken.mAppAnimator.animation == null) { 494 if (DEBUG_WALLPAPER) Slog.v(TAG, 495 "Skipping hidden and not animating token: " + w); 496 continue; 497 } 498 } 499 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win #" + i + " " + w + ": isOnScreen=" 500 + w.isOnScreen() + " mDrawState=" + w.mWinAnimator.mDrawState); 501 502 if (!inFreeformSpace) { 503 TaskStack stack = w.getStack(); 504 inFreeformSpace = stack != null && stack.mStackId == FREEFORM_WORKSPACE_STACK_ID; 505 } 506 507 // If the app is executing an animation because the keyguard is going away, 508 // keep the wallpaper during the animation so it doesn't flicker out. 509 final boolean hasWallpaper = (w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0 510 || (w.mAppToken != null && w.mWinAnimator.mKeyguardGoingAwayAnimation); 511 if (hasWallpaper && w.isOnScreen() 512 && (mWallpaperTarget == w || w.isDrawFinishedLw())) { 513 if (DEBUG_WALLPAPER) Slog.v(TAG, "Found wallpaper target: #" + i + "=" + w); 514 result.setWallpaperTarget(w, i); 515 if (w == mWallpaperTarget && w.mWinAnimator.isAnimating()) { 516 // The current wallpaper target is animating, so we'll look behind it for 517 // another possible target and figure out what is going on later. 518 if (DEBUG_WALLPAPER) Slog.v(TAG, 519 "Win " + w + ": token animating, looking behind."); 520 continue; 521 } 522 break; 523 } else if (w == winAnimator.mWindowDetachedWallpaper) { 524 windowDetachedI = i; 525 } 526 } 527 528 if (result.wallpaperTarget == null && windowDetachedI >= 0) { 529 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, 530 "Found animating detached wallpaper activity: #" + windowDetachedI + "=" + w); 531 result.setWallpaperTarget(w, windowDetachedI); 532 } 533 if (result.wallpaperTarget == null && inFreeformSpace) { 534 // In freeform mode we set the wallpaper as its own target, so we don't need an 535 // additional window to make it visible. 536 result.setWallpaperTarget(result.topWallpaper, result.topWallpaperIndex); 537 } 538 } 539 540 private boolean updateWallpaperWindowsTarget( 541 WindowList windows, FindWallpaperTargetResult result) { 542 543 boolean targetChanged = false; 544 WindowState wallpaperTarget = result.wallpaperTarget; 545 int wallpaperTargetIndex = result.wallpaperTargetIndex; 546 547 if (mWallpaperTarget != wallpaperTarget 548 && (mLowerWallpaperTarget == null || mLowerWallpaperTarget != wallpaperTarget)) { 549 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, 550 "New wallpaper target: " + wallpaperTarget + " oldTarget: " + mWallpaperTarget); 551 552 mLowerWallpaperTarget = null; 553 mUpperWallpaperTarget = null; 554 555 WindowState oldW = mWallpaperTarget; 556 mWallpaperTarget = wallpaperTarget; 557 targetChanged = true; 558 559 // Now what is happening... if the current and new targets are animating, 560 // then we are in our super special mode! 561 if (wallpaperTarget != null && oldW != null) { 562 boolean oldAnim = oldW.isAnimatingLw(); 563 boolean foundAnim = wallpaperTarget.isAnimatingLw(); 564 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, 565 "New animation: " + foundAnim + " old animation: " + oldAnim); 566 if (foundAnim && oldAnim) { 567 int oldI = windows.indexOf(oldW); 568 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, 569 "New i: " + wallpaperTargetIndex + " old i: " + oldI); 570 if (oldI >= 0) { 571 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, 572 "Animating wallpapers: old#" + oldI + "=" + oldW + "; new#" 573 + wallpaperTargetIndex + "=" + wallpaperTarget); 574 575 // Set the new target correctly. 576 if (wallpaperTarget.mAppToken != null 577 && wallpaperTarget.mAppToken.hiddenRequested) { 578 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, 579 "Old wallpaper still the target."); 580 mWallpaperTarget = oldW; 581 wallpaperTarget = oldW; 582 wallpaperTargetIndex = oldI; 583 } 584 // Now set the upper and lower wallpaper targets correctly, 585 // and make sure that we are positioning the wallpaper below the lower. 586 else if (wallpaperTargetIndex > oldI) { 587 // The new target is on top of the old one. 588 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, 589 "Found target above old target."); 590 mUpperWallpaperTarget = wallpaperTarget; 591 mLowerWallpaperTarget = oldW; 592 wallpaperTarget = oldW; 593 wallpaperTargetIndex = oldI; 594 } else { 595 // The new target is below the old one. 596 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, 597 "Found target below old target."); 598 mUpperWallpaperTarget = oldW; 599 mLowerWallpaperTarget = wallpaperTarget; 600 } 601 } 602 } 603 } 604 605 } else if (mLowerWallpaperTarget != null) { 606 // Is it time to stop animating? 607 if (!mLowerWallpaperTarget.isAnimatingLw() || !mUpperWallpaperTarget.isAnimatingLw()) { 608 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "No longer animating wallpaper targets!"); 609 mLowerWallpaperTarget = null; 610 mUpperWallpaperTarget = null; 611 mWallpaperTarget = wallpaperTarget; 612 targetChanged = true; 613 } 614 } 615 616 result.setWallpaperTarget(wallpaperTarget, wallpaperTargetIndex); 617 return targetChanged; 618 } 619 620 boolean updateWallpaperWindowsTargetByLayer( 621 WindowList windows, FindWallpaperTargetResult result) { 622 623 WindowState wallpaperTarget = result.wallpaperTarget; 624 int wallpaperTargetIndex = result.wallpaperTargetIndex; 625 boolean visible = wallpaperTarget != null; 626 627 if (visible) { 628 // The window is visible to the compositor...but is it visible to the user? 629 // That is what the wallpaper cares about. 630 visible = isWallpaperVisible(wallpaperTarget); 631 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible); 632 633 // If the wallpaper target is animating, we may need to copy its layer adjustment. 634 // Only do this if we are not transferring between two wallpaper targets. 635 mWallpaperAnimLayerAdjustment = 636 (mLowerWallpaperTarget == null && wallpaperTarget.mAppToken != null) 637 ? wallpaperTarget.mAppToken.mAppAnimator.animLayerAdjustment : 0; 638 639 final int maxLayer = (mService.mPolicy.getMaxWallpaperLayer() * TYPE_LAYER_MULTIPLIER) 640 + TYPE_LAYER_OFFSET; 641 642 // Now w is the window we are supposed to be behind... but we 643 // need to be sure to also be behind any of its attached windows, 644 // AND any starting window associated with it, AND below the 645 // maximum layer the policy allows for wallpapers. 646 while (wallpaperTargetIndex > 0) { 647 WindowState wb = windows.get(wallpaperTargetIndex - 1); 648 if (wb.mBaseLayer < maxLayer && 649 wb.mAttachedWindow != wallpaperTarget && 650 (wallpaperTarget.mAttachedWindow == null || 651 wb.mAttachedWindow != wallpaperTarget.mAttachedWindow) && 652 (wb.mAttrs.type != TYPE_APPLICATION_STARTING 653 || wallpaperTarget.mToken == null 654 || wb.mToken != wallpaperTarget.mToken)) { 655 // This window is not related to the previous one in any 656 // interesting way, so stop here. 657 break; 658 } 659 wallpaperTarget = wb; 660 wallpaperTargetIndex--; 661 } 662 } else { 663 if (DEBUG_WALLPAPER) Slog.v(TAG, "No wallpaper target"); 664 } 665 666 result.setWallpaperTarget(wallpaperTarget, wallpaperTargetIndex); 667 return visible; 668 } 669 670 boolean updateWallpaperWindowsPlacement(WindowList windows, 671 WindowState wallpaperTarget, int wallpaperTargetIndex, boolean visible) { 672 673 // TODO(multidisplay): Wallpapers on main screen only. 674 final DisplayInfo displayInfo = mService.getDefaultDisplayContentLocked().getDisplayInfo(); 675 final int dw = displayInfo.logicalWidth; 676 final int dh = displayInfo.logicalHeight; 677 678 // Start stepping backwards from here, ensuring that our wallpaper windows 679 // are correctly placed. 680 boolean changed = false; 681 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) { 682 WindowToken token = mWallpaperTokens.get(curTokenNdx); 683 if (token.hidden == visible) { 684 if (DEBUG_WALLPAPER_LIGHT) Slog.d(TAG, 685 "Wallpaper token " + token + " hidden=" + !visible); 686 token.hidden = !visible; 687 // Need to do a layout to ensure the wallpaper now has the correct size. 688 mService.getDefaultDisplayContentLocked().layoutNeeded = true; 689 } 690 691 final WindowList tokenWindows = token.windows; 692 for (int wallpaperNdx = tokenWindows.size() - 1; wallpaperNdx >= 0; wallpaperNdx--) { 693 WindowState wallpaper = tokenWindows.get(wallpaperNdx); 694 695 if (visible) { 696 updateWallpaperOffset(wallpaper, dw, dh, false); 697 } 698 699 // First, make sure the client has the current visibility state. 700 dispatchWallpaperVisibility(wallpaper, visible); 701 702 wallpaper.mWinAnimator.mAnimLayer = 703 wallpaper.mLayer + mWallpaperAnimLayerAdjustment; 704 if (DEBUG_LAYERS || DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "adjustWallpaper win " 705 + wallpaper + " anim layer: " + wallpaper.mWinAnimator.mAnimLayer); 706 707 // First, if this window is at the current index, then all is well. 708 if (wallpaper == wallpaperTarget) { 709 wallpaperTargetIndex--; 710 wallpaperTarget = wallpaperTargetIndex > 0 711 ? windows.get(wallpaperTargetIndex - 1) : null; 712 continue; 713 } 714 715 // The window didn't match... the current wallpaper window, 716 // wherever it is, is in the wrong place, so make sure it is not in the list. 717 int oldIndex = windows.indexOf(wallpaper); 718 if (oldIndex >= 0) { 719 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, 720 "Wallpaper removing at " + oldIndex + ": " + wallpaper); 721 windows.remove(oldIndex); 722 mService.mWindowsChanged = true; 723 if (oldIndex < wallpaperTargetIndex) { 724 wallpaperTargetIndex--; 725 } 726 } 727 728 // Now stick it in. For apps over wallpaper keep the wallpaper at the bottommost 729 // layer. For keyguard over wallpaper put the wallpaper under the keyguard. 730 int insertionIndex = 0; 731 if (visible && wallpaperTarget != null) { 732 final int type = wallpaperTarget.mAttrs.type; 733 final int privateFlags = wallpaperTarget.mAttrs.privateFlags; 734 if ((privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 735 || type == TYPE_KEYGUARD_SCRIM) { 736 insertionIndex = windows.indexOf(wallpaperTarget); 737 } 738 } 739 if (DEBUG_WALLPAPER_LIGHT || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG, 740 "Moving wallpaper " + wallpaper 741 + " from " + oldIndex + " to " + insertionIndex); 742 743 windows.add(insertionIndex, wallpaper); 744 mService.mWindowsChanged = true; 745 changed = true; 746 } 747 } 748 749 return changed; 750 } 751 752 boolean adjustWallpaperWindows() { 753 mService.mInnerFields.mWallpaperMayChange = false; 754 755 final WindowList windows = mService.getDefaultWindowListLocked(); 756 // First find top-most window that has asked to be on top of the wallpaper; 757 // all wallpapers go behind it. 758 findWallpaperTarget(windows, mFindResults); 759 final boolean targetChanged = updateWallpaperWindowsTarget(windows, mFindResults); 760 final boolean visible = updateWallpaperWindowsTargetByLayer(windows, mFindResults); 761 WindowState wallpaperTarget = mFindResults.wallpaperTarget; 762 int wallpaperTargetIndex = mFindResults.wallpaperTargetIndex; 763 764 if (wallpaperTarget == null && mFindResults.topWallpaper != null) { 765 // There is no wallpaper target, so it goes at the bottom. 766 // We will assume it is the same place as last time, if known. 767 wallpaperTarget = mFindResults.topWallpaper; 768 wallpaperTargetIndex = mFindResults.topWallpaperIndex + 1; 769 } else { 770 // Okay i is the position immediately above the wallpaper. 771 // Look at what is below it for later. 772 wallpaperTarget = wallpaperTargetIndex > 0 773 ? windows.get(wallpaperTargetIndex - 1) : null; 774 } 775 776 if (visible) { 777 if (mWallpaperTarget.mWallpaperX >= 0) { 778 mLastWallpaperX = mWallpaperTarget.mWallpaperX; 779 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep; 780 } 781 if (mWallpaperTarget.mWallpaperY >= 0) { 782 mLastWallpaperY = mWallpaperTarget.mWallpaperY; 783 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep; 784 } 785 if (mWallpaperTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) { 786 mLastWallpaperDisplayOffsetX = mWallpaperTarget.mWallpaperDisplayOffsetX; 787 } 788 if (mWallpaperTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) { 789 mLastWallpaperDisplayOffsetY = mWallpaperTarget.mWallpaperDisplayOffsetY; 790 } 791 } 792 793 final boolean changed = updateWallpaperWindowsPlacement( 794 windows, wallpaperTarget, wallpaperTargetIndex, visible); 795 796 if (targetChanged && DEBUG_WALLPAPER_LIGHT) Slog.d(TAG, "New wallpaper: target=" 797 + mWallpaperTarget + " lower=" + mLowerWallpaperTarget + " upper=" 798 + mUpperWallpaperTarget); 799 800 return changed; 801 } 802 803 boolean processWallpaperDrawPendingTimeout() { 804 if (mWallpaperDrawState == WALLPAPER_DRAW_PENDING) { 805 mWallpaperDrawState = WALLPAPER_DRAW_TIMEOUT; 806 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG, 807 "*** WALLPAPER DRAW TIMEOUT"); 808 return true; 809 } 810 return false; 811 } 812 813 boolean wallpaperTransitionReady() { 814 boolean transitionReady = true; 815 boolean wallpaperReady = true; 816 for (int curTokenIndex = mWallpaperTokens.size() - 1; 817 curTokenIndex >= 0 && wallpaperReady; curTokenIndex--) { 818 WindowToken token = mWallpaperTokens.get(curTokenIndex); 819 for (int curWallpaperIndex = token.windows.size() - 1; curWallpaperIndex >= 0; 820 curWallpaperIndex--) { 821 WindowState wallpaper = token.windows.get(curWallpaperIndex); 822 if (wallpaper.mWallpaperVisible && !wallpaper.isDrawnLw()) { 823 // We've told this wallpaper to be visible, but it is not drawn yet 824 wallpaperReady = false; 825 if (mWallpaperDrawState != WALLPAPER_DRAW_TIMEOUT) { 826 // wait for this wallpaper until it is drawn or timeout 827 transitionReady = false; 828 } 829 if (mWallpaperDrawState == WALLPAPER_DRAW_NORMAL) { 830 mWallpaperDrawState = WALLPAPER_DRAW_PENDING; 831 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT); 832 mService.mH.sendEmptyMessageDelayed(WALLPAPER_DRAW_PENDING_TIMEOUT, 833 WALLPAPER_DRAW_PENDING_TIMEOUT_DURATION); 834 } 835 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG, 836 "Wallpaper should be visible but has not been drawn yet. " + 837 "mWallpaperDrawState=" + mWallpaperDrawState); 838 break; 839 } 840 } 841 } 842 if (wallpaperReady) { 843 mWallpaperDrawState = WALLPAPER_DRAW_NORMAL; 844 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT); 845 } 846 847 return transitionReady; 848 } 849 850 void addWallpaperToken(WindowToken token) { 851 mWallpaperTokens.add(token); 852 } 853 854 void removeWallpaperToken(WindowToken token) { 855 mWallpaperTokens.remove(token); 856 } 857 858 void dump(PrintWriter pw, String prefix) { 859 pw.print(prefix); pw.print("mWallpaperTarget="); pw.println(mWallpaperTarget); 860 if (mLowerWallpaperTarget != null || mUpperWallpaperTarget != null) { 861 pw.print(prefix); pw.print("mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget); 862 pw.print(prefix); pw.print("mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget); 863 } 864 pw.print(prefix); pw.print("mLastWallpaperX="); pw.print(mLastWallpaperX); 865 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY); 866 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE 867 || mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) { 868 pw.print(prefix); 869 pw.print("mLastWallpaperDisplayOffsetX="); pw.print(mLastWallpaperDisplayOffsetX); 870 pw.print(" mLastWallpaperDisplayOffsetY="); pw.println(mLastWallpaperDisplayOffsetY); 871 } 872 } 873 874 void dumpTokens(PrintWriter pw, String prefix, boolean dumpAll) { 875 if (!mWallpaperTokens.isEmpty()) { 876 pw.println(); 877 pw.print(prefix); pw.println("Wallpaper tokens:"); 878 for (int i = mWallpaperTokens.size() - 1; i >= 0; i--) { 879 WindowToken token = mWallpaperTokens.get(i); 880 pw.print(prefix); pw.print("Wallpaper #"); pw.print(i); 881 pw.print(' '); pw.print(token); 882 if (dumpAll) { 883 pw.println(':'); 884 token.dump(pw, " "); 885 } else { 886 pw.println(); 887 } 888 } 889 } 890 } 891 892 /** Helper class for storing the results of a wallpaper target find operation. */ 893 final private static class FindWallpaperTargetResult { 894 int topWallpaperIndex = 0; 895 WindowState topWallpaper = null; 896 int wallpaperTargetIndex = 0; 897 WindowState wallpaperTarget = null; 898 899 void setTopWallpaper(WindowState win, int index) { 900 topWallpaper = win; 901 topWallpaperIndex = index; 902 } 903 904 void setWallpaperTarget(WindowState win, int index) { 905 wallpaperTarget = win; 906 wallpaperTargetIndex = index; 907 } 908 909 void reset() { 910 topWallpaperIndex = 0; 911 topWallpaper = null; 912 wallpaperTargetIndex = 0; 913 wallpaperTarget = null; 914 } 915 } 916} 917