From 703db472fc8bedb39ec2c99fcfe8d709d2926afe Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Tue, 10 Dec 2019 18:04:12 +0100 Subject: [PATCH] Start implementing color parsing --- .../protect/card_locker/PkpassImporter.java | 31 ++++++++++++++++++- .../java/protect/card_locker/PkpassTest.java | 3 ++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/PkpassImporter.java b/app/src/main/java/protect/card_locker/PkpassImporter.java index 8c492d350..91deaaaf6 100644 --- a/app/src/main/java/protect/card_locker/PkpassImporter.java +++ b/app/src/main/java/protect/card_locker/PkpassImporter.java @@ -1,6 +1,7 @@ package protect.card_locker; import android.content.Context; +import android.graphics.Color; import android.net.Uri; import com.google.common.collect.ImmutableMap; @@ -12,6 +13,8 @@ import org.json.JSONObject; import java.io.IOException; import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -93,6 +96,32 @@ public class PkpassImporter { return null; } - return new LoyaltyCard(-1, store, note, cardId, barcodeType, null, null); + // Prepare to parse colors + Pattern rgbPattern = Pattern.compile("^rgb\\(\\s*(\\d{1,3})\\s*,\\s*(\\d{1,3})\\s*,\\s*(\\d{1,3})\\s*\\)$"); + + // Optional. Background color of the pass, specified as an CSS-style RGB triple. For example, rgb(23, 187, 82). + Integer headerColor = null; + Matcher headerColorMatcher = rgbPattern.matcher(json.getString("backgroundColor")); + if(headerColorMatcher.find()) + { + headerColor = Color.rgb( + Integer.parseInt(headerColorMatcher.group(0)), + Integer.parseInt(headerColorMatcher.group(1)), + Integer.parseInt(headerColorMatcher.group(2))); + } + + + // Optional. Color of the label text, specified as a CSS-style RGB triple. For example, rgb(255, 255, 255). + Integer headerTextColor = null; + Matcher headerTextColorMatcher = rgbPattern.matcher(json.getString("labelColor")); + if(headerTextColorMatcher.find()) + { + headerTextColor = Color.rgb( + Integer.parseInt(headerTextColorMatcher.group(0)), + Integer.parseInt(headerTextColorMatcher.group(1)), + Integer.parseInt(headerTextColorMatcher.group(2))); + } + + return new LoyaltyCard(-1, store, note, cardId, barcodeType, headerColor, headerTextColor); } } \ No newline at end of file diff --git a/app/src/test/java/protect/card_locker/PkpassTest.java b/app/src/test/java/protect/card_locker/PkpassTest.java index 767af5dcc..ed408fe02 100644 --- a/app/src/test/java/protect/card_locker/PkpassTest.java +++ b/app/src/test/java/protect/card_locker/PkpassTest.java @@ -1,6 +1,7 @@ package protect.card_locker; import android.app.Activity; +import android.graphics.Color; import org.json.JSONException; import org.json.JSONObject; @@ -110,5 +111,7 @@ public class PkpassTest { assertEquals(card.cardId, "0000001"); assertEquals(card.note, "Staff Pass for Employee Number 001"); assertEquals(card.store, "Passbook Example Company"); + assertEquals(card.headerColor, String.valueOf(Color.rgb(90, 90, 90))); + assertEquals(card.headerTextColor, String.valueOf(Color.rgb(255, 255, 255))); } }