diff --git a/cordova-js-src/plugin/android/statusbar.js b/cordova-js-src/plugin/android/statusbar.js index a4baeabf..131c0139 100644 --- a/cordova-js-src/plugin/android/statusbar.js +++ b/cordova-js-src/plugin/android/statusbar.js @@ -60,28 +60,29 @@ Object.defineProperty(statusBar, 'setBackgroundColor', { statusBarScript.style.color = value; var rgbStr = window.getComputedStyle(statusBarScript).getPropertyValue('color'); - if (!rgbStr.match(/^rgb/)) { return; } - - var rgbVals = rgbStr.match(/\d+/g).map(function (v) { return parseInt(v, 10); }); - - if (rgbVals.length < 3) { + if (!rgbStr.match(/^rgb/)) { return; - } else if (rgbVals.length === 3) { - rgbVals = [255].concat(rgbVals); } - // TODO: Use `padStart(2, '0')` once SDK 24 is dropped. - const padRgb = (val) => { - const hex = val.toString(16); - return hex.length === 1 ? '0' + hex : hex; - }; - const a = padRgb(rgbVals[0]); - const r = padRgb(rgbVals[1]); - const g = padRgb(rgbVals[2]); - const b = padRgb(rgbVals[3]); - const hexStr = '#' + a + r + g + b; + var rgbVals = rgbStr.match(/[\d.]+/g).map(function (v, i) { return (i < 3) ? parseInt(v, 10) : parseFloat(v); }); + if (rgbVals.length < 3) { + return; + } if (window.StatusBar) { + // try to let the StatusBar plugin handle it + // TODO: Use `padStart(2, '0')` once SDK 24 is dropped. + const padRgb = (val) => { + const hex = val.toString(16); + return hex.length === 1 ? '0' + hex : hex; + }; + + const r = padRgb(rgbVals[0]); + const g = padRgb(rgbVals[1]); + const b = padRgb(rgbVals[2]); + const a = padRgb(255 * (rgbVals[3] !== undefined ? rgbVals[3] : 1.0)); + + const hexStr = '#' + a + r + g + b; window.StatusBar.backgroundColorByHexString(hexStr); } else { exec(null, null, 'SystemBarPlugin', 'setStatusBarBackgroundColor', rgbVals); diff --git a/framework/src/org/apache/cordova/SystemBarPlugin.java b/framework/src/org/apache/cordova/SystemBarPlugin.java index 4967b121..c03721f0 100644 --- a/framework/src/org/apache/cordova/SystemBarPlugin.java +++ b/framework/src/org/apache/cordova/SystemBarPlugin.java @@ -121,19 +121,17 @@ public class SystemBarPlugin extends CordovaPlugin { * If the supplied ARGB is invalid or fails to parse, it will silently ignore * the change request. * - * @param argbVals {A, R, G, B} + * @param argbVals {R, G, B, A} */ private void setStatusBarBackgroundColor(JSONArray argbVals) { try { - int a = argbVals.getInt(0); - int r = argbVals.getInt(1); - int g = argbVals.getInt(2); - int b = argbVals.getInt(3); - overrideStatusBarBackgroundColor = parseColorFromString(String.format("#%02X%02X%02X%02X", a, r, g, b)); + int r = argbVals.getInt(0); + int g = argbVals.getInt(1); + int b = argbVals.getInt(2); + int a = Math.round(255 * (float)argbVals.optDouble(3, 1.0)); - if (overrideStatusBarBackgroundColor != null) { - updateStatusBar(overrideStatusBarBackgroundColor); - } + overrideStatusBarBackgroundColor = Color.argb(a, r, g, b); + updateStatusBar(overrideStatusBarBackgroundColor); } catch (JSONException e) { // Silently skip }