Refactor ScanActivity result code (use ParseResult)

This commit is contained in:
Sylvia van Os
2024-10-01 17:33:45 +02:00
parent ea456c6d80
commit 8eeff0058b
12 changed files with 197 additions and 165 deletions

View File

@@ -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; }
}

View File

@@ -1,6 +0,0 @@
package protect.card_locker;
public interface BarcodeValuesListDisambiguatorCallback {
void onUserChoseBarcode(BarcodeValues barcodeValues);
void onUserDismissedSelector();
}

View File

@@ -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<String> 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;
}

View File

@@ -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<BarcodeValues> 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;
}
});

View File

@@ -257,12 +257,9 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
return;
}
Intent intent = result.getData();
List<BarcodeValues> 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<BarcodeValues> barcodeValuesList, String group, boolean closeAppOnNoBarcode) {
if (barcodeValuesList.isEmpty()) {
throw new IllegalArgumentException("barcodesValues may not be empty");
private void processParseResultList(List<ParseResult> 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<BarcodeValues> barcodeValuesList;
List<ParseResult> 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);
}
}

View File

@@ -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
)
)
}
}
}
}

View File

@@ -0,0 +1,6 @@
package protect.card_locker;
public interface ParseResultListDisambiguatorCallback {
void onUserChoseParseResult(ParseResult parseResult);
void onUserDismissedSelector();
}

View File

@@ -0,0 +1,6 @@
package protect.card_locker
enum class ParseResultType {
FULL,
BARCODE_ONLY
}

View File

@@ -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<BarcodeValues> barcodeValuesList = Utils.parseSetBarcodeActivityResult(requestCode, resultCode, intent, this);
List<ParseResult> 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);
}

View File

@@ -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<BarcodeValues> retrieveBarcodesFromImage(Context context, Uri uri) {
static public List<ParseResult> 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<BarcodeValues> barcodesFromBitmap = getBarcodesFromBitmap(bitmap);
List<ParseResult> 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<BarcodeValues> retrieveBarcodesFromPdf(Context context, Uri uri) {
static public List<ParseResult> 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<BarcodeValues> barcodesFromPdfPages = new ArrayList<>();
List<ParseResult> 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<BarcodeValues> barcodesFromPage = getBarcodesFromBitmap(renderedPage);
for (BarcodeValues barcodeValues : barcodesFromPage) {
barcodeValues.setNote(String.format(context.getString(R.string.pageWithNumber), i+1));
barcodesFromPdfPages.add(barcodeValues);
List<ParseResult> 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<ParseResult>
*/
static public List<BarcodeValues> parseSetBarcodeActivityResult(int requestCode, int resultCode, Intent intent, Context context) {
static public List<ParseResult> 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<BarcodeValues> getBarcodesFromBitmap(Bitmap bitmap) {
static public List<ParseResult> 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<BarcodeValues> getBarcodesFromBitmapReal(Bitmap bitmap) {
static private List<ParseResult> 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<BarcodeValues> barcodeValuesList = new ArrayList<>();
List<ParseResult> 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<BarcodeValues> barcodeValuesList, BarcodeValuesListDisambiguatorCallback callback) {
static public void makeUserChooseParseResultFromList(Context context, List<ParseResult> 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();

View File

@@ -360,6 +360,5 @@
<string name="exportCancelled">Export cancelled</string>
<string name="useFrontImage">Use front image</string>
<string name="useBackImage">Use back image</string>
<string name="addFromPkpass">Select a Passbook file (.pkpass)</string>
<string name="unsupportedFile">This file is not supported</string>
</resources>

View File

@@ -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(