Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button to save generated codes to history #175

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/menu/save.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<item
android:id="@+id/save"
android:icon="@drawable/ic_save_white_24dp"
android:title="Save"
android:title="@string/save_to_history"
app:showAsAction="always" />

</menu>
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -345,5 +345,7 @@
<string name="error_correction_level">Fehlerkorrektur-Level</string>
<string name="barcode_format">Format</string>
<string name="code_generation_error">Es konnte kein Code generiert werden.</string>
<string name="something_went_wrong">Etwas ist schief gelaufen</string>
<string name="save_to_history">In der Historie speichern</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@
<string name="error_correction_level">Livello di correzione degli errori</string>
<string name="barcode_format">Formato</string>
<string name="code_generation_error">Non è stato possibile generare alcun codice.</string>
<string name="something_went_wrong">Qualcosa è andato storto</string>
<string name="save_to_history">Salva nella cronologia</string>


</resources>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,7 @@
<string name="error_correction_level">Foutcorrectieniveau</string>
<string name="barcode_format">Formaat</string>
<string name="code_generation_error">Er kon geen code worden gegenereerd.</string>
<string name="something_went_wrong">Er ging iets mis</string>
<string name="save_to_history">Opslaan in geschiedenis</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
<item>Vehicle Identification Number (VIN)</item>
</string-array>

<string name="something_went_wrong">Something went wrong</string>
<string name="copied_to_clipboard">Copied to clipboard.</string>
<string name="noname">No name specified</string>

Expand Down Expand Up @@ -256,6 +257,7 @@
<string name="barcode_format">Format</string>
<string name="error_correction_level">Error correction level</string>
<string name="code_generation_error">No code could be generated.</string>
<string name="save_to_history">Save to history</string>

<string name="generator_text_textfield">Text</string>
<string name="generator_text_explanation">You can enter any text you want to encode. Multiple lines are allowed.</string>
Expand Down