From 649dffc3e091bbfd0462dabf2b754710a3e7709c Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Wed, 18 Aug 2021 15:10:37 +0200 Subject: [PATCH] Close service after save if task removed by the user --- .../services/DatabaseTaskNotificationService.kt | 15 +++++++++++++-- .../keepass/services/LockNotificationService.kt | 2 +- .../kunzisoft/keepass/timeout/TimeoutHelper.kt | 9 ++++----- .../kunzisoft/keepass/utils/BroadcastAction.kt | 2 +- 4 files changed, 19 insertions(+), 9 deletions(-) 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 f628604bb..a4c052a52 100644 --- a/app/src/main/java/com/kunzisoft/keepass/services/DatabaseTaskNotificationService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/services/DatabaseTaskNotificationService.kt @@ -62,6 +62,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress private var mActionTaskBinder = ActionTaskBinder() private var mActionTaskListeners = LinkedList() private var mActionRunning = false + private var mTaskRemovedRequested = false private var mIconId: Int = R.drawable.notification_ic_database_load private var mTitleId: Int = R.string.database_opened @@ -301,7 +302,9 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress removeIntentData(intent) TimeoutHelper.releaseTemporarilyDisableTimeout() // Stop service after save if user remove task - if (TimeoutHelper.checkTimeAndLockIfTimeout(this@DatabaseTaskNotificationService)) { + if (save && mTaskRemovedRequested) { + actionOnLock() + } else if (TimeoutHelper.checkTimeAndLockIfTimeout(this@DatabaseTaskNotificationService)) { if (!database.loaded) { stopSelf() } else { @@ -312,6 +315,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress } catch (e: IllegalStateException) {} } } + mTaskRemovedRequested = false } sendBroadcast(Intent(DATABASE_STOP_TASK_ACTION)) @@ -485,7 +489,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress } override fun actionOnLock() { - if (!TimeoutHelper.temporarilyDisableTimeout) { + if (!TimeoutHelper.temporarilyDisableLock) { closeDatabase(mDatabase) // Remove the lock timer (no more needed if it exists) TimeoutHelper.cancelLockTimer(this) @@ -494,6 +498,13 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress } } + override fun onTaskRemoved(rootIntent: Intent?) { + if (TimeoutHelper.temporarilyDisableLock) { + mTaskRemovedRequested = true + } + super.onTaskRemoved(rootIntent) + } + private fun buildDatabaseCreateActionTask(intent: Intent, database: Database): ActionRunnable? { if (intent.hasExtra(DATABASE_URI_KEY) diff --git a/app/src/main/java/com/kunzisoft/keepass/services/LockNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/services/LockNotificationService.kt index 51f8f5049..9adc2b150 100644 --- a/app/src/main/java/com/kunzisoft/keepass/services/LockNotificationService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/services/LockNotificationService.kt @@ -43,7 +43,7 @@ abstract class LockNotificationService : NotificationService() { } override fun onTaskRemoved(rootIntent: Intent?) { - if (!TimeoutHelper.temporarilyDisableTimeout) { + if (!TimeoutHelper.temporarilyDisableLock) { actionOnLock() } super.onTaskRemoved(rootIntent) diff --git a/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.kt b/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.kt index ee94f66b0..f8a8514aa 100644 --- a/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.kt @@ -25,7 +25,6 @@ import android.content.Context import android.content.Intent import android.os.Build import android.util.Log -import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.utils.LOCK_ACTION @@ -39,7 +38,7 @@ object TimeoutHelper { private const val TAG = "TimeoutHelper" private var lastAppTimeoutRecord: Long? = null - var temporarilyDisableTimeout = false + var temporarilyDisableLock = false private set private fun getLockPendingIntent(context: Context): PendingIntent { @@ -103,7 +102,7 @@ object TimeoutHelper { */ fun checkTime(context: Context, timeoutAction: (() -> Unit)? = null): Boolean { // No effect if temporarily disable - if (temporarilyDisableTimeout) + if (temporarilyDisableLock) return true // Check whether the timeout has expired @@ -159,13 +158,13 @@ object TimeoutHelper { * Temporarily disable timeout, checkTime() function always return true */ fun temporarilyDisableTimeout() { - temporarilyDisableTimeout = true + temporarilyDisableLock = true } /** * Release the temporarily disable timeout */ fun releaseTemporarilyDisableTimeout() { - temporarilyDisableTimeout = false + temporarilyDisableLock = false } } \ No newline at end of file diff --git a/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt b/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt index 34493613e..d7d84d445 100644 --- a/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt +++ b/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt @@ -51,7 +51,7 @@ class LockReceiver(var lockAction: () -> Unit) : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { // If allowed, lock and exit - if (!TimeoutHelper.temporarilyDisableTimeout) { + if (!TimeoutHelper.temporarilyDisableLock) { intent.action?.let { when (it) { Intent.ACTION_SCREEN_ON -> {