From 8519e12aa79387a9a7fd008221a01c0afcaaa9b5 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Sun, 24 Mar 2024 13:07:29 +0100 Subject: [PATCH] Add page number to barcode selector for PDF scan results --- .../protect/card_locker/BarcodeValues.java | 11 +++++--- .../main/java/protect/card_locker/Utils.java | 26 +++++++++++++++---- app/src/main/res/values/strings.xml | 3 ++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/protect/card_locker/BarcodeValues.java b/app/src/main/java/protect/card_locker/BarcodeValues.java index 681d1572f..e9b745fd5 100644 --- a/app/src/main/java/protect/card_locker/BarcodeValues.java +++ b/app/src/main/java/protect/card_locker/BarcodeValues.java @@ -3,12 +3,17 @@ package protect.card_locker; public class BarcodeValues { private final String mFormat; private final String mContent; + private String mNote; public BarcodeValues(String format, String content) { mFormat = format; mContent = content; } + public void setNote(String note) { + mNote = note; + } + public String format() { return mFormat; } @@ -17,7 +22,5 @@ public class BarcodeValues { return mContent; } - public boolean isEmpty() { - return mFormat == null && mContent == null; - } -} + public String note() { return mNote; } +} \ No newline at end of file diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index 7ebf43c3f..f3df29134 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -187,7 +187,7 @@ public class Utils { return new ArrayList<>(); } - // Loop over all pages to find a barcode + // Loop over all pages to find barcodes List barcodesFromPdfPages = new ArrayList<>(); Bitmap renderedPage; for (int i = 0; i < renderer.getPageCount(); i++) { @@ -196,7 +196,11 @@ public class Utils { page.render(renderedPage, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); page.close(); - barcodesFromPdfPages.addAll(getBarcodesFromBitmap(renderedPage)); + List barcodesFromPage = getBarcodesFromBitmap(renderedPage); + for (BarcodeValues barcodeValues : barcodesFromPage) { + barcodeValues.setNote(String.format(context.getString(R.string.pageWithNumber), i+1)); + barcodesFromPdfPages.add(barcodeValues); + } } renderer.close(); @@ -323,12 +327,24 @@ public class Utils { // 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++) { - CatimaBarcode catimaBarcode = CatimaBarcode.fromName(barcodeValuesList.get(i).format()); - barcodeDescriptions[i] = catimaBarcode.prettyName() + ": " + barcodeValuesList.get(i).content(); + BarcodeValues barcodeValues = barcodeValuesList.get(i); + CatimaBarcode catimaBarcode = CatimaBarcode.fromName(barcodeValues.format()); + + String barcodeContent = barcodeValues.content(); + // 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.prettyName(), barcodeContent); + } else { + barcodeDescriptions[i] = String.format("%s (%s)", catimaBarcode.prettyName(), barcodeContent); + } } MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context); - builder.setTitle(context.getString(R.string.multiple_barcodes_found_choose_one)); + builder.setTitle(context.getString(R.string.multipleBarcodesFoundPleaseChooseOne)); builder.setItems( barcodeDescriptions, (dialogInterface, i) -> callback.onUserChoseBarcode(barcodeValuesList.get(i)) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a67df074a..0c6473e25 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -344,5 +344,6 @@ Select a PDF file Could not read the file Could not find a supported file manager - Which of the found barcodes do you want to use? + Which of the found barcodes do you want to use? + Page %d