Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bfb0a089 authored by Jacky Wang's avatar Jacky Wang
Browse files

[Catalyst] Move common APIs from SettingsStore to KeyValueStore

Bug: 325144964
Flag: EXEMPT library
Test: atest
Change-Id: Id4c9c44667cafedcc7fea492a066c5371738eb81
parent 0b3b6a65
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -47,6 +47,37 @@ interface KeyValueStore : KeyedObservable<String> {
     * @param value value to set, null means remove
     */
    fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?)

    /** Gets the boolean value of given key. */
    fun getBoolean(key: String): Boolean? = getValue(key, Boolean::class.javaObjectType)

    /** Sets boolean value for given key, null value means delete the key from data store. */
    fun setBoolean(key: String, value: Boolean?) =
        setValue(key, Boolean::class.javaObjectType, value)

    /** Gets the float value of given key. */
    fun getFloat(key: String): Float? = getValue(key, Float::class.javaObjectType)

    /** Sets float value for given key, null value means delete the key from data store. */
    fun setFloat(key: String, value: Float?) = setValue(key, Float::class.javaObjectType, value)

    /** Gets the int value of given key. */
    fun getInt(key: String): Int? = getValue(key, Int::class.javaObjectType)

    /** Sets int value for given key, null value means delete the key from data store. */
    fun setInt(key: String, value: Int?) = setValue(key, Int::class.javaObjectType, value)

    /** Gets the long value of given key. */
    fun getLong(key: String): Long? = getValue(key, Long::class.javaObjectType)

    /** Sets long value for given key, null value means delete the key from data store. */
    fun setLong(key: String, value: Long?) = setValue(key, Long::class.javaObjectType, value)

    /** Gets the string value of given key. */
    fun getString(key: String): String? = getValue(key, String::class.javaObjectType)

    /** Sets string value for given key, null value means delete the key from data store. */
    fun setString(key: String, value: String?) = setValue(key, String::class.javaObjectType, value)
}

/** [SharedPreferences] based [KeyValueStore]. */
+0 −31
Original line number Diff line number Diff line
@@ -50,37 +50,6 @@ abstract class SettingsStore(protected val contentResolver: ContentResolver) :
        contentResolver.unregisterContentObserver(contentObserver)
    }

    /** Gets the boolean value of given key. */
    fun getBoolean(key: String): Boolean? = getValue(key, Boolean::class.javaObjectType)

    /** Sets boolean value for given key, null value means delete the key from data store. */
    fun setBoolean(key: String, value: Boolean?) =
        setValue(key, Boolean::class.javaObjectType, value)

    /** Gets the float value of given key. */
    fun getFloat(key: String): Float? = getValue(key, Float::class.javaObjectType)

    /** Sets float value for given key, null value means delete the key from data store. */
    fun setFloat(key: String, value: Float?) = setValue(key, Float::class.javaObjectType, value)

    /** Gets the int value of given key. */
    fun getInt(key: String): Int? = getValue(key, Int::class.javaObjectType)

    /** Sets int value for given key, null value means delete the key from data store. */
    fun setInt(key: String, value: Int?) = setValue(key, Int::class.javaObjectType, value)

    /** Gets the long value of given key. */
    fun getLong(key: String): Long? = getValue(key, Long::class.javaObjectType)

    /** Sets long value for given key, null value means delete the key from data store. */
    fun setLong(key: String, value: Long?) = setValue(key, Long::class.javaObjectType, value)

    /** Gets the string value of given key. */
    fun getString(key: String): String? = getValue(key, String::class.javaObjectType)

    /** Sets string value for given key, null value means delete the key from data store. */
    fun setString(key: String, value: String?) = setValue(key, String::class.javaObjectType, value)

    /** Tag for logging. */
    abstract val tag: String
}
+5 −9
Original line number Diff line number Diff line
@@ -55,9 +55,9 @@ import com.android.settingslib.metadata.RangeValue
import com.android.settingslib.metadata.ReadWritePermit
import com.android.settingslib.preference.PreferenceScreenFactory
import com.android.settingslib.preference.PreferenceScreenProvider
import java.util.Locale
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.Locale

private const val TAG = "PreferenceGraphBuilder"

@@ -399,15 +399,11 @@ fun PreferenceMetadata.toProto(
            value = preferenceValueProto {
                when (metadata) {
                    is BooleanValue ->
                        metadata
                            .storage(context)
                            .getValue(metadata.key, Boolean::class.javaObjectType)
                            ?.let { booleanValue = it }
                        metadata.storage(context).getBoolean(metadata.key)?.let {
                            booleanValue = it
                        }
                    is RangeValue -> {
                        metadata
                            .storage(context)
                            .getValue(metadata.key, Int::class.javaObjectType)
                            ?.let { intValue = it }
                        metadata.storage(context).getInt(metadata.key)?.let { intValue = it }
                    }
                    else -> {}
                }
+2 −2
Original line number Diff line number Diff line
@@ -146,7 +146,7 @@ class PreferenceSetterApiHandler(
                val booleanValue = value.booleanValue
                val resultCode = metadata.checkWritePermit(booleanValue)
                if (resultCode != PreferenceSetterResult.OK) return resultCode
                storage.setValue(key, Boolean::class.javaObjectType, booleanValue)
                storage.setBoolean(key, booleanValue)
                return PreferenceSetterResult.OK
            } else if (value.hasIntValue()) {
                val intValue = value.intValue
@@ -155,7 +155,7 @@ class PreferenceSetterApiHandler(
                if (metadata is RangeValue && !metadata.isValidValue(application, intValue)) {
                    return PreferenceSetterResult.INVALID_REQUEST
                }
                storage.setValue(key, Int::class.javaObjectType, intValue)
                storage.setInt(key, intValue)
                return PreferenceSetterResult.OK
            }
        } catch (e: Exception) {
+2 −6
Original line number Diff line number Diff line
@@ -26,8 +26,7 @@ import androidx.annotation.StringRes
interface TwoStatePreference : PreferenceMetadata, PersistentPreference<Boolean>, BooleanValue {

    override fun shouldDisableDependents(context: Context) =
        storage(context).getValue(key, Boolean::class.javaObjectType) != true ||
            super.shouldDisableDependents(context)
        storage(context).getBoolean(key) != true || super.shouldDisableDependents(context)
}

/** A preference that provides a two-state toggleable option. */
@@ -42,7 +41,4 @@ constructor(
/** A preference that provides a two-state toggleable option that can be used as a main switch. */
open class MainSwitchPreference
@JvmOverloads
constructor(
    override val key: String,
    @StringRes override val title: Int = 0,
) : TwoStatePreference
 No newline at end of file
constructor(override val key: String, @StringRes override val title: Int = 0) : TwoStatePreference
Loading