diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index f24119ddb..bbf6b0fd2 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -104,9 +104,6 @@ public class LoyaltyCardEditActivity extends AppCompatActivity ImageView cardImageFront; ImageView cardImageBack; - Bitmap frontImageBitmap; - Bitmap backImageBitmap; - Button enterButton; int loyaltyCardId; diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index 779e36d08..51b002cda 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -89,6 +89,7 @@ public class LoyaltyCardViewActivity extends AppCompatActivity boolean starred; boolean backgroundNeedsDarkIcons; FullscreenType fullscreenType = FullscreenType.NONE; + boolean isBarcodeSupported = true; enum FullscreenType { NONE, @@ -406,7 +407,7 @@ public class LoyaltyCardViewActivity extends AppCompatActivity } expiryView.setTag(loyaltyCard.expiry); - if (fullscreenType != FullscreenType.NONE) { + if (fullscreenType == FullscreenType.NONE) { makeBottomSheetVisibleIfUseful(); } @@ -459,8 +460,6 @@ public class LoyaltyCardViewActivity extends AppCompatActivity // Set shadow colour of store text so even same color on same color would be readable storeName.setShadowLayer(1, 1, 1, backgroundNeedsDarkIcons ? Color.BLACK : Color.WHITE); - Boolean isBarcodeSupported = true; - if (format != null && !BarcodeSelectorActivity.SUPPORTED_BARCODE_TYPES.contains(format.name())) { isBarcodeSupported = false; @@ -682,14 +681,15 @@ public class LoyaltyCardViewActivity extends AppCompatActivity private void setFullscreen(FullscreenType fullscreenType) { ActionBar actionBar = getSupportActionBar(); - if (fullscreenType != FullscreenType.NONE) - { - Log.d(TAG, "Move into of fullscreen"); + if (fullscreenType != FullscreenType.NONE) { + Log.d(TAG, "Move into fullscreen"); if (fullscreenType == FullscreenType.IMAGE_FRONT) { barcodeImage.setImageBitmap(frontImageBitmap); + barcodeImage.setVisibility(View.VISIBLE); } else if (fullscreenType == FullscreenType.IMAGE_BACK) { barcodeImage.setImageBitmap(backImageBitmap); + barcodeImage.setVisibility(View.VISIBLE); } else { // Prepare redraw after size change redrawBarcodeAfterResize(); @@ -737,10 +737,16 @@ public class LoyaltyCardViewActivity extends AppCompatActivity barcodeScaler.setProgress(100); // Prepare redraw after size change - redrawBarcodeAfterResize(); + if (format != null && isBarcodeSupported) { + redrawBarcodeAfterResize(); + } else { + barcodeImage.setVisibility(View.GONE); + } // Show maximize and hide minimize button and scaler - maximizeButton.setVisibility(View.VISIBLE); + if (format != null && isBarcodeSupported) { + maximizeButton.setVisibility(View.VISIBLE); + } minimizeButton.setVisibility(View.GONE); barcodeScaler.setVisibility(View.GONE); diff --git a/app/src/main/java/protect/card_locker/importexport/StocardImporter.java b/app/src/main/java/protect/card_locker/importexport/StocardImporter.java index e88041182..7a9a416ed 100644 --- a/app/src/main/java/protect/card_locker/importexport/StocardImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/StocardImporter.java @@ -2,6 +2,7 @@ package protect.card_locker.importexport; import android.content.Context; import android.database.sqlite.SQLiteDatabase; +import android.graphics.Bitmap; import android.graphics.BitmapFactory; import com.google.zxing.BarcodeFormat; @@ -13,9 +14,13 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.math.BigDecimal; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.util.ArrayList; @@ -118,14 +123,14 @@ public class StocardImporter implements Importer loyaltyCardHashMap, cardName, "frontImage", - read(zipInputStream) + readImage(zipInputStream) ); } else if (fileName.endsWith("/images/back.png")) { loyaltyCardHashMap = appendToLoyaltyCardHashMap( loyaltyCardHashMap, cardName, "backImage", - read(zipInputStream) + readImage(zipInputStream) ); } } @@ -155,12 +160,10 @@ public class StocardImporter implements Importer long loyaltyCardInternalId = db.insertLoyaltyCard(database, store, note, null, BigDecimal.valueOf(0), null, cardId, null, barcodeType, null, 0); if (loyaltyCardData.containsKey("frontImage")) { - byte[] byteArray = ((String) loyaltyCardData.get("frontImage")).getBytes(); - Utils.saveCardImage(context, BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length), (int) loyaltyCardInternalId, true); + Utils.saveCardImage(context, (Bitmap) loyaltyCardData.get("frontImage"), (int) loyaltyCardInternalId, true); } if (loyaltyCardData.containsKey("backImage")) { - byte[] byteArray = ((String) loyaltyCardData.get("backImage")).getBytes(); - Utils.saveCardImage(context, BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length), (int) loyaltyCardInternalId, false); + Utils.saveCardImage(context, (Bitmap) loyaltyCardData.get("backImage"), (int) loyaltyCardInternalId, false); } } @@ -186,17 +189,19 @@ public class StocardImporter implements Importer } private String read(ZipInputStream zipInputStream) throws IOException { - int read; - byte[] buffer = new byte[4096]; - StringBuilder stringBuilder = new StringBuilder(); - while ((read = zipInputStream.read(buffer, 0, 4096)) >= 0) { - stringBuilder.append(new String(buffer, 0, read, StandardCharsets.UTF_8)); + Reader reader = new BufferedReader(new InputStreamReader(zipInputStream, Charset.forName(StandardCharsets.UTF_8.name()))); + int c; + while ((c = reader.read()) != -1) { + stringBuilder.append((char) c); } - return stringBuilder.toString(); } + private Bitmap readImage(ZipInputStream zipInputStream) { + return BitmapFactory.decodeStream(zipInputStream); + } + private JSONObject readJSON(ZipInputStream zipInputStream) throws IOException, JSONException { return new JSONObject(read(zipInputStream)); } diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index cb3ba9089..273455194 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -3,6 +3,7 @@ package protect.card_locker; import android.app.Activity; import android.database.Cursor; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.os.Environment; @@ -1146,6 +1147,9 @@ public class ImportExportTest assertEquals(null, card.barcodeType); assertEquals(0, card.starStatus); + assertNull(Utils.retrieveCardImage(activity.getApplicationContext(), 1, true)); + assertNull(Utils.retrieveCardImage(activity.getApplicationContext(), 1, false)); + card = db.getLoyaltyCard(2); assertEquals("Air Miles", card.store); @@ -1158,6 +1162,9 @@ public class ImportExportTest assertEquals(null, card.barcodeType); assertEquals(0, card.starStatus); + assertTrue(BitmapFactory.decodeStream(getClass().getResourceAsStream("stocard-front.jpg")).sameAs(Utils.retrieveCardImage(activity.getApplicationContext(), 2, true))); + assertTrue(BitmapFactory.decodeStream(getClass().getResourceAsStream("stocard-back.jpg")).sameAs(Utils.retrieveCardImage(activity.getApplicationContext(), 2, false))); + card = db.getLoyaltyCard(3); assertEquals("jö", card.store); @@ -1170,6 +1177,9 @@ public class ImportExportTest assertEquals(BarcodeFormat.RSS_EXPANDED, card.barcodeType); assertEquals(0, card.starStatus); + assertNull(Utils.retrieveCardImage(activity.getApplicationContext(), 3, true)); + assertNull(Utils.retrieveCardImage(activity.getApplicationContext(), 3, false)); + TestHelpers.getEmptyDb(activity); } diff --git a/app/src/test/res/protect/card_locker/stocard-back.jpg b/app/src/test/res/protect/card_locker/stocard-back.jpg new file mode 100644 index 000000000..523c6aeaa Binary files /dev/null and b/app/src/test/res/protect/card_locker/stocard-back.jpg differ diff --git a/app/src/test/res/protect/card_locker/stocard-front.jpg b/app/src/test/res/protect/card_locker/stocard-front.jpg new file mode 100644 index 000000000..1eae74749 Binary files /dev/null and b/app/src/test/res/protect/card_locker/stocard-front.jpg differ diff --git a/build.gradle b/build.gradle index 5dac49ae5..aa118486e 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:4.1.3' + classpath 'com.android.tools.build:gradle:4.2.2' classpath 'gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:4.7.0' // NOTE: Do not place your application dependencies here; they belong