mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Better thread callback encapsulation
This commit is contained in:
@@ -55,6 +55,8 @@ import com.kunzisoft.keepass.database.element.Database
|
||||
import com.kunzisoft.keepass.education.FileDatabaseSelectActivityEducation
|
||||
import com.kunzisoft.keepass.model.SearchInfo
|
||||
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CREATE_TASK
|
||||
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.DATABASE_URI_KEY
|
||||
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.KEY_FILE_URI_KEY
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.*
|
||||
import com.kunzisoft.keepass.view.asError
|
||||
@@ -169,11 +171,14 @@ class FileDatabaseSelectActivity : SpecialModeActivity(),
|
||||
}
|
||||
DatabaseFilesViewModel.DatabaseFileAction.ADD -> {
|
||||
databaseFiles.databaseFileToActivate?.let { databaseFileToAdd ->
|
||||
// TODO notify unique element
|
||||
mAdapterDatabaseHistory?.notifyDataSetChanged()
|
||||
}
|
||||
GroupActivity.launch(this@FileDatabaseSelectActivity)
|
||||
}
|
||||
DatabaseFilesViewModel.DatabaseFileAction.UPDATE -> {
|
||||
databaseFiles.databaseFileToActivate?.let { databaseFileToUpdate ->
|
||||
// TODO notify unique element
|
||||
mAdapterDatabaseHistory?.notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
@@ -189,14 +194,12 @@ class FileDatabaseSelectActivity : SpecialModeActivity(),
|
||||
|
||||
// Attach the dialog thread to this activity
|
||||
mProgressDatabaseTaskProvider = ProgressDatabaseTaskProvider(this).apply {
|
||||
onActionFinish = { actionTask, _ ->
|
||||
onActionFinish = { actionTask, result ->
|
||||
when (actionTask) {
|
||||
ACTION_DATABASE_CREATE_TASK -> {
|
||||
// TODO add Database file
|
||||
// databaseFilesViewModel.addDatabaseFile()
|
||||
databaseFilesViewModel.loadListOfDatabases()
|
||||
runOnUiThread {
|
||||
GroupActivity.launch(this@FileDatabaseSelectActivity)
|
||||
result.data?.getParcelable<Uri?>(DATABASE_URI_KEY)?.let { databaseUri ->
|
||||
val keyFileUri = result.data?.getParcelable<Uri?>(KEY_FILE_URI_KEY)
|
||||
databaseFilesViewModel.addDatabaseFile(databaseUri, keyFileUri)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,30 +106,16 @@ class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
).execute()
|
||||
}
|
||||
|
||||
fun addOrUpdateDatabaseUri(databaseUri: Uri, keyFileUri: Uri? = null) {
|
||||
IOActionTask(
|
||||
{
|
||||
val fileDatabaseHistoryRetrieve = databaseFileHistoryDao.getByDatabaseUri(databaseUri.toString())
|
||||
|
||||
val fileDatabaseHistory = FileDatabaseHistoryEntity(
|
||||
databaseUri.toString(),
|
||||
fileDatabaseHistoryRetrieve?.databaseAlias ?: "",
|
||||
keyFileUri?.toString(),
|
||||
System.currentTimeMillis()
|
||||
)
|
||||
|
||||
// Update values if history element not yet in the database
|
||||
if (fileDatabaseHistoryRetrieve == null) {
|
||||
databaseFileHistoryDao.add(fileDatabaseHistory)
|
||||
} else {
|
||||
databaseFileHistoryDao.update(fileDatabaseHistory)
|
||||
}
|
||||
}
|
||||
).execute()
|
||||
fun addOrUpdateDatabaseUri(databaseUri: Uri, keyFileUri: Uri? = null,
|
||||
databaseFileAddedOrUpdatedResult: ((DatabaseFile?) -> Unit)? = null) {
|
||||
addOrUpdateDatabaseFile(DatabaseFile(
|
||||
databaseUri,
|
||||
keyFileUri
|
||||
), databaseFileAddedOrUpdatedResult)
|
||||
}
|
||||
|
||||
fun addOrUpdateDatabaseFile(databaseFileToAddOrUpdate: DatabaseFile,
|
||||
databaseFileToAddedOrUpdatedResult: ((DatabaseFile?) -> Unit)? = null) {
|
||||
databaseFileAddedOrUpdatedResult: ((DatabaseFile?) -> Unit)? = null) {
|
||||
IOActionTask(
|
||||
{
|
||||
databaseFileToAddOrUpdate.databaseUri?.let { databaseUri ->
|
||||
@@ -163,7 +149,7 @@ class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
}
|
||||
},
|
||||
{
|
||||
databaseFileToAddedOrUpdatedResult?.invoke(it)
|
||||
databaseFileAddedOrUpdatedResult?.invoke(it)
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ open class AssignPasswordInDatabaseRunnable (
|
||||
: SaveDatabaseRunnable(context, database, true) {
|
||||
|
||||
private var mMasterPassword: String? = null
|
||||
protected var mKeyFile: Uri? = null
|
||||
protected var mKeyFileUri: Uri? = null
|
||||
|
||||
private var mBackupKey: ByteArray? = null
|
||||
|
||||
@@ -45,7 +45,7 @@ open class AssignPasswordInDatabaseRunnable (
|
||||
if (withMasterPassword)
|
||||
this.mMasterPassword = masterPassword
|
||||
if (withKeyFile)
|
||||
this.mKeyFile = keyFile
|
||||
this.mKeyFileUri = keyFile
|
||||
}
|
||||
|
||||
override fun onStartRun() {
|
||||
@@ -55,7 +55,7 @@ open class AssignPasswordInDatabaseRunnable (
|
||||
mBackupKey = ByteArray(database.masterKey.size)
|
||||
System.arraycopy(database.masterKey, 0, mBackupKey!!, 0, mBackupKey!!.size)
|
||||
|
||||
val uriInputStream = UriUtil.getUriInputStream(context.contentResolver, mKeyFile)
|
||||
val uriInputStream = UriUtil.getUriInputStream(context.contentResolver, mKeyFileUri)
|
||||
database.retrieveMasterKey(mMasterPassword, uriInputStream)
|
||||
} catch (e: Exception) {
|
||||
erase(mBackupKey)
|
||||
|
||||
@@ -34,7 +34,8 @@ class CreateDatabaseRunnable(context: Context,
|
||||
withMasterPassword: Boolean,
|
||||
masterPassword: String?,
|
||||
withKeyFile: Boolean,
|
||||
keyFile: Uri?)
|
||||
keyFile: Uri?,
|
||||
private val createDatabaseResult: ((Result) -> Unit)?)
|
||||
: AssignPasswordInDatabaseRunnable(context, mDatabase, databaseUri, withMasterPassword, masterPassword, withKeyFile, keyFile) {
|
||||
|
||||
override fun onStartRun() {
|
||||
@@ -61,10 +62,16 @@ class CreateDatabaseRunnable(context: Context,
|
||||
if (PreferencesUtil.rememberDatabaseLocations(context)) {
|
||||
FileDatabaseHistoryAction.getInstance(context.applicationContext)
|
||||
.addOrUpdateDatabaseUri(mDatabaseUri,
|
||||
if (PreferencesUtil.rememberKeyFileLocations(context)) mKeyFile else null)
|
||||
if (PreferencesUtil.rememberKeyFileLocations(context)) mKeyFileUri else null)
|
||||
}
|
||||
} else {
|
||||
Log.e("CreateDatabaseRunnable", "Unable to create the database")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFinishRun() {
|
||||
super.onFinishRun()
|
||||
|
||||
createDatabaseResult?.invoke(result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class LoadDatabaseRunnable(private val context: Context,
|
||||
private val mCipherEntity: CipherDatabaseEntity?,
|
||||
private val mFixDuplicateUUID: Boolean,
|
||||
private val progressTaskUpdater: ProgressTaskUpdater?,
|
||||
private val mDuplicateUuidAction: ((Result) -> Unit)?)
|
||||
private val mLoadDatabaseResult: ((Result) -> Unit)?)
|
||||
: ActionRunnable() {
|
||||
|
||||
private val cacheDirectory = context.applicationContext.filesDir
|
||||
@@ -60,7 +60,6 @@ class LoadDatabaseRunnable(private val context: Context,
|
||||
progressTaskUpdater)
|
||||
}
|
||||
catch (e: DuplicateUuidDatabaseException) {
|
||||
mDuplicateUuidAction?.invoke(result)
|
||||
setError(e)
|
||||
}
|
||||
catch (e: LoadDatabaseException) {
|
||||
@@ -89,6 +88,6 @@ class LoadDatabaseRunnable(private val context: Context,
|
||||
}
|
||||
|
||||
override fun onFinishRun() {
|
||||
|
||||
mLoadDatabaseResult?.invoke(result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -397,7 +397,12 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
|
||||
intent.getStringExtra(MASTER_PASSWORD_KEY),
|
||||
intent.getBooleanExtra(KEY_FILE_CHECKED_KEY, false),
|
||||
keyFileUri
|
||||
)
|
||||
) { result ->
|
||||
result.data = Bundle().apply {
|
||||
putParcelable(DATABASE_URI_KEY, databaseUri)
|
||||
putParcelable(KEY_FILE_URI_KEY, keyFileUri)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
@@ -766,7 +771,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
|
||||
const val MASTER_PASSWORD_CHECKED_KEY = "MASTER_PASSWORD_CHECKED_KEY"
|
||||
const val MASTER_PASSWORD_KEY = "MASTER_PASSWORD_KEY"
|
||||
const val KEY_FILE_CHECKED_KEY = "KEY_FILE_CHECKED_KEY"
|
||||
const val KEY_FILE_URI_KEY = "KEY_FILE_KEY"
|
||||
const val KEY_FILE_URI_KEY = "KEY_FILE_URI_KEY"
|
||||
const val READ_ONLY_KEY = "READ_ONLY_KEY"
|
||||
const val CIPHER_ENTITY_KEY = "CIPHER_ENTITY_KEY"
|
||||
const val FIX_DUPLICATE_UUID_KEY = "FIX_DUPLICATE_UUID_KEY"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.kunzisoft.keepass.viewmodels
|
||||
|
||||
import android.app.Application
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
|
||||
@@ -36,38 +37,33 @@ class DatabaseFilesViewModel(application: Application) : AndroidViewModel(applic
|
||||
}
|
||||
}
|
||||
|
||||
fun addDatabaseFile(databaseFileToAdd: DatabaseFile) {
|
||||
addOrUpdateDatabaseFile(databaseFileToAdd, DatabaseFileAction.ADD)
|
||||
fun addDatabaseFile(databaseUri: Uri, keyFileUri: Uri?) {
|
||||
mFileDatabaseHistoryAction?.addOrUpdateDatabaseUri(databaseUri, keyFileUri) { databaseFileAdded ->
|
||||
databaseFileAdded?.let { _ ->
|
||||
databaseFilesLoaded.value = databaseFilesLoaded.value?.apply {
|
||||
this.databaseFileAction = DatabaseFileAction.ADD
|
||||
this.databaseFileList.add(databaseFileAdded)
|
||||
this.databaseFileToActivate = databaseFileAdded
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateDatabaseFile(databaseFileToUpdate: DatabaseFile) {
|
||||
addOrUpdateDatabaseFile(databaseFileToUpdate, DatabaseFileAction.UPDATE)
|
||||
}
|
||||
|
||||
private fun addOrUpdateDatabaseFile(databaseFileToAddOrUpdate: DatabaseFile,
|
||||
databaseFileAction: DatabaseFileAction) {
|
||||
mFileDatabaseHistoryAction?.addOrUpdateDatabaseFile(databaseFileToAddOrUpdate) { databaseFileAddedOrUpdated ->
|
||||
databaseFileAddedOrUpdated?.let { _ ->
|
||||
mFileDatabaseHistoryAction?.addOrUpdateDatabaseFile(databaseFileToUpdate) { databaseFileUpdated ->
|
||||
databaseFileUpdated?.let { _ ->
|
||||
databaseFilesLoaded.value = databaseFilesLoaded.value?.apply {
|
||||
this.databaseFileAction = databaseFileAction
|
||||
when (databaseFileAction) {
|
||||
DatabaseFileAction.ADD -> {
|
||||
databaseFileList.add(databaseFileAddedOrUpdated)
|
||||
}
|
||||
DatabaseFileAction.UPDATE -> {
|
||||
databaseFileList
|
||||
.find { it.databaseUri == databaseFileAddedOrUpdated.databaseUri }
|
||||
this.databaseFileAction = DatabaseFileAction.UPDATE
|
||||
this.databaseFileList
|
||||
.find { it.databaseUri == databaseFileUpdated.databaseUri }
|
||||
?.apply {
|
||||
keyFileUri = databaseFileAddedOrUpdated.keyFileUri
|
||||
databaseAlias = databaseFileAddedOrUpdated.databaseAlias
|
||||
databaseFileExists = databaseFileAddedOrUpdated.databaseFileExists
|
||||
databaseLastModified = databaseFileAddedOrUpdated.databaseLastModified
|
||||
databaseSize = databaseFileAddedOrUpdated.databaseSize
|
||||
keyFileUri = databaseFileUpdated.keyFileUri
|
||||
databaseAlias = databaseFileUpdated.databaseAlias
|
||||
databaseFileExists = databaseFileUpdated.databaseFileExists
|
||||
databaseLastModified = databaseFileUpdated.databaseLastModified
|
||||
databaseSize = databaseFileUpdated.databaseSize
|
||||
}
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
this.databaseFileToActivate = databaseFileAddedOrUpdated
|
||||
this.databaseFileToActivate = databaseFileUpdated
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user