From cd52ba07b339fc00bac339968cd642c1217c535c Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Thu, 8 Aug 2024 17:31:27 +0200 Subject: [PATCH 1/2] feat(statusbar)!: make DEFAULT change style based on the Android theme --- status-bar/README.md | 10 ++++----- .../plugins/statusbar/StatusBar.java | 22 +++++++++++++++---- .../plugins/statusbar/StatusBarPlugin.java | 8 +++++++ status-bar/src/definitions.ts | 1 - 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/status-bar/README.md b/status-bar/README.md index cd7ef29c8..555c1239e 100644 --- a/status-bar/README.md +++ b/status-bar/README.md @@ -226,11 +226,11 @@ This method is only supported on Android. #### 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 01d354a29..60b5b7836 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.view.View; import android.view.Window; @@ -14,27 +15,40 @@ public class StatusBar { private int currentStatusBarColor; private final AppCompatActivity activity; - private final String defaultStyle; + private String currentStyle = "DEFAULT"; public StatusBar(AppCompatActivity activity) { // save initial color of the status bar this.activity = activity; this.currentStatusBarColor = activity.getWindow().getStatusBarColor(); - this.defaultStyle = getStyle(); + setStyle(this.currentStyle); } 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 a66492014..91aa684cd 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,7 @@ package com.capacitorjs.plugins.statusbar; +import android.content.res.Configuration; + import com.getcapacitor.JSObject; import com.getcapacitor.Plugin; import com.getcapacitor.PluginCall; @@ -18,6 +20,12 @@ public void load() { implementation = new StatusBar(getActivity()); } + @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 e1f69e384..074aa4423 100644 --- a/status-bar/src/definitions.ts +++ b/status-bar/src/definitions.ts @@ -26,7 +26,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 */ From 492a56aa05533f9885a1f868457db267aff737fa Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Thu, 8 Aug 2024 18:01:14 +0200 Subject: [PATCH 2/2] fmt --- .../main/java/com/capacitorjs/plugins/statusbar/StatusBar.java | 3 +-- .../com/capacitorjs/plugins/statusbar/StatusBarPlugin.java | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) 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 60b5b7836..e8e15e1f0 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 @@ -37,8 +37,7 @@ public void setStyle(String style) { } private String getStyleForTheme() { - int currentNightMode = activity.getResources().getConfiguration().uiMode - & Configuration.UI_MODE_NIGHT_MASK; + int currentNightMode = activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; if (currentNightMode != Configuration.UI_MODE_NIGHT_YES) { return "LIGHT"; } 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 91aa684cd..b5d1e835f 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,7 +1,6 @@ package com.capacitorjs.plugins.statusbar; import android.content.res.Configuration; - import com.getcapacitor.JSObject; import com.getcapacitor.Plugin; import com.getcapacitor.PluginCall;