Fix blur effect on Android 12 (#1832)

This commit is contained in:
Howard Wu 2022-04-11 00:06:17 +08:00 committed by GitHub
parent 4cb9115fa5
commit 6b7fdb2fac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 8 deletions

View File

@ -20,8 +20,10 @@
package org.lsposed.manager.ui.dialog; package org.lsposed.manager.ui.dialog;
import android.animation.ValueAnimator; import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.util.Log;
import android.view.SurfaceControl; import android.view.SurfaceControl;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
@ -30,10 +32,11 @@ import android.view.animation.DecelerateInterpolator;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.os.BuildCompat;
import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import org.lsposed.manager.App;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@SuppressWarnings({"JavaReflectionMemberAccess", "ConstantConditions"}) @SuppressWarnings({"JavaReflectionMemberAccess", "ConstantConditions"})
@ -59,9 +62,10 @@ public class BlurBehindDialogBuilder extends MaterialAlertDialogBuilder {
animator.setDuration(150); animator.setDuration(150);
Window window = dialog.getWindow(); Window window = dialog.getWindow();
View view = window.getDecorView(); View view = window.getDecorView();
if (BuildCompat.isAtLeastS()) { if (Build.VERSION.SDK_INT >= 31) {
window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
animator.addUpdateListener(animation -> window.getAttributes().setBlurBehindRadius((Integer) animation.getAnimatedValue())); window.getAttributes().setBlurBehindRadius(50);
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
} else if (supportBlur) { } else if (supportBlur) {
try { try {
Object viewRootImpl = view.getClass().getMethod("getViewRootImpl").invoke(view); Object viewRootImpl = view.getClass().getMethod("getViewRootImpl").invoke(view);
@ -70,19 +74,18 @@ public class BlurBehindDialogBuilder extends MaterialAlertDialogBuilder {
} }
SurfaceControl surfaceControl = (SurfaceControl) viewRootImpl.getClass().getMethod("getSurfaceControl").invoke(viewRootImpl); SurfaceControl surfaceControl = (SurfaceControl) viewRootImpl.getClass().getMethod("getSurfaceControl").invoke(viewRootImpl);
@SuppressLint("BlockedPrivateApi") Method setBackgroundBlurRadius = SurfaceControl.Transaction.class.getDeclaredMethod("setBackgroundBlurRadius", SurfaceControl.class, int.class);
Method setBackgroundBlurRadius = SurfaceControl.Transaction.class.getDeclaredMethod("setBackgroundBlurRadius", SurfaceControl.class, int.class);
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()); setBackgroundBlurRadius.invoke(transaction, surfaceControl, animation.getAnimatedValue());
transaction.apply(); transaction.apply();
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); Log.e(App.TAG, "Blur behind dialog builder", t);
} }
}); });
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); Log.e(App.TAG, "Blur behind dialog builder", t);
} }
} }
view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@ -107,7 +110,7 @@ public class BlurBehindDialogBuilder extends MaterialAlertDialogBuilder {
Method get = c.getMethod("getBoolean", String.class, boolean.class); Method get = c.getMethod("getBoolean", String.class, boolean.class);
value = (boolean) get.invoke(c, key, defaultValue); value = (boolean) get.invoke(c, key, defaultValue);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); Log.e(App.TAG, "Blur behind dialog builder get system property", e);
} }
return value; return value;
} }