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

Commit 9c5107a2 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[SettingsLib] Support customization for CardPreference" into main

parents 3a6a5c9d ef9ee536
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -69,13 +69,11 @@
        </LinearLayout>

        <ImageView
            android:id="@android:id/closeButton"
            android:id="@android:id/icon1"
            android:layout_width="@dimen/settingslib_expressive_space_medium4"
            android:layout_height="@dimen/settingslib_expressive_space_medium4"
            android:padding="@dimen/settingslib_expressive_space_extrasmall4"
            android:layout_gravity="center"
            android:contentDescription="@string/settingslib_dismiss_button_content_description"
            android:src="@drawable/settingslib_expressive_icon_close"
            android:tint="@color/settingslib_materialColorOnSecondary" />

    </LinearLayout>
+37 −15
Original line number Diff line number Diff line
@@ -18,29 +18,51 @@ package com.android.settingslib.widget
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import com.android.settingslib.widget.preference.card.R
import com.android.settingslib.widget.theme.R as ThemeR

/**
 * The CardPreference shows a card like suggestion in homepage, which also support dismiss.
 * The CardPreference shows a card like suggestion in homepage, which also support additional action
 * like dismiss.
 */
class CardPreference @JvmOverloads constructor(
class CardPreference
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
    defStyleRes: Int = 0,
) : Preference(context, attrs, defStyleAttr, defStyleRes), GroupSectionDividerMixin {

    @DrawableRes private var actionIcon: Int = 0
    private var actionIconContentDescription: CharSequence? = null
    private var action: ((CardPreference) -> Unit)? = null

    init {
        layoutResource = R.layout.settingslib_expressive_preference_card
    }
    private var dismissible = false
        set(value) {
            if (field != value) {
                field = value
                notifyChanged()

    fun useDismissAction() =
        setAdditionalAction(
            ThemeR.drawable.settingslib_expressive_icon_close,
            context.getString(ThemeR.string.settingslib_dismiss_button_content_description),
        ) {
            it.isVisible = false
        }

    fun setAdditionalAction(
        @DrawableRes icon: Int,
        contentDescription: CharSequence?,
        action: ((CardPreference) -> Unit)?,
    ) {
        actionIcon = icon
        actionIconContentDescription = contentDescription
        this.action = action
        notifyChanged()
    }

    override fun onBindViewHolder(holder: PreferenceViewHolder) {
@@ -48,11 +70,11 @@ class CardPreference @JvmOverloads constructor(
        holder.isDividerAllowedBelow = false
        holder.isDividerAllowedAbove = false

        holder.findViewById(android.R.id.closeButton)?.let { dismissButton ->
            dismissButton.visibility = if (dismissible) View.VISIBLE else View.GONE
            dismissButton.setOnClickListener {
                isVisible = false
            }
        (holder.findViewById(android.R.id.icon1) as ImageView).apply {
            visibility = if (actionIcon != 0) View.VISIBLE else View.GONE
            setImageResource(actionIcon)
            contentDescription = actionIconContentDescription
            setOnClickListener { action?.invoke(this@CardPreference) }
        }
    }
}