Upload custom attachment with thread

This commit is contained in:
J-Jamet committed 2021-03-01 16:39:46 +01:00
1 parent 5aa3f79616
commit 0e3b8fdbb6
6 files changed
+121 -126

No files matched your search

@@ -20,11 +20,8 @@
package com.kunzisoft.keepass.activities
import android.app.Activity
import android.content.ContentResolver
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
@@ -37,10 +34,8 @@ import com.kunzisoft.keepass.activities.helpers.SelectFileHelper
import com.kunzisoft.keepass.activities.lock.LockingActivity
import com.kunzisoft.keepass.activities.lock.resetAppTimeoutWhenViewFocusedOrChanged
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.database.BinaryFile
import com.kunzisoft.keepass.database.element.icon.IconImage
import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.stream.readAllBytes
import com.kunzisoft.keepass.utils.UriUtil
import com.kunzisoft.keepass.view.updateLockPaddingLeft
import com.kunzisoft.keepass.viewmodels.IconPickerViewModel
@@ -111,6 +106,9 @@ class IconPickerActivity : LockingActivity() {
})
finish()
}
iconPickerViewModel.iconCustomAdded.observe(this) { _ ->
uploadButton.isEnabled = true
}
}
override fun onResume() {
@@ -137,44 +135,6 @@ class IconPickerActivity : LockingActivity() {
return super.onOptionsItemSelected(item)
}
private fun buildNewIcon(iconToUploadUri: Uri) {
mDatabase?.buildNewCustomIcon(UriUtil.getBinaryDir(this))?.let { customIcon ->
uploadIconToDatabase(iconToUploadUri, customIcon.binaryFile, contentResolver,
{
}
)
iconPickerViewModel.addCustomIcon(customIcon)
}
}
// TODO Encapsulate
fun uploadIconToDatabase(iconToUploadUri: Uri,
binaryFile: BinaryFile,
contentResolver: ContentResolver,
update: ((percent: Int)->Unit)? = null,
canceled: ()-> Boolean = { false },
bufferSize: Int = DEFAULT_BUFFER_SIZE,) {
var dataUploaded = 0L
val fileSize = contentResolver.openFileDescriptor(iconToUploadUri, "r")?.statSize ?: 0
UriUtil.getUriInputStream(contentResolver, iconToUploadUri)?.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("", "", e)
}
}
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
@@ -184,7 +144,13 @@ class IconPickerActivity : LockingActivity() {
if (documentFile.length() > MAX_ICON_SIZE) {
// TODO Error Icon size too big
} else {
buildNewIcon(iconToUploadUri)
mDatabase?.let { database ->
iconPickerViewModel.addCustomIcon(database,
contentResolver,
UriUtil.getBinaryDir(this),
iconToUploadUri)
uploadButton.isEnabled = false
}
}
}
}
@@ -29,13 +29,10 @@ import android.util.Log
import androidx.documentfile.provider.DocumentFile
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.Attachment
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.database.BinaryFile
import com.kunzisoft.keepass.model.AttachmentState
import com.kunzisoft.keepass.model.EntryAttachmentState
import com.kunzisoft.keepass.model.StreamDirection
import com.kunzisoft.keepass.stream.readAllBytes
import com.kunzisoft.keepass.utils.UriUtil
import com.kunzisoft.keepass.tasks.BinaryStreamManager
import kotlinx.coroutines.*
import java.util.*
import java.util.concurrent.CopyOnWriteArrayList
@@ -347,24 +344,27 @@ class AttachmentFileNotificationService: LockNotificationService() {
when (streamDirection) {
StreamDirection.UPLOAD -> {
uploadToDatabase(
BinaryStreamManager.uploadToDatabase(
attachmentNotification.uri,
attachment.binaryFile,
contentResolver, 1024,
contentResolver,
{ percent ->
publishProgress(percent)
},
{ // Cancellation
downloadState == AttachmentState.CANCELED
}
) { percent ->
publishProgress(percent)
}
)
}
StreamDirection.DOWNLOAD -> {
downloadFromDatabase(
BinaryStreamManager.downloadFromDatabase(
attachmentNotification.uri,
attachment.binaryFile,
contentResolver, 1024) { percent ->
publishProgress(percent)
}
contentResolver,
{ percent ->
publishProgress(percent)
}
)
}
}
} catch (e: Exception) {
@@ -396,57 +396,6 @@ class AttachmentFileNotificationService: LockNotificationService() {
attachmentNotification.entryAttachmentState.downloadState = AttachmentState.CANCELED
}
fun downloadFromDatabase(attachmentToUploadUri: Uri,
binaryFile: BinaryFile,
contentResolver: ContentResolver,
bufferSize: Int = DEFAULT_BUFFER_SIZE,
update: ((percent: Int)->Unit)? = null) {
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) { buffer ->
outputStream.write(buffer)
dataDownloaded += buffer.size
try {
val percentDownload = (100 * dataDownloaded / fileSize).toInt()
update?.invoke(percentDownload)
} catch (e: Exception) {
Log.e(TAG, "", e)
}
}
}
}
}
}
fun uploadToDatabase(attachmentFromDownloadUri: Uri,
binaryFile: BinaryFile,
contentResolver: ContentResolver,
bufferSize: Int = DEFAULT_BUFFER_SIZE,
canceled: ()-> Boolean = { false },
update: ((percent: Int)->Unit)? = null) {
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)
}
}
}
}
}
}
private fun publishProgress(percent: Int) {
// Publish progress
val currentTime = System.currentTimeMillis()
@@ -0,0 +1,66 @@
package com.kunzisoft.keepass.tasks
import android.content.ContentResolver
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
object BinaryStreamManager {
fun downloadFromDatabase(attachmentToUploadUri: Uri,
binaryFile: BinaryFile,
contentResolver: ContentResolver,
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)
}
}
}
}
}
}
fun uploadToDatabase(attachmentFromDownloadUri: Uri,
binaryFile: BinaryFile,
contentResolver: ContentResolver,
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)
}
}
}
}
}
}
private val TAG = BinaryStreamManager::class.java.name
}
@@ -1,15 +0,0 @@
package com.kunzisoft.keepass.tasks
import android.content.Context
import androidx.work.Worker
import androidx.work.WorkerParameters
class IconUploadWorker(appContext: Context, workerParams: WorkerParameters):
Worker(appContext, workerParams) {
override fun doWork(): Result {
//uploadImages()
return Result.success()
}
}
@@ -1,12 +1,20 @@
package com.kunzisoft.keepass.viewmodels
import android.content.ContentResolver
import android.net.Uri
import androidx.lifecycle.MutableLiveData
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 kotlinx.coroutines.*
import java.io.File
class IconPickerViewModel: ViewModel() {
private val mainScope = CoroutineScope(Dispatchers.Main)
val iconStandardSelected: MutableLiveData<IconImageStandard> by lazy {
MutableLiveData<IconImageStandard>()
}
@@ -27,7 +35,29 @@ class IconPickerViewModel: ViewModel() {
iconCustomSelected.value = icon
}
fun addCustomIcon(icon: IconImageCustom) {
iconCustomAdded.value = icon
fun addCustomIcon(database: Database,
contentResolver: ContentResolver,
iconDir: File,
iconToUploadUri: Uri) {
mainScope.launch {
withContext(Dispatchers.IO) {
// on Progress with thread
val asyncResult: Deferred<IconImageCustom?> = async {
database.buildNewCustomIcon(iconDir)?.let { customIcon ->
BinaryStreamManager.uploadToDatabase(
iconToUploadUri,
customIcon.binaryFile,
contentResolver
)
customIcon
}
}
withContext(Dispatchers.Main) {
asyncResult.await()?.let { customIcon ->
iconCustomAdded.value = customIcon
}
}
}
}
}
}