Fix blur behind for Android 14 (#2884)

This commit is contained in:
Howard Wu 2023-12-16 02:41:35 +08:00 committed by GitHub
parent 7127344dc0
commit f892443923
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 13 deletions

View File

@ -38,6 +38,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import org.lsposed.manager.App; import org.lsposed.manager.App;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.function.Consumer;
@SuppressWarnings({"JavaReflectionMemberAccess", "ConstantConditions"}) @SuppressWarnings({"JavaReflectionMemberAccess", "ConstantConditions"})
public class BlurBehindDialogBuilder extends MaterialAlertDialogBuilder { public class BlurBehindDialogBuilder extends MaterialAlertDialogBuilder {
@ -55,20 +56,46 @@ public class BlurBehindDialogBuilder extends MaterialAlertDialogBuilder {
@Override @Override
public AlertDialog create() { public AlertDialog create() {
AlertDialog dialog = super.create(); AlertDialog dialog = super.create();
dialog.setOnShowListener(d -> setBackgroundBlurRadius(dialog)); setupWindowBlurListener(dialog);
return dialog; return dialog;
} }
private void setBackgroundBlurRadius(AlertDialog dialog) { private void setupWindowBlurListener(AlertDialog dialog) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { var window = dialog.getWindow();
Window window = dialog.getWindow(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (Build.VERSION.SDK_INT >= 31) { window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); Consumer<Boolean> windowBlurEnabledListener = enabled -> updateWindowForBlurs(window, enabled);
window.getAttributes().setBlurBehindRadius(53); //android.R.styleable.Window_windowBlurBehindRadius window.getDecorView().addOnAttachStateChangeListener(
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); new View.OnAttachStateChangeListener() {
} else if (supportBlur) { @Override
public void onViewAttachedToWindow(@NonNull View v) {
window.getWindowManager().addCrossWindowBlurEnabledListener(
windowBlurEnabledListener);
}
@Override
public void onViewDetachedFromWindow(@NonNull View v) {
window.getWindowManager().removeCrossWindowBlurEnabledListener(
windowBlurEnabledListener);
}
});
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
dialog.setOnShowListener(d -> updateWindowForBlurs(window, supportBlur));
}
}
private void updateWindowForBlurs(Window window, boolean blursEnabled) {
float mDimAmountWithBlur = 0.1f;
float mDimAmountNoBlur = 0.32f;
window.setDimAmount(blursEnabled ?
mDimAmountWithBlur : mDimAmountNoBlur);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
window.getAttributes().setBlurBehindRadius(20);
window.setAttributes(window.getAttributes());
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
if (blursEnabled) {
View view = window.getDecorView(); View view = window.getDecorView();
ValueAnimator animator = ValueAnimator.ofInt(1, 153); ValueAnimator animator = ValueAnimator.ofInt(1, 53);
animator.setInterpolator(new DecelerateInterpolator()); animator.setInterpolator(new DecelerateInterpolator());
try { try {
Object viewRootImpl = view.getClass().getMethod("getViewRootImpl").invoke(view); Object viewRootImpl = view.getClass().getMethod("getViewRootImpl").invoke(view);
@ -81,7 +108,10 @@ public class BlurBehindDialogBuilder extends MaterialAlertDialogBuilder {
animator.addUpdateListener(animation -> { animator.addUpdateListener(animation -> {
try { try {
SurfaceControl.Transaction transaction = new SurfaceControl.Transaction(); SurfaceControl.Transaction transaction = new SurfaceControl.Transaction();
setBackgroundBlurRadius.invoke(transaction, surfaceControl, animation.getAnimatedValue()); var animatedValue = animation.getAnimatedValue();
if (animatedValue != null) {
setBackgroundBlurRadius.invoke(transaction, surfaceControl, (int) animatedValue);
}
transaction.apply(); transaction.apply();
} catch (Throwable t) { } catch (Throwable t) {
Log.e(App.TAG, "Blur behind dialog builder", t); Log.e(App.TAG, "Blur behind dialog builder", t);
@ -92,11 +122,11 @@ public class BlurBehindDialogBuilder extends MaterialAlertDialogBuilder {
} }
view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override @Override
public void onViewAttachedToWindow(View v) { public void onViewAttachedToWindow(@NonNull View v) {
} }
@Override @Override
public void onViewDetachedFromWindow(View v) { public void onViewDetachedFromWindow(@NonNull View v) {
animator.cancel(); animator.cancel();
} }
}); });