mirror of
https://github.com/apache/cordova-android.git
synced 2026-07-06 00:00:08 +08:00
fix(statusbar): Properly handle CSS colours (#1955)
This commit is contained in:
+18
-17
@@ -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);
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user