Better service implementation

This commit is contained in:
J-Jamet
2020-12-07 18:22:04 +01:00
parent e4ba1d9bae
commit c75d99030c
3 changed files with 15 additions and 20 deletions

View File

@@ -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,
mServiceConnection!!,
Context.BIND_ABOVE_CLIENT)
if (mBinder == null) {
applicationContext.startService(mIntentAdvancedUnlockService)
}
}
}

View File

@@ -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()
}
}
}

View File

@@ -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()