refactor(android)!: setStatusBarBackgroundColor (#250)

This commit is contained in:
エリス 2022-10-05 16:29:18 +09:00 committed by GitHub
parent bb82db9aa9
commit 21847d0b78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -169,21 +169,20 @@ public class StatusBar extends CordovaPlugin {
}
private void setStatusBarBackgroundColor(final String colorPref) {
if (colorPref != null && !colorPref.isEmpty()) {
final Window window = cordova.getActivity().getWindow();
// Method and constants not available on all SDKs but we want to be able to compile this code with any SDK
window.clearFlags(0x04000000); // SDK 19: WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
try {
// Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21
window.getClass().getMethod("setStatusBarColor", int.class).invoke(window, Color.parseColor(colorPref));
} catch (IllegalArgumentException ignore) {
LOG.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
} catch (Exception ignore) {
// this should not happen, only in case Android removes this method in a version > 21
LOG.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
}
if (colorPref.isEmpty()) return;
int color;
try {
color = Color.parseColor(colorPref);
} catch (IllegalArgumentException ignore) {
LOG.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
return;
}
final Window window = cordova.getActivity().getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // SDK 19-30
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // SDK 21
window.setStatusBarColor(color);
}
private void setStatusBarTransparent(final boolean transparent) {