Skip to content

Commit 7184110

Browse files
committed
feat: use getComputedStyle for setBackgroundColor
1 parent 68b8ac3 commit 7184110

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

cordova-js-src/plugin/android/statusbar.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,31 @@ Object.defineProperty(statusBar, 'setBackgroundColor', {
5454
enumerable: false,
5555
writable: false,
5656
value: function (value) {
57-
// Confirm if value is a valid hex code is before passing to native-side
58-
if (!(/^#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(value))) {
59-
console.error('Invalid color hex code. Valid format: #RRGGBB or #AARRGGBB');
57+
var script = document.querySelector('script[src$="cordova.js"]');
58+
script.style.color = value;
59+
var rgbStr = window.getComputedStyle(script).getPropertyValue('color');
60+
61+
if (!rgbStr.match(/^rgb/)) { return; }
62+
63+
var rgbVals = rgbStr.match(/\d+/g).map(function (v) { return parseInt(v, 10); });
64+
65+
if (rgbVals.length < 3) {
6066
return;
67+
} else if (rgbVals.length === 3) {
68+
rgbVals = [255].concat(rgbVals);
6169
}
6270

71+
const padRgb = (val) => val.toString(16).padStart(2, '0');
72+
const a = padRgb(rgbVals[0]);
73+
const r = padRgb(rgbVals[1]);
74+
const g = padRgb(rgbVals[2]);
75+
const b = padRgb(rgbVals[3]);
76+
const hexStr = '#' + a + r + g + b;
77+
6378
if (window.StatusBar) {
64-
window.StatusBar.backgroundColorByHexString(value);
79+
window.StatusBar.backgroundColorByHexString(hexStr);
6580
} else {
66-
exec(null, null, 'SystemBarPlugin', 'setStatusBarBackgroundColor', [value]);
81+
exec(null, null, 'SystemBarPlugin', 'setStatusBarBackgroundColor', rgbVals);
6782
}
6883
}
6984
});

framework/src/org/apache/cordova/SystemBarPlugin.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
8989
boolean visible = args.getBoolean(0);
9090
cordova.getActivity().runOnUiThread(() -> setStatusBarVisible(visible));
9191
} else if ("setStatusBarBackgroundColor".equals(action)) {
92-
String bgColor = args.getString(0);
93-
cordova.getActivity().runOnUiThread(() -> setStatusBarBackgroundColor(bgColor));
92+
cordova.getActivity().runOnUiThread(() -> setStatusBarBackgroundColor(args));
9493
} else {
9594
return false;
9695
}
@@ -120,17 +119,27 @@ private void setStatusBarVisible(final boolean visible) {
120119

121120
/**
122121
* Allow the app to override the status bar background color from JS API.
123-
* If the supplied string is invalid and fails to parse, it will silently ignore
122+
* If the supplied ARGB is invalid or fails to parse, it will silently ignore
124123
* the change request.
125124
*
126-
* @param colorPref hex string
125+
* @param argbVals {A, R, G, B}
127126
*/
128-
private void setStatusBarBackgroundColor(final String colorPref) {
129-
int parsedColor = parseColorFromString(colorPref);
130-
if (parsedColor == INVALID_COLOR) return;
131-
132-
overrideStatusBarBackgroundColor = parsedColor;
133-
updateStatusBar(overrideStatusBarBackgroundColor);
127+
private void setStatusBarBackgroundColor(JSONArray argbVals) {
128+
try {
129+
int a = argbVals.getInt(0);
130+
int r = argbVals.getInt(1);
131+
int g = argbVals.getInt(2);
132+
int b = argbVals.getInt(3);
133+
String hexColor = String.format("#%02X%02X%02X%02X", a, r, g, b);
134+
135+
int parsedColor = parseColorFromString(hexColor);
136+
if (parsedColor == INVALID_COLOR) return;
137+
138+
overrideStatusBarBackgroundColor = parsedColor;
139+
updateStatusBar(overrideStatusBarBackgroundColor);
140+
} catch (JSONException e) {
141+
// Silently skip
142+
}
134143
}
135144

136145
/**

0 commit comments

Comments
 (0)