diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-06-13 16:33:25 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-06-13 16:33:25 -0300 |
| commit | 6e658fb0fef2912840896f35c8d6bde2842a69cf (patch) | |
| tree | 73337ca1d46bac415473eb13014db5cb7f674edb /android/src | |
| parent | f1a976619e6aeca61629e49066bfc2ef70bffbd8 (diff) | |
| parent | 4f5e344a2ee88a40d59c3c65be6c74fbb4efd952 (diff) | |
| download | mullvadvpn-6e658fb0fef2912840896f35c8d6bde2842a69cf.tar.xz mullvadvpn-6e658fb0fef2912840896f35c8d6bde2842a69cf.zip | |
Merge branch 'show-location'
Diffstat (limited to 'android/src')
6 files changed, 87 insertions, 7 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt index ae3194c5aa..c4b5fdb5cd 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt @@ -1,5 +1,6 @@ package net.mullvad.mullvadvpn +import kotlinx.coroutines.async import kotlinx.coroutines.launch import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.Deferred @@ -19,6 +20,7 @@ import android.view.View import android.view.ViewGroup import android.widget.Button +import net.mullvad.mullvadvpn.model.GeoIpLocation import net.mullvad.mullvadvpn.model.TunnelStateTransition class ConnectFragment : Fragment() { @@ -26,6 +28,7 @@ class ConnectFragment : Fragment() { private lateinit var headerBar: HeaderBar private lateinit var notificationBanner: NotificationBanner private lateinit var status: ConnectionStatus + private lateinit var locationInfo: LocationInfo private lateinit var parentActivity: MainActivity @@ -35,6 +38,8 @@ class ConnectFragment : Fragment() { private var fetchInitialStateJob = fetchInitialState() private var generateWireguardKeyJob = generateWireguardKey() + private var lastKnownRealLocation: GeoIpLocation? = null + private var activeAction: Job? = null private var attachListenerJob: Job? = null private var updateViewJob: Job? = null @@ -61,6 +66,7 @@ class ConnectFragment : Fragment() { headerBar = HeaderBar(view, context!!) notificationBanner = NotificationBanner(view) status = ConnectionStatus(view, context!!) + locationInfo = LocationInfo(view, daemon) actionButton = ConnectActionButton(view) actionButton.apply { @@ -149,6 +155,7 @@ class ConnectFragment : Fragment() { } private fun disconnect() { + updateView(TunnelStateTransition.Disconnecting()) activeAction?.cancel() activeAction = GlobalScope.launch(Dispatchers.Default) { @@ -172,6 +179,7 @@ class ConnectFragment : Fragment() { headerBar.setState(state) notificationBanner.setState(state) status.setState(state) + locationInfo.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 new file mode 100644 index 0000000000..b25406c90e --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/LocationInfo.kt @@ -0,0 +1,70 @@ +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 + +import android.view.View +import android.widget.TextView + +import net.mullvad.mullvadvpn.model.GeoIpLocation +import net.mullvad.mullvadvpn.model.TunnelStateTransition + +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) + + private var lastKnownRealLocation: GeoIpLocation? = 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 + } + } + + 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() + } + + private fun fetchLocation() = GlobalScope.async(Dispatchers.Default) { + daemon.await().getCurrentLocation() + } +} diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadDaemon.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadDaemon.kt index 5dd6401615..d16ba06a1a 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadDaemon.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadDaemon.kt @@ -1,6 +1,7 @@ package net.mullvad.mullvadvpn import net.mullvad.mullvadvpn.model.AccountData +import net.mullvad.mullvadvpn.model.GeoIpLocation import net.mullvad.mullvadvpn.model.PublicKey import net.mullvad.mullvadvpn.model.RelayList import net.mullvad.mullvadvpn.model.RelaySettingsUpdate @@ -19,6 +20,7 @@ class MullvadDaemon(val vpnService: MullvadVpnService) { external fun disconnect() external fun generateWireguardKey(): Boolean external fun getAccountData(accountToken: String): AccountData? + external fun getCurrentLocation(): GeoIpLocation? external fun getRelayLocations(): RelayList external fun getSettings(): Settings external fun getState(): TunnelStateTransition diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/model/GeoIpLocation.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/model/GeoIpLocation.kt new file mode 100644 index 0000000000..ed7ccb7e66 --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/model/GeoIpLocation.kt @@ -0,0 +1,3 @@ +package net.mullvad.mullvadvpn.model + +data class GeoIpLocation(val country: String, val city: String?, val hostname: String?) diff --git a/android/src/main/res/layout/connect.xml b/android/src/main/res/layout/connect.xml index d35679aba4..19440b794f 100644 --- a/android/src/main/res/layout/connect.xml +++ b/android/src/main/res/layout/connect.xml @@ -96,31 +96,29 @@ android:text="@string/unsecured_connection" android:textAllCaps="true" /> - <TextView + <TextView android:id="@+id/country" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:textSize="34sp" android:textStyle="bold" - android:text="@string/country" + android:text="" /> - <TextView + <TextView android:id="@+id/city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:textSize="34sp" android:textStyle="bold" android:text="" - android:visibility="invisible" /> - <TextView + <TextView android:id="@+id/hostname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:textSize="16sp" android:textStyle="bold" android:text="" - android:visibility="invisible" /> </LinearLayout> diff --git a/android/src/main/res/values/strings.xml b/android/src/main/res/values/strings.xml index 632feefb6f..fc8ec8d0c7 100644 --- a/android/src/main/res/values/strings.xml +++ b/android/src/main/res/values/strings.xml @@ -16,7 +16,6 @@ <string name="creating_secure_connection">Creating secure connection</string> <string name="secure_connection">Secure connection</string> <string name="blocking_internet">Blocking internet</string> - <string name="country">Country</string> <string name="connect">Secure my connection</string> <string name="cancel">Cancel</string> <string name="disconnect">Disconnect</string> |
