Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix "Unable to start service com.flowcrypt.email.service.PassPhrasesInRAMServ" errors #2790

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,7 @@ class FlowCryptApplication : Application(), Configuration.Provider {
keysStorage.secretKeyRingsLiveData.observeForever {
val hasTemporaryPassPhrases =
keysStorage.getRawKeys().any { it.passphraseType == KeyEntity.PassphraseType.RAM }
if (hasTemporaryPassPhrases) {
if (GeneralUtil.isAppForegrounded()) {
//we can run a foreground service only if the app is visible for a user
PassPhrasesInRAMService.start(this)
}
} else {
if (!hasTemporaryPassPhrases) {
PassPhrasesInRAMService.stop(this)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.lifecycle.lifecycleScope
import com.flowcrypt.email.BuildConfig
import com.flowcrypt.email.R
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.extensions.java.lang.printStackTraceIfDebugOnly
import com.flowcrypt.email.extensions.toast
import com.flowcrypt.email.model.KeysStorage
import com.flowcrypt.email.security.KeysStorageImpl
Expand Down Expand Up @@ -176,15 +177,15 @@ class PassPhrasesInRAMService : BaseLifecycleService() {
* @param context Interface to global information about an application environment.
*/
fun start(context: Context) {
val startEmailServiceIntent = Intent(context, PassPhrasesInRAMService::class.java)
val startServiceIntent = Intent(context, PassPhrasesInRAMService::class.java)
try {
context.startForegroundService(startEmailServiceIntent)
context.startForegroundService(startServiceIntent)
} catch (e: Exception) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (e is ForegroundServiceStartNotAllowedException) {
/*Because this service should be restarted by the system we can skip this exception.
It seems this service was started manually after the app crash via the trigger.*/
e.printStackTrace()
e.printStackTraceIfDebugOnly()
}
} else throw e
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ import androidx.navigation.ui.NavigationUI
import androidx.viewbinding.ViewBinding
import com.flowcrypt.email.R
import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.extensions.showFeedbackFragment
import com.flowcrypt.email.jetpack.viewmodel.AccountViewModel
import com.flowcrypt.email.security.KeysStorageImpl
import com.flowcrypt.email.service.PassPhrasesInRAMService
import com.flowcrypt.email.util.LogsUtil

/**
Expand Down Expand Up @@ -91,6 +94,7 @@ abstract class BaseActivity<T : ViewBinding> : AppCompatActivity() {
initViews()
setupNavigation()
initAccountViewModel()
setupPassPhrasesInRAMService()
}

override fun onNewIntent(intent: Intent) {
Expand Down Expand Up @@ -195,4 +199,17 @@ abstract class BaseActivity<T : ViewBinding> : AppCompatActivity() {
)
}
}

private fun setupPassPhrasesInRAMService() {
val keysStorage = KeysStorageImpl.getInstance(this)
keysStorage.secretKeyRingsLiveData.observe(this) {
val hasTemporaryPassPhrases =
keysStorage.getRawKeys().any { it.passphraseType == KeyEntity.PassphraseType.RAM }
if (hasTemporaryPassPhrases) {
PassPhrasesInRAMService.start(this)
} else {
PassPhrasesInRAMService.stop(this)
}
}
}
}
Loading