blob: f2ad6fb7a888087f834c4a97e82ae2fb0c004ddc [file] [log] [blame]
Wale Ogunwalee8069dc2015-08-18 09:52:01 -07001/*
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
Wale Ogunwale1e129a42016-11-21 13:03:47 -080019import com.android.internal.util.ToBooleanFunction;
20
Wale Ogunwale44f036f2017-09-29 05:09:09 -070021import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -080022import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070023import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Jorim Jaggife762342016-10-13 14:33:27 +020024import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070025import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Jorim Jaggife762342016-10-13 14:33:27 +020026
Adrian Roose99bc052017-11-20 17:55:31 +010027import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010028import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080029import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080030import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
31import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
32import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
33import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070034import static com.android.server.wm.WindowManagerService.H.WALLPAPER_DRAW_PENDING_TIMEOUT;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070035
36import android.os.Bundle;
37import android.os.Debug;
38import android.os.IBinder;
39import android.os.RemoteException;
40import android.os.SystemClock;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -070041import android.util.ArraySet;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070042import android.util.Slog;
43import android.view.DisplayInfo;
44import android.view.WindowManager;
Jorim Jaggife762342016-10-13 14:33:27 +020045import android.view.animation.Animation;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070046
47import java.io.PrintWriter;
48import java.util.ArrayList;
49
50/**
51 * Controls wallpaper windows visibility, ordering, and so on.
52 * NOTE: All methods in this class must be called with the window manager service lock held.
53 */
54class WallpaperController {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080055 private static final String TAG = TAG_WITH_CLASS_NAME ? "WallpaperController" : TAG_WM;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080056 private WindowManagerService mService;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070057
Jorim Jaggi879ff722016-11-04 18:08:17 -070058 private final ArrayList<WallpaperWindowToken> mWallpaperTokens = new ArrayList<>();
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070059
60 // If non-null, this is the currently visible window that is associated
61 // with the wallpaper.
62 private WindowState mWallpaperTarget = null;
63 // If non-null, we are in the middle of animating from one wallpaper target
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -080064 // to another, and this is the previous wallpaper target.
65 private WindowState mPrevWallpaperTarget = null;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070066
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070067 private float mLastWallpaperX = -1;
68 private float mLastWallpaperY = -1;
69 private float mLastWallpaperXStep = -1;
70 private float mLastWallpaperYStep = -1;
71 private int mLastWallpaperDisplayOffsetX = Integer.MIN_VALUE;
72 private int mLastWallpaperDisplayOffsetY = Integer.MIN_VALUE;
73
74 // This is set when we are waiting for a wallpaper to tell us it is done
75 // changing its scroll position.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -080076 private WindowState mWaitingOnWallpaper;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070077
78 // The last time we had a timeout when waiting for a wallpaper.
79 private long mLastWallpaperTimeoutTime;
80 // We give a wallpaper up to 150ms to finish scrolling.
81 private static final long WALLPAPER_TIMEOUT = 150;
82 // Time we wait after a timeout before trying to wait again.
83 private static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
84
85 // Set to the wallpaper window we would like to hide once the transition animations are done.
86 // This is useful in cases where we don't want the wallpaper to be hidden when the close app
87 // is a wallpaper target and is done animating out, but the opening app isn't a wallpaper
88 // target and isn't done animating in.
Wale Ogunwalee4da0c12016-07-29 12:47:02 -070089 WindowState mDeferredHideWallpaper = null;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070090
91 // We give a wallpaper up to 500ms to finish drawing before playing app transitions.
92 private static final long WALLPAPER_DRAW_PENDING_TIMEOUT_DURATION = 500;
93 private static final int WALLPAPER_DRAW_NORMAL = 0;
94 private static final int WALLPAPER_DRAW_PENDING = 1;
95 private static final int WALLPAPER_DRAW_TIMEOUT = 2;
96 private int mWallpaperDrawState = WALLPAPER_DRAW_NORMAL;
97
Wale Ogunwale5f61d042015-08-20 10:09:47 -070098 private final FindWallpaperTargetResult mFindResults = new FindWallpaperTargetResult();
99
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800100 private final ToBooleanFunction<WindowState> mFindWallpaperTargetFunction = w -> {
101 final WindowAnimator winAnimator = mService.mAnimator;
102 if ((w.mAttrs.type == TYPE_WALLPAPER)) {
103 if (mFindResults.topWallpaper == null || mFindResults.resetTopWallpaper) {
104 mFindResults.setTopWallpaper(w);
105 mFindResults.resetTopWallpaper = false;
106 }
107 return false;
108 }
109
110 mFindResults.resetTopWallpaper = true;
111 if (w != winAnimator.mWindowDetachedWallpaper && w.mAppToken != null) {
112 // If this window's app token is hidden and not animating, it is of no interest to us.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200113 if (w.mAppToken.isHidden() && !w.mAppToken.isSelfAnimating()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800114 if (DEBUG_WALLPAPER) Slog.v(TAG,
115 "Skipping hidden and not animating token: " + w);
116 return false;
117 }
118 }
119 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w + ": isOnScreen=" + w.isOnScreen()
120 + " mDrawState=" + w.mWinAnimator.mDrawState);
121
122 if (w.mWillReplaceWindow && mWallpaperTarget == null
123 && !mFindResults.useTopWallpaperAsTarget) {
124 // When we are replacing a window and there was wallpaper before replacement, we want to
125 // keep the window until the new windows fully appear and can determine the visibility,
126 // to avoid flickering.
127 mFindResults.setUseTopWallpaperAsTarget(true);
128 }
129
130 final boolean keyguardGoingAwayWithWallpaper = (w.mAppToken != null
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200131 && w.mAppToken.isSelfAnimating()
132 && AppTransition.isKeyguardGoingAwayTransit(w.mAppToken.getTransit())
133 && (w.mAppToken.getTransitFlags()
134 & TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER) != 0);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800135
136 boolean needsShowWhenLockedWallpaper = false;
137 if ((w.mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0
138 && mService.mPolicy.isKeyguardLocked()
139 && mService.mPolicy.isKeyguardOccluded()) {
140 // The lowest show when locked window decides whether we need to put the wallpaper
141 // behind.
142 needsShowWhenLockedWallpaper = !isFullscreen(w.mAttrs)
143 || (w.mAppToken != null && !w.mAppToken.fillsParent());
144 }
145
146 if (keyguardGoingAwayWithWallpaper || needsShowWhenLockedWallpaper) {
147 // Keep the wallpaper during Keyguard exit but also when it's needed for a
148 // non-fullscreen show when locked activity.
149 mFindResults.setUseTopWallpaperAsTarget(true);
150 }
151
Winson Chunge2d72172018-01-25 17:46:20 +0000152 final RecentsAnimationController recentsAnimationController =
153 mService.getRecentsAnimationController();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800154 final boolean hasWallpaper = (w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
Winson Chunge2d72172018-01-25 17:46:20 +0000155 final boolean isRecentsTransitionTarget = (recentsAnimationController != null
156 && recentsAnimationController.isWallpaperVisible(w));
157 if (isRecentsTransitionTarget) {
158 if (DEBUG_WALLPAPER) Slog.v(TAG, "Found recents animation wallpaper target: " + w);
159 mFindResults.setWallpaperTarget(w);
160 return true;
161 } else if (hasWallpaper && w.isOnScreen()
162 && (mWallpaperTarget == w || w.isDrawFinishedLw())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800163 if (DEBUG_WALLPAPER) Slog.v(TAG, "Found wallpaper target: " + w);
164 mFindResults.setWallpaperTarget(w);
165 if (w == mWallpaperTarget && w.mWinAnimator.isAnimationSet()) {
166 // The current wallpaper target is animating, so we'll look behind it for
167 // another possible target and figure out what is going on later.
168 if (DEBUG_WALLPAPER) Slog.v(TAG,
169 "Win " + w + ": token animating, looking behind.");
170 }
171 // Found a target! End search.
172 return true;
173 } else if (w == winAnimator.mWindowDetachedWallpaper) {
174 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
175 "Found animating detached wallpaper target win: " + w);
176 mFindResults.setUseTopWallpaperAsTarget(true);
177 }
178 return false;
179 };
180
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700181 public WallpaperController(WindowManagerService service) {
182 mService = service;
183 }
184
185 WindowState getWallpaperTarget() {
186 return mWallpaperTarget;
187 }
188
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700189 boolean isWallpaperTarget(WindowState win) {
190 return win == mWallpaperTarget;
191 }
192
193 boolean isBelowWallpaperTarget(WindowState win) {
194 return mWallpaperTarget != null && mWallpaperTarget.mLayer >= win.mBaseLayer;
195 }
196
197 boolean isWallpaperVisible() {
198 return isWallpaperVisible(mWallpaperTarget);
199 }
200
Jorim Jaggife762342016-10-13 14:33:27 +0200201 /**
202 * Starts {@param a} on all wallpaper windows.
203 */
204 void startWallpaperAnimation(Animation a) {
205 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700206 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Jorim Jaggife762342016-10-13 14:33:27 +0200207 token.startAnimation(a);
208 }
209 }
210
Winson Chunge2d72172018-01-25 17:46:20 +0000211 private final boolean isWallpaperVisible(WindowState wallpaperTarget) {
212 final RecentsAnimationController recentsAnimationController =
213 mService.getRecentsAnimationController();
214 boolean isAnimatingWithRecentsComponent = recentsAnimationController != null
215 && recentsAnimationController.isWallpaperVisible(wallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700216 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper vis: target " + wallpaperTarget + ", obscured="
217 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200218 + " animating=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
219 ? wallpaperTarget.mAppToken.isSelfAnimating() : null)
Winson Chunge2d72172018-01-25 17:46:20 +0000220 + " prev=" + mPrevWallpaperTarget
221 + " recentsAnimationWallpaperVisible=" + isAnimatingWithRecentsComponent);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700222 return (wallpaperTarget != null
Winson Chunge2d72172018-01-25 17:46:20 +0000223 && (!wallpaperTarget.mObscured
224 || isAnimatingWithRecentsComponent
225 || (wallpaperTarget.mAppToken != null
226 && wallpaperTarget.mAppToken.isSelfAnimating())))
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800227 || mPrevWallpaperTarget != null;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700228 }
229
230 boolean isWallpaperTargetAnimating() {
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700231 return mWallpaperTarget != null && mWallpaperTarget.mWinAnimator.isAnimationSet()
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200232 && (mWallpaperTarget.mAppToken == null
233 || !mWallpaperTarget.mAppToken.isWaitingForTransitionStart());
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700234 }
235
236 void updateWallpaperVisibility() {
Filip Gruszczynski63a35e22015-11-05 15:38:59 -0800237 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700238
239 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700240 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700241 token.updateWallpaperVisibility(visible);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700242 }
243 }
244
245 void hideDeferredWallpapersIfNeeded() {
246 if (mDeferredHideWallpaper != null) {
247 hideWallpapers(mDeferredHideWallpaper);
248 mDeferredHideWallpaper = null;
249 }
250 }
251
252 void hideWallpapers(final WindowState winGoingAway) {
253 if (mWallpaperTarget != null
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800254 && (mWallpaperTarget != winGoingAway || mPrevWallpaperTarget != null)) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700255 return;
256 }
257 if (mService.mAppTransition.isRunning()) {
258 // Defer hiding the wallpaper when app transition is running until the animations
259 // are done.
260 mDeferredHideWallpaper = winGoingAway;
261 return;
262 }
263
264 final boolean wasDeferred = (mDeferredHideWallpaper == winGoingAway);
265 for (int i = mWallpaperTokens.size() - 1; i >= 0; i--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700266 final WallpaperWindowToken token = mWallpaperTokens.get(i);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700267 token.hideWallpaperToken(wasDeferred, "hideWallpapers");
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200268 if (DEBUG_WALLPAPER_LIGHT && !token.isHidden()) Slog.d(TAG, "Hiding wallpaper " + token
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800269 + " from " + winGoingAway + " target=" + mWallpaperTarget + " prev="
270 + mPrevWallpaperTarget + "\n" + Debug.getCallers(5, " "));
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700271 }
272 }
273
274 boolean updateWallpaperOffset(WindowState wallpaperWin, int dw, int dh, boolean sync) {
275 boolean rawChanged = false;
Winson4b4ba902016-07-27 19:45:52 -0700276 // Set the default wallpaper x-offset to either edge of the screen (depending on RTL), to
277 // match the behavior of most Launchers
278 float defaultWallpaperX = wallpaperWin.isRtl() ? 1f : 0f;
279 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : defaultWallpaperX;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700280 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
281 int availw = wallpaperWin.mFrame.right - wallpaperWin.mFrame.left - dw;
282 int offset = availw > 0 ? -(int)(availw * wpx + .5f) : 0;
283 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
284 offset += mLastWallpaperDisplayOffsetX;
285 }
286 boolean changed = wallpaperWin.mXOffset != offset;
287 if (changed) {
288 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper " + wallpaperWin + " x: " + offset);
289 wallpaperWin.mXOffset = offset;
290 }
291 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
292 wallpaperWin.mWallpaperX = wpx;
293 wallpaperWin.mWallpaperXStep = wpxs;
294 rawChanged = true;
295 }
296
297 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
298 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
299 int availh = wallpaperWin.mFrame.bottom - wallpaperWin.mFrame.top - dh;
300 offset = availh > 0 ? -(int)(availh * wpy + .5f) : 0;
301 if (mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
302 offset += mLastWallpaperDisplayOffsetY;
303 }
304 if (wallpaperWin.mYOffset != offset) {
305 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper " + wallpaperWin + " y: " + offset);
306 changed = true;
307 wallpaperWin.mYOffset = offset;
308 }
309 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
310 wallpaperWin.mWallpaperY = wpy;
311 wallpaperWin.mWallpaperYStep = wpys;
312 rawChanged = true;
313 }
314
315 if (rawChanged && (wallpaperWin.mAttrs.privateFlags &
316 WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS) != 0) {
317 try {
318 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
319 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
320 + " y=" + wallpaperWin.mWallpaperY);
321 if (sync) {
322 mWaitingOnWallpaper = wallpaperWin;
323 }
324 wallpaperWin.mClient.dispatchWallpaperOffsets(
325 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
326 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
327 if (sync) {
328 if (mWaitingOnWallpaper != null) {
329 long start = SystemClock.uptimeMillis();
330 if ((mLastWallpaperTimeoutTime + WALLPAPER_TIMEOUT_RECOVERY)
331 < start) {
332 try {
333 if (DEBUG_WALLPAPER) Slog.v(TAG,
334 "Waiting for offset complete...");
335 mService.mWindowMap.wait(WALLPAPER_TIMEOUT);
336 } catch (InterruptedException e) {
337 }
338 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
339 if ((start + WALLPAPER_TIMEOUT) < SystemClock.uptimeMillis()) {
340 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
341 + wallpaperWin);
342 mLastWallpaperTimeoutTime = start;
343 }
344 }
345 mWaitingOnWallpaper = null;
346 }
347 }
348 } catch (RemoteException e) {
349 }
350 }
351
352 return changed;
353 }
354
355 void setWindowWallpaperPosition(
356 WindowState window, float x, float y, float xStep, float yStep) {
357 if (window.mWallpaperX != x || window.mWallpaperY != y) {
358 window.mWallpaperX = x;
359 window.mWallpaperY = y;
360 window.mWallpaperXStep = xStep;
361 window.mWallpaperYStep = yStep;
362 updateWallpaperOffsetLocked(window, true);
363 }
364 }
365
366 void setWindowWallpaperDisplayOffset(WindowState window, int x, int y) {
367 if (window.mWallpaperDisplayOffsetX != x || window.mWallpaperDisplayOffsetY != y) {
368 window.mWallpaperDisplayOffsetX = x;
369 window.mWallpaperDisplayOffsetY = y;
370 updateWallpaperOffsetLocked(window, true);
371 }
372 }
373
374 Bundle sendWindowWallpaperCommand(
375 WindowState window, String action, int x, int y, int z, Bundle extras, boolean sync) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800376 if (window == mWallpaperTarget || window == mPrevWallpaperTarget) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700377 boolean doWait = sync;
378 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700379 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700380 token.sendWindowWallpaperCommand(action, x, y, z, extras, sync);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700381 }
382
383 if (doWait) {
384 // TODO: Need to wait for result.
385 }
386 }
387
388 return null;
389 }
390
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700391 private void updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700392 final DisplayContent displayContent = changingTarget.getDisplayContent();
393 if (displayContent == null) {
394 return;
395 }
396 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
397 final int dw = displayInfo.logicalWidth;
398 final int dh = displayInfo.logicalHeight;
399
400 WindowState target = mWallpaperTarget;
401 if (target != null) {
402 if (target.mWallpaperX >= 0) {
403 mLastWallpaperX = target.mWallpaperX;
404 } else if (changingTarget.mWallpaperX >= 0) {
405 mLastWallpaperX = changingTarget.mWallpaperX;
406 }
407 if (target.mWallpaperY >= 0) {
408 mLastWallpaperY = target.mWallpaperY;
409 } else if (changingTarget.mWallpaperY >= 0) {
410 mLastWallpaperY = changingTarget.mWallpaperY;
411 }
412 if (target.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
413 mLastWallpaperDisplayOffsetX = target.mWallpaperDisplayOffsetX;
414 } else if (changingTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
415 mLastWallpaperDisplayOffsetX = changingTarget.mWallpaperDisplayOffsetX;
416 }
417 if (target.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
418 mLastWallpaperDisplayOffsetY = target.mWallpaperDisplayOffsetY;
419 } else if (changingTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
420 mLastWallpaperDisplayOffsetY = changingTarget.mWallpaperDisplayOffsetY;
421 }
422 if (target.mWallpaperXStep >= 0) {
423 mLastWallpaperXStep = target.mWallpaperXStep;
424 } else if (changingTarget.mWallpaperXStep >= 0) {
425 mLastWallpaperXStep = changingTarget.mWallpaperXStep;
426 }
427 if (target.mWallpaperYStep >= 0) {
428 mLastWallpaperYStep = target.mWallpaperYStep;
429 } else if (changingTarget.mWallpaperYStep >= 0) {
430 mLastWallpaperYStep = changingTarget.mWallpaperYStep;
431 }
432 }
433
434 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700435 mWallpaperTokens.get(curTokenNdx).updateWallpaperOffset(dw, dh, sync);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700436 }
437 }
438
439 void clearLastWallpaperTimeoutTime() {
440 mLastWallpaperTimeoutTime = 0;
441 }
442
443 void wallpaperCommandComplete(IBinder window) {
444 if (mWaitingOnWallpaper != null &&
445 mWaitingOnWallpaper.mClient.asBinder() == window) {
446 mWaitingOnWallpaper = null;
447 mService.mWindowMap.notifyAll();
448 }
449 }
450
451 void wallpaperOffsetsComplete(IBinder window) {
452 if (mWaitingOnWallpaper != null &&
453 mWaitingOnWallpaper.mClient.asBinder() == window) {
454 mWaitingOnWallpaper = null;
455 mService.mWindowMap.notifyAll();
456 }
457 }
458
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800459 private void findWallpaperTarget(DisplayContent dc) {
460 mFindResults.reset();
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700461 if (dc.isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800462 // In freeform mode we set the wallpaper as its own target, so we don't need an
463 // additional window to make it visible.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800464 mFindResults.setUseTopWallpaperAsTarget(true);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800465 }
Wale Ogunwale6cbba702016-06-28 16:27:31 -0700466
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800467 dc.forAllWindows(mFindWallpaperTargetFunction, true /* traverseTopToBottom */);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800468
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800469 if (mFindResults.wallpaperTarget == null && mFindResults.useTopWallpaperAsTarget) {
470 mFindResults.setWallpaperTarget(mFindResults.topWallpaper);
Wale Ogunwale21fdd912015-08-20 12:34:57 -0700471 }
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700472 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700473
Jorim Jaggife762342016-10-13 14:33:27 +0200474 private boolean isFullscreen(WindowManager.LayoutParams attrs) {
475 return attrs.x == 0 && attrs.y == 0
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800476 && attrs.width == MATCH_PARENT && attrs.height == MATCH_PARENT;
Jorim Jaggife762342016-10-13 14:33:27 +0200477 }
478
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700479 /** Updates the target wallpaper if needed and returns true if an update happened. */
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800480 private void updateWallpaperWindowsTarget(DisplayContent dc,
481 FindWallpaperTargetResult result) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700482
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700483 WindowState wallpaperTarget = result.wallpaperTarget;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700484
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700485 if (mWallpaperTarget == wallpaperTarget
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800486 || (mPrevWallpaperTarget != null && mPrevWallpaperTarget == wallpaperTarget)) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700487
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800488 if (mPrevWallpaperTarget == null) {
489 return;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700490 }
491
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800492 // Is it time to stop animating?
493 if (!mPrevWallpaperTarget.isAnimatingLw()) {
494 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "No longer animating wallpaper targets!");
495 mPrevWallpaperTarget = null;
496 mWallpaperTarget = wallpaperTarget;
497 }
498 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700499 }
500
501 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800502 "New wallpaper target: " + wallpaperTarget + " prevTarget: " + mWallpaperTarget);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700503
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800504 mPrevWallpaperTarget = null;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700505
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800506 final WindowState prevWallpaperTarget = mWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700507 mWallpaperTarget = wallpaperTarget;
508
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800509 if (wallpaperTarget == null || prevWallpaperTarget == null) {
510 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700511 }
512
513 // Now what is happening... if the current and new targets are animating,
514 // then we are in our super special mode!
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800515 boolean oldAnim = prevWallpaperTarget.isAnimatingLw();
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700516 boolean foundAnim = wallpaperTarget.isAnimatingLw();
517 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
518 "New animation: " + foundAnim + " old animation: " + oldAnim);
519
520 if (!foundAnim || !oldAnim) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800521 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700522 }
523
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800524 if (dc.getWindow(w -> w == prevWallpaperTarget) == null) {
525 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700526 }
527
528 final boolean newTargetHidden = wallpaperTarget.mAppToken != null
529 && wallpaperTarget.mAppToken.hiddenRequested;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800530 final boolean oldTargetHidden = prevWallpaperTarget.mAppToken != null
531 && prevWallpaperTarget.mAppToken.hiddenRequested;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700532
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800533 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Animating wallpapers:" + " old: "
534 + prevWallpaperTarget + " hidden=" + oldTargetHidden + " new: " + wallpaperTarget
535 + " hidden=" + newTargetHidden);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700536
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800537 mPrevWallpaperTarget = prevWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700538
539 if (newTargetHidden && !oldTargetHidden) {
540 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Old wallpaper still the target.");
541 // Use the old target if new target is hidden but old target
542 // is not. If they're both hidden, still use the new target.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800543 mWallpaperTarget = prevWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700544 } else if (newTargetHidden == oldTargetHidden
545 && !mService.mOpeningApps.contains(wallpaperTarget.mAppToken)
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800546 && (mService.mOpeningApps.contains(prevWallpaperTarget.mAppToken)
547 || mService.mClosingApps.contains(prevWallpaperTarget.mAppToken))) {
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700548 // If they're both hidden (or both not hidden), prefer the one that's currently in
549 // opening or closing app list, this allows transition selection logic to better
550 // determine the wallpaper status of opening/closing apps.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800551 mWallpaperTarget = prevWallpaperTarget;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700552 }
553
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800554 result.setWallpaperTarget(wallpaperTarget);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700555 }
556
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800557 private void updateWallpaperTokens(boolean visible) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700558 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700559 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000560 token.updateWallpaperWindows(visible);
Robert Carrdee1b3f2017-02-27 11:33:33 -0800561 token.getDisplayContent().assignWindowLayers(false);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700562 }
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700563 }
564
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800565 void adjustWallpaperWindows(DisplayContent dc) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700566 mService.mRoot.mWallpaperMayChange = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700567
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700568 // First find top-most window that has asked to be on top of the wallpaper;
569 // all wallpapers go behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800570 findWallpaperTarget(dc);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800571 updateWallpaperWindowsTarget(dc, mFindResults);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700572
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800573 // The window is visible to the compositor...but is it visible to the user?
574 // That is what the wallpaper cares about.
575 final boolean visible = mWallpaperTarget != null && isWallpaperVisible(mWallpaperTarget);
576 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700577
578 if (visible) {
579 if (mWallpaperTarget.mWallpaperX >= 0) {
580 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
581 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
582 }
583 if (mWallpaperTarget.mWallpaperY >= 0) {
584 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
585 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
586 }
587 if (mWallpaperTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
588 mLastWallpaperDisplayOffsetX = mWallpaperTarget.mWallpaperDisplayOffsetX;
589 }
590 if (mWallpaperTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
591 mLastWallpaperDisplayOffsetY = mWallpaperTarget.mWallpaperDisplayOffsetY;
592 }
593 }
594
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800595 updateWallpaperTokens(visible);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700596
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800597 if (DEBUG_WALLPAPER_LIGHT) Slog.d(TAG, "New wallpaper: target=" + mWallpaperTarget
598 + " prev=" + mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700599 }
600
601 boolean processWallpaperDrawPendingTimeout() {
602 if (mWallpaperDrawState == WALLPAPER_DRAW_PENDING) {
603 mWallpaperDrawState = WALLPAPER_DRAW_TIMEOUT;
604 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG,
605 "*** WALLPAPER DRAW TIMEOUT");
Winson Chunge2d72172018-01-25 17:46:20 +0000606
607 // If there was a recents animation in progress, cancel that animation
608 if (mService.getRecentsAnimationController() != null) {
609 mService.getRecentsAnimationController().cancelAnimation();
610 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700611 return true;
612 }
613 return false;
614 }
615
616 boolean wallpaperTransitionReady() {
617 boolean transitionReady = true;
618 boolean wallpaperReady = true;
619 for (int curTokenIndex = mWallpaperTokens.size() - 1;
620 curTokenIndex >= 0 && wallpaperReady; curTokenIndex--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700621 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenIndex);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700622 if (token.hasVisibleNotDrawnWallpaper()) {
623 // We've told this wallpaper to be visible, but it is not drawn yet
624 wallpaperReady = false;
625 if (mWallpaperDrawState != WALLPAPER_DRAW_TIMEOUT) {
626 // wait for this wallpaper until it is drawn or timeout
627 transitionReady = false;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700628 }
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700629 if (mWallpaperDrawState == WALLPAPER_DRAW_NORMAL) {
630 mWallpaperDrawState = WALLPAPER_DRAW_PENDING;
631 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT);
632 mService.mH.sendEmptyMessageDelayed(WALLPAPER_DRAW_PENDING_TIMEOUT,
633 WALLPAPER_DRAW_PENDING_TIMEOUT_DURATION);
634 }
635 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG,
636 "Wallpaper should be visible but has not been drawn yet. " +
637 "mWallpaperDrawState=" + mWallpaperDrawState);
638 break;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700639 }
640 }
641 if (wallpaperReady) {
642 mWallpaperDrawState = WALLPAPER_DRAW_NORMAL;
643 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT);
644 }
645
646 return transitionReady;
647 }
648
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700649 /**
650 * Adjusts the wallpaper windows if the input display has a pending wallpaper layout or one of
651 * the opening apps should be a wallpaper target.
652 */
Wale Ogunwale0303c572016-10-20 10:16:29 -0700653 void adjustWallpaperWindowsForAppTransitionIfNeeded(DisplayContent dc,
654 ArraySet<AppWindowToken> openingApps) {
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700655 boolean adjust = false;
656 if ((dc.pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
657 adjust = true;
658 } else {
659 for (int i = openingApps.size() - 1; i >= 0; --i) {
660 final AppWindowToken token = openingApps.valueAt(i);
661 if (token.windowsCanBeWallpaperTarget()) {
662 adjust = true;
663 break;
664 }
665 }
666 }
667
Wale Ogunwale0303c572016-10-20 10:16:29 -0700668 if (adjust) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800669 adjustWallpaperWindows(dc);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700670 }
671 }
672
Jorim Jaggi879ff722016-11-04 18:08:17 -0700673 void addWallpaperToken(WallpaperWindowToken token) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700674 mWallpaperTokens.add(token);
675 }
676
Jorim Jaggi879ff722016-11-04 18:08:17 -0700677 void removeWallpaperToken(WallpaperWindowToken token) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700678 mWallpaperTokens.remove(token);
679 }
680
681 void dump(PrintWriter pw, String prefix) {
682 pw.print(prefix); pw.print("mWallpaperTarget="); pw.println(mWallpaperTarget);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800683 if (mPrevWallpaperTarget != null) {
684 pw.print(prefix); pw.print("mPrevWallpaperTarget="); pw.println(mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700685 }
686 pw.print(prefix); pw.print("mLastWallpaperX="); pw.print(mLastWallpaperX);
687 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
688 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE
689 || mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
690 pw.print(prefix);
691 pw.print("mLastWallpaperDisplayOffsetX="); pw.print(mLastWallpaperDisplayOffsetX);
692 pw.print(" mLastWallpaperDisplayOffsetY="); pw.println(mLastWallpaperDisplayOffsetY);
693 }
694 }
695
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700696 /** Helper class for storing the results of a wallpaper target find operation. */
697 final private static class FindWallpaperTargetResult {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700698 WindowState topWallpaper = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800699 boolean useTopWallpaperAsTarget = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700700 WindowState wallpaperTarget = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800701 boolean resetTopWallpaper = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700702
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800703 void setTopWallpaper(WindowState win) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700704 topWallpaper = win;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700705 }
706
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800707 void setWallpaperTarget(WindowState win) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700708 wallpaperTarget = win;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800709 }
710
711 void setUseTopWallpaperAsTarget(boolean topWallpaperAsTarget) {
712 useTopWallpaperAsTarget = topWallpaperAsTarget;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700713 }
714
715 void reset() {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700716 topWallpaper = null;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700717 wallpaperTarget = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800718 useTopWallpaperAsTarget = false;
719 resetTopWallpaper = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700720 }
721 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700722}