Skip to content

Commit

Permalink
Prevent invalid values in BarcodeValues
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastProject committed Sep 3, 2024
1 parent 18f0db3 commit 94ab35d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/protect/card_locker/BarcodeValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

public class BarcodeValues {
@Nullable
private final String mFormat;
private final CatimaBarcode mFormat;
private final String mContent;
private String mNote;

public BarcodeValues(@Nullable String format, String content) {
public BarcodeValues(@Nullable CatimaBarcode format, String content) {
mFormat = format;
mContent = content;
}
Expand All @@ -17,7 +17,7 @@ public void setNote(String note) {
mNote = note;
}

public @Nullable String format() {
public @Nullable CatimaBarcode format() {
return mFormat;
}

Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/protect/card_locker/CatimaBarcode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package protect.card_locker;

import androidx.annotation.NonNull;

import com.google.zxing.BarcodeFormat;

import java.util.Arrays;
Expand Down Expand Up @@ -45,15 +47,15 @@ private CatimaBarcode(BarcodeFormat barcodeFormat) {
mBarcodeFormat = barcodeFormat;
}

public static CatimaBarcode fromBarcode(BarcodeFormat barcodeFormat) {
public static CatimaBarcode fromBarcode(@NonNull BarcodeFormat barcodeFormat) {
return new CatimaBarcode(barcodeFormat);
}

public static CatimaBarcode fromName(String name) {
public static CatimaBarcode fromName(@NonNull String name) {
return new CatimaBarcode(BarcodeFormat.valueOf(name));
}

public static CatimaBarcode fromPrettyName(String prettyName) {
public static CatimaBarcode fromPrettyName(@NonNull String prettyName) {
try {
return new CatimaBarcode(barcodeFormats.get(barcodePrettyNames.indexOf(prettyName)));
} catch (IndexOutOfBoundsException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,11 @@ public void onTabReselected(TabLayout.Tab tab) {
Utils.makeUserChooseBarcodeFromList(this, barcodeValuesList, new BarcodeValuesListDisambiguatorCallback() {
@Override
public void onUserChoseBarcode(BarcodeValues barcodeValues) {
CatimaBarcode barcodeType = barcodeValues.format();

Bundle bundle = new Bundle();
bundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID, barcodeValues.content());
bundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_TYPE, barcodeValues.format());
bundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_TYPE, barcodeType != null ? barcodeType.name() : null);
bundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_ID, "");
tempLoyaltyCard.updateFromBundle(bundle);
}
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/protect/card_locker/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,14 @@ private void processBarcodeValuesList(List<BarcodeValues> barcodeValuesList, Str
Utils.makeUserChooseBarcodeFromList(MainActivity.this, barcodeValuesList, new BarcodeValuesListDisambiguatorCallback() {
@Override
public void onUserChoseBarcode(BarcodeValues barcodeValues) {
CatimaBarcode barcodeType = barcodeValues.format();

Intent newIntent = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class);
Bundle newBundle = new Bundle();
newBundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_TYPE, barcodeValues.format());
newBundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID, barcodeValues.content());

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);
newBundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_ID, null);
if (group != null) {
newBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group);
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/protect/card_locker/ScanActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ private void handleActivityResult(int requestCode, int resultCode, Intent intent
Utils.makeUserChooseBarcodeFromList(this, barcodeValuesList, new BarcodeValuesListDisambiguatorCallback() {
@Override
public void onUserChoseBarcode(BarcodeValues barcodeValues) {
returnResult(barcodeValues.content(), barcodeValues.format());
CatimaBarcode barcodeType = barcodeValues.format();

returnResult(barcodeValues.content(), barcodeType != null ? barcodeType.name() : null);
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/protect/card_locker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ static public List<BarcodeValues> parseSetBarcodeActivityResult(int requestCode,
Log.i(TAG, "Read barcode id: " + contents);
Log.i(TAG, "Read format: " + format);

return Collections.singletonList(new BarcodeValues(format, contents));
return Collections.singletonList(new BarcodeValues(format != null ? CatimaBarcode.fromName(format) : null, contents));
}

throw new UnsupportedOperationException("Unknown request code for parseSetBarcodeActivityResult");
Expand Down Expand Up @@ -323,7 +323,7 @@ static private List<BarcodeValues> getBarcodesFromBitmapReal(Bitmap bitmap) {
Log.i(TAG, "Read barcode id: " + barcodeResult.getText());
Log.i(TAG, "Read format: " + barcodeResult.getBarcodeFormat().name());

barcodeValuesList.add(new BarcodeValues(barcodeResult.getBarcodeFormat().name(), barcodeResult.getText()));
barcodeValuesList.add(new BarcodeValues(CatimaBarcode.fromBarcode(barcodeResult.getBarcodeFormat()), barcodeResult.getText()));
}

return barcodeValuesList;
Expand All @@ -344,7 +344,7 @@ static public void makeUserChooseBarcodeFromList(Context context, List<BarcodeVa
CharSequence[] barcodeDescriptions = new CharSequence[barcodeValuesList.size()];
for (int i = 0; i < barcodeValuesList.size(); i++) {
BarcodeValues barcodeValues = barcodeValuesList.get(i);
CatimaBarcode catimaBarcode = CatimaBarcode.fromName(barcodeValues.format());
CatimaBarcode catimaBarcode = barcodeValues.format();

String barcodeContent = barcodeValues.content();
// Shorten overly long barcodes
Expand All @@ -353,9 +353,9 @@ static public void makeUserChooseBarcodeFromList(Context context, List<BarcodeVa
}

if (barcodeValues.note() != null) {
barcodeDescriptions[i] = String.format("%s: %s (%s)", barcodeValues.note(), catimaBarcode.prettyName(), barcodeContent);
barcodeDescriptions[i] = String.format("%s: %s (%s)", barcodeValues.note(), catimaBarcode != null ? catimaBarcode.prettyName() : context.getString(R.string.noBarcode), barcodeContent);
} else {
barcodeDescriptions[i] = String.format("%s (%s)", catimaBarcode.prettyName(), barcodeContent);
barcodeDescriptions[i] = String.format("%s (%s)", catimaBarcode != null ? catimaBarcode.prettyName() : context.getString(R.string.noBarcode), barcodeContent);
}
}

Expand Down

0 comments on commit 94ab35d

Please sign in to comment.