Files
Flexera-Launcher/quickstep/src/com/android/launcher3/statehandlers/DepthController.java
T
Vinay Joglekar 8f29707aee Fix app tile blur when clicked on recents button rapidly.
When we tap on recents, setActivityStarted set to true where
setBaseSurface is called with launcher surface. When app is opened
setActivityStarted is false and setBaseSurface is set to null.

In subsequent calls, between where call to setBaseSurface with launcher
is skipped. This causes due to race condition between
removeOnDrawListener which is "asynchronously" posted from
onLauncherDraw and addOnDrawListener. This makes addOnDrawListener not
to get properly added.

Hence mBaseSurface remains null and we do not get chance to set the blur
to launcher layer to 0 and then reparent/relayer blur behind live tile.
Hence blur is still over launcher layer which makes live tile blur.

Added boolean that makes async removeOnDrawListener in onLauncherDraw
cancellable.

Fix: 387428198
Test: Rapidly tap on recents button
Flag: com.android.launcher3.enable_overview_background_wallpaper_blur
Change-Id: I5ed2ec198563d9edb181a723c9db47ecfe500c5d
2025-06-05 09:20:16 -07:00

237 lines
8.7 KiB
Java

/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.statehandlers;
import static com.android.app.animation.Interpolators.LINEAR;
import static com.android.launcher3.states.StateAnimationConfig.ANIM_DEPTH;
import static com.android.launcher3.states.StateAnimationConfig.SKIP_DEPTH_CONTROLLER;
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
import static com.android.launcher3.util.MultiPropertyFactory.MULTI_PROPERTY_VALUE;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.view.CrossWindowBlurListeners;
import android.view.View;
import android.view.ViewRootImpl;
import android.view.ViewTreeObserver;
import androidx.annotation.VisibleForTesting;
import com.android.launcher3.BaseActivity;
import com.android.launcher3.LauncherState;
import com.android.launcher3.anim.PendingAnimation;
import com.android.launcher3.statemanager.StateManager.StateHandler;
import com.android.launcher3.states.StateAnimationConfig;
import com.android.launcher3.uioverrides.QuickstepLauncher;
import com.android.quickstep.util.BaseDepthController;
import java.io.PrintWriter;
import java.util.function.Consumer;
/**
* Controls blur and wallpaper zoom, for the Launcher surface only.
*/
public class DepthController extends BaseDepthController implements StateHandler<LauncherState>,
BaseActivity.MultiWindowModeChangedListener {
@VisibleForTesting
final ViewTreeObserver.OnDrawListener mOnDrawListener = this::onLauncherDraw;
private final Consumer<Boolean> mCrossWindowBlurListener = this::setCrossWindowBlursEnabled;
private final Runnable mOpaquenessListener = this::applyDepthAndBlur;
// Workaround for animating the depth when multiwindow mode changes.
private boolean mIgnoreStateChangesDuringMultiWindowAnimation = false;
private View.OnAttachStateChangeListener mOnAttachListener;
// Ensure {@link mOnDrawListener} is added only once to avoid spamming DragLayer's mRunQueue
// via {@link View#post(Runnable)}
private boolean mIsOnDrawListenerAdded = false;
private boolean mRemoveOnDrawListenerCancelled = false;
public DepthController(QuickstepLauncher launcher) {
super(launcher);
}
private void onLauncherDraw() {
View view = mLauncher.getDragLayer();
ViewRootImpl viewRootImpl = view.getViewRootImpl();
setBaseSurface(viewRootImpl != null ? viewRootImpl.getSurfaceControl() : null);
mRemoveOnDrawListenerCancelled = false;
view.post(() -> {
if (!mRemoveOnDrawListenerCancelled) {
removeOnDrawListener();
}
});
}
private void ensureDependencies() {
View rootView = mLauncher.getRootView();
if (rootView == null) {
return;
}
if (mOnAttachListener != null) {
return;
}
mOnAttachListener = new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View view) {
UI_HELPER_EXECUTOR.execute(() ->
CrossWindowBlurListeners.getInstance().addListener(
mLauncher.getMainExecutor(), mCrossWindowBlurListener));
mLauncher.getScrimView().addOpaquenessListener(mOpaquenessListener);
// To handle the case where window token is invalid during last setDepth call.
applyDepthAndBlur();
}
@Override
public void onViewDetachedFromWindow(View view) {
removeSecondaryListeners();
}
};
rootView.addOnAttachStateChangeListener(mOnAttachListener);
if (rootView.isAttachedToWindow()) {
mOnAttachListener.onViewAttachedToWindow(rootView);
}
}
/**
* Cleans up after this controller so it can be garbage collected without leaving traces.
*/
public void dispose() {
removeSecondaryListeners();
if (mLauncher.getRootView() != null && mOnAttachListener != null) {
mLauncher.getRootView().removeOnAttachStateChangeListener(mOnAttachListener);
mOnAttachListener = null;
}
}
private void removeSecondaryListeners() {
UI_HELPER_EXECUTOR.execute(() ->
CrossWindowBlurListeners.getInstance()
.removeListener(mCrossWindowBlurListener));
if (mOpaquenessListener != null) {
mLauncher.getScrimView().removeOpaquenessListener(mOpaquenessListener);
}
}
/**
* Sets if the underlying activity is started or not
*/
public void setActivityStarted(boolean isStarted) {
if (isStarted) {
addOnDrawListener();
} else {
removeOnDrawListener();
setBaseSurface(null);
setEarlyWakeup(false);
}
}
@Override
public void setState(LauncherState toState) {
if (mIgnoreStateChangesDuringMultiWindowAnimation) {
return;
}
stateDepth.setValue(toState.getDepth(mLauncher));
if (toState == LauncherState.BACKGROUND_APP) {
addOnDrawListener();
}
}
@Override
public void setStateWithAnimation(LauncherState toState, StateAnimationConfig config,
PendingAnimation animation) {
if (config.hasAnimationFlag(SKIP_DEPTH_CONTROLLER)
|| mIgnoreStateChangesDuringMultiWindowAnimation) {
return;
}
float toDepth = toState.getDepth(mLauncher);
animation.setFloat(stateDepth, MULTI_PROPERTY_VALUE, toDepth,
config.getInterpolator(ANIM_DEPTH, LINEAR));
}
@Override
protected void applyDepthAndBlur() {
ensureDependencies();
super.applyDepthAndBlur();
}
@Override
protected void onInvalidSurface() {
// Lets wait for surface to become valid again
addOnDrawListener();
}
private void addOnDrawListener() {
mRemoveOnDrawListenerCancelled = true;
if (mIsOnDrawListenerAdded) {
return;
}
mLauncher.getDragLayer().getViewTreeObserver().addOnDrawListener(mOnDrawListener);
mIsOnDrawListenerAdded = true;
}
private void removeOnDrawListener() {
mRemoveOnDrawListenerCancelled = true;
if (!mIsOnDrawListenerAdded) {
return;
}
mLauncher.getDragLayer().getViewTreeObserver().removeOnDrawListener(mOnDrawListener);
mIsOnDrawListenerAdded = false;
}
@Override
public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
mIgnoreStateChangesDuringMultiWindowAnimation = true;
ObjectAnimator mwAnimation = ObjectAnimator.ofFloat(stateDepth, MULTI_PROPERTY_VALUE,
mLauncher.getStateManager().getState().getDepth(mLauncher, isInMultiWindowMode))
.setDuration(300);
mwAnimation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mIgnoreStateChangesDuringMultiWindowAnimation = false;
}
});
mwAnimation.setAutoCancel(true);
mwAnimation.start();
}
public void dump(String prefix, PrintWriter writer) {
writer.println(prefix + "DepthController");
writer.println(prefix + "\tmMaxBlurRadius=" + mMaxBlurRadius);
writer.println(prefix + "\tmCrossWindowBlursEnabled=" + mCrossWindowBlursEnabled);
writer.println(prefix + "\tmBaseSurface=" + mBaseSurface);
writer.println(prefix + "\tmBaseSurfaceOverride=" + mBaseSurfaceOverride);
writer.println(prefix + "\tmStateDepth=" + stateDepth.getValue());
writer.println(prefix + "\tmWidgetDepth=" + widgetDepth.getValue());
writer.println(prefix + "\tmCurrentBlur=" + mCurrentBlur);
writer.println(prefix + "\tmInEarlyWakeUp=" + mInEarlyWakeUp);
writer.println(prefix + "\tmIgnoreStateChangesDuringMultiWindowAnimation="
+ mIgnoreStateChangesDuringMultiWindowAnimation);
writer.println(prefix + "\tmPauseBlurs=" + mPauseBlurs);
writer.println(prefix + "\tmWaitingOnSurfaceValidity=" + mWaitingOnSurfaceValidity);
}
}