From fd1b86b1cc645c95a30fff868f9560ea683d187d Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Wed, 18 Dec 2019 13:26:12 +0100 Subject: [PATCH] Fix sharing loyalty card with icon --- .../protect/card_locker/ImportURIHelper.java | 10 +++++----- .../java/protect/card_locker/ImportURITest.java | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/protect/card_locker/ImportURIHelper.java b/app/src/main/java/protect/card_locker/ImportURIHelper.java index e7f641ea4..7565a81ea 100644 --- a/app/src/main/java/protect/card_locker/ImportURIHelper.java +++ b/app/src/main/java/protect/card_locker/ImportURIHelper.java @@ -3,14 +3,13 @@ package protect.card_locker; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.net.Uri; +import android.util.Base64; import org.json.JSONException; import org.json.JSONObject; import java.io.InvalidObjectException; -import java.io.UnsupportedEncodingException; public class ImportURIHelper { private static final String STORE = DBHelper.LoyaltyCardDbIds.STORE; @@ -52,14 +51,15 @@ public class ImportURIHelper { Integer headerTextColor = Integer.parseInt(uri.getQueryParameter(HEADER_TEXT_COLOR)); String iconData = uri.getQueryParameter(ICON); Bitmap icon = null; + if(!iconData.isEmpty()) { - byte[] iconBytes = iconData.getBytes("UTF-8"); + byte[] iconBytes = Base64.decode(iconData, Base64.URL_SAFE); icon = DBHelper.convertBitmapBlobToBitmap(iconBytes); } ExtrasHelper extras = new ExtrasHelper().fromJSON(new JSONObject(uri.getQueryParameter(EXTRAS))); return new LoyaltyCard(-1, store, note, cardId, barcodeType, headerColor, headerTextColor, icon, extras); - } catch (NullPointerException | NumberFormatException | JSONException | UnsupportedEncodingException ex) { + } catch (NullPointerException | NumberFormatException | JSONException ex) { throw new InvalidObjectException("Not a valid import URI"); } } @@ -69,7 +69,7 @@ public class ImportURIHelper { String icon = ""; if(loyaltyCard.icon != null) { - icon = DBHelper.convertBitmapToBlob(loyaltyCard.icon).toString(); + icon = Base64.encodeToString(DBHelper.convertBitmapToBlob(loyaltyCard.icon), Base64.URL_SAFE); } Uri.Builder uriBuilder = new Uri.Builder(); diff --git a/app/src/test/java/protect/card_locker/ImportURITest.java b/app/src/test/java/protect/card_locker/ImportURITest.java index 77cb2a137..d69ee875b 100644 --- a/app/src/test/java/protect/card_locker/ImportURITest.java +++ b/app/src/test/java/protect/card_locker/ImportURITest.java @@ -6,18 +6,20 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.net.Uri; + import com.google.zxing.BarcodeFormat; import org.json.JSONException; -import org.json.JSONObject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; + import java.io.InvalidObjectException; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static protect.card_locker.DBHelper.LoyaltyCardDbIds; @@ -40,12 +42,14 @@ public class ImportURITest { { // Generate card Bitmap icon = BitmapFactory.decodeResource(Resources.getSystem(), R.drawable.app_icon_intro); + assertNotNull(icon); ExtrasHelper extrasHelper = new ExtrasHelper(); extrasHelper.addLanguageValue("en", "key", "value"); db.insertLoyaltyCard("store", "note", BarcodeFormat.UPC_A.toString(), LoyaltyCardDbIds.BARCODE_TYPE, Color.BLACK, Color.WHITE, icon, extrasHelper); // Get card LoyaltyCard card = db.getLoyaltyCard(1); + assertNotNull(card.icon); // Card to URI Uri cardUri = importURIHelper.toUri(card); @@ -60,7 +64,15 @@ public class ImportURITest { assertEquals(card.headerTextColor, parsedCard.headerTextColor); assertEquals(card.note, parsedCard.note); assertEquals(card.store, parsedCard.store); - assertEquals(card.icon.getRowBytes(), parsedCard.icon.getRowBytes()); + assertEquals(card.icon.getWidth(), parsedCard.icon.getWidth()); + assertEquals(card.icon.getHeight(), parsedCard.icon.getHeight()); + for(int i = 0; i < card.icon.getWidth(); i++) + { + for(int j = 0; j < card.icon.getHeight(); j++) + { + assertEquals(card.icon.getPixel(i, j), parsedCard.icon.getPixel(i, j)); + } + } assertEquals(card.extras.toJSON().toString(), parsedCard.extras.toJSON().toString()); }