diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-09-18 14:03:48 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-09-18 20:29:23 +0200 |
commit | d200437813ae09769dc90820ded3911a324601ca (patch) | |
tree | a0092ae87fb90afa70c67c4ed3715c5f1c2e1da6 /ui/src/main/java/com/wireguard/android/preference | |
parent | 3ffe7a5e68dcd6e1f3e589d904b723aa4eb24a38 (diff) |
ui: move to Jetpack DataStore instead of SharedPrefs
Hopefully PreferencesPreferenceDataStore gets to go away sometime down
the line.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/preference')
3 files changed, 142 insertions, 12 deletions
diff --git a/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt b/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt index 3d47d2ea..5d21a541 100644 --- a/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt @@ -4,7 +4,6 @@ */ package com.wireguard.android.preference -import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.util.AttributeSet @@ -15,6 +14,7 @@ import com.wireguard.android.R import com.wireguard.android.activity.SettingsActivity import com.wireguard.android.backend.Tunnel import com.wireguard.android.backend.WgQuickBackend +import com.wireguard.android.util.UserKnobs import com.wireguard.android.util.lifecycleScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob @@ -38,16 +38,15 @@ class KernelModuleDisablerPreference(context: Context, attrs: AttributeSet?) : P override fun getTitle() = if (state == State.UNKNOWN) "" else context.getString(state.titleResourceId) - @SuppressLint("ApplySharedPref") override fun onClick() { - if (state == State.DISABLED) { - setState(State.ENABLING) - Application.getSharedPreferences().edit().putBoolean("disable_kernel_module", false).commit() - } else if (state == State.ENABLED) { - setState(State.DISABLING) - Application.getSharedPreferences().edit().putBoolean("disable_kernel_module", true).commit() - } lifecycleScope.launch { + if (state == State.DISABLED) { + setState(State.ENABLING) + UserKnobs.setDisableKernelModule(false) + } else if (state == State.ENABLED) { + setState(State.DISABLING) + UserKnobs.setDisableKernelModule(true) + } val observableTunnels = Application.getTunnelManager().getTunnels() val downings = observableTunnels.map { async(SupervisorJob()) { it.setStateAsync(Tunnel.State.DOWN) } } try { diff --git a/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt b/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt index 7e437982..5ba2c4f0 100644 --- a/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt @@ -4,7 +4,6 @@ */ package com.wireguard.android.preference -import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.system.OsConstants @@ -15,6 +14,7 @@ import com.wireguard.android.Application import com.wireguard.android.R import com.wireguard.android.activity.SettingsActivity import com.wireguard.android.util.ErrorMessages +import com.wireguard.android.util.UserKnobs import com.wireguard.android.util.lifecycleScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -27,7 +27,6 @@ class ModuleDownloaderPreference(context: Context, attrs: AttributeSet?) : Prefe override fun getTitle() = context.getString(R.string.module_installer_title) - @SuppressLint("ApplySharedPref") override fun onClick() { setState(State.WORKING) lifecycleScope.launch { @@ -36,7 +35,7 @@ class ModuleDownloaderPreference(context: Context, attrs: AttributeSet?) : Prefe OsConstants.ENOENT -> setState(State.NOTFOUND) OsConstants.EXIT_SUCCESS -> { setState(State.SUCCESS) - Application.getSharedPreferences().edit().remove("disable_kernel_module").commit() + UserKnobs.setDisableKernelModule(null) withContext(Dispatchers.IO) { val restartIntent = Intent(context, SettingsActivity::class.java) restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) diff --git a/ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt b/ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt new file mode 100644 index 00000000..96ca2b1c --- /dev/null +++ b/ui/src/main/java/com/wireguard/android/preference/PreferencesPreferenceDataStore.kt @@ -0,0 +1,132 @@ +/* + * Copyright © 2020 WireGuard LLC. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +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.preference.PreferenceDataStore +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.launch +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) + coroutineScope.launch { + dataStore.edit { + if (value == null) it.remove(pk) + else it[pk] = value + } + } + } + + override fun putStringSet(key: String?, values: Set<String?>?) { + if (key == null) return + val pk = preferencesSetKey<String>(key) + val filteredValues = values?.filterNotNull()?.toSet() + coroutineScope.launch { + dataStore.edit { + if (filteredValues == null || filteredValues.isEmpty()) it.remove(pk) + else it[pk] = filteredValues + } + } + } + + override fun putInt(key: String?, value: Int) { + if (key == null) return + val pk = preferencesKey<Int>(key) + coroutineScope.launch { + dataStore.edit { + it[pk] = value + } + } + } + + override fun putLong(key: String?, value: Long) { + if (key == null) return + val pk = preferencesKey<Long>(key) + coroutineScope.launch { + dataStore.edit { + it[pk] = value + } + } + } + + override fun putFloat(key: String?, value: Float) { + if (key == null) return + val pk = preferencesKey<Float>(key) + coroutineScope.launch { + dataStore.edit { + it[pk] = value + } + } + } + + override fun putBoolean(key: String?, value: Boolean) { + if (key == null) return + val pk = preferencesKey<Boolean>(key) + coroutineScope.launch { + dataStore.edit { + it[pk] = value + } + } + } + + override fun getString(key: String?, defValue: String?): String? { + if (key == null) return defValue + val pk = preferencesKey<String>(key) + return runBlocking { + dataStore.data.map { it[pk] ?: defValue }.first() + } + } + + override fun getStringSet(key: String?, defValues: Set<String?>?): Set<String?>? { + if (key == null) return defValues + val pk = preferencesSetKey<String>(key) + return runBlocking { + dataStore.data.map { it[pk] ?: defValues }.first() + } + } + + override fun getInt(key: String?, defValue: Int): Int { + if (key == null) return defValue + val pk = preferencesKey<Int>(key) + return runBlocking { + dataStore.data.map { it[pk] ?: defValue }.first() + } + } + + override fun getLong(key: String?, defValue: Long): Long { + if (key == null) return defValue + val pk = preferencesKey<Long>(key) + return runBlocking { + dataStore.data.map { it[pk] ?: defValue }.first() + } + } + + override fun getFloat(key: String?, defValue: Float): Float { + if (key == null) return defValue + val pk = preferencesKey<Float>(key) + return runBlocking { + dataStore.data.map { it[pk] ?: defValue }.first() + } + } + + override fun getBoolean(key: String?, defValue: Boolean): Boolean { + if (key == null) return defValue + val pk = preferencesKey<Boolean>(key) + return runBlocking { + dataStore.data.map { it[pk] ?: defValue }.first() + } + } +} |