From 94ecb13d2fd9f36a10b0209b2ddf161b4cc0a07f Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 6 May 2021 16:55:29 +0200 Subject: ui: update datastore and rework api Signed-off-by: Jason A. Donenfeld --- build.gradle | 2 +- .../main/java/com/wireguard/android/Application.kt | 9 ++--- .../preference/PreferencesPreferenceDataStore.kt | 39 ++++++++++++---------- .../java/com/wireguard/android/util/UserKnobs.kt | 22 ++++++------ 4 files changed, 38 insertions(+), 34 deletions(-) diff --git a/build.gradle b/build.gradle index e7160643..6b1606fd 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { coordinatorLayoutVersion = '1.1.0' coreKtxVersion = '1.3.2' coroutinesVersion = '1.4.3' - datastoreVersion = '1.0.0-alpha02' + datastoreVersion = '1.0.0-beta01' desugarVersion = '1.0.10' fragmentVersion = '1.3.3' jsr305Version = '3.0.2' 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) : PreferenceDataStore() { override fun putString(key: String?, value: String?) { if (key == null) return - val pk = preferencesKey(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?) { if (key == null) return - val pk = preferencesSetKey(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(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(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(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(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(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?): Set? { if (key == null) return defValues - val pk = preferencesSetKey(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(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(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(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(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("disable_kernel_module") + private val DISABLE_KERNEL_MODULE = booleanPreferencesKey("disable_kernel_module") val disableKernelModule: Flow get() = Application.getPreferencesDataStore().data.map { it[DISABLE_KERNEL_MODULE] ?: false @@ -29,31 +29,31 @@ object UserKnobs { } } - private val MULTIPLE_TUNNELS = preferencesKey("multiple_tunnels") + private val MULTIPLE_TUNNELS = booleanPreferencesKey("multiple_tunnels") val multipleTunnels: Flow get() = Application.getPreferencesDataStore().data.map { it[MULTIPLE_TUNNELS] ?: false } - private val DARK_THEME = preferencesKey("dark_theme") + private val DARK_THEME = booleanPreferencesKey("dark_theme") val darkTheme: Flow get() = Application.getPreferencesDataStore().data.map { it[DARK_THEME] ?: false } - private val ALLOW_REMOTE_CONTROL_INTENTS = preferencesKey("allow_remote_control_intents") + private val ALLOW_REMOTE_CONTROL_INTENTS = booleanPreferencesKey("allow_remote_control_intents") val allowRemoteControlIntents: Flow get() = Application.getPreferencesDataStore().data.map { it[ALLOW_REMOTE_CONTROL_INTENTS] ?: false } - private val RESTORE_ON_BOOT = preferencesKey("restore_on_boot") + private val RESTORE_ON_BOOT = booleanPreferencesKey("restore_on_boot") val restoreOnBoot: Flow get() = Application.getPreferencesDataStore().data.map { it[RESTORE_ON_BOOT] ?: false } - private val LAST_USED_TUNNEL = preferencesKey("last_used_tunnel") + private val LAST_USED_TUNNEL = stringPreferencesKey("last_used_tunnel") val lastUsedTunnel: Flow get() = Application.getPreferencesDataStore().data.map { it[LAST_USED_TUNNEL] @@ -68,7 +68,7 @@ object UserKnobs { } } - private val RUNNING_TUNNELS = preferencesSetKey("enabled_configs") + private val RUNNING_TUNNELS = stringSetPreferencesKey("enabled_configs") val runningTunnels: Flow> get() = Application.getPreferencesDataStore().data.map { it[RUNNING_TUNNELS] ?: emptySet() -- cgit v1.2.3