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>
This commit is contained in:
Jason A. Donenfeld
2020-09-18 20:29:23 +02:00
parent 3ffe7a5e68
commit d200437813
10 changed files with 304 additions and 101 deletions
@@ -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 {
@@ -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)
@@ -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()
}
}
}