summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/AppVersionInfoCache.kt93
1 files changed, 45 insertions, 48 deletions
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() {