mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-12-05 16:22:31 -08:00
fix: Add app in background event
This commit is contained in:
6 files changed
+65
-23
No files matched your search
@@ -75,7 +75,6 @@ import com.kunzisoft.keepass.utils.getParcelableCompat
|
||||
import com.kunzisoft.keepass.view.asError
|
||||
import com.kunzisoft.keepass.view.showActionErrorIfNeeded
|
||||
import com.kunzisoft.keepass.viewmodels.DatabaseFilesViewModel
|
||||
import com.kunzisoft.keepass.viewmodels.DeviceUnlockViewModel.Companion.isAutoOpenBiometricPromptAllowed
|
||||
import java.io.FileNotFoundException
|
||||
|
||||
class FileDatabaseSelectActivity : DatabaseModeActivity(),
|
||||
@@ -355,11 +354,6 @@ class FileDatabaseSelectActivity : DatabaseModeActivity(),
|
||||
} else {
|
||||
mAdapterDatabaseHistory?.clearDatabaseFileHistoryList()
|
||||
}
|
||||
|
||||
// Allow auto open biometric prompt
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
isAutoOpenBiometricPromptAllowed = true
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
|
||||
@@ -23,7 +23,6 @@ import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.MotionEvent
|
||||
@@ -54,7 +53,6 @@ import com.kunzisoft.keepass.utils.closeDatabase
|
||||
import com.kunzisoft.keepass.utils.registerLockReceiver
|
||||
import com.kunzisoft.keepass.utils.unregisterLockReceiver
|
||||
import com.kunzisoft.keepass.view.showActionErrorIfNeeded
|
||||
import com.kunzisoft.keepass.viewmodels.DeviceUnlockViewModel.Companion.isAutoOpenBiometricPromptAllowed
|
||||
import com.kunzisoft.keepass.viewmodels.NodesViewModel
|
||||
import java.util.UUID
|
||||
|
||||
@@ -72,8 +70,6 @@ abstract class DatabaseLockActivity : DatabaseModeActivity(),
|
||||
protected var mMergeDataAllowed: Boolean = false
|
||||
private var mAutoSaveEnable: Boolean = true
|
||||
|
||||
private var isDatabaseUiVisible: Boolean = false
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
@@ -192,10 +188,6 @@ abstract class DatabaseLockActivity : DatabaseModeActivity(),
|
||||
mLockReceiver = LockReceiver {
|
||||
mDatabase = null
|
||||
closeDatabase(database)
|
||||
// Don't allow auto open prompt if lock become when UI visible
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
isAutoOpenBiometricPromptAllowed = !isDatabaseUiVisible
|
||||
}
|
||||
mExitLock = true
|
||||
closeOptionsMenu()
|
||||
finish()
|
||||
@@ -423,8 +415,6 @@ abstract class DatabaseLockActivity : DatabaseModeActivity(),
|
||||
}
|
||||
|
||||
invalidateOptionsMenu()
|
||||
|
||||
isDatabaseUiVisible = true
|
||||
}
|
||||
|
||||
protected fun checkTimeAndLockIfTimeoutOrResetTimeout(action: (() -> Unit)? = null) {
|
||||
@@ -439,8 +429,6 @@ abstract class DatabaseLockActivity : DatabaseModeActivity(),
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
isDatabaseUiVisible = false
|
||||
|
||||
super.onPause()
|
||||
|
||||
if (mTimeoutEnable) {
|
||||
|
||||
@@ -19,15 +19,54 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.app
|
||||
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
import androidx.multidex.MultiDexApplication
|
||||
import com.kunzisoft.keepass.activities.stylish.Stylish
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class App : MultiDexApplication() {
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifecycleObserver)
|
||||
|
||||
Stylish.load(this)
|
||||
PRNGFixes.apply()
|
||||
}
|
||||
}
|
||||
|
||||
object AppLifecycleObserver : DefaultLifecycleObserver {
|
||||
|
||||
var isAppInForeground: Boolean = false
|
||||
private set
|
||||
|
||||
var lockBackgroundEvent = false
|
||||
|
||||
private val _appJustLaunched = MutableSharedFlow<Unit>(replay = 0)
|
||||
val appJustLaunched = _appJustLaunched.asSharedFlow()
|
||||
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
override fun onStart(owner: LifecycleOwner) {
|
||||
super.onStart(owner)
|
||||
val wasPreviouslyInBackground = !isAppInForeground
|
||||
isAppInForeground = true
|
||||
if (!lockBackgroundEvent && wasPreviouslyInBackground) {
|
||||
GlobalScope.launch {
|
||||
_appJustLaunched.emit(Unit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStop(owner: LifecycleOwner) {
|
||||
super.onStop(owner)
|
||||
isAppInForeground = false
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ import androidx.annotation.StringRes
|
||||
import androidx.media.app.NotificationCompat
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.activities.GroupActivity
|
||||
import com.kunzisoft.keepass.app.AppLifecycleObserver
|
||||
import com.kunzisoft.keepass.app.database.CipherDatabaseAction
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
|
||||
import com.kunzisoft.keepass.database.ContextualDatabase
|
||||
@@ -625,6 +626,16 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
|
||||
intent?.removeExtra(NEW_ELEMENT_KEY)
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
AppLifecycleObserver.lockBackgroundEvent = true
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
AppLifecycleObserver.lockBackgroundEvent = false
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute action with a coroutine
|
||||
*/
|
||||
|
||||
@@ -7,6 +7,8 @@ import androidx.annotation.RequiresApi
|
||||
import androidx.biometric.BiometricManager
|
||||
import androidx.biometric.BiometricPrompt
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.kunzisoft.keepass.app.AppLifecycleObserver
|
||||
import com.kunzisoft.keepass.app.database.CipherDatabaseAction
|
||||
import com.kunzisoft.keepass.biometric.DeviceUnlockCryptoPrompt
|
||||
import com.kunzisoft.keepass.biometric.DeviceUnlockCryptoPromptType
|
||||
@@ -19,6 +21,8 @@ import com.kunzisoft.keepass.model.CredentialStorage
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.update
|
||||
import javax.crypto.Cipher
|
||||
|
||||
@@ -34,6 +38,7 @@ class DeviceUnlockViewModel(application: Application): AndroidViewModel(applicat
|
||||
|
||||
private var deviceUnlockMode = DeviceUnlockMode.BIOMETRIC_UNAVAILABLE
|
||||
var cryptoPrompt: DeviceUnlockCryptoPrompt? = null
|
||||
private var isAutoOpenBiometricPromptAllowed = true
|
||||
private var cryptoPromptShowPending: Boolean = false
|
||||
|
||||
// TODO Retrieve credential storage from app database
|
||||
@@ -44,6 +49,13 @@ class DeviceUnlockViewModel(application: Application): AndroidViewModel(applicat
|
||||
private val _uiState = MutableStateFlow(DeviceUnlockState())
|
||||
val uiState: StateFlow<DeviceUnlockState> = _uiState
|
||||
|
||||
init {
|
||||
AppLifecycleObserver.appJustLaunched
|
||||
.onEach {
|
||||
isAutoOpenBiometricPromptAllowed = true
|
||||
}.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
fun checkConditionToStoreCredential(condition: Boolean, databaseFileUri: Uri?) {
|
||||
isConditionToStoreCredentialVerified = condition
|
||||
checkUnlockAvailability(databaseFileUri)
|
||||
@@ -364,7 +376,8 @@ class DeviceUnlockViewModel(application: Application): AndroidViewModel(applicat
|
||||
deviceUnlockManager?.initDecryptData(cipherDatabase.specParameters) { cryptoPrompt ->
|
||||
onPromptRequested(
|
||||
cryptoPrompt,
|
||||
autoOpen = isAutoOpenBiometricPromptAllowed || cryptoPromptShowPending
|
||||
autoOpen = isAutoOpenBiometricPromptAllowed
|
||||
|| cryptoPromptShowPending
|
||||
)
|
||||
} ?: setException(Exception("AdvancedUnlockManager not initialized"))
|
||||
} catch (e: Exception) {
|
||||
@@ -416,10 +429,6 @@ class DeviceUnlockViewModel(application: Application): AndroidViewModel(applicat
|
||||
super.onCleared()
|
||||
clear()
|
||||
}
|
||||
|
||||
companion object {
|
||||
var isAutoOpenBiometricPromptAllowed = true
|
||||
}
|
||||
}
|
||||
|
||||
enum class DeviceUnlockPromptMode {
|
||||
|
||||
Reference in new issue
Block a user