Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-11858: (android) Add StatusBarStyle feature support for Android M+ #78

Merged
merged 2 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Supported Platforms
-------------------

- iOS
- Android 6+
- Windows Phone 7
- Windows Phone 8
- Windows Phone 8.1
Expand All @@ -179,6 +180,7 @@ Supported Platforms
-------------------

- iOS
- Android 6+
- Windows Phone 7
- Windows Phone 8
- Windows Phone 8.1
Expand All @@ -195,6 +197,7 @@ Supported Platforms
-------------------

- iOS
- Android 6+
- Windows Phone 7
- Windows Phone 8
- Windows Phone 8.1
Expand All @@ -211,6 +214,7 @@ Supported Platforms
-------------------

- iOS
- Android 6+
- Windows Phone 7
- Windows Phone 8
- Windows Phone 8.1
Expand Down
75 changes: 75 additions & 0 deletions src/android/StatusBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.cordova.LOG;
import org.apache.cordova.PluginResult;
import org.json.JSONException;
import java.util.Arrays;

public class StatusBar extends CordovaPlugin {
private static final String TAG = "StatusBar";
Expand All @@ -60,6 +61,9 @@ public void run() {

// Read 'StatusBarBackgroundColor' from config.xml, default is #000000.
setStatusBarBackgroundColor(preferences.getString("StatusBarBackgroundColor", "#000000"));

// Read 'StatusBarStyle' from config.xml, default is 'lightcontent'.
setStatusBarStyle(preferences.getString("StatusBarStyle", "lightcontent"));
}
});
}
Expand Down Expand Up @@ -159,6 +163,46 @@ public void run() {
else return args.getBoolean(0) == false;
}

if ("styleDefault".equals(action)) {
this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
setStatusBarStyle("default");
}
});
return true;
}

if ("styleLightContent".equals(action)) {
this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
setStatusBarStyle("lightcontent");
}
});
return true;
}

if ("styleBlackTranslucent".equals(action)) {
this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
setStatusBarStyle("blacktranslucent");
}
});
return true;
}

if ("styleBlackOpaque".equals(action)) {
this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
setStatusBarStyle("blackopaque");
}
});
return true;
}

return false;
}

Expand Down Expand Up @@ -198,4 +242,35 @@ private void setStatusBarTransparent(final boolean transparent) {
}
}
}

private void setStatusBarStyle(final String style) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (style != null && !style.isEmpty()) {
View decorView = cordova.getActivity().getWindow().getDecorView();
int uiOptions = decorView.getSystemUiVisibility();

String[] darkContentStyles = {
"default",
};

String[] lightContentStyles = {
"lightcontent",
"blacktranslucent",
"blackopaque",
};

if (Arrays.asList(darkContentStyles).contains(style.toLowerCase())) {
decorView.setSystemUiVisibility(uiOptions | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
return;
}

if (Arrays.asList(lightContentStyles).contains(style.toLowerCase())) {
decorView.setSystemUiVisibility(uiOptions & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
return;
}

LOG.e(TAG, "Invalid style, must be either 'default', 'lightcontent' or the deprecated 'blacktranslucent' and 'blackopaque'");
}
}
}
}