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 7, 2024
1 parent 7326b6c commit 4f7cb7d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 34 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
38 changes: 34 additions & 4 deletions app/src/main/java/protect/card_locker/LoyaltyCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public LoyaltyCard() {
setBarcodeType(null);
setHeaderColor(null);
setStarStatus(0);
setLastUsed(0);
setLastUsed(Utils.getUnixTime());
setZoomLevel(100);
setArchiveStatus(0);
}
Expand Down Expand Up @@ -229,67 +229,97 @@ public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(archiveStatus);
}

public static LoyaltyCard fromBundle(Bundle bundle) {
public static LoyaltyCard fromBundle(Bundle bundle, boolean requireFull) {
// Grab default card
LoyaltyCard loyaltyCard = new LoyaltyCard();

// Update from bundle
loyaltyCard.updateFromBundle(bundle);
loyaltyCard.updateFromBundle(bundle, requireFull);

// Return updated version
return loyaltyCard;
}

public void updateFromBundle(Bundle bundle) {
public void updateFromBundle(Bundle bundle, boolean requireFull) {
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_ID)) {
setId(bundle.getInt(BUNDLE_LOYALTY_CARD_ID));
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_ID);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_STORE)) {
setStore(Objects.requireNonNull(bundle.getString(BUNDLE_LOYALTY_CARD_STORE)));
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_STORE);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_NOTE)) {
setNote(bundle.getString(BUNDLE_LOYALTY_CARD_NOTE));
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_NOTE);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_VALID_FROM)) {
long tmpValidFrom = bundle.getLong(BUNDLE_LOYALTY_CARD_VALID_FROM, -1);
setValidFrom(tmpValidFrom != -1 ? new Date(tmpValidFrom) : null);
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_VALID_FROM);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_EXPIRY)) {
long tmpExpiry = bundle.getLong(BUNDLE_LOYALTY_CARD_EXPIRY, -1);
setExpiry(tmpExpiry != -1 ? new Date(tmpExpiry) : null);
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_EXPIRY);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_BALANCE)) {
setBalance(new BigDecimal(bundle.getString(BUNDLE_LOYALTY_CARD_BALANCE)));
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_BALANCE);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_BALANCE_TYPE)) {
String tmpBalanceType = bundle.getString(BUNDLE_LOYALTY_CARD_BALANCE_TYPE, null);
setBalanceType(tmpBalanceType != null ? Currency.getInstance(tmpBalanceType) : null);
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_BALANCE_TYPE);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_CARD_ID)) {
setCardId(Objects.requireNonNull(bundle.getString(BUNDLE_LOYALTY_CARD_CARD_ID)));
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_CARD_ID);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_BARCODE_ID)) {
setBarcodeId(bundle.getString(BUNDLE_LOYALTY_CARD_BARCODE_ID));
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_BARCODE_ID);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_BARCODE_TYPE)) {
String tmpBarcodeType = bundle.getString(BUNDLE_LOYALTY_CARD_BARCODE_TYPE, null);
setBarcodeType(tmpBarcodeType != null ? CatimaBarcode.fromName(tmpBarcodeType) : null);
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_BARCODE_TYPE);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_HEADER_COLOR)) {
int tmpHeaderColor = bundle.getInt(BUNDLE_LOYALTY_CARD_HEADER_COLOR, -1);
setHeaderColor(tmpHeaderColor != -1 ? tmpHeaderColor : null);
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_HEADER_COLOR);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_STAR_STATUS)) {
setStarStatus(bundle.getInt(BUNDLE_LOYALTY_CARD_STAR_STATUS));
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_STAR_STATUS);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_LAST_USED)) {
setLastUsed(bundle.getLong(BUNDLE_LOYALTY_CARD_LAST_USED));
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_LAST_USED);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_ZOOM_LEVEL)) {
setZoomLevel(bundle.getInt(BUNDLE_LOYALTY_CARD_ZOOM_LEVEL));
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_ZOOM_LEVEL);
}
if (bundle.containsKey(BUNDLE_LOYALTY_CARD_ARCHIVE_STATUS)) {
setArchiveStatus(bundle.getInt(BUNDLE_LOYALTY_CARD_ARCHIVE_STATUS));
} else if (requireFull) {
throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_ARCHIVE_STATUS);
}
}

Expand Down
18 changes: 2 additions & 16 deletions app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ private boolean extractIntentFields(Intent intent) {

// If the intent contains any loyalty card fields, override those fields in our current temp card
if (b != null) {
tempLoyaltyCard.updateFromBundle(b);
tempLoyaltyCard.updateFromBundle(b, false);
}

Log.d(TAG, "Edit activity: id=" + loyaltyCardId
Expand Down Expand Up @@ -704,19 +704,9 @@ public void onTabReselected(TabLayout.Tab tab) {
Utils.makeUserChooseBarcodeFromList(this, barcodeValuesList, new BarcodeValuesListDisambiguatorCallback() {
@Override
public void onUserChoseBarcode(BarcodeValues barcodeValues) {
<<<<<<< Updated upstream
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_ID, "");
tempLoyaltyCard.updateFromBundle(bundle);
=======
CatimaBarcode barcodeType = barcodeValues.format();

setLoyaltyCardCardId(barcodeValues.content());
setLoyaltyCardBarcodeType(barcodeType);
setLoyaltyCardBarcodeType(barcodeValues.format());
setLoyaltyCardBarcodeId("");
>>>>>>> Stashed changes
}

@Override
Expand Down Expand Up @@ -957,7 +947,6 @@ protected void onResume() {
}

// Fix up some fields
// TODO: Double check if necessary and if can't reflow differently
if (tempLoyaltyCard.barcodeType != null) {
try {
barcodeTypeField.setText(tempLoyaltyCard.barcodeType.prettyName());
Expand Down Expand Up @@ -998,9 +987,6 @@ protected void onResume() {

generateIcon(storeFieldEdit.getText().toString().trim());

// It can't be null because we set it in updateTempState but SpotBugs insists it can be
// NP_NULL_ON_SOME_PATH: Possible null pointer dereference and
// NP_NULL_PARAM_DEREF: Method call passes null for non-null parameter
Integer headerColor = tempLoyaltyCard.headerColor;
if (headerColor != null) {
thumbnail.setOnClickListener(new ChooseCardImage());
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 4f7cb7d

Please sign in to comment.