From c75d99030c733a3b19d6b2c80ddabd3a9f7a66fa Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Mon, 7 Dec 2020 18:22:04 +0100 Subject: [PATCH] Better service implementation --- .../app/database/CipherDatabaseAction.kt | 20 ++++++++----------- .../biometric/AdvancedUnlockedManager.kt | 9 ++++----- .../AdvancedUnlockNotificationService.kt | 6 +++--- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt b/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt index b0b2566ab..d0ea0f767 100644 --- a/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt +++ b/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt @@ -45,20 +45,16 @@ class CipherDatabaseAction(context: Context) { private var mBinder: AdvancedUnlockNotificationService.AdvancedUnlockBinder? = null private var mServiceConnection: ServiceConnection? = null - fun initialize() { - applicationContext.startService(mIntentAdvancedUnlockService) - } - @Synchronized - private fun attachService(serviceAttached: () -> Unit) { + private fun attachService(performedAction: () -> Unit) { // Check if a service is currently running else do nothing if (mBinder != null) { - serviceAttached.invoke() + performedAction.invoke() } else if (mServiceConnection == null) { mServiceConnection = object : ServiceConnection { override fun onServiceConnected(name: ComponentName?, serviceBinder: IBinder?) { mBinder = (serviceBinder as AdvancedUnlockNotificationService.AdvancedUnlockBinder) - serviceAttached.invoke() + performedAction.invoke() } override fun onServiceDisconnected(name: ComponentName?) { @@ -66,11 +62,11 @@ class CipherDatabaseAction(context: Context) { mServiceConnection = null } } - // bind Service - mServiceConnection?.let { - applicationContext.bindService(mIntentAdvancedUnlockService, - it, - Context.BIND_ABOVE_CLIENT) + applicationContext.bindService(mIntentAdvancedUnlockService, + mServiceConnection!!, + Context.BIND_ABOVE_CLIENT) + if (mBinder == null) { + applicationContext.startService(mIntentAdvancedUnlockService) } } } diff --git a/app/src/main/java/com/kunzisoft/keepass/biometric/AdvancedUnlockedManager.kt b/app/src/main/java/com/kunzisoft/keepass/biometric/AdvancedUnlockedManager.kt index 141a71b13..54485fde0 100644 --- a/app/src/main/java/com/kunzisoft/keepass/biometric/AdvancedUnlockedManager.kt +++ b/app/src/main/java/com/kunzisoft/keepass/biometric/AdvancedUnlockedManager.kt @@ -78,7 +78,6 @@ class AdvancedUnlockedManager(var context: FragmentActivity, // Add old listener to enable the button, only be call here because of onCheckedChange bug onCheckedPasswordChangeListener?.onCheckedChanged(compoundButton, checked) } - cipherDatabaseAction.initialize() } /** @@ -184,10 +183,10 @@ class AdvancedUnlockedManager(var context: FragmentActivity, } Mode.EXTRACT_CREDENTIAL -> { // retrieve the encrypted value from preferences - cipherDatabaseAction.getCipherDatabase(databaseFileUri) { - it?.encryptedValue?.let { value -> + cipherDatabaseAction.getCipherDatabase(databaseFileUri) { cipherDatabase -> + cipherDatabase?.encryptedValue?.let { value -> biometricUnlockDatabaseHelper?.decryptData(value) - } + } ?: deleteEncryptedDatabaseKey() } } } @@ -294,7 +293,7 @@ class AdvancedUnlockedManager(var context: FragmentActivity, openBiometricPrompt(biometricPrompt, cryptoObject, promptInfo) } } - } + } ?: deleteEncryptedDatabaseKey() } } } diff --git a/app/src/main/java/com/kunzisoft/keepass/notifications/AdvancedUnlockNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/notifications/AdvancedUnlockNotificationService.kt index 81a3816f3..0e6367911 100644 --- a/app/src/main/java/com/kunzisoft/keepass/notifications/AdvancedUnlockNotificationService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/notifications/AdvancedUnlockNotificationService.kt @@ -61,9 +61,9 @@ class AdvancedUnlockNotificationService : NotificationService() { // Unfortunately swipe is disabled in lollipop+ setDeleteIntent(pendingDeleteIntent) } - // Not necessarilly a foreground service - // startForeground(notificationId, notificationBuilder.build()) - notificationManager?.notify(notificationId, notificationBuilder.build()) + // Not necessarily a foreground service + startForeground(notificationId, notificationBuilder.build()) + //notificationManager?.notify(notificationId, notificationBuilder.build()) if (intent?.action == ACTION_REMOVE_KEYS) { stopSelf()