Skip to content

Commit ab45fda

Browse files
authored
Merge branch 'main' into chore/update-root-packages
2 parents 0aeaf10 + c859c84 commit ab45fda

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

status-bar/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ setBackgroundColor(options: BackgroundColorOptions) => Promise<void>
159159
```
160160

161161
Set the background color of the status bar.
162+
Calling this function updates the foreground color of the status bar if the style is set to default, except on iOS versions lower than 17.
162163
Not available on Android 15+.
163164

164165
| Param | Type |

status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBar.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import android.graphics.Color;
55
import android.os.Build;
66
import android.util.DisplayMetrics;
7+
import android.util.TypedValue;
78
import android.view.View;
89
import android.view.Window;
910
import android.view.WindowInsets;
1011
import android.view.WindowManager;
1112
import androidx.appcompat.app.AppCompatActivity;
13+
import androidx.core.graphics.ColorUtils;
1214
import androidx.core.view.ViewCompat;
1315
import androidx.core.view.WindowCompat;
1416
import androidx.core.view.WindowInsetsCompat;
@@ -63,11 +65,20 @@ public void updateStyle() {
6365

6466
public void setBackgroundColor(int color) {
6567
Window window = activity.getWindow();
66-
clearTranslucentStatusFlagDeprecated();
67-
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
68-
setStatusBarColorDeprecated(color);
69-
// update the local color field as well
70-
currentStatusBarColor = color;
68+
if (shouldSetStatusBarColor(isEdgeToEdgeOptOutEnabled(window))) {
69+
clearTranslucentStatusFlagDeprecated();
70+
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
71+
setStatusBarColorDeprecated(color);
72+
currentStatusBarColor = color;
73+
74+
// only set foreground color if style is default
75+
if (currentStyle.equals("DEFAULT")) {
76+
// determine if the color is light or dark using luminance and set icon color
77+
boolean isLightColor = ColorUtils.calculateLuminance(color) > 0.5;
78+
WindowInsetsControllerCompat insetsController = WindowCompat.getInsetsController(window, window.getDecorView());
79+
insetsController.setAppearanceLightStatusBars(isLightColor);
80+
}
81+
}
7182
}
7283

7384
public void hide() {
@@ -107,6 +118,29 @@ public void setOverlaysWebView(Boolean overlays) {
107118
listener.onChange(statusBarOverlayChanged, getInfo());
108119
}
109120

121+
private boolean shouldSetStatusBarColor(boolean hasOptOut) {
122+
boolean canSetStatusBar;
123+
int deviceApi = Build.VERSION.SDK_INT;
124+
if (deviceApi < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
125+
// device below Android 15 - can always set status bar
126+
canSetStatusBar = true;
127+
} else if (deviceApi == Build.VERSION_CODES.VANILLA_ICE_CREAM) {
128+
canSetStatusBar = hasOptOut; // app targets 15 - can set status bar if opted out
129+
} else {
130+
canSetStatusBar = false; // app targets 16 - opt-out ignored or app targets 15 but there is not opt out
131+
}
132+
return canSetStatusBar;
133+
}
134+
135+
private boolean isEdgeToEdgeOptOutEnabled(Window window) {
136+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
137+
TypedValue value = new TypedValue();
138+
window.getContext().getTheme().resolveAttribute(android.R.attr.windowOptOutEdgeToEdgeEnforcement, value, true);
139+
return value.data != 0; // value is set to -1 on true as of Android 15, so we have to do this.
140+
}
141+
return false;
142+
}
143+
110144
private boolean getIsOverlaid() {
111145
return (
112146
(getSystemUiVisibilityDeprecated(activity.getWindow().getDecorView()) & getSystemUiFlagLayoutFullscreenDeprecated()) ==

status-bar/src/definitions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export interface StatusBarPlugin {
170170

171171
/**
172172
* Set the background color of the status bar.
173+
* Calling this function updates the foreground color of the status bar if the style is set to default, except on iOS versions lower than 17.
173174
* Not available on Android 15+.
174175
*
175176
* @since 1.0.0

0 commit comments

Comments
 (0)