blob: 2873b6db1a202e1ea86a0aa48493ddbef5e28740 [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) {
Robert Carr217e7cc2018-01-31 18:08:39 -0800278 int xOffset = 0;
279 int yOffset = 0;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700280 boolean rawChanged = false;
Winson4b4ba902016-07-27 19:45:52 -0700281 // Set the default wallpaper x-offset to either edge of the screen (depending on RTL), to
282 // match the behavior of most Launchers
283 float defaultWallpaperX = wallpaperWin.isRtl() ? 1f : 0f;
284 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : defaultWallpaperX;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700285 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
286 int availw = wallpaperWin.mFrame.right - wallpaperWin.mFrame.left - dw;
287 int offset = availw > 0 ? -(int)(availw * wpx + .5f) : 0;
288 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
289 offset += mLastWallpaperDisplayOffsetX;
290 }
Robert Carr217e7cc2018-01-31 18:08:39 -0800291 xOffset = offset;
292
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700293 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
294 wallpaperWin.mWallpaperX = wpx;
295 wallpaperWin.mWallpaperXStep = wpxs;
296 rawChanged = true;
297 }
298
299 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
300 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
301 int availh = wallpaperWin.mFrame.bottom - wallpaperWin.mFrame.top - dh;
302 offset = availh > 0 ? -(int)(availh * wpy + .5f) : 0;
303 if (mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
304 offset += mLastWallpaperDisplayOffsetY;
305 }
Robert Carr217e7cc2018-01-31 18:08:39 -0800306 yOffset = offset;
307
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700308 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
309 wallpaperWin.mWallpaperY = wpy;
310 wallpaperWin.mWallpaperYStep = wpys;
311 rawChanged = true;
312 }
313
Robert Carr217e7cc2018-01-31 18:08:39 -0800314 boolean changed = wallpaperWin.mWinAnimator.setWallpaperOffset(xOffset, yOffset);
315
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700316 if (rawChanged && (wallpaperWin.mAttrs.privateFlags &
317 WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS) != 0) {
318 try {
319 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
320 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
321 + " y=" + wallpaperWin.mWallpaperY);
322 if (sync) {
323 mWaitingOnWallpaper = wallpaperWin;
324 }
325 wallpaperWin.mClient.dispatchWallpaperOffsets(
326 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
327 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
328 if (sync) {
329 if (mWaitingOnWallpaper != null) {
330 long start = SystemClock.uptimeMillis();
331 if ((mLastWallpaperTimeoutTime + WALLPAPER_TIMEOUT_RECOVERY)
332 < start) {
333 try {
334 if (DEBUG_WALLPAPER) Slog.v(TAG,
335 "Waiting for offset complete...");
336 mService.mWindowMap.wait(WALLPAPER_TIMEOUT);
337 } catch (InterruptedException e) {
338 }
339 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
340 if ((start + WALLPAPER_TIMEOUT) < SystemClock.uptimeMillis()) {
341 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
342 + wallpaperWin);
343 mLastWallpaperTimeoutTime = start;
344 }
345 }
346 mWaitingOnWallpaper = null;
347 }
348 }
349 } catch (RemoteException e) {
350 }
351 }
352
353 return changed;
354 }
355
356 void setWindowWallpaperPosition(
357 WindowState window, float x, float y, float xStep, float yStep) {
358 if (window.mWallpaperX != x || window.mWallpaperY != y) {
359 window.mWallpaperX = x;
360 window.mWallpaperY = y;
361 window.mWallpaperXStep = xStep;
362 window.mWallpaperYStep = yStep;
363 updateWallpaperOffsetLocked(window, true);
364 }
365 }
366
367 void setWindowWallpaperDisplayOffset(WindowState window, int x, int y) {
368 if (window.mWallpaperDisplayOffsetX != x || window.mWallpaperDisplayOffsetY != y) {
369 window.mWallpaperDisplayOffsetX = x;
370 window.mWallpaperDisplayOffsetY = y;
371 updateWallpaperOffsetLocked(window, true);
372 }
373 }
374
375 Bundle sendWindowWallpaperCommand(
376 WindowState window, String action, int x, int y, int z, Bundle extras, boolean sync) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800377 if (window == mWallpaperTarget || window == mPrevWallpaperTarget) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700378 boolean doWait = sync;
379 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700380 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700381 token.sendWindowWallpaperCommand(action, x, y, z, extras, sync);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700382 }
383
384 if (doWait) {
385 // TODO: Need to wait for result.
386 }
387 }
388
389 return null;
390 }
391
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700392 private void updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700393 final DisplayContent displayContent = changingTarget.getDisplayContent();
394 if (displayContent == null) {
395 return;
396 }
397 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
398 final int dw = displayInfo.logicalWidth;
399 final int dh = displayInfo.logicalHeight;
400
401 WindowState target = mWallpaperTarget;
402 if (target != null) {
403 if (target.mWallpaperX >= 0) {
404 mLastWallpaperX = target.mWallpaperX;
405 } else if (changingTarget.mWallpaperX >= 0) {
406 mLastWallpaperX = changingTarget.mWallpaperX;
407 }
408 if (target.mWallpaperY >= 0) {
409 mLastWallpaperY = target.mWallpaperY;
410 } else if (changingTarget.mWallpaperY >= 0) {
411 mLastWallpaperY = changingTarget.mWallpaperY;
412 }
413 if (target.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
414 mLastWallpaperDisplayOffsetX = target.mWallpaperDisplayOffsetX;
415 } else if (changingTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
416 mLastWallpaperDisplayOffsetX = changingTarget.mWallpaperDisplayOffsetX;
417 }
418 if (target.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
419 mLastWallpaperDisplayOffsetY = target.mWallpaperDisplayOffsetY;
420 } else if (changingTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
421 mLastWallpaperDisplayOffsetY = changingTarget.mWallpaperDisplayOffsetY;
422 }
423 if (target.mWallpaperXStep >= 0) {
424 mLastWallpaperXStep = target.mWallpaperXStep;
425 } else if (changingTarget.mWallpaperXStep >= 0) {
426 mLastWallpaperXStep = changingTarget.mWallpaperXStep;
427 }
428 if (target.mWallpaperYStep >= 0) {
429 mLastWallpaperYStep = target.mWallpaperYStep;
430 } else if (changingTarget.mWallpaperYStep >= 0) {
431 mLastWallpaperYStep = changingTarget.mWallpaperYStep;
432 }
433 }
434
435 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700436 mWallpaperTokens.get(curTokenNdx).updateWallpaperOffset(dw, dh, sync);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700437 }
438 }
439
440 void clearLastWallpaperTimeoutTime() {
441 mLastWallpaperTimeoutTime = 0;
442 }
443
444 void wallpaperCommandComplete(IBinder window) {
445 if (mWaitingOnWallpaper != null &&
446 mWaitingOnWallpaper.mClient.asBinder() == window) {
447 mWaitingOnWallpaper = null;
448 mService.mWindowMap.notifyAll();
449 }
450 }
451
452 void wallpaperOffsetsComplete(IBinder window) {
453 if (mWaitingOnWallpaper != null &&
454 mWaitingOnWallpaper.mClient.asBinder() == window) {
455 mWaitingOnWallpaper = null;
456 mService.mWindowMap.notifyAll();
457 }
458 }
459
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800460 private void findWallpaperTarget(DisplayContent dc) {
461 mFindResults.reset();
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700462 if (dc.isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800463 // In freeform mode we set the wallpaper as its own target, so we don't need an
464 // additional window to make it visible.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800465 mFindResults.setUseTopWallpaperAsTarget(true);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800466 }
Wale Ogunwale6cbba702016-06-28 16:27:31 -0700467
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800468 dc.forAllWindows(mFindWallpaperTargetFunction, true /* traverseTopToBottom */);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800469
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800470 if (mFindResults.wallpaperTarget == null && mFindResults.useTopWallpaperAsTarget) {
471 mFindResults.setWallpaperTarget(mFindResults.topWallpaper);
Wale Ogunwale21fdd912015-08-20 12:34:57 -0700472 }
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700473 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700474
Jorim Jaggife762342016-10-13 14:33:27 +0200475 private boolean isFullscreen(WindowManager.LayoutParams attrs) {
476 return attrs.x == 0 && attrs.y == 0
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800477 && attrs.width == MATCH_PARENT && attrs.height == MATCH_PARENT;
Jorim Jaggife762342016-10-13 14:33:27 +0200478 }
479
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700480 /** Updates the target wallpaper if needed and returns true if an update happened. */
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800481 private void updateWallpaperWindowsTarget(DisplayContent dc,
482 FindWallpaperTargetResult result) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700483
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700484 WindowState wallpaperTarget = result.wallpaperTarget;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700485
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700486 if (mWallpaperTarget == wallpaperTarget
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800487 || (mPrevWallpaperTarget != null && mPrevWallpaperTarget == wallpaperTarget)) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700488
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800489 if (mPrevWallpaperTarget == null) {
490 return;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700491 }
492
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800493 // Is it time to stop animating?
494 if (!mPrevWallpaperTarget.isAnimatingLw()) {
495 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "No longer animating wallpaper targets!");
496 mPrevWallpaperTarget = null;
497 mWallpaperTarget = wallpaperTarget;
498 }
499 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700500 }
501
502 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800503 "New wallpaper target: " + wallpaperTarget + " prevTarget: " + mWallpaperTarget);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700504
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800505 mPrevWallpaperTarget = null;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700506
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800507 final WindowState prevWallpaperTarget = mWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700508 mWallpaperTarget = wallpaperTarget;
509
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800510 if (wallpaperTarget == null || prevWallpaperTarget == null) {
511 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700512 }
513
514 // Now what is happening... if the current and new targets are animating,
515 // then we are in our super special mode!
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800516 boolean oldAnim = prevWallpaperTarget.isAnimatingLw();
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700517 boolean foundAnim = wallpaperTarget.isAnimatingLw();
518 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
519 "New animation: " + foundAnim + " old animation: " + oldAnim);
520
521 if (!foundAnim || !oldAnim) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800522 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700523 }
524
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800525 if (dc.getWindow(w -> w == prevWallpaperTarget) == null) {
526 return;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700527 }
528
529 final boolean newTargetHidden = wallpaperTarget.mAppToken != null
530 && wallpaperTarget.mAppToken.hiddenRequested;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800531 final boolean oldTargetHidden = prevWallpaperTarget.mAppToken != null
532 && prevWallpaperTarget.mAppToken.hiddenRequested;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700533
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800534 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Animating wallpapers:" + " old: "
535 + prevWallpaperTarget + " hidden=" + oldTargetHidden + " new: " + wallpaperTarget
536 + " hidden=" + newTargetHidden);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700537
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800538 mPrevWallpaperTarget = prevWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700539
540 if (newTargetHidden && !oldTargetHidden) {
541 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Old wallpaper still the target.");
542 // Use the old target if new target is hidden but old target
543 // is not. If they're both hidden, still use the new target.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800544 mWallpaperTarget = prevWallpaperTarget;
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700545 } else if (newTargetHidden == oldTargetHidden
546 && !mService.mOpeningApps.contains(wallpaperTarget.mAppToken)
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800547 && (mService.mOpeningApps.contains(prevWallpaperTarget.mAppToken)
548 || mService.mClosingApps.contains(prevWallpaperTarget.mAppToken))) {
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700549 // If they're both hidden (or both not hidden), prefer the one that's currently in
550 // opening or closing app list, this allows transition selection logic to better
551 // determine the wallpaper status of opening/closing apps.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800552 mWallpaperTarget = prevWallpaperTarget;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700553 }
554
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800555 result.setWallpaperTarget(wallpaperTarget);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700556 }
557
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800558 private void updateWallpaperTokens(boolean visible) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700559 for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700560 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenNdx);
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000561 token.updateWallpaperWindows(visible);
Robert Carrdee1b3f2017-02-27 11:33:33 -0800562 token.getDisplayContent().assignWindowLayers(false);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700563 }
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700564 }
565
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800566 void adjustWallpaperWindows(DisplayContent dc) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700567 mService.mRoot.mWallpaperMayChange = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700568
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700569 // First find top-most window that has asked to be on top of the wallpaper;
570 // all wallpapers go behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800571 findWallpaperTarget(dc);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800572 updateWallpaperWindowsTarget(dc, mFindResults);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700573
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800574 // The window is visible to the compositor...but is it visible to the user?
575 // That is what the wallpaper cares about.
576 final boolean visible = mWallpaperTarget != null && isWallpaperVisible(mWallpaperTarget);
577 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700578
579 if (visible) {
580 if (mWallpaperTarget.mWallpaperX >= 0) {
581 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
582 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
583 }
584 if (mWallpaperTarget.mWallpaperY >= 0) {
585 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
586 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
587 }
588 if (mWallpaperTarget.mWallpaperDisplayOffsetX != Integer.MIN_VALUE) {
589 mLastWallpaperDisplayOffsetX = mWallpaperTarget.mWallpaperDisplayOffsetX;
590 }
591 if (mWallpaperTarget.mWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
592 mLastWallpaperDisplayOffsetY = mWallpaperTarget.mWallpaperDisplayOffsetY;
593 }
594 }
595
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800596 updateWallpaperTokens(visible);
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700597
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800598 if (DEBUG_WALLPAPER_LIGHT) Slog.d(TAG, "New wallpaper: target=" + mWallpaperTarget
599 + " prev=" + mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700600 }
601
602 boolean processWallpaperDrawPendingTimeout() {
603 if (mWallpaperDrawState == WALLPAPER_DRAW_PENDING) {
604 mWallpaperDrawState = WALLPAPER_DRAW_TIMEOUT;
605 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG,
606 "*** WALLPAPER DRAW TIMEOUT");
Winson Chunge2d72172018-01-25 17:46:20 +0000607
608 // If there was a recents animation in progress, cancel that animation
609 if (mService.getRecentsAnimationController() != null) {
610 mService.getRecentsAnimationController().cancelAnimation();
611 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700612 return true;
613 }
614 return false;
615 }
616
617 boolean wallpaperTransitionReady() {
618 boolean transitionReady = true;
619 boolean wallpaperReady = true;
620 for (int curTokenIndex = mWallpaperTokens.size() - 1;
621 curTokenIndex >= 0 && wallpaperReady; curTokenIndex--) {
Jorim Jaggi879ff722016-11-04 18:08:17 -0700622 final WallpaperWindowToken token = mWallpaperTokens.get(curTokenIndex);
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700623 if (token.hasVisibleNotDrawnWallpaper()) {
624 // We've told this wallpaper to be visible, but it is not drawn yet
625 wallpaperReady = false;
626 if (mWallpaperDrawState != WALLPAPER_DRAW_TIMEOUT) {
627 // wait for this wallpaper until it is drawn or timeout
628 transitionReady = false;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700629 }
Wale Ogunwalee4da0c12016-07-29 12:47:02 -0700630 if (mWallpaperDrawState == WALLPAPER_DRAW_NORMAL) {
631 mWallpaperDrawState = WALLPAPER_DRAW_PENDING;
632 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT);
633 mService.mH.sendEmptyMessageDelayed(WALLPAPER_DRAW_PENDING_TIMEOUT,
634 WALLPAPER_DRAW_PENDING_TIMEOUT_DURATION);
635 }
636 if (DEBUG_APP_TRANSITIONS || DEBUG_WALLPAPER) Slog.v(TAG,
637 "Wallpaper should be visible but has not been drawn yet. " +
638 "mWallpaperDrawState=" + mWallpaperDrawState);
639 break;
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700640 }
641 }
642 if (wallpaperReady) {
643 mWallpaperDrawState = WALLPAPER_DRAW_NORMAL;
644 mService.mH.removeMessages(WALLPAPER_DRAW_PENDING_TIMEOUT);
645 }
646
647 return transitionReady;
648 }
649
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700650 /**
651 * Adjusts the wallpaper windows if the input display has a pending wallpaper layout or one of
652 * the opening apps should be a wallpaper target.
653 */
Wale Ogunwale0303c572016-10-20 10:16:29 -0700654 void adjustWallpaperWindowsForAppTransitionIfNeeded(DisplayContent dc,
655 ArraySet<AppWindowToken> openingApps) {
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700656 boolean adjust = false;
657 if ((dc.pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
658 adjust = true;
659 } else {
660 for (int i = openingApps.size() - 1; i >= 0; --i) {
661 final AppWindowToken token = openingApps.valueAt(i);
662 if (token.windowsCanBeWallpaperTarget()) {
663 adjust = true;
664 break;
665 }
666 }
667 }
668
Wale Ogunwale0303c572016-10-20 10:16:29 -0700669 if (adjust) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800670 adjustWallpaperWindows(dc);
Wale Ogunwale0c4a40ef2016-10-07 07:06:40 -0700671 }
672 }
673
Jorim Jaggi879ff722016-11-04 18:08:17 -0700674 void addWallpaperToken(WallpaperWindowToken token) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700675 mWallpaperTokens.add(token);
676 }
677
Jorim Jaggi879ff722016-11-04 18:08:17 -0700678 void removeWallpaperToken(WallpaperWindowToken token) {
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700679 mWallpaperTokens.remove(token);
680 }
681
682 void dump(PrintWriter pw, String prefix) {
683 pw.print(prefix); pw.print("mWallpaperTarget="); pw.println(mWallpaperTarget);
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800684 if (mPrevWallpaperTarget != null) {
685 pw.print(prefix); pw.print("mPrevWallpaperTarget="); pw.println(mPrevWallpaperTarget);
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700686 }
687 pw.print(prefix); pw.print("mLastWallpaperX="); pw.print(mLastWallpaperX);
688 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
689 if (mLastWallpaperDisplayOffsetX != Integer.MIN_VALUE
690 || mLastWallpaperDisplayOffsetY != Integer.MIN_VALUE) {
691 pw.print(prefix);
692 pw.print("mLastWallpaperDisplayOffsetX="); pw.print(mLastWallpaperDisplayOffsetX);
693 pw.print(" mLastWallpaperDisplayOffsetY="); pw.println(mLastWallpaperDisplayOffsetY);
694 }
695 }
696
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700697 /** Helper class for storing the results of a wallpaper target find operation. */
698 final private static class FindWallpaperTargetResult {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700699 WindowState topWallpaper = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800700 boolean useTopWallpaperAsTarget = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700701 WindowState wallpaperTarget = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800702 boolean resetTopWallpaper = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700703
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800704 void setTopWallpaper(WindowState win) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700705 topWallpaper = win;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700706 }
707
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800708 void setWallpaperTarget(WindowState win) {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700709 wallpaperTarget = win;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800710 }
711
712 void setUseTopWallpaperAsTarget(boolean topWallpaperAsTarget) {
713 useTopWallpaperAsTarget = topWallpaperAsTarget;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700714 }
715
716 void reset() {
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700717 topWallpaper = null;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700718 wallpaperTarget = null;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800719 useTopWallpaperAsTarget = false;
720 resetTopWallpaper = false;
Wale Ogunwale5f61d042015-08-20 10:09:47 -0700721 }
722 }
Wale Ogunwalee8069dc2015-08-18 09:52:01 -0700723}