blob: a7d51f175ab55c9598a0191b63cede034d6bda55 [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();
Jorim Jaggi82c17862018-02-21 17:50:18 +0100154 final boolean animationWallpaper = w.mAppToken != null && w.mAppToken.getAnimation() != null
155 && w.mAppToken.getAnimation().getShowWallpaper();
156 final boolean hasWallpaper = (w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0
157 || animationWallpaper;
Winson Chunge2d72172018-01-25 17:46:20 +0000158 final boolean isRecentsTransitionTarget = (recentsAnimationController != null
159 && recentsAnimationController.isWallpaperVisible(w));
160 if (isRecentsTransitionTarget) {
161 if (DEBUG_WALLPAPER) Slog.v(TAG, "Found recents animation wallpaper target: " + w);
162 mFindResults.setWallpaperTarget(w);
163 return true;
164 } else if (hasWallpaper && w.isOnScreen()
165 && (mWallpaperTarget == w || w.isDrawFinishedLw())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800166 if (DEBUG_WALLPAPER) Slog.v(TAG, "Found wallpaper target: " + w);
167 mFindResults.setWallpaperTarget(w);
168 if (w == mWallpaperTarget && w.mWinAnimator.isAnimationSet()) {
169 // The current wallpaper target is animating, so we'll look behind it for
170 // another possible target and figure out what is going on later.
171 if (DEBUG_WALLPAPER) Slog.v(TAG,
172 "Win " + w + ": token animating, looking behind.");
173 }
174 // Found a target! End search.
175 return true;
176 } else if (w == winAnimator.mWindowDetachedWallpaper) {
177 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
178 "Found animating detached wallpaper target win: " + w);
179 mFindResults.setUseTopWallpaperAsTarget(true);
180 }
181 return false;
182 };
183
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700184 public WallpaperController(WindowManagerService service) {
185 mService = service;
186 }
187
188 WindowState getWallpaperTarget() {
189 return mWallpaperTarget;
190 }
191
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700192 boolean isWallpaperTarget(WindowState win) {
193 return win == mWallpaperTarget;
194 }
195
196 boolean isBelowWallpaperTarget(WindowState win) {
197 return mWallpaperTarget != null && mWallpaperTarget.mLayer >= win.mBaseLayer;
198 }
199
200 boolean isWallpaperVisible() {
201 return isWallpaperVisible(mWallpaperTarget);
202 }
203
Jorim Jaggife762342016-10-13 14:33:27 +0200204 /**
205 * Starts {@param a} on all wallpaper windows.
206 */
207 void startWallpaperAnimation(Animation a) {
208 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700209 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Jorim Jaggife762342016-10-13 14:33:27 +0200210 token.startAnimation(a);
211 }
212 }
213
Winson Chunge2d72172018-01-25 17:46:20 +0000214 private final boolean isWallpaperVisible(WindowState wallpaperTarget) {
215 final RecentsAnimationController recentsAnimationController =
216 mService.getRecentsAnimationController();
217 boolean isAnimatingWithRecentsComponent = recentsAnimationController != null
218 && recentsAnimationController.isWallpaperVisible(wallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700219 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper vis: target " + wallpaperTarget + ", obscured="
220 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200221 + " animating=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
222 ? wallpaperTarget.mAppToken.isSelfAnimating() : null)
Winson Chunge2d72172018-01-25 17:46:20 +0000223 + " prev=" + mPrevWallpaperTarget
224 + " recentsAnimationWallpaperVisible=" + isAnimatingWithRecentsComponent);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700225 return (wallpaperTarget != null
Winson Chunge2d72172018-01-25 17:46:20 +0000226 && (!wallpaperTarget.mObscured
227 || isAnimatingWithRecentsComponent
228 || (wallpaperTarget.mAppToken != null
229 && wallpaperTarget.mAppToken.isSelfAnimating())))
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800230 || mPrevWallpaperTarget != null;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700231 }
232
233 boolean isWallpaperTargetAnimating() {
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700234 return mWallpaperTarget != null && mWallpaperTarget.mWinAnimator.isAnimationSet()
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200235 && (mWallpaperTarget.mAppToken == null
236 || !mWallpaperTarget.mAppToken.isWaitingForTransitionStart());
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700237 }
238
239 void updateWallpaperVisibility() {
Filip Gruszczynski63a35e22015-11-05 15:38:59 -0800240 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700241
242 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700243 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700244 token.updateWallpaperVisibility(visible);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700245 }
246 }
247
248 void hideDeferredWallpapersIfNeeded() {
249 if (mDeferredHideWallpaper != null) {
250 hideWallpapers(mDeferredHideWallpaper);
251 mDeferredHideWallpaper = null;
252 }
253 }
254
255 void hideWallpapers(final WindowState winGoingAway) {
256 if (mWallpaperTarget != null
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800257 && (mWallpaperTarget != winGoingAway || mPrevWallpaperTarget != null)) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700258 return;
259 }
260 if (mService.mAppTransition.isRunning()) {
261 // Defer hiding the wallpaper when app transition is running until the animations
262 // are done.
263 mDeferredHideWallpaper = winGoingAway;
264 return;
265 }
266
267 final boolean wasDeferred = (mDeferredHideWallpaper == winGoingAway);
268 for (int i = mWallpaperTokens.size() - 1; i >= 0; i--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700269 final WallpaperWindowToken token = mWallpaperTokens.get(i);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700270 token.hideWallpaperToken(wasDeferred, "hideWallpapers");
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200271 if (DEBUG_WALLPAPER_LIGHT && !token.isHidden()) Slog.d(TAG, "Hiding wallpaper " + token
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800272 + " from " + winGoingAway + " target=" + mWallpaperTarget + " prev="
273 + mPrevWallpaperTarget + "\n" + Debug.getCallers(5, " "));
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700274 }
275 }
276
277 boolean updateWallpaperOffset(WindowState wallpaperWin, int dw, int dh, boolean sync) {
278 boolean rawChanged = false;
Winson4b4ba902016-07-27 19:45:52 -0700279 // Set the default wallpaper x-offset to either edge of the screen (depending on RTL), to
280 // match the behavior of most Launchers
281 float defaultWallpaperX = wallpaperWin.isRtl() ? 1f : 0f;
282 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : defaultWallpaperX;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700283 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
284 int availw = wallpaperWin.mFrame.right - wallpaperWin.mFrame.left - dw;
285 int offset = availw > 0 ? -(int)(availw * wpx + .5f) : 0;
286 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
287 offset += mLastWallpaperDisplayOffsetX;
288 }
Rob Carr06be6bb2018-02-06 16:26:06 +0000289 boolean changed = wallpaperWin.mXOffset != offset;
290 if (changed) {
291 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper " + wallpaperWin + " x: " + offset);
292 wallpaperWin.mXOffset = offset;
293 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700294 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
295 wallpaperWin.mWallpaperX = wpx;
296 wallpaperWin.mWallpaperXStep = wpxs;
297 rawChanged = true;
298 }
299
300 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
301 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
302 int availh = wallpaperWin.mFrame.bottom - wallpaperWin.mFrame.top - dh;
303 offset = availh > 0 ? -(int)(availh * wpy + .5f) : 0;
304 if (mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
305 offset += mLastWallpaperDisplayOffsetY;
306 }
Rob Carr06be6bb2018-02-06 16:26:06 +0000307 if (wallpaperWin.mYOffset != offset) {
308 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper " + wallpaperWin + " y: " + offset);
309 changed = true;
310 wallpaperWin.mYOffset = offset;
311 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700312 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
313 wallpaperWin.mWallpaperY = wpy;
314 wallpaperWin.mWallpaperYStep = wpys;
315 rawChanged = true;
316 }
317
318 if (rawChanged && (wallpaperWin.mAttrs.privateFlags &
319 WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS) != 0) {
320 try {
321 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
322 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
323 + " y=" + wallpaperWin.mWallpaperY);
324 if (sync) {
325 mWaitingOnWallpaper = wallpaperWin;
326 }
327 wallpaperWin.mClient.dispatchWallpaperOffsets(
328 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
329 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
330 if (sync) {
331 if (mWaitingOnWallpaper != null) {
332 long start = SystemClock.uptimeMillis();
333 if ((mLastWallpaperTimeoutTime + WALLPAPER_TIMEOUT_RECOVERY)
334 < start) {
335 try {
336 if (DEBUG_WALLPAPER) Slog.v(TAG,
337 "Waiting for offset complete...");
338 mService.mWindowMap.wait(WALLPAPER_TIMEOUT);
339 } catch (InterruptedException e) {
340 }
341 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
342 if ((start + WALLPAPER_TIMEOUT) < SystemClock.uptimeMillis()) {
343 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
344 + wallpaperWin);
345 mLastWallpaperTimeoutTime = start;
346 }
347 }
348 mWaitingOnWallpaper = null;
349 }
350 }
351 } catch (RemoteException e) {
352 }
353 }
354
355 return changed;
356 }
357
358 void setWindowWallpaperPosition(
359 WindowState window, float x, float y, float xStep, float yStep) {
360 if (window.mWallpaperX != x || window.mWallpaperY != y) {
361 window.mWallpaperX = x;
362 window.mWallpaperY = y;
363 window.mWallpaperXStep = xStep;
364 window.mWallpaperYStep = yStep;
365 updateWallpaperOffsetLocked(window, true);
366 }
367 }
368
369 void setWindowWallpaperDisplayOffset(WindowState window, int x, int y) {
370 if (window.mWallpaperDisplayOffsetX != x || window.mWallpaperDisplayOffsetY != y) {
371 window.mWallpaperDisplayOffsetX = x;
372 window.mWallpaperDisplayOffsetY = y;
373 updateWallpaperOffsetLocked(window, true);
374 }
375 }
376
377 Bundle sendWindowWallpaperCommand(
378 WindowState window, String action, int x, int y, int z, Bundle extras, boolean sync) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800379 if (window == mWallpaperTarget || window == mPrevWallpaperTarget) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700380 boolean doWait = sync;
381 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700382 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700383 token.sendWindowWallpaperCommand(action, x, y, z, extras, sync);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700384 }
385
386 if (doWait) {
387 // TODO: Need to wait for result.
388 }
389 }
390
391 return null;
392 }
393
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700394 private void updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700395 final DisplayContent displayContent = changingTarget.getDisplayContent();
396 if (displayContent == null) {
397 return;
398 }
399 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
400 final int dw = displayInfo.logicalWidth;
401 final int dh = displayInfo.logicalHeight;
402
403 WindowState target = mWallpaperTarget;
404 if (target != null) {
405 if (target.mWallpaperX >= 0) {
406 mLastWallpaperX = target.mWallpaperX;
407 } else if (changingTarget.mWallpaperX >= 0) {
408 mLastWallpaperX = changingTarget.mWallpaperX;
409 }
410 if (target.mWallpaperY >= 0) {
411 mLastWallpaperY = target.mWallpaperY;
412 } else if (changingTarget.mWallpaperY >= 0) {
413 mLastWallpaperY = changingTarget.mWallpaperY;
414 }
415 if (target.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
416 mLastWallpaperDisplayOffsetX = target.mWallpaperDisplayOffsetX;
417 } else if (changingTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
418 mLastWallpaperDisplayOffsetX = changingTarget.mWallpaperDisplayOffsetX;
419 }
420 if (target.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
421 mLastWallpaperDisplayOffsetY = target.mWallpaperDisplayOffsetY;
422 } else if (changingTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
423 mLastWallpaperDisplayOffsetY = changingTarget.mWallpaperDisplayOffsetY;
424 }
425 if (target.mWallpaperXStep >= 0) {
426 mLastWallpaperXStep = target.mWallpaperXStep;
427 } else if (changingTarget.mWallpaperXStep >= 0) {
428 mLastWallpaperXStep = changingTarget.mWallpaperXStep;
429 }
430 if (target.mWallpaperYStep >= 0) {
431 mLastWallpaperYStep = target.mWallpaperYStep;
432 } else if (changingTarget.mWallpaperYStep >= 0) {
433 mLastWallpaperYStep = changingTarget.mWallpaperYStep;
434 }
435 }
436
437 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700438 mWallpaperTokens.get(curTokenNdx).updateWallpaperOffset(dw, dh, sync);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700439 }
440 }
441
442 void clearLastWallpaperTimeoutTime() {
443 mLastWallpaperTimeoutTime = 0;
444 }
445
446 void wallpaperCommandComplete(IBinder window) {
447 if (mWaitingOnWallpaper != null &&
448 mWaitingOnWallpaper.mClient.asBinder() == window) {
449 mWaitingOnWallpaper = null;
450 mService.mWindowMap.notifyAll();
451 }
452 }
453
454 void wallpaperOffsetsComplete(IBinder window) {
455 if (mWaitingOnWallpaper != null &&
456 mWaitingOnWallpaper.mClient.asBinder() == window) {
457 mWaitingOnWallpaper = null;
458 mService.mWindowMap.notifyAll();
459 }
460 }
461
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800462 private void findWallpaperTarget(DisplayContent dc) {
463 mFindResults.reset();
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700464 if (dc.isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800465 // In freeform mode we set the wallpaper as its own target, so we don't need an
466 // additional window to make it visible.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800467 mFindResults.setUseTopWallpaperAsTarget(true);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800468 }
Wale Ogunwale6cbba702016-06-28 16:27:31 -0700469
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800470 dc.forAllWindows(mFindWallpaperTargetFunction, true /* traverseTopToBottom */);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800471
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800472 if (mFindResults.wallpaperTarget == null && mFindResults.useTopWallpaperAsTarget) {
473 mFindResults.setWallpaperTarget(mFindResults.topWallpaper);
Wale Ogunwale21fdd912015-08-20 12:34:57 -0700474 }
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700475 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700476
Jorim Jaggife762342016-10-13 14:33:27 +0200477 private boolean isFullscreen(WindowManager.LayoutParams attrs) {
478 return attrs.x == 0 && attrs.y == 0
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800479 && attrs.width == MATCH_PARENT && attrs.height == MATCH_PARENT;
Jorim Jaggife762342016-10-13 14:33:27 +0200480 }
481
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700482 /** Updates the target wallpaper if needed and returns true if an update happened. */
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800483 private void updateWallpaperWindowsTarget(DisplayContent dc,
484 FindWallpaperTargetResult result) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700485
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700486 WindowState wallpaperTarget = result.wallpaperTarget;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700487
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700488 if (mWallpaperTarget == wallpaperTarget
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800489 || (mPrevWallpaperTarget != null && mPrevWallpaperTarget == wallpaperTarget)) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700490
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800491 if (mPrevWallpaperTarget == null) {
492 return;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700493 }
494
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800495 // Is it time to stop animating?
496 if (!mPrevWallpaperTarget.isAnimatingLw()) {
497 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "No longer animating wallpaper targets!");
498 mPrevWallpaperTarget = null;
499 mWallpaperTarget = wallpaperTarget;
500 }
501 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700502 }
503
504 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800505 "New wallpaper target: " + wallpaperTarget + " prevTarget: " + mWallpaperTarget);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700506
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800507 mPrevWallpaperTarget = null;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700508
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800509 final WindowState prevWallpaperTarget = mWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700510 mWallpaperTarget = wallpaperTarget;
511
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800512 if (wallpaperTarget == null || prevWallpaperTarget == null) {
513 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700514 }
515
516 // Now what is happening... if the current and new targets are animating,
517 // then we are in our super special mode!
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800518 boolean oldAnim = prevWallpaperTarget.isAnimatingLw();
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700519 boolean foundAnim = wallpaperTarget.isAnimatingLw();
520 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
521 "New animation: " + foundAnim + " old animation: " + oldAnim);
522
523 if (!foundAnim || !oldAnim) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800524 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700525 }
526
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800527 if (dc.getWindow(w -> w == prevWallpaperTarget) == null) {
528 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700529 }
530
531 final boolean newTargetHidden = wallpaperTarget.mAppToken != null
532 && wallpaperTarget.mAppToken.hiddenRequested;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800533 final boolean oldTargetHidden = prevWallpaperTarget.mAppToken != null
534 && prevWallpaperTarget.mAppToken.hiddenRequested;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700535
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800536 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Animating wallpapers:" + " old: "
537 + prevWallpaperTarget + " hidden=" + oldTargetHidden + " new: " + wallpaperTarget
538 + " hidden=" + newTargetHidden);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700539
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800540 mPrevWallpaperTarget = prevWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700541
542 if (newTargetHidden && !oldTargetHidden) {
543 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Old wallpaper still the target.");
544 // Use the old target if new target is hidden but old target
545 // is not. If they're both hidden, still use the new target.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800546 mWallpaperTarget = prevWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700547 } else if (newTargetHidden == oldTargetHidden
548 && !mService.mOpeningApps.contains(wallpaperTarget.mAppToken)
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800549 && (mService.mOpeningApps.contains(prevWallpaperTarget.mAppToken)
550 || mService.mClosingApps.contains(prevWallpaperTarget.mAppToken))) {
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700551 // If they're both hidden (or both not hidden), prefer the one that's currently in
552 // opening or closing app list, this allows transition selection logic to better
553 // determine the wallpaper status of opening/closing apps.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800554 mWallpaperTarget = prevWallpaperTarget;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700555 }
556
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800557 result.setWallpaperTarget(wallpaperTarget);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700558 }
559
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800560 private void updateWallpaperTokens(boolean visible) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700561 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700562 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000563 token.updateWallpaperWindows(visible);
Robert Carrdee1b3f2017-02-27 11:33:33 -0800564 token.getDisplayContent().assignWindowLayers(false);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700565 }
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700566 }
567
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800568 void adjustWallpaperWindows(DisplayContent dc) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700569 mService.mRoot.mWallpaperMayChange = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700570
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700571 // First find top-most window that has asked to be on top of the wallpaper;
572 // all wallpapers go behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800573 findWallpaperTarget(dc);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800574 updateWallpaperWindowsTarget(dc, mFindResults);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700575
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800576 // The window is visible to the compositor...but is it visible to the user?
577 // That is what the wallpaper cares about.
578 final boolean visible = mWallpaperTarget != null && isWallpaperVisible(mWallpaperTarget);
579 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700580
581 if (visible) {
582 if (mWallpaperTarget.mWallpaperX >= 0) {
583 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
584 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
585 }
586 if (mWallpaperTarget.mWallpaperY >= 0) {
587 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
588 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
589 }
590 if (mWallpaperTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
591 mLastWallpaperDisplayOffsetX = mWallpaperTarget.mWallpaperDisplayOffsetX;
592 }
593 if (mWallpaperTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
594 mLastWallpaperDisplayOffsetY = mWallpaperTarget.mWallpaperDisplayOffsetY;
595 }
596 }
597
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800598 updateWallpaperTokens(visible);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700599
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800600 if (DEBUG_WALLPAPER_LIGHT) Slog.d(TAG, "New wallpaper: target=" + mWallpaperTarget
601 + " prev=" + mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700602 }
603
604 boolean processWallpaperDrawPendingTimeout() {
605 if (mWallpaperDrawState == WALLPAPER_DRAW_PENDING) {
606 mWallpaperDrawState = WALLPAPER_DRAW_TIMEOUT;
607 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG,
608 "*** WALLPAPER DRAW TIMEOUT");
Winson Chunge2d72172018-01-25 17:46:20 +0000609
610 // If there was a recents animation in progress, cancel that animation
611 if (mService.getRecentsAnimationController() != null) {
612 mService.getRecentsAnimationController().cancelAnimation();
613 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700614 return true;
615 }
616 return false;
617 }
618
619 boolean wallpaperTransitionReady() {
620 boolean transitionReady = true;
621 boolean wallpaperReady = true;
622 for (int curTokenIndex = mWallpaperTokens.size() - 1;
623 curTokenIndex >= 0 && wallpaperReady; curTokenIndex--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700624 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenIndex);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700625 if (token.hasVisibleNotDrawnWallpaper()) {
626 // We've told this wallpaper to be visible, but it is not drawn yet
627 wallpaperReady = false;
628 if (mWallpaperDrawState != WALLPAPER_DRAW_TIMEOUT) {
629 // wait for this wallpaper until it is drawn or timeout
630 transitionReady = false;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700631 }
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700632 if (mWallpaperDrawState == WALLPAPER_DRAW_NORMAL) {
633 mWallpaperDrawState = WALLPAPER_DRAW_PENDING;
634 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT);
635 mService.mH.sendEmptyMessageDelayed(WALLPAPER_DRAW_PENDING_TIMEOUT,
636 WALLPAPER_DRAW_PENDING_TIMEOUT_DURATION);
637 }
638 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG,
639 "Wallpaper should be visible but has not been drawn yet. " +
640 "mWallpaperDrawState=" + mWallpaperDrawState);
641 break;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700642 }
643 }
644 if (wallpaperReady) {
645 mWallpaperDrawState = WALLPAPER_DRAW_NORMAL;
646 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT);
647 }
648
649 return transitionReady;
650 }
651
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700652 /**
653 * Adjusts the wallpaper windows if the input display has a pending wallpaper layout or one of
654 * the opening apps should be a wallpaper target.
655 */
Wale Ogunwale0303c572016-10-20 10:16:29 -0700656 void adjustWallpaperWindowsForAppTransitionIfNeeded(DisplayContent dc,
657 ArraySet<AppWindowToken> openingApps) {
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700658 boolean adjust = false;
659 if ((dc.pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
660 adjust = true;
661 } else {
662 for (int i = openingApps.size() - 1; i >= 0; --i) {
663 final AppWindowToken token = openingApps.valueAt(i);
664 if (token.windowsCanBeWallpaperTarget()) {
665 adjust = true;
666 break;
667 }
668 }
669 }
670
Wale Ogunwale0303c572016-10-20 10:16:29 -0700671 if (adjust) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800672 adjustWallpaperWindows(dc);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700673 }
674 }
675
Jorim Jaggi879ff722016-11-04 18:08:17 -0700676 void addWallpaperToken(WallpaperWindowToken token) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700677 mWallpaperTokens.add(token);
678 }
679
Jorim Jaggi879ff722016-11-04 18:08:17 -0700680 void removeWallpaperToken(WallpaperWindowToken token) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700681 mWallpaperTokens.remove(token);
682 }
683
684 void dump(PrintWriter pw, String prefix) {
685 pw.print(prefix); pw.print("mWallpaperTarget="); pw.println(mWallpaperTarget);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800686 if (mPrevWallpaperTarget != null) {
687 pw.print(prefix); pw.print("mPrevWallpaperTarget="); pw.println(mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700688 }
689 pw.print(prefix); pw.print("mLastWallpaperX="); pw.print(mLastWallpaperX);
690 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
691 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE
692 || mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
693 pw.print(prefix);
694 pw.print("mLastWallpaperDisplayOffsetX="); pw.print(mLastWallpaperDisplayOffsetX);
695 pw.print(" mLastWallpaperDisplayOffsetY="); pw.println(mLastWallpaperDisplayOffsetY);
696 }
697 }
698
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700699 /** Helper class for storing the results of a wallpaper target find operation. */
700 final private static class FindWallpaperTargetResult {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700701 WindowState topWallpaper = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800702 boolean useTopWallpaperAsTarget = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700703 WindowState wallpaperTarget = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800704 boolean resetTopWallpaper = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700705
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800706 void setTopWallpaper(WindowState win) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700707 topWallpaper = win;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700708 }
709
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800710 void setWallpaperTarget(WindowState win) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700711 wallpaperTarget = win;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800712 }
713
714 void setUseTopWallpaperAsTarget(boolean topWallpaperAsTarget) {
715 useTopWallpaperAsTarget = topWallpaperAsTarget;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700716 }
717
718 void reset() {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700719 topWallpaper = null;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700720 wallpaperTarget = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800721 useTopWallpaperAsTarget = false;
722 resetTopWallpaper = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700723 }
724 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700725}