Fix progress message

This commit is contained in:
J-Jamet
2022-05-19 15:00:12 +02:00
parent b1cb0c3786
commit bc755ae1df
5 changed files with 73 additions and 68 deletions

View File

@@ -47,6 +47,7 @@ import com.kunzisoft.keepass.database.exception.InvalidCredentialsDatabaseExcept
import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.hardware.HardwareKeyResponseHelper
import com.kunzisoft.keepass.model.CipherEncryptDatabase
import com.kunzisoft.keepass.model.ProgressMessage
import com.kunzisoft.keepass.model.SnapFileDatabaseInfo
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_ASSIGN_PASSWORD_TASK
@@ -176,19 +177,13 @@ class DatabaseTaskProvider {
private val actionTaskListener = object: DatabaseTaskNotificationService.ActionTaskListener {
override fun onStartAction(database: Database,
titleId: Int?,
messageId: Int?,
warningId: Int?,
cancelable: (() -> Unit)?) {
startDialog(titleId, messageId, warningId, cancelable)
progressMessage: ProgressMessage) {
startDialog(progressMessage)
}
override fun onUpdateAction(database: Database,
titleId: Int?,
messageId: Int?,
warningId: Int?,
cancelable: (() -> Unit)?) {
updateDialog(titleId, messageId, warningId, cancelable)
progressMessage: ProgressMessage) {
updateDialog(progressMessage)
}
override fun onStopAction(database: Database,
@@ -242,10 +237,7 @@ class DatabaseTaskProvider {
private var requestChallengeListener: DatabaseTaskNotificationService.RequestChallengeListener? = null
private fun startDialog(titleId: Int?,
messageId: Int?,
warningId: Int?,
cancelable: (() -> Unit)?) {
private fun startDialog(progressMessage: ProgressMessage) {
activity?.let { activity ->
activity.lifecycleScope.launch {
if (progressTaskDialogFragment == null) {
@@ -259,26 +251,17 @@ class DatabaseTaskProvider {
PROGRESS_TASK_DIALOG_TAG
)
}
updateDialog(titleId, messageId, warningId, cancelable)
updateDialog(progressMessage)
}
}
}
private fun updateDialog(titleId: Int?,
messageId: Int?,
warningId: Int?,
cancelable: (() -> Unit)?) {
private fun updateDialog(progressMessage: ProgressMessage) {
progressTaskDialogFragment?.apply {
titleId?.let {
updateTitle(it)
}
messageId?.let {
updateMessage(it)
}
warningId?.let {
updateWarning(it)
}
setCancellable(cancelable)
updateTitle(progressMessage.titleId)
updateMessage(progressMessage.messageId)
updateWarning(progressMessage.warningId)
setCancellable(progressMessage.cancelable)
}
}

View File

@@ -0,0 +1,13 @@
package com.kunzisoft.keepass.model
import androidx.annotation.StringRes
data class ProgressMessage(
@StringRes
var titleId: Int,
@StringRes
var messageId: Int? = null,
@StringRes
var warningId: Int? = null,
var cancelable: (() -> Unit)? = null
)

View File

@@ -45,6 +45,7 @@ import com.kunzisoft.keepass.database.element.node.NodeId
import com.kunzisoft.keepass.database.element.node.Type
import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.model.CipherEncryptDatabase
import com.kunzisoft.keepass.model.ProgressMessage
import com.kunzisoft.keepass.model.SnapFileDatabaseInfo
import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
@@ -79,10 +80,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
private var mCreationState = false
private var mIconId: Int = R.drawable.notification_ic_database_load
private var mTitleId: Int = R.string.database_opened
private var mMessageId: Int? = null
private var mWarningId: Int? = null
private var mCancelable: (() -> Unit)? = null
private var mProgressMessage: ProgressMessage = ProgressMessage(R.string.database_opened)
override fun retrieveChannelId(): String {
return CHANNEL_DATABASE_ID
@@ -154,15 +152,9 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
interface ActionTaskListener {
fun onStartAction(database: Database,
titleId: Int?,
messageId: Int?,
warningId: Int?,
cancelable: (() -> Unit)? = null)
progressMessage: ProgressMessage)
fun onUpdateAction(database: Database,
titleId: Int?,
messageId: Int?,
warningId: Int?,
cancelable: (() -> Unit)? = null)
progressMessage: ProgressMessage)
fun onStopAction(database: Database,
actionTask: String,
result: ActionRunnable.Result)
@@ -239,7 +231,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
if (mActionRunning) {
mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onStartAction(
database, mTitleId, mMessageId, mWarningId, mCancelable
database, mProgressMessage
)
}
}
@@ -354,14 +346,14 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
mActionRunning = true
sendBroadcast(Intent(DATABASE_START_TASK_ACTION).apply {
putExtra(DATABASE_TASK_TITLE_KEY, mTitleId)
putExtra(DATABASE_TASK_MESSAGE_KEY, mMessageId)
putExtra(DATABASE_TASK_WARNING_KEY, mWarningId)
putExtra(DATABASE_TASK_TITLE_KEY, mProgressMessage.titleId)
putExtra(DATABASE_TASK_MESSAGE_KEY, mProgressMessage.messageId)
putExtra(DATABASE_TASK_WARNING_KEY, mProgressMessage.warningId)
})
mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onStartAction(
database, mTitleId, mMessageId, mWarningId, mCancelable
database, mProgressMessage
)
}
@@ -451,7 +443,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
else
R.drawable.notification_ic_database_load
mTitleId = when {
mProgressMessage.titleId = when {
saveAction -> {
R.string.saving_database
}
@@ -476,14 +468,14 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
}
}
mMessageId = when (intentAction) {
mProgressMessage.messageId = when (intentAction) {
ACTION_DATABASE_LOAD_TASK,
ACTION_DATABASE_MERGE_TASK,
ACTION_DATABASE_RELOAD_TASK -> null
else -> null
}
mWarningId =
mProgressMessage.warningId =
if (!saveAction
|| intentAction == ACTION_DATABASE_LOAD_TASK
|| intentAction == ACTION_DATABASE_MERGE_TASK
@@ -495,7 +487,9 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
val notificationBuilder = buildNewNotification().apply {
setSmallIcon(mIconId)
intent?.let {
setContentTitle(getString(intent.getIntExtra(DATABASE_TASK_TITLE_KEY, mTitleId)))
setContentTitle(getString(
intent.getIntExtra(DATABASE_TASK_TITLE_KEY, mProgressMessage.titleId))
)
}
setAutoCancel(false)
setContentIntent(null)
@@ -601,17 +595,26 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
}
}
override fun updateMessage(resId: Int) {
mMessageId = resId
private fun notifyProgressMessage() {
mDatabase?.let { database ->
mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onUpdateAction(
database, mTitleId, mMessageId, mWarningId, mCancelable
database, mProgressMessage
)
}
}
}
override fun updateMessage(progressMessage: ProgressMessage) {
mProgressMessage = progressMessage
notifyProgressMessage()
}
override fun updateMessage(resId: Int) {
mProgressMessage.messageId = resId
notifyProgressMessage()
}
override fun actionOnLock() {
if (!TimeoutHelper.temporarilyDisableLock) {
closeDatabase(mDatabase)
@@ -636,24 +639,28 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
runBlocking {
// Initialize the channels
initializeChallengeResponse()
val previousMessageId = mMessageId
mCancelable = {
cancelChallengeResponse(R.string.error_cancel_by_user)
val previousMessage = mProgressMessage.copy()
mProgressMessage.apply {
messageId = R.string.waiting_challenge_request
cancelable = {
cancelChallengeResponse(R.string.error_cancel_by_user)
}
}
// Send the request
updateMessage(R.string.waiting_challenge_request)
updateMessage(mProgressMessage)
val challengeResponseRequestListener = mRequestChallengeListenerChannel?.receive()
challengeResponseRequestListener?.onChallengeResponseRequested(hardwareKey, seed)
// Wait the response
updateMessage(R.string.waiting_challenge_response)
mProgressMessage.apply {
messageId = R.string.waiting_challenge_response
}
updateMessage(mProgressMessage)
response = mResponseChallengeChannel?.receive() ?: byteArrayOf()
// Close channels
closeChallengeResponse()
// Restore previous message
mCancelable = null
previousMessageId?.let {
updateMessage(it)
}
mProgressMessage.cancelable = null
updateMessage(previousMessage)
}
return response
}

View File

@@ -110,18 +110,18 @@ open class ProgressTaskDialogFragment : DialogFragment() {
}
}
fun updateTitle(@StringRes resId: Int) {
this.title = resId
fun updateTitle(@StringRes resId: Int?) {
this.title = resId ?: UNDEFINED
updateView(titleView, title)
}
fun updateMessage(@StringRes resId: Int) {
this.message = resId
fun updateMessage(@StringRes resId: Int?) {
this.message = resId ?: UNDEFINED
updateView(messageView, message)
}
fun updateWarning(@StringRes resId: Int) {
this.warning = resId
fun updateWarning(@StringRes resId: Int?) {
this.warning = resId ?: UNDEFINED
updateView(warningView, warning)
}

View File

@@ -20,7 +20,9 @@
package com.kunzisoft.keepass.tasks
import androidx.annotation.StringRes
import com.kunzisoft.keepass.model.ProgressMessage
interface ProgressTaskUpdater {
fun updateMessage(progressMessage: ProgressMessage)
fun updateMessage(@StringRes resId: Int)
}