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

Commit e0004647 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[Catalyst] Add SettingsStore.setDefaultValue" into main

parents b6d0b42e 35c69427
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -51,9 +51,9 @@ class SettingsGlobalStore private constructor(contentResolver: ContentResolver)
                else -> throw UnsupportedOperationException("Get $key $valueType")
            }
                as T?
        } catch (e: SettingNotFoundException) {
        } catch (_: SettingNotFoundException) {
            null
        }
        } ?: getDefaultValue(key, valueType)

    override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
        if (value == null) {
+2 −2
Original line number Diff line number Diff line
@@ -51,9 +51,9 @@ class SettingsSecureStore private constructor(contentResolver: ContentResolver)
                else -> throw UnsupportedOperationException("Get $key $valueType")
            }
                as T?
        } catch (e: SettingNotFoundException) {
        } catch (_: SettingNotFoundException) {
            null
        }
        } ?: getDefaultValue(key, valueType)

    override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
        if (value == null) {
+21 −0
Original line number Diff line number Diff line
@@ -20,11 +20,14 @@ import android.content.ContentResolver
import android.database.ContentObserver
import android.net.Uri
import android.util.Log
import java.util.concurrent.ConcurrentHashMap

/** Base class of the Settings provider data stores. */
abstract class SettingsStore(protected val contentResolver: ContentResolver) :
    AbstractKeyedDataObservable<String>(), KeyValueStore {

    private val defaultValues = ConcurrentHashMap<String, Any>()

    private val contentObserver =
        object : ContentObserver(HandlerExecutor.main) {
            override fun onChange(selfChange: Boolean) {
@@ -50,6 +53,24 @@ abstract class SettingsStore(protected val contentResolver: ContentResolver) :
        contentResolver.unregisterContentObserver(contentObserver)
    }

    /**
     * Sets default value for given key.
     *
     * The observers are not notified for this operation.
     */
    fun setDefaultValue(key: String, value: Any) {
        val oldValue = defaultValues.put(key, value)
        if (oldValue == null) {
            Log.d(tag, "setDefaultValue $key $value")
        } else if (oldValue != value) {
            Log.w(tag, "$key default value is changed from $oldValue to $value")
        }
    }

    @Suppress("UNCHECKED_CAST")
    override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) =
        defaultValues[key] as T?

    /** Tag for logging. */
    abstract val tag: String
}
+2 −2
Original line number Diff line number Diff line
@@ -51,9 +51,9 @@ class SettingsSystemStore private constructor(contentResolver: ContentResolver)
                else -> throw UnsupportedOperationException("Get $key $valueType")
            }
                as T?
        } catch (e: SettingNotFoundException) {
        } catch (_: SettingNotFoundException) {
            null
        }
        } ?: getDefaultValue(key, valueType)

    override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
        if (value == null) {