summaryrefslogtreecommitdiffhomepage
path: root/ui/src
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2021-05-06 16:55:29 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2021-05-06 17:02:28 +0200
commit94ecb13d2fd9f36a10b0209b2ddf161b4cc0a07f (patch)
treeddf5437289149c6151a572dca889d8e4ebc9f61a /ui/src
parent6008efcd95ec10870c325adce3fff0a85ecda238 (diff)
ui: update datastore and rework api
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src')
-rw-r--r--ui/src/main/java/com/wireguard/android/Application.kt9
-rw-r--r--ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt39
-rw-r--r--ui/src/main/java/com/wireguard/android/util/UserKnobs.kt22
3 files changed, 37 insertions, 33 deletions
diff --git a/ui/src/main/java/com/wireguard/android/Application.kt b/ui/src/main/java/com/wireguard/android/Application.kt
index fe98d0d2..13d372c6 100644
--- a/ui/src/main/java/com/wireguard/android/Application.kt
+++ b/ui/src/main/java/com/wireguard/android/Application.kt
@@ -12,9 +12,10 @@ import android.os.StrictMode.ThreadPolicy
import android.os.StrictMode.VmPolicy
import android.util.Log
import androidx.appcompat.app.AppCompatDelegate
-import androidx.datastore.DataStore
-import androidx.datastore.preferences.Preferences
-import androidx.datastore.preferences.createDataStore
+import androidx.datastore.core.DataStore
+import androidx.datastore.preferences.core.PreferenceDataStoreFactory
+import androidx.datastore.preferences.core.Preferences
+import androidx.datastore.preferences.preferencesDataStoreFile
import com.wireguard.android.backend.Backend
import com.wireguard.android.backend.GoBackend
import com.wireguard.android.backend.WgQuickBackend
@@ -100,7 +101,7 @@ class Application : android.app.Application() {
rootShell = RootShell(applicationContext)
toolsInstaller = ToolsInstaller(applicationContext, rootShell)
moduleLoader = ModuleLoader(applicationContext, rootShell, USER_AGENT)
- preferencesDataStore = applicationContext.createDataStore(name = "settings")
+ preferencesDataStore = PreferenceDataStoreFactory.create { applicationContext.preferencesDataStoreFile("settings") }
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
coroutineScope.launch {
AppCompatDelegate.setDefaultNightMode(
diff --git a/ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt b/ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt
index 96ca2b1c..dc9375bc 100644
--- a/ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt
+++ b/ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt
@@ -5,12 +5,15 @@
package com.wireguard.android.preference
-import androidx.datastore.DataStore
-import androidx.datastore.preferences.Preferences
-import androidx.datastore.preferences.edit
-import androidx.datastore.preferences.preferencesKey
-import androidx.datastore.preferences.preferencesSetKey
-import androidx.datastore.preferences.remove
+import androidx.datastore.core.DataStore
+import androidx.datastore.preferences.core.Preferences
+import androidx.datastore.preferences.core.booleanPreferencesKey
+import androidx.datastore.preferences.core.edit
+import androidx.datastore.preferences.core.floatPreferencesKey
+import androidx.datastore.preferences.core.intPreferencesKey
+import androidx.datastore.preferences.core.longPreferencesKey
+import androidx.datastore.preferences.core.stringPreferencesKey
+import androidx.datastore.preferences.core.stringSetPreferencesKey
import androidx.preference.PreferenceDataStore
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.first
@@ -21,7 +24,7 @@ import kotlinx.coroutines.runBlocking
class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope, private val dataStore: DataStore<Preferences>) : PreferenceDataStore() {
override fun putString(key: String?, value: String?) {
if (key == null) return
- val pk = preferencesKey<String>(key)
+ val pk = stringPreferencesKey(key)
coroutineScope.launch {
dataStore.edit {
if (value == null) it.remove(pk)
@@ -32,7 +35,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope,
override fun putStringSet(key: String?, values: Set<String?>?) {
if (key == null) return
- val pk = preferencesSetKey<String>(key)
+ val pk = stringSetPreferencesKey(key)
val filteredValues = values?.filterNotNull()?.toSet()
coroutineScope.launch {
dataStore.edit {
@@ -44,7 +47,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope,
override fun putInt(key: String?, value: Int) {
if (key == null) return
- val pk = preferencesKey<Int>(key)
+ val pk = intPreferencesKey(key)
coroutineScope.launch {
dataStore.edit {
it[pk] = value
@@ -54,7 +57,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope,
override fun putLong(key: String?, value: Long) {
if (key == null) return
- val pk = preferencesKey<Long>(key)
+ val pk = longPreferencesKey(key)
coroutineScope.launch {
dataStore.edit {
it[pk] = value
@@ -64,7 +67,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope,
override fun putFloat(key: String?, value: Float) {
if (key == null) return
- val pk = preferencesKey<Float>(key)
+ val pk = floatPreferencesKey(key)
coroutineScope.launch {
dataStore.edit {
it[pk] = value
@@ -74,7 +77,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope,
override fun putBoolean(key: String?, value: Boolean) {
if (key == null) return
- val pk = preferencesKey<Boolean>(key)
+ val pk = booleanPreferencesKey(key)
coroutineScope.launch {
dataStore.edit {
it[pk] = value
@@ -84,7 +87,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope,
override fun getString(key: String?, defValue: String?): String? {
if (key == null) return defValue
- val pk = preferencesKey<String>(key)
+ val pk = stringPreferencesKey(key)
return runBlocking {
dataStore.data.map { it[pk] ?: defValue }.first()
}
@@ -92,7 +95,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope,
override fun getStringSet(key: String?, defValues: Set<String?>?): Set<String?>? {
if (key == null) return defValues
- val pk = preferencesSetKey<String>(key)
+ val pk = stringSetPreferencesKey(key)
return runBlocking {
dataStore.data.map { it[pk] ?: defValues }.first()
}
@@ -100,7 +103,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope,
override fun getInt(key: String?, defValue: Int): Int {
if (key == null) return defValue
- val pk = preferencesKey<Int>(key)
+ val pk = intPreferencesKey(key)
return runBlocking {
dataStore.data.map { it[pk] ?: defValue }.first()
}
@@ -108,7 +111,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope,
override fun getLong(key: String?, defValue: Long): Long {
if (key == null) return defValue
- val pk = preferencesKey<Long>(key)
+ val pk = longPreferencesKey(key)
return runBlocking {
dataStore.data.map { it[pk] ?: defValue }.first()
}
@@ -116,7 +119,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope,
override fun getFloat(key: String?, defValue: Float): Float {
if (key == null) return defValue
- val pk = preferencesKey<Float>(key)
+ val pk = floatPreferencesKey(key)
return runBlocking {
dataStore.data.map { it[pk] ?: defValue }.first()
}
@@ -124,7 +127,7 @@ class PreferencesPreferenceDataStore(private val coroutineScope: CoroutineScope,
override fun getBoolean(key: String?, defValue: Boolean): Boolean {
if (key == null) return defValue
- val pk = preferencesKey<Boolean>(key)
+ val pk = booleanPreferencesKey(key)
return runBlocking {
dataStore.data.map { it[pk] ?: defValue }.first()
}
diff --git a/ui/src/main/java/com/wireguard/android/util/UserKnobs.kt b/ui/src/main/java/com/wireguard/android/util/UserKnobs.kt
index b0107ebf..a983bf5a 100644
--- a/ui/src/main/java/com/wireguard/android/util/UserKnobs.kt
+++ b/ui/src/main/java/com/wireguard/android/util/UserKnobs.kt
@@ -5,16 +5,16 @@
package com.wireguard.android.util
-import androidx.datastore.preferences.edit
-import androidx.datastore.preferences.preferencesKey
-import androidx.datastore.preferences.preferencesSetKey
-import androidx.datastore.preferences.remove
+import androidx.datastore.preferences.core.booleanPreferencesKey
+import androidx.datastore.preferences.core.edit
+import androidx.datastore.preferences.core.stringPreferencesKey
+import androidx.datastore.preferences.core.stringSetPreferencesKey
import com.wireguard.android.Application
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
object UserKnobs {
- private val DISABLE_KERNEL_MODULE = preferencesKey<Boolean>("disable_kernel_module")
+ private val DISABLE_KERNEL_MODULE = booleanPreferencesKey("disable_kernel_module")
val disableKernelModule: Flow<Boolean>
get() = Application.getPreferencesDataStore().data.map {
it[DISABLE_KERNEL_MODULE] ?: false
@@ -29,31 +29,31 @@ object UserKnobs {
}
}
- private val MULTIPLE_TUNNELS = preferencesKey<Boolean>("multiple_tunnels")
+ private val MULTIPLE_TUNNELS = booleanPreferencesKey("multiple_tunnels")
val multipleTunnels: Flow<Boolean>
get() = Application.getPreferencesDataStore().data.map {
it[MULTIPLE_TUNNELS] ?: false
}
- private val DARK_THEME = preferencesKey<Boolean>("dark_theme")
+ private val DARK_THEME = booleanPreferencesKey("dark_theme")
val darkTheme: Flow<Boolean>
get() = Application.getPreferencesDataStore().data.map {
it[DARK_THEME] ?: false
}
- private val ALLOW_REMOTE_CONTROL_INTENTS = preferencesKey<Boolean>("allow_remote_control_intents")
+ private val ALLOW_REMOTE_CONTROL_INTENTS = booleanPreferencesKey("allow_remote_control_intents")
val allowRemoteControlIntents: Flow<Boolean>
get() = Application.getPreferencesDataStore().data.map {
it[ALLOW_REMOTE_CONTROL_INTENTS] ?: false
}
- private val RESTORE_ON_BOOT = preferencesKey<Boolean>("restore_on_boot")
+ private val RESTORE_ON_BOOT = booleanPreferencesKey("restore_on_boot")
val restoreOnBoot: Flow<Boolean>
get() = Application.getPreferencesDataStore().data.map {
it[RESTORE_ON_BOOT] ?: false
}
- private val LAST_USED_TUNNEL = preferencesKey<String>("last_used_tunnel")
+ private val LAST_USED_TUNNEL = stringPreferencesKey("last_used_tunnel")
val lastUsedTunnel: Flow<String?>
get() = Application.getPreferencesDataStore().data.map {
it[LAST_USED_TUNNEL]
@@ -68,7 +68,7 @@ object UserKnobs {
}
}
- private val RUNNING_TUNNELS = preferencesSetKey<String>("enabled_configs")
+ private val RUNNING_TUNNELS = stringSetPreferencesKey("enabled_configs")
val runningTunnels: Flow<Set<String>>
get() = Application.getPreferencesDataStore().data.map {
it[RUNNING_TUNNELS] ?: emptySet()