[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,12 +45,21 @@ 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) {
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 -> {
dialog.getWindow().setBackgroundBlurRadius((Integer) animation.getAnimatedValue());
});
} else {
try {
Object viewRootImpl = view.getClass().getMethod("getViewRootImpl").invoke(view);
if (viewRootImpl == null) {
@ -58,9 +67,7 @@ public class BlurBehindDialogBuilder extends AlertDialog.Builder {
}
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);
animator.addUpdateListener(animation -> {
try {
@ -71,6 +78,10 @@ public class BlurBehindDialogBuilder extends AlertDialog.Builder {
t.printStackTrace();
}
});
} catch (Throwable t) {
t.printStackTrace();
}
}
view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
@ -83,9 +94,6 @@ public class BlurBehindDialogBuilder extends AlertDialog.Builder {
}
});
animator.start();
} catch (Throwable t) {
t.printStackTrace();
}
}
}

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());