@@ -9,8 +9,12 @@ import androidx.activity.ComponentActivity
9
9
import androidx.activity.compose.setContent
10
10
import androidx.activity.result.contract.ActivityResultContracts
11
11
import androidx.core.view.WindowCompat
12
+ import androidx.lifecycle.Lifecycle
13
+ import androidx.lifecycle.lifecycleScope
14
+ import androidx.lifecycle.repeatOnLifecycle
12
15
import kotlinx.coroutines.flow.filterIsInstance
13
16
import kotlinx.coroutines.flow.first
17
+ import kotlinx.coroutines.launch
14
18
import net.mullvad.mullvadvpn.compose.screen.MullvadApp
15
19
import net.mullvad.mullvadvpn.di.paymentModule
16
20
import net.mullvad.mullvadvpn.di.uiModule
@@ -60,46 +64,40 @@ class MainActivity : ComponentActivity() {
60
64
super .onCreate(savedInstanceState)
61
65
62
66
setContent { AppTheme { MullvadApp () } }
63
- }
64
67
65
- fun initializeStateHandlerAndServiceConnection () {
66
- requestNotificationPermissionIfMissing(requestNotificationPermissionLauncher)
67
- serviceConnectionManager.bind(
68
- vpnPermissionRequestHandler = ::requestVpnPermission,
69
- apiEndpointConfiguration = intent?.getApiEndpointConfigurationExtras()
70
- )
68
+ // We use lifecycleScope here to get less start service in background exceptions
69
+ // Se this article for more information:
70
+ // https://medium.com/@lepicekmichal/android-background-service-without-hiccup-501e4479110f
71
+ lifecycleScope.launch {
72
+ repeatOnLifecycle(Lifecycle .State .STARTED ) {
73
+ if (privacyDisclaimerRepository.hasAcceptedPrivacyDisclosure()) {
74
+ startServiceSuspend(waitForConnectedReady = false )
75
+ }
76
+ }
77
+ }
71
78
}
72
79
73
- suspend fun startServiceSuspend () {
80
+ suspend fun startServiceSuspend (waitForConnectedReady : Boolean = true ) {
74
81
requestNotificationPermissionIfMissing(requestNotificationPermissionLauncher)
75
82
serviceConnectionManager.bind(
76
83
vpnPermissionRequestHandler = ::requestVpnPermission,
77
84
apiEndpointConfiguration = intent?.getApiEndpointConfigurationExtras()
78
85
)
79
- // Ensure we wait until the service is ready
80
- serviceConnectionManager.connectionState
81
- .filterIsInstance<ServiceConnectionState .ConnectedReady >()
82
- .first()
86
+ if (waitForConnectedReady) {
87
+ // Ensure we wait until the service is ready
88
+ serviceConnectionManager.connectionState
89
+ .filterIsInstance<ServiceConnectionState .ConnectedReady >()
90
+ .first()
91
+ }
83
92
}
84
93
85
94
override fun onActivityResult (requestCode : Int , resultCode : Int , resultData : Intent ? ) {
86
95
serviceConnectionManager.onVpnPermissionResult(resultCode == Activity .RESULT_OK )
87
96
}
88
97
89
- override fun onStart () {
90
- super .onStart()
91
-
92
- if (privacyDisclaimerRepository.hasAcceptedPrivacyDisclosure()) {
93
- initializeStateHandlerAndServiceConnection()
94
- }
95
- }
96
-
97
98
override fun onStop () {
98
99
Log .d(" mullvad" , " Stopping main activity" )
99
100
super .onStop()
100
-
101
- // NOTE: `super.onStop()` must be called before unbinding due to the fragment state handling
102
- // otherwise the fragments will believe there was an unexpected disconnect.
103
101
serviceConnectionManager.unbind()
104
102
}
105
103
0 commit comments