diff options
| author | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2024-02-07 23:54:01 +0100 |
|---|---|---|
| committer | Jonatan Rhodin <jonatan.rhodin@mullvad.net> | 2024-02-07 23:54:01 +0100 |
| commit | 1b80203504f58fab47dc6dbe53f223084acecc6a (patch) | |
| tree | 3b9a918bbc882d7251eaeba4e734d8daa6ff08a6 /android | |
| parent | 4227f4aa6e047942644333fa071e2dfb59b2a74f (diff) | |
| parent | 4c1c57a8cf962585bb4601b4f5d954f3c293521f (diff) | |
| download | mullvadvpn-1b80203504f58fab47dc6dbe53f223084acecc6a.tar.xz mullvadvpn-1b80203504f58fab47dc6dbe53f223084acecc6a.zip | |
Merge branch 'google-play-issue-droid-578'
Diffstat (limited to 'android')
| -rw-r--r-- | android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt | 44 |
1 files changed, 21 insertions, 23 deletions
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt index 4298fa17fa..a0841c0746 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt @@ -9,8 +9,12 @@ import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.result.contract.ActivityResultContracts import androidx.core.view.WindowCompat +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle import kotlinx.coroutines.flow.filterIsInstance import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch import net.mullvad.mullvadvpn.compose.screen.MullvadApp import net.mullvad.mullvadvpn.di.paymentModule import net.mullvad.mullvadvpn.di.uiModule @@ -60,46 +64,40 @@ class MainActivity : ComponentActivity() { super.onCreate(savedInstanceState) setContent { AppTheme { MullvadApp() } } - } - fun initializeStateHandlerAndServiceConnection() { - requestNotificationPermissionIfMissing(requestNotificationPermissionLauncher) - serviceConnectionManager.bind( - vpnPermissionRequestHandler = ::requestVpnPermission, - apiEndpointConfiguration = intent?.getApiEndpointConfigurationExtras() - ) + // We use lifecycleScope here to get less start service in background exceptions + // Se this article for more information: + // https://medium.com/@lepicekmichal/android-background-service-without-hiccup-501e4479110f + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { + if (privacyDisclaimerRepository.hasAcceptedPrivacyDisclosure()) { + startServiceSuspend(waitForConnectedReady = false) + } + } + } } - suspend fun startServiceSuspend() { + suspend fun startServiceSuspend(waitForConnectedReady: Boolean = true) { requestNotificationPermissionIfMissing(requestNotificationPermissionLauncher) serviceConnectionManager.bind( vpnPermissionRequestHandler = ::requestVpnPermission, apiEndpointConfiguration = intent?.getApiEndpointConfigurationExtras() ) - // Ensure we wait until the service is ready - serviceConnectionManager.connectionState - .filterIsInstance<ServiceConnectionState.ConnectedReady>() - .first() + if (waitForConnectedReady) { + // Ensure we wait until the service is ready + serviceConnectionManager.connectionState + .filterIsInstance<ServiceConnectionState.ConnectedReady>() + .first() + } } override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { serviceConnectionManager.onVpnPermissionResult(resultCode == Activity.RESULT_OK) } - override fun onStart() { - super.onStart() - - if (privacyDisclaimerRepository.hasAcceptedPrivacyDisclosure()) { - initializeStateHandlerAndServiceConnection() - } - } - override fun onStop() { Log.d("mullvad", "Stopping main activity") super.onStop() - - // NOTE: `super.onStop()` must be called before unbinding due to the fragment state handling - // otherwise the fragments will believe there was an unexpected disconnect. serviceConnectionManager.unbind() } |
