Files
Flexera-Launcher/quickstep/src/com/android/launcher3/statehandlers/DepthController.java
T
TMCandClaude Opus 5.5 7dfc8f24ea Mark every Flexera edit to Lawnchair and Launcher3 files
Comments only: git grep "Flexera Launcher:" now finds every behaviour
change Flexera made to an upstream file, so that upstream merges stay
easy. A reviewer rebuilt the files as they were before this pass and
found every code token unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 18:50:52 +01:00

331 lines
13 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.AbstractFloatingView;
import com.android.launcher3.BaseActivity;
import com.android.launcher3.LauncherState;
import com.android.launcher3.Utilities;
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.BaseDepthControllerImpl;
import java.io.PrintWriter;
import java.util.function.Consumer;
import app.lawnchair.preferences2.PreferenceCacheExtensionsKt;
import app.lawnchair.compat.LawnchairQuickstepCompat;
import app.lawnchair.preferences2.PreferenceManager2;
/**
* Controls blur and wallpaper zoom, for the Launcher surface only.
*/
public class DepthController extends BaseDepthControllerImpl 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;
private final boolean mEnableDepth;
public DepthController(QuickstepLauncher launcher) {
super(launcher);
var prefs = PreferenceManager2.getInstance(launcher);
var pref = prefs.getWallpaperDepthEffect();
mEnableDepth = PreferenceCacheExtensionsKt.firstCached(pref);
}
private void onLauncherDraw() {
View view = mLauncher.getDragLayer();
ViewRootImpl viewRootImpl = view.getViewRootImpl();
try {
if (Utilities.ATLEAST_Q) {
setBaseSurface(viewRootImpl != null ? viewRootImpl.getSurfaceControl() : null);
}
} catch (Throwable t) {
// LC-Ignored
}
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) {
if (Utilities.ATLEAST_S) {
try {
UI_HELPER_EXECUTOR.execute(() ->
CrossWindowBlurListeners.getInstance().addListener(
mLauncher.getMainExecutor(), mCrossWindowBlurListener));
} catch (Throwable t) {
// LC-Ignored
}
}
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() {
if (Utilities.ATLEAST_S) {
try {
UI_HELPER_EXECUTOR.execute(() ->
CrossWindowBlurListeners.getInstance()
.removeListener(mCrossWindowBlurListener));
} catch (Throwable t) {
// LC-Ignored
}
}
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();
}
// Re-apply even when depth is already 0 so an interrupted All Apps / depth transition
// cannot leave workspace RenderEffect or surface blur stuck until the next resume.
if (toState == LauncherState.NORMAL) {
clearWorkspaceRenderEffects();
applyDepthAndBlur();
}
}
@Override
public void setStateWithAnimation(LauncherState toState, StateAnimationConfig config,
PendingAnimation animation) {
if (config.hasAnimationFlag(SKIP_DEPTH_CONTROLLER)
|| mIgnoreStateChangesDuringMultiWindowAnimation) {
if (toState == LauncherState.NORMAL) {
clearWorkspaceRenderEffects();
}
return;
}
float toDepth = toState.getDepth(mLauncher);
animation.setFloat(stateDepth, MULTI_PROPERTY_VALUE, toDepth,
config.getInterpolator(ANIM_DEPTH, LINEAR));
if (toState == LauncherState.NORMAL) {
animation.addEndListener(success -> {
if (mLauncher.getStateManager().getState() == LauncherState.NORMAL) {
clearWorkspaceRenderEffects();
applyDepthAndBlur();
}
});
}
}
/**
* Clears a stuck workspace icon blur after resume when settled on home.
*/
public void clearStuckBlurOnResumeIfHome() {
// Flexera Launcher: a folder blur with no folder on the screen, open or still closing,
// is a stuck one too. It should not come about; this makes sure it could not stay.
if (folderBlur.getValue() > 0f && AbstractFloatingView.getAnyView(
mLauncher, AbstractFloatingView.TYPE_FOLDER) == null) {
folderBlur.setValue(0f);
}
// Flexera Launcher: likewise quick launch's blur with its page gone.
if (quickLaunchBlur.getValue() > 0f
&& !app.flexera.home.QuickLaunchPage.isShowing(mLauncher)) {
quickLaunchBlur.setValue(0f);
}
if (mLauncher.getStateManager().getState() == LauncherState.NORMAL
&& !mLauncher.getStateManager().isInTransition()) {
clearWorkspaceRenderEffects();
applyDepthAndBlur();
}
}
// Flexera Launcher: the owner's switch for the wallpaper's zoom, which the blur behind a
// folder or quick launch's page must not turn back on (BaseDepthControllerImpl).
@Override
protected boolean isDepthEffectEnabled() {
return mEnableDepth;
}
@Override
protected void applyDepthAndBlur() {
try {
// Flexera Launcher: the blur behind an open folder is a setting of its own, and
// also comes where the wallpaper's zoom is switched off.
if (LawnchairQuickstepCompat.ATLEAST_R
&& (mEnableDepth || folderBlurNeedsApplying())) {
ensureDependencies();
super.applyDepthAndBlur();
} else if (mLauncher.getStateManager().getState() == LauncherState.NORMAL
&& !mLauncher.getStateManager().isInTransition()) {
clearWorkspaceRenderEffects();
}
} catch (Throwable t) {
// LC-Ignored
if (mLauncher.getStateManager().getState() == LauncherState.NORMAL) {
try {
clearWorkspaceRenderEffects();
} catch (Throwable ignored) {
// LC-Ignored
}
}
}
}
@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);
}
}