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

Commit 2ffa7b74 authored by Jacky Wang's avatar Jacky Wang
Browse files

[Catalyst] Clean up AmbientWallpaperPreference data store

NO_IFTTT=Catalyst only

Bug: 396630828
Flag: com.android.systemui.shared.ambient_aod
Test: manual
Change-Id: I40d317a1731a286097ea66fffff9e866d1f3bffb
parent 5d74763e
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ import com.android.systemui.shared.Flags.ambientAod
 * subpage for additional related settings.
 */
@ProvidePreferenceScreen(AmbientDisplayAlwaysOnPreferenceScreen.KEY)
open class AmbientDisplayAlwaysOnPreferenceScreen :
open class AmbientDisplayAlwaysOnPreferenceScreen(context: Context) :
    PreferenceScreenMixin,
    BooleanValuePreference,
    PrimarySwitchPreferenceBinding,
@@ -68,7 +68,7 @@ open class AmbientDisplayAlwaysOnPreferenceScreen :
    PreferenceLifecycleProvider,
    PreferenceSummaryProvider {

    private val ambientWallpaperPreference = AmbientWallpaperPreference()
    private val ambientWallpaperPreference = AmbientWallpaperPreference(context)

    override val title: Int
        get() = if (ambientAod()) R.string.doze_always_on_title2 else R.string.doze_always_on_title
@@ -107,7 +107,7 @@ open class AmbientDisplayAlwaysOnPreferenceScreen :
            if (isAodSuppressedByBedtime(context)) {
                R.string.aware_summary_when_bedtime_on
            } else if (context.isAmbientWallpaperOptionsAvailable) {
                if (ambientWallpaperPreference.isChecked(context)) {
                if (ambientWallpaperPreference.isChecked()) {
                    R.string.doze_always_on_summary_with_wallpaper
                } else {
                    R.string.doze_always_on_summary_without_wallpaper
+8 −30
Original line number Diff line number Diff line
@@ -18,49 +18,27 @@ package com.android.settings.display.ambient
import android.content.Context
import android.provider.Settings.Secure.DOZE_ALWAYS_ON_WALLPAPER_ENABLED
import com.android.settings.R
import com.android.settings.display.AmbientDisplayAlwaysOnPreference
import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.KeyValueStoreDelegate
import com.android.settingslib.datastore.KeyedObserver
import com.android.settingslib.datastore.SettingsSecureStore
import com.android.settingslib.metadata.SwitchPreference

class AmbientWallpaperPreference :
class AmbientWallpaperPreference(context: Context) :
    SwitchPreference(
        KEY,
        R.string.doze_always_on_wallpaper_title,
        R.string.doze_always_on_wallpaper_description,
    ) {

    override fun storage(context: Context): KeyValueStore =
        Storage(SettingsSecureStore.get(context))
    private val dataStore = context.dataStore

    fun isChecked(context: Context) = storage(context).getBoolean(KEY)!!
    override fun storage(context: Context) = dataStore

    @Suppress("UNCHECKED_CAST")
    private class Storage(private val settingsStore: KeyValueStore) :
        KeyValueStoreDelegate, KeyedObserver<String> {

        override val keyValueStoreDelegate
            get() = settingsStore

        override fun contains(key: String) = settingsStore.contains(key)

        override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) = true as T

        override fun <T : Any> getValue(key: String, valueType: Class<T>) =
            settingsStore.getValue(key, valueType) ?: getDefaultValue(key, valueType)

        override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) =
            settingsStore.setValue(key, valueType, value)

        override fun onKeyChanged(key: String, reason: Int) {
            notifyChange(AmbientDisplayAlwaysOnPreference.KEY, reason)
            notifyChange(AmbientDisplayMainSwitchPreference.KEY, reason)
        }
    }
    fun isChecked() = dataStore.getBoolean(KEY)!!

    companion object {
        val KEY = DOZE_ALWAYS_ON_WALLPAPER_ENABLED
        const val KEY = DOZE_ALWAYS_ON_WALLPAPER_ENABLED

        private val Context.dataStore: KeyValueStore
            get() = SettingsSecureStore.get(this).apply { setDefaultValue(KEY, true) }
    }
}