mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Better service implementation
This commit is contained in:
@@ -50,15 +50,15 @@ class CipherDatabaseAction(context: Context) {
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun getTempCipherDao(tempCipherDaoRetrieved: (MutableList<CipherDatabaseEntity>?) -> Unit) {
|
||||
private fun attachService(serviceAttached: () -> Unit) {
|
||||
// Check if a service is currently running else do nothing
|
||||
if (mBinder != null) {
|
||||
tempCipherDaoRetrieved.invoke(mBinder?.getTempCipherDao())
|
||||
serviceAttached.invoke()
|
||||
} else if (mServiceConnection == null) {
|
||||
mServiceConnection = object : ServiceConnection {
|
||||
override fun onServiceConnected(name: ComponentName?, serviceBinder: IBinder?) {
|
||||
mBinder = (serviceBinder as AdvancedUnlockNotificationService.AdvancedUnlockBinder)
|
||||
tempCipherDaoRetrieved.invoke(mBinder?.getTempCipherDao())
|
||||
serviceAttached.invoke()
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(name: ComponentName?) {
|
||||
@@ -78,8 +78,8 @@ class CipherDatabaseAction(context: Context) {
|
||||
fun getCipherDatabase(databaseUri: Uri,
|
||||
cipherDatabaseResultListener: (CipherDatabaseEntity?) -> Unit) {
|
||||
if (useTempDao) {
|
||||
getTempCipherDao { tempCipherDao ->
|
||||
cipherDatabaseResultListener.invoke(tempCipherDao?.firstOrNull { it.databaseUri == databaseUri.toString()})
|
||||
attachService {
|
||||
cipherDatabaseResultListener.invoke(mBinder?.getCipherDatabase(databaseUri))
|
||||
}
|
||||
} else {
|
||||
IOActionTask(
|
||||
@@ -103,10 +103,8 @@ class CipherDatabaseAction(context: Context) {
|
||||
fun addOrUpdateCipherDatabase(cipherDatabaseEntity: CipherDatabaseEntity,
|
||||
cipherDatabaseResultListener: (() -> Unit)? = null) {
|
||||
if (useTempDao) {
|
||||
getTempCipherDao { tempCipherDao ->
|
||||
val cipherDatabaseRetrieve = tempCipherDao?.firstOrNull { it.databaseUri == cipherDatabaseEntity.databaseUri }
|
||||
cipherDatabaseRetrieve?.replaceContent(cipherDatabaseEntity)
|
||||
?: tempCipherDao?.add(cipherDatabaseEntity)
|
||||
attachService {
|
||||
mBinder?.addOrUpdateCipherDatabase(cipherDatabaseEntity)
|
||||
cipherDatabaseResultListener?.invoke()
|
||||
}
|
||||
} else {
|
||||
@@ -129,24 +127,26 @@ class CipherDatabaseAction(context: Context) {
|
||||
|
||||
fun deleteByDatabaseUri(databaseUri: Uri,
|
||||
cipherDatabaseResultListener: (() -> Unit)? = null) {
|
||||
getTempCipherDao { tempCipherDao ->
|
||||
tempCipherDao?.firstOrNull { it.databaseUri == databaseUri.toString() }?.let {
|
||||
tempCipherDao.remove(it)
|
||||
if (useTempDao) {
|
||||
attachService {
|
||||
mBinder?.deleteByDatabaseUri(databaseUri)
|
||||
cipherDatabaseResultListener?.invoke()
|
||||
}
|
||||
} else {
|
||||
IOActionTask(
|
||||
{
|
||||
cipherDatabaseDao.deleteByDatabaseUri(databaseUri.toString())
|
||||
},
|
||||
{
|
||||
cipherDatabaseResultListener?.invoke()
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
IOActionTask(
|
||||
{
|
||||
cipherDatabaseDao.deleteByDatabaseUri(databaseUri.toString())
|
||||
},
|
||||
{
|
||||
cipherDatabaseResultListener?.invoke()
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
|
||||
fun deleteAll() {
|
||||
getTempCipherDao { tempCipherDao ->
|
||||
tempCipherDao?.clear()
|
||||
attachService {
|
||||
mBinder?.deleteAll()
|
||||
}
|
||||
IOActionTask(
|
||||
{
|
||||
|
||||
@@ -279,10 +279,9 @@ class AdvancedUnlockedManager(var context: FragmentActivity,
|
||||
setAdvancedUnlockedMessageView("")
|
||||
|
||||
if (biometricUnlockDatabaseHelper != null) {
|
||||
cipherDatabaseAction.getCipherDatabase(databaseFileUri) {
|
||||
|
||||
it?.specParameters?.let { specs ->
|
||||
biometricUnlockDatabaseHelper?.initDecryptData(specs) { biometricPrompt, cryptoObject, promptInfo ->
|
||||
cipherDatabaseAction.getCipherDatabase(databaseFileUri) { cipherDatabase ->
|
||||
cipherDatabase?.let {
|
||||
biometricUnlockDatabaseHelper?.initDecryptData(it.specParameters) { biometricPrompt, cryptoObject, promptInfo ->
|
||||
|
||||
// Set listener to open the biometric dialog and check credential
|
||||
advancedUnlockInfoView?.setIconViewClickListener { _ ->
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.kunzisoft.keepass.notifications
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Binder
|
||||
import android.os.IBinder
|
||||
import com.kunzisoft.keepass.R
|
||||
@@ -14,8 +15,21 @@ class AdvancedUnlockNotificationService : NotificationService() {
|
||||
private var mActionTaskBinder = AdvancedUnlockBinder()
|
||||
|
||||
inner class AdvancedUnlockBinder: Binder() {
|
||||
fun getTempCipherDao(): MutableList<CipherDatabaseEntity> {
|
||||
return mTempCipherDao
|
||||
fun getCipherDatabase(databaseUri: Uri): CipherDatabaseEntity? {
|
||||
return mTempCipherDao.firstOrNull { it.databaseUri == databaseUri.toString()}
|
||||
}
|
||||
fun addOrUpdateCipherDatabase(cipherDatabaseEntity: CipherDatabaseEntity) {
|
||||
val cipherDatabaseRetrieve = mTempCipherDao.firstOrNull { it.databaseUri == cipherDatabaseEntity.databaseUri }
|
||||
cipherDatabaseRetrieve?.replaceContent(cipherDatabaseEntity)
|
||||
?: mTempCipherDao.add(cipherDatabaseEntity)
|
||||
}
|
||||
fun deleteByDatabaseUri(databaseUri: Uri) {
|
||||
mTempCipherDao.firstOrNull { it.databaseUri == databaseUri.toString() }?.let {
|
||||
mTempCipherDao.remove(it)
|
||||
}
|
||||
}
|
||||
fun deleteAll() {
|
||||
mTempCipherDao.clear()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +61,9 @@ class AdvancedUnlockNotificationService : NotificationService() {
|
||||
// Unfortunately swipe is disabled in lollipop+
|
||||
setDeleteIntent(pendingDeleteIntent)
|
||||
}
|
||||
startForeground(notificationId, notificationBuilder.build())
|
||||
// Not necessarilly a foreground service
|
||||
// startForeground(notificationId, notificationBuilder.build())
|
||||
notificationManager?.notify(notificationId, notificationBuilder.build())
|
||||
|
||||
if (intent?.action == ACTION_REMOVE_KEYS) {
|
||||
stopSelf()
|
||||
|
||||
Reference in New Issue
Block a user