Skip to content

Commit

Permalink
Add page number to barcode selector for PDF scan results
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastProject committed Mar 24, 2024
1 parent 1429abd commit 8519e12
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
11 changes: 7 additions & 4 deletions app/src/main/java/protect/card_locker/BarcodeValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
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;
}
Expand All @@ -17,7 +22,5 @@ public String content() {
return mContent;
}

public boolean isEmpty() {
return mFormat == null && mContent == null;
}
}
public String note() { return mNote; }
}
26 changes: 21 additions & 5 deletions app/src/main/java/protect/card_locker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ static public List<BarcodeValues> retrieveBarcodesFromPdf(Context context, Uri u
return new ArrayList<>();
}

// Loop over all pages to find a barcode
// Loop over all pages to find barcodes
List<BarcodeValues> barcodesFromPdfPages = new ArrayList<>();
Bitmap renderedPage;
for (int i = 0; i < renderer.getPageCount(); i++) {
Expand All @@ -196,7 +196,11 @@ static public List<BarcodeValues> retrieveBarcodesFromPdf(Context context, Uri u
page.render(renderedPage, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
page.close();

barcodesFromPdfPages.addAll(getBarcodesFromBitmap(renderedPage));
List<BarcodeValues> barcodesFromPage = getBarcodesFromBitmap(renderedPage);
for (BarcodeValues barcodeValues : barcodesFromPage) {
barcodeValues.setNote(String.format(context.getString(R.string.pageWithNumber), i+1));
barcodesFromPdfPages.add(barcodeValues);
}
}
renderer.close();

Expand Down Expand Up @@ -323,12 +327,24 @@ static public void makeUserChooseBarcodeFromList(Context context, List<BarcodeVa
// 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))
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -344,5 +344,6 @@
<string name="addFromPdfFile">Select a PDF file</string>
<string name="errorReadingFile">Could not read the file</string>
<string name="failedLaunchingFileManager">Could not find a supported file manager</string>
<string name="multiple_barcodes_found_choose_one">Which of the found barcodes do you want to use?</string>
<string name="multipleBarcodesFoundPleaseChooseOne">Which of the found barcodes do you want to use?</string>
<string name="pageWithNumber">Page <xliff:g>%d</xliff:g></string>
</resources>

0 comments on commit 8519e12

Please sign in to comment.