fix: Loading dialog with Yubikey #1506

This commit is contained in:
J-Jamet
2023-03-26 14:46:11 +02:00
parent 2b23649674
commit 02f6caf4dd
2 changed files with 123 additions and 89 deletions

View File

@@ -128,25 +128,29 @@ class DatabaseTaskProvider(private var context: Context,
} }
private val actionTaskListener = object: DatabaseTaskNotificationService.ActionTaskListener { private val actionTaskListener = object: DatabaseTaskNotificationService.ActionTaskListener {
override fun onStartAction(database: Database, override fun onActionStarted(database: Database,
progressMessage: ProgressMessage) { progressMessage: ProgressMessage) {
if (showDialog) if (showDialog)
startDialog(progressMessage) startDialog(progressMessage)
} }
override fun onUpdateAction(database: Database, override fun onActionUpdated(database: Database,
progressMessage: ProgressMessage) { progressMessage: ProgressMessage) {
if (showDialog) if (showDialog)
updateDialog(progressMessage) updateDialog(progressMessage)
} }
override fun onStopAction(database: Database, override fun onActionStopped(database: Database) {
actionTask: String,
result: ActionRunnable.Result) {
onActionFinish?.invoke(database, actionTask, result)
// Remove the progress task // Remove the progress task
stopDialog() stopDialog()
} }
override fun onActionFinished(database: Database,
actionTask: String,
result: ActionRunnable.Result) {
onActionFinish?.invoke(database, actionTask, result)
onActionStopped(database)
}
} }
private val mActionDatabaseListener = object: DatabaseChangedDialogFragment.ActionDatabaseChangedListener { private val mActionDatabaseListener = object: DatabaseChangedDialogFragment.ActionDatabaseChangedListener {
@@ -225,6 +229,16 @@ class DatabaseTaskProvider(private var context: Context,
private fun initServiceConnection() { private fun initServiceConnection() {
if (serviceConnection == null) { if (serviceConnection == null) {
serviceConnection = object : ServiceConnection { serviceConnection = object : ServiceConnection {
override fun onBindingDied(name: ComponentName?) {
stopDialog()
super.onBindingDied(name)
}
override fun onNullBinding(name: ComponentName?) {
stopDialog()
super.onNullBinding(name)
}
override fun onServiceConnected(name: ComponentName?, serviceBinder: IBinder?) { override fun onServiceConnected(name: ComponentName?, serviceBinder: IBinder?) {
mBinder = (serviceBinder as DatabaseTaskNotificationService.ActionTaskBinder?)?.apply { mBinder = (serviceBinder as DatabaseTaskNotificationService.ActionTaskBinder?)?.apply {
addServiceListeners(this) addServiceListeners(this)
@@ -272,8 +286,6 @@ class DatabaseTaskProvider(private var context: Context,
} }
fun registerProgressTask() { fun registerProgressTask() {
stopDialog()
// Register a database task receiver to stop loading dialog when service finish the task // Register a database task receiver to stop loading dialog when service finish the task
databaseTaskBroadcastReceiver = object : BroadcastReceiver() { databaseTaskBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
@@ -284,7 +296,6 @@ class DatabaseTaskProvider(private var context: Context,
} }
DATABASE_STOP_TASK_ACTION -> { DATABASE_STOP_TASK_ACTION -> {
// Remove the progress task // Remove the progress task
stopDialog()
unBindService() unBindService()
} }
} }
@@ -302,8 +313,6 @@ class DatabaseTaskProvider(private var context: Context,
} }
fun unregisterProgressTask() { fun unregisterProgressTask() {
stopDialog()
removeServiceListeners(mBinder) removeServiceListeners(mBinder)
mBinder = null mBinder = null

View File

@@ -79,7 +79,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
// Channel to connect asynchronously a response // Channel to connect asynchronously a response
private var mResponseChallengeChannel: Channel<ByteArray?>? = null private var mResponseChallengeChannel: Channel<ByteArray?>? = null
private var mActionRunning = false private var mActionRunning = 0
private var mTaskRemovedRequested = false private var mTaskRemovedRequested = false
private var mSaveState = false private var mSaveState = false
@@ -135,20 +135,14 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
} }
interface ActionTaskListener { interface ActionTaskListener {
fun onStartAction(database: Database, fun onActionStarted(database: Database,
progressMessage: ProgressMessage) progressMessage: ProgressMessage)
fun onUpdateAction(database: Database, fun onActionUpdated(database: Database,
progressMessage: ProgressMessage) progressMessage: ProgressMessage)
fun onStopAction(database: Database, fun onActionStopped(database: Database)
actionTask: String, fun onActionFinished(database: Database,
result: ActionRunnable.Result) actionTask: String,
} result: ActionRunnable.Result)
interface RequestChallengeListener {
fun onChallengeResponseRequested(
hardwareKey: HardwareKey,
seed: ByteArray?
)
} }
fun checkDatabase() { fun checkDatabase() {
@@ -211,16 +205,24 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
} }
/** /**
* Force to call [ActionTaskListener.onStartAction] if the action is still running * Force to call [ActionTaskListener.onActionStarted] if the action is still running
* or [ActionTaskListener.onActionStopped] if the action is no longer running
*/ */
fun checkAction() { fun checkAction() {
mDatabase?.let { database -> mDatabase?.let { database ->
if (mActionRunning) { // Check if action / sub-action is running
if (mActionRunning > 0) {
mActionTaskListeners.forEach { actionTaskListener -> mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onStartAction( actionTaskListener.onActionStarted(
database, mProgressMessage database, mProgressMessage
) )
} }
} else {
mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onActionStopped(
database
)
}
} }
} }
} }
@@ -330,81 +332,104 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
else -> null else -> null
} }
// Sub action is an action in another action, don't perform pre and post action
val isMainAction = intentAction != ACTION_CHALLENGE_RESPONDED
// Build and launch the action // Build and launch the action
if (actionRunnable != null) { if (actionRunnable != null) {
mainScope.launch { mainScope.launch {
executeAction(this@DatabaseTaskNotificationService, executeAction(this@DatabaseTaskNotificationService,
{ {
TimeoutHelper.temporarilyDisableTimeout() mActionRunning++
if (isMainAction) {
TimeoutHelper.temporarilyDisableTimeout()
mActionRunning = true sendBroadcast(Intent(DATABASE_START_TASK_ACTION).apply {
putExtra(DATABASE_TASK_TITLE_KEY, mProgressMessage.titleId)
putExtra(DATABASE_TASK_MESSAGE_KEY, mProgressMessage.messageId)
putExtra(DATABASE_TASK_WARNING_KEY, mProgressMessage.warningId)
})
sendBroadcast(Intent(DATABASE_START_TASK_ACTION).apply { mActionTaskListeners.forEach { actionTaskListener ->
putExtra(DATABASE_TASK_TITLE_KEY, mProgressMessage.titleId) actionTaskListener.onActionStarted(
putExtra(DATABASE_TASK_MESSAGE_KEY, mProgressMessage.messageId) database,
putExtra(DATABASE_TASK_WARNING_KEY, mProgressMessage.warningId) mProgressMessage
}) )
}
mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onStartAction(
database, mProgressMessage
)
} }
}, },
{ {
actionRunnable actionRunnable
}, },
{ result -> { result ->
try { if (isMainAction) {
mActionTaskListeners.forEach { actionTaskListener -> try {
mTaskRemovedRequested = false mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onStopAction(database, intentAction!!, result) mTaskRemovedRequested = false
} actionTaskListener.onActionFinished(
} finally { database,
// Save the database info before performing action intentAction!!,
when (intentAction) { result
ACTION_DATABASE_LOAD_TASK, )
ACTION_DATABASE_MERGE_TASK,
ACTION_DATABASE_RELOAD_TASK -> {
saveDatabaseInfo()
} }
} } finally {
val save = !database.isReadOnly // Save the database info before performing action
&& (intentAction == ACTION_DATABASE_SAVE when (intentAction) {
|| intent?.getBooleanExtra(SAVE_DATABASE_KEY, false) == true) ACTION_DATABASE_LOAD_TASK,
// Save the database info after performing save action ACTION_DATABASE_MERGE_TASK,
if (save) { ACTION_DATABASE_RELOAD_TASK -> {
database.fileUri?.let { saveDatabaseInfo()
val newSnapFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo(
FileDatabaseInfo(applicationContext, it))
mLastLocalSaveTime = System.currentTimeMillis()
mSnapFileDatabaseInfo = newSnapFileDatabaseInfo
}
}
removeIntentData(intent)
TimeoutHelper.releaseTemporarilyDisableTimeout()
// Stop service after save if user remove task
if (save && mTaskRemovedRequested) {
actionOnLock()
} else if (TimeoutHelper.checkTimeAndLockIfTimeout(this@DatabaseTaskNotificationService)) {
if (!database.loaded) {
stopSelf()
} else {
// Restart the service to open lock notification
try {
startService(Intent(applicationContext,
DatabaseTaskNotificationService::class.java))
} catch (e: IllegalStateException) {
Log.w(TAG, "Cannot restart the database task service", e)
} }
} }
val save = !database.isReadOnly
&& (intentAction == ACTION_DATABASE_SAVE
|| intent?.getBooleanExtra(
SAVE_DATABASE_KEY,
false
) == true)
// Save the database info after performing save action
if (save) {
database.fileUri?.let {
val newSnapFileDatabaseInfo =
SnapFileDatabaseInfo.fromFileDatabaseInfo(
FileDatabaseInfo(applicationContext, it)
)
mLastLocalSaveTime = System.currentTimeMillis()
mSnapFileDatabaseInfo = newSnapFileDatabaseInfo
}
}
removeIntentData(intent)
TimeoutHelper.releaseTemporarilyDisableTimeout()
// Stop service after save if user remove task
if (save && mTaskRemovedRequested) {
actionOnLock()
} else if (TimeoutHelper.checkTimeAndLockIfTimeout(this@DatabaseTaskNotificationService)) {
if (!database.loaded) {
stopSelf()
} else {
// Restart the service to open lock notification
try {
startService(
Intent(
applicationContext,
DatabaseTaskNotificationService::class.java
)
)
} catch (e: IllegalStateException) {
Log.w(
TAG,
"Cannot restart the database task service",
e
)
}
}
}
mTaskRemovedRequested = false
} }
mTaskRemovedRequested = false
sendBroadcast(Intent(DATABASE_STOP_TASK_ACTION))
} }
mActionRunning--
sendBroadcast(Intent(DATABASE_STOP_TASK_ACTION))
mActionRunning = false
} }
) )
} }
@@ -577,7 +602,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
private fun notifyProgressMessage() { private fun notifyProgressMessage() {
mDatabase?.let { database -> mDatabase?.let { database ->
mActionTaskListeners.forEach { actionTaskListener -> mActionTaskListeners.forEach { actionTaskListener ->
actionTaskListener.onUpdateAction( actionTaskListener.onActionUpdated(
database, mProgressMessage database, mProgressMessage
) )
} }