Add waiting task message and cancellable

This commit is contained in:
J-Jamet committed 2022-05-18 19:49:18 +02:00
1 parent 344118a755
commit cbde96dd82
5 files changed
+93 -20

No files matched your search

@@ -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)
}
}
@@ -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
}
@@ -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"
@@ -58,6 +58,16 @@
android:layout_marginEnd="20dp"
style="@style/KeepassDXStyle.TextAppearance.Warning"/>
<Button
android:id="@+id/progress_dialog_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:text="@string/entry_cancel" />
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/progress_dialog_bar"
app:indicatorColor="?attr/colorAccent"
+3
View File
@@ -79,6 +79,8 @@
<string name="content_description_keyboard_close_fields">Close fields</string>
<string name="select_to_copy">Select to copy %1$s to clipboard</string>
<string name="retrieving_db_key">Retrieving database key…</string>
<string name="waiting_challenge_request">Waiting for the challenge request…</string>
<string name="waiting_challenge_response">Waiting for the challenge response…</string>
<string name="database">Database</string>
<string name="template_group_name">Templates</string>
<string name="decrypting_db">Decrypting database content…</string>
@@ -198,6 +200,7 @@
<string name="error_challenge_already_requested">"Challenge already requested"</string>
<string name="error_response_already_provided">Response already provided.</string>
<string name="error_no_response_from_challenge">Unable to get the response from the challenge.</string>
<string name="error_cancel_by_user">Cancelled by user.</string>
<string name="field_name">Field name</string>
<string name="field_value">Field value</string>
<string name="file_not_found_content">Could not find file. Try reopening it from your file browser.</string>