diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/service/PassPhrasesInRAMService.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/service/PassPhrasesInRAMService.kt index 1eb6b70a42..235748d288 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/service/PassPhrasesInRAMService.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/service/PassPhrasesInRAMService.kt @@ -60,18 +60,22 @@ class PassPhrasesInRAMService : BaseLifecycleService() { } else -> { - val notification = prepareNotification( - useActionButton = keysStorage.hasNonEmptyPassphrase(KeyEntity.PassphraseType.RAM) - ) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { - startForeground( - R.id.notification_id_passphrase_service, notification, - //https://developer.android.com/about/versions/14/changes/fgs-types-required#permission-for-fgs-type - ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE - ) + if (intent != null) { + //checking intent != null will prevent recreating the service after the system killed it + startForeground( + R.id.notification_id_passphrase_service, + prepareNotification(keysStorage.hasNonEmptyPassphrase(KeyEntity.PassphraseType.RAM)), + //https://developer.android.com/about/versions/14/changes/fgs-types-required#permission-for-fgs-type + ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE + ) + } else { + stopSelf() + } } else { startForeground( - R.id.notification_id_passphrase_service, notification + R.id.notification_id_passphrase_service, + prepareNotification(keysStorage.hasNonEmptyPassphrase(KeyEntity.PassphraseType.RAM)) ) } } @@ -82,6 +86,16 @@ class PassPhrasesInRAMService : BaseLifecycleService() { private fun subscribeToPassphrasesUpdates() { lifecycleScope.launch { keysStorage.getPassPhrasesUpdatesFlow().collect { + if ( + Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && + foregroundServiceType != ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE + ) { + NotificationManagerCompat.from(applicationContext).cancel( + R.id.notification_id_passphrase_service + ) + return@collect + } + updateNotification(keysStorage.hasNonEmptyPassphrase(KeyEntity.PassphraseType.RAM)) } } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/BaseActivity.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/BaseActivity.kt index 3802ef5130..5b64681479 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/BaseActivity.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/BaseActivity.kt @@ -1,6 +1,6 @@ /* * © 2016-present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com - * Contributors: DenBond7 + * Contributors: denbond7 */ package com.flowcrypt.email.ui.activity @@ -21,6 +21,7 @@ import androidx.appcompat.widget.Toolbar import androidx.core.view.MenuProvider import androidx.core.view.allViews import androidx.drawerlayout.widget.DrawerLayout +import androidx.lifecycle.Lifecycle import androidx.navigation.NavController import androidx.navigation.NavDestination import androidx.navigation.fragment.NavHostFragment @@ -206,7 +207,9 @@ abstract class BaseActivity : AppCompatActivity() { val hasTemporaryPassPhrases = keysStorage.getRawKeys().any { it.passphraseType == KeyEntity.PassphraseType.RAM } if (hasTemporaryPassPhrases) { - PassPhrasesInRAMService.start(this) + if (lifecycle.currentState in listOf(Lifecycle.State.STARTED, Lifecycle.State.RESUMED)) { + PassPhrasesInRAMService.start(this@BaseActivity) + } } else { PassPhrasesInRAMService.stop(this) }