[Catalyst] Refine DataSaverMainSwitchPreference

Export datastore for preference state monitoring.

Bug: 368359883
Flag: com.android.settings.flags.catalyst
Test: devtool
Change-Id: Ie6b001cdd6a8d27f9cb16d563852ad88b7366916
This commit is contained in:
Jacky Wang
2024-12-08 21:08:38 +08:00
parent 7ca56d493a
commit 387753c005
3 changed files with 40 additions and 37 deletions

View File

@@ -19,18 +19,14 @@ package com.android.settings.datausage
import android.content.Context import android.content.Context
import com.android.settings.R import com.android.settings.R
import com.android.settings.widget.MainSwitchBarMetadata import com.android.settings.widget.MainSwitchBarMetadata
import com.android.settingslib.datastore.AbstractKeyedDataObservable
import com.android.settingslib.datastore.DataChangeReason
import com.android.settingslib.datastore.KeyValueStore import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.NoOpKeyedObservable
import com.android.settingslib.metadata.PreferenceLifecycleContext
import com.android.settingslib.metadata.PreferenceLifecycleProvider import com.android.settingslib.metadata.PreferenceLifecycleProvider
import com.android.settingslib.metadata.ReadWritePermit import com.android.settingslib.metadata.ReadWritePermit
import com.android.settingslib.metadata.SensitivityLevel import com.android.settingslib.metadata.SensitivityLevel
class DataSaverMainSwitchPreference(context: Context) : class DataSaverMainSwitchPreference : MainSwitchBarMetadata, PreferenceLifecycleProvider {
MainSwitchBarMetadata, PreferenceLifecycleProvider {
private val dataSaverBackend = DataSaverBackend(context)
private var dataSaverBackendListener: DataSaverBackend.Listener? = null
override val key override val key
get() = KEY get() = KEY
@@ -38,7 +34,7 @@ class DataSaverMainSwitchPreference(context: Context) :
override val title override val title
get() = R.string.data_saver_switch_title get() = R.string.data_saver_switch_title
override fun storage(context: Context): KeyValueStore = DataSaverStore(dataSaverBackend) override fun storage(context: Context) = createDataStore(context)
override fun getReadPermit(context: Context, myUid: Int, callingUid: Int) = override fun getReadPermit(context: Context, myUid: Int, callingUid: Int) =
ReadWritePermit.ALLOW ReadWritePermit.ALLOW
@@ -49,24 +45,11 @@ class DataSaverMainSwitchPreference(context: Context) :
override val sensitivityLevel override val sensitivityLevel
get() = SensitivityLevel.NO_SENSITIVITY get() = SensitivityLevel.NO_SENSITIVITY
override fun onStart(context: PreferenceLifecycleContext) {
val listener = DataSaverBackend.Listener { context.notifyPreferenceChange(KEY) }
dataSaverBackendListener = listener
dataSaverBackend.addListener(listener)
}
override fun onStop(context: PreferenceLifecycleContext) {
dataSaverBackendListener?.let {
dataSaverBackend.remListener(it)
dataSaverBackendListener = null
}
}
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
private class DataSaverStore(private val dataSaverBackend: DataSaverBackend) : private class DataSaverStore(private val dataSaverBackend: DataSaverBackend) :
NoOpKeyedObservable<String>(), KeyValueStore { AbstractKeyedDataObservable<String>(), KeyValueStore, DataSaverBackend.Listener {
override fun contains(key: String) = true // just assume the datastore contains the value override fun contains(key: String) = key == KEY
override fun <T : Any> getValue(key: String, valueType: Class<T>): T? = override fun <T : Any> getValue(key: String, valueType: Class<T>): T? =
dataSaverBackend.isDataSaverEnabled as T? dataSaverBackend.isDataSaverEnabled as T?
@@ -74,9 +57,20 @@ class DataSaverMainSwitchPreference(context: Context) :
override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) { override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
dataSaverBackend.isDataSaverEnabled = value as Boolean dataSaverBackend.isDataSaverEnabled = value as Boolean
} }
override fun onFirstObserverAdded() = dataSaverBackend.addListener(this)
override fun onLastObserverRemoved() = dataSaverBackend.remListener(this)
override fun onDataSaverChanged(isDataSaving: Boolean) =
notifyChange(KEY, DataChangeReason.UPDATE)
} }
companion object { companion object {
const val KEY = "use_data_saver" const val KEY = "use_data_saver"
/** Creates [KeyValueStore] for data saver preference. */
fun createDataStore(context: Context): KeyValueStore =
DataSaverStore(DataSaverBackend(context))
} }
} }

View File

@@ -19,24 +19,30 @@ package com.android.settings.datausage
import android.content.Context import android.content.Context
import android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID import android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID
import com.android.settings.R import com.android.settings.R
import com.android.settings.Settings.DataSaverSummaryActivity
import com.android.settings.flags.Flags import com.android.settings.flags.Flags
import com.android.settings.utils.makeLaunchIntent
import com.android.settingslib.datastore.HandlerExecutor
import com.android.settingslib.datastore.KeyedObserver
import com.android.settingslib.metadata.PreferenceAvailabilityProvider import com.android.settingslib.metadata.PreferenceAvailabilityProvider
import com.android.settingslib.metadata.PreferenceLifecycleContext import com.android.settingslib.metadata.PreferenceLifecycleContext
import com.android.settingslib.metadata.PreferenceLifecycleProvider import com.android.settingslib.metadata.PreferenceLifecycleProvider
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.metadata.PreferenceSummaryProvider import com.android.settingslib.metadata.PreferenceSummaryProvider
import com.android.settingslib.metadata.ProvidePreferenceScreen import com.android.settingslib.metadata.ProvidePreferenceScreen
import com.android.settingslib.metadata.preferenceHierarchy import com.android.settingslib.metadata.preferenceHierarchy
import com.android.settingslib.preference.PreferenceScreenCreator import com.android.settingslib.preference.PreferenceScreenCreator
import com.android.settings.datausage.DataSaverMainSwitchPreference.Companion.KEY as DATA_SAVER_KEY
@ProvidePreferenceScreen @ProvidePreferenceScreen
class DataSaverScreen : class DataSaverScreen(context: Context) :
PreferenceScreenCreator, PreferenceScreenCreator,
PreferenceAvailabilityProvider, PreferenceAvailabilityProvider,
PreferenceSummaryProvider, PreferenceSummaryProvider,
PreferenceLifecycleProvider { PreferenceLifecycleProvider {
private var dataSaverBackend: DataSaverBackend? = null private val dataSaverStore = DataSaverMainSwitchPreference.createDataStore(context)
private var dataSaverBackendListener: DataSaverBackend.Listener? = null private lateinit var keyedObserver: KeyedObserver<String>
override val key override val key
get() = KEY get() = KEY
@@ -53,7 +59,7 @@ class DataSaverScreen :
override fun getSummary(context: Context): CharSequence? = override fun getSummary(context: Context): CharSequence? =
when { when {
DataSaverBackend(context).isDataSaverEnabled -> dataSaverStore.getBoolean(DATA_SAVER_KEY) == true ->
context.getString(R.string.data_saver_on) context.getString(R.string.data_saver_on)
else -> context.getString(R.string.data_saver_off) else -> context.getString(R.string.data_saver_off)
} }
@@ -65,21 +71,21 @@ class DataSaverScreen :
override fun fragmentClass() = DataSaverSummary::class.java override fun fragmentClass() = DataSaverSummary::class.java
override fun getLaunchIntent(context: Context, metadata: PreferenceMetadata?) =
makeLaunchIntent(context, DataSaverSummaryActivity::class.java, metadata?.key)
override fun getPreferenceHierarchy(context: Context) = override fun getPreferenceHierarchy(context: Context) =
preferenceHierarchy(this) { +DataSaverMainSwitchPreference(context) } preferenceHierarchy(this) { +DataSaverMainSwitchPreference() }
override fun hasCompleteHierarchy() = false override fun hasCompleteHierarchy() = false
override fun onStart(context: PreferenceLifecycleContext) { override fun onCreate(context: PreferenceLifecycleContext) {
val listener = DataSaverBackend.Listener { context.notifyPreferenceChange(KEY) } keyedObserver = KeyedObserver { _, _ -> context.notifyPreferenceChange(KEY) }
dataSaverBackendListener = listener dataSaverStore.addObserver(DATA_SAVER_KEY, keyedObserver, HandlerExecutor.main)
dataSaverBackend = DataSaverBackend(context).apply { addListener(listener) }
} }
override fun onStop(context: PreferenceLifecycleContext) { override fun onDestroy(context: PreferenceLifecycleContext) {
dataSaverBackend?.remListener(dataSaverBackendListener) dataSaverStore.removeObserver(DATA_SAVER_KEY, keyedObserver)
dataSaverBackend = null
dataSaverBackendListener = null
} }
companion object { companion object {

View File

@@ -16,13 +16,16 @@
package com.android.settings.datausage package com.android.settings.datausage
import androidx.test.core.app.ApplicationProvider
import com.android.settings.flags.Flags import com.android.settings.flags.Flags
import com.android.settingslib.preference.CatalystScreenTestCase import com.android.settingslib.preference.CatalystScreenTestCase
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import org.junit.Test import org.junit.Test
class DataSaverScreenTest : CatalystScreenTestCase() { class DataSaverScreenTest : CatalystScreenTestCase() {
override val preferenceScreenCreator = DataSaverScreen() override val preferenceScreenCreator =
DataSaverScreen(ApplicationProvider.getApplicationContext())
override val flagName override val flagName
get() = Flags.FLAG_CATALYST_RESTRICT_BACKGROUND_PARENT_ENTRY get() = Flags.FLAG_CATALYST_RESTRICT_BACKGROUND_PARENT_ENTRY