blob: 1218d3bc1b9b936716e7e8b4c952bdc66d08bf12 [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
152 final boolean hasWallpaper = (w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
153 if (hasWallpaper && w.isOnScreen() && (mWallpaperTarget == w || w.isDrawFinishedLw())) {
154 if (DEBUG_WALLPAPER) Slog.v(TAG, "Found wallpaper target: " + w);
155 mFindResults.setWallpaperTarget(w);
156 if (w == mWallpaperTarget && w.mWinAnimator.isAnimationSet()) {
157 // The current wallpaper target is animating, so we'll look behind it for
158 // another possible target and figure out what is going on later.
159 if (DEBUG_WALLPAPER) Slog.v(TAG,
160 "Win " + w + ": token animating, looking behind.");
161 }
162 // Found a target! End search.
163 return true;
164 } else if (w == winAnimator.mWindowDetachedWallpaper) {
165 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
166 "Found animating detached wallpaper target win: " + w);
167 mFindResults.setUseTopWallpaperAsTarget(true);
168 }
169 return false;
170 };
171
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700172 public WallpaperController(WindowManagerService service) {
173 mService = service;
174 }
175
176 WindowState getWallpaperTarget() {
177 return mWallpaperTarget;
178 }
179
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700180 boolean isWallpaperTarget(WindowState win) {
181 return win == mWallpaperTarget;
182 }
183
184 boolean isBelowWallpaperTarget(WindowState win) {
185 return mWallpaperTarget != null && mWallpaperTarget.mLayer >= win.mBaseLayer;
186 }
187
188 boolean isWallpaperVisible() {
189 return isWallpaperVisible(mWallpaperTarget);
190 }
191
Jorim Jaggife762342016-10-13 14:33:27 +0200192 /**
193 * Starts {@param a} on all wallpaper windows.
194 */
195 void startWallpaperAnimation(Animation a) {
196 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700197 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Jorim Jaggife762342016-10-13 14:33:27 +0200198 token.startAnimation(a);
199 }
200 }
201
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700202 private boolean isWallpaperVisible(WindowState wallpaperTarget) {
203 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper vis: target " + wallpaperTarget + ", obscured="
204 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200205 + " animating=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
206 ? wallpaperTarget.mAppToken.isSelfAnimating() : null)
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800207 + " prev=" + mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700208 return (wallpaperTarget != null
209 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200210 && wallpaperTarget.mAppToken.isSelfAnimating())))
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800211 || mPrevWallpaperTarget != null;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700212 }
213
214 boolean isWallpaperTargetAnimating() {
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700215 return mWallpaperTarget != null && mWallpaperTarget.mWinAnimator.isAnimationSet()
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200216 && (mWallpaperTarget.mAppToken == null
217 || !mWallpaperTarget.mAppToken.isWaitingForTransitionStart());
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700218 }
219
220 void updateWallpaperVisibility() {
Filip Gruszczynski63a35e22015-11-05 15:38:59 -0800221 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700222
223 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700224 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700225 token.updateWallpaperVisibility(visible);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700226 }
227 }
228
229 void hideDeferredWallpapersIfNeeded() {
230 if (mDeferredHideWallpaper != null) {
231 hideWallpapers(mDeferredHideWallpaper);
232 mDeferredHideWallpaper = null;
233 }
234 }
235
236 void hideWallpapers(final WindowState winGoingAway) {
237 if (mWallpaperTarget != null
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800238 && (mWallpaperTarget != winGoingAway || mPrevWallpaperTarget != null)) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700239 return;
240 }
241 if (mService.mAppTransition.isRunning()) {
242 // Defer hiding the wallpaper when app transition is running until the animations
243 // are done.
244 mDeferredHideWallpaper = winGoingAway;
245 return;
246 }
247
248 final boolean wasDeferred = (mDeferredHideWallpaper == winGoingAway);
249 for (int i = mWallpaperTokens.size() - 1; i >= 0; i--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700250 final WallpaperWindowToken token = mWallpaperTokens.get(i);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700251 token.hideWallpaperToken(wasDeferred, "hideWallpapers");
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200252 if (DEBUG_WALLPAPER_LIGHT && !token.isHidden()) Slog.d(TAG, "Hiding wallpaper " + token
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800253 + " from " + winGoingAway + " target=" + mWallpaperTarget + " prev="
254 + mPrevWallpaperTarget + "\n" + Debug.getCallers(5, " "));
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700255 }
256 }
257
258 boolean updateWallpaperOffset(WindowState wallpaperWin, int dw, int dh, boolean sync) {
259 boolean rawChanged = false;
Winson4b4ba902016-07-27 19:45:52 -0700260 // Set the default wallpaper x-offset to either edge of the screen (depending on RTL), to
261 // match the behavior of most Launchers
262 float defaultWallpaperX = wallpaperWin.isRtl() ? 1f : 0f;
263 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : defaultWallpaperX;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700264 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
265 int availw = wallpaperWin.mFrame.right - wallpaperWin.mFrame.left - dw;
266 int offset = availw > 0 ? -(int)(availw * wpx + .5f) : 0;
267 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
268 offset += mLastWallpaperDisplayOffsetX;
269 }
270 boolean changed = wallpaperWin.mXOffset != offset;
271 if (changed) {
272 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper " + wallpaperWin + " x: " + offset);
273 wallpaperWin.mXOffset = offset;
274 }
275 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
276 wallpaperWin.mWallpaperX = wpx;
277 wallpaperWin.mWallpaperXStep = wpxs;
278 rawChanged = true;
279 }
280
281 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
282 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
283 int availh = wallpaperWin.mFrame.bottom - wallpaperWin.mFrame.top - dh;
284 offset = availh > 0 ? -(int)(availh * wpy + .5f) : 0;
285 if (mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
286 offset += mLastWallpaperDisplayOffsetY;
287 }
288 if (wallpaperWin.mYOffset != offset) {
289 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper " + wallpaperWin + " y: " + offset);
290 changed = true;
291 wallpaperWin.mYOffset = offset;
292 }
293 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
294 wallpaperWin.mWallpaperY = wpy;
295 wallpaperWin.mWallpaperYStep = wpys;
296 rawChanged = true;
297 }
298
299 if (rawChanged && (wallpaperWin.mAttrs.privateFlags &
300 WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS) != 0) {
301 try {
302 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
303 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
304 + " y=" + wallpaperWin.mWallpaperY);
305 if (sync) {
306 mWaitingOnWallpaper = wallpaperWin;
307 }
308 wallpaperWin.mClient.dispatchWallpaperOffsets(
309 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
310 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
311 if (sync) {
312 if (mWaitingOnWallpaper != null) {
313 long start = SystemClock.uptimeMillis();
314 if ((mLastWallpaperTimeoutTime + WALLPAPER_TIMEOUT_RECOVERY)
315 < start) {
316 try {
317 if (DEBUG_WALLPAPER) Slog.v(TAG,
318 "Waiting for offset complete...");
319 mService.mWindowMap.wait(WALLPAPER_TIMEOUT);
320 } catch (InterruptedException e) {
321 }
322 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
323 if ((start + WALLPAPER_TIMEOUT) < SystemClock.uptimeMillis()) {
324 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
325 + wallpaperWin);
326 mLastWallpaperTimeoutTime = start;
327 }
328 }
329 mWaitingOnWallpaper = null;
330 }
331 }
332 } catch (RemoteException e) {
333 }
334 }
335
336 return changed;
337 }
338
339 void setWindowWallpaperPosition(
340 WindowState window, float x, float y, float xStep, float yStep) {
341 if (window.mWallpaperX != x || window.mWallpaperY != y) {
342 window.mWallpaperX = x;
343 window.mWallpaperY = y;
344 window.mWallpaperXStep = xStep;
345 window.mWallpaperYStep = yStep;
346 updateWallpaperOffsetLocked(window, true);
347 }
348 }
349
350 void setWindowWallpaperDisplayOffset(WindowState window, int x, int y) {
351 if (window.mWallpaperDisplayOffsetX != x || window.mWallpaperDisplayOffsetY != y) {
352 window.mWallpaperDisplayOffsetX = x;
353 window.mWallpaperDisplayOffsetY = y;
354 updateWallpaperOffsetLocked(window, true);
355 }
356 }
357
358 Bundle sendWindowWallpaperCommand(
359 WindowState window, String action, int x, int y, int z, Bundle extras, boolean sync) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800360 if (window == mWallpaperTarget || window == mPrevWallpaperTarget) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700361 boolean doWait = sync;
362 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700363 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700364 token.sendWindowWallpaperCommand(action, x, y, z, extras, sync);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700365 }
366
367 if (doWait) {
368 // TODO: Need to wait for result.
369 }
370 }
371
372 return null;
373 }
374
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700375 private void updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700376 final DisplayContent displayContent = changingTarget.getDisplayContent();
377 if (displayContent == null) {
378 return;
379 }
380 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
381 final int dw = displayInfo.logicalWidth;
382 final int dh = displayInfo.logicalHeight;
383
384 WindowState target = mWallpaperTarget;
385 if (target != null) {
386 if (target.mWallpaperX >= 0) {
387 mLastWallpaperX = target.mWallpaperX;
388 } else if (changingTarget.mWallpaperX >= 0) {
389 mLastWallpaperX = changingTarget.mWallpaperX;
390 }
391 if (target.mWallpaperY >= 0) {
392 mLastWallpaperY = target.mWallpaperY;
393 } else if (changingTarget.mWallpaperY >= 0) {
394 mLastWallpaperY = changingTarget.mWallpaperY;
395 }
396 if (target.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
397 mLastWallpaperDisplayOffsetX = target.mWallpaperDisplayOffsetX;
398 } else if (changingTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
399 mLastWallpaperDisplayOffsetX = changingTarget.mWallpaperDisplayOffsetX;
400 }
401 if (target.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
402 mLastWallpaperDisplayOffsetY = target.mWallpaperDisplayOffsetY;
403 } else if (changingTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
404 mLastWallpaperDisplayOffsetY = changingTarget.mWallpaperDisplayOffsetY;
405 }
406 if (target.mWallpaperXStep >= 0) {
407 mLastWallpaperXStep = target.mWallpaperXStep;
408 } else if (changingTarget.mWallpaperXStep >= 0) {
409 mLastWallpaperXStep = changingTarget.mWallpaperXStep;
410 }
411 if (target.mWallpaperYStep >= 0) {
412 mLastWallpaperYStep = target.mWallpaperYStep;
413 } else if (changingTarget.mWallpaperYStep >= 0) {
414 mLastWallpaperYStep = changingTarget.mWallpaperYStep;
415 }
416 }
417
418 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700419 mWallpaperTokens.get(curTokenNdx).updateWallpaperOffset(dw, dh, sync);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700420 }
421 }
422
423 void clearLastWallpaperTimeoutTime() {
424 mLastWallpaperTimeoutTime = 0;
425 }
426
427 void wallpaperCommandComplete(IBinder window) {
428 if (mWaitingOnWallpaper != null &&
429 mWaitingOnWallpaper.mClient.asBinder() == window) {
430 mWaitingOnWallpaper = null;
431 mService.mWindowMap.notifyAll();
432 }
433 }
434
435 void wallpaperOffsetsComplete(IBinder window) {
436 if (mWaitingOnWallpaper != null &&
437 mWaitingOnWallpaper.mClient.asBinder() == window) {
438 mWaitingOnWallpaper = null;
439 mService.mWindowMap.notifyAll();
440 }
441 }
442
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800443 private void findWallpaperTarget(DisplayContent dc) {
444 mFindResults.reset();
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700445 if (dc.isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800446 // In freeform mode we set the wallpaper as its own target, so we don't need an
447 // additional window to make it visible.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800448 mFindResults.setUseTopWallpaperAsTarget(true);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800449 }
Wale Ogunwale6cbba702016-06-28 16:27:31 -0700450
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800451 dc.forAllWindows(mFindWallpaperTargetFunction, true /* traverseTopToBottom */);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800452
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800453 if (mFindResults.wallpaperTarget == null && mFindResults.useTopWallpaperAsTarget) {
454 mFindResults.setWallpaperTarget(mFindResults.topWallpaper);
Wale Ogunwale21fdd912015-08-20 12:34:57 -0700455 }
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700456 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700457
Jorim Jaggife762342016-10-13 14:33:27 +0200458 private boolean isFullscreen(WindowManager.LayoutParams attrs) {
459 return attrs.x == 0 && attrs.y == 0
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800460 && attrs.width == MATCH_PARENT && attrs.height == MATCH_PARENT;
Jorim Jaggife762342016-10-13 14:33:27 +0200461 }
462
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700463 /** Updates the target wallpaper if needed and returns true if an update happened. */
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800464 private void updateWallpaperWindowsTarget(DisplayContent dc,
465 FindWallpaperTargetResult result) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700466
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700467 WindowState wallpaperTarget = result.wallpaperTarget;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700468
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700469 if (mWallpaperTarget == wallpaperTarget
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800470 || (mPrevWallpaperTarget != null && mPrevWallpaperTarget == wallpaperTarget)) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700471
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800472 if (mPrevWallpaperTarget == null) {
473 return;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700474 }
475
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800476 // Is it time to stop animating?
477 if (!mPrevWallpaperTarget.isAnimatingLw()) {
478 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "No longer animating wallpaper targets!");
479 mPrevWallpaperTarget = null;
480 mWallpaperTarget = wallpaperTarget;
481 }
482 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700483 }
484
485 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800486 "New wallpaper target: " + wallpaperTarget + " prevTarget: " + mWallpaperTarget);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700487
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800488 mPrevWallpaperTarget = null;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700489
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800490 final WindowState prevWallpaperTarget = mWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700491 mWallpaperTarget = wallpaperTarget;
492
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800493 if (wallpaperTarget == null || prevWallpaperTarget == null) {
494 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700495 }
496
497 // Now what is happening... if the current and new targets are animating,
498 // then we are in our super special mode!
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800499 boolean oldAnim = prevWallpaperTarget.isAnimatingLw();
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700500 boolean foundAnim = wallpaperTarget.isAnimatingLw();
501 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
502 "New animation: " + foundAnim + " old animation: " + oldAnim);
503
504 if (!foundAnim || !oldAnim) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800505 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700506 }
507
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800508 if (dc.getWindow(w -> w == prevWallpaperTarget) == null) {
509 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700510 }
511
512 final boolean newTargetHidden = wallpaperTarget.mAppToken != null
513 && wallpaperTarget.mAppToken.hiddenRequested;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800514 final boolean oldTargetHidden = prevWallpaperTarget.mAppToken != null
515 && prevWallpaperTarget.mAppToken.hiddenRequested;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700516
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800517 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Animating wallpapers:" + " old: "
518 + prevWallpaperTarget + " hidden=" + oldTargetHidden + " new: " + wallpaperTarget
519 + " hidden=" + newTargetHidden);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700520
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800521 mPrevWallpaperTarget = prevWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700522
523 if (newTargetHidden && !oldTargetHidden) {
524 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Old wallpaper still the target.");
525 // Use the old target if new target is hidden but old target
526 // is not. If they're both hidden, still use the new target.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800527 mWallpaperTarget = prevWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700528 } else if (newTargetHidden == oldTargetHidden
529 && !mService.mOpeningApps.contains(wallpaperTarget.mAppToken)
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800530 && (mService.mOpeningApps.contains(prevWallpaperTarget.mAppToken)
531 || mService.mClosingApps.contains(prevWallpaperTarget.mAppToken))) {
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700532 // If they're both hidden (or both not hidden), prefer the one that's currently in
533 // opening or closing app list, this allows transition selection logic to better
534 // determine the wallpaper status of opening/closing apps.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800535 mWallpaperTarget = prevWallpaperTarget;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700536 }
537
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800538 result.setWallpaperTarget(wallpaperTarget);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700539 }
540
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800541 private void updateWallpaperTokens(boolean visible) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700542 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700543 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000544 token.updateWallpaperWindows(visible);
Robert Carrdee1b3f2017-02-27 11:33:33 -0800545 token.getDisplayContent().assignWindowLayers(false);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700546 }
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700547 }
548
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800549 void adjustWallpaperWindows(DisplayContent dc) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700550 mService.mRoot.mWallpaperMayChange = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700551
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700552 // First find top-most window that has asked to be on top of the wallpaper;
553 // all wallpapers go behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800554 findWallpaperTarget(dc);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800555 updateWallpaperWindowsTarget(dc, mFindResults);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700556
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800557 // The window is visible to the compositor...but is it visible to the user?
558 // That is what the wallpaper cares about.
559 final boolean visible = mWallpaperTarget != null && isWallpaperVisible(mWallpaperTarget);
560 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700561
562 if (visible) {
563 if (mWallpaperTarget.mWallpaperX >= 0) {
564 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
565 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
566 }
567 if (mWallpaperTarget.mWallpaperY >= 0) {
568 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
569 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
570 }
571 if (mWallpaperTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
572 mLastWallpaperDisplayOffsetX = mWallpaperTarget.mWallpaperDisplayOffsetX;
573 }
574 if (mWallpaperTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
575 mLastWallpaperDisplayOffsetY = mWallpaperTarget.mWallpaperDisplayOffsetY;
576 }
577 }
578
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800579 updateWallpaperTokens(visible);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700580
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800581 if (DEBUG_WALLPAPER_LIGHT) Slog.d(TAG, "New wallpaper: target=" + mWallpaperTarget
582 + " prev=" + mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700583 }
584
585 boolean processWallpaperDrawPendingTimeout() {
586 if (mWallpaperDrawState == WALLPAPER_DRAW_PENDING) {
587 mWallpaperDrawState = WALLPAPER_DRAW_TIMEOUT;
588 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG,
589 "*** WALLPAPER DRAW TIMEOUT");
590 return true;
591 }
592 return false;
593 }
594
595 boolean wallpaperTransitionReady() {
596 boolean transitionReady = true;
597 boolean wallpaperReady = true;
598 for (int curTokenIndex = mWallpaperTokens.size() - 1;
599 curTokenIndex >= 0 && wallpaperReady; curTokenIndex--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700600 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenIndex);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700601 if (token.hasVisibleNotDrawnWallpaper()) {
602 // We've told this wallpaper to be visible, but it is not drawn yet
603 wallpaperReady = false;
604 if (mWallpaperDrawState != WALLPAPER_DRAW_TIMEOUT) {
605 // wait for this wallpaper until it is drawn or timeout
606 transitionReady = false;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700607 }
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700608 if (mWallpaperDrawState == WALLPAPER_DRAW_NORMAL) {
609 mWallpaperDrawState = WALLPAPER_DRAW_PENDING;
610 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT);
611 mService.mH.sendEmptyMessageDelayed(WALLPAPER_DRAW_PENDING_TIMEOUT,
612 WALLPAPER_DRAW_PENDING_TIMEOUT_DURATION);
613 }
614 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG,
615 "Wallpaper should be visible but has not been drawn yet. " +
616 "mWallpaperDrawState=" + mWallpaperDrawState);
617 break;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700618 }
619 }
620 if (wallpaperReady) {
621 mWallpaperDrawState = WALLPAPER_DRAW_NORMAL;
622 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT);
623 }
624
625 return transitionReady;
626 }
627
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700628 /**
629 * Adjusts the wallpaper windows if the input display has a pending wallpaper layout or one of
630 * the opening apps should be a wallpaper target.
631 */
Wale Ogunwale0303c572016-10-20 10:16:29 -0700632 void adjustWallpaperWindowsForAppTransitionIfNeeded(DisplayContent dc,
633 ArraySet<AppWindowToken> openingApps) {
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700634 boolean adjust = false;
635 if ((dc.pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
636 adjust = true;
637 } else {
638 for (int i = openingApps.size() - 1; i >= 0; --i) {
639 final AppWindowToken token = openingApps.valueAt(i);
640 if (token.windowsCanBeWallpaperTarget()) {
641 adjust = true;
642 break;
643 }
644 }
645 }
646
Wale Ogunwale0303c572016-10-20 10:16:29 -0700647 if (adjust) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800648 adjustWallpaperWindows(dc);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700649 }
650 }
651
Jorim Jaggi879ff722016-11-04 18:08:17 -0700652 void addWallpaperToken(WallpaperWindowToken token) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700653 mWallpaperTokens.add(token);
654 }
655
Jorim Jaggi879ff722016-11-04 18:08:17 -0700656 void removeWallpaperToken(WallpaperWindowToken token) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700657 mWallpaperTokens.remove(token);
658 }
659
660 void dump(PrintWriter pw, String prefix) {
661 pw.print(prefix); pw.print("mWallpaperTarget="); pw.println(mWallpaperTarget);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800662 if (mPrevWallpaperTarget != null) {
663 pw.print(prefix); pw.print("mPrevWallpaperTarget="); pw.println(mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700664 }
665 pw.print(prefix); pw.print("mLastWallpaperX="); pw.print(mLastWallpaperX);
666 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
667 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE
668 || mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
669 pw.print(prefix);
670 pw.print("mLastWallpaperDisplayOffsetX="); pw.print(mLastWallpaperDisplayOffsetX);
671 pw.print(" mLastWallpaperDisplayOffsetY="); pw.println(mLastWallpaperDisplayOffsetY);
672 }
673 }
674
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700675 /** Helper class for storing the results of a wallpaper target find operation. */
676 final private static class FindWallpaperTargetResult {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700677 WindowState topWallpaper = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800678 boolean useTopWallpaperAsTarget = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700679 WindowState wallpaperTarget = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800680 boolean resetTopWallpaper = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700681
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800682 void setTopWallpaper(WindowState win) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700683 topWallpaper = win;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700684 }
685
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800686 void setWallpaperTarget(WindowState win) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700687 wallpaperTarget = win;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800688 }
689
690 void setUseTopWallpaperAsTarget(boolean topWallpaperAsTarget) {
691 useTopWallpaperAsTarget = topWallpaperAsTarget;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700692 }
693
694 void reset() {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700695 topWallpaper = null;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700696 wallpaperTarget = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800697 useTopWallpaperAsTarget = false;
698 resetTopWallpaper = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700699 }
700 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700701}