diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-01-07 11:29:50 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-01-07 11:29:50 -0300 |
| commit | 243d8d20ef26dbb7d82881b9bb1791509d6c322e (patch) | |
| tree | 186e6e5b0aff3c8b36bff57b52f1c96328c84987 /android/src | |
| parent | 54d6b88002a372c1e24535a08f211fef7bf7c679 (diff) | |
| parent | d024ebe236c91817eceac4847177d5e3d6b31a18 (diff) | |
| download | mullvadvpn-243d8d20ef26dbb7d82881b9bb1791509d6c322e.tar.xz mullvadvpn-243d8d20ef26dbb7d82881b9bb1791509d6c322e.zip | |
Merge branch 'remove-deferred-fields'
Diffstat (limited to 'android/src')
12 files changed, 58 insertions, 138 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AccountCache.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AccountCache.kt index 44001c5de9..35abe6d53a 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AccountCache.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AccountCache.kt @@ -1,6 +1,5 @@ package net.mullvad.mullvadvpn.dataproxy -import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job @@ -12,7 +11,7 @@ import org.joda.time.format.DateTimeFormat val EXPIRY_FORMAT = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss z") -class AccountCache(val settingsListener: SettingsListener, val daemon: Deferred<MullvadDaemon>) { +class AccountCache(val settingsListener: SettingsListener, val daemon: MullvadDaemon) { private var fetchJob: Job? = null private var accountNumber: String? = null private var accountExpiry: DateTime? = null @@ -55,7 +54,7 @@ class AccountCache(val settingsListener: SettingsListener, val daemon: Deferred< private fun fetchAccountExpiry() = GlobalScope.launch(Dispatchers.Default) { val accountNumber = this@AccountCache.accountNumber val accountData = accountNumber?.let { account -> - val result = daemon.await().getAccountData(account) + val result = daemon.getAccountData(account) when (result) { is GetAccountDataResult.Ok -> result.accountData diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoCache.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoCache.kt index ac01929b1b..a192fb0f75 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoCache.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoCache.kt @@ -1,14 +1,13 @@ package net.mullvad.mullvadvpn.dataproxy import android.content.Context -import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import net.mullvad.mullvadvpn.model.AppVersionInfo import net.mullvad.mullvadvpn.service.MullvadDaemon -class AppVersionInfoCache(val context: Context, val daemon: Deferred<MullvadDaemon>) { +class AppVersionInfoCache(val context: Context, val daemon: MullvadDaemon) { companion object { val LEGACY_SHARED_PREFERENCES = "app_version_info_cache" } @@ -69,11 +68,10 @@ class AppVersionInfoCache(val context: Context, val daemon: Deferred<MullvadDaem fun onDestroy() { setUpJob.cancel() - tearDown() + daemon.onAppVersionInfoChange = null } private fun setUp() = GlobalScope.launch(Dispatchers.Default) { - val daemon = this@AppVersionInfoCache.daemon.await() val currentVersion = daemon.getCurrentVersion() version = currentVersion @@ -91,8 +89,4 @@ class AppVersionInfoCache(val context: Context, val daemon: Deferred<MullvadDaem } } } - - private fun tearDown() = GlobalScope.launch(Dispatchers.Default) { - daemon.await().onAppVersionInfoChange = null - } } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/KeyStatusListener.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/KeyStatusListener.kt index 04748ed754..74b10de728 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/KeyStatusListener.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/KeyStatusListener.kt @@ -1,15 +1,12 @@ package net.mullvad.mullvadvpn.dataproxy -import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import net.mullvad.mullvadvpn.model.KeygenEvent import net.mullvad.mullvadvpn.service.MullvadDaemon -class KeyStatusListener(val asyncDaemon: Deferred<MullvadDaemon>) { - private var daemon: MullvadDaemon? = null - +class KeyStatusListener(val daemon: MullvadDaemon) { private val setUpJob = setUp() var keyStatus: KeygenEvent? = null @@ -33,48 +30,47 @@ class KeyStatusListener(val asyncDaemon: Deferred<MullvadDaemon>) { } private fun setUp() = GlobalScope.launch(Dispatchers.Default) { - daemon = asyncDaemon.await() - daemon?.onKeygenEvent = { event -> keyStatus = event } - val wireguardKey = daemon?.getWireguardKey() + daemon.onKeygenEvent = { event -> keyStatus = event } + val wireguardKey = daemon.getWireguardKey() if (wireguardKey != null) { keyStatus = KeygenEvent.NewKey(wireguardKey, null, null) } } fun generateKey() = GlobalScope.launch(Dispatchers.Default) { - setUpJob.join() - val oldStatus = keyStatus - val newStatus = daemon?.generateWireguardKey() - val newFailure = newStatus?.failure() - if (oldStatus is KeygenEvent.NewKey && newStatus != null) { - keyStatus = KeygenEvent.NewKey(oldStatus.publicKey, - oldStatus.verified, - newFailure) - } else { - keyStatus = newStatus - } + setUpJob.join() + val oldStatus = keyStatus + val newStatus = daemon.generateWireguardKey() + val newFailure = newStatus?.failure() + if (oldStatus is KeygenEvent.NewKey && newStatus != null) { + keyStatus = KeygenEvent.NewKey(oldStatus.publicKey, + oldStatus.verified, + newFailure) + } else { + keyStatus = newStatus + } } fun verifyKey() = GlobalScope.launch(Dispatchers.Default) { - setUpJob.join() - val verified = daemon?.verifyWireguardKey() - // Only update verification status if the key is actually there - when (val state = keyStatus) { - is KeygenEvent.NewKey -> { - keyStatus = KeygenEvent.NewKey(state.publicKey, - verified, - state.replacementFailure) - } + setUpJob.join() + val verified = daemon.verifyWireguardKey() + // Only update verification status if the key is actually there + when (val state = keyStatus) { + is KeygenEvent.NewKey -> { + keyStatus = KeygenEvent.NewKey(state.publicKey, + verified, + state.replacementFailure) } + } } fun onDestroy() { setUpJob.cancel() - daemon?.onKeygenEvent = null + daemon.onKeygenEvent = null } private fun retryKeyGeneration() = GlobalScope.launch(Dispatchers.Default) { setUpJob.join() - keyStatus = daemon?.generateWireguardKey() + keyStatus = daemon.generateWireguardKey() } } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt index a511156bf3..7d04bcc2c7 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt @@ -1,6 +1,5 @@ package net.mullvad.mullvadvpn.dataproxy -import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job @@ -21,20 +20,19 @@ const val MAX_DELAY: Long = 30 * 60 * 1000 const val MAX_RETRIES: Int = 17 // ceil(log2(MAX_DELAY / DELAY_SCALE) + 1) class LocationInfoCache( - val daemon: Deferred<MullvadDaemon>, - val connectivityListener: Deferred<ConnectivityListener>, + val daemon: MullvadDaemon, + val connectivityListener: ConnectivityListener, val relayListListener: RelayListListener ) { private var lastKnownRealLocation: GeoIpLocation? = null private var activeFetch: Job? = null - private val connectivityListenerId = GlobalScope.async(Dispatchers.Default) { - connectivityListener.await().connectivityNotifier.subscribe { isConnected -> + private val connectivityListenerId = + connectivityListener.connectivityNotifier.subscribe { isConnected -> if (isConnected) { fetchLocation() } } - } var onNewLocation: ((GeoIpLocation?) -> Unit)? = null set(value) { @@ -132,7 +130,7 @@ class LocationInfoCache( } private fun executeFetch() = GlobalScope.async(Dispatchers.Default) { - daemon.await().getCurrentLocation() + daemon.getCurrentLocation() } private suspend fun delayFetch(retryAttempt: Int) { @@ -149,10 +147,10 @@ class LocationInfoCache( delay(duration) } - private suspend fun shouldRetryFetch(): Boolean { + private fun shouldRetryFetch(): Boolean { val state = this.state - return connectivityListener.await().isConnected && + return connectivityListener.isConnected && (state is TunnelState.Disconnected || state is TunnelState.Connected) } } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/RelayListListener.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/RelayListListener.kt index 056331d223..5fe1918551 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/RelayListListener.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/RelayListListener.kt @@ -1,6 +1,5 @@ package net.mullvad.mullvadvpn.dataproxy -import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -11,10 +10,7 @@ import net.mullvad.mullvadvpn.relaylist.RelayItem import net.mullvad.mullvadvpn.relaylist.RelayList import net.mullvad.mullvadvpn.service.MullvadDaemon -class RelayListListener( - val daemon: Deferred<MullvadDaemon>, - val settingsListener: SettingsListener -) { +class RelayListListener(val daemon: MullvadDaemon, val settingsListener: SettingsListener) { private val setUpJob = setUp() private var relayList: RelayList? = null @@ -45,12 +41,7 @@ class RelayListListener( fun onDestroy() { setUpJob.cancel() settingsListener.onRelaySettingsChange = null - - if (daemon.isActive) { - daemon.cancel() - } else { - daemon.getCompleted().onRelayListChange = null - } + daemon.onRelayListChange = null } private fun setUp() = GlobalScope.launch(Dispatchers.Default) { @@ -58,14 +49,14 @@ class RelayListListener( fetchInitialRelayList() } - private suspend fun setUpListener() { - daemon.await().onRelayListChange = { relayLocations -> + private fun setUpListener() { + daemon.onRelayListChange = { relayLocations -> relayListChanged(RelayList(relayLocations)) } } - private suspend fun fetchInitialRelayList() { - val relayLocations = daemon.await().getRelayLocations() + private fun fetchInitialRelayList() { + val relayLocations = daemon.getRelayLocations() synchronized(this) { if (relayList == null) { diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/SettingsListener.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/SettingsListener.kt index a1c87442ba..a822526890 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/SettingsListener.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/SettingsListener.kt @@ -1,19 +1,14 @@ package net.mullvad.mullvadvpn.dataproxy -import kotlinx.coroutines.Deferred -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch import net.mullvad.mullvadvpn.model.RelaySettings import net.mullvad.mullvadvpn.model.Settings import net.mullvad.mullvadvpn.service.MullvadDaemon -class SettingsListener(val asyncDaemon: Deferred<MullvadDaemon>) { - private lateinit var daemon: MullvadDaemon - - private val setUpJob = setUp() +class SettingsListener(val daemon: MullvadDaemon) { + private val listenerId = daemon.onSettingsChange.subscribe { maybeSettings -> + maybeSettings?.let { settings -> handleNewSettings(settings) } + } - private var listenerId = -1 private var settings: Settings? = null var onAccountNumberChange: ((String?) -> Unit)? = null @@ -33,21 +28,11 @@ class SettingsListener(val asyncDaemon: Deferred<MullvadDaemon>) { } fun onDestroy() { - setUpJob.cancel() - if (listenerId != -1) { daemon.onSettingsChange.unsubscribe(listenerId) } } - private fun setUp() = GlobalScope.launch(Dispatchers.Default) { - daemon = asyncDaemon.await() - - listenerId = daemon.onSettingsChange.subscribe { maybeSettings -> - maybeSettings?.let { settings -> handleNewSettings(settings) } - } - } - private fun handleNewSettings(newSettings: Settings) { synchronized(this) { if (settings?.accountToken != newSettings.accountToken) { diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/WwwAuthTokenRetriever.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/WwwAuthTokenRetriever.kt deleted file mode 100644 index 41f778c349..0000000000 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/WwwAuthTokenRetriever.kt +++ /dev/null @@ -1,22 +0,0 @@ -package net.mullvad.mullvadvpn.dataproxy - -import kotlinx.coroutines.Deferred -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch -import net.mullvad.mullvadvpn.service.MullvadDaemon - -class WwwAuthTokenRetriever(val asyncDaemon: Deferred<MullvadDaemon>) { - private var daemon: MullvadDaemon? = null - private val setUpJob = setUp() - - private fun setUp() = GlobalScope.launch(Dispatchers.Default) { - daemon = asyncDaemon.await() - } - - suspend fun getAuthToken(): String { - setUpJob.join() - // returning an empty string is valid in case of any failures - return daemon?.getWwwAuthToken() ?: "" - } -} diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ConnectFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ConnectFragment.kt index 0bd1fff0d2..1c41c912ac 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ConnectFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ConnectFragment.kt @@ -49,8 +49,7 @@ class ConnectFragment : ServiceDependentFragment() { } headerBar = HeaderBar(view, resources) - notificationBanner = - NotificationBanner(view, parentActivity, appVersionInfoCache, wwwAuthTokenRetriever) + notificationBanner = NotificationBanner(view, parentActivity, appVersionInfoCache, daemon) status = ConnectionStatus(view, resources) locationInfo = LocationInfo(view, context!!) diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/NotificationBanner.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/NotificationBanner.kt index 43805321aa..3214248f3d 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/NotificationBanner.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/NotificationBanner.kt @@ -13,9 +13,9 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.launch import net.mullvad.mullvadvpn.R import net.mullvad.mullvadvpn.dataproxy.AppVersionInfoCache -import net.mullvad.mullvadvpn.dataproxy.WwwAuthTokenRetriever import net.mullvad.mullvadvpn.model.KeygenEvent import net.mullvad.mullvadvpn.model.TunnelState +import net.mullvad.mullvadvpn.service.MullvadDaemon import net.mullvad.talpid.tunnel.ActionAfterDisconnect import net.mullvad.talpid.tunnel.ErrorState import net.mullvad.talpid.tunnel.ErrorStateCause @@ -25,7 +25,7 @@ class NotificationBanner( val parentView: View, val context: Context, val versionInfoCache: AppVersionInfoCache, - val authTokenRetriever: WwwAuthTokenRetriever + val daemon: MullvadDaemon ) { enum class ExternalLink { Download, KeyManagement } @@ -61,8 +61,8 @@ class NotificationBanner( } override fun onClick(): Job { - return GlobalScope.launch(Dispatchers.Main) { - val token = authTokenRetriever.getAuthToken() + return GlobalScope.launch(Dispatchers.Default) { + val token = daemon.getWwwAuthToken() val url = Uri.parse(keyManagementUrl + "?token=" + token) context.startActivity(Intent(Intent.ACTION_VIEW, url)) } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ServiceConnection.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ServiceConnection.kt index 831d52274f..af4f49efda 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ServiceConnection.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ServiceConnection.kt @@ -1,46 +1,31 @@ package net.mullvad.mullvadvpn.ui -import kotlinx.coroutines.CompletableDeferred import net.mullvad.mullvadvpn.dataproxy.AccountCache import net.mullvad.mullvadvpn.dataproxy.AppVersionInfoCache import net.mullvad.mullvadvpn.dataproxy.KeyStatusListener import net.mullvad.mullvadvpn.dataproxy.LocationInfoCache import net.mullvad.mullvadvpn.dataproxy.RelayListListener import net.mullvad.mullvadvpn.dataproxy.SettingsListener -import net.mullvad.mullvadvpn.dataproxy.WwwAuthTokenRetriever -import net.mullvad.mullvadvpn.service.MullvadDaemon import net.mullvad.mullvadvpn.service.ServiceInstance -import net.mullvad.talpid.ConnectivityListener class ServiceConnection(private val service: ServiceInstance, val mainActivity: MainActivity) { - private val asyncDaemon = CompletableDeferred<MullvadDaemon>() - private val asyncConnectivityListener = CompletableDeferred<ConnectivityListener>() - val daemon = service.daemon val connectionProxy = service.connectionProxy val connectivityListener = service.connectivityListener - val appVersionInfoCache = AppVersionInfoCache(mainActivity, asyncDaemon) - val keyStatusListener = KeyStatusListener(asyncDaemon) - val settingsListener = SettingsListener(asyncDaemon) - val accountCache = AccountCache(settingsListener, asyncDaemon) - var relayListListener = RelayListListener(asyncDaemon, settingsListener) - val locationInfoCache = - LocationInfoCache(asyncDaemon, asyncConnectivityListener, relayListListener) - val wwwAuthTokenRetriever = WwwAuthTokenRetriever(asyncDaemon) + val appVersionInfoCache = AppVersionInfoCache(mainActivity, daemon) + val keyStatusListener = KeyStatusListener(daemon) + val settingsListener = SettingsListener(daemon) + val accountCache = AccountCache(settingsListener, daemon) + var relayListListener = RelayListListener(daemon, settingsListener) + val locationInfoCache = LocationInfoCache(daemon, connectivityListener, relayListListener) init { - asyncDaemon.complete(daemon) - asyncConnectivityListener.complete(connectivityListener) appVersionInfoCache.onCreate() - connectionProxy.mainActivity = mainActivity } fun onDestroy() { - asyncDaemon.cancel() - asyncConnectivityListener.cancel() - accountCache.onDestroy() appVersionInfoCache.onDestroy() keyStatusListener.onDestroy() diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ServiceDependentFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ServiceDependentFragment.kt index d38f2f4384..fa855137db 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ServiceDependentFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ServiceDependentFragment.kt @@ -7,7 +7,6 @@ import net.mullvad.mullvadvpn.dataproxy.KeyStatusListener import net.mullvad.mullvadvpn.dataproxy.LocationInfoCache import net.mullvad.mullvadvpn.dataproxy.RelayListListener import net.mullvad.mullvadvpn.dataproxy.SettingsListener -import net.mullvad.mullvadvpn.dataproxy.WwwAuthTokenRetriever import net.mullvad.mullvadvpn.service.MullvadDaemon import net.mullvad.talpid.ConnectivityListener @@ -39,9 +38,6 @@ open class ServiceDependentFragment : ServiceAwareFragment() { lateinit var settingsListener: SettingsListener private set - lateinit var wwwAuthTokenRetriever: WwwAuthTokenRetriever - private set - override fun onNewServiceConnection(serviceConnection: ServiceConnection) { accountCache = serviceConnection.accountCache appVersionInfoCache = serviceConnection.appVersionInfoCache @@ -52,6 +48,5 @@ open class ServiceDependentFragment : ServiceAwareFragment() { locationInfoCache = serviceConnection.locationInfoCache relayListListener = serviceConnection.relayListListener settingsListener = serviceConnection.settingsListener - wwwAuthTokenRetriever = serviceConnection.wwwAuthTokenRetriever } } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/WireguardKeyFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/WireguardKeyFragment.kt index 7bf8174ebe..f24a5b7c79 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/WireguardKeyFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/WireguardKeyFragment.kt @@ -85,7 +85,7 @@ class WireguardKeyFragment : ServiceDependentFragment() { override fun onClick(): Job { return GlobalScope.launch(Dispatchers.Default) { - val token = wwwAuthTokenRetriever.getAuthToken() + val token = daemon.getWwwAuthToken() val intent = Intent(Intent.ACTION_VIEW, Uri.parse(keyUrl + "?token=" + token)) startActivity(intent) |
