summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md2
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt27
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt1
3 files changed, 30 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ab55f92589..e03c736a19 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,6 +35,8 @@ Line wrap the file at 100 chars. Th
- Fix notification message to update to `null` version when version check cache is stale right
after an update.
- Fix `null` pointer exception when connectivity event intent has no network info.
+- Fix fast loop trying to fetch location and preventing the device from sleeping. This should
+ improve battery life in some cases.
### Security
#### Linux
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 2183922536..a511156bf3 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/LocationInfoCache.kt
@@ -5,6 +5,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
+import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.model.GeoIpLocation
import net.mullvad.mullvadvpn.model.TunnelState
@@ -15,6 +16,10 @@ import net.mullvad.mullvadvpn.service.MullvadDaemon
import net.mullvad.talpid.ConnectivityListener
import net.mullvad.talpid.tunnel.ActionAfterDisconnect
+const val DELAY_SCALE: Long = 50
+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>,
@@ -68,6 +73,10 @@ class LocationInfoCache(
}
}
+ fun onDestroy() {
+ activeFetch?.cancel()
+ }
+
private fun locationFromSelectedRelay(): GeoIpLocation? {
val relayItem = relayListListener.selectedRelayItem
@@ -98,10 +107,14 @@ class LocationInfoCache(
activeFetch = GlobalScope.launch(Dispatchers.Main) {
var newLocation: GeoIpLocation? = null
+ var retry = 0
previousFetch?.join()
while (newLocation == null && shouldRetryFetch() && state == initialState) {
+ delayFetch(retry)
+ retry += 1
+
newLocation = executeFetch().await()
}
@@ -122,6 +135,20 @@ class LocationInfoCache(
daemon.await().getCurrentLocation()
}
+ private suspend fun delayFetch(retryAttempt: Int) {
+ var duration = 0L
+
+ // The first attempt has no delay
+ if (retryAttempt >= MAX_RETRIES) {
+ duration = MAX_DELAY
+ } else if (retryAttempt >= 1) {
+ val exponent = retryAttempt - 1
+ duration = (1L shl exponent) * DELAY_SCALE
+ }
+
+ delay(duration)
+ }
+
private suspend fun shouldRetryFetch(): Boolean {
val state = this.state
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt
index 89b33b6869..7660e240bb 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt
@@ -121,6 +121,7 @@ class MainActivity : FragmentActivity() {
accountCache.onDestroy()
appVersionInfoCache.onDestroy()
keyStatusListener.onDestroy()
+ locationInfoCache.onDestroy()
relayListListener.onDestroy()
settingsListener.onDestroy()