From dd879140c42fa09c4274a76b9693be16e7f24f2a Mon Sep 17 00:00:00 2001 From: Max Ruman Date: Fri, 21 Apr 2017 12:55:58 +0200 Subject: [PATCH] CB-11858: (android) Add StatusBarStyle feature support for Android M+ --- README.md | 4 ++ src/android/StatusBar.java | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/README.md b/README.md index 50ab55e..f5fcb48 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ Supported Platforms ------------------- - iOS +- Android 6+ - Windows Phone 7 - Windows Phone 8 - Windows Phone 8.1 @@ -179,6 +180,7 @@ Supported Platforms ------------------- - iOS +- Android 6+ - Windows Phone 7 - Windows Phone 8 - Windows Phone 8.1 @@ -195,6 +197,7 @@ Supported Platforms ------------------- - iOS +- Android 6+ - Windows Phone 7 - Windows Phone 8 - Windows Phone 8.1 @@ -211,6 +214,7 @@ Supported Platforms ------------------- - iOS +- Android 6+ - Windows Phone 7 - Windows Phone 8 - Windows Phone 8.1 diff --git a/src/android/StatusBar.java b/src/android/StatusBar.java index 6c99d78..32acf91 100644 --- a/src/android/StatusBar.java +++ b/src/android/StatusBar.java @@ -34,6 +34,7 @@ import org.apache.cordova.CordovaWebView; 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"; @@ -60,6 +61,9 @@ public class StatusBar extends CordovaPlugin { // 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")); } }); } @@ -142,6 +146,46 @@ public class StatusBar extends CordovaPlugin { return true; } + 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; } @@ -164,4 +208,35 @@ public class StatusBar extends CordovaPlugin { } } } + + 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'"); + } + } + } }