Loading packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java +27 −2 Original line number Diff line number Diff line Loading @@ -32,6 +32,7 @@ import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.ColorInt; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.android.settingslib.widget.mainswitch.R; Loading @@ -42,7 +43,7 @@ import java.util.List; /** * MainSwitchBar is a View with a customized Switch. * This component is used as the main switch of the page * to enable or disable the prefereces on the page. * to enable or disable the preferences on the page. */ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListener { Loading @@ -58,6 +59,8 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen protected CompoundButton mSwitch; private final View mFrameView; private @Nullable PreChangeListener mPreChangeListener; public MainSwitchBar(Context context) { this(context, null); } Loading Loading @@ -138,10 +141,20 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen @Override public boolean performClick() { if (callPreChangeListener()) { mSwitch.performClick(); } return super.performClick(); } protected boolean callPreChangeListener() { return mPreChangeListener == null || mPreChangeListener.preChange(!mSwitch.isChecked()); } public void setPreChangeListener(@Nullable PreChangeListener preChangeListener) { mPreChangeListener = preChangeListener; } /** * Update the switch status */ Loading Loading @@ -341,4 +354,16 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen requestLayout(); } /** * Listener callback before switch is toggled. */ public interface PreChangeListener { /** * Returns if the new value can be set. * * When false is return, the switch toggle is not triggered at all. */ boolean preChange(boolean isCheck); } } packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/PreferenceMetadata.kt +5 −16 Original line number Diff line number Diff line Loading @@ -107,20 +107,11 @@ interface PreferenceMetadata { * * UI framework normally does not allow user to interact with the preference widget when it is * disabled. * * [dependencyOfEnabledState] is provided to support dependency, the [shouldDisableDependents] * value of dependent preference is used to decide enabled state. */ fun isEnabled(context: Context): Boolean { val dependency = dependencyOfEnabledState(context) ?: return true return !dependency.shouldDisableDependents(context) } fun isEnabled(context: Context): Boolean = true /** Returns the key of depended preference to decide the enabled state. */ fun dependencyOfEnabledState(context: Context): PreferenceMetadata? = null /** Returns whether this preference's dependents should be disabled. */ fun shouldDisableDependents(context: Context): Boolean = !isEnabled(context) /** Returns the keys of depended preferences. */ fun dependencies(context: Context): Array<String> = arrayOf() /** Returns if the preference is persistent in datastore. */ fun isPersistent(context: Context): Boolean = this is PersistentPreference<*> Loading Loading @@ -174,13 +165,11 @@ interface PreferenceMetadata { } /** Metadata of preference group. */ @AnyThread interface PreferenceGroup : PreferenceMetadata @AnyThread interface PreferenceGroup : PreferenceMetadata /** Metadata of preference category. */ @AnyThread open class PreferenceCategory(override val key: String, override val title: Int) : PreferenceGroup open class PreferenceCategory(override val key: String, override val title: Int) : PreferenceGroup /** Metadata of preference screen. */ @AnyThread Loading packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/PreferenceTypes.kt +2 −10 Original line number Diff line number Diff line Loading @@ -16,18 +16,10 @@ package com.android.settingslib.metadata import android.content.Context import androidx.annotation.StringRes /** * Common base class for preferences that have two selectable states, save a boolean value, and may * have dependent preferences that are enabled/disabled based on the current state. */ interface TwoStatePreference : PreferenceMetadata, PersistentPreference<Boolean>, BooleanValue { override fun shouldDisableDependents(context: Context) = storage(context).getBoolean(key) != true || super.shouldDisableDependents(context) } /** Common base class for preferences that have two selectable states and save a boolean value. */ interface TwoStatePreference : PreferenceMetadata, PersistentPreference<Boolean>, BooleanValue /** A preference that provides a two-state toggleable option. */ open class SwitchPreference Loading packages/SettingsLib/Preference/src/com/android/settingslib/preference/PreferenceScreenBindingHelper.kt +5 −5 Original line number Diff line number Diff line Loading @@ -97,14 +97,14 @@ class PreferenceScreenBindingHelper( val preferencesBuilder = ImmutableMap.builder<String, PreferenceHierarchyNode>() val dependenciesBuilder = ImmutableMultimap.builder<String, String>() val lifecycleAwarePreferences = mutableListOf<PreferenceLifecycleProvider>() fun PreferenceMetadata.addDependency(dependency: PreferenceMetadata) { dependenciesBuilder.put(key, dependency.key) } fun PreferenceHierarchyNode.addNode() { metadata.let { preferencesBuilder.put(it.key, this) it.dependencyOfEnabledState(context)?.addDependency(it) val key = it.key preferencesBuilder.put(key, this) for (dependency in it.dependencies(context)) { dependenciesBuilder.put(dependency, key) } if (it is PreferenceLifecycleProvider) lifecycleAwarePreferences.add(it) } } Loading Loading
packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java +27 −2 Original line number Diff line number Diff line Loading @@ -32,6 +32,7 @@ import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.ColorInt; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.android.settingslib.widget.mainswitch.R; Loading @@ -42,7 +43,7 @@ import java.util.List; /** * MainSwitchBar is a View with a customized Switch. * This component is used as the main switch of the page * to enable or disable the prefereces on the page. * to enable or disable the preferences on the page. */ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListener { Loading @@ -58,6 +59,8 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen protected CompoundButton mSwitch; private final View mFrameView; private @Nullable PreChangeListener mPreChangeListener; public MainSwitchBar(Context context) { this(context, null); } Loading Loading @@ -138,10 +141,20 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen @Override public boolean performClick() { if (callPreChangeListener()) { mSwitch.performClick(); } return super.performClick(); } protected boolean callPreChangeListener() { return mPreChangeListener == null || mPreChangeListener.preChange(!mSwitch.isChecked()); } public void setPreChangeListener(@Nullable PreChangeListener preChangeListener) { mPreChangeListener = preChangeListener; } /** * Update the switch status */ Loading Loading @@ -341,4 +354,16 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen requestLayout(); } /** * Listener callback before switch is toggled. */ public interface PreChangeListener { /** * Returns if the new value can be set. * * When false is return, the switch toggle is not triggered at all. */ boolean preChange(boolean isCheck); } }
packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/PreferenceMetadata.kt +5 −16 Original line number Diff line number Diff line Loading @@ -107,20 +107,11 @@ interface PreferenceMetadata { * * UI framework normally does not allow user to interact with the preference widget when it is * disabled. * * [dependencyOfEnabledState] is provided to support dependency, the [shouldDisableDependents] * value of dependent preference is used to decide enabled state. */ fun isEnabled(context: Context): Boolean { val dependency = dependencyOfEnabledState(context) ?: return true return !dependency.shouldDisableDependents(context) } fun isEnabled(context: Context): Boolean = true /** Returns the key of depended preference to decide the enabled state. */ fun dependencyOfEnabledState(context: Context): PreferenceMetadata? = null /** Returns whether this preference's dependents should be disabled. */ fun shouldDisableDependents(context: Context): Boolean = !isEnabled(context) /** Returns the keys of depended preferences. */ fun dependencies(context: Context): Array<String> = arrayOf() /** Returns if the preference is persistent in datastore. */ fun isPersistent(context: Context): Boolean = this is PersistentPreference<*> Loading Loading @@ -174,13 +165,11 @@ interface PreferenceMetadata { } /** Metadata of preference group. */ @AnyThread interface PreferenceGroup : PreferenceMetadata @AnyThread interface PreferenceGroup : PreferenceMetadata /** Metadata of preference category. */ @AnyThread open class PreferenceCategory(override val key: String, override val title: Int) : PreferenceGroup open class PreferenceCategory(override val key: String, override val title: Int) : PreferenceGroup /** Metadata of preference screen. */ @AnyThread Loading
packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/PreferenceTypes.kt +2 −10 Original line number Diff line number Diff line Loading @@ -16,18 +16,10 @@ package com.android.settingslib.metadata import android.content.Context import androidx.annotation.StringRes /** * Common base class for preferences that have two selectable states, save a boolean value, and may * have dependent preferences that are enabled/disabled based on the current state. */ interface TwoStatePreference : PreferenceMetadata, PersistentPreference<Boolean>, BooleanValue { override fun shouldDisableDependents(context: Context) = storage(context).getBoolean(key) != true || super.shouldDisableDependents(context) } /** Common base class for preferences that have two selectable states and save a boolean value. */ interface TwoStatePreference : PreferenceMetadata, PersistentPreference<Boolean>, BooleanValue /** A preference that provides a two-state toggleable option. */ open class SwitchPreference Loading
packages/SettingsLib/Preference/src/com/android/settingslib/preference/PreferenceScreenBindingHelper.kt +5 −5 Original line number Diff line number Diff line Loading @@ -97,14 +97,14 @@ class PreferenceScreenBindingHelper( val preferencesBuilder = ImmutableMap.builder<String, PreferenceHierarchyNode>() val dependenciesBuilder = ImmutableMultimap.builder<String, String>() val lifecycleAwarePreferences = mutableListOf<PreferenceLifecycleProvider>() fun PreferenceMetadata.addDependency(dependency: PreferenceMetadata) { dependenciesBuilder.put(key, dependency.key) } fun PreferenceHierarchyNode.addNode() { metadata.let { preferencesBuilder.put(it.key, this) it.dependencyOfEnabledState(context)?.addDependency(it) val key = it.key preferencesBuilder.put(key, this) for (dependency in it.dependencies(context)) { dependenciesBuilder.put(dependency, key) } if (it is PreferenceLifecycleProvider) lifecycleAwarePreferences.add(it) } } Loading