Skip to content

Commit

Permalink
Bind ServiceConnectionManager to Activity
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Jul 9, 2024
1 parent 1529a38 commit 117daca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import net.mullvad.mullvadvpn.repository.RelayOverridesRepository
import net.mullvad.mullvadvpn.repository.SettingsRepository
import net.mullvad.mullvadvpn.repository.SplashCompleteRepository
import net.mullvad.mullvadvpn.repository.SplitTunnelingRepository
import net.mullvad.mullvadvpn.ui.MainActivity
import net.mullvad.mullvadvpn.ui.serviceconnection.AppVersionInfoRepository
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager
import net.mullvad.mullvadvpn.usecase.AccountExpiryNotificationUseCase
Expand Down Expand Up @@ -97,9 +98,11 @@ val uiModule = module {
single<String>(named(SELF_PACKAGE_NAME)) { androidContext().packageName }

viewModel { SplitTunnelingViewModel(get(), get(), Dispatchers.Default) }

single { ApplicationsProvider(get(), get(named(SELF_PACKAGE_NAME))) }

single { ServiceConnectionManager(androidContext()) }
scope<MainActivity> { scoped { ServiceConnectionManager(androidContext()) } }

single { InetAddressValidator.getInstance() }
single { androidContext().resources }
single { androidContext().assets }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.core.view.WindowCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import co.touchlab.kermit.Logger
import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.compose.screen.MullvadApp
import net.mullvad.mullvadvpn.di.paymentModule
Expand All @@ -21,36 +22,34 @@ import net.mullvad.mullvadvpn.repository.PrivacyDisclaimerRepository
import net.mullvad.mullvadvpn.repository.SplashCompleteRepository
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager
import net.mullvad.mullvadvpn.viewmodel.NoDaemonViewModel
import org.koin.android.ext.android.getKoin
import org.koin.android.ext.android.inject
import org.koin.android.scope.AndroidScopeComponent
import org.koin.androidx.scope.createActivityScope
import org.koin.core.context.loadKoinModules

class MainActivity : ComponentActivity() {
class MainActivity : ComponentActivity(), AndroidScopeComponent {
override val scope = createActivityScope()

private val requestNotificationPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
// NotificationManager.areNotificationsEnabled is used to check the state rather than
// handling the callback value.
}

private lateinit var privacyDisclaimerRepository: PrivacyDisclaimerRepository
private lateinit var serviceConnectionManager: ServiceConnectionManager
private lateinit var splashCompleteRepository: SplashCompleteRepository
private val intentProvider by inject<IntentProvider>()
private val noDaemonViewModel by inject<NoDaemonViewModel>()
private val privacyDisclaimerRepository by inject<PrivacyDisclaimerRepository>()
private val serviceConnectionManager by inject<ServiceConnectionManager>()
private val splashCompleteRepository by inject<SplashCompleteRepository>()

private var isReadyNextDraw: Boolean = false
private lateinit var noDaemonViewModel: NoDaemonViewModel
private lateinit var intentProvider: IntentProvider

override fun onCreate(savedInstanceState: Bundle?) {
loadKoinModules(listOf(uiModule, paymentModule))

// Tell the system that we will draw behind the status bar and navigation bar
WindowCompat.setDecorFitsSystemWindows(window, false)

with(getKoin()) {
privacyDisclaimerRepository = get()
serviceConnectionManager = get()
noDaemonViewModel = get()
intentProvider = get()
splashCompleteRepository = get()
}
lifecycle.addObserver(noDaemonViewModel)

installSplashScreen().setKeepOnScreenCondition {
Expand All @@ -64,6 +63,7 @@ class MainActivity : ComponentActivity() {
if (savedInstanceState == null) {
intentProvider.setStartIntent(intent)
}

setContent { AppTheme { MullvadApp() } }

// This is to protect against tapjacking attacks
Expand All @@ -74,6 +74,7 @@ class MainActivity : ComponentActivity() {
// https://medium.com/@lepicekmichal/android-background-service-without-hiccup-501e4479110f
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
Logger.d("MainActivity $this: STARTED")
if (privacyDisclaimerRepository.hasAcceptedPrivacyDisclosure()) {
bindService()
}
Expand Down

0 comments on commit 117daca

Please sign in to comment.