diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-01-21 17:25:08 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-04-23 14:04:43 +0000 |
| commit | 68befbcd4f832ce4bf4cf47736e7e3c83c0c8c47 (patch) | |
| tree | 161039bb0870c73e6f8b3f90ef5727d84db71288 | |
| parent | 8ec8092d6e83d7d6382257489bc03a5edc2dc111 (diff) | |
| download | mullvadvpn-68befbcd4f832ce4bf4cf47736e7e3c83c0c8c47.tar.xz mullvadvpn-68befbcd4f832ce4bf4cf47736e7e3c83c0c8c47.zip | |
Refactor to centralize handling of service conn.
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt index 6915edfe9f..4d926b1f96 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt @@ -14,6 +14,7 @@ import android.view.WindowManager import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentManager +import kotlin.properties.Delegates.observable import net.mullvad.mullvadvpn.BuildConfig import net.mullvad.mullvadvpn.R import net.mullvad.mullvadvpn.dataproxy.MullvadProblemReport @@ -27,7 +28,6 @@ open class MainActivity : FragmentActivity() { private var isUiVisible = false private var service: MullvadVpnService.LocalBinder? = null - private var serviceConnection: ServiceConnection? = null private var visibleSecureScreens = HashSet<Fragment>() private val deviceIsTv by lazy { @@ -36,6 +36,16 @@ open class MainActivity : FragmentActivity() { uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION } + private var serviceConnection by observable<ServiceConnection?>( + null + ) { _, oldConnection, newConnection -> + oldConnection?.onDestroy() + + if (newConnection == null) { + serviceNotifier.notify(null) + } + } + private val serviceConnectionManager = object : android.content.ServiceConnection { override fun onServiceConnected(className: ComponentName, binder: IBinder) { android.util.Log.d("mullvad", "UI successfully connected to the service") @@ -47,26 +57,19 @@ open class MainActivity : FragmentActivity() { localBinder.serviceNotifier.subscribe(this@MainActivity) { service -> android.util.Log.d("mullvad", "UI connection to the service changed: $service") - serviceConnection?.onDestroy() serviceConnection = service?.let { safeService -> ServiceConnection(safeService, ::handleNewServiceConnection).apply { vpnPermission.onRequest = ::requestVpnPermission } } - - if (service == null) { - serviceNotifier.notify(null) - } } } override fun onServiceDisconnected(className: ComponentName) { android.util.Log.d("mullvad", "UI lost the connection to the service") - service?.serviceNotifier?.unsubscribe(this@MainActivity) - serviceConnection?.onDestroy() - service = null serviceConnection = null + service = null serviceNotifier.notify(null) } } @@ -124,7 +127,6 @@ open class MainActivity : FragmentActivity() { override fun onStop() { android.util.Log.d("mullvad", "Stoping main activity") isUiVisible = false - service?.isUiVisible = false service = null unbindService(serviceConnectionManager) @@ -133,7 +135,7 @@ open class MainActivity : FragmentActivity() { override fun onDestroy() { serviceNotifier.unsubscribeAll() - serviceConnection?.onDestroy() + serviceConnection = null super.onDestroy() } |
