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

Commit 882509d8 authored by Jacky Wang's avatar Jacky Wang Committed by Android (Google) Code Review
Browse files

Merge "Add PreferenceIconProvider to support dynamic preference icon" into main

parents eddcceb3 3a93a70d
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import androidx.annotation.StringRes
 * information:
 * - [PreferenceTitleProvider]: provide dynamic title content
 * - [PreferenceSummaryProvider]: provide dynamic summary content (e.g. based on preference value)
 * - [PreferenceIconProvider]: provide dynamic icon content (e.g. based on flag)
 * - [PreferenceAvailabilityProvider]: provide preference availability (e.g. based on flag)
 * - [PreferenceLifecycleProvider]: provide the lifecycle callbacks and notify state change
 *
@@ -160,6 +161,19 @@ interface PreferenceMetadata {
            this is PreferenceSummaryProvider -> getSummary(context)
            else -> null
        }

    /**
     * Returns the preference icon.
     *
     * Implement [PreferenceIconProvider] interface if icon content is provided dynamically
     * (e.g. icon is provided based on flag value).
     */
    fun getPreferenceIcon(context: Context): Int =
        when {
            icon != 0 -> icon
            this is PreferenceIconProvider -> getIcon(context)
            else -> 0
        }
}

/** Metadata of preference group. */
+11 −0
Original line number Diff line number Diff line
@@ -40,6 +40,17 @@ interface PreferenceSummaryProvider {
    fun getSummary(context: Context): CharSequence?
}

/**
 * Interface to provide dynamic preference icon.
 *
 * Implement this interface implies that the preference icon should not be cached for indexing.
 */
interface PreferenceIconProvider {

    /** Provides preference icon. */
    fun getIcon(context: Context): Int
}

/**
 * Interface to provide the state of preference availability.
 *
+4 −3
Original line number Diff line number Diff line
@@ -62,12 +62,13 @@ interface PreferenceBinding {
    fun bind(preference: Preference, metadata: PreferenceMetadata) {
        metadata.apply {
            preference.key = key
            if (icon != 0) {
                preference.setIcon(icon)
            val context = preference.context
            val preferenceIcon = metadata.getPreferenceIcon(context)
            if (preferenceIcon != 0) {
                preference.setIcon(preferenceIcon)
            } else {
                preference.icon = null
            }
            val context = preference.context
            val isPreferenceScreen = preference is PreferenceScreen
            preference.peekExtras()?.clear()
            extras(context)?.let { preference.extras.putAll(it) }