From 8eeff0058bc7c788231dd0698a8a32496d2fdc1a Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Tue, 1 Oct 2024 17:33:45 +0200 Subject: [PATCH] Refactor ScanActivity result code (use ParseResult) --- .../protect/card_locker/BarcodeValues.java | 29 ------- ...arcodeValuesListDisambiguatorCallback.java | 6 -- .../java/protect/card_locker/LoyaltyCard.java | 68 +++++++++++------ .../card_locker/LoyaltyCardEditActivity.java | 28 +++---- .../protect/card_locker/MainActivity.java | 40 +++++----- .../java/protect/card_locker/ParseResult.kt | 29 +++++++ .../ParseResultListDisambiguatorCallback.java | 6 ++ .../protect/card_locker/ParseResultType.kt | 6 ++ .../protect/card_locker/ScanActivity.java | 49 +++++------- .../main/java/protect/card_locker/Utils.java | 76 +++++++++++-------- app/src/main/res/values/strings.xml | 1 - .../LoyaltyCardViewActivityTest.java | 24 ++++-- 12 files changed, 197 insertions(+), 165 deletions(-) delete mode 100644 app/src/main/java/protect/card_locker/BarcodeValues.java delete mode 100644 app/src/main/java/protect/card_locker/BarcodeValuesListDisambiguatorCallback.java create mode 100644 app/src/main/java/protect/card_locker/ParseResult.kt create mode 100644 app/src/main/java/protect/card_locker/ParseResultListDisambiguatorCallback.java create mode 100644 app/src/main/java/protect/card_locker/ParseResultType.kt diff --git a/app/src/main/java/protect/card_locker/BarcodeValues.java b/app/src/main/java/protect/card_locker/BarcodeValues.java deleted file mode 100644 index 567b6aab3..000000000 --- a/app/src/main/java/protect/card_locker/BarcodeValues.java +++ /dev/null @@ -1,29 +0,0 @@ -package protect.card_locker; - -import androidx.annotation.Nullable; - -public class BarcodeValues { - @Nullable - private final CatimaBarcode mFormat; - private final String mContent; - private String mNote; - - public BarcodeValues(@Nullable CatimaBarcode format, String content) { - mFormat = format; - mContent = content; - } - - public void setNote(String note) { - mNote = note; - } - - public @Nullable CatimaBarcode format() { - return mFormat; - } - - public String content() { - return mContent; - } - - public String note() { return mNote; } -} \ No newline at end of file diff --git a/app/src/main/java/protect/card_locker/BarcodeValuesListDisambiguatorCallback.java b/app/src/main/java/protect/card_locker/BarcodeValuesListDisambiguatorCallback.java deleted file mode 100644 index c5c55395d..000000000 --- a/app/src/main/java/protect/card_locker/BarcodeValuesListDisambiguatorCallback.java +++ /dev/null @@ -1,6 +0,0 @@ -package protect.card_locker; - -public interface BarcodeValuesListDisambiguatorCallback { - void onUserChoseBarcode(BarcodeValues barcodeValues); - void onUserDismissedSelector(); -} diff --git a/app/src/main/java/protect/card_locker/LoyaltyCard.java b/app/src/main/java/protect/card_locker/LoyaltyCard.java index 2b77ea498..03473f9ea 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCard.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCard.java @@ -11,6 +11,7 @@ import androidx.annotation.Nullable; import java.math.BigDecimal; import java.util.Currency; import java.util.Date; +import java.util.List; import java.util.Objects; public class LoyaltyCard implements Parcelable { @@ -227,6 +228,7 @@ public class LoyaltyCard implements Parcelable { parcel.writeInt(archiveStatus); } + @NonNull public static LoyaltyCard fromBundle(Bundle bundle, boolean requireFull) { // Grab default card LoyaltyCard loyaltyCard = new LoyaltyCard(); @@ -238,7 +240,7 @@ public class LoyaltyCard implements Parcelable { return loyaltyCard; } - public void updateFromBundle(Bundle bundle, boolean requireFull) { + public void updateFromBundle(@NonNull Bundle bundle, boolean requireFull) { if (bundle.containsKey(BUNDLE_LOYALTY_CARD_ID)) { setId(bundle.getInt(BUNDLE_LOYALTY_CARD_ID)); } else if (requireFull) { @@ -321,34 +323,56 @@ public class LoyaltyCard implements Parcelable { } } - public Bundle toBundle() { + public Bundle toBundle(List exportLimit) { + boolean exportIsLimited = !exportLimit.isEmpty(); + Bundle bundle = new Bundle(); - bundle.putInt(BUNDLE_LOYALTY_CARD_ID, id); - bundle.putString(BUNDLE_LOYALTY_CARD_STORE, store); - bundle.putString(BUNDLE_LOYALTY_CARD_NOTE, note); - if (validFrom != null) { - bundle.putLong(BUNDLE_LOYALTY_CARD_VALID_FROM, validFrom.getTime()); + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_ID)) { + bundle.putInt(BUNDLE_LOYALTY_CARD_ID, id); } - if (expiry != null) { - bundle.putLong(BUNDLE_LOYALTY_CARD_EXPIRY, expiry.getTime()); + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_STORE)) { + bundle.putString(BUNDLE_LOYALTY_CARD_STORE, store); } - bundle.putString(BUNDLE_LOYALTY_CARD_BALANCE, balance.toString()); - if (balanceType != null) { - bundle.putString(BUNDLE_LOYALTY_CARD_BALANCE_TYPE, balanceType.toString()); + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_NOTE)) { + bundle.putString(BUNDLE_LOYALTY_CARD_NOTE, note); } - bundle.putString(BUNDLE_LOYALTY_CARD_CARD_ID, cardId); - bundle.putString(BUNDLE_LOYALTY_CARD_BARCODE_ID, barcodeId); - if (barcodeType != null) { - bundle.putString(BUNDLE_LOYALTY_CARD_BARCODE_TYPE, barcodeType.name()); + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_VALID_FROM)) { + bundle.putLong(BUNDLE_LOYALTY_CARD_VALID_FROM, validFrom != null ? validFrom.getTime() : -1); } - if (headerColor != null) { - bundle.putInt(BUNDLE_LOYALTY_CARD_HEADER_COLOR, headerColor); + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_EXPIRY)) { + bundle.putLong(BUNDLE_LOYALTY_CARD_EXPIRY, expiry != null ? expiry.getTime() : -1); + } + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_BALANCE)) { + bundle.putString(BUNDLE_LOYALTY_CARD_BALANCE, balance.toString()); + } + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_BALANCE_TYPE)) { + bundle.putString(BUNDLE_LOYALTY_CARD_BALANCE_TYPE, balanceType != null ? balanceType.toString() : null); + } + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_CARD_ID)) { + bundle.putString(BUNDLE_LOYALTY_CARD_CARD_ID, cardId); + } + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_BARCODE_ID)) { + bundle.putString(BUNDLE_LOYALTY_CARD_BARCODE_ID, barcodeId); + } + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_BARCODE_TYPE)) { + bundle.putString(BUNDLE_LOYALTY_CARD_BARCODE_TYPE, barcodeType != null ? barcodeType.name() : null); + } + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_HEADER_COLOR)) { + bundle.putInt(BUNDLE_LOYALTY_CARD_HEADER_COLOR, headerColor != null ? headerColor : -1); + } + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_STAR_STATUS)) { + bundle.putInt(BUNDLE_LOYALTY_CARD_STAR_STATUS, starStatus); + } + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_LAST_USED)) { + bundle.putLong(BUNDLE_LOYALTY_CARD_LAST_USED, lastUsed); + } + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_ZOOM_LEVEL)) { + bundle.putInt(BUNDLE_LOYALTY_CARD_ZOOM_LEVEL, zoomLevel); + } + if (!exportIsLimited || exportLimit.contains(BUNDLE_LOYALTY_CARD_ARCHIVE_STATUS)) { + bundle.putInt(BUNDLE_LOYALTY_CARD_ARCHIVE_STATUS, archiveStatus); } - bundle.putInt(BUNDLE_LOYALTY_CARD_STAR_STATUS, starStatus); - bundle.putLong(BUNDLE_LOYALTY_CARD_LAST_USED, lastUsed); - bundle.putInt(BUNDLE_LOYALTY_CARD_ZOOM_LEVEL, zoomLevel); - bundle.putInt(BUNDLE_LOYALTY_CARD_ARCHIVE_STATUS, archiveStatus); return bundle; } diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index 241e99af7..ecaa208b9 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -693,27 +693,21 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements mCardIdAndBarCodeEditorLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { - Intent intent = result.getData(); - if (intent == null) { - Log.d("barcode card id editor", "barcode and card id editor picker returned without an intent"); + Intent resultIntent = result.getData(); + if (resultIntent == null) { + Log.d(TAG, "barcode and card id editor picker returned without an intent"); return; } - List barcodeValuesList = Utils.parseSetBarcodeActivityResult(Utils.BARCODE_SCAN, result.getResultCode(), intent, getApplicationContext()); + Bundle resultIntentBundle = resultIntent.getExtras(); + if (resultIntentBundle == null) { + Log.d(TAG, "barcode and card id editor picker returned without a bundle"); + return; + } - Utils.makeUserChooseBarcodeFromList(this, barcodeValuesList, new BarcodeValuesListDisambiguatorCallback() { - @Override - public void onUserChoseBarcode(BarcodeValues barcodeValues) { - setLoyaltyCardCardId(barcodeValues.content()); - setLoyaltyCardBarcodeType(barcodeValues.format()); - setLoyaltyCardBarcodeId(""); - } - - @Override - public void onUserDismissedSelector() { - - } - }); + tempLoyaltyCard.updateFromBundle(resultIntentBundle, false); + generateBarcode(); + hasChanged = true; } }); diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index a8e97c298..bc02562f6 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -257,12 +257,9 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard return; } - Intent intent = result.getData(); - List barcodeValuesList = Utils.parseSetBarcodeActivityResult(Utils.BARCODE_SCAN, result.getResultCode(), intent, this); - - Bundle inputBundle = intent.getExtras(); - String group = inputBundle != null ? inputBundle.getString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) : null; - processBarcodeValuesList(barcodeValuesList, group, false); + Intent editIntent = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class); + editIntent.putExtras(result.getData().getExtras()); + startActivity(editIntent); }); mSettingsLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { @@ -422,21 +419,16 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard } } - private void processBarcodeValuesList(List barcodeValuesList, String group, boolean closeAppOnNoBarcode) { - if (barcodeValuesList.isEmpty()) { - throw new IllegalArgumentException("barcodesValues may not be empty"); + private void processParseResultList(List parseResultList, String group, boolean closeAppOnNoBarcode) { + if (parseResultList.isEmpty()) { + throw new IllegalArgumentException("parseResultList may not be empty"); } - Utils.makeUserChooseBarcodeFromList(MainActivity.this, barcodeValuesList, new BarcodeValuesListDisambiguatorCallback() { + Utils.makeUserChooseParseResultFromList(MainActivity.this, parseResultList, new ParseResultListDisambiguatorCallback() { @Override - public void onUserChoseBarcode(BarcodeValues barcodeValues) { - CatimaBarcode barcodeType = barcodeValues.format(); - + public void onUserChoseParseResult(ParseResult parseResult) { Intent intent = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class); - Bundle bundle = new Bundle(); - bundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID, barcodeValues.content()); - bundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_TYPE, barcodeType != null ? barcodeType.name() : null); - bundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_ID, null); + Bundle bundle = parseResult.toLoyaltyCardBundle(); if (group != null) { bundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group); } @@ -459,25 +451,27 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard // Check if an image or file was shared to us if (Intent.ACTION_SEND.equals(receivedAction)) { - List barcodeValuesList; + List parseResultList; if (receivedType.equals("text/plain")) { - barcodeValuesList = Collections.singletonList(new BarcodeValues(null, intent.getStringExtra(Intent.EXTRA_TEXT))); + LoyaltyCard loyaltyCard = new LoyaltyCard(); + loyaltyCard.setCardId(intent.getStringExtra(Intent.EXTRA_TEXT)); + parseResultList = Collections.singletonList(new ParseResult(ParseResultType.BARCODE_ONLY, loyaltyCard)); } else if (receivedType.startsWith("image/")) { - barcodeValuesList = Utils.retrieveBarcodesFromImage(this, intent.getParcelableExtra(Intent.EXTRA_STREAM)); + parseResultList = Utils.retrieveBarcodesFromImage(this, intent.getParcelableExtra(Intent.EXTRA_STREAM)); } else if (receivedType.equals("application/pdf")) { - barcodeValuesList = Utils.retrieveBarcodesFromPdf(this, intent.getParcelableExtra(Intent.EXTRA_STREAM)); + parseResultList = Utils.retrieveBarcodesFromPdf(this, intent.getParcelableExtra(Intent.EXTRA_STREAM)); } else { Log.e(TAG, "Wrong mime-type"); return; } - if (barcodeValuesList.isEmpty()) { + if (parseResultList.isEmpty()) { finish(); return; } - processBarcodeValuesList(barcodeValuesList, null, true); + processParseResultList(parseResultList, null, true); } } diff --git a/app/src/main/java/protect/card_locker/ParseResult.kt b/app/src/main/java/protect/card_locker/ParseResult.kt new file mode 100644 index 000000000..190db5ec1 --- /dev/null +++ b/app/src/main/java/protect/card_locker/ParseResult.kt @@ -0,0 +1,29 @@ +package protect.card_locker + +import android.os.Bundle + +class ParseResult( + val parseResultType: ParseResultType, + val loyaltyCard: LoyaltyCard) { + var note: String? = null + + fun toLoyaltyCardBundle(): Bundle { + when (parseResultType) { + ParseResultType.FULL -> return loyaltyCard.toBundle(listOf()) + ParseResultType.BARCODE_ONLY -> { + val defaultLoyaltyCard = LoyaltyCard() + defaultLoyaltyCard.setBarcodeId(null) + defaultLoyaltyCard.setBarcodeType(loyaltyCard.barcodeType) + defaultLoyaltyCard.setCardId(loyaltyCard.cardId) + + return defaultLoyaltyCard.toBundle( + listOf( + LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_ID, + LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_TYPE, + LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID + ) + ) + } + } + } +} diff --git a/app/src/main/java/protect/card_locker/ParseResultListDisambiguatorCallback.java b/app/src/main/java/protect/card_locker/ParseResultListDisambiguatorCallback.java new file mode 100644 index 000000000..50acba584 --- /dev/null +++ b/app/src/main/java/protect/card_locker/ParseResultListDisambiguatorCallback.java @@ -0,0 +1,6 @@ +package protect.card_locker; + +public interface ParseResultListDisambiguatorCallback { + void onUserChoseParseResult(ParseResult parseResult); + void onUserDismissedSelector(); +} diff --git a/app/src/main/java/protect/card_locker/ParseResultType.kt b/app/src/main/java/protect/card_locker/ParseResultType.kt new file mode 100644 index 000000000..219644645 --- /dev/null +++ b/app/src/main/java/protect/card_locker/ParseResultType.kt @@ -0,0 +1,6 @@ +package protect.card_locker + +enum class ParseResultType { + FULL, + BARCODE_ONLY +} diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index dff50567b..fea6db1cf 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -156,7 +156,7 @@ public class ScanActivity extends CatimaAppCompatActivity { addFromImage(); break; case 3: - addFromPdfFile(); + addFromPdf(); break; default: throw new IllegalArgumentException("Unknown 'Add a card in a different way' dialog option"); @@ -181,16 +181,11 @@ public class ScanActivity extends CatimaAppCompatActivity { barcodeScannerView.decodeSingle(new BarcodeCallback() { @Override public void barcodeResult(BarcodeResult result) { - Intent scanResult = new Intent(); - Bundle scanResultBundle = new Bundle(); - scanResultBundle.putString(BARCODE_CONTENTS, result.getText()); - scanResultBundle.putString(BARCODE_FORMAT, result.getBarcodeFormat().name()); - if (addGroup != null) { - scanResultBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, addGroup); - } - scanResult.putExtras(scanResultBundle); - ScanActivity.this.setResult(RESULT_OK, scanResult); - finish(); + LoyaltyCard loyaltyCard = new LoyaltyCard(); + loyaltyCard.setCardId(result.getText()); + loyaltyCard.setBarcodeType(CatimaBarcode.fromBarcode(result.getBarcodeFormat())); + + returnResult(new ParseResult(ParseResultType.BARCODE_ONLY, loyaltyCard)); } @Override @@ -294,35 +289,31 @@ public class ScanActivity extends CatimaAppCompatActivity { mScannerActive = isActive; } - private void returnResult(String barcodeContents, String barcodeFormat) { - Intent manualResult = new Intent(); - Bundle manualResultBundle = new Bundle(); - manualResultBundle.putString(BARCODE_CONTENTS, barcodeContents); - manualResultBundle.putString(BARCODE_FORMAT, barcodeFormat); + private void returnResult(ParseResult parseResult) { + Intent result = new Intent(); + Bundle bundle = parseResult.toLoyaltyCardBundle(); if (addGroup != null) { - manualResultBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, addGroup); + bundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, addGroup); } - manualResult.putExtras(manualResultBundle); - ScanActivity.this.setResult(RESULT_OK, manualResult); + result.putExtras(bundle); + ScanActivity.this.setResult(RESULT_OK, result); finish(); } private void handleActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); - List barcodeValuesList = Utils.parseSetBarcodeActivityResult(requestCode, resultCode, intent, this); + List parseResultList = Utils.parseSetBarcodeActivityResult(requestCode, resultCode, intent, this); - if (barcodeValuesList.isEmpty()) { + if (parseResultList.isEmpty()) { setScannerActive(true); return; } - Utils.makeUserChooseBarcodeFromList(this, barcodeValuesList, new BarcodeValuesListDisambiguatorCallback() { + Utils.makeUserChooseParseResultFromList(this, parseResultList, new ParseResultListDisambiguatorCallback() { @Override - public void onUserChoseBarcode(BarcodeValues barcodeValues) { - CatimaBarcode barcodeType = barcodeValues.format(); - - returnResult(barcodeValues.content(), barcodeType != null ? barcodeType.name() : null); + public void onUserChoseParseResult(ParseResult parseResult) { + returnResult(parseResult); } @Override @@ -369,7 +360,9 @@ public class ScanActivity extends CatimaAppCompatActivity { // Buttons builder.setPositiveButton(getString(R.string.ok), (dialog, which) -> { - returnResult(input.getText().toString(), null); + LoyaltyCard loyaltyCard = new LoyaltyCard(); + loyaltyCard.setCardId(input.getText().toString()); + returnResult(new ParseResult(ParseResultType.BARCODE_ONLY, loyaltyCard)); }); builder.setNegativeButton(getString(R.string.cancel), (dialog, which) -> dialog.cancel()); AlertDialog dialog = builder.create(); @@ -418,7 +411,7 @@ public class ScanActivity extends CatimaAppCompatActivity { PermissionUtils.requestStorageReadPermission(this, PERMISSION_SCAN_ADD_FROM_IMAGE); } - public void addFromPdfFile() { + public void addFromPdf() { PermissionUtils.requestStorageReadPermission(this, PERMISSION_SCAN_ADD_FROM_PDF); } diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index 0ed45be3f..349174c0e 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -79,6 +79,7 @@ import java.util.Collections; import java.util.Currency; import java.util.Date; import java.util.GregorianCalendar; +import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -144,7 +145,7 @@ public class Utils { return ColorUtils.calculateLuminance(backgroundColor) > LUMINANCE_MIDPOINT; } - static public List retrieveBarcodesFromImage(Context context, Uri uri) { + static public List retrieveBarcodesFromImage(Context context, Uri uri) { Log.i(TAG, "Received image file with possible barcode"); if (uri == null) { @@ -163,7 +164,7 @@ public class Utils { return new ArrayList<>(); } - List barcodesFromBitmap = getBarcodesFromBitmap(bitmap); + List barcodesFromBitmap = getBarcodesFromBitmap(bitmap); if (barcodesFromBitmap.isEmpty()) { Log.i(TAG, "No barcode found in image file"); @@ -173,7 +174,7 @@ public class Utils { return barcodesFromBitmap; } - static public List retrieveBarcodesFromPdf(Context context, Uri uri) { + static public List retrieveBarcodesFromPdf(Context context, Uri uri) { Log.i(TAG, "Received PDF file with possible barcode"); if (uri == null) { Log.e(TAG, "Uri did not contain any data"); @@ -183,7 +184,7 @@ public class Utils { ParcelFileDescriptor parcelFileDescriptor = null; PdfRenderer renderer = null; - List barcodesFromPdfPages = new ArrayList<>(); + List barcodesFromPdfPages = new ArrayList<>(); try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r"); @@ -205,10 +206,10 @@ public class Utils { page.render(renderedPage, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); page.close(); - List barcodesFromPage = getBarcodesFromBitmap(renderedPage); - for (BarcodeValues barcodeValues : barcodesFromPage) { - barcodeValues.setNote(String.format(context.getString(R.string.pageWithNumber), i+1)); - barcodesFromPdfPages.add(barcodeValues); + List barcodesFromPage = getBarcodesFromBitmap(renderedPage); + for (ParseResult parseResult : barcodesFromPage) { + parseResult.setNote(String.format(context.getString(R.string.pageWithNumber), i+1)); + barcodesFromPdfPages.add(parseResult); } } } @@ -237,17 +238,17 @@ public class Utils { } /** - * Returns the Barcode format and content based on the result of an activity. - * It shows toasts to notify the end-user as needed itself and will return an empty - * BarcodeValues object if the activity was cancelled or nothing could be found. + * Returns the ParseResult based on the result of an activity. + * It shows toasts to notify the end-user as needed itself and will return an empty list if the + * activity was cancelled or nothing could be found. * * @param requestCode * @param resultCode * @param intent * @param context - * @return BarcodeValues + * @return List */ - static public List parseSetBarcodeActivityResult(int requestCode, int resultCode, Intent intent, Context context) { + static public List parseSetBarcodeActivityResult(int requestCode, int resultCode, Intent intent, Context context) { String contents; String format; @@ -276,7 +277,15 @@ public class Utils { Log.i(TAG, "Read barcode id: " + contents); Log.i(TAG, "Read format: " + format); - return Collections.singletonList(new BarcodeValues(format != null ? CatimaBarcode.fromName(format) : null, contents)); + LoyaltyCard loyaltyCard = new LoyaltyCard(); + if (format != null) { + loyaltyCard.setBarcodeType(CatimaBarcode.fromName(format)); + } + if (contents != null) { + loyaltyCard.setCardId(contents); + } + + return Collections.singletonList(new ParseResult(ParseResultType.BARCODE_ONLY, loyaltyCard)); } throw new UnsupportedOperationException("Unknown request code for parseSetBarcodeActivityResult"); @@ -296,7 +305,7 @@ public class Utils { return MediaStore.Images.Media.getBitmap(context.getContentResolver(), data); } - static public List getBarcodesFromBitmap(Bitmap bitmap) { + static public List getBarcodesFromBitmap(Bitmap bitmap) { // This function is vulnerable to OOM, so we try again with a smaller bitmap is we get OOM for (int i = 0; i < 10; i++) { try { @@ -311,7 +320,7 @@ public class Utils { return new ArrayList<>(); } - static private List getBarcodesFromBitmapReal(Bitmap bitmap) { + static private List getBarcodesFromBitmapReal(Bitmap bitmap) { // In order to decode it, the Bitmap must first be converted into a pixel array... int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()]; bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); @@ -320,7 +329,7 @@ public class Utils { LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); - List barcodeValuesList = new ArrayList<>(); + List parseResultList = new ArrayList<>(); try { MultiFormatReader multiFormatReader = new MultiFormatReader(); MultipleBarcodeReader multipleBarcodeReader = new GenericMultipleBarcodeReader(multiFormatReader); @@ -331,37 +340,42 @@ public class Utils { Log.i(TAG, "Read barcode id: " + barcodeResult.getText()); Log.i(TAG, "Read format: " + barcodeResult.getBarcodeFormat().name()); - barcodeValuesList.add(new BarcodeValues(CatimaBarcode.fromBarcode(barcodeResult.getBarcodeFormat()), barcodeResult.getText())); + LoyaltyCard loyaltyCard = new LoyaltyCard(); + loyaltyCard.setCardId(barcodeResult.getText()); + loyaltyCard.setBarcodeType(CatimaBarcode.fromBarcode(barcodeResult.getBarcodeFormat())); + parseResultList.add(new ParseResult(ParseResultType.BARCODE_ONLY, loyaltyCard)); } - return barcodeValuesList; + return parseResultList; } catch (NotFoundException e) { - return barcodeValuesList; + return parseResultList; } } - static public void makeUserChooseBarcodeFromList(Context context, List barcodeValuesList, BarcodeValuesListDisambiguatorCallback callback) { + static public void makeUserChooseParseResultFromList(Context context, List parseResultList, ParseResultListDisambiguatorCallback callback) { // If there is only one choice, consider it chosen - if (barcodeValuesList.size() == 1) { - callback.onUserChoseBarcode(barcodeValuesList.get(0)); + if (parseResultList.size() == 1) { + callback.onUserChoseParseResult(parseResultList.get(0)); return; } // Ask user to choose a barcode // TODO: This should contain an image of the barcode in question to help users understand the choice they're making - CharSequence[] barcodeDescriptions = new CharSequence[barcodeValuesList.size()]; - for (int i = 0; i < barcodeValuesList.size(); i++) { - BarcodeValues barcodeValues = barcodeValuesList.get(i); - CatimaBarcode catimaBarcode = barcodeValues.format(); + CharSequence[] barcodeDescriptions = new CharSequence[parseResultList.size()]; + for (int i = 0; i < parseResultList.size(); i++) { + ParseResult parseResult = parseResultList.get(i); + CatimaBarcode catimaBarcode = parseResult.getLoyaltyCard().barcodeType; - String barcodeContent = barcodeValues.content(); + String barcodeContent = parseResult.getLoyaltyCard().cardId; // Shorten overly long barcodes if (barcodeContent.length() > 22) { barcodeContent = barcodeContent.substring(0, 20) + "…"; } - if (barcodeValues.note() != null) { - barcodeDescriptions[i] = String.format("%s: %s (%s)", barcodeValues.note(), catimaBarcode != null ? catimaBarcode.prettyName() : context.getString(R.string.noBarcode), barcodeContent); + String parseResultNote = parseResult.getNote(); + + if (parseResultNote != null) { + barcodeDescriptions[i] = String.format("%s: %s (%s)", parseResultNote, catimaBarcode != null ? catimaBarcode.prettyName() : context.getString(R.string.noBarcode), barcodeContent); } else { barcodeDescriptions[i] = String.format("%s (%s)", catimaBarcode != null ? catimaBarcode.prettyName() : context.getString(R.string.noBarcode), barcodeContent); } @@ -371,7 +385,7 @@ public class Utils { builder.setTitle(context.getString(R.string.multipleBarcodesFoundPleaseChooseOne)); builder.setItems( barcodeDescriptions, - (dialogInterface, i) -> callback.onUserChoseBarcode(barcodeValuesList.get(i)) + (dialogInterface, i) -> callback.onUserChoseParseResult(parseResultList.get(i)) ); builder.setOnCancelListener(dialogInterface -> callback.onUserDismissedSelector()); builder.show(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 881eccf1f..fe94854b8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -360,6 +360,5 @@ Export cancelled Use front image Use back image - Select a Passbook file (.pkpass) This file is not supported diff --git a/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java b/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java index eb61e0316..cada3eba0 100644 --- a/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java +++ b/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java @@ -231,10 +231,14 @@ public class LoyaltyCardViewActivityTest { assertNotNull(bundle); Intent resultIntent = new Intent(intent); - Bundle resultBundle = new Bundle(); - resultBundle.putString(BarcodeSelectorActivity.BARCODE_CONTENTS, BARCODE_DATA); - resultBundle.putString(BarcodeSelectorActivity.BARCODE_FORMAT, BARCODE_TYPE.name()); - resultIntent.putExtras(resultBundle); + + LoyaltyCard loyaltyCard = new LoyaltyCard(); + loyaltyCard.setBarcodeId(null); + loyaltyCard.setBarcodeType(BARCODE_TYPE); + loyaltyCard.setCardId(BARCODE_DATA); + ParseResult parseResult = new ParseResult(ParseResultType.BARCODE_ONLY, loyaltyCard); + + resultIntent.putExtras(parseResult.toLoyaltyCardBundle()); // Respond to image capture, success shadowOf(activity).receiveResult( @@ -267,10 +271,14 @@ public class LoyaltyCardViewActivityTest { assertNotNull(bundle); Intent resultIntent = new Intent(intent); - Bundle resultBundle = new Bundle(); - resultBundle.putString(BarcodeSelectorActivity.BARCODE_FORMAT, barcodeType); - resultBundle.putString(BarcodeSelectorActivity.BARCODE_CONTENTS, barcodeData); - resultIntent.putExtras(resultBundle); + + LoyaltyCard loyaltyCard = new LoyaltyCard(); + loyaltyCard.setBarcodeId(null); + loyaltyCard.setBarcodeType(barcodeType != null ? CatimaBarcode.fromName(barcodeType) : null); + loyaltyCard.setCardId(barcodeData); + ParseResult parseResult = new ParseResult(ParseResultType.BARCODE_ONLY, loyaltyCard); + + resultIntent.putExtras(parseResult.toLoyaltyCardBundle()); // Respond to barcode selection, success shadowOf(activity).receiveResult(