diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-03-25 20:51:57 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-05-23 12:43:30 +0000 |
| commit | 78bbd50518b064092b9dff46685375e2cba5c52e (patch) | |
| tree | c51c0981d465b5e25f37396fc7676d802f7b94b0 | |
| parent | b00d41846fbdea6067b2101478352f16dd20ffc5 (diff) | |
| download | mullvadvpn-78bbd50518b064092b9dff46685375e2cba5c52e.tar.xz mullvadvpn-78bbd50518b064092b9dff46685375e2cba5c52e.zip | |
Restore selected relay item based on settings
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt | 18 | ||||
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayList.kt | 34 |
2 files changed, 52 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt index 0fb8c5f409..5b54c0d355 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt @@ -12,6 +12,7 @@ import kotlinx.coroutines.Job import android.os.Bundle import android.support.v4.app.FragmentActivity +import net.mullvad.mullvadvpn.model.RelaySettings import net.mullvad.mullvadvpn.model.Settings import net.mullvad.mullvadvpn.relaylist.RelayItem import net.mullvad.mullvadvpn.relaylist.RelayList @@ -35,6 +36,8 @@ class MainActivity : FragmentActivity() { var selectedRelayItem: RelayItem? = null + private val restoreSelectedRelayListItemJob = restoreSelectedRelayListItem() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main) @@ -47,6 +50,7 @@ class MainActivity : FragmentActivity() { } override fun onDestroy() { + restoreSelectedRelayListItemJob.cancel() asyncSettings.cancel() asyncRelayList.cancel() asyncDaemon.cancel() @@ -74,4 +78,18 @@ class MainActivity : FragmentActivity() { private fun fetchSettings() = GlobalScope.async(Dispatchers.Default) { asyncDaemon.await().getSettings() } + + private fun restoreSelectedRelayListItem() = GlobalScope.launch(Dispatchers.Default) { + val relaySettings = asyncSettings.await().relaySettings + + when (relaySettings) { + is RelaySettings.CustomTunnelEndpoint -> selectedRelayItem = null + is RelaySettings.RelayConstraints -> { + val location = relaySettings.location + val relayList = asyncRelayList.await() + + selectedRelayItem = relayList.findItemForLocation(location) + } + } + } } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayList.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayList.kt index 964833f675..e6cc4bd54e 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayList.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayList.kt @@ -1,5 +1,8 @@ package net.mullvad.mullvadvpn.relaylist +import net.mullvad.mullvadvpn.model.Constraint +import net.mullvad.mullvadvpn.model.LocationConstraint + class RelayList { val countries: List<RelayCountry> @@ -16,4 +19,35 @@ class RelayList { RelayCountry(country.name, country.code, false, cities) } } + + fun findItemForLocation(constraint: Constraint<LocationConstraint>): RelayItem? { + when (constraint) { + is Constraint.Any -> return null + is Constraint.Only -> { + val location = constraint.value + + when (location) { + is LocationConstraint.Country -> { + return countries.find { country -> country.code == location.countryCode } + } + is LocationConstraint.City -> { + val country = countries.find { country -> + country.code == location.countryCode + } + + return country?.cities?.find { city -> city.code == location.cityCode } + } + is LocationConstraint.Hostname -> { + val country = countries.find { country -> + country.code == location.countryCode + } + + val city = country?.cities?.find { city -> city.code == location.cityCode } + + return city?.relays?.find { relay -> relay.name == location.hostname } + } + } + } + } + } } |
