diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-06-14 14:23:18 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-06-18 10:40:26 +0000 |
| commit | 87013396a9acd88c0fcd3e45f75d2de5cc3d1a01 (patch) | |
| tree | df3101bacce84812da43ca9d69ef5175d2584393 /android | |
| parent | 2dc2f762228ed61cd27971fefe74c3540778f1a1 (diff) | |
| download | mullvadvpn-87013396a9acd88c0fcd3e45f75d2de5cc3d1a01.tar.xz mullvadvpn-87013396a9acd88c0fcd3e45f75d2de5cc3d1a01.zip | |
Use `LocationInfoCache`
Diffstat (limited to 'android')
4 files changed, 37 insertions, 55 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt index 9c1804ce68..947bf82345 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt @@ -1,6 +1,5 @@ package net.mullvad.mullvadvpn -import kotlinx.coroutines.async import kotlinx.coroutines.launch import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.Deferred @@ -21,6 +20,7 @@ import android.view.ViewGroup import android.widget.Button import android.widget.ImageButton +import net.mullvad.mullvadvpn.dataproxy.LocationInfoCache import net.mullvad.mullvadvpn.model.GeoIpLocation import net.mullvad.mullvadvpn.model.TunnelStateTransition @@ -32,6 +32,7 @@ class ConnectFragment : Fragment() { private lateinit var locationInfo: LocationInfo private lateinit var parentActivity: MainActivity + private lateinit var locationInfoCache: LocationInfoCache private var daemon = CompletableDeferred<MullvadDaemon>() private var vpnPermission = CompletableDeferred<Unit>() @@ -50,6 +51,7 @@ class ConnectFragment : Fragment() { super.onAttach(context) parentActivity = context as MainActivity + locationInfoCache = parentActivity.locationInfoCache waitForDaemonJob = waitForDaemon(parentActivity.asyncDaemon) } @@ -71,7 +73,7 @@ class ConnectFragment : Fragment() { headerBar = HeaderBar(view, context!!) notificationBanner = NotificationBanner(view) status = ConnectionStatus(view, context!!) - locationInfo = LocationInfo(view, daemon) + locationInfo = LocationInfo(view, locationInfoCache) actionButton = ConnectActionButton(view) actionButton.apply { @@ -86,11 +88,16 @@ class ConnectFragment : Fragment() { } override fun onDestroyView() { + locationInfo.onDestroy() + waitForDaemonJob?.cancel() attachListenerJob?.cancel() + detachListener() + generateWireguardKeyJob.cancel() updateViewJob?.cancel() + super.onDestroyView() } @@ -184,7 +191,7 @@ class ConnectFragment : Fragment() { headerBar.setState(state) notificationBanner.setState(state) status.setState(state) - locationInfo.setState(state) + locationInfoCache.setState(state) } private fun openSwitchLocationScreen() { diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/LocationInfo.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/LocationInfo.kt index b25406c90e..4670cd5e12 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/LocationInfo.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/LocationInfo.kt @@ -1,8 +1,6 @@ package net.mullvad.mullvadvpn -import kotlinx.coroutines.async import kotlinx.coroutines.launch -import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job @@ -10,61 +8,31 @@ import kotlinx.coroutines.Job import android.view.View import android.widget.TextView -import net.mullvad.mullvadvpn.model.GeoIpLocation -import net.mullvad.mullvadvpn.model.TunnelStateTransition +import net.mullvad.mullvadvpn.dataproxy.LocationInfoCache -class LocationInfo(val parentView: View, val daemon: Deferred<MullvadDaemon>) { - private val country: TextView = parentView.findViewById(R.id.country) - private val city: TextView = parentView.findViewById(R.id.city) - private val hostname: TextView = parentView.findViewById(R.id.hostname) +class LocationInfo(val parentView: View, val locationInfoCache: LocationInfoCache) { + private val countryLabel: TextView = parentView.findViewById(R.id.country) + private val cityLabel: TextView = parentView.findViewById(R.id.city) + private val hostnameLabel: TextView = parentView.findViewById(R.id.hostname) - private var lastKnownRealLocation: GeoIpLocation? = null + private var updateJob: Job? = null - private var activeFetch: Job? = null - - var location: GeoIpLocation? = null - set(value) { - field = value - updateViews(value) - } - - fun setState(state: TunnelStateTransition) { - activeFetch?.cancel() - activeFetch = null - - when (state) { - is TunnelStateTransition.Disconnected -> activeFetch = fetchRealLocation() - is TunnelStateTransition.Connecting -> activeFetch = fetchRelayLocation() - is TunnelStateTransition.Connected -> activeFetch = fetchRelayLocation() - is TunnelStateTransition.Disconnecting -> location = lastKnownRealLocation - is TunnelStateTransition.Blocked -> location = null + init { + locationInfoCache.onNewLocation = { country, city, hostname -> + updateJob?.cancel() + updateJob = updateViews(country, city, hostname) } } - fun updateViews(location: GeoIpLocation?) { - country.text = location?.country ?: "" - city.text = location?.city ?: "" - hostname.text = location?.hostname ?: "" - } - - private fun fetchRealLocation() = GlobalScope.launch(Dispatchers.Main) { - var realLocation: GeoIpLocation? = null - var remainingAttempts = 10 - - while (realLocation == null && remainingAttempts > 0) { - realLocation = fetchLocation().await() - remainingAttempts -= 1 - } - - lastKnownRealLocation = realLocation - location = realLocation - } - - private fun fetchRelayLocation() = GlobalScope.launch(Dispatchers.Main) { - location = fetchLocation().await() + fun onDestroy() { + updateJob?.cancel() + locationInfoCache.onNewLocation = null } - private fun fetchLocation() = GlobalScope.async(Dispatchers.Default) { - daemon.await().getCurrentLocation() + fun updateViews(country: String, city: String, hostname: String) = + GlobalScope.launch(Dispatchers.Main) { + countryLabel.text = country + cityLabel.text = city + hostnameLabel.text = hostname } } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt index 4f39a771de..4b5414da71 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt @@ -17,6 +17,7 @@ import android.os.IBinder import android.support.v4.app.FragmentActivity import net.mullvad.mullvadvpn.dataproxy.AccountCache +import net.mullvad.mullvadvpn.dataproxy.LocationInfoCache import net.mullvad.mullvadvpn.dataproxy.RelayListListener import net.mullvad.mullvadvpn.model.RelaySettings import net.mullvad.mullvadvpn.model.Settings @@ -34,6 +35,7 @@ class MainActivity : FragmentActivity() { get() = runBlocking { asyncSettings.await() } val accountCache = AccountCache(this) + val locationInfoCache = LocationInfoCache(asyncDaemon) var relayListListener = RelayListListener(this) private var waitForDaemonJob: Job? = null 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 ba3f9cefd4..98e529fbb8 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt @@ -16,11 +16,15 @@ class LocationInfoCache(val daemon: Deferred<MullvadDaemon>) { private var activeFetch: Job? = null var onNewLocation: ((String, String, String) -> Unit)? = null + set(value) { + field = value + notifyNewLocation() + } var location: GeoIpLocation? = null set(value) { field = value - notifyNewLocation(value) + notifyNewLocation() } fun setState(state: TunnelStateTransition) { @@ -36,7 +40,8 @@ class LocationInfoCache(val daemon: Deferred<MullvadDaemon>) { } } - fun notifyNewLocation(location: GeoIpLocation?) { + fun notifyNewLocation() { + val location = this.location val country = location?.country ?: "" val city = location?.city ?: "" val hostname = location?.hostname ?: "" |
