Skip to content

Commit

Permalink
Merge branch 'google-play-issue-droid-578'
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Feb 7, 2024
2 parents 4227f4a + 4c1c57a commit 1b80203
Showing 1 changed file with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit 1b80203

Please sign in to comment.