[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"); 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) { if (primary_color != null) {
primary_color.setVisible(!prefFollowSystemAccent.isChecked()); primary_color.setVisible(!prefFollowSystemAccent.isChecked());
} }

View File

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