diff --git a/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java b/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java index f51ef7e0ec..5d6806d8dc 100644 --- a/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java +++ b/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java @@ -71,7 +71,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { }); final Bundle b = getIntent().getExtras(); - final String initialCardId = b != null ? b.getString("initialCardId") : null; + final String initialCardId = b != null ? b.getString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID) : null; if (initialCardId != null) { cardId.setText(initialCardId); diff --git a/app/src/main/java/protect/card_locker/BarcodeValues.java b/app/src/main/java/protect/card_locker/BarcodeValues.java index e9b745fd54..567b6aab36 100644 --- a/app/src/main/java/protect/card_locker/BarcodeValues.java +++ b/app/src/main/java/protect/card_locker/BarcodeValues.java @@ -1,11 +1,14 @@ package protect.card_locker; +import androidx.annotation.Nullable; + public class BarcodeValues { - private final String mFormat; + @Nullable + private final CatimaBarcode mFormat; private final String mContent; private String mNote; - public BarcodeValues(String format, String content) { + public BarcodeValues(@Nullable CatimaBarcode format, String content) { mFormat = format; mContent = content; } @@ -14,7 +17,7 @@ public void setNote(String note) { mNote = note; } - public String format() { + public @Nullable CatimaBarcode format() { return mFormat; } diff --git a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java index 823acbeacc..14d8d7e85b 100644 --- a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java +++ b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java @@ -61,7 +61,7 @@ public void onCreate(Bundle bundle) { private void onClickAction(int position) { Cursor selected = DBHelper.getLoyaltyCardCursor(mDatabase, DBHelper.LoyaltyCardArchiveFilter.All); selected.moveToPosition(position); - LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(selected); + LoyaltyCard loyaltyCard = LoyaltyCard.fromCursor(selected); Log.d(TAG, "Creating shortcut for card " + loyaltyCard.store + "," + loyaltyCard.id); diff --git a/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java b/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java index ece22037c7..1e21fc6b82 100644 --- a/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java +++ b/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java @@ -42,10 +42,10 @@ public Flow.Publisher createPublisherForAllAvailable() { Cursor loyaltyCardCursor = DBHelper.getLoyaltyCardCursor(mDatabase, DBHelper.LoyaltyCardArchiveFilter.Unarchived); return subscriber -> { while (loyaltyCardCursor.moveToNext()) { - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(loyaltyCardCursor); + LoyaltyCard card = LoyaltyCard.fromCursor(loyaltyCardCursor); Intent openIntent = new Intent(this, LoyaltyCardViewActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .putExtra("id", card.id); + .putExtra(LoyaltyCardViewActivity.BUNDLE_ID, card.id); PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), card.id, openIntent, PendingIntent.FLAG_IMMUTABLE); subscriber.onNext( new Control.StatelessBuilder(PREFIX + card.id, pendingIntent) @@ -73,7 +73,7 @@ public Flow.Publisher createPublisherFor(@NonNull List controlI if (card != null) { Intent openIntent = new Intent(this, LoyaltyCardViewActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .putExtra("id", card.id); + .putExtra(LoyaltyCardViewActivity.BUNDLE_ID, card.id); PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), card.id, openIntent, PendingIntent.FLAG_IMMUTABLE); control = new Control.StatefulBuilder(controlId, pendingIntent) .setTitle(card.store) @@ -129,7 +129,7 @@ public void performControlAction(@NonNull String controlId, @NonNull ControlActi consumer.accept(ControlAction.RESPONSE_OK); Intent openIntent = new Intent(this, LoyaltyCardViewActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .putExtra("id", controlIdToCardId(controlId)); + .putExtra(LoyaltyCardViewActivity.BUNDLE_ID, controlIdToCardId(controlId)); startActivity(openIntent); closePowerScreenOnAndroid11(); diff --git a/app/src/main/java/protect/card_locker/CatimaBarcode.java b/app/src/main/java/protect/card_locker/CatimaBarcode.java index d1e74e8767..267b85acaa 100644 --- a/app/src/main/java/protect/card_locker/CatimaBarcode.java +++ b/app/src/main/java/protect/card_locker/CatimaBarcode.java @@ -1,5 +1,7 @@ package protect.card_locker; +import androidx.annotation.NonNull; + import com.google.zxing.BarcodeFormat; import java.util.Arrays; @@ -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) { diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 56b2c4a711..7fd52962d3 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -332,7 +332,7 @@ public static Set imageFiles(Context context, final SQLiteDatabase datab Set files = new HashSet<>(); Cursor cardCursor = getLoyaltyCardCursor(database); while (cardCursor.moveToNext()) { - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cardCursor); + LoyaltyCard card = LoyaltyCard.fromCursor(cardCursor); for (ImageLocationType imageLocationType : ImageLocationType.values()) { String name = Utils.getCardImageFileName(card.id, imageLocationType); if (Utils.retrieveCardImageAsFile(context, name).exists()) { @@ -542,7 +542,7 @@ public static LoyaltyCard getLoyaltyCard(SQLiteDatabase database, final int id) if (data.getCount() == 1) { data.moveToFirst(); - card = LoyaltyCard.toLoyaltyCard(data); + card = LoyaltyCard.fromCursor(data); } data.close(); diff --git a/app/src/main/java/protect/card_locker/LoyaltyCard.java b/app/src/main/java/protect/card_locker/LoyaltyCard.java index 95e68f0775..d4e3bf5317 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCard.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCard.java @@ -1,6 +1,7 @@ package protect.card_locker; import android.database.Cursor; +import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; @@ -10,76 +11,207 @@ import java.math.BigDecimal; import java.util.Currency; import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; public class LoyaltyCard implements Parcelable { - public final int id; - public final String store; - public final String note; + public int id; + public String store; + public String note; @Nullable - public final Date validFrom; + public Date validFrom; @Nullable - public final Date expiry; - public final BigDecimal balance; + public Date expiry; + public BigDecimal balance; @Nullable - public final Currency balanceType; - public final String cardId; + public Currency balanceType; + public String cardId; @Nullable - public final String barcodeId; + public String barcodeId; @Nullable - public final CatimaBarcode barcodeType; + public CatimaBarcode barcodeType; @Nullable - public final Integer headerColor; - public final int starStatus; - public final int archiveStatus; - public final long lastUsed; + public Integer headerColor; + public int starStatus; + public long lastUsed; public int zoomLevel; + public int archiveStatus; + public static final String BUNDLE_LOYALTY_CARD_ID = "loyaltyCardId"; + public static final String BUNDLE_LOYALTY_CARD_STORE = "loyaltyCardStore"; + public static final String BUNDLE_LOYALTY_CARD_NOTE = "loyaltyCardNote"; + public static final String BUNDLE_LOYALTY_CARD_VALID_FROM = "loyaltyCardValidFrom"; + public static final String BUNDLE_LOYALTY_CARD_EXPIRY = "loyaltyCardExpiry"; + public static final String BUNDLE_LOYALTY_CARD_BALANCE = "loyaltyCardBalance"; + public static final String BUNDLE_LOYALTY_CARD_BALANCE_TYPE = "loyaltyCardBalanceType"; + public static final String BUNDLE_LOYALTY_CARD_CARD_ID = "loyaltyCardCardId"; + public static final String BUNDLE_LOYALTY_CARD_BARCODE_ID = "loyaltyCardBarcodeId"; + public static final String BUNDLE_LOYALTY_CARD_BARCODE_TYPE = "loyaltyCardBarcodeType"; + public static final String BUNDLE_LOYALTY_CARD_HEADER_COLOR = "loyaltyCardHeaderColor"; + public static final String BUNDLE_LOYALTY_CARD_STAR_STATUS = "loyaltyCardStarStatus"; + public static final String BUNDLE_LOYALTY_CARD_LAST_USED = "loyaltyCardLastUsed"; + public static final String BUNDLE_LOYALTY_CARD_ZOOM_LEVEL = "loyaltyCardZoomLevel"; + public static final String BUNDLE_LOYALTY_CARD_ARCHIVE_STATUS = "loyaltyCardArchiveStatus"; + + /** + * Create a loyalty card object with default values + */ + public LoyaltyCard() { + setId(-1); + setStore(""); + setNote(""); + setValidFrom(null); + setExpiry(null); + setBalance(new BigDecimal("0")); + setBalanceType(null); + setCardId(""); + setBarcodeId(null); + setBarcodeType(null); + setHeaderColor(null); + setStarStatus(0); + setLastUsed(Utils.getUnixTime()); + setZoomLevel(100); + setArchiveStatus(0); + } + + /** + * Create a new loyalty card + * + * @param id + * @param store + * @param note + * @param validFrom + * @param expiry + * @param balance + * @param balanceType + * @param cardId + * @param barcodeId + * @param barcodeType + * @param headerColor + * @param starStatus + * @param lastUsed + * @param zoomLevel + * @param archiveStatus + */ public LoyaltyCard(final int id, final String store, final String note, @Nullable final Date validFrom, @Nullable final Date expiry, final BigDecimal balance, @Nullable final Currency balanceType, final String cardId, @Nullable final String barcodeId, @Nullable final CatimaBarcode barcodeType, @Nullable final Integer headerColor, final int starStatus, final long lastUsed, final int zoomLevel, final int archiveStatus) { + setId(id); + setStore(store); + setNote(note); + setValidFrom(validFrom); + setExpiry(expiry); + setBalance(balance); + setBalanceType(balanceType); + setCardId(cardId); + setBarcodeId(barcodeId); + setBarcodeType(barcodeType); + setHeaderColor(headerColor); + setStarStatus(starStatus); + setLastUsed(lastUsed); + setZoomLevel(zoomLevel); + setArchiveStatus(archiveStatus); + } + + public void setId(int id) { this.id = id; + } + + public void setStore(@NonNull String store) { this.store = store; + } + + public void setNote(@NonNull String note) { this.note = note; + } + + public void setValidFrom(@Nullable Date validFrom) { this.validFrom = validFrom; + } + + public void setExpiry(@Nullable Date expiry) { this.expiry = expiry; + } + + public void setBalance(@NonNull BigDecimal balance) { this.balance = balance; + } + + public void setBalanceType(@Nullable Currency balanceType) { this.balanceType = balanceType; + } + + public void setCardId(@NonNull String cardId) { this.cardId = cardId; + } + + public void setBarcodeId(@Nullable String barcodeId) { this.barcodeId = barcodeId; + } + + public void setBarcodeType(@Nullable CatimaBarcode barcodeType) { this.barcodeType = barcodeType; + } + + public void setHeaderColor(@Nullable Integer headerColor) { this.headerColor = headerColor; + } + + public void setStarStatus(int starStatus) { + if (starStatus != 0 && starStatus != 1) { + throw new IllegalArgumentException("starStatus must be 0 or 1"); + } + this.starStatus = starStatus; + } + + public void setLastUsed(long lastUsed) { this.lastUsed = lastUsed; + } + + public void setZoomLevel(int zoomLevel) { + if (zoomLevel < 0 || zoomLevel > 100) { + throw new IllegalArgumentException("zoomLevel must be in range 0-100"); + } + this.zoomLevel = zoomLevel; + } + + public void setArchiveStatus(int archiveStatus) { + if (archiveStatus != 0 && archiveStatus != 1) { + throw new IllegalArgumentException("archiveStatus must be 0 or 1"); + } + this.archiveStatus = archiveStatus; } protected LoyaltyCard(Parcel in) { - id = in.readInt(); - store = in.readString(); - note = in.readString(); + setId(in.readInt()); + setStore(Objects.requireNonNull(in.readString())); + setNote(Objects.requireNonNull(in.readString())); long tmpValidFrom = in.readLong(); - validFrom = tmpValidFrom != -1 ? new Date(tmpValidFrom) : null; + setValidFrom(tmpValidFrom > 0 ? new Date(tmpValidFrom) : null); long tmpExpiry = in.readLong(); - expiry = tmpExpiry != -1 ? new Date(tmpExpiry) : null; - balance = (BigDecimal) in.readValue(BigDecimal.class.getClassLoader()); - balanceType = (Currency) in.readValue(Currency.class.getClassLoader()); - cardId = in.readString(); - barcodeId = in.readString(); + setExpiry(tmpExpiry > 0 ? new Date(tmpExpiry) : null); + setBalance((BigDecimal) in.readValue(BigDecimal.class.getClassLoader())); + setBalanceType((Currency) in.readValue(Currency.class.getClassLoader())); + setCardId(Objects.requireNonNull(in.readString())); + setBarcodeId(in.readString()); String tmpBarcodeType = in.readString(); - barcodeType = !tmpBarcodeType.isEmpty() ? CatimaBarcode.fromName(tmpBarcodeType) : null; + setBarcodeType((tmpBarcodeType != null && !tmpBarcodeType.isEmpty()) ? CatimaBarcode.fromName(tmpBarcodeType) : null); int tmpHeaderColor = in.readInt(); - headerColor = tmpHeaderColor != -1 ? tmpHeaderColor : null; - starStatus = in.readInt(); - lastUsed = in.readLong(); - zoomLevel = in.readInt(); - archiveStatus = in.readInt(); + setHeaderColor(tmpHeaderColor != -1 ? tmpHeaderColor : null); + setStarStatus(in.readInt()); + setLastUsed(in.readLong()); + setZoomLevel(in.readInt()); + setArchiveStatus(in.readInt()); } @Override - public void writeToParcel(Parcel parcel, int i) { + public void writeToParcel(Parcel parcel, int flags) { parcel.writeInt(id); parcel.writeString(store); parcel.writeString(note); @@ -97,51 +229,171 @@ public void writeToParcel(Parcel parcel, int i) { parcel.writeInt(archiveStatus); } - public static LoyaltyCard toLoyaltyCard(Cursor cursor) { - int id = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ID)); - String store = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.STORE)); - String note = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.NOTE)); - long validFromLong = cursor.getLong(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.VALID_FROM)); - long expiryLong = cursor.getLong(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.EXPIRY)); - BigDecimal balance = new BigDecimal(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BALANCE))); - String cardId = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.CARD_ID)); - String barcodeId = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BARCODE_ID)); - int starred = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.STAR_STATUS)); - long lastUsed = cursor.getLong(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.LAST_USED)); - int zoomLevel = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ZOOM_LEVEL)); - int archived = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ARCHIVE_STATUS)); + public static LoyaltyCard fromBundle(Bundle bundle, boolean requireFull) { + // Grab default card + LoyaltyCard loyaltyCard = new LoyaltyCard(); - int barcodeTypeColumn = cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BARCODE_TYPE); - int balanceTypeColumn = cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BALANCE_TYPE); - int headerColorColumn = cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.HEADER_COLOR); + // Update from bundle + loyaltyCard.updateFromBundle(bundle, requireFull); - CatimaBarcode barcodeType = null; - Currency balanceType = null; - Date validFrom = null; - Date expiry = null; - Integer headerColor = null; + // Return updated version + return loyaltyCard; + } - if (cursor.isNull(barcodeTypeColumn) == false) { - barcodeType = CatimaBarcode.fromName(cursor.getString(barcodeTypeColumn)); + 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 (cursor.isNull(balanceTypeColumn) == false) { - balanceType = Currency.getInstance(cursor.getString(balanceTypeColumn)); + 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 (validFromLong > 0) { - validFrom = new Date(validFromLong); + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_NOTE)) { + setNote(Objects.requireNonNull(bundle.getString(BUNDLE_LOYALTY_CARD_NOTE))); + } else if (requireFull) { + throw new IllegalArgumentException("Missing key " + BUNDLE_LOYALTY_CARD_NOTE); } - - if (expiryLong > 0) { - expiry = new Date(expiryLong); + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_VALID_FROM)) { + long tmpValidFrom = bundle.getLong(BUNDLE_LOYALTY_CARD_VALID_FROM); + setValidFrom(tmpValidFrom > 0 ? 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); + setExpiry(tmpExpiry > 0 ? 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); + 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); + 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); + 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); } + } + + public Bundle toBundle() { + Bundle bundle = new Bundle(); - if (cursor.isNull(headerColorColumn) == false) { - headerColor = cursor.getInt(headerColorColumn); + 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 (expiry != null) { + bundle.putLong(BUNDLE_LOYALTY_CARD_EXPIRY, expiry.getTime()); + } + bundle.putString(BUNDLE_LOYALTY_CARD_BALANCE, balance.toString()); + if (balanceType != null) { + bundle.putString(BUNDLE_LOYALTY_CARD_BALANCE_TYPE, balanceType.toString()); + } + 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 (headerColor != null) { + bundle.putInt(BUNDLE_LOYALTY_CARD_HEADER_COLOR, headerColor); } + 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; + } + + public static LoyaltyCard fromCursor(Cursor cursor) { + // id + int id = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ID)); + // store + String store = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.STORE)); + // note + String note = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.NOTE)); + // validFrom + long validFromLong = cursor.getLong(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.VALID_FROM)); + Date validFrom = validFromLong > 0 ? new Date(validFromLong) : null; + // expiry + long expiryLong = cursor.getLong(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.EXPIRY)); + Date expiry = expiryLong > 0 ? new Date(expiryLong) : null; + // balance + BigDecimal balance = new BigDecimal(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BALANCE))); + // balanceType + int balanceTypeColumn = cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BALANCE_TYPE); + Currency balanceType = !cursor.isNull(balanceTypeColumn) ? Currency.getInstance(cursor.getString(balanceTypeColumn)) : null; + // cardId + String cardId = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.CARD_ID)); + // barcodeId + int barcodeIdColumn = cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BARCODE_ID); + String barcodeId = !cursor.isNull(barcodeIdColumn) ? cursor.getString(barcodeIdColumn) : null; + // barcodeType + int barcodeTypeColumn = cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BARCODE_TYPE); + CatimaBarcode barcodeType = !cursor.isNull(barcodeTypeColumn) ? CatimaBarcode.fromName(cursor.getString(barcodeTypeColumn)) : null; + // headerColor + int headerColorColumn = cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.HEADER_COLOR); + Integer headerColor = !cursor.isNull(headerColorColumn) ? cursor.getInt(headerColorColumn) : null; + // starStatus + int starStatus = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.STAR_STATUS)); + // lastUsed + long lastUsed = cursor.getLong(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.LAST_USED)); + // zoomLevel + int zoomLevel = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ZOOM_LEVEL)); + // archiveStatus + int archiveStatus = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ARCHIVE_STATUS)); - return new LoyaltyCard(id, store, note, validFrom, expiry, balance, balanceType, cardId, barcodeId, barcodeType, headerColor, starred, lastUsed, zoomLevel, archived); + return new LoyaltyCard(id, store, note, validFrom, expiry, balance, balanceType, cardId, barcodeId, barcodeType, headerColor, starStatus, lastUsed, zoomLevel, archiveStatus); } public static boolean isDuplicate(final LoyaltyCard a, final LoyaltyCard b) { diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java b/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java index 6b807b7ecc..5ecb89d012 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java @@ -79,7 +79,7 @@ public LoyaltyCardListItemViewHolder onCreateViewHolder(@NonNull ViewGroup input public LoyaltyCard getCard(int position) { mCursor.moveToPosition(position); - return LoyaltyCard.toLoyaltyCard(mCursor); + return LoyaltyCard.fromCursor(mCursor); } public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor inputCursor) { @@ -87,7 +87,7 @@ public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor i boolean showDivider = false; inputHolder.mDivider.setVisibility(View.GONE); - LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(inputCursor); + LoyaltyCard loyaltyCard = LoyaltyCard.fromCursor(inputCursor); Bitmap icon = Utils.retrieveCardImage(mContext, loyaltyCard.id, ImageLocationType.icon); if (mLoyaltyCardListDisplayOptions.showingNameBelowThumbnail() && icon != null) { @@ -192,7 +192,7 @@ public ArrayList getSelectedItems() { int i; for (i = 0; i < mSelectedItems.size(); i++) { mCursor.moveToPosition(mSelectedItems.keyAt(i)); - result.add(LoyaltyCard.toLoyaltyCard(mCursor)); + result.add(LoyaltyCard.fromCursor(mCursor)); } return result; diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index bd12131209..fdafa2963c 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -127,9 +127,6 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements public static final String BUNDLE_DUPLICATE_ID = "duplicateId"; public static final String BUNDLE_UPDATE = "update"; public static final String BUNDLE_OPEN_SET_ICON_MENU = "openSetIconMenu"; - public static final String BUNDLE_CARDID = "cardId"; - public static final String BUNDLE_BARCODEID = "barcodeId"; - public static final String BUNDLE_BARCODETYPE = "barcodeType"; public static final String BUNDLE_ADDGROUP = "addGroup"; TabLayout tabs; @@ -162,15 +159,11 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements boolean updateLoyaltyCard; boolean duplicateFromLoyaltyCardId; boolean openSetIconMenu; - String cardId; - String barcodeId; - String barcodeType; String addGroup; Uri importLoyaltyCardUri = null; SQLiteDatabase mDatabase; - ImportURIHelper importUriHelper; boolean hasChanged = false; String tempStoredOldBarcodeValue = null; @@ -183,7 +176,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements HashMap currencies = new HashMap<>(); HashMap currencySymbols = new HashMap<>(); - LoyaltyCard tempLoyaltyCard; + LoyaltyCard tempLoyaltyCard = new LoyaltyCard(); LoyaltyCardField tempLoyaltyCardField; ActivityResultLauncher mPhotoTakerLauncher; @@ -215,53 +208,112 @@ protected void attachBaseContext(Context base) { super.attachBaseContext(base); } - private static LoyaltyCard updateTempState(LoyaltyCard loyaltyCard, LoyaltyCardField fieldName, Object value) { - return new LoyaltyCard( - (int) (fieldName == LoyaltyCardField.id ? value : loyaltyCard.id), - (String) (fieldName == LoyaltyCardField.store ? value : loyaltyCard.store), - (String) (fieldName == LoyaltyCardField.note ? value : loyaltyCard.note), - (Date) (fieldName == LoyaltyCardField.validFrom ? value : loyaltyCard.validFrom), - (Date) (fieldName == LoyaltyCardField.expiry ? value : loyaltyCard.expiry), - (BigDecimal) (fieldName == LoyaltyCardField.balance ? value : loyaltyCard.balance), - (Currency) (fieldName == LoyaltyCardField.balanceType ? value : loyaltyCard.balanceType), - (String) (fieldName == LoyaltyCardField.cardId ? value : loyaltyCard.cardId), - (String) (fieldName == LoyaltyCardField.barcodeId ? value : loyaltyCard.barcodeId), - (CatimaBarcode) (fieldName == LoyaltyCardField.barcodeType ? value : loyaltyCard.barcodeType), - (Integer) (fieldName == LoyaltyCardField.headerColor ? value : loyaltyCard.headerColor), - (int) (fieldName == LoyaltyCardField.starStatus ? value : loyaltyCard.starStatus), - 0, // Unimportant, always set to null in doSave so the DB updates it to the current timestamp - 100, // Unimportant, not updated in doSave, defaults to 100 for new cards - (int) (fieldName == LoyaltyCardField.archiveStatus ? value : loyaltyCard.archiveStatus) - ); + protected void setLoyaltyCardStore(@NonNull String store) { + tempLoyaltyCard.setStore(store); + + hasChanged = true; } - protected void updateTempState(LoyaltyCardField fieldName, Object value) { - tempLoyaltyCard = updateTempState(tempLoyaltyCard, fieldName, value); + protected void setLoyaltyCardNote(@NonNull String note) { + tempLoyaltyCard.setNote(note); - if (initDone && (fieldName == LoyaltyCardField.cardId || fieldName == LoyaltyCardField.barcodeId || fieldName == LoyaltyCardField.barcodeType)) { - generateBarcode(); - } + hasChanged = true; + } + + protected void setLoyaltyCardValidFrom(@Nullable Date validFrom) { + tempLoyaltyCard.setValidFrom(validFrom); + + hasChanged = true; + } + + protected void setLoyaltyCardExpiry(@Nullable Date expiry) { + tempLoyaltyCard.setExpiry(expiry); + + hasChanged = true; + } + + protected void setLoyaltyCardBalance(@NonNull BigDecimal balance) { + tempLoyaltyCard.setBalance(balance); + + hasChanged = true; + } + + protected void setLoyaltyCardBalanceType(@Nullable Currency balanceType) { + tempLoyaltyCard.setBalanceType(balanceType); + + hasChanged = true; + } + + protected void setLoyaltyCardCardId(@NonNull String cardId) { + tempLoyaltyCard.setCardId(cardId); + + generateBarcode(); + + hasChanged = true; + } + + protected void setLoyaltyCardBarcodeId(@Nullable String barcodeId) { + tempLoyaltyCard.setBarcodeId(barcodeId); + + generateBarcode(); + + hasChanged = true; + } + + protected void setLoyaltyCardBarcodeType(@Nullable CatimaBarcode barcodeType) { + tempLoyaltyCard.setBarcodeType(barcodeType); + + generateBarcode(); + + hasChanged = true; + } + + protected void setLoyaltyCardHeaderColor(@Nullable Integer headerColor) { + tempLoyaltyCard.setHeaderColor(headerColor); hasChanged = true; } - private void extractIntentFields(Intent intent) { + /* Extract intent fields and return if code should keep running */ + private boolean extractIntentFields(Intent intent) { final Bundle b = intent.getExtras(); + + addGroup = b != null ? b.getString(BUNDLE_ADDGROUP) : null; + openSetIconMenu = b != null && b.getBoolean(BUNDLE_OPEN_SET_ICON_MENU, false); + loyaltyCardId = b != null ? b.getInt(BUNDLE_ID) : 0; updateLoyaltyCard = b != null && b.getBoolean(BUNDLE_UPDATE, false); duplicateFromLoyaltyCardId = b != null && b.getBoolean(BUNDLE_DUPLICATE_ID, false); + importLoyaltyCardUri = intent.getData(); - openSetIconMenu = b != null && b.getBoolean(BUNDLE_OPEN_SET_ICON_MENU, false); - - cardId = b != null ? b.getString(BUNDLE_CARDID) : null; - barcodeId = b != null ? b.getString(BUNDLE_BARCODEID) : null; - barcodeType = b != null ? b.getString(BUNDLE_BARCODETYPE) : null; - addGroup = b != null ? b.getString(BUNDLE_ADDGROUP) : null; + // If we have to import a loyalty card, do so + if (updateLoyaltyCard || duplicateFromLoyaltyCardId) { + tempLoyaltyCard = DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId); + if (tempLoyaltyCard == null) { + Log.w(TAG, "Could not lookup loyalty card " + loyaltyCardId); + Toast.makeText(this, R.string.noCardExistsError, Toast.LENGTH_LONG).show(); + finish(); + return false; + } + } else if (importLoyaltyCardUri != null) { + try { + tempLoyaltyCard = new ImportURIHelper(this).parse(importLoyaltyCardUri); + } catch (InvalidObjectException ex) { + Toast.makeText(this, R.string.failedParsingImportUriError, Toast.LENGTH_LONG).show(); + finish(); + return false; + } + } - importLoyaltyCardUri = intent.getData(); + // If the intent contains any loyalty card fields, override those fields in our current temp card + if (b != null) { + tempLoyaltyCard.updateFromBundle(b, false); + } Log.d(TAG, "Edit activity: id=" + loyaltyCardId + ", updateLoyaltyCard=" + updateLoyaltyCard); + + return true; } @Override @@ -333,9 +385,9 @@ protected void onCreate(Bundle savedInstanceState) { mDatabase = new DBHelper(this).getWritableDatabase(); - extractIntentFields(getIntent()); - - importUriHelper = new ImportURIHelper(this); + if (!extractIntentFields(getIntent())) { + return; + } for (Currency currency : Currency.getAvailableCurrencies()) { currencies.put(currency.getSymbol(), currency); @@ -370,7 +422,7 @@ protected void onCreate(Bundle savedInstanceState) { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String storeName = s.toString().trim(); - updateTempState(LoyaltyCardField.store, storeName); + setLoyaltyCardStore(storeName); generateIcon(storeName); if (storeName.length() == 0) { @@ -384,7 +436,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { noteFieldEdit.addTextChangedListener(new SimpleTextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - updateTempState(LoyaltyCardField.note, s.toString()); + setLoyaltyCardNote(s.toString()); } }); @@ -397,7 +449,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { balanceField.setOnFocusChangeListener((v, hasFocus) -> { if (!hasFocus && !onResuming && !onRestoring) { if (balanceField.getText().toString().isEmpty()) { - updateTempState(LoyaltyCardField.balance, BigDecimal.valueOf(0)); + setLoyaltyCardBalance(BigDecimal.valueOf(0)); } balanceField.setText(Utils.formatBalanceWithoutCurrencySymbol(tempLoyaltyCard.balance, tempLoyaltyCard.balanceType)); @@ -410,7 +462,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { if (onResuming || onRestoring) return; try { BigDecimal balance = Utils.parseBalance(s.toString(), tempLoyaltyCard.balanceType); - updateTempState(LoyaltyCardField.balance, balance); + setLoyaltyCardBalance(balance); balanceField.setError(null); validBalance = true; } catch (ParseException e) { @@ -432,7 +484,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { currency = currencies.get(s.toString()); } - updateTempState(LoyaltyCardField.balanceType, currency); + setLoyaltyCardBalanceType(currency); if (tempLoyaltyCard.balance != null && !onResuming && !onRestoring) { balanceField.setText(Utils.formatBalanceWithoutCurrencySymbol(tempLoyaltyCard.balance, currency)); @@ -491,7 +543,7 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - updateTempState(LoyaltyCardField.cardId, s.toString()); + setLoyaltyCardCardId(s.toString()); if (s.length() == 0) { cardIdFieldView.setError(getString(R.string.field_must_not_be_empty)); @@ -516,7 +568,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { // request to update it to match the card id (if changed) tempStoredOldBarcodeValue = null; - updateTempState(LoyaltyCardField.barcodeId, null); + setLoyaltyCardBarcodeId(null); } else if (s.toString().equals(getString(R.string.setBarcodeId))) { if (!lastValue.toString().equals(getString(R.string.setBarcodeId))) { barcodeIdField.setText(lastValue); @@ -554,7 +606,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); input.requestFocus(); } else { - updateTempState(LoyaltyCardField.barcodeId, s.toString()); + setLoyaltyCardBarcodeId(s.toString()); } } @@ -573,12 +625,12 @@ public void afterTextChanged(Editable s) { public void onTextChanged(CharSequence s, int start, int before, int count) { if (!s.toString().isEmpty()) { if (s.toString().equals(getString(R.string.noBarcode))) { - updateTempState(LoyaltyCardField.barcodeType, null); + setLoyaltyCardBarcodeType(null); } else { try { CatimaBarcode barcodeFormat = CatimaBarcode.fromPrettyName(s.toString()); - updateTempState(LoyaltyCardField.barcodeType, barcodeFormat); + setLoyaltyCardBarcodeType(barcodeFormat); if (!barcodeFormat.isSupported()) { Toast.makeText(LoyaltyCardEditActivity.this, getString(R.string.unsupportedBarcodeType), Toast.LENGTH_LONG).show(); @@ -652,9 +704,9 @@ public void onTabReselected(TabLayout.Tab tab) { Utils.makeUserChooseBarcodeFromList(this, barcodeValuesList, new BarcodeValuesListDisambiguatorCallback() { @Override public void onUserChoseBarcode(BarcodeValues barcodeValues) { - cardId = barcodeValues.content(); - barcodeType = barcodeValues.format(); - barcodeId = ""; + setLoyaltyCardCardId(barcodeValues.content()); + setLoyaltyCardBarcodeType(barcodeValues.format()); + setLoyaltyCardBarcodeId(""); } @Override @@ -796,30 +848,6 @@ protected void onResume() { onResuming = true; - if (tempLoyaltyCard == null) { - if (updateLoyaltyCard || duplicateFromLoyaltyCardId) { - tempLoyaltyCard = DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId); - if (tempLoyaltyCard == null) { - Log.w(TAG, "Could not lookup loyalty card " + loyaltyCardId); - Toast.makeText(this, R.string.noCardExistsError, Toast.LENGTH_LONG).show(); - finish(); - return; - } - } else if (importLoyaltyCardUri != null) { - try { - tempLoyaltyCard = importUriHelper.parse(importLoyaltyCardUri); - } catch (InvalidObjectException ex) { - Toast.makeText(this, R.string.failedParsingImportUriError, Toast.LENGTH_LONG).show(); - finish(); - return; - } - } else { - // New card, use default values - tempLoyaltyCard = new LoyaltyCard(-1, "", "", null, null, new BigDecimal("0"), null, "", null, null, null, 0, Utils.getUnixTime(), 100,0); - - } - } - if (!initDone) { if (updateLoyaltyCard) { setTitle(R.string.editCardTitle); @@ -868,7 +896,7 @@ protected void onResume() { // which can cause issues when switching locale because it parses the balance and e.g. the decimal separator may have changed. formatBalanceCurrencyField(tempLoyaltyCard.balanceType); BigDecimal balance = tempLoyaltyCard.balance == null ? new BigDecimal("0") : tempLoyaltyCard.balance; - tempLoyaltyCard = updateTempState(tempLoyaltyCard, LoyaltyCardField.balance, balance); + setLoyaltyCardBalance(balance); balanceField.setText(Utils.formatBalanceWithoutCurrencySymbol(tempLoyaltyCard.balance, tempLoyaltyCard.balanceType)); validBalance = true; Log.d(TAG, "Setting balance to " + balance); @@ -915,35 +943,30 @@ protected void onResume() { if (tempLoyaltyCard.headerColor == null) { // If name is set, pick colour relevant for name. Otherwise pick randomly - updateTempState(LoyaltyCardField.headerColor, tempLoyaltyCard.store.isEmpty() ? Utils.getRandomHeaderColor(this) : Utils.getHeaderColor(this, tempLoyaltyCard)); + setLoyaltyCardHeaderColor(tempLoyaltyCard.store.isEmpty() ? Utils.getRandomHeaderColor(this) : Utils.getHeaderColor(this, tempLoyaltyCard)); } - // Update from intent - if (barcodeType != null) { + // Fix up some fields + if (tempLoyaltyCard.barcodeType != null) { try { - barcodeTypeField.setText(CatimaBarcode.fromName(barcodeType).prettyName()); + barcodeTypeField.setText(tempLoyaltyCard.barcodeType.prettyName()); } catch (IllegalArgumentException e) { barcodeTypeField.setText(getString(R.string.noBarcode)); } } - if (cardId != null) { - cardIdFieldView.setText(cardId); + if (tempLoyaltyCard.cardId != null) { + cardIdFieldView.setText(tempLoyaltyCard.cardId); } - if (barcodeId != null) { - if (!barcodeId.isEmpty()) { - barcodeIdField.setText(barcodeId); + if (tempLoyaltyCard.barcodeId != null) { + if (!tempLoyaltyCard.barcodeId.isEmpty()) { + barcodeIdField.setText(tempLoyaltyCard.barcodeId); } else { barcodeIdField.setText(getString(R.string.sameAsCardId)); } } - // Empty intent values - barcodeType = null; - cardId = null; - barcodeId = null; - // Initialization has finished if (!initDone) { initDone = true; @@ -964,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()); @@ -990,7 +1010,7 @@ protected void setColorFromIcon() { if (icon != null && (icon instanceof Bitmap)) { int headerColor = Utils.getHeaderColorFromImage((Bitmap) icon, Utils.getHeaderColor(this, tempLoyaltyCard)); - updateTempState(LoyaltyCardField.headerColor, headerColor); + setLoyaltyCardHeaderColor(headerColor); thumbnailEditIcon.setBackgroundColor(Utils.needsDarkForeground(headerColor) ? Color.BLACK : Color.WHITE); thumbnailEditIcon.setColorFilter(Utils.needsDarkForeground(headerColor) ? Color.WHITE : Color.BLACK); @@ -1038,7 +1058,16 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.toString().equals(getString(defaultOptionStringId))) { dateField.setTag(null); - updateTempState(loyaltyCardField, null); + switch (loyaltyCardField) { + case validFrom: + setLoyaltyCardValidFrom(null); + break; + case expiry: + setLoyaltyCardExpiry(null); + break; + default: + throw new AssertionError("Unexpected field: " + loyaltyCardField); + } } else if (s.toString().equals(getString(chooseDateOptionStringId))) { if (!lastValue.toString().equals(getString(chooseDateOptionStringId))) { dateField.setText(lastValue); @@ -1255,7 +1284,7 @@ class EditCardIdAndBarcode implements View.OnClickListener { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), ScanActivity.class); final Bundle b = new Bundle(); - b.putString(LoyaltyCardEditActivity.BUNDLE_CARDID, cardIdFieldView.getText().toString()); + b.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID, cardIdFieldView.getText().toString()); i.putExtras(b); mCardIdAndBarCodeEditorLauncher.launch(i); } @@ -1405,7 +1434,7 @@ public void onClick(View v) throws NoSuchElementException { @Override public void onColorSelected(int dialogId, int color) { // Save new colour - updateTempState(LoyaltyCardField.headerColor, color); + setLoyaltyCardHeaderColor(color); // Unset image if set mIconRemoved = true; @@ -1496,11 +1525,11 @@ private void setMaterialDatePickerResultListener() { switch (tempLoyaltyCardField) { case validFrom: formatDateField(LoyaltyCardEditActivity.this, validFromField, newDate); - updateTempState(LoyaltyCardField.validFrom, newDate); + setLoyaltyCardValidFrom(newDate); break; case expiry: formatDateField(LoyaltyCardEditActivity.this, expiryField, newDate); - updateTempState(LoyaltyCardField.expiry, newDate); + setLoyaltyCardExpiry(newDate); break; default: throw new AssertionError("Unexpected field: " + tempLoyaltyCardField); @@ -1594,8 +1623,6 @@ private void doSave() { e.printStackTrace(); } - Log.i(TAG, "Set " + loyaltyCardId + " to " + cardId + " (update: " + updateLoyaltyCard + ")"); - DBHelper.setLoyaltyCardGroups(mDatabase, loyaltyCardId, selectedGroups); ShortcutHelper.updateShortcuts(this, DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId)); @@ -1693,10 +1720,6 @@ public void startCropperUri(Uri sourceUri) { } private void generateBarcode() { - if (tempLoyaltyCard == null) { - return; - } - mTasks.flushTaskList(TaskHandler.TYPE.BARCODE, true, false, false); String cardIdString = tempLoyaltyCard.barcodeId != null ? tempLoyaltyCard.barcodeId : tempLoyaltyCard.cardId; diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index 9792a0bfde..caa0f2e2d9 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -97,6 +97,10 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements static final String STATE_IMAGEINDEX = "imageIndex"; static final String STATE_FULLSCREEN = "isFullscreen"; + static final String BUNDLE_ID = "id"; + static final String BUNDLE_CARDLIST = "cardList"; + static final String BUNDLE_TRANSITION_RIGHT = "transition_right"; + final private TaskHandler mTasks = new TaskHandler(); Runnable barcodeImageGenerationFinishedCallback; @@ -181,8 +185,8 @@ enum ImageType { private void extractIntentFields(Intent intent) { final Bundle b = intent.getExtras(); - loyaltyCardId = b != null ? b.getInt("id") : 0; - cardList = b != null ? b.getIntegerArrayList("cardList") : null; + loyaltyCardId = b != null ? b.getInt(BUNDLE_ID) : 0; + cardList = b != null ? b.getIntegerArrayList(BUNDLE_CARDLIST) : null; Log.d(TAG, "View activity: id=" + loyaltyCardId); } @@ -208,7 +212,7 @@ protected void onCreate(Bundle savedInstanceState) { return; } - int transitionRight = incomingIntentExtras.getInt("transition_right", -1); + int transitionRight = incomingIntentExtras.getInt(BUNDLE_TRANSITION_RIGHT, -1); if (transitionRight == 1) { // right side transition overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); @@ -572,8 +576,8 @@ private void prevNextCard(boolean next) { // Restart activity with new card id and index Intent intent = getIntent(); Bundle b = intent.getExtras(); - b.putInt("id", loyaltyCardId); - b.putInt("transition_right", transitionRight ? 1 : 0); + b.putInt(BUNDLE_ID, loyaltyCardId); + b.putInt(BUNDLE_TRANSITION_RIGHT, transitionRight ? 1 : 0); intent.putExtras(b); intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index a10f1178da..6b8ff2c795 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -426,15 +426,18 @@ private void processBarcodeValuesList(List barcodeValuesList, Str Utils.makeUserChooseBarcodeFromList(MainActivity.this, barcodeValuesList, new BarcodeValuesListDisambiguatorCallback() { @Override public void onUserChoseBarcode(BarcodeValues barcodeValues) { - Intent newIntent = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class); - Bundle newBundle = new Bundle(); - newBundle.putString(LoyaltyCardEditActivity.BUNDLE_BARCODETYPE, barcodeValues.format()); - newBundle.putString(LoyaltyCardEditActivity.BUNDLE_CARDID, barcodeValues.content()); + CatimaBarcode barcodeType = barcodeValues.format(); + + 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); if (group != null) { - newBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group); + bundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group); } - newIntent.putExtras(newBundle); - startActivity(newIntent); + intent.putExtras(bundle); + startActivity(intent); } @Override @@ -781,14 +784,14 @@ public void onRowClicked(int inputPosition) { Intent intent = new Intent(this, LoyaltyCardViewActivity.class); intent.setAction(""); final Bundle b = new Bundle(); - b.putInt("id", loyaltyCard.id); + b.putInt(LoyaltyCardViewActivity.BUNDLE_ID, loyaltyCard.id); ArrayList cardList = new ArrayList<>(); for (int i = 0; i < mAdapter.getItemCount(); i++) { cardList.add(mAdapter.getCard(i).id); } - b.putIntegerArrayList("cardList", cardList); + b.putIntegerArrayList(LoyaltyCardViewActivity.BUNDLE_CARDLIST, cardList); intent.putExtras(b); startActivity(intent); diff --git a/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java b/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java index 363d9c11f7..258d5ddec5 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java @@ -33,7 +33,7 @@ public void swapCursor(Cursor inputCursor) { @Override public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor inputCursor) { - LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(inputCursor); + LoyaltyCard loyaltyCard = LoyaltyCard.fromCursor(inputCursor); Boolean overlayValue = mInGroupOverlay.get(loyaltyCard.id); if ((overlayValue != null ? overlayValue : isLoyaltyCardInGroup(loyaltyCard.id))) { mAnimationItemsIndex.put(inputCursor.getPosition(), true); diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index 9c177751e9..fd2e3a04a2 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -82,7 +82,7 @@ public class ScanActivity extends CatimaAppCompatActivity { private void extractIntentFields(Intent intent) { final Bundle b = intent.getExtras(); - cardId = b != null ? b.getString(LoyaltyCardEditActivity.BUNDLE_CARDID) : null; + cardId = b != null ? b.getString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID) : null; addGroup = b != null ? b.getString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) : null; Log.d(TAG, "Scan activity: id=" + cardId); } @@ -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 @@ -338,7 +340,7 @@ private void addWithoutBarcode() { // Buttons builder.setPositiveButton(getString(R.string.ok), (dialog, which) -> { - returnResult(input.getText().toString(), ""); + returnResult(input.getText().toString(), null); }); builder.setNegativeButton(getString(R.string.cancel), (dialog, which) -> dialog.cancel()); AlertDialog dialog = builder.create(); @@ -373,7 +375,7 @@ public void addManually() { Intent i = new Intent(getApplicationContext(), BarcodeSelectorActivity.class); if (cardId != null) { final Bundle b = new Bundle(); - b.putString("initialCardId", cardId); + b.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID, cardId); i.putExtras(b); } manualAddLauncher.launch(i); diff --git a/app/src/main/java/protect/card_locker/ShortcutHelper.java b/app/src/main/java/protect/card_locker/ShortcutHelper.java index 4df23de2c8..36a28aa2dc 100644 --- a/app/src/main/java/protect/card_locker/ShortcutHelper.java +++ b/app/src/main/java/protect/card_locker/ShortcutHelper.java @@ -133,8 +133,7 @@ static ShortcutInfoCompat.Builder createShortcutBuilder(Context context, Loyalty // one replace it. intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_SINGLE_TOP); final Bundle bundle = new Bundle(); - bundle.putInt("id", loyaltyCard.id); - bundle.putBoolean("view", true); + bundle.putInt(LoyaltyCardViewActivity.BUNDLE_ID, loyaltyCard.id); intent.putExtras(bundle); Bitmap iconBitmap = Utils.retrieveCardImage(context, loyaltyCard.id, ImageLocationType.icon); diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index dbba4e539e..116f60add3 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -268,7 +268,7 @@ static public List 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"); @@ -323,7 +323,7 @@ static private List 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; @@ -344,7 +344,7 @@ static public void makeUserChooseBarcodeFromList(Context context, List numCards ? card.id - numCards : numCards - card.id + 1; @@ -236,7 +236,7 @@ private void checkLoyaltyCardsFiveStarred() { while (index < 10) { cursor.moveToNext(); - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cursor); + LoyaltyCard card = LoyaltyCard.fromCursor(cursor); String expectedStore = String.format("store, \"%4d", index); String expectedNote = String.format("note, \"%4d", index); @@ -258,7 +258,7 @@ private void checkLoyaltyCardsFiveStarred() { index = 1; while (cursor.moveToNext() && index < 5) { - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cursor); + LoyaltyCard card = LoyaltyCard.fromCursor(cursor); String expectedStore = String.format("store, \"%4d", index); String expectedNote = String.format("note, \"%4d", index); diff --git a/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java b/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java index 4d6ce4cd5c..1ada6674d5 100644 --- a/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java +++ b/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java @@ -137,7 +137,7 @@ public void TestCursorAdapterStarring() { assertEquals(4, cursor.getCount()); assertTrue(cursor.moveToFirst()); - LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(cursor); + LoyaltyCard loyaltyCard = LoyaltyCard.fromCursor(cursor); assertEquals("storeD", loyaltyCard.store); View view = createView(cursor); ConstraintLayout star = view.findViewById(R.id.star); @@ -146,7 +146,7 @@ public void TestCursorAdapterStarring() { assertEquals(View.GONE, archive.getVisibility()); assertTrue(cursor.moveToNext()); - loyaltyCard = LoyaltyCard.toLoyaltyCard(cursor); + loyaltyCard = LoyaltyCard.fromCursor(cursor); assertEquals("storeC", loyaltyCard.store); view = createView(cursor); star = view.findViewById(R.id.star); @@ -155,7 +155,7 @@ public void TestCursorAdapterStarring() { assertEquals(View.GONE, archive.getVisibility()); assertTrue(cursor.moveToNext()); - loyaltyCard = LoyaltyCard.toLoyaltyCard(cursor); + loyaltyCard = LoyaltyCard.fromCursor(cursor); assertEquals("storeB", loyaltyCard.store); view = createView(cursor); star = view.findViewById(R.id.star); @@ -164,7 +164,7 @@ public void TestCursorAdapterStarring() { assertEquals(View.VISIBLE, archive.getVisibility()); assertTrue(cursor.moveToNext()); - loyaltyCard = LoyaltyCard.toLoyaltyCard(cursor); + loyaltyCard = LoyaltyCard.fromCursor(cursor); assertEquals("storeA", loyaltyCard.store); view = createView(cursor); star = view.findViewById(R.id.star); diff --git a/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java b/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java index 2d54f36261..eb61e0316c 100644 --- a/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java +++ b/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java @@ -37,9 +37,11 @@ import android.widget.LinearLayout; import android.widget.TextView; +import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; +import androidx.test.core.app.ApplicationProvider; import com.google.android.material.bottomappbar.BottomAppBar; import com.google.android.material.floatingactionbutton.FloatingActionButton; @@ -347,8 +349,13 @@ public void noCrashOnRegionlessLocale() { @Test public void noDataLossOnResumeOrRotate() { + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + registerMediaStoreIntentHandler(); + Integer cardId; + for (boolean newCard : new boolean[]{false, true}) { System.out.println(); System.out.println("====="); @@ -356,21 +363,14 @@ public void noDataLossOnResumeOrRotate() { System.out.println("====="); System.out.println(); - ActivityController activityController; - if (!newCard) { - activityController = createActivityWithLoyaltyCard(true); + cardId = (int) DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null, 0); } else { - activityController = Robolectric.buildActivity(LoyaltyCardEditActivity.class).create(); + cardId = null; } + ActivityController activityController = createActivityWithLoyaltyCard(true, cardId); LoyaltyCardEditActivity activity = (LoyaltyCardEditActivity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - - if (!newCard) { - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); - } activityController.start(); activityController.visible(); @@ -403,9 +403,9 @@ public void noDataLossOnResumeOrRotate() { storeField.setText("correct store"); noteField.setText("correct note"); LoyaltyCardEditActivity.formatDateField(context, validFromField, validFromDate); - activity.updateTempState(LoyaltyCardField.validFrom, validFromDate); + activity.setLoyaltyCardValidFrom(validFromDate); LoyaltyCardEditActivity.formatDateField(context, expiryField, expiryDate); - activity.updateTempState(LoyaltyCardField.expiry, expiryDate); + activity.setLoyaltyCardExpiry(expiryDate); balanceField.setText("100"); balanceTypeField.setText(currency.getSymbol()); cardIdField.setText("12345678"); @@ -461,6 +461,9 @@ public void startWithoutParametersCheckFieldsAvailable() { @Test public void startWithoutParametersCannotCreateLoyaltyCard() { + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + ActivityController activityController = Robolectric.buildActivity(LoyaltyCardEditActivity.class).create(); activityController.start(); activityController.visible(); @@ -468,7 +471,6 @@ public void startWithoutParametersCannotCreateLoyaltyCard() { Activity activity = (Activity) activityController.get(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); assertEquals(0, DBHelper.getLoyaltyCardCount(database)); final EditText storeField = activity.findViewById(R.id.storeNameEdit); @@ -583,18 +585,20 @@ public void startWithoutParametersCaptureBarcodeCancel() throws IOException { assertEquals(true, activity.isFinishing()); } - private ActivityController createActivityWithLoyaltyCard(boolean editMode) { + private ActivityController createActivityWithLoyaltyCard(boolean editMode, @Nullable Integer loyaltyCardId) { Intent intent = new Intent(); final Bundle bundle = new Bundle(); - bundle.putInt("id", 1); Class clazz; if (editMode) { - bundle.putBoolean("update", true); + if (loyaltyCardId != null) { + bundle.putInt(LoyaltyCardEditActivity.BUNDLE_ID, loyaltyCardId); + bundle.putBoolean(LoyaltyCardEditActivity.BUNDLE_UPDATE, true); + } clazz = LoyaltyCardEditActivity.class; } else { - bundle.putBoolean("view", true); + bundle.putInt(LoyaltyCardViewActivity.BUNDLE_ID, loyaltyCardId); clazz = LoyaltyCardViewActivity.class; } @@ -605,12 +609,14 @@ private ActivityController createActivityWithLoyaltyCard(boolean editMode) { @Test public void startWithLoyaltyCardEditModeCheckDisplay() throws IOException { - ActivityController activityController = createActivityWithLoyaltyCard(true); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); activityController.start(); activityController.visible(); @@ -623,12 +629,13 @@ public void startWithLoyaltyCardEditModeCheckDisplay() throws IOException { @Test public void startWithLoyaltyCardViewModeCheckDisplay() { - ActivityController activityController = createActivityWithLoyaltyCard(false); - Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(false, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -641,12 +648,13 @@ public void startWithLoyaltyCardViewModeCheckDisplay() { @Test public void startWithLoyaltyCardWithBarcodeUpdateBarcode() throws IOException { - ActivityController activityController = createActivityWithLoyaltyCard(true); - Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null, 0); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -665,12 +673,13 @@ public void startWithLoyaltyCardWithBarcodeUpdateBarcode() throws IOException { @Test public void startWithLoyaltyCardWithReceiptUpdateReceiptCancel() throws IOException { - ActivityController activityController = createActivityWithLoyaltyCard(true); - LoyaltyCardEditActivity activity = (LoyaltyCardEditActivity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); + LoyaltyCardEditActivity activity = (LoyaltyCardEditActivity) activityController.get(); activityController.start(); activityController.visible(); @@ -703,12 +712,13 @@ public void startWithLoyaltyCardWithReceiptUpdateReceiptCancel() throws IOExcept @Test public void startWithLoyaltyCardNoExpirySetExpiry() throws IOException { - ActivityController activityController = createActivityWithLoyaltyCard(true); - Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -735,12 +745,13 @@ public void startWithLoyaltyCardNoExpirySetExpiry() throws IOException { @Test public void startWithLoyaltyCardExpirySetNoExpiry() throws IOException { - ActivityController activityController = createActivityWithLoyaltyCard(true); - Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, new Date(), new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, new Date(), new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -759,12 +770,13 @@ public void startWithLoyaltyCardExpirySetNoExpiry() throws IOException { @Test public void startWithLoyaltyCardNoBalanceSetBalance() throws IOException { - ActivityController activityController = createActivityWithLoyaltyCard(true); - Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null, 0); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -809,12 +821,13 @@ public void onFocusChange(View v, boolean hasFocus) { @Test public void startWithLoyaltyCardBalanceSetNoBalance() throws IOException { - ActivityController activityController = createActivityWithLoyaltyCard(true); - Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("10.00"), Currency.getInstance("USD"), EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null, 0); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("10.00"), Currency.getInstance("USD"), EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -849,12 +862,13 @@ public void startWithLoyaltyCardBalanceSetNoBalance() throws IOException { @Test public void startWithLoyaltyCardSameAsCardIDUpdateBarcodeID() { - ActivityController activityController = createActivityWithLoyaltyCard(true); - Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -880,12 +894,13 @@ public void startWithLoyaltyCardSameAsCardIDUpdateBarcodeID() { @Test public void startWithLoyaltyCardSameAsCardIDUpdateCardID() { - ActivityController activityController = createActivityWithLoyaltyCard(true); - Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -913,12 +928,13 @@ public void startWithLoyaltyCardSameAsCardIDUpdateCardID() { @Test public void startWithLoyaltyCardDifferentFromCardIDUpdateCardIDUpdate() { - ActivityController activityController = createActivityWithLoyaltyCard(true); - Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, "123456", EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, "123456", EAN_BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -949,12 +965,13 @@ public void startWithLoyaltyCardDifferentFromCardIDUpdateCardIDUpdate() { @Test public void startWithLoyaltyCardDifferentFromCardIDUpdateCardIDDoNotUpdate() { - ActivityController activityController = createActivityWithLoyaltyCard(true); - Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, "123456", EAN_BARCODE_TYPE, Color.BLACK, 0, null, 0); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, "123456", EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -985,11 +1002,13 @@ public void startWithLoyaltyCardDifferentFromCardIDUpdateCardIDDoNotUpdate() { @Test public void checkMenu() throws IOException { - ActivityController activityController = createActivityWithLoyaltyCard(false); - Activity activity = (Activity) activityController.get(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(false, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -1011,15 +1030,13 @@ public void checkMenu() throws IOException { @Test public void startWithMissingLoyaltyCard() throws IOException { - ActivityController activityController = createActivityWithLoyaltyCard(true); + ActivityController activityController = createActivityWithLoyaltyCard(true, 1); Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); - activityController.resume(); // The activity should find that the card is missing and shut down - assertTrue(activity.isFinishing()); // Make sure the activity can close down @@ -1030,11 +1047,13 @@ public void startWithMissingLoyaltyCard() throws IOException { @Test public void startWithoutParametersViewBack() { - ActivityController activityController = createActivityWithLoyaltyCard(false); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null, 0); + ActivityController activityController = createActivityWithLoyaltyCard(false, (int) cardId); Activity activity = (Activity) activityController.get(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); activityController.start(); activityController.visible(); @@ -1049,11 +1068,13 @@ public void startWithoutParametersViewBack() { @Test public void startWithoutColors() { - ActivityController activityController = createActivityWithLoyaltyCard(false); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, null, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(false, (int) cardId); Activity activity = (Activity) activityController.get(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, null, 0, null,0); activityController.start(); activityController.visible(); @@ -1068,11 +1089,13 @@ public void startWithoutColors() { @Test public void startLoyaltyCardWithoutColorsSave() throws IOException, ParseException { - ActivityController activityController = createActivityWithLoyaltyCard(true); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, null, 0, null, 0); + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); Activity activity = (Activity) activityController.get(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, null, 0, null,0); activityController.start(); activityController.visible(); @@ -1086,11 +1109,13 @@ public void startLoyaltyCardWithoutColorsSave() throws IOException, ParseExcepti @Test public void startLoyaltyCardWithExplicitNoBarcodeSave() throws IOException, ParseException { - ActivityController activityController = createActivityWithLoyaltyCard(true); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, null, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); Activity activity = (Activity) activityController.get(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, null, Color.BLACK, 0, null,0); activityController.start(); activityController.visible(); @@ -1104,12 +1129,13 @@ public void startLoyaltyCardWithExplicitNoBarcodeSave() throws IOException, Pars @Test public void removeBarcodeFromLoyaltyCard() throws IOException, ParseException { - ActivityController activityController = createActivityWithLoyaltyCard(true); - Activity activity = (Activity) activityController.get(); - final Context context = activity.getApplicationContext(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(true, (int) cardId); + Activity activity = (Activity) activityController.get(); activityController.start(); activityController.visible(); @@ -1119,7 +1145,7 @@ public void removeBarcodeFromLoyaltyCard() throws IOException, ParseException { checkAllFields(activity, ViewMode.UPDATE_CARD, "store", "note", context.getString(R.string.anyDate), context.getString(R.string.never), "0", context.getString(R.string.points), BARCODE_DATA, context.getString(R.string.sameAsCardId), BARCODE_TYPE.prettyName(), null, null); // Complete empty barcode selection successfully - selectBarcodeWithResult(activity, BARCODE_DATA, "", true); + selectBarcodeWithResult(activity, BARCODE_DATA, null, true); activityController.resume(); // Check if the barcode type is NO_BARCODE as expected @@ -1134,11 +1160,14 @@ public void removeBarcodeFromLoyaltyCard() throws IOException, ParseException { @Test public void checkPushStarIcon() { - ActivityController activityController = createActivityWithLoyaltyCard(false); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(false, (int) cardId); Activity activity = (Activity) activityController.get(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + activityController.start(); activityController.visible(); activityController.resume(); @@ -1168,11 +1197,13 @@ public void checkPushStarIcon() { @Test public void checkBarcodeFullscreenWorkflow() { - ActivityController activityController = createActivityWithLoyaltyCard(false); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null, 0); + + ActivityController activityController = createActivityWithLoyaltyCard(false, (int) cardId); AppCompatActivity activity = (AppCompatActivity) activityController.get(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); activityController.start(); activityController.visible(); @@ -1262,11 +1293,13 @@ public void checkBarcodeFullscreenWorkflow() { @Test public void checkNoBarcodeFullscreenWorkflow() { - ActivityController activityController = createActivityWithLoyaltyCard(false); + final Context context = ApplicationProvider.getApplicationContext(); + SQLiteDatabase database = TestHelpers.getEmptyDb(context).getWritableDatabase(); + + long cardId = DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, null, Color.BLACK, 0, null, 0); + ActivityController activityController = createActivityWithLoyaltyCard(false, (int) cardId); AppCompatActivity activity = (AppCompatActivity) activityController.get(); - SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, null, Color.BLACK, 0, null,0); activityController.start(); activityController.visible();