Skip to content

Commit

Permalink
feat: default style is according device theme on android
Browse files Browse the repository at this point in the history
  • Loading branch information
andredestro committed Aug 12, 2024
1 parent c138245 commit 0bfc9a9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ Quick Example
StatusBar.styleDefault
=================

Use the default statusbar (dark text, for light backgrounds).
For iOS - Automatically chooses light or dark content based on the user interface style.
Automatically chooses light or dark content based on the device theme on Android or automatically selects an appearance and updates it dynamically to maintain contrast with the content below it on iOS.

StatusBar.styleDefault();

Expand Down
47 changes: 31 additions & 16 deletions src/android/StatusBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public class StatusBar extends CordovaPlugin {
private static final String TAG = "StatusBar";
private static final String CORDOVA_STATIC_CHANNEL = "StatusBarStaticChannel";

private boolean doOverlay;

private static final String ACTION_HIDE = "hide";
private static final String ACTION_SHOW = "show";
private static final String ACTION_READY = "_ready";
Expand All @@ -63,6 +61,9 @@ public class StatusBar extends CordovaPlugin {
private static final String STYLE_LIGHT_CONTENT = "lightcontent";
private static final String STYLE_DARK_CONTENT = "darkcontent";

private boolean doOverlay;
private String currentStyle = "";

private AppCompatActivity activity;
private Window window;

Expand Down Expand Up @@ -116,14 +117,11 @@ else if(Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
} else {
// Read 'StatusBarBackgroundColor' from config.xml, default is #000000.
setStatusBarBackgroundColor(preferences.getString("StatusBarBackgroundColor", "#000000"));

// Read 'StatusBarStyle' from config.xml, default is 'lightcontent'.
String styleSetting = preferences.getString("StatusBarStyle", "lightcontent");
if (styleSetting.equalsIgnoreCase("blacktranslucent") || styleSetting.equalsIgnoreCase("blackopaque")) {
LOG.w(TAG, styleSetting +" is deprecated and will be removed in next major release, use lightcontent");
}
setStatusBarStyle(styleSetting);
}

// Read 'StatusBarStyle' from config.xml, default is 'default'.
String styleSetting = preferences.getString("StatusBarStyle", "default");
setStatusBarStyle(styleSetting);
}
});
}
Expand Down Expand Up @@ -268,10 +266,9 @@ public int getStatusBarHeight() {
}

private void setStatusBarTransparent(final boolean isTransparent) {
final Window window = cordova.getActivity().getWindow();
int visibility = isTransparent
? View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
: View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_VISIBLE;
? View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
: View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_VISIBLE;

window.getDecorView().setSystemUiVisibility(visibility);

Expand All @@ -280,27 +277,45 @@ private void setStatusBarTransparent(final boolean isTransparent) {
}
}

private void setStatusBarStyle(final String style) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !style.isEmpty()) {
private void setStatusBarStyle(String style) {
if (!style.isEmpty()) {
this.currentStyle = style;
View decorView = window.getDecorView();
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, decorView);

if (style.equals(STYLE_DEFAULT) || style.equals(STYLE_DARK_CONTENT)) {
if (style.equals(STYLE_DEFAULT)) {
style = getStyleFromDeviceTheme();
}
if (style.equals(STYLE_DARK_CONTENT)) {
windowInsetsControllerCompat.setAppearanceLightStatusBars(true);
} else if (style.equals(STYLE_LIGHT_CONTENT)) {
windowInsetsControllerCompat.setAppearanceLightStatusBars(false);
} else {
LOG.e(TAG, "Invalid style, must be either 'default' or 'lightcontent'");
LOG.e(TAG, "Invalid style, must be either 'default', 'lightcontent' or 'darkcontent'");
}
}
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.currentStyle.equals(STYLE_DEFAULT))
setStatusBarStyle(STYLE_DEFAULT);
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
pluginResult.setKeepCallback(true);
webView.sendPluginResult(pluginResult, CORDOVA_STATIC_CHANNEL);
}

private String getStyleFromDeviceTheme() {
int nightModeFlags = cordova.getContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {
case Configuration.UI_MODE_NIGHT_YES:
case Configuration.UI_MODE_NIGHT_UNDEFINED:
default:
return STYLE_LIGHT_CONTENT;

case Configuration.UI_MODE_NIGHT_NO:
return STYLE_DARK_CONTENT;
}
}
}
3 changes: 2 additions & 1 deletion www/statusbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ var StatusBar = {
},

styleDefault: function () {
// dark text ( to be used on a light background and on iOS automatically chooses light or dark content based on the user interface style)
// Automatically chooses light or dark content based on the device theme on Android or
// automatically selects an appearance and updates it dynamically to maintain contrast with the content below it on iOS.
exec(null, null, 'StatusBar', 'styleDefault', []);
},

Expand Down

0 comments on commit 0bfc9a9

Please sign in to comment.