Skip to content

Commit cc7bf73

Browse files
fix(android): sync status and navigation bars with app theme (#2002)
* fix(android): sync status and navigation bars with app theme * Update src/plugins/system/android/com/foxdebug/system/System.java Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 880dd85 commit cc7bf73

File tree

1 file changed

+40
-7
lines changed
  • src/plugins/system/android/com/foxdebug/system

1 file changed

+40
-7
lines changed

src/plugins/system/android/com/foxdebug/system/System.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,17 +1710,50 @@ private void setUiTheme(
17101710
try {
17111711
this.systemBarColor = Color.parseColor(systemBarColor);
17121712
this.theme = new Theme(scheme);
1713-
1714-
preferences.set("BackgroundColor", this.systemBarColor);
17151713

1714+
preferences.set("BackgroundColor", this.systemBarColor);
17161715
webView.getPluginManager().postMessage("updateSystemBars", null);
1716+
applySystemBarTheme();
17171717

17181718
callback.success();
17191719
} catch (IllegalArgumentException e) {
17201720
callback.error("Invalid color: " + systemBarColor);
1721+
} catch (Exception e) {
1722+
callback.error(e.toString());
17211723
}
17221724
}
17231725

1726+
private void applySystemBarTheme() {
1727+
final Window window = activity.getWindow();
1728+
final View decorView = window.getDecorView();
1729+
1730+
// Keep Cordova's BackgroundColor flow for API 36+, but also apply the
1731+
// window colors directly so OEM variants do not leave stale system-bar
1732+
// colors behind after a theme switch.
1733+
window.clearFlags(0x04000000 | 0x08000000); // FLAG_TRANSLUCENT_STATUS | FLAG_TRANSLUCENT_NAVIGATION
1734+
window.addFlags(0x80000000); // FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
1735+
1736+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
1737+
window.setNavigationBarContrastEnforced(false);
1738+
window.setStatusBarContrastEnforced(false);
1739+
}
1740+
1741+
decorView.setBackgroundColor(this.systemBarColor);
1742+
1743+
View rootView = activity.findViewById(android.R.id.content);
1744+
if (rootView != null) {
1745+
rootView.setBackgroundColor(this.systemBarColor);
1746+
}
1747+
1748+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
1749+
window.setStatusBarColor(this.systemBarColor);
1750+
window.setNavigationBarColor(this.systemBarColor);
1751+
}
1752+
1753+
setStatusBarStyle(window);
1754+
setNavigationBarStyle(window);
1755+
}
1756+
17241757
private void setStatusBarStyle(final Window window) {
17251758
String themeType = theme.getType();
17261759
View decorView = window.getDecorView();
@@ -1754,22 +1787,22 @@ private void setNavigationBarStyle(final Window window) {
17541787
String themeType = theme.getType();
17551788
View decorView = window.getDecorView();
17561789
int uiOptions;
1790+
int lightNavigationBar;
17571791

17581792
if (SDK_INT <= 30) {
17591793
uiOptions = getDeprecatedSystemUiVisibility(decorView);
1760-
// 0x80000000 FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
1761-
// 0x00000010 SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
1794+
lightNavigationBar = View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
17621795

17631796
if (themeType.equals("light")) {
1764-
setDeprecatedSystemUiVisibility(decorView, uiOptions | 0x80000000 | 0x00000010);
1797+
setDeprecatedSystemUiVisibility(decorView, uiOptions | lightNavigationBar);
17651798
return;
17661799
}
1767-
setDeprecatedSystemUiVisibility(decorView, uiOptions | (0x80000000 & ~0x00000010));
1800+
setDeprecatedSystemUiVisibility(decorView, uiOptions & ~lightNavigationBar);
17681801
return;
17691802
}
17701803

17711804
uiOptions = Objects.requireNonNull(decorView.getWindowInsetsController()).getSystemBarsAppearance();
1772-
int lightNavigationBar = WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS;
1805+
lightNavigationBar = WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS;
17731806

17741807
if (themeType.equals("light")) {
17751808
decorView.getWindowInsetsController().setSystemBarsAppearance(uiOptions | lightNavigationBar, lightNavigationBar);

0 commit comments

Comments
 (0)