2022-03-08 11:05:03 -08:00
|
|
|
package co.electriccoin.zcash.ui
|
2021-09-14 14:02:06 -07:00
|
|
|
|
|
|
|
import android.os.Bundle
|
2021-12-03 05:19:15 -08:00
|
|
|
import android.os.SystemClock
|
2022-09-26 01:28:08 -07:00
|
|
|
import android.view.WindowManager
|
2021-09-14 14:02:06 -07:00
|
|
|
import androidx.activity.ComponentActivity
|
|
|
|
import androidx.activity.compose.setContent
|
2021-10-09 07:37:03 -07:00
|
|
|
import androidx.activity.viewModels
|
2021-12-03 05:19:15 -08:00
|
|
|
import androidx.annotation.VisibleForTesting
|
2021-12-09 12:18:18 -08:00
|
|
|
import androidx.compose.foundation.layout.fillMaxHeight
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
2022-10-03 06:59:09 -07:00
|
|
|
import androidx.compose.runtime.Composable
|
2022-09-26 01:28:08 -07:00
|
|
|
import androidx.compose.runtime.CompositionLocalProvider
|
2021-11-12 04:09:30 -08:00
|
|
|
import androidx.compose.runtime.collectAsState
|
2022-10-03 06:59:09 -07:00
|
|
|
import androidx.compose.runtime.getValue
|
2021-12-09 12:18:18 -08:00
|
|
|
import androidx.compose.ui.Modifier
|
2021-12-03 05:19:15 -08:00
|
|
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
2021-12-06 12:31:39 -08:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2022-01-31 13:42:22 -08:00
|
|
|
import androidx.navigation.NavHostController
|
2022-09-29 06:13:54 -07:00
|
|
|
import cash.z.ecc.android.sdk.ext.collectWith
|
2022-09-26 01:28:08 -07:00
|
|
|
import co.electriccoin.zcash.spackle.EmulatorWtfUtil
|
|
|
|
import co.electriccoin.zcash.spackle.FirebaseTestLabUtil
|
2022-09-29 06:13:54 -07:00
|
|
|
import co.electriccoin.zcash.ui.common.LocalScreenBrightness
|
2022-09-26 01:28:08 -07:00
|
|
|
import co.electriccoin.zcash.ui.common.LocalScreenSecurity
|
2022-09-29 06:13:54 -07:00
|
|
|
import co.electriccoin.zcash.ui.common.ScreenBrightness
|
2022-09-26 01:28:08 -07:00
|
|
|
import co.electriccoin.zcash.ui.common.ScreenSecurity
|
2022-07-13 00:16:05 -07:00
|
|
|
import co.electriccoin.zcash.ui.design.compat.FontCompat
|
2022-06-08 07:22:09 -07:00
|
|
|
import co.electriccoin.zcash.ui.design.component.ConfigurationOverride
|
2022-03-08 11:05:03 -08:00
|
|
|
import co.electriccoin.zcash.ui.design.component.GradientSurface
|
2022-06-08 07:22:09 -07:00
|
|
|
import co.electriccoin.zcash.ui.design.component.Override
|
2022-03-08 11:05:03 -08:00
|
|
|
import co.electriccoin.zcash.ui.design.theme.ZcashTheme
|
2022-04-19 05:04:56 -07:00
|
|
|
import co.electriccoin.zcash.ui.screen.backup.WrapBackup
|
2022-03-08 11:05:03 -08:00
|
|
|
import co.electriccoin.zcash.ui.screen.home.viewmodel.SecretState
|
|
|
|
import co.electriccoin.zcash.ui.screen.home.viewmodel.WalletViewModel
|
2022-06-09 10:17:58 -07:00
|
|
|
import co.electriccoin.zcash.ui.screen.onboarding.WrapOnboarding
|
2022-10-03 06:59:09 -07:00
|
|
|
import co.electriccoin.zcash.ui.screen.warning.WrapNotEnoughSpace
|
|
|
|
import co.electriccoin.zcash.ui.screen.warning.viewmodel.StorageCheckViewModel
|
2022-06-08 07:22:09 -07:00
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
2022-09-26 01:28:08 -07:00
|
|
|
import kotlinx.coroutines.flow.map
|
2021-12-06 12:31:39 -08:00
|
|
|
import kotlinx.coroutines.launch
|
2021-12-03 05:19:15 -08:00
|
|
|
import kotlin.time.Duration
|
|
|
|
import kotlin.time.Duration.Companion.milliseconds
|
|
|
|
import kotlin.time.Duration.Companion.seconds
|
2021-09-14 14:02:06 -07:00
|
|
|
|
|
|
|
class MainActivity : ComponentActivity() {
|
2021-10-09 07:37:03 -07:00
|
|
|
|
2022-01-31 13:42:22 -08:00
|
|
|
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
|
|
|
val walletViewModel by viewModels<WalletViewModel>()
|
|
|
|
|
2022-10-03 06:59:09 -07:00
|
|
|
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
|
|
|
val storageCheckViewModel by viewModels<StorageCheckViewModel>()
|
|
|
|
|
2022-01-31 13:42:22 -08:00
|
|
|
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
|
|
|
|
lateinit var navControllerForTesting: NavHostController
|
2021-11-12 04:09:30 -08:00
|
|
|
|
2022-06-08 07:22:09 -07:00
|
|
|
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
|
|
|
val configurationOverrideFlow = MutableStateFlow<ConfigurationOverride?>(null)
|
|
|
|
|
2021-09-14 14:02:06 -07:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2021-12-03 05:19:15 -08:00
|
|
|
|
|
|
|
setupSplashScreen()
|
|
|
|
|
2022-07-13 00:16:05 -07:00
|
|
|
if (FontCompat.isFontPrefetchNeeded()) {
|
|
|
|
lifecycleScope.launch {
|
|
|
|
FontCompat.prefetchFontsLegacy(applicationContext)
|
|
|
|
setupUiContent()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setupUiContent()
|
|
|
|
}
|
2021-09-14 14:02:06 -07:00
|
|
|
}
|
2021-11-17 12:19:49 -08:00
|
|
|
|
2021-12-03 05:19:15 -08:00
|
|
|
private fun setupSplashScreen() {
|
2021-12-09 12:18:18 -08:00
|
|
|
val splashScreen = installSplashScreen()
|
|
|
|
val start = SystemClock.elapsedRealtime().milliseconds
|
|
|
|
|
2022-04-04 05:48:47 -07:00
|
|
|
splashScreen.setKeepOnScreenCondition {
|
2021-12-09 12:18:18 -08:00
|
|
|
if (SPLASH_SCREEN_DELAY > Duration.ZERO) {
|
|
|
|
val now = SystemClock.elapsedRealtime().milliseconds
|
2021-12-03 05:19:15 -08:00
|
|
|
|
2021-12-09 12:18:18 -08:00
|
|
|
// This delay is for debug purposes only; do not enable for production usage.
|
|
|
|
if (now - start < SPLASH_SCREEN_DELAY) {
|
2022-04-04 05:48:47 -07:00
|
|
|
return@setKeepOnScreenCondition true
|
2021-12-09 12:18:18 -08:00
|
|
|
}
|
2021-12-03 05:19:15 -08:00
|
|
|
}
|
2021-12-09 12:18:18 -08:00
|
|
|
|
2021-12-29 11:38:14 -08:00
|
|
|
SecretState.Loading == walletViewModel.secretState.value
|
2021-12-03 05:19:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:31:39 -08:00
|
|
|
private fun setupUiContent() {
|
2022-09-26 01:28:08 -07:00
|
|
|
val screenSecurity = ScreenSecurity()
|
2022-09-29 06:13:54 -07:00
|
|
|
val screenBrightness = ScreenBrightness()
|
|
|
|
observeScreenSecurityFlag(screenSecurity)
|
|
|
|
observeScreenBrightnessFlag(screenBrightness)
|
2022-09-26 01:28:08 -07:00
|
|
|
|
2021-12-06 12:31:39 -08:00
|
|
|
setContent {
|
2022-06-08 07:22:09 -07:00
|
|
|
Override(configurationOverrideFlow) {
|
|
|
|
ZcashTheme {
|
|
|
|
GradientSurface(
|
|
|
|
Modifier
|
|
|
|
.fillMaxWidth()
|
|
|
|
.fillMaxHeight()
|
|
|
|
) {
|
2022-09-29 06:13:54 -07:00
|
|
|
CompositionLocalProvider(
|
|
|
|
LocalScreenSecurity provides screenSecurity,
|
|
|
|
LocalScreenBrightness provides screenBrightness
|
|
|
|
) {
|
2022-10-03 06:59:09 -07:00
|
|
|
val isEnoughSpace by storageCheckViewModel.isEnoughSpace.collectAsState()
|
|
|
|
if (isEnoughSpace == false) {
|
|
|
|
WrapNotEnoughSpace()
|
|
|
|
} else {
|
|
|
|
MainContent()
|
2022-08-09 12:23:38 -07:00
|
|
|
}
|
2021-12-09 12:18:18 -08:00
|
|
|
}
|
2021-12-06 12:31:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-31 13:42:22 -08:00
|
|
|
|
|
|
|
// Force collection to improve performance; sync can start happening while
|
|
|
|
// the user is going through the backup flow. Don't use eager collection in the view model,
|
|
|
|
// so that the collection is still tied to UI lifecycle.
|
2022-09-29 06:13:54 -07:00
|
|
|
walletViewModel.synchronizer.collectWith(lifecycleScope) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 06:59:09 -07:00
|
|
|
@Composable
|
|
|
|
private fun MainContent() {
|
|
|
|
when (val secretState = walletViewModel.secretState.collectAsState().value) {
|
|
|
|
SecretState.Loading -> {
|
|
|
|
// For now, keep displaying splash screen using condition above.
|
|
|
|
// In the future, we might consider displaying something different here.
|
|
|
|
}
|
|
|
|
SecretState.None -> {
|
|
|
|
WrapOnboarding()
|
|
|
|
}
|
|
|
|
is SecretState.NeedsBackup -> {
|
|
|
|
WrapBackup(
|
|
|
|
secretState.persistableWallet,
|
|
|
|
onBackupComplete = { walletViewModel.persistBackupComplete() }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
is SecretState.Ready -> {
|
|
|
|
Navigation()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 06:13:54 -07:00
|
|
|
private fun observeScreenSecurityFlag(screenSecurity: ScreenSecurity) {
|
|
|
|
screenSecurity.referenceCount.map { it > 0 }.collectWith(lifecycleScope) { isSecure ->
|
|
|
|
val isTest = FirebaseTestLabUtil.isFirebaseTestLab(applicationContext) ||
|
|
|
|
EmulatorWtfUtil.isEmulatorWtf(applicationContext)
|
|
|
|
|
|
|
|
if (isSecure && !isTest) {
|
|
|
|
window.setFlags(
|
|
|
|
WindowManager.LayoutParams.FLAG_SECURE,
|
|
|
|
WindowManager.LayoutParams.FLAG_SECURE
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun observeScreenBrightnessFlag(screenBrightness: ScreenBrightness) {
|
|
|
|
screenBrightness.referenceCount.map { it > 0 }.collectWith(lifecycleScope) { maxBrightness ->
|
|
|
|
if (maxBrightness) {
|
|
|
|
window.attributes = window.attributes.apply {
|
|
|
|
this.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
window.attributes = window.attributes.apply {
|
|
|
|
this.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
|
|
|
|
}
|
2022-02-21 06:50:09 -08:00
|
|
|
}
|
2022-01-31 13:42:22 -08:00
|
|
|
}
|
2021-12-06 12:31:39 -08:00
|
|
|
}
|
|
|
|
|
2021-12-03 05:19:15 -08:00
|
|
|
companion object {
|
|
|
|
@VisibleForTesting
|
|
|
|
internal val SPLASH_SCREEN_DELAY = 0.seconds
|
2022-06-14 23:53:54 -07:00
|
|
|
}
|
|
|
|
}
|