[app] Use Android S blur api

This commit is contained in:
tehcneko 2021-05-19 18:22:27 +08:00
parent 8c4a806092
commit d490c93fca
3 changed files with 42 additions and 34 deletions

View File

@ -300,7 +300,7 @@ public class SettingsActivity extends BaseActivity {
}
SwitchPreference prefFollowSystemAccent = findPreference("follow_system_accent");
if (prefFollowSystemAccent != null && (Build.VERSION.SDK_INT >= 31 || Build.VERSION.SDK_INT == 30 && Build.VERSION.PREVIEW_SDK_INT != 0)) {
if (prefFollowSystemAccent != null && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S || Build.VERSION.SDK_INT == Build.VERSION_CODES.R && Build.VERSION.PREVIEW_SDK_INT != 0)) {
if (primary_color != null) {
primary_color.setVisible(!prefFollowSystemAccent.isChecked());
}

View File

@ -45,47 +45,55 @@ public class BlurBehindDialogBuilder extends AlertDialog.Builder {
@Override
public AlertDialog create() {
AlertDialog dialog = super.create();
dialog.setOnShowListener(d -> setBackgroundBlurRadius(dialog.getWindow().getDecorView()));
dialog.setOnShowListener(d -> setBackgroundBlurRadius(dialog));
return dialog;
}
private void setBackgroundBlurRadius(View view) {
private void setBackgroundBlurRadius(AlertDialog dialog) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && supportBlur) {
try {
Object viewRootImpl = view.getClass().getMethod("getViewRootImpl").invoke(view);
if (viewRootImpl == null) {
return;
}
SurfaceControl surfaceControl = (SurfaceControl) viewRootImpl.getClass().getMethod("getSurfaceControl").invoke(viewRootImpl);
ValueAnimator animator = ValueAnimator.ofInt(1, 150);
animator.setInterpolator(new DecelerateInterpolator());
animator.setDuration(150);
Method setBackgroundBlurRadius = SurfaceControl.Transaction.class.getDeclaredMethod("setBackgroundBlurRadius", SurfaceControl.class, int.class);
ValueAnimator animator = ValueAnimator.ofInt(1, 150);
animator.setInterpolator(new DecelerateInterpolator());
animator.setDuration(150);
View view = dialog.getWindow().getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S || Build.VERSION.SDK_INT == Build.VERSION_CODES.R && Build.VERSION.PREVIEW_SDK_INT != 0) {
animator.addUpdateListener(animation -> {
try {
SurfaceControl.Transaction transaction = new SurfaceControl.Transaction();
setBackgroundBlurRadius.invoke(transaction, surfaceControl, animation.getAnimatedValue());
transaction.apply();
} catch (Throwable t) {
t.printStackTrace();
}
dialog.getWindow().setBackgroundBlurRadius((Integer) animation.getAnimatedValue());
});
view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
} else {
try {
Object viewRootImpl = view.getClass().getMethod("getViewRootImpl").invoke(view);
if (viewRootImpl == null) {
return;
}
SurfaceControl surfaceControl = (SurfaceControl) viewRootImpl.getClass().getMethod("getSurfaceControl").invoke(viewRootImpl);
@Override
public void onViewDetachedFromWindow(View v) {
animator.cancel();
}
});
animator.start();
} catch (Throwable t) {
t.printStackTrace();
Method setBackgroundBlurRadius = SurfaceControl.Transaction.class.getDeclaredMethod("setBackgroundBlurRadius", SurfaceControl.class, int.class);
animator.addUpdateListener(animation -> {
try {
SurfaceControl.Transaction transaction = new SurfaceControl.Transaction();
setBackgroundBlurRadius.invoke(transaction, surfaceControl, animation.getAnimatedValue());
transaction.apply();
} catch (Throwable t) {
t.printStackTrace();
}
});
} catch (Throwable t) {
t.printStackTrace();
}
}
view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
}
@Override
public void onViewDetachedFromWindow(View v) {
animator.cancel();
}
});
animator.start();
}
}

View File

@ -110,7 +110,7 @@ public class ThemeUtil {
@StyleRes
public static int getColorThemeStyleRes() {
if ((Build.VERSION.SDK_INT >= 31 || Build.VERSION.SDK_INT == 30 && Build.VERSION.PREVIEW_SDK_INT != 0) && isSystemAccent()) {
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S || Build.VERSION.SDK_INT == Build.VERSION_CODES.R && Build.VERSION.PREVIEW_SDK_INT != 0) && isSystemAccent()) {
return R.style.ThemeOverlay_system;
}
Integer theme = colorThemeMap.get(getColorTheme());