diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/IconPickerActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/IconPickerActivity.kt index bd9221245..7ce143e36 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/IconPickerActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/IconPickerActivity.kt @@ -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, diff --git a/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryStreamManager.kt b/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryStreamManager.kt index c14ea759a..72dfa56c5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryStreamManager.kt +++ b/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryStreamManager.kt @@ -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 } \ No newline at end of file diff --git a/app/src/main/java/com/kunzisoft/keepass/viewmodels/IconPickerViewModel.kt b/app/src/main/java/com/kunzisoft/keepass/viewmodels/IconPickerViewModel.kt index f31ea80a6..fc6ebeb43 100644 --- a/app/src/main/java/com/kunzisoft/keepass/viewmodels/IconPickerViewModel.kt +++ b/app/src/main/java/com/kunzisoft/keepass/viewmodels/IconPickerViewModel.kt @@ -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 = async { database.buildNewCustomIcon(iconDir)?.let { customIcon -> - BinaryStreamManager.uploadToDatabase( - iconToUploadUri, - customIcon.binaryFile, - contentResolver - ) + resizeBitmapAndStoreDataInBinaryFile(contentResolver, + iconToUploadUri, customIcon.binaryFile) customIcon } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 10eccac11..5c5e8c431 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -137,7 +137,8 @@ The field name already exists. Database URI cannot be retrieved. Unable to properly rebuild the list. - The image you are trying to upload is too big. + The file you are trying to upload is too big. + An error occurred while uploading the file data. Field name Field value Could not find file. Try reopening it from your file browser.