blob: 8a53a2b79c9757244bcdbba8b4bca4fcb5ee216e [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 Jaggife762342016-10-13 14:33:27 +020028import static com.android.server.wm.AppTransition.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
67 private int mWallpaperAnimLayerAdjustment;
68
69 private float mLastWallpaperX = -1;
70 private float mLastWallpaperY = -1;
71 private float mLastWallpaperXStep = -1;
72 private float mLastWallpaperYStep = -1;
73 private int mLastWallpaperDisplayOffsetX = Integer.MIN_VALUE;
74 private int mLastWallpaperDisplayOffsetY = Integer.MIN_VALUE;
75
76 // This is set when we are waiting for a wallpaper to tell us it is done
77 // changing its scroll position.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -080078 private WindowState mWaitingOnWallpaper;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070079
80 // The last time we had a timeout when waiting for a wallpaper.
81 private long mLastWallpaperTimeoutTime;
82 // We give a wallpaper up to 150ms to finish scrolling.
83 private static final long WALLPAPER_TIMEOUT = 150;
84 // Time we wait after a timeout before trying to wait again.
85 private static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
86
87 // Set to the wallpaper window we would like to hide once the transition animations are done.
88 // This is useful in cases where we don't want the wallpaper to be hidden when the close app
89 // is a wallpaper target and is done animating out, but the opening app isn't a wallpaper
90 // target and isn't done animating in.
Wale Ogunwalee4da0c12016-07-29 12:47:02 -070091 WindowState mDeferredHideWallpaper = null;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -070092
93 // We give a wallpaper up to 500ms to finish drawing before playing app transitions.
94 private static final long WALLPAPER_DRAW_PENDING_TIMEOUT_DURATION = 500;
95 private static final int WALLPAPER_DRAW_NORMAL = 0;
96 private static final int WALLPAPER_DRAW_PENDING = 1;
97 private static final int WALLPAPER_DRAW_TIMEOUT = 2;
98 private int mWallpaperDrawState = WALLPAPER_DRAW_NORMAL;
99
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700100 private final FindWallpaperTargetResult mFindResults = new FindWallpaperTargetResult();
101
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800102 private final ToBooleanFunction<WindowState> mFindWallpaperTargetFunction = w -> {
103 final WindowAnimator winAnimator = mService.mAnimator;
104 if ((w.mAttrs.type == TYPE_WALLPAPER)) {
105 if (mFindResults.topWallpaper == null || mFindResults.resetTopWallpaper) {
106 mFindResults.setTopWallpaper(w);
107 mFindResults.resetTopWallpaper = false;
108 }
109 return false;
110 }
111
112 mFindResults.resetTopWallpaper = true;
113 if (w != winAnimator.mWindowDetachedWallpaper && w.mAppToken != null) {
114 // 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 +0200115 if (w.mAppToken.isHidden() && !w.mAppToken.isSelfAnimating()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800116 if (DEBUG_WALLPAPER) Slog.v(TAG,
117 "Skipping hidden and not animating token: " + w);
118 return false;
119 }
120 }
121 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w + ": isOnScreen=" + w.isOnScreen()
122 + " mDrawState=" + w.mWinAnimator.mDrawState);
123
124 if (w.mWillReplaceWindow && mWallpaperTarget == null
125 && !mFindResults.useTopWallpaperAsTarget) {
126 // When we are replacing a window and there was wallpaper before replacement, we want to
127 // keep the window until the new windows fully appear and can determine the visibility,
128 // to avoid flickering.
129 mFindResults.setUseTopWallpaperAsTarget(true);
130 }
131
132 final boolean keyguardGoingAwayWithWallpaper = (w.mAppToken != null
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200133 && w.mAppToken.isSelfAnimating()
134 && AppTransition.isKeyguardGoingAwayTransit(w.mAppToken.getTransit())
135 && (w.mAppToken.getTransitFlags()
136 & TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER) != 0);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800137
138 boolean needsShowWhenLockedWallpaper = false;
139 if ((w.mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0
140 && mService.mPolicy.isKeyguardLocked()
141 && mService.mPolicy.isKeyguardOccluded()) {
142 // The lowest show when locked window decides whether we need to put the wallpaper
143 // behind.
144 needsShowWhenLockedWallpaper = !isFullscreen(w.mAttrs)
145 || (w.mAppToken != null && !w.mAppToken.fillsParent());
146 }
147
148 if (keyguardGoingAwayWithWallpaper || needsShowWhenLockedWallpaper) {
149 // Keep the wallpaper during Keyguard exit but also when it's needed for a
150 // non-fullscreen show when locked activity.
151 mFindResults.setUseTopWallpaperAsTarget(true);
152 }
153
154 final boolean hasWallpaper = (w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
155 if (hasWallpaper && w.isOnScreen() && (mWallpaperTarget == w || w.isDrawFinishedLw())) {
156 if (DEBUG_WALLPAPER) Slog.v(TAG, "Found wallpaper target: " + w);
157 mFindResults.setWallpaperTarget(w);
158 if (w == mWallpaperTarget && w.mWinAnimator.isAnimationSet()) {
159 // The current wallpaper target is animating, so we'll look behind it for
160 // another possible target and figure out what is going on later.
161 if (DEBUG_WALLPAPER) Slog.v(TAG,
162 "Win " + w + ": token animating, looking behind.");
163 }
164 // Found a target! End search.
165 return true;
166 } else if (w == winAnimator.mWindowDetachedWallpaper) {
167 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
168 "Found animating detached wallpaper target win: " + w);
169 mFindResults.setUseTopWallpaperAsTarget(true);
170 }
171 return false;
172 };
173
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700174 public WallpaperController(WindowManagerService service) {
175 mService = service;
176 }
177
178 WindowState getWallpaperTarget() {
179 return mWallpaperTarget;
180 }
181
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700182 boolean isWallpaperTarget(WindowState win) {
183 return win == mWallpaperTarget;
184 }
185
186 boolean isBelowWallpaperTarget(WindowState win) {
187 return mWallpaperTarget != null && mWallpaperTarget.mLayer >= win.mBaseLayer;
188 }
189
190 boolean isWallpaperVisible() {
191 return isWallpaperVisible(mWallpaperTarget);
192 }
193
Jorim Jaggife762342016-10-13 14:33:27 +0200194 /**
195 * Starts {@param a} on all wallpaper windows.
196 */
197 void startWallpaperAnimation(Animation a) {
198 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700199 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Jorim Jaggife762342016-10-13 14:33:27 +0200200 token.startAnimation(a);
201 }
202 }
203
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700204 private boolean isWallpaperVisible(WindowState wallpaperTarget) {
205 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper vis: target " + wallpaperTarget + ", obscured="
206 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200207 + " animating=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
208 ? wallpaperTarget.mAppToken.isSelfAnimating() : null)
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800209 + " prev=" + mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700210 return (wallpaperTarget != null
211 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200212 && wallpaperTarget.mAppToken.isSelfAnimating())))
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800213 || mPrevWallpaperTarget != null;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700214 }
215
216 boolean isWallpaperTargetAnimating() {
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700217 return mWallpaperTarget != null && mWallpaperTarget.mWinAnimator.isAnimationSet()
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200218 && (mWallpaperTarget.mAppToken == null
219 || !mWallpaperTarget.mAppToken.isWaitingForTransitionStart());
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700220 }
221
222 void updateWallpaperVisibility() {
Filip Gruszczynski63a35e22015-11-05 15:38:59 -0800223 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700224
225 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700226 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700227 token.updateWallpaperVisibility(visible);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700228 }
229 }
230
231 void hideDeferredWallpapersIfNeeded() {
232 if (mDeferredHideWallpaper != null) {
233 hideWallpapers(mDeferredHideWallpaper);
234 mDeferredHideWallpaper = null;
235 }
236 }
237
238 void hideWallpapers(final WindowState winGoingAway) {
239 if (mWallpaperTarget != null
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800240 && (mWallpaperTarget != winGoingAway || mPrevWallpaperTarget != null)) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700241 return;
242 }
243 if (mService.mAppTransition.isRunning()) {
244 // Defer hiding the wallpaper when app transition is running until the animations
245 // are done.
246 mDeferredHideWallpaper = winGoingAway;
247 return;
248 }
249
250 final boolean wasDeferred = (mDeferredHideWallpaper == winGoingAway);
251 for (int i = mWallpaperTokens.size() - 1; i >= 0; i--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700252 final WallpaperWindowToken token = mWallpaperTokens.get(i);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700253 token.hideWallpaperToken(wasDeferred, "hideWallpapers");
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200254 if (DEBUG_WALLPAPER_LIGHT && !token.isHidden()) Slog.d(TAG, "Hiding wallpaper " + token
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800255 + " from " + winGoingAway + " target=" + mWallpaperTarget + " prev="
256 + mPrevWallpaperTarget + "\n" + Debug.getCallers(5, " "));
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700257 }
258 }
259
260 boolean updateWallpaperOffset(WindowState wallpaperWin, int dw, int dh, boolean sync) {
261 boolean rawChanged = false;
Winson4b4ba902016-07-27 19:45:52 -0700262 // Set the default wallpaper x-offset to either edge of the screen (depending on RTL), to
263 // match the behavior of most Launchers
264 float defaultWallpaperX = wallpaperWin.isRtl() ? 1f : 0f;
265 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : defaultWallpaperX;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700266 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
267 int availw = wallpaperWin.mFrame.right - wallpaperWin.mFrame.left - dw;
268 int offset = availw > 0 ? -(int)(availw * wpx + .5f) : 0;
269 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
270 offset += mLastWallpaperDisplayOffsetX;
271 }
272 boolean changed = wallpaperWin.mXOffset != offset;
273 if (changed) {
274 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper " + wallpaperWin + " x: " + offset);
275 wallpaperWin.mXOffset = offset;
276 }
277 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
278 wallpaperWin.mWallpaperX = wpx;
279 wallpaperWin.mWallpaperXStep = wpxs;
280 rawChanged = true;
281 }
282
283 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
284 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
285 int availh = wallpaperWin.mFrame.bottom - wallpaperWin.mFrame.top - dh;
286 offset = availh > 0 ? -(int)(availh * wpy + .5f) : 0;
287 if (mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
288 offset += mLastWallpaperDisplayOffsetY;
289 }
290 if (wallpaperWin.mYOffset != offset) {
291 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper " + wallpaperWin + " y: " + offset);
292 changed = true;
293 wallpaperWin.mYOffset = offset;
294 }
295 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
296 wallpaperWin.mWallpaperY = wpy;
297 wallpaperWin.mWallpaperYStep = wpys;
298 rawChanged = true;
299 }
300
301 if (rawChanged && (wallpaperWin.mAttrs.privateFlags &
302 WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS) != 0) {
303 try {
304 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
305 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
306 + " y=" + wallpaperWin.mWallpaperY);
307 if (sync) {
308 mWaitingOnWallpaper = wallpaperWin;
309 }
310 wallpaperWin.mClient.dispatchWallpaperOffsets(
311 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
312 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
313 if (sync) {
314 if (mWaitingOnWallpaper != null) {
315 long start = SystemClock.uptimeMillis();
316 if ((mLastWallpaperTimeoutTime + WALLPAPER_TIMEOUT_RECOVERY)
317 < start) {
318 try {
319 if (DEBUG_WALLPAPER) Slog.v(TAG,
320 "Waiting for offset complete...");
321 mService.mWindowMap.wait(WALLPAPER_TIMEOUT);
322 } catch (InterruptedException e) {
323 }
324 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
325 if ((start + WALLPAPER_TIMEOUT) < SystemClock.uptimeMillis()) {
326 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
327 + wallpaperWin);
328 mLastWallpaperTimeoutTime = start;
329 }
330 }
331 mWaitingOnWallpaper = null;
332 }
333 }
334 } catch (RemoteException e) {
335 }
336 }
337
338 return changed;
339 }
340
341 void setWindowWallpaperPosition(
342 WindowState window, float x, float y, float xStep, float yStep) {
343 if (window.mWallpaperX != x || window.mWallpaperY != y) {
344 window.mWallpaperX = x;
345 window.mWallpaperY = y;
346 window.mWallpaperXStep = xStep;
347 window.mWallpaperYStep = yStep;
348 updateWallpaperOffsetLocked(window, true);
349 }
350 }
351
352 void setWindowWallpaperDisplayOffset(WindowState window, int x, int y) {
353 if (window.mWallpaperDisplayOffsetX != x || window.mWallpaperDisplayOffsetY != y) {
354 window.mWallpaperDisplayOffsetX = x;
355 window.mWallpaperDisplayOffsetY = y;
356 updateWallpaperOffsetLocked(window, true);
357 }
358 }
359
360 Bundle sendWindowWallpaperCommand(
361 WindowState window, String action, int x, int y, int z, Bundle extras, boolean sync) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800362 if (window == mWallpaperTarget || window == mPrevWallpaperTarget) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700363 boolean doWait = sync;
364 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700365 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700366 token.sendWindowWallpaperCommand(action, x, y, z, extras, sync);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700367 }
368
369 if (doWait) {
370 // TODO: Need to wait for result.
371 }
372 }
373
374 return null;
375 }
376
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700377 private void updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700378 final DisplayContent displayContent = changingTarget.getDisplayContent();
379 if (displayContent == null) {
380 return;
381 }
382 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
383 final int dw = displayInfo.logicalWidth;
384 final int dh = displayInfo.logicalHeight;
385
386 WindowState target = mWallpaperTarget;
387 if (target != null) {
388 if (target.mWallpaperX >= 0) {
389 mLastWallpaperX = target.mWallpaperX;
390 } else if (changingTarget.mWallpaperX >= 0) {
391 mLastWallpaperX = changingTarget.mWallpaperX;
392 }
393 if (target.mWallpaperY >= 0) {
394 mLastWallpaperY = target.mWallpaperY;
395 } else if (changingTarget.mWallpaperY >= 0) {
396 mLastWallpaperY = changingTarget.mWallpaperY;
397 }
398 if (target.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
399 mLastWallpaperDisplayOffsetX = target.mWallpaperDisplayOffsetX;
400 } else if (changingTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
401 mLastWallpaperDisplayOffsetX = changingTarget.mWallpaperDisplayOffsetX;
402 }
403 if (target.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
404 mLastWallpaperDisplayOffsetY = target.mWallpaperDisplayOffsetY;
405 } else if (changingTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
406 mLastWallpaperDisplayOffsetY = changingTarget.mWallpaperDisplayOffsetY;
407 }
408 if (target.mWallpaperXStep >= 0) {
409 mLastWallpaperXStep = target.mWallpaperXStep;
410 } else if (changingTarget.mWallpaperXStep >= 0) {
411 mLastWallpaperXStep = changingTarget.mWallpaperXStep;
412 }
413 if (target.mWallpaperYStep >= 0) {
414 mLastWallpaperYStep = target.mWallpaperYStep;
415 } else if (changingTarget.mWallpaperYStep >= 0) {
416 mLastWallpaperYStep = changingTarget.mWallpaperYStep;
417 }
418 }
419
420 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700421 mWallpaperTokens.get(curTokenNdx).updateWallpaperOffset(dw, dh, sync);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700422 }
423 }
424
425 void clearLastWallpaperTimeoutTime() {
426 mLastWallpaperTimeoutTime = 0;
427 }
428
429 void wallpaperCommandComplete(IBinder window) {
430 if (mWaitingOnWallpaper != null &&
431 mWaitingOnWallpaper.mClient.asBinder() == window) {
432 mWaitingOnWallpaper = null;
433 mService.mWindowMap.notifyAll();
434 }
435 }
436
437 void wallpaperOffsetsComplete(IBinder window) {
438 if (mWaitingOnWallpaper != null &&
439 mWaitingOnWallpaper.mClient.asBinder() == window) {
440 mWaitingOnWallpaper = null;
441 mService.mWindowMap.notifyAll();
442 }
443 }
444
445 int getAnimLayerAdjustment() {
446 return mWallpaperAnimLayerAdjustment;
447 }
448
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800449 private void findWallpaperTarget(DisplayContent dc) {
450 mFindResults.reset();
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700451 if (dc.isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800452 // In freeform mode we set the wallpaper as its own target, so we don't need an
453 // additional window to make it visible.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800454 mFindResults.setUseTopWallpaperAsTarget(true);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800455 }
Wale Ogunwale6cbba702016-06-28 16:27:31 -0700456
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800457 dc.forAllWindows(mFindWallpaperTargetFunction, true /* traverseTopToBottom */);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800458
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800459 if (mFindResults.wallpaperTarget == null && mFindResults.useTopWallpaperAsTarget) {
460 mFindResults.setWallpaperTarget(mFindResults.topWallpaper);
Wale Ogunwale21fdd912015-08-20 12:34:57 -0700461 }
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700462 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700463
Jorim Jaggife762342016-10-13 14:33:27 +0200464 private boolean isFullscreen(WindowManager.LayoutParams attrs) {
465 return attrs.x == 0 && attrs.y == 0
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800466 && attrs.width == MATCH_PARENT && attrs.height == MATCH_PARENT;
Jorim Jaggife762342016-10-13 14:33:27 +0200467 }
468
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700469 /** Updates the target wallpaper if needed and returns true if an update happened. */
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800470 private void updateWallpaperWindowsTarget(DisplayContent dc,
471 FindWallpaperTargetResult result) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700472
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700473 WindowState wallpaperTarget = result.wallpaperTarget;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700474
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700475 if (mWallpaperTarget == wallpaperTarget
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800476 || (mPrevWallpaperTarget != null && mPrevWallpaperTarget == wallpaperTarget)) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700477
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800478 if (mPrevWallpaperTarget == null) {
479 return;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700480 }
481
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800482 // Is it time to stop animating?
483 if (!mPrevWallpaperTarget.isAnimatingLw()) {
484 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "No longer animating wallpaper targets!");
485 mPrevWallpaperTarget = null;
486 mWallpaperTarget = wallpaperTarget;
487 }
488 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700489 }
490
491 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800492 "New wallpaper target: " + wallpaperTarget + " prevTarget: " + mWallpaperTarget);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700493
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800494 mPrevWallpaperTarget = null;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700495
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800496 final WindowState prevWallpaperTarget = mWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700497 mWallpaperTarget = wallpaperTarget;
498
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800499 if (wallpaperTarget == null || prevWallpaperTarget == null) {
500 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700501 }
502
503 // Now what is happening... if the current and new targets are animating,
504 // then we are in our super special mode!
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800505 boolean oldAnim = prevWallpaperTarget.isAnimatingLw();
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700506 boolean foundAnim = wallpaperTarget.isAnimatingLw();
507 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
508 "New animation: " + foundAnim + " old animation: " + oldAnim);
509
510 if (!foundAnim || !oldAnim) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800511 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700512 }
513
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800514 if (dc.getWindow(w -> w == prevWallpaperTarget) == null) {
515 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700516 }
517
518 final boolean newTargetHidden = wallpaperTarget.mAppToken != null
519 && wallpaperTarget.mAppToken.hiddenRequested;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800520 final boolean oldTargetHidden = prevWallpaperTarget.mAppToken != null
521 && prevWallpaperTarget.mAppToken.hiddenRequested;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700522
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800523 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Animating wallpapers:" + " old: "
524 + prevWallpaperTarget + " hidden=" + oldTargetHidden + " new: " + wallpaperTarget
525 + " hidden=" + newTargetHidden);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700526
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800527 mPrevWallpaperTarget = prevWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700528
529 if (newTargetHidden && !oldTargetHidden) {
530 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Old wallpaper still the target.");
531 // Use the old target if new target is hidden but old target
532 // is not. If they're both hidden, still use the new target.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800533 mWallpaperTarget = prevWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700534 } else if (newTargetHidden == oldTargetHidden
535 && !mService.mOpeningApps.contains(wallpaperTarget.mAppToken)
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800536 && (mService.mOpeningApps.contains(prevWallpaperTarget.mAppToken)
537 || mService.mClosingApps.contains(prevWallpaperTarget.mAppToken))) {
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700538 // If they're both hidden (or both not hidden), prefer the one that's currently in
539 // opening or closing app list, this allows transition selection logic to better
540 // determine the wallpaper status of opening/closing apps.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800541 mWallpaperTarget = prevWallpaperTarget;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700542 }
543
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800544 result.setWallpaperTarget(wallpaperTarget);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700545 }
546
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800547 private void updateWallpaperTokens(boolean visible) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700548 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700549 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800550 token.updateWallpaperWindows(visible, mWallpaperAnimLayerAdjustment);
Robert Carrdee1b3f2017-02-27 11:33:33 -0800551 token.getDisplayContent().assignWindowLayers(false);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700552 }
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700553 }
554
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800555 void adjustWallpaperWindows(DisplayContent dc) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700556 mService.mRoot.mWallpaperMayChange = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700557
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700558 // First find top-most window that has asked to be on top of the wallpaper;
559 // all wallpapers go behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800560 findWallpaperTarget(dc);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800561 updateWallpaperWindowsTarget(dc, mFindResults);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700562
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800563 // The window is visible to the compositor...but is it visible to the user?
564 // That is what the wallpaper cares about.
565 final boolean visible = mWallpaperTarget != null && isWallpaperVisible(mWallpaperTarget);
566 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700567
568 if (visible) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800569 // If the wallpaper target is animating, we may need to copy its layer adjustment.
570 // Only do this if we are not transferring between two wallpaper targets.
571 mWallpaperAnimLayerAdjustment =
572 (mPrevWallpaperTarget == null && mWallpaperTarget.mAppToken != null)
Robert Carrdee1b3f2017-02-27 11:33:33 -0800573 ? mWallpaperTarget.mAppToken.getAnimLayerAdjustment() : 0;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800574
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700575 if (mWallpaperTarget.mWallpaperX >= 0) {
576 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
577 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
578 }
579 if (mWallpaperTarget.mWallpaperY >= 0) {
580 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
581 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
582 }
583 if (mWallpaperTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
584 mLastWallpaperDisplayOffsetX = mWallpaperTarget.mWallpaperDisplayOffsetX;
585 }
586 if (mWallpaperTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
587 mLastWallpaperDisplayOffsetY = mWallpaperTarget.mWallpaperDisplayOffsetY;
588 }
589 }
590
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800591 updateWallpaperTokens(visible);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700592
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800593 if (DEBUG_WALLPAPER_LIGHT) Slog.d(TAG, "New wallpaper: target=" + mWallpaperTarget
594 + " prev=" + mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700595 }
596
597 boolean processWallpaperDrawPendingTimeout() {
598 if (mWallpaperDrawState == WALLPAPER_DRAW_PENDING) {
599 mWallpaperDrawState = WALLPAPER_DRAW_TIMEOUT;
600 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG,
601 "*** WALLPAPER DRAW TIMEOUT");
602 return true;
603 }
604 return false;
605 }
606
607 boolean wallpaperTransitionReady() {
608 boolean transitionReady = true;
609 boolean wallpaperReady = true;
610 for (int curTokenIndex = mWallpaperTokens.size() - 1;
611 curTokenIndex >= 0 && wallpaperReady; curTokenIndex--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700612 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenIndex);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700613 if (token.hasVisibleNotDrawnWallpaper()) {
614 // We've told this wallpaper to be visible, but it is not drawn yet
615 wallpaperReady = false;
616 if (mWallpaperDrawState != WALLPAPER_DRAW_TIMEOUT) {
617 // wait for this wallpaper until it is drawn or timeout
618 transitionReady = false;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700619 }
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700620 if (mWallpaperDrawState == WALLPAPER_DRAW_NORMAL) {
621 mWallpaperDrawState = WALLPAPER_DRAW_PENDING;
622 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT);
623 mService.mH.sendEmptyMessageDelayed(WALLPAPER_DRAW_PENDING_TIMEOUT,
624 WALLPAPER_DRAW_PENDING_TIMEOUT_DURATION);
625 }
626 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG,
627 "Wallpaper should be visible but has not been drawn yet. " +
628 "mWallpaperDrawState=" + mWallpaperDrawState);
629 break;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700630 }
631 }
632 if (wallpaperReady) {
633 mWallpaperDrawState = WALLPAPER_DRAW_NORMAL;
634 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT);
635 }
636
637 return transitionReady;
638 }
639
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700640 /**
641 * Adjusts the wallpaper windows if the input display has a pending wallpaper layout or one of
642 * the opening apps should be a wallpaper target.
643 */
Wale Ogunwale0303c572016-10-20 10:16:29 -0700644 void adjustWallpaperWindowsForAppTransitionIfNeeded(DisplayContent dc,
645 ArraySet<AppWindowToken> openingApps) {
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700646 boolean adjust = false;
647 if ((dc.pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
648 adjust = true;
649 } else {
650 for (int i = openingApps.size() - 1; i >= 0; --i) {
651 final AppWindowToken token = openingApps.valueAt(i);
652 if (token.windowsCanBeWallpaperTarget()) {
653 adjust = true;
654 break;
655 }
656 }
657 }
658
Wale Ogunwale0303c572016-10-20 10:16:29 -0700659 if (adjust) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800660 adjustWallpaperWindows(dc);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700661 }
662 }
663
Jorim Jaggi879ff722016-11-04 18:08:17 -0700664 void addWallpaperToken(WallpaperWindowToken token) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700665 mWallpaperTokens.add(token);
666 }
667
Jorim Jaggi879ff722016-11-04 18:08:17 -0700668 void removeWallpaperToken(WallpaperWindowToken token) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700669 mWallpaperTokens.remove(token);
670 }
671
672 void dump(PrintWriter pw, String prefix) {
673 pw.print(prefix); pw.print("mWallpaperTarget="); pw.println(mWallpaperTarget);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800674 if (mPrevWallpaperTarget != null) {
675 pw.print(prefix); pw.print("mPrevWallpaperTarget="); pw.println(mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700676 }
677 pw.print(prefix); pw.print("mLastWallpaperX="); pw.print(mLastWallpaperX);
678 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
679 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE
680 || mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
681 pw.print(prefix);
682 pw.print("mLastWallpaperDisplayOffsetX="); pw.print(mLastWallpaperDisplayOffsetX);
683 pw.print(" mLastWallpaperDisplayOffsetY="); pw.println(mLastWallpaperDisplayOffsetY);
684 }
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700685
686 if (mWallpaperAnimLayerAdjustment != 0) {
687 pw.println(prefix + "mWallpaperAnimLayerAdjustment=" + mWallpaperAnimLayerAdjustment);
688 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700689 }
690
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700691 /** Helper class for storing the results of a wallpaper target find operation. */
692 final private static class FindWallpaperTargetResult {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700693 WindowState topWallpaper = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800694 boolean useTopWallpaperAsTarget = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700695 WindowState wallpaperTarget = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800696 boolean resetTopWallpaper = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700697
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800698 void setTopWallpaper(WindowState win) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700699 topWallpaper = win;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700700 }
701
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800702 void setWallpaperTarget(WindowState win) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700703 wallpaperTarget = win;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800704 }
705
706 void setUseTopWallpaperAsTarget(boolean topWallpaperAsTarget) {
707 useTopWallpaperAsTarget = topWallpaperAsTarget;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700708 }
709
710 void reset() {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700711 topWallpaper = null;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700712 wallpaperTarget = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800713 useTopWallpaperAsTarget = false;
714 resetTopWallpaper = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700715 }
716 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700717}