summaryrefslogtreecommitdiffhomepage
path: root/android/src/main
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-07-29 16:04:30 -0300
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-07-29 16:04:30 -0300
commitbf49b5efb0a3a92c06bcc70c22314672a12f9345 (patch)
tree5b74445dea5bd8e4b9c81bc2e2dff149fde40767 /android/src/main
parentf5192748bbe64e72bdf3324efdc303a30e718815 (diff)
parent0d93d93bcbaebbbf2d18efda97b1e364a44dd34c (diff)
downloadmullvadvpn-bf49b5efb0a3a92c06bcc70c22314672a12f9345.tar.xz
mullvadvpn-bf49b5efb0a3a92c06bcc70c22314672a12f9345.zip
Merge branch 'improve-reconnection-speed'
Diffstat (limited to 'android/src/main')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectivityListener.kt59
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt2
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt7
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt11
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt26
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/Relay.kt11
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayCity.kt4
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayList.kt26
8 files changed, 56 insertions, 90 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectivityListener.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectivityListener.kt
deleted file mode 100644
index cf212762aa..0000000000
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectivityListener.kt
+++ /dev/null
@@ -1,59 +0,0 @@
-package net.mullvad.mullvadvpn
-
-import kotlinx.coroutines.CompletableDeferred
-
-import android.content.BroadcastReceiver
-import android.content.Context
-import android.content.Intent
-import android.content.IntentFilter
-import android.net.ConnectivityManager
-import android.net.NetworkInfo
-import android.net.NetworkInfo.DetailedState
-
-class ConnectivityListener : BroadcastReceiver() {
- var vpnDisconnected = CompletableDeferred<Unit>()
- private set
-
- fun register(context: Context) {
- val intentFilter = IntentFilter()
-
- intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
- context.registerReceiver(this, intentFilter)
-
- checkInitialState(context)
- }
-
- fun unregister(context: Context) {
- context.unregisterReceiver(this)
- }
-
- override fun onReceive(context: Context, intent: Intent) {
- val networkInfo =
- intent.getParcelableExtra<NetworkInfo>(ConnectivityManager.EXTRA_NETWORK_INFO)
-
- if (networkInfo.type == ConnectivityManager.TYPE_VPN) {
- if (networkInfo.detailedState == DetailedState.DISCONNECTED) {
- vpnDisconnected.complete(Unit)
- } else if (networkInfo.detailedState == DetailedState.CONNECTED) {
- vpnDisconnected.cancel()
- vpnDisconnected = CompletableDeferred<Unit>()
- }
- }
- }
-
- private fun checkInitialState(context: Context) {
- val connectivityManager =
- context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
-
- val isVpnConnected = connectivityManager.allNetworks
- .map({ network -> connectivityManager.getNetworkInfo(network) })
- .any({ networkInfo ->
- networkInfo.type == ConnectivityManager.TYPE_VPN
- && networkInfo.detailedState == DetailedState.CONNECTED
- })
-
- if (!isVpnConnected) {
- vpnDisconnected.complete(Unit)
- }
- }
-}
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt
index d16e36a1b6..f5d4d6bd60 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/MainActivity.kt
@@ -38,10 +38,10 @@ class MainActivity : FragmentActivity() {
val connectionProxy = ConnectionProxy(this)
val keyStatusListener = KeyStatusListener(daemon)
- val locationInfoCache = LocationInfoCache(daemon)
val problemReport = MullvadProblemReport()
var settingsListener = SettingsListener(this)
var relayListListener = RelayListListener(this)
+ val locationInfoCache = LocationInfoCache(daemon, relayListListener)
val accountCache = AccountCache(settingsListener, daemon)
private var waitForDaemonJob: Job? = null
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt
index d8c340bcd2..bc0c317cf2 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt
@@ -18,7 +18,6 @@ import android.os.IBinder
import net.mullvad.mullvadvpn.model.TunConfig
class MullvadVpnService : VpnService() {
- private val connectivityListener = ConnectivityListener()
private val created = CompletableDeferred<Unit>()
private val binder = LocalBinder()
@@ -26,7 +25,6 @@ class MullvadVpnService : VpnService() {
override fun onCreate() {
created.complete(Unit)
- connectivityListener.register(this)
}
override fun onBind(intent: Intent): IBinder {
@@ -34,16 +32,11 @@ class MullvadVpnService : VpnService() {
}
override fun onDestroy() {
- connectivityListener.unregister(this)
daemon.cancel()
created.cancel()
}
fun createTun(config: TunConfig): Int {
- runBlocking {
- connectivityListener.vpnDisconnected.await()
- }
-
val builder = Builder().apply {
for (address in config.addresses) {
addAddress(address, 32)
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt
index d35374b29e..4d3dae8159 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt
@@ -9,6 +9,7 @@ import android.graphics.drawable.Drawable
import android.view.View
import android.widget.Button
+import net.mullvad.mullvadvpn.model.ActionAfterDisconnect
import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.relaylist.RelayItem
@@ -43,9 +44,17 @@ class SwitchLocationButton(val parentView: View) {
private fun update() {
updateJob?.cancel()
updateJob = GlobalScope.launch(Dispatchers.Main) {
+ val state = this@SwitchLocationButton.state
+
when (state) {
is TunnelState.Disconnected -> showLocation()
- is TunnelState.Disconnecting -> showLocation()
+ is TunnelState.Disconnecting -> {
+ when (state.actionAfterDisconnect) {
+ is ActionAfterDisconnect.Nothing -> showLocation()
+ is ActionAfterDisconnect.Block -> showLocation()
+ is ActionAfterDisconnect.Reconnect -> showLabel()
+ }
+ }
is TunnelState.Connecting -> showLabel()
is TunnelState.Connected -> showLabel()
is TunnelState.Blocked -> showLocation()
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 74745118d3..3884490bcc 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt
@@ -11,8 +11,14 @@ import net.mullvad.mullvadvpn.model.ActionAfterDisconnect
import net.mullvad.mullvadvpn.model.GeoIpLocation
import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.MullvadDaemon
+import net.mullvad.mullvadvpn.relaylist.RelayCity
+import net.mullvad.mullvadvpn.relaylist.RelayCountry
+import net.mullvad.mullvadvpn.relaylist.Relay
-class LocationInfoCache(val daemon: Deferred<MullvadDaemon>) {
+class LocationInfoCache(
+ val daemon: Deferred<MullvadDaemon>,
+ val relayListListener: RelayListListener
+) {
private var lastKnownRealLocation: GeoIpLocation? = null
private var activeFetch: Job? = null
@@ -43,7 +49,7 @@ class LocationInfoCache(val daemon: Deferred<MullvadDaemon>) {
when (value.actionAfterDisconnect) {
is ActionAfterDisconnect.Nothing -> location = lastKnownRealLocation
is ActionAfterDisconnect.Block -> location = null
- is ActionAfterDisconnect.Reconnect -> {} // Leave location unchanged
+ is ActionAfterDisconnect.Reconnect -> location = locationFromSelectedRelay()
}
}
is TunnelState.Blocked -> location = null
@@ -59,6 +65,22 @@ class LocationInfoCache(val daemon: Deferred<MullvadDaemon>) {
onNewLocation?.invoke(country, city, hostname)
}
+ private fun locationFromSelectedRelay(): GeoIpLocation? {
+ val relayItem = relayListListener.selectedRelayItem
+
+ when (relayItem) {
+ is RelayCountry -> return GeoIpLocation(relayItem.name, null, null)
+ is RelayCity -> return GeoIpLocation(relayItem.country.name, relayItem.name, null)
+ is Relay -> return GeoIpLocation(
+ relayItem.city.country.name,
+ relayItem.city.name,
+ relayItem.name
+ )
+ }
+
+ return null
+ }
+
private fun fetchLocation() {
val previousFetch = activeFetch
val initialState = state
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/Relay.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/Relay.kt
index 863dc69d8e..8620a64963 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/Relay.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/Relay.kt
@@ -2,20 +2,15 @@ package net.mullvad.mullvadvpn.relaylist
import net.mullvad.mullvadvpn.model.LocationConstraint
-data class Relay(
- val countryCode: String,
- val cityCode: String,
- override val name: String,
- val cityName: String
-) : RelayItem {
+data class Relay(val city: RelayCity, override val name: String) : RelayItem {
override val code = name
override val type = RelayItemType.Relay
- override val location = LocationConstraint.Hostname(countryCode, cityCode, name)
+ override val location = LocationConstraint.Hostname(city.country.code, city.code, name)
override val hasChildren = false
override val visibleChildCount = 0
- override val locationName = "$cityName ($name)"
+ override val locationName = "${city.name} ($name)"
override var expanded
get() = false
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayCity.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayCity.kt
index 15c52fce43..9d4ba65ccf 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayCity.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayCity.kt
@@ -3,14 +3,14 @@ package net.mullvad.mullvadvpn.relaylist
import net.mullvad.mullvadvpn.model.LocationConstraint
class RelayCity(
+ val country: RelayCountry,
override val name: String,
- val countryCode: String,
override val code: String,
override var expanded: Boolean,
val relays: List<Relay>
) : RelayItem {
override val type = RelayItemType.City
- override val location = LocationConstraint.City(countryCode, code)
+ override val location = LocationConstraint.City(country.code, code)
override val hasChildren
get() = relays.size > 1
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 7c3870c4dd..26cd7ffa58 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayList.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayList.kt
@@ -9,19 +9,25 @@ class RelayList {
constructor(model: net.mullvad.mullvadvpn.model.RelayList) {
countries = model.countries
.map { country ->
- val cities = country.cities
- .map { city ->
- val relays = city.relays
- .filter { relay -> relay.hasWireguardTunnels }
- .map { relay ->
- Relay(country.code, city.code, relay.hostname, city.name)
- }
+ val cities = mutableListOf<RelayCity>()
+ val relayCountry = RelayCountry(country.name, country.code, false, cities)
- RelayCity(city.name, country.code, city.code, false, relays)
+ for (city in country.cities) {
+ val relays = mutableListOf<Relay>()
+ val relayCity = RelayCity(relayCountry, city.name, city.code, false, relays)
+
+ val validCityRelays = city.relays.filter { relay -> relay.hasWireguardTunnels }
+
+ for (relay in validCityRelays) {
+ relays.add(Relay(relayCity, relay.hostname))
}
- .filter { city -> city.relays.isNotEmpty() }
- RelayCountry(country.name, country.code, false, cities)
+ if (relays.isNotEmpty()) {
+ cities.add(relayCity)
+ }
+ }
+
+ relayCountry
}
.filter { country -> country.cities.isNotEmpty() }
}