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) { private void setStatusBarBackgroundColor(final String colorPref) {
if (colorPref != null && !colorPref.isEmpty()) { if (colorPref.isEmpty()) return;
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 int color;
window.clearFlags(0x04000000); // SDK 19: WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); try {
window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); color = Color.parseColor(colorPref);
try { } catch (IllegalArgumentException ignore) {
// Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21 LOG.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
window.getClass().getMethod("setStatusBarColor", int.class).invoke(window, Color.parseColor(colorPref)); return;
} 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);
}
} }
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) { private void setStatusBarTransparent(final boolean transparent) {