From 173f5ce9796baea299e94d38e009fa765be9ea2b Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Mon, 7 Dec 2020 20:21:39 +0100 Subject: [PATCH] Add advanced unlock timeout --- .../biometric/AdvancedUnlockedManager.kt | 2 + .../AdvancedUnlockNotificationService.kt | 57 ++++++++++++++++--- app/src/main/res/values/donottranslate.xml | 16 +++++- app/src/main/res/values/strings.xml | 14 +++++ .../res/xml/preferences_advanced_unlock.xml | 4 +- 5 files changed, 83 insertions(+), 10 deletions(-) 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 50b0bffa9..1396c6a79 100644 --- a/app/src/main/java/com/kunzisoft/keepass/biometric/AdvancedUnlockedManager.kt +++ b/app/src/main/java/com/kunzisoft/keepass/biometric/AdvancedUnlockedManager.kt @@ -35,6 +35,7 @@ import androidx.biometric.BiometricPrompt import androidx.fragment.app.FragmentActivity import com.kunzisoft.keepass.R import com.kunzisoft.keepass.app.database.CipherDatabaseAction +import com.kunzisoft.keepass.notifications.AdvancedUnlockNotificationService import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.view.AdvancedUnlockInfoView @@ -190,6 +191,7 @@ class AdvancedUnlockedManager(var context: FragmentActivity, Mode.STORE_CREDENTIAL -> { // newly store the entered password in encrypted way biometricUnlockDatabaseHelper?.encryptData(passwordView?.text.toString()) + AdvancedUnlockNotificationService.startServiceForTimeout(context) } Mode.EXTRACT_CREDENTIAL -> { // retrieve the encrypted value from preferences 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 0e6367911..b56300fed 100644 --- a/app/src/main/java/com/kunzisoft/keepass/notifications/AdvancedUnlockNotificationService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/notifications/AdvancedUnlockNotificationService.kt @@ -1,12 +1,16 @@ package com.kunzisoft.keepass.notifications import android.app.PendingIntent +import android.content.Context import android.content.Intent import android.net.Uri import android.os.Binder import android.os.IBinder import com.kunzisoft.keepass.R import com.kunzisoft.keepass.app.database.CipherDatabaseEntity +import com.kunzisoft.keepass.settings.PreferencesUtil +import com.kunzisoft.keepass.timeout.TimeoutHelper +import kotlinx.coroutines.* class AdvancedUnlockNotificationService : NotificationService() { @@ -14,6 +18,9 @@ class AdvancedUnlockNotificationService : NotificationService() { private var mActionTaskBinder = AdvancedUnlockBinder() + private var notificationTimeoutMilliSecs: Long = 0 + private var mTimerJob: Job? = null + inner class AdvancedUnlockBinder: Binder() { fun getCipherDatabase(databaseUri: Uri): CipherDatabaseEntity? { return mTempCipherDao.firstOrNull { it.databaseUri == databaseUri.toString()} @@ -51,8 +58,13 @@ class AdvancedUnlockNotificationService : NotificationService() { action = ACTION_REMOVE_KEYS } val pendingDeleteIntent = PendingIntent.getService(this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT) + val deviceCredential = PreferencesUtil.isDeviceCredentialUnlockEnable(this) val notificationBuilder = buildNewNotification().apply { - setSmallIcon(R.drawable.bolt) + setSmallIcon(if (deviceCredential) { + R.drawable.bolt + } else { + R.drawable.fingerprint + }) intent?.let { setContentTitle(getString(R.string.advanced_unlock)) } @@ -61,12 +73,35 @@ class AdvancedUnlockNotificationService : NotificationService() { // Unfortunately swipe is disabled in lollipop+ setDeleteIntent(pendingDeleteIntent) } - // Not necessarily a foreground service - startForeground(notificationId, notificationBuilder.build()) - //notificationManager?.notify(notificationId, notificationBuilder.build()) - if (intent?.action == ACTION_REMOVE_KEYS) { - stopSelf() + when (intent?.action) { + ACTION_TIMEOUT -> { + notificationTimeoutMilliSecs = PreferencesUtil.getAdvancedUnlockTimeout(this) + // Not necessarily a foreground service + if (mTimerJob == null && notificationTimeoutMilliSecs != TimeoutHelper.NEVER) { + mTimerJob = CoroutineScope(Dispatchers.Main).launch { + val maxPos = 100 + val posDurationMills = notificationTimeoutMilliSecs / maxPos + for (pos in maxPos downTo 0) { + notificationBuilder.setProgress(maxPos, pos, false) + startForeground(notificationId, notificationBuilder.build()) + delay(posDurationMills) + if (pos <= 0) { + stopSelf() + } + } + notificationManager?.cancel(notificationId) + mTimerJob = null + cancel() + } + } else { + startForeground(notificationId, notificationBuilder.build()) + } + } + ACTION_REMOVE_KEYS -> { + stopSelf() + } + else -> {} } return START_STICKY @@ -79,12 +114,20 @@ class AdvancedUnlockNotificationService : NotificationService() { override fun onDestroy() { mTempCipherDao.clear() + mTimerJob?.cancel() super.onDestroy() } companion object { private const val CHANNEL_ADVANCED_UNLOCK_ID = "com.kunzisoft.keepass.notification.channel.unlock" - const val ACTION_REMOVE_KEYS = "ACTION_REMOVE_KEYS" + private const val ACTION_TIMEOUT = "ACTION_TIMEOUT" + private const val ACTION_REMOVE_KEYS = "ACTION_REMOVE_KEYS" + + fun startServiceForTimeout(context: Context) { + context.startService(Intent(context, AdvancedUnlockNotificationService::class.java).apply { + action = ACTION_TIMEOUT + }) + } } } \ No newline at end of file diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index 380a88632..757f9f374 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -101,7 +101,7 @@ temp_advanced_unlock_enable_key false temp_advanced_unlock_timeout_key - 30000 + 36000000 biometric_delete_all_key_key @@ -291,6 +291,20 @@ 1800000 -1 + + 300000 + 900000 + 1800000 + 3600000 + 7200000 + 18000000 + 36000000 + 86400000 + 172800000 + 604800000 + 2592000000 + -1 + 32dp diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a3ea0a771..f5d405725 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -521,6 +521,20 @@ 30 minutes Never + + 5 minutes + 15 minutes + 30 minutes + 1 hour + 2 hours + 5 hours + 10 hours + 24 hours + 48 hours + 1 week + 1 month + Never + Small Medium diff --git a/app/src/main/res/xml/preferences_advanced_unlock.xml b/app/src/main/res/xml/preferences_advanced_unlock.xml index 010428229..0b66ceefb 100644 --- a/app/src/main/res/xml/preferences_advanced_unlock.xml +++ b/app/src/main/res/xml/preferences_advanced_unlock.xml @@ -52,8 +52,8 @@ android:title="@string/temp_advanced_unlock_timeout_title" android:summary="@string/temp_advanced_unlock_timeout_summary" android:dependency="@string/temp_advanced_unlock_enable_key" - android:entries="@array/timeout_options" - android:entryValues="@array/timeout_values" + android:entries="@array/large_timeout_options" + android:entryValues="@array/large_timeout_values" android:dialogTitle="@string/advanced_unlock_timeout" android:defaultValue="@string/temp_advanced_unlock_timeout_default"/>