From cbde96dd82f8d12c415a2bbdd80bc3aa882ccaff Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Wed, 18 May 2022 19:49:18 +0200 Subject: [PATCH] Add waiting task message and cancellable --- .../database/action/DatabaseTaskProvider.kt | 35 ++++++++++---- .../DatabaseTaskNotificationService.kt | 46 ++++++++++++++++--- .../tasks/ProgressTaskDialogFragment.kt | 19 ++++++-- app/src/main/res/layout/fragment_progress.xml | 10 ++++ app/src/main/res/values/strings.xml | 3 ++ 5 files changed, 93 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/DatabaseTaskProvider.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/DatabaseTaskProvider.kt index c52d4d63d..1e3f88986 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/DatabaseTaskProvider.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/DatabaseTaskProvider.kt @@ -158,15 +158,25 @@ class DatabaseTaskProvider { } private val actionTaskListener = object: DatabaseTaskNotificationService.ActionTaskListener { - override fun onStartAction(database: Database, titleId: Int?, messageId: Int?, warningId: Int?) { - startDialog(titleId, messageId, warningId) + override fun onStartAction(database: Database, + titleId: Int?, + messageId: Int?, + warningId: Int?, + cancelable: (() -> Unit)?) { + startDialog(titleId, messageId, warningId, cancelable) } - override fun onUpdateAction(database: Database, titleId: Int?, messageId: Int?, warningId: Int?) { - updateDialog(titleId, messageId, warningId) + override fun onUpdateAction(database: Database, + titleId: Int?, + messageId: Int?, + warningId: Int?, + cancelable: (() -> Unit)?) { + updateDialog(titleId, messageId, warningId, cancelable) } - override fun onStopAction(database: Database, actionTask: String, result: ActionRunnable.Result) { + override fun onStopAction(database: Database, + actionTask: String, + result: ActionRunnable.Result) { onActionFinish?.invoke(database, actionTask, result) // Remove the progress task stopDialog() @@ -233,9 +243,10 @@ class DatabaseTaskProvider { } } - private fun startDialog(titleId: Int? = null, - messageId: Int? = null, - warningId: Int? = null) { + private fun startDialog(titleId: Int?, + messageId: Int?, + warningId: Int?, + cancelable: (() -> Unit)?) { activity?.let { activity -> activity.lifecycleScope.launch { if (progressTaskDialogFragment == null) { @@ -249,12 +260,15 @@ class DatabaseTaskProvider { PROGRESS_TASK_DIALOG_TAG ) } - updateDialog(titleId, messageId, warningId) + updateDialog(titleId, messageId, warningId, cancelable) } } } - private fun updateDialog(titleId: Int?, messageId: Int?, warningId: Int?) { + private fun updateDialog(titleId: Int?, + messageId: Int?, + warningId: Int?, + cancelable: (() -> Unit)?) { progressTaskDialogFragment?.apply { titleId?.let { updateTitle(it) @@ -265,6 +279,7 @@ class DatabaseTaskProvider { warningId?.let { updateWarning(it) } + setCancellable(cancelable) } } diff --git a/app/src/main/java/com/kunzisoft/keepass/services/DatabaseTaskNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/services/DatabaseTaskNotificationService.kt index e2217d3b2..099367c6c 100644 --- a/app/src/main/java/com/kunzisoft/keepass/services/DatabaseTaskNotificationService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/services/DatabaseTaskNotificationService.kt @@ -81,7 +81,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress private var mTitleId: Int = R.string.database_opened private var mMessageId: Int? = null private var mWarningId: Int? = null - + private var mCancelable: (() -> Unit)? = null override fun retrieveChannelId(): String { return CHANNEL_DATABASE_ID @@ -151,9 +151,19 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress } interface ActionTaskListener { - fun onStartAction(database: Database, titleId: Int?, messageId: Int?, warningId: Int?) - fun onUpdateAction(database: Database, titleId: Int?, messageId: Int?, warningId: Int?) - fun onStopAction(database: Database, actionTask: String, result: ActionRunnable.Result) + fun onStartAction(database: Database, + titleId: Int?, + messageId: Int?, + warningId: Int?, + cancelable: (() -> Unit)? = null) + fun onUpdateAction(database: Database, + titleId: Int?, + messageId: Int?, + warningId: Int?, + cancelable: (() -> Unit)? = null) + fun onStopAction(database: Database, + actionTask: String, + result: ActionRunnable.Result) } interface RequestChallengeListener { @@ -226,7 +236,9 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress mDatabase?.let { database -> if (mActionRunning) { mActionTaskListeners.forEach { actionTaskListener -> - actionTaskListener.onStartAction(database, mTitleId, mMessageId, mWarningId) + actionTaskListener.onStartAction( + database, mTitleId, mMessageId, mWarningId, mCancelable + ) } } } @@ -332,7 +344,9 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress }) mActionTaskListeners.forEach { actionTaskListener -> - actionTaskListener.onStartAction(database, mTitleId, mMessageId, mWarningId) + actionTaskListener.onStartAction( + database, mTitleId, mMessageId, mWarningId, mCancelable + ) } }, @@ -575,7 +589,9 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress mMessageId = resId mDatabase?.let { database -> mActionTaskListeners.forEach { actionTaskListener -> - actionTaskListener.onUpdateAction(database, mTitleId, mMessageId, mWarningId) + actionTaskListener.onUpdateAction( + database, mTitleId, mMessageId, mWarningId, mCancelable + ) } } } @@ -603,10 +619,26 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress var response: ByteArray runBlocking { // Send the request + val previousMessageId = mMessageId + mCancelable = { + mRequestChallengeListenerChannel.cancel(CancellationException(getString(R.string.error_cancel_by_user))) + mRequestChallengeListenerChannel = Channel(0) + } + updateMessage(R.string.waiting_challenge_request) val challengeResponseRequestListener = mRequestChallengeListenerChannel.receive() challengeResponseRequestListener.onChallengeResponseRequested(hardwareKey, seed) // Wait the response + mCancelable = { + mResponseChallengeChannel.cancel(CancellationException(getString(R.string.error_cancel_by_user))) + mResponseChallengeChannel = Channel(0) + } + updateMessage(R.string.waiting_challenge_response) response = mResponseChallengeChannel.receive() ?: byteArrayOf() + // Restore previous message + mCancelable = null + previousMessageId?.let { + updateMessage(it) + } } return response } diff --git a/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.kt b/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.kt index 26bb26565..91f037367 100644 --- a/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.kt +++ b/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.kt @@ -24,15 +24,16 @@ import android.app.Dialog import android.os.Bundle import android.util.Log import android.view.View +import android.widget.Button import android.widget.ProgressBar import android.widget.TextView import androidx.annotation.StringRes import androidx.appcompat.app.AlertDialog +import androidx.core.view.isVisible import androidx.fragment.app.DialogFragment import com.kunzisoft.keepass.R -import java.lang.Exception -open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater { +open class ProgressTaskDialogFragment : DialogFragment() { @StringRes private var title = UNDEFINED @@ -40,10 +41,12 @@ open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater { private var message = UNDEFINED @StringRes private var warning = UNDEFINED + private var cancellable: (() -> Unit)? = null private var titleView: TextView? = null private var messageView: TextView? = null private var warningView: TextView? = null + private var cancelButton: Button? = null private var progressView: ProgressBar? = null override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { @@ -63,11 +66,13 @@ open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater { titleView = root.findViewById(R.id.progress_dialog_title) messageView = root.findViewById(R.id.progress_dialog_message) warningView = root.findViewById(R.id.progress_dialog_warning) + cancelButton = root.findViewById(R.id.progress_dialog_cancel) progressView = root.findViewById(R.id.progress_dialog_bar) updateTitle(title) updateMessage(message) updateWarning(warning) + setCancellable(cancellable) isCancelable = false @@ -99,7 +104,7 @@ open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater { updateView(titleView, title) } - override fun updateMessage(@StringRes resId: Int) { + fun updateMessage(@StringRes resId: Int) { this.message = resId updateView(messageView, message) } @@ -109,6 +114,14 @@ open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater { updateView(warningView, warning) } + fun setCancellable(cancellable: (() -> Unit)?) { + this.cancellable = cancellable + cancelButton?.isVisible = cancellable != null + cancelButton?.setOnClickListener { + cancellable?.invoke() + } + } + companion object { private val TAG = ProgressTaskDialogFragment::class.java.simpleName const val PROGRESS_TASK_DIALOG_TAG = "progressDialogFragment" diff --git a/app/src/main/res/layout/fragment_progress.xml b/app/src/main/res/layout/fragment_progress.xml index b3f79e265..6edd632d2 100644 --- a/app/src/main/res/layout/fragment_progress.xml +++ b/app/src/main/res/layout/fragment_progress.xml @@ -58,6 +58,16 @@ android:layout_marginEnd="20dp" style="@style/KeepassDXStyle.TextAppearance.Warning"/> +