mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-21 17:41:50 -08:00
Resize icon file to upload
This commit is contained in:
1 parent
3e6cd98cb9
commit
d95a3e00aa
4 files changed
+102
-35
No files matched your search
@@ -40,7 +40,6 @@ import com.kunzisoft.keepass.database.element.icon.IconImage
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.UriUtil
|
||||
import com.kunzisoft.keepass.view.asError
|
||||
import com.kunzisoft.keepass.view.showActionErrorIfNeeded
|
||||
import com.kunzisoft.keepass.view.updateLockPaddingLeft
|
||||
import com.kunzisoft.keepass.viewmodels.IconPickerViewModel
|
||||
|
||||
@@ -113,7 +112,10 @@ class IconPickerActivity : LockingActivity() {
|
||||
})
|
||||
finish()
|
||||
}
|
||||
iconPickerViewModel.iconCustomAdded.observe(this) { _ ->
|
||||
iconPickerViewModel.iconCustomAdded.observe(this) { iconCustomAdded ->
|
||||
if (iconCustomAdded.binaryFile.length <= 0) {
|
||||
Snackbar.make(coordinatorLayout, R.string.error_upload_file, Snackbar.LENGTH_LONG).asError().show()
|
||||
}
|
||||
uploadButton.isEnabled = true
|
||||
}
|
||||
}
|
||||
@@ -149,7 +151,7 @@ class IconPickerActivity : LockingActivity() {
|
||||
uri?.let { iconToUploadUri ->
|
||||
UriUtil.getFileData(this, iconToUploadUri)?.also { documentFile ->
|
||||
if (documentFile.length() > MAX_ICON_SIZE) {
|
||||
Snackbar.make(coordinatorLayout, R.string.error_image_to_big, Snackbar.LENGTH_LONG).asError().show()
|
||||
Snackbar.make(coordinatorLayout, R.string.error_file_to_big, Snackbar.LENGTH_LONG).asError().show()
|
||||
} else {
|
||||
mDatabase?.let { database ->
|
||||
iconPickerViewModel.addCustomIcon(database,
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
package com.kunzisoft.keepass.tasks
|
||||
|
||||
import android.content.ContentResolver
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import com.kunzisoft.keepass.database.element.Database
|
||||
import com.kunzisoft.keepass.database.element.database.BinaryFile
|
||||
import com.kunzisoft.keepass.stream.readAllBytes
|
||||
import com.kunzisoft.keepass.utils.UriUtil
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
object BinaryStreamManager {
|
||||
|
||||
@@ -16,20 +22,28 @@ object BinaryStreamManager {
|
||||
update: ((percent: Int)->Unit)? = null,
|
||||
canceled: ()-> Boolean = { false },
|
||||
bufferSize: Int = DEFAULT_BUFFER_SIZE) {
|
||||
var dataDownloaded = 0L
|
||||
val fileSize = binaryFile.length
|
||||
UriUtil.getUriOutputStream(contentResolver, attachmentToUploadUri)?.use { outputStream ->
|
||||
Database.getInstance().loadedCipherKey?.let { binaryCipherKey ->
|
||||
binaryFile.getUnGzipInputDataStream(binaryCipherKey).use { inputStream ->
|
||||
inputStream.readAllBytes(bufferSize, canceled) { buffer ->
|
||||
outputStream.write(buffer)
|
||||
dataDownloaded += buffer.size
|
||||
try {
|
||||
val percentDownload = (100 * dataDownloaded / fileSize).toInt()
|
||||
update?.invoke(percentDownload)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "", e)
|
||||
}
|
||||
downloadFromDatabase(outputStream, binaryFile, update, canceled, bufferSize)
|
||||
}
|
||||
}
|
||||
|
||||
fun downloadFromDatabase(outputStream: OutputStream,
|
||||
binaryFile: BinaryFile,
|
||||
update: ((percent: Int)->Unit)? = null,
|
||||
canceled: ()-> Boolean = { false },
|
||||
bufferSize: Int = DEFAULT_BUFFER_SIZE) {
|
||||
val fileSize = binaryFile.length
|
||||
var dataDownloaded = 0L
|
||||
Database.getInstance().loadedCipherKey?.let { binaryCipherKey ->
|
||||
binaryFile.getUnGzipInputDataStream(binaryCipherKey).use { inputStream ->
|
||||
inputStream.readAllBytes(bufferSize, canceled) { buffer ->
|
||||
outputStream.write(buffer)
|
||||
dataDownloaded += buffer.size
|
||||
try {
|
||||
val percentDownload = (100 * dataDownloaded / fileSize).toInt()
|
||||
update?.invoke(percentDownload)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,25 +56,77 @@ object BinaryStreamManager {
|
||||
update: ((percent: Int)->Unit)? = null,
|
||||
canceled: ()-> Boolean = { false },
|
||||
bufferSize: Int = DEFAULT_BUFFER_SIZE) {
|
||||
var dataUploaded = 0L
|
||||
val fileSize = contentResolver.openFileDescriptor(attachmentFromDownloadUri, "r")?.statSize ?: 0
|
||||
UriUtil.getUriInputStream(contentResolver, attachmentFromDownloadUri)?.use { inputStream ->
|
||||
Database.getInstance().loadedCipherKey?.let { binaryCipherKey ->
|
||||
binaryFile.getGzipOutputDataStream(binaryCipherKey).use { outputStream ->
|
||||
inputStream.readAllBytes(bufferSize, canceled) { buffer ->
|
||||
outputStream.write(buffer)
|
||||
dataUploaded += buffer.size
|
||||
try {
|
||||
val percentDownload = (100 * dataUploaded / fileSize).toInt()
|
||||
update?.invoke(percentDownload)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "", e)
|
||||
}
|
||||
uploadToDatabase(inputStream, fileSize, binaryFile, update, canceled, bufferSize)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Better implementation
|
||||
fun uploadToDatabase(inputStream: InputStream,
|
||||
fileSize: Long,
|
||||
binaryFile: BinaryFile,
|
||||
update: ((percent: Int)->Unit)? = null,
|
||||
canceled: ()-> Boolean = { false },
|
||||
bufferSize: Int = DEFAULT_BUFFER_SIZE) {
|
||||
var dataUploaded = 0L
|
||||
Database.getInstance().loadedCipherKey?.let { binaryCipherKey ->
|
||||
binaryFile.getGzipOutputDataStream(binaryCipherKey).use { outputStream ->
|
||||
inputStream.readAllBytes(bufferSize, canceled) { buffer ->
|
||||
outputStream.write(buffer)
|
||||
dataUploaded += buffer.size
|
||||
try {
|
||||
val percentDownload = (100 * dataUploaded / fileSize).toInt()
|
||||
update?.invoke(percentDownload)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun resizeBitmapAndStoreDataInBinaryFile(contentResolver: ContentResolver,
|
||||
bitmapUri: Uri,
|
||||
binaryFile: BinaryFile) {
|
||||
UriUtil.getUriInputStream(contentResolver, bitmapUri)?.use { inputStream ->
|
||||
BitmapFactory.decodeStream(inputStream)?.let { bitmap ->
|
||||
val bitmapResized = bitmap.resize(MAX_ICON_SIZE)
|
||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||
bitmapResized?.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream)
|
||||
val bitmapData: ByteArray = byteArrayOutputStream.toByteArray()
|
||||
val byteArrayInputStream = ByteArrayInputStream(bitmapData)
|
||||
uploadToDatabase(
|
||||
byteArrayInputStream,
|
||||
bitmapData.size.toLong(),
|
||||
binaryFile
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* reduces the size of the image
|
||||
* @param image
|
||||
* @param maxSize
|
||||
* @return
|
||||
*/
|
||||
fun Bitmap.resize(maxSize: Int): Bitmap? {
|
||||
var width = this.width
|
||||
var height = this.height
|
||||
val bitmapRatio = width.toFloat() / height.toFloat()
|
||||
if (bitmapRatio > 1) {
|
||||
width = maxSize
|
||||
height = (width / bitmapRatio).toInt()
|
||||
} else {
|
||||
height = maxSize
|
||||
width = (height * bitmapRatio).toInt()
|
||||
}
|
||||
return Bitmap.createScaledBitmap(this, width, height, true)
|
||||
}
|
||||
|
||||
private const val MAX_ICON_SIZE = 128
|
||||
|
||||
private val TAG = BinaryStreamManager::class.java.name
|
||||
}
|
||||
@@ -7,10 +7,11 @@ import androidx.lifecycle.ViewModel
|
||||
import com.kunzisoft.keepass.database.element.Database
|
||||
import com.kunzisoft.keepass.database.element.icon.IconImageCustom
|
||||
import com.kunzisoft.keepass.database.element.icon.IconImageStandard
|
||||
import com.kunzisoft.keepass.tasks.BinaryStreamManager
|
||||
import com.kunzisoft.keepass.tasks.BinaryStreamManager.resizeBitmapAndStoreDataInBinaryFile
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
|
||||
|
||||
class IconPickerViewModel: ViewModel() {
|
||||
|
||||
private val mainScope = CoroutineScope(Dispatchers.Main)
|
||||
@@ -44,11 +45,8 @@ class IconPickerViewModel: ViewModel() {
|
||||
// on Progress with thread
|
||||
val asyncResult: Deferred<IconImageCustom?> = async {
|
||||
database.buildNewCustomIcon(iconDir)?.let { customIcon ->
|
||||
BinaryStreamManager.uploadToDatabase(
|
||||
iconToUploadUri,
|
||||
customIcon.binaryFile,
|
||||
contentResolver
|
||||
)
|
||||
resizeBitmapAndStoreDataInBinaryFile(contentResolver,
|
||||
iconToUploadUri, customIcon.binaryFile)
|
||||
customIcon
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +137,8 @@
|
||||
<string name="error_field_name_already_exists">The field name already exists.</string>
|
||||
<string name="error_database_uri_null">Database URI cannot be retrieved.</string>
|
||||
<string name="error_rebuild_list">Unable to properly rebuild the list.</string>
|
||||
<string name="error_image_to_big">The image you are trying to upload is too big.</string>
|
||||
<string name="error_file_to_big">The file you are trying to upload is too big.</string>
|
||||
<string name="error_upload_file">An error occurred while uploading the file data.</string>
|
||||
<string name="field_name">Field name</string>
|
||||
<string name="field_value">Field value</string>
|
||||
<string name="file_not_found_content">Could not find file. Try reopening it from your file browser.</string>
|
||||
|
||||
Reference in new issue
Block a user