diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-12-20 11:15:58 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-12-20 11:15:58 -0300 |
| commit | ec58b082eb71afc6cbf9a4b895c38496cbe8b17c (patch) | |
| tree | 02b811ece52b82b1abdb9808118e7096578fdedb | |
| parent | 2436b73ad8ea4ac4d2c995d9dff76591c9c960b7 (diff) | |
| parent | 87e515f32d7e34985d2c5cdf57102ea629245909 (diff) | |
| download | mullvadvpn-ec58b082eb71afc6cbf9a4b895c38496cbe8b17c.tar.xz mullvadvpn-ec58b082eb71afc6cbf9a4b895c38496cbe8b17c.zip | |
Merge branch 'cleanup-obsoleted-code'
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt | 17 | ||||
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt | 97 |
2 files changed, 34 insertions, 80 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt index 7423b91586..f22094dfe9 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -3,7 +3,6 @@ package net.mullvad.mullvadvpn.service import android.content.Intent import android.os.Binder import android.os.IBinder -import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope @@ -16,7 +15,7 @@ import net.mullvad.talpid.util.EventNotifier class MullvadVpnService : TalpidVpnService() { private val binder = LocalBinder() - private var resetComplete: CompletableDeferred<Unit>? = null + private var isStopping = false private lateinit var daemon: Deferred<MullvadDaemon> private lateinit var connectionProxy: ConnectionProxy @@ -34,10 +33,10 @@ class MullvadVpnService : TalpidVpnService() { } override fun onRebind(intent: Intent) { - resetComplete?.let { reset -> + if (isStopping) { tearDown() setUp() - reset.complete(Unit) + isStopping = false } } @@ -52,16 +51,8 @@ class MullvadVpnService : TalpidVpnService() { } inner class LocalBinder : Binder() { - val daemon - get() = this@MullvadVpnService.daemon val serviceNotifier get() = this@MullvadVpnService.serviceNotifier - val connectionProxy - get() = this@MullvadVpnService.connectionProxy - val connectivityListener - get() = this@MullvadVpnService.connectivityListener - val resetComplete - get() = this@MullvadVpnService.resetComplete fun stop() { this@MullvadVpnService.stop() @@ -96,7 +87,7 @@ class MullvadVpnService : TalpidVpnService() { } private fun stop() { - resetComplete = CompletableDeferred() + isStopping = true serviceNotifier.notify(null) 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 f9c9f24bac..71c8ffdfb5 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt @@ -6,22 +6,11 @@ import android.content.Intent import android.os.Bundle import android.os.IBinder import android.support.v4.app.FragmentActivity -import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.Job import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import net.mullvad.mullvadvpn.R -import net.mullvad.mullvadvpn.dataproxy.AccountCache -import net.mullvad.mullvadvpn.dataproxy.AppVersionInfoCache -import net.mullvad.mullvadvpn.dataproxy.ConnectionProxy -import net.mullvad.mullvadvpn.dataproxy.KeyStatusListener -import net.mullvad.mullvadvpn.dataproxy.LocationInfoCache import net.mullvad.mullvadvpn.dataproxy.MullvadProblemReport -import net.mullvad.mullvadvpn.dataproxy.RelayListListener -import net.mullvad.mullvadvpn.dataproxy.WwwAuthTokenRetriever -import net.mullvad.mullvadvpn.service.MullvadDaemon import net.mullvad.mullvadvpn.service.MullvadVpnService import net.mullvad.talpid.util.EventNotifier @@ -30,40 +19,21 @@ class MainActivity : FragmentActivity() { val KEY_SHOULD_CONNECT = "should_connect" } - private var serviceConnection: ServiceConnection? = null - private var serviceConnectionSubscription: Int? = null - - var daemon = CompletableDeferred<MullvadDaemon>() - private set - var service = CompletableDeferred<MullvadVpnService.LocalBinder>() - private set - val problemReport = MullvadProblemReport() val serviceNotifier = EventNotifier<ServiceConnection?>(null) - val appVersionInfoCache: AppVersionInfoCache - get() = serviceConnection!!.appVersionInfoCache - val connectionProxy: ConnectionProxy - get() = serviceConnection!!.connectionProxy - val keyStatusListener: KeyStatusListener - get() = serviceConnection!!.keyStatusListener - val relayListListener: RelayListListener - get() = serviceConnection!!.relayListListener - val locationInfoCache: LocationInfoCache - get() = serviceConnection!!.locationInfoCache - val accountCache: AccountCache - get() = serviceConnection!!.accountCache - val wwwAuthTokenRetriever: WwwAuthTokenRetriever - get() = serviceConnection!!.wwwAuthTokenRetriever - - private var quitJob: Job? = null - private var serviceToStop: MullvadVpnService.LocalBinder? = null - private var waitForDaemonJob: Job? = null + private var service: MullvadVpnService.LocalBinder? = null + private var serviceConnection: ServiceConnection? = null + private var serviceConnectionSubscription: Int? = null + private var shouldConnect = false + private var shouldStopService = false private val serviceConnectionManager = object : android.content.ServiceConnection { override fun onServiceConnected(className: ComponentName, binder: IBinder) { val localBinder = binder as MullvadVpnService.LocalBinder + service = localBinder + serviceConnectionSubscription = localBinder.serviceNotifier.subscribe { service -> serviceConnection?.onDestroy() @@ -73,32 +43,22 @@ class MainActivity : FragmentActivity() { serviceConnection = newConnection serviceNotifier.notify(newConnection) - } - waitForDaemonJob = GlobalScope.launch(Dispatchers.Default) { - localBinder.resetComplete?.await() - service.complete(localBinder) - daemon.complete(localBinder.daemon.await()) + if (shouldConnect) { + tryToConnect() + } } } override fun onServiceDisconnected(className: ComponentName) { - waitForDaemonJob?.cancel() - waitForDaemonJob = null - - serviceConnectionSubscription?.let { subscription -> - runBlocking { - service.await().serviceNotifier.unsubscribe(subscription) + serviceConnectionSubscription?.let { subscriptionId -> + service?.apply { + serviceNotifier.unsubscribe(subscriptionId) } - serviceConnection = null } - service.cancel() - daemon.cancel() - - service = CompletableDeferred<MullvadVpnService.LocalBinder>() - daemon = CompletableDeferred<MullvadDaemon>() - + serviceConnection = null + serviceConnectionSubscription = null serviceNotifier.notify(null) } } @@ -112,7 +72,8 @@ class MainActivity : FragmentActivity() { } if (intent.getBooleanExtra(KEY_SHOULD_CONNECT, false)) { - connectionProxy.connect() + shouldConnect = true + tryToConnect() } } @@ -130,11 +91,12 @@ class MainActivity : FragmentActivity() { } override fun onStop() { - quitJob?.cancel() - serviceNotifier.unsubscribeAll() - serviceToStop?.apply { stop() } + if (shouldStopService) { + service?.apply { stop() } + } + unbindService(serviceConnectionManager) super.onStop() @@ -143,9 +105,6 @@ class MainActivity : FragmentActivity() { override fun onDestroy() { serviceConnection?.onDestroy() - waitForDaemonJob?.cancel() - daemon.cancel() - super.onDestroy() } @@ -168,10 +127,14 @@ class MainActivity : FragmentActivity() { } fun quit() { - quitJob?.cancel() - quitJob = GlobalScope.launch(Dispatchers.Main) { - serviceToStop = service.await() - finishAndRemoveTask() + shouldStopService = true + finishAndRemoveTask() + } + + private fun tryToConnect() { + serviceConnection?.apply { + connectionProxy.connect() + shouldConnect = false } } @@ -183,6 +146,6 @@ class MainActivity : FragmentActivity() { } private fun setVpnPermission(allow: Boolean) = GlobalScope.launch(Dispatchers.Default) { - connectionProxy.vpnPermission.complete(allow) + serviceConnection?.connectionProxy?.vpnPermission?.complete(allow) } } |
