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 4, 2024
1 parent 6fcf6e0 commit 8a415f7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import net.mullvad.mullvadvpn.repository.RelayListRepository
import net.mullvad.mullvadvpn.repository.RelayOverridesRepository
import net.mullvad.mullvadvpn.repository.SettingsRepository
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 @@ -100,9 +101,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 @@ -9,6 +9,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 @@ -19,10 +20,14 @@ import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.repository.PrivacyDisclaimerRepository
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager
import net.mullvad.mullvadvpn.viewmodel.NoDaemonViewModel
import org.koin.android.ext.android.getKoin
import org.koin.android.scope.AndroidScopeComponent
import org.koin.androidx.scope.activityScope
import org.koin.androidx.scope.createActivityScope
import org.koin.core.context.loadKoinModules

class MainActivity : ComponentActivity() {
class MainActivity : ComponentActivity(), AndroidScopeComponent {
override val scope by activityScope()

private val requestNotificationPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
// NotificationManager.areNotificationsEnabled is used to check the state rather than
Expand All @@ -40,7 +45,7 @@ class MainActivity : ComponentActivity() {
// Tell the system that we will draw behind the status bar and navigation bar
WindowCompat.setDecorFitsSystemWindows(window, false)

with(getKoin()) {
with(createActivityScope()) {
privacyDisclaimerRepository = get()
serviceConnectionManager = get()
noDaemonViewModel = get()
Expand All @@ -65,6 +70,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 All @@ -82,8 +88,14 @@ class MainActivity : ComponentActivity() {
serviceConnectionManager.bind()
}

override fun onStart() {
super.onStart()
Logger.d("MainActivity $this: onStart")
}

override fun onStop() {
super.onStop()
Logger.d("MainActivity $this: onStop")
serviceConnectionManager.unbind()
}

Expand Down

0 comments on commit 8a415f7

Please sign in to comment.