[Catalyst] Add Data Saver settings metadata
Bug: 368359883 Flag: com.android.settings.flags.catalyst Test: manual Change-Id: I7a4fe8cdd73a551a2b281e3a5a3a0f269b54ee5e
This commit is contained in:
@@ -81,6 +81,7 @@
|
|||||||
android:title="@string/data_saver_title"
|
android:title="@string/data_saver_title"
|
||||||
android:icon="@drawable/ic_settings_data_usage"
|
android:icon="@drawable/ic_settings_data_usage"
|
||||||
android:order="10"
|
android:order="10"
|
||||||
|
android:persistent="false"
|
||||||
settings:isPreferenceVisible="@bool/config_show_data_saver"
|
settings:isPreferenceVisible="@bool/config_show_data_saver"
|
||||||
android:fragment="com.android.settings.datausage.DataSaverSummary"/>
|
android:fragment="com.android.settings.datausage.DataSaverSummary"/>
|
||||||
|
|
||||||
|
|||||||
@@ -17,29 +17,39 @@ package com.android.settings.datausage;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.flags.Flags;
|
||||||
|
|
||||||
public class DataSaverPreference extends Preference implements DataSaverBackend.Listener {
|
public class DataSaverPreference extends Preference implements DataSaverBackend.Listener {
|
||||||
|
|
||||||
private final DataSaverBackend mDataSaverBackend;
|
private final @Nullable DataSaverBackend mDataSaverBackend;
|
||||||
|
|
||||||
public DataSaverPreference(Context context, AttributeSet attrs) {
|
public DataSaverPreference(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
mDataSaverBackend = new DataSaverBackend(context);
|
mDataSaverBackend = isCatalystEnabled() ? null : new DataSaverBackend(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCatalystEnabled() {
|
||||||
|
return Flags.catalyst() && Flags.catalystRestrictBackgroundParentEntry();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttached() {
|
public void onAttached() {
|
||||||
super.onAttached();
|
super.onAttached();
|
||||||
mDataSaverBackend.addListener(this);
|
if (mDataSaverBackend != null) {
|
||||||
|
mDataSaverBackend.addListener(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDetached() {
|
public void onDetached() {
|
||||||
super.onDetached();
|
super.onDetached();
|
||||||
mDataSaverBackend.remListener(this);
|
if (mDataSaverBackend != null) {
|
||||||
|
mDataSaverBackend.remListener(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -17,14 +17,27 @@
|
|||||||
package com.android.settings.datausage
|
package com.android.settings.datausage
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID
|
||||||
import com.android.settings.R
|
import com.android.settings.R
|
||||||
import com.android.settings.flags.Flags
|
import com.android.settings.flags.Flags
|
||||||
|
import com.android.settingslib.metadata.PreferenceAvailabilityProvider
|
||||||
|
import com.android.settingslib.metadata.PreferenceLifecycleContext
|
||||||
|
import com.android.settingslib.metadata.PreferenceLifecycleProvider
|
||||||
|
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
|
||||||
|
|
||||||
@ProvidePreferenceScreen
|
@ProvidePreferenceScreen
|
||||||
class DataSaverScreen : PreferenceScreenCreator {
|
class DataSaverScreen :
|
||||||
|
PreferenceScreenCreator,
|
||||||
|
PreferenceAvailabilityProvider,
|
||||||
|
PreferenceSummaryProvider,
|
||||||
|
PreferenceLifecycleProvider {
|
||||||
|
|
||||||
|
private var dataSaverBackend: DataSaverBackend? = null
|
||||||
|
private var dataSaverBackendListener: DataSaverBackend.Listener? = null
|
||||||
|
|
||||||
override val key
|
override val key
|
||||||
get() = KEY
|
get() = KEY
|
||||||
|
|
||||||
@@ -34,6 +47,20 @@ class DataSaverScreen : PreferenceScreenCreator {
|
|||||||
override val icon: Int
|
override val icon: Int
|
||||||
get() = R.drawable.ic_settings_data_usage
|
get() = R.drawable.ic_settings_data_usage
|
||||||
|
|
||||||
|
override fun isIndexable(context: Context) =
|
||||||
|
DataUsageUtils.hasMobileData(context) &&
|
||||||
|
DataUsageUtils.getDefaultSubscriptionId(context) != INVALID_SUBSCRIPTION_ID
|
||||||
|
|
||||||
|
override fun getSummary(context: Context): CharSequence? =
|
||||||
|
when {
|
||||||
|
DataSaverBackend(context).isDataSaverEnabled ->
|
||||||
|
context.getString(R.string.data_saver_on)
|
||||||
|
else -> context.getString(R.string.data_saver_off)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isAvailable(context: Context) =
|
||||||
|
context.resources.getBoolean(R.bool.config_show_data_saver)
|
||||||
|
|
||||||
override fun isFlagEnabled(context: Context) = Flags.catalystRestrictBackgroundParentEntry()
|
override fun isFlagEnabled(context: Context) = Flags.catalystRestrictBackgroundParentEntry()
|
||||||
|
|
||||||
override fun fragmentClass() = DataSaverSummary::class.java
|
override fun fragmentClass() = DataSaverSummary::class.java
|
||||||
@@ -43,6 +70,18 @@ class DataSaverScreen : PreferenceScreenCreator {
|
|||||||
|
|
||||||
override fun hasCompleteHierarchy() = false
|
override fun hasCompleteHierarchy() = false
|
||||||
|
|
||||||
|
override fun onStart(context: PreferenceLifecycleContext) {
|
||||||
|
val listener = DataSaverBackend.Listener { context.notifyPreferenceChange(this) }
|
||||||
|
dataSaverBackendListener = listener
|
||||||
|
dataSaverBackend = DataSaverBackend(context).apply { addListener(listener) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onStop(context: PreferenceLifecycleContext) {
|
||||||
|
dataSaverBackend?.remListener(dataSaverBackendListener)
|
||||||
|
dataSaverBackend = null
|
||||||
|
dataSaverBackendListener = null
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val KEY = "restrict_background_parent_entry"
|
const val KEY = "restrict_background_parent_entry"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package com.android.settings.network
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.android.settings.R
|
import com.android.settings.R
|
||||||
|
import com.android.settings.datausage.DataSaverScreen
|
||||||
import com.android.settings.flags.Flags
|
import com.android.settings.flags.Flags
|
||||||
import com.android.settingslib.metadata.PreferenceIconProvider
|
import com.android.settingslib.metadata.PreferenceIconProvider
|
||||||
import com.android.settingslib.metadata.ProvidePreferenceScreen
|
import com.android.settingslib.metadata.ProvidePreferenceScreen
|
||||||
@@ -43,7 +44,10 @@ class NetworkDashboardScreen : PreferenceScreenCreator, PreferenceIconProvider {
|
|||||||
|
|
||||||
override fun fragmentClass() = NetworkDashboardFragment::class.java
|
override fun fragmentClass() = NetworkDashboardFragment::class.java
|
||||||
|
|
||||||
override fun getPreferenceHierarchy(context: Context) = preferenceHierarchy(this) {}
|
override fun getPreferenceHierarchy(context: Context) =
|
||||||
|
preferenceHierarchy(this) {
|
||||||
|
+DataSaverScreen.KEY order 10
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val KEY = "network_provider_and_internet_screen"
|
const val KEY = "network_provider_and_internet_screen"
|
||||||
|
|||||||
Reference in New Issue
Block a user