Skip to content

Commit

Permalink
wip (#2790)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 authored Jul 3, 2024
1 parent c85a025 commit 2961b33
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
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)
}
}
}
}

0 comments on commit 2961b33

Please sign in to comment.