From a207f0ef25d0b15c560d8bbcb202799510bb8230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=A4nge?= Date: Wed, 30 Aug 2023 17:49:23 +0200 Subject: [PATCH 1/2] Move `createHistoryItem` method to Utils class --- .../qrscanner/helpers/Utils.java | 35 +++++++++++++++++ .../ui/viewmodel/ResultViewModel.java | 38 ++----------------- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/helpers/Utils.java b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/helpers/Utils.java index 723afa11..08ced903 100644 --- a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/helpers/Utils.java +++ b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/helpers/Utils.java @@ -13,6 +13,8 @@ import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; +import com.journeyapps.barcodescanner.BarcodeResult; +import com.secuso.privacyfriendlycodescanner.qrscanner.database.HistoryItem; import java.util.EnumMap; import java.util.Map; @@ -90,4 +92,37 @@ public static Bitmap generateCode(String data, BarcodeFormat original_format, in return null; } } + + public static HistoryItem createHistoryItem(Bitmap mCodeImage, BarcodeResult currentBarcodeResult, boolean prefSaveRealImage) { + HistoryItem currentHistoryItem = new HistoryItem(); + + Bitmap image; + if (prefSaveRealImage) { + float height; + float width; + if (mCodeImage.getWidth() == 0 || mCodeImage.getWidth() == 0) { + height = 200f; + width = 200f; + } else if (mCodeImage.getWidth() > mCodeImage.getHeight()) { + height = (float) mCodeImage.getHeight() / (float) mCodeImage.getWidth() * 200f; + width = 200f; + } else { + width = (float) mCodeImage.getWidth() / (float) mCodeImage.getHeight() * 200f; + height = 200f; + } + image = Bitmap.createScaledBitmap(mCodeImage, (int) width, (int) height, false); + } else { + image = Utils.generateCode(currentBarcodeResult.getText(), currentBarcodeResult.getBarcodeFormat(), null, currentBarcodeResult.getResult().getResultMetadata()); + } + currentHistoryItem.setImage(image); + + currentHistoryItem.setFormat(currentBarcodeResult.getResult().getBarcodeFormat()); + currentHistoryItem.setNumBits(currentBarcodeResult.getResult().getNumBits()); + currentHistoryItem.setRawBytes(currentBarcodeResult.getResult().getRawBytes()); + currentHistoryItem.setResultPoints(currentBarcodeResult.getResult().getResultPoints()); + currentHistoryItem.setText(currentBarcodeResult.getResult().getText()); + currentHistoryItem.setTimestamp(currentBarcodeResult.getResult().getTimestamp()); + + return currentHistoryItem; + } } diff --git a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/ui/viewmodel/ResultViewModel.java b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/ui/viewmodel/ResultViewModel.java index fd9b2708..a231fa93 100644 --- a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/ui/viewmodel/ResultViewModel.java +++ b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/ui/viewmodel/ResultViewModel.java @@ -1,5 +1,7 @@ package com.secuso.privacyfriendlycodescanner.qrscanner.ui.viewmodel; +import static com.secuso.privacyfriendlycodescanner.qrscanner.helpers.PrefManager.PREF_SAVE_REAL_IMAGE_TO_HISTORY; + import android.app.Application; import android.content.SharedPreferences; import android.graphics.Bitmap; @@ -20,8 +22,6 @@ import com.secuso.privacyfriendlycodescanner.qrscanner.database.HistoryItem; import com.secuso.privacyfriendlycodescanner.qrscanner.helpers.Utils; -import static com.secuso.privacyfriendlycodescanner.qrscanner.helpers.PrefManager.PREF_SAVE_REAL_IMAGE_TO_HISTORY; - public class ResultViewModel extends AndroidViewModel { private BarcodeResult currentBarcodeResult = null; @@ -73,7 +73,7 @@ public void initFromScan(BarcodeResult barcodeResult) { mCodeImage = Utils.generateCode(currentBarcodeResult.getText(), currentBarcodeResult.getBarcodeFormat(), null, currentBarcodeResult.getResult().getResultMetadata()); } - createHistoryItem(); + currentHistoryItem = Utils.createHistoryItem(mCodeImage, barcodeResult, mPreferences.getBoolean(PREF_SAVE_REAL_IMAGE_TO_HISTORY, false)); if (mPreferences.getBoolean("bool_history", true)) { this.saveHistoryItem(currentHistoryItem); } @@ -89,38 +89,6 @@ public void updateHistoryItem(HistoryItem item) { AppRepository.getInstance(getApplication()).updateHistoryEntry(item); } - private void createHistoryItem() { - currentHistoryItem = new HistoryItem(); - - Bitmap image; - boolean prefSaveRealImage = mPreferences.getBoolean(PREF_SAVE_REAL_IMAGE_TO_HISTORY, false); - if (prefSaveRealImage) { - float height; - float width; - if (mCodeImage.getWidth() == 0 || mCodeImage.getWidth() == 0) { - height = 200f; - width = 200f; - } else if (mCodeImage.getWidth() > mCodeImage.getHeight()) { - height = (float) mCodeImage.getHeight() / (float) mCodeImage.getWidth() * 200f; - width = 200f; - } else { - width = (float) mCodeImage.getWidth() / (float) mCodeImage.getHeight() * 200f; - height = 200f; - } - image = Bitmap.createScaledBitmap(mCodeImage, (int) width, (int) height, false); - } else { - image = Utils.generateCode(currentBarcodeResult.getText(), currentBarcodeResult.getBarcodeFormat(), null, currentBarcodeResult.getResult().getResultMetadata()); - } - currentHistoryItem.setImage(image); - - currentHistoryItem.setFormat(currentBarcodeResult.getResult().getBarcodeFormat()); - currentHistoryItem.setNumBits(currentBarcodeResult.getResult().getNumBits()); - currentHistoryItem.setRawBytes(currentBarcodeResult.getResult().getRawBytes()); - currentHistoryItem.setResultPoints(currentBarcodeResult.getResult().getResultPoints()); - currentHistoryItem.setText(currentBarcodeResult.getResult().getText()); - currentHistoryItem.setTimestamp(currentBarcodeResult.getResult().getTimestamp()); - } - /** * Sometimes the result points array of currentBarcodeResult includes null objects, what might lead to crashes. * This function fills the array with duplicates from a valid ResultPoint or with a new ResultPoint with coordinates (0.0,0.0). From 0185f23a6a028cf0f4682ad1f9fd2b83273ff5d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=A4nge?= Date: Wed, 30 Aug 2023 17:57:32 +0200 Subject: [PATCH 2/2] Add save button to QrGeneratorDisplayActivity --- .../generator/QrGeneratorDisplayActivity.java | 38 +++++++++++++++++++ app/src/main/res/menu/save.xml | 2 +- app/src/main/res/values-de/strings.xml | 2 + app/src/main/res/values-it/strings.xml | 2 + app/src/main/res/values-nl/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 6 files changed, 47 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/ui/activities/generator/QrGeneratorDisplayActivity.java b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/ui/activities/generator/QrGeneratorDisplayActivity.java index 8f42b0df..e316114a 100644 --- a/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/ui/activities/generator/QrGeneratorDisplayActivity.java +++ b/app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/ui/activities/generator/QrGeneratorDisplayActivity.java @@ -1,10 +1,15 @@ package com.secuso.privacyfriendlycodescanner.qrscanner.ui.activities.generator; +import static com.secuso.privacyfriendlycodescanner.qrscanner.helpers.PrefManager.PREF_SAVE_REAL_IMAGE_TO_HISTORY; + import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; +import android.graphics.Bitmap; import android.os.Build; import android.os.Bundle; +import android.preference.PreferenceManager; +import android.provider.MediaStore; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -20,18 +25,25 @@ import androidx.appcompat.content.res.AppCompatResources; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; +import androidx.lifecycle.ViewModelProvider; import com.bumptech.glide.Glide; import com.bumptech.glide.request.target.BitmapImageViewTarget; import com.google.android.material.textfield.TextInputLayout; import com.google.zxing.BarcodeFormat; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; +import com.journeyapps.barcodescanner.BarcodeResult; import com.secuso.privacyfriendlycodescanner.qrscanner.R; +import com.secuso.privacyfriendlycodescanner.qrscanner.database.AppRepository; +import com.secuso.privacyfriendlycodescanner.qrscanner.database.HistoryItem; import com.secuso.privacyfriendlycodescanner.qrscanner.generator.Contents; import com.secuso.privacyfriendlycodescanner.qrscanner.generator.QRGeneratorUtils; +import com.secuso.privacyfriendlycodescanner.qrscanner.helpers.Utils; import com.secuso.privacyfriendlycodescanner.qrscanner.ui.activities.ScannerActivity; import com.secuso.privacyfriendlycodescanner.qrscanner.ui.helpers.IconArrayAdapter; +import com.secuso.privacyfriendlycodescanner.qrscanner.ui.viewmodel.ScannerViewModel; +import java.io.IOException; import java.util.Arrays; public class QrGeneratorDisplayActivity extends AppCompatActivity { @@ -203,6 +215,7 @@ private void saveImageToStorage() { @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.share, menu); + getMenuInflater().inflate(R.menu.save, menu); return true; } @@ -211,6 +224,31 @@ public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.share) { QRGeneratorUtils.shareImage(this, QRGeneratorUtils.getCachedUri()); return true; + } else if (item.getItemId() == R.id.save) { + // Use an instance of ScannerViewModel to get the BarcodeResult by 'scanning' the image + ScannerViewModel scannerViewModel = new ViewModelProvider(this).get(ScannerViewModel.class); + scannerViewModel.isScanComplete().observe(this, scanComplete -> { + if (scanComplete) { + BarcodeResult result = scannerViewModel.getScanResult().getValue(); + scannerViewModel.clearScanResult(); + if (result == null) { + Toast.makeText(this, getText(R.string.something_went_wrong), Toast.LENGTH_SHORT).show(); + } else { + try { + //Get the Bitmap, create the HistoryItem and insert it into the db. + Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), QRGeneratorUtils.getCachedUri()); + HistoryItem historyItem = Utils.createHistoryItem(bitmap, result, PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(PREF_SAVE_REAL_IMAGE_TO_HISTORY, false)); + AppRepository.getInstance(getApplication()).insertHistoryEntry(historyItem); + + Toast.makeText(this, getText(R.string.activity_result_toast_saved), Toast.LENGTH_SHORT).show(); + } catch (IOException e) { + Toast.makeText(this, getText(R.string.something_went_wrong), Toast.LENGTH_SHORT).show(); + } + } + } + }); + //Start the 'scan' + scannerViewModel.getBarcodeResultFromImage(QRGeneratorUtils.getCachedUri()); } return super.onOptionsItemSelected(item); } diff --git a/app/src/main/res/menu/save.xml b/app/src/main/res/menu/save.xml index ee817c41..2ed1a9c9 100644 --- a/app/src/main/res/menu/save.xml +++ b/app/src/main/res/menu/save.xml @@ -5,7 +5,7 @@ \ No newline at end of file diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 5dc8d6a9..ead8d47d 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -345,5 +345,7 @@ Fehlerkorrektur-Level Format Es konnte kein Code generiert werden. + Etwas ist schief gelaufen + In der Historie speichern diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 3b6510cd..c386d856 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -352,6 +352,8 @@ Livello di correzione degli errori Formato Non è stato possibile generare alcun codice. + Qualcosa è andato storto + Salva nella cronologia diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 16913943..e5a35f67 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -352,5 +352,7 @@ Foutcorrectieniveau Formaat Er kon geen code worden gegenereerd. + Er ging iets mis + Opslaan in geschiedenis diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 188568ea..96e22f5d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -180,6 +180,7 @@ Vehicle Identification Number (VIN) + Something went wrong Copied to clipboard. No name specified @@ -256,6 +257,7 @@ Format Error correction level No code could be generated. + Save to history Text You can enter any text you want to encode. Multiple lines are allowed.