2023-08-23 00:03:42 +03:00
|
|
|
package com.x8bit.bitwarden
|
|
|
|
|
2023-10-03 22:34:51 +03:00
|
|
|
import android.content.Intent
|
2023-08-23 00:03:42 +03:00
|
|
|
import android.os.Bundle
|
2024-01-26 19:26:17 +03:00
|
|
|
import android.view.WindowManager
|
2023-08-23 00:03:42 +03:00
|
|
|
import androidx.activity.compose.setContent
|
2023-10-03 22:34:51 +03:00
|
|
|
import androidx.activity.viewModels
|
2024-01-13 00:43:18 +03:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import androidx.appcompat.app.AppCompatDelegate
|
2024-01-17 02:41:34 +03:00
|
|
|
import androidx.compose.runtime.getValue
|
2024-01-13 00:43:18 +03:00
|
|
|
import androidx.core.os.LocaleListCompat
|
2023-09-28 22:18:03 +03:00
|
|
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
2024-01-17 02:41:34 +03:00
|
|
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
2024-01-26 19:26:17 +03:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2024-01-30 18:43:55 +03:00
|
|
|
import com.x8bit.bitwarden.data.autofill.manager.AutofillActivityManager
|
2024-01-28 21:25:15 +03:00
|
|
|
import com.x8bit.bitwarden.data.autofill.manager.AutofillCompletionManager
|
2024-02-13 01:42:51 +03:00
|
|
|
import com.x8bit.bitwarden.data.platform.annotation.OmitFromCoverage
|
2024-01-13 00:43:18 +03:00
|
|
|
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
|
2023-09-05 21:13:26 +03:00
|
|
|
import com.x8bit.bitwarden.ui.platform.feature.rootnav.RootNavScreen
|
|
|
|
import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme
|
2023-08-23 00:03:42 +03:00
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
2024-01-26 19:26:17 +03:00
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
2024-01-13 00:43:18 +03:00
|
|
|
import javax.inject.Inject
|
2023-08-23 00:03:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Primary entry point for the application.
|
|
|
|
*/
|
2024-02-13 01:42:51 +03:00
|
|
|
@OmitFromCoverage
|
2023-08-23 00:03:42 +03:00
|
|
|
@AndroidEntryPoint
|
2024-01-13 00:43:18 +03:00
|
|
|
class MainActivity : AppCompatActivity() {
|
2023-10-03 22:34:51 +03:00
|
|
|
|
|
|
|
private val mainViewModel: MainViewModel by viewModels()
|
|
|
|
|
2024-01-30 18:43:55 +03:00
|
|
|
@Inject
|
|
|
|
lateinit var autofillActivityManager: AutofillActivityManager
|
|
|
|
|
2024-01-28 21:25:15 +03:00
|
|
|
@Inject
|
|
|
|
lateinit var autofillCompletionManager: AutofillCompletionManager
|
|
|
|
|
2024-01-13 00:43:18 +03:00
|
|
|
@Inject
|
|
|
|
lateinit var settingsRepository: SettingsRepository
|
|
|
|
|
2023-08-23 00:03:42 +03:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
2023-09-28 22:18:03 +03:00
|
|
|
var shouldShowSplashScreen = true
|
|
|
|
installSplashScreen().setKeepOnScreenCondition { shouldShowSplashScreen }
|
2023-08-23 00:03:42 +03:00
|
|
|
super.onCreate(savedInstanceState)
|
2024-01-20 00:01:43 +03:00
|
|
|
|
2024-01-26 19:26:17 +03:00
|
|
|
observeViewModelEvents()
|
|
|
|
|
2024-01-21 20:35:39 +03:00
|
|
|
if (savedInstanceState == null) {
|
|
|
|
mainViewModel.trySendAction(
|
|
|
|
MainAction.ReceiveFirstIntent(
|
|
|
|
intent = intent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2024-01-20 00:01:43 +03:00
|
|
|
|
2024-01-13 00:43:18 +03:00
|
|
|
// Within the app the language will change dynamically and will be managed
|
|
|
|
// by the OS, but we need to ensure we properly set the language when
|
|
|
|
// upgrading from older versions that handle this differently.
|
|
|
|
settingsRepository.appLanguage.localeName?.let { localeName ->
|
|
|
|
val localeList = LocaleListCompat.forLanguageTags(localeName)
|
|
|
|
AppCompatDelegate.setApplicationLocales(localeList)
|
|
|
|
}
|
2023-08-29 17:43:15 +03:00
|
|
|
setContent {
|
2024-01-17 02:41:34 +03:00
|
|
|
val state by mainViewModel.stateFlow.collectAsStateWithLifecycle()
|
|
|
|
BitwardenTheme(
|
|
|
|
theme = state.theme,
|
|
|
|
) {
|
2023-09-28 22:18:03 +03:00
|
|
|
RootNavScreen(
|
|
|
|
onSplashScreenRemoved = { shouldShowSplashScreen = false },
|
|
|
|
)
|
2023-08-29 17:43:15 +03:00
|
|
|
}
|
|
|
|
}
|
2023-08-23 00:03:42 +03:00
|
|
|
}
|
2023-10-03 22:34:51 +03:00
|
|
|
|
|
|
|
override fun onNewIntent(intent: Intent) {
|
|
|
|
super.onNewIntent(intent)
|
2024-01-17 02:41:34 +03:00
|
|
|
mainViewModel.trySendAction(
|
2023-10-03 22:34:51 +03:00
|
|
|
action = MainAction.ReceiveNewIntent(
|
|
|
|
intent = intent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2024-01-26 19:26:17 +03:00
|
|
|
|
|
|
|
private fun observeViewModelEvents() {
|
|
|
|
mainViewModel
|
|
|
|
.eventFlow
|
|
|
|
.onEach { event ->
|
|
|
|
when (event) {
|
2024-01-28 21:25:15 +03:00
|
|
|
is MainEvent.CompleteAutofill -> {
|
|
|
|
handleCompleteAutofill(event)
|
|
|
|
}
|
|
|
|
|
2024-01-26 19:26:17 +03:00
|
|
|
is MainEvent.ScreenCaptureSettingChange -> {
|
|
|
|
handleScreenCaptureSettingChange(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.launchIn(lifecycleScope)
|
|
|
|
}
|
|
|
|
|
2024-01-28 21:25:15 +03:00
|
|
|
private fun handleCompleteAutofill(event: MainEvent.CompleteAutofill) {
|
|
|
|
autofillCompletionManager.completeAutofill(
|
|
|
|
activity = this,
|
|
|
|
cipherView = event.cipherView,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-26 19:26:17 +03:00
|
|
|
private fun handleScreenCaptureSettingChange(event: MainEvent.ScreenCaptureSettingChange) {
|
|
|
|
if (event.isAllowed) {
|
|
|
|
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
|
|
|
} else {
|
|
|
|
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 00:03:42 +03:00
|
|
|
}
|