-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a crash when trying to write history items to parcels.
- Loading branch information
Showing
4 changed files
with
116 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,7 +115,4 @@ private AppDatabase getMigratedRoomDatabase() { | |
testHelper.closeWhenFinished(database); | ||
return database; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 98 additions & 5 deletions
103
...in/java/com/secuso/privacyFriendlyCodeScanner/qrscanner/ui/viewmodel/ResultViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,114 @@ | ||
package com.secuso.privacyfriendlycodescanner.qrscanner.ui.viewmodel; | ||
|
||
import android.arch.lifecycle.ViewModel; | ||
import android.app.Activity; | ||
import android.app.Application; | ||
import android.arch.lifecycle.AndroidViewModel; | ||
import android.content.SharedPreferences; | ||
import android.graphics.Bitmap; | ||
import android.preference.PreferenceManager; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.content.ContextCompat; | ||
|
||
import com.google.zxing.BarcodeFormat; | ||
import com.google.zxing.client.result.ParsedResult; | ||
import com.google.zxing.client.result.ResultParser; | ||
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.ui.resultfragments.ResultFragment; | ||
import com.secuso.privacyfriendlycodescanner.qrscanner.helpers.Utils; | ||
|
||
public class ResultViewModel extends ViewModel { | ||
import static com.secuso.privacyfriendlycodescanner.qrscanner.helpers.PrefManager.PREF_SAVE_REAL_IMAGE_TO_HISTORY; | ||
|
||
public BarcodeResult currentBarcodeResult = null; | ||
public ResultFragment currentResultFragment = null; | ||
public class ResultViewModel extends AndroidViewModel { | ||
|
||
private BarcodeResult currentBarcodeResult = null; | ||
|
||
public HistoryItem currentHistoryItem = null; | ||
public ParsedResult mParsedResult = null; | ||
public Bitmap mCodeImage = null; | ||
public boolean mSavedToHistory = false; | ||
|
||
private SharedPreferences mPreferences; | ||
|
||
public ResultViewModel(@NonNull Application application) { | ||
super(application); | ||
|
||
mPreferences = PreferenceManager.getDefaultSharedPreferences(application); | ||
} | ||
|
||
/** | ||
* After this function is called the the following values will not be null: | ||
* <ul> | ||
* <li>currentHistoryItem</li> | ||
* <li>mCodeImage</li> | ||
* <li>mSavedToHistory</li> | ||
* <li>mParsedResult</li> | ||
* </ul> | ||
* If the state can not be created the activity will call {@link Activity#finish()} | ||
* This method will also update the {@link HistoryItem} in the database with a recreation of the QR Code if the image is missing. | ||
*/ | ||
public void initFromHistoryItem(HistoryItem historyItem) { | ||
currentHistoryItem = historyItem; | ||
mParsedResult = ResultParser.parseResult(currentHistoryItem.getResult()); | ||
mCodeImage = currentHistoryItem.getImage(); | ||
if(mCodeImage == null) { | ||
mCodeImage = Utils.generateCode(currentHistoryItem.getText(), BarcodeFormat.QR_CODE, null, null); | ||
currentHistoryItem.setImage(mCodeImage); | ||
currentHistoryItem.setFormat(BarcodeFormat.QR_CODE); | ||
updateHistoryItem(currentHistoryItem); | ||
} | ||
mSavedToHistory = true; | ||
} | ||
|
||
public void initFromScan(BarcodeResult barcodeResult) { | ||
currentBarcodeResult = barcodeResult; | ||
mParsedResult = ResultParser.parseResult(currentBarcodeResult.getResult()); | ||
mCodeImage = currentBarcodeResult.getBitmapWithResultPoints(ContextCompat.getColor(getApplication(), R.color.colorAccent)); | ||
|
||
createHistoryItem(); | ||
} | ||
|
||
public void saveHistoryItem(HistoryItem item) { | ||
AppRepository.getInstance(getApplication()).insertHistoryEntry(item); | ||
mSavedToHistory = true; | ||
} | ||
|
||
|
||
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()); | ||
} | ||
|
||
} |