Loading res/xml/display_settings.xml +1 −1 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ settings:userRestriction="no_config_brightness"/> <com.android.settingslib.PrimarySwitchPreference android:key="screen_brightness_mode" android:key="@string/preference_key_auto_brightness" android:title="@string/auto_brightness_title" android:fragment="com.android.settings.display.AutoBrightnessSettings" settings:useAdminDisabledSummary="true" Loading src/com/android/settings/display/AutoBrightnessScreen.kt +23 −7 Original line number Diff line number Diff line Loading @@ -17,7 +17,7 @@ package com.android.settings.display import android.content.Context import android.os.UserManager import android.provider.Settings import android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE import android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC import android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL import androidx.preference.Preference Loading @@ -26,8 +26,10 @@ import com.android.settings.PreferenceRestrictionMixin import com.android.settings.R import com.android.settings.flags.Flags import com.android.settingslib.PrimarySwitchPreferenceBinding import com.android.settingslib.datastore.AbstractKeyedDataObservable import com.android.settingslib.datastore.HandlerExecutor import com.android.settingslib.datastore.KeyValueStore import com.android.settingslib.datastore.KeyedObservableDelegate import com.android.settingslib.datastore.KeyedObserver import com.android.settingslib.datastore.SettingsStore import com.android.settingslib.datastore.SettingsSystemStore import com.android.settingslib.metadata.BooleanValuePreference Loading Loading @@ -106,18 +108,32 @@ class AutoBrightnessScreen : */ @Suppress("UNCHECKED_CAST") private class AutoBrightnessDataStore(private val settingsStore: SettingsStore) : KeyedObservableDelegate<String>(settingsStore), KeyValueStore { AbstractKeyedDataObservable<String>(), KeyedObserver<String>, KeyValueStore { override fun contains(key: String) = settingsStore.contains(key) override fun contains(key: String) = settingsStore.contains(SCREEN_BRIGHTNESS_MODE) override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) = DEFAULT_VALUE.toBoolean() as T override fun <T : Any> getValue(key: String, valueType: Class<T>) = (settingsStore.getInt(key) ?: DEFAULT_VALUE).toBoolean() as T (settingsStore.getInt(SCREEN_BRIGHTNESS_MODE) ?: DEFAULT_VALUE).toBoolean() as T override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) = settingsStore.setInt(key, (value as? Boolean)?.toBrightnessMode()) settingsStore.setInt(SCREEN_BRIGHTNESS_MODE, (value as? Boolean)?.toBrightnessMode()) override fun onFirstObserverAdded() { // observe the underlying storage key settingsStore.addObserver(SCREEN_BRIGHTNESS_MODE, this, HandlerExecutor.main) } override fun onKeyChanged(key: String, reason: Int) { // forward data change to preference hierarchy key notifyChange(KEY, reason) } override fun onLastObserverRemoved() { settingsStore.removeObserver(SCREEN_BRIGHTNESS_MODE, this) } /** Converts brightness mode integer to boolean. */ private fun Int.toBoolean() = this == SCREEN_BRIGHTNESS_MODE_AUTOMATIC Loading @@ -128,7 +144,7 @@ class AutoBrightnessScreen : } companion object { const val KEY = Settings.System.SCREEN_BRIGHTNESS_MODE const val KEY = "auto_brightness_entry" private const val DEFAULT_VALUE = SCREEN_BRIGHTNESS_MODE_MANUAL } } tests/robotests/src/com/android/settings/display/AutoBrightnessScreenTest.kt +5 −1 Original line number Diff line number Diff line Loading @@ -128,7 +128,11 @@ class AutoBrightnessScreenTest { } private fun setScreenBrightnessMode(value: Int) = Settings.System.putInt(context.contentResolver, AutoBrightnessScreen.KEY, value) Settings.System.putInt( context.contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, value, ) private fun getResId() = when { Loading Loading
res/xml/display_settings.xml +1 −1 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ settings:userRestriction="no_config_brightness"/> <com.android.settingslib.PrimarySwitchPreference android:key="screen_brightness_mode" android:key="@string/preference_key_auto_brightness" android:title="@string/auto_brightness_title" android:fragment="com.android.settings.display.AutoBrightnessSettings" settings:useAdminDisabledSummary="true" Loading
src/com/android/settings/display/AutoBrightnessScreen.kt +23 −7 Original line number Diff line number Diff line Loading @@ -17,7 +17,7 @@ package com.android.settings.display import android.content.Context import android.os.UserManager import android.provider.Settings import android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE import android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC import android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL import androidx.preference.Preference Loading @@ -26,8 +26,10 @@ import com.android.settings.PreferenceRestrictionMixin import com.android.settings.R import com.android.settings.flags.Flags import com.android.settingslib.PrimarySwitchPreferenceBinding import com.android.settingslib.datastore.AbstractKeyedDataObservable import com.android.settingslib.datastore.HandlerExecutor import com.android.settingslib.datastore.KeyValueStore import com.android.settingslib.datastore.KeyedObservableDelegate import com.android.settingslib.datastore.KeyedObserver import com.android.settingslib.datastore.SettingsStore import com.android.settingslib.datastore.SettingsSystemStore import com.android.settingslib.metadata.BooleanValuePreference Loading Loading @@ -106,18 +108,32 @@ class AutoBrightnessScreen : */ @Suppress("UNCHECKED_CAST") private class AutoBrightnessDataStore(private val settingsStore: SettingsStore) : KeyedObservableDelegate<String>(settingsStore), KeyValueStore { AbstractKeyedDataObservable<String>(), KeyedObserver<String>, KeyValueStore { override fun contains(key: String) = settingsStore.contains(key) override fun contains(key: String) = settingsStore.contains(SCREEN_BRIGHTNESS_MODE) override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) = DEFAULT_VALUE.toBoolean() as T override fun <T : Any> getValue(key: String, valueType: Class<T>) = (settingsStore.getInt(key) ?: DEFAULT_VALUE).toBoolean() as T (settingsStore.getInt(SCREEN_BRIGHTNESS_MODE) ?: DEFAULT_VALUE).toBoolean() as T override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) = settingsStore.setInt(key, (value as? Boolean)?.toBrightnessMode()) settingsStore.setInt(SCREEN_BRIGHTNESS_MODE, (value as? Boolean)?.toBrightnessMode()) override fun onFirstObserverAdded() { // observe the underlying storage key settingsStore.addObserver(SCREEN_BRIGHTNESS_MODE, this, HandlerExecutor.main) } override fun onKeyChanged(key: String, reason: Int) { // forward data change to preference hierarchy key notifyChange(KEY, reason) } override fun onLastObserverRemoved() { settingsStore.removeObserver(SCREEN_BRIGHTNESS_MODE, this) } /** Converts brightness mode integer to boolean. */ private fun Int.toBoolean() = this == SCREEN_BRIGHTNESS_MODE_AUTOMATIC Loading @@ -128,7 +144,7 @@ class AutoBrightnessScreen : } companion object { const val KEY = Settings.System.SCREEN_BRIGHTNESS_MODE const val KEY = "auto_brightness_entry" private const val DEFAULT_VALUE = SCREEN_BRIGHTNESS_MODE_MANUAL } }
tests/robotests/src/com/android/settings/display/AutoBrightnessScreenTest.kt +5 −1 Original line number Diff line number Diff line Loading @@ -128,7 +128,11 @@ class AutoBrightnessScreenTest { } private fun setScreenBrightnessMode(value: Int) = Settings.System.putInt(context.contentResolver, AutoBrightnessScreen.KEY, value) Settings.System.putInt( context.contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, value, ) private fun getResId() = when { Loading