summaryrefslogtreecommitdiffhomepage
path: root/android/src
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2019-10-07 16:25:58 +0200
committerLinus Färnstrand <linus@mullvad.net>2019-10-07 16:25:58 +0200
commit48d9e233e6bbe9c56372ee59324b786f770363d0 (patch)
tree00ed94738e56b11e77023e5d95ddc54a1a5b7116 /android/src
parent2db11de43396abfe1f6a295d9fd22511898f7a07 (diff)
parent4c8ff3f8ec7baf674b95536a94e18e79440b428f (diff)
downloadmullvadvpn-48d9e233e6bbe9c56372ee59324b786f770363d0.tar.xz
mullvadvpn-48d9e233e6bbe9c56372ee59324b786f770363d0.zip
Merge branch 'cache-version-check-in-daemon'
Diffstat (limited to 'android/src')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadDaemon.kt5
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt4
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoCache.kt93
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoFetcher.kt69
4 files changed, 50 insertions, 121 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadDaemon.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadDaemon.kt
index fe1ba2bc71..02596f6996 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadDaemon.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadDaemon.kt
@@ -14,6 +14,7 @@ import net.mullvad.mullvadvpn.util.EventNotifier
class MullvadDaemon(val vpnService: MullvadVpnService) {
val onSettingsChange = EventNotifier<Settings?>(null)
+ var onAppVersionInfoChange: ((AppVersionInfo) -> Unit)? = null
var onKeygenEvent: ((KeygenEvent) -> Unit)? = null
var onRelayListChange: ((RelayList) -> Unit)? = null
var onTunnelStateChange: ((TunnelState) -> Unit)? = null
@@ -43,6 +44,10 @@ class MullvadDaemon(val vpnService: MullvadVpnService) {
private external fun initialize(vpnService: MullvadVpnService)
+ private fun notifyAppVersionInfoEvent(appVersionInfo: AppVersionInfo) {
+ onAppVersionInfoChange?.invoke(appVersionInfo)
+ }
+
private fun notifyKeygenEvent(event: KeygenEvent) {
onKeygenEvent?.invoke(event)
}
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt
index e36c184bfe..388e4c0a5d 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt
@@ -10,7 +10,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
-import net.mullvad.mullvadvpn.dataproxy.AppVersionInfoFetcher
import net.mullvad.mullvadvpn.dataproxy.ConnectionProxy
import net.mullvad.mullvadvpn.model.TunConfig
@@ -23,7 +22,6 @@ class MullvadVpnService : VpnService() {
private lateinit var daemon: Deferred<MullvadDaemon>
private lateinit var connectionProxy: ConnectionProxy
private lateinit var notificationManager: ForegroundNotificationManager
- private lateinit var versionInfoFetcher: AppVersionInfoFetcher
override fun onCreate() {
setUp()
@@ -95,7 +93,6 @@ class MullvadVpnService : VpnService() {
daemon = startDaemon()
connectionProxy = ConnectionProxy(this, daemon)
notificationManager = startNotificationManager()
- versionInfoFetcher = AppVersionInfoFetcher(daemon, this)
}
private fun startDaemon() = GlobalScope.async(Dispatchers.Default) {
@@ -130,6 +127,5 @@ class MullvadVpnService : VpnService() {
private fun tearDown() {
connectionProxy.onDestroy()
notificationManager.onDestroy()
- versionInfoFetcher.stop()
}
}
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoCache.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoCache.kt
index 9bc6011fbc..cf62ffed0b 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoCache.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoCache.kt
@@ -1,27 +1,27 @@
package net.mullvad.mullvadvpn.dataproxy
import android.content.Context
-import android.content.SharedPreferences
-import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.MainActivity
+import net.mullvad.mullvadvpn.model.AppVersionInfo
class AppVersionInfoCache(val parentActivity: MainActivity) {
companion object {
- val KEY_CURRENT_IS_SUPPORTED = "current_is_supported"
- val KEY_CURRENT_IS_OUTDATED = "current_is_outdated"
- val KEY_LAST_UPDATED = "last_updated"
- val KEY_LATEST_STABLE = "latest_stable"
- val KEY_LATEST = "latest"
- val SHARED_PREFERENCES = "app_version_info_cache"
+ val LEGACY_SHARED_PREFERENCES = "app_version_info_cache"
}
- private val preferences: SharedPreferences
- get() = parentActivity.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE)
+ private val daemon = parentActivity.daemon
+ private val setUpJob = setUp()
- private val fetchCurrentVersionJob = fetchCurrentVersion()
+ private var appVersionInfo: AppVersionInfo? = null
+ set(value) {
+ synchronized(this) {
+ field = value
+ updateUpgradeVersion()
+ }
+ }
var onUpdate: (() -> Unit)? = null
set(value) {
@@ -29,62 +29,59 @@ class AppVersionInfoCache(val parentActivity: MainActivity) {
value?.invoke()
}
+ val latestStable
+ get() = appVersionInfo?.latestStable
+ val latest
+ get() = appVersionInfo?.latest
+ val isSupported
+ get() = appVersionInfo?.currentIsSupported ?: true
+ var isOutdated = false
+ get() = appVersionInfo?.currentIsOutdated ?: false
+
var version: String? = null
private set
var isStable = true
private set
- var lastUpdated = 0L
- private set
- var isSupported = true
- private set
- var isOutdated = false
- private set
- var latestStable: String? = null
- private set
- var latest: String? = null
- private set
-
var upgradeVersion: String? = null
private set
- private val listener = object : OnSharedPreferenceChangeListener {
- override fun onSharedPreferenceChanged(preferences: SharedPreferences, key: String) {
- when (key) {
- KEY_CURRENT_IS_SUPPORTED -> isSupported = preferences.getBoolean(key, isSupported)
- KEY_CURRENT_IS_OUTDATED -> isOutdated = preferences.getBoolean(key, isOutdated)
- KEY_LAST_UPDATED -> lastUpdated = preferences.getLong(key, lastUpdated)
- KEY_LATEST_STABLE -> latestStable = preferences.getString(key, latestStable)
- KEY_LATEST -> latest = preferences.getString(key, latest)
- else -> return
- }
-
- updateUpgradeVersion()
- }
- }
-
fun onCreate() {
- preferences.registerOnSharedPreferenceChangeListener(listener)
-
- lastUpdated = preferences.getLong(KEY_LAST_UPDATED, 0L)
- isSupported = preferences.getBoolean(KEY_CURRENT_IS_SUPPORTED, true)
- isOutdated = preferences.getBoolean(KEY_CURRENT_IS_OUTDATED, false)
- latestStable = preferences.getString(KEY_LATEST_STABLE, null)
- latest = preferences.getString(KEY_LATEST, null)
+ parentActivity.getSharedPreferences(LEGACY_SHARED_PREFERENCES, Context.MODE_PRIVATE)
+ .edit()
+ .clear()
+ .commit()
}
fun onDestroy() {
- fetchCurrentVersionJob.cancel()
- preferences.unregisterOnSharedPreferenceChangeListener(listener)
+ setUpJob.cancel()
+ tearDown()
}
- private fun fetchCurrentVersion() = GlobalScope.launch(Dispatchers.Default) {
- val currentVersion = parentActivity.daemon.await().getCurrentVersion()
+ private fun setUp() = GlobalScope.launch(Dispatchers.Default) {
+ val daemon = this@AppVersionInfoCache.daemon.await()
+ val currentVersion = daemon.getCurrentVersion()
version = currentVersion
isStable = !currentVersion.contains("-")
updateUpgradeVersion()
+
+ daemon.onAppVersionInfoChange = { newAppVersionInfo ->
+ appVersionInfo = newAppVersionInfo
+ }
+
+ synchronized(this@AppVersionInfoCache) {
+ val initialVersionInfo = daemon.getVersionInfo()
+
+ if (appVersionInfo == null) {
+ appVersionInfo = initialVersionInfo
+ }
+ }
+ }
+
+ private fun tearDown() = GlobalScope.launch(Dispatchers.Default) {
+ daemon.await().onAppVersionInfoChange = null
}
private fun updateUpgradeVersion() {
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoFetcher.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoFetcher.kt
deleted file mode 100644
index f067660ee5..0000000000
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoFetcher.kt
+++ /dev/null
@@ -1,69 +0,0 @@
-package net.mullvad.mullvadvpn.dataproxy
-
-import android.content.Context
-import java.util.Calendar
-import kotlinx.coroutines.Deferred
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.GlobalScope
-import kotlinx.coroutines.delay
-import kotlinx.coroutines.launch
-import net.mullvad.mullvadvpn.MullvadDaemon
-
-val ONE_DAY_IN_MILLISECONDS = 24L * 60L * 60L * 1000L
-val ONE_MINUTE_IN_MILLISECONDS = 60L * 1000L
-
-class AppVersionInfoFetcher(val daemon: Deferred<MullvadDaemon>, val context: Context) {
- private val preferences =
- context.getSharedPreferences(AppVersionInfoCache.SHARED_PREFERENCES, Context.MODE_PRIVATE)
-
- private val mainLoop = run()
-
- fun stop() {
- mainLoop.cancel()
- }
-
- private fun run() = GlobalScope.launch(Dispatchers.Default) {
- while (true) {
- delay(calculateDelay())
- fetch()
- }
- }
-
- private fun calculateDelay(): Long {
- val now = Calendar.getInstance().timeInMillis
- val lastUpdated = preferences.getLong(AppVersionInfoCache.KEY_LAST_UPDATED, 0)
- val delta = now - lastUpdated
-
- if (delta < 0 || delta >= ONE_DAY_IN_MILLISECONDS) {
- return 0
- } else {
- return ONE_DAY_IN_MILLISECONDS - delta
- }
- }
-
- private suspend fun fetch() {
- var now = Calendar.getInstance().timeInMillis
- var versionInfo = daemon.await().getVersionInfo()
- var attempt = 0
-
- while (attempt < 5 && versionInfo == null) {
- delay(ONE_MINUTE_IN_MILLISECONDS)
- now = Calendar.getInstance().timeInMillis
- versionInfo = daemon.await().getVersionInfo()
- attempt += 1
- }
-
- if (versionInfo != null) {
- preferences.edit().apply {
- with(AppVersionInfoCache) {
- putLong(KEY_LAST_UPDATED, now)
- putBoolean(KEY_CURRENT_IS_SUPPORTED, versionInfo.currentIsSupported)
- putBoolean(KEY_CURRENT_IS_OUTDATED, versionInfo.currentIsOutdated)
- putString(KEY_LATEST_STABLE, versionInfo.latestStable)
- putString(KEY_LATEST, versionInfo.latest)
- commit()
- }
- }
- }
- }
-}