From b216290a6a24305e2fe6d72740b78f526b6d8b28 Mon Sep 17 00:00:00 2001 From: Thomas Vidas Date: Mon, 14 Dec 2020 12:07:31 -0500 Subject: [PATCH 1/8] Add ColorExtended utility for parsing color --- .../main/java/com/getcapacitor/Bridge.java | 4 ++- .../com/getcapacitor/util/ColorExtended.java | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 android/capacitor/src/main/java/com/getcapacitor/util/ColorExtended.java diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index cd582024d..1379401d7 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -43,6 +43,8 @@ import org.json.JSONException; import org.json.JSONObject; +import main.java.com.getcapacitor.util.ColorExtended; + /** * The Bridge class is the main engine of Capacitor. It manages * loading and communicating with all Plugins, @@ -400,7 +402,7 @@ private void initWebView() { String backgroundColor = this.config.getString("android.backgroundColor", this.config.getString("backgroundColor", null)); try { if (backgroundColor != null) { - webView.setBackgroundColor(Color.parseColor(backgroundColor)); + webView.setBackgroundColor(ColorExtended.parseColor(backgroundColor, ColorExtended.ColorFormat.RGBA)); } } catch (IllegalArgumentException ex) { Logger.debug("WebView background color not applied"); diff --git a/android/capacitor/src/main/java/com/getcapacitor/util/ColorExtended.java b/android/capacitor/src/main/java/com/getcapacitor/util/ColorExtended.java new file mode 100644 index 000000000..e5b751624 --- /dev/null +++ b/android/capacitor/src/main/java/com/getcapacitor/util/ColorExtended.java @@ -0,0 +1,32 @@ +package main.java.com.getcapacitor.util; + +import android.graphics.Color; + +public class ColorExtended extends Color { + public enum ColorFormat { + RGB, + RGBA, + ARGB, + // Add support for HSV in the future? + } + + /** + * Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. + * @param colorString The hexadecimal color string. + * @param incomingFormat The format that the hex string is in. + * @return The corresponding color as an int. + */ + public static int parseColor(String colorString, ColorFormat incomingFormat) { + if (incomingFormat == ColorFormat.RGB || incomingFormat == ColorFormat.ARGB) { + return Color.parseColor(colorString); + } else { + if (colorString.length() != 9) { + throw new IllegalArgumentException("The encoded color space is invalid or unknown"); + } + + // Format should be #RRGGBBAA at this point, convert to #AARRGGBB + String reformattedString = "#" + colorString.substring(7) + colorString.substring(1, 7); + return Color.parseColor(reformattedString); + } + } +} From 0c62e049f625c426dd862951988bfe15f63b7846 Mon Sep 17 00:00:00 2001 From: Thomas Vidas Date: Mon, 14 Dec 2020 13:07:49 -0500 Subject: [PATCH 2/8] Rename class and method names, only accept rgba and rgb strings --- .../main/java/com/getcapacitor/Bridge.java | 4 +-- .../com/getcapacitor/util/ColorExtended.java | 32 ------------------- .../java/com/getcapacitor/util/WebColor.java | 27 ++++++++++++++++ 3 files changed, 29 insertions(+), 34 deletions(-) delete mode 100644 android/capacitor/src/main/java/com/getcapacitor/util/ColorExtended.java create mode 100644 android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 1379401d7..ad19cb2de 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -43,7 +43,7 @@ import org.json.JSONException; import org.json.JSONObject; -import main.java.com.getcapacitor.util.ColorExtended; +import main.java.com.getcapacitor.util.WebColor; /** * The Bridge class is the main engine of Capacitor. It manages @@ -402,7 +402,7 @@ private void initWebView() { String backgroundColor = this.config.getString("android.backgroundColor", this.config.getString("backgroundColor", null)); try { if (backgroundColor != null) { - webView.setBackgroundColor(ColorExtended.parseColor(backgroundColor, ColorExtended.ColorFormat.RGBA)); + webView.setBackgroundColor(WebColor.parseColorRGBA(backgroundColor)); } } catch (IllegalArgumentException ex) { Logger.debug("WebView background color not applied"); diff --git a/android/capacitor/src/main/java/com/getcapacitor/util/ColorExtended.java b/android/capacitor/src/main/java/com/getcapacitor/util/ColorExtended.java deleted file mode 100644 index e5b751624..000000000 --- a/android/capacitor/src/main/java/com/getcapacitor/util/ColorExtended.java +++ /dev/null @@ -1,32 +0,0 @@ -package main.java.com.getcapacitor.util; - -import android.graphics.Color; - -public class ColorExtended extends Color { - public enum ColorFormat { - RGB, - RGBA, - ARGB, - // Add support for HSV in the future? - } - - /** - * Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. - * @param colorString The hexadecimal color string. - * @param incomingFormat The format that the hex string is in. - * @return The corresponding color as an int. - */ - public static int parseColor(String colorString, ColorFormat incomingFormat) { - if (incomingFormat == ColorFormat.RGB || incomingFormat == ColorFormat.ARGB) { - return Color.parseColor(colorString); - } else { - if (colorString.length() != 9) { - throw new IllegalArgumentException("The encoded color space is invalid or unknown"); - } - - // Format should be #RRGGBBAA at this point, convert to #AARRGGBB - String reformattedString = "#" + colorString.substring(7) + colorString.substring(1, 7); - return Color.parseColor(reformattedString); - } - } -} diff --git a/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java new file mode 100644 index 000000000..307ee7f25 --- /dev/null +++ b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java @@ -0,0 +1,27 @@ +package main.java.com.getcapacitor.util; + +import android.graphics.Color; + +public class WebColor { + /** + * Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. + * @param colorString The hexadecimal color string. The format is an RGB or RGBA hex string. + * @return The corresponding color as an int. + */ + public static int parseColorRGBA(String colorString) { + String formattedColor = colorString; + if (colorString.charAt(0) != '#') { + formattedColor = "#" + formattedColor; + } + + if (formattedColor.length() != 7 || formattedColor.length() != 9) { + throw new IllegalArgumentException("The encoded color space is invalid or unknown"); + } else if (formattedColor.length() == 7) { + return Color.parseColor(colorString); + } else { + // Convert to Android format #AARRGGBB from #RRGGBBAA + formattedColor = "#" + formattedColor.substring(7) + formattedColor.substring(1, 7); + return Color.parseColor(formattedColor); + } + } +} From ae641acb6a28791db890773cf254d77dfdb5506d Mon Sep 17 00:00:00 2001 From: Thomas Vidas Date: Mon, 14 Dec 2020 14:00:46 -0500 Subject: [PATCH 3/8] rename --- android/capacitor/src/main/java/com/getcapacitor/Bridge.java | 2 +- .../capacitor/src/main/java/com/getcapacitor/util/WebColor.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index ad19cb2de..b9153f736 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -402,7 +402,7 @@ private void initWebView() { String backgroundColor = this.config.getString("android.backgroundColor", this.config.getString("backgroundColor", null)); try { if (backgroundColor != null) { - webView.setBackgroundColor(WebColor.parseColorRGBA(backgroundColor)); + webView.setBackgroundColor(WebColor.parseColor(backgroundColor)); } } catch (IllegalArgumentException ex) { Logger.debug("WebView background color not applied"); diff --git a/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java index 307ee7f25..01c01fee9 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java +++ b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java @@ -8,7 +8,7 @@ public class WebColor { * @param colorString The hexadecimal color string. The format is an RGB or RGBA hex string. * @return The corresponding color as an int. */ - public static int parseColorRGBA(String colorString) { + public static int parseColor(String colorString) { String formattedColor = colorString; if (colorString.charAt(0) != '#') { formattedColor = "#" + formattedColor; From 9217e7a63cc5daff1ef950e1a7c6b0c907fd8bc5 Mon Sep 17 00:00:00 2001 From: Thomas Vidas Date: Mon, 14 Dec 2020 14:24:08 -0500 Subject: [PATCH 4/8] Package name fix --- .../capacitor/src/main/java/com/getcapacitor/util/WebColor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java index 01c01fee9..4d18fe4be 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java +++ b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java @@ -1,4 +1,4 @@ -package main.java.com.getcapacitor.util; +package com.getcapacitor.util; import android.graphics.Color; From fcd10d317faed32a2dce52b9958367ce793ac230 Mon Sep 17 00:00:00 2001 From: Thomas Vidas Date: Mon, 14 Dec 2020 14:44:16 -0500 Subject: [PATCH 5/8] Update android/capacitor/src/main/java/com/getcapacitor/Bridge.java Co-authored-by: Carl Poole --- android/capacitor/src/main/java/com/getcapacitor/Bridge.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index b9153f736..15eed7231 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -43,7 +43,7 @@ import org.json.JSONException; import org.json.JSONObject; -import main.java.com.getcapacitor.util.WebColor; +import com.getcapacitor.util.WebColor; /** * The Bridge class is the main engine of Capacitor. It manages From 302c48f639e399de905649d51bea16e8eaa63021 Mon Sep 17 00:00:00 2001 From: Thomas Vidas Date: Mon, 14 Dec 2020 15:20:12 -0500 Subject: [PATCH 6/8] Bad boolean --- .../capacitor/src/main/java/com/getcapacitor/util/WebColor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java index 4d18fe4be..224ec9fff 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java +++ b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java @@ -14,7 +14,7 @@ public static int parseColor(String colorString) { formattedColor = "#" + formattedColor; } - if (formattedColor.length() != 7 || formattedColor.length() != 9) { + if (formattedColor.length() != 7 && formattedColor.length() != 9) { throw new IllegalArgumentException("The encoded color space is invalid or unknown"); } else if (formattedColor.length() == 7) { return Color.parseColor(colorString); From 9beaf1d72a886c9f4257c08f5646a5d04630102d Mon Sep 17 00:00:00 2001 From: Thomas Vidas Date: Mon, 14 Dec 2020 15:25:25 -0500 Subject: [PATCH 7/8] Fix strings not prefixed with # --- .../capacitor/src/main/java/com/getcapacitor/util/WebColor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java index 224ec9fff..ee6248479 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java +++ b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java @@ -17,7 +17,7 @@ public static int parseColor(String colorString) { if (formattedColor.length() != 7 && formattedColor.length() != 9) { throw new IllegalArgumentException("The encoded color space is invalid or unknown"); } else if (formattedColor.length() == 7) { - return Color.parseColor(colorString); + return Color.parseColor(formattedColor); } else { // Convert to Android format #AARRGGBB from #RRGGBBAA formattedColor = "#" + formattedColor.substring(7) + formattedColor.substring(1, 7); From 71dae802438b3194a916e791e4dd7896f83e2fc3 Mon Sep 17 00:00:00 2001 From: Thomas Vidas Date: Mon, 14 Dec 2020 15:37:44 -0500 Subject: [PATCH 8/8] fmt --- android/capacitor/src/main/java/com/getcapacitor/Bridge.java | 3 +-- .../src/main/java/com/getcapacitor/util/WebColor.java | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 15eed7231..c43ce3fbe 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -26,6 +26,7 @@ import com.getcapacitor.cordova.MockCordovaWebViewImpl; import com.getcapacitor.util.HostMask; import com.getcapacitor.util.PermissionHelper; +import com.getcapacitor.util.WebColor; import java.io.File; import java.net.SocketTimeoutException; import java.net.URL; @@ -43,8 +44,6 @@ import org.json.JSONException; import org.json.JSONObject; -import com.getcapacitor.util.WebColor; - /** * The Bridge class is the main engine of Capacitor. It manages * loading and communicating with all Plugins, diff --git a/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java index ee6248479..e055021e7 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java +++ b/android/capacitor/src/main/java/com/getcapacitor/util/WebColor.java @@ -3,6 +3,7 @@ import android.graphics.Color; public class WebColor { + /** * Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. * @param colorString The hexadecimal color string. The format is an RGB or RGBA hex string.