Skip to content

Commit 675813e

Browse files
authored
Improved running PassPhrasesInRAMService.| #2703 (#3041)
1 parent 834041d commit 675813e

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

FlowCrypt/src/main/java/com/flowcrypt/email/service/PassPhrasesInRAMService.kt

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,22 @@ class PassPhrasesInRAMService : BaseLifecycleService() {
6060
}
6161

6262
else -> {
63-
val notification = prepareNotification(
64-
useActionButton = keysStorage.hasNonEmptyPassphrase(KeyEntity.PassphraseType.RAM)
65-
)
6663
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
67-
startForeground(
68-
R.id.notification_id_passphrase_service, notification,
69-
//https://developer.android.com/about/versions/14/changes/fgs-types-required#permission-for-fgs-type
70-
ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
71-
)
64+
if (intent != null) {
65+
//checking intent != null will prevent recreating the service after the system killed it
66+
startForeground(
67+
R.id.notification_id_passphrase_service,
68+
prepareNotification(keysStorage.hasNonEmptyPassphrase(KeyEntity.PassphraseType.RAM)),
69+
//https://developer.android.com/about/versions/14/changes/fgs-types-required#permission-for-fgs-type
70+
ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
71+
)
72+
} else {
73+
stopSelf()
74+
}
7275
} else {
7376
startForeground(
74-
R.id.notification_id_passphrase_service, notification
77+
R.id.notification_id_passphrase_service,
78+
prepareNotification(keysStorage.hasNonEmptyPassphrase(KeyEntity.PassphraseType.RAM))
7579
)
7680
}
7781
}
@@ -82,6 +86,16 @@ class PassPhrasesInRAMService : BaseLifecycleService() {
8286
private fun subscribeToPassphrasesUpdates() {
8387
lifecycleScope.launch {
8488
keysStorage.getPassPhrasesUpdatesFlow().collect {
89+
if (
90+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE &&
91+
foregroundServiceType != ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
92+
) {
93+
NotificationManagerCompat.from(applicationContext).cancel(
94+
R.id.notification_id_passphrase_service
95+
)
96+
return@collect
97+
}
98+
8599
updateNotification(keysStorage.hasNonEmptyPassphrase(KeyEntity.PassphraseType.RAM))
86100
}
87101
}

FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/BaseActivity.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* © 2016-present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com
3-
* Contributors: DenBond7
3+
* Contributors: denbond7
44
*/
55

66
package com.flowcrypt.email.ui.activity
@@ -21,6 +21,7 @@ import androidx.appcompat.widget.Toolbar
2121
import androidx.core.view.MenuProvider
2222
import androidx.core.view.allViews
2323
import androidx.drawerlayout.widget.DrawerLayout
24+
import androidx.lifecycle.Lifecycle
2425
import androidx.navigation.NavController
2526
import androidx.navigation.NavDestination
2627
import androidx.navigation.fragment.NavHostFragment
@@ -206,7 +207,9 @@ abstract class BaseActivity<T : ViewBinding> : AppCompatActivity() {
206207
val hasTemporaryPassPhrases =
207208
keysStorage.getRawKeys().any { it.passphraseType == KeyEntity.PassphraseType.RAM }
208209
if (hasTemporaryPassPhrases) {
209-
PassPhrasesInRAMService.start(this)
210+
if (lifecycle.currentState in listOf(Lifecycle.State.STARTED, Lifecycle.State.RESUMED)) {
211+
PassPhrasesInRAMService.start(this@BaseActivity)
212+
}
210213
} else {
211214
PassPhrasesInRAMService.stop(this)
212215
}

0 commit comments

Comments
 (0)