diff --git a/status-bar/README.md b/status-bar/README.md index 8e7586905..93dfc5a37 100644 --- a/status-bar/README.md +++ b/status-bar/README.md @@ -270,11 +270,11 @@ of the space underneath it. #### Style -| Members | Value | Description | Since | -| ------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- | -| **`Dark`** | 'DARK' | Light text for dark backgrounds. | 1.0.0 | -| **`Light`** | 'LIGHT' | Dark text for light backgrounds. | 1.0.0 | -| **`Default`** | 'DEFAULT' | The style is based on the device appearance. If the device is using Dark mode, the statusbar text will be light. If the device is using Light mode, the statusbar text will be dark. On Android the default will be the one the app was launched with. | 1.0.0 | +| Members | Value | Description | Since | +| ------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- | +| **`Dark`** | 'DARK' | Light text for dark backgrounds. | 1.0.0 | +| **`Light`** | 'LIGHT' | Dark text for light backgrounds. | 1.0.0 | +| **`Default`** | 'DEFAULT' | The style is based on the device appearance. If the device is using Dark mode, the statusbar text will be light. If the device is using Light mode, the statusbar text will be dark. | 1.0.0 | #### Animation diff --git a/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBar.java b/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBar.java index 4b8155d6a..0239bd845 100644 --- a/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBar.java +++ b/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBar.java @@ -1,5 +1,6 @@ package com.capacitorjs.plugins.statusbar; +import android.content.res.Configuration; import android.graphics.Color; import android.os.Build; import android.util.DisplayMetrics; @@ -21,15 +22,13 @@ public class StatusBar { private int currentStatusBarColor; private final ChangeListener listener; private final AppCompatActivity activity; - private final String defaultStyle; + private String currentStyle = "DEFAULT"; public StatusBar(AppCompatActivity activity, StatusBarConfig config, ChangeListener listener) { // save initial color of the status bar this.activity = activity; this.currentStatusBarColor = activity.getWindow().getStatusBarColor(); this.listener = listener; - this.defaultStyle = getStyle(); - setBackgroundColor(config.getBackgroundColor()); setStyle(config.getStyle()); setOverlaysWebView(config.isOverlaysWebView()); @@ -41,15 +40,27 @@ public StatusBar(AppCompatActivity activity, StatusBarConfig config, ChangeListe public void setStyle(String style) { Window window = activity.getWindow(); View decorView = window.getDecorView(); - + this.currentStyle = style; if (style.equals("DEFAULT")) { - style = this.defaultStyle; + style = getStyleForTheme(); } WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, decorView); windowInsetsControllerCompat.setAppearanceLightStatusBars(!style.equals("DARK")); } + private String getStyleForTheme() { + int currentNightMode = activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; + if (currentNightMode != Configuration.UI_MODE_NIGHT_YES) { + return "LIGHT"; + } + return "DARK"; + } + + public void updateStyle() { + setStyle(this.currentStyle); + } + @SuppressWarnings("deprecation") public void setBackgroundColor(int color) { Window window = activity.getWindow(); diff --git a/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBarPlugin.java b/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBarPlugin.java index 6c32f8d9d..0491c0c01 100644 --- a/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBarPlugin.java +++ b/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBarPlugin.java @@ -1,5 +1,6 @@ package com.capacitorjs.plugins.statusbar; +import android.content.res.Configuration; import com.getcapacitor.JSObject; import com.getcapacitor.Logger; import com.getcapacitor.Plugin; @@ -49,6 +50,12 @@ private String styleFromConfig(String style) { } } + @Override + protected void handleOnConfigurationChanged(Configuration newConfig) { + super.handleOnConfigurationChanged(newConfig); + implementation.updateStyle(); + } + @PluginMethod public void setStyle(final PluginCall call) { final String style = call.getString("style"); diff --git a/status-bar/src/definitions.ts b/status-bar/src/definitions.ts index 7112a4532..acefc6771 100644 --- a/status-bar/src/definitions.ts +++ b/status-bar/src/definitions.ts @@ -69,7 +69,6 @@ export enum Style { * The style is based on the device appearance. * If the device is using Dark mode, the statusbar text will be light. * If the device is using Light mode, the statusbar text will be dark. - * On Android the default will be the one the app was launched with. * * @since 1.0.0 */