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

Commit 43c80be5 authored by Chun-Ku Lin's avatar Chun-Ku Lin
Browse files

Define IntDef for button type and size

Bug: 397514562
Test: manual
Flag: EXEMPT low risk
Change-Id: Ib5dc3c929a0454fb08d599a6b1d08651deae4df9
parent 620d3940
Loading
Loading
Loading
Loading
+37 −11
Original line number Original line Diff line number Diff line
@@ -27,6 +27,7 @@ import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout;


import androidx.annotation.GravityInt;
import androidx.annotation.GravityInt;
import androidx.annotation.IntDef;
import androidx.preference.Preference;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import androidx.preference.PreferenceViewHolder;


@@ -34,21 +35,46 @@ import com.android.settingslib.widget.preference.button.R;


import com.google.android.material.button.MaterialButton;
import com.google.android.material.button.MaterialButton;


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
/**
 * A preference handled a button
 * A preference handled a button
 */
 */
public class ButtonPreference extends Preference implements GroupSectionDividerMixin {
public class ButtonPreference extends Preference implements GroupSectionDividerMixin {


    public static final int TYPE_FILLED = 0;
    public static final int TYPE_TONAL = 1;
    public static final int TYPE_OUTLINE = 2;

    @IntDef({TYPE_FILLED, TYPE_TONAL, TYPE_OUTLINE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Type {
    }

    public static final int SIZE_NORMAL = 0;
    public static final int SIZE_LARGE = 1;
    public static final int SIZE_EXTRA_LARGE = 2;

    @IntDef({SIZE_NORMAL, SIZE_LARGE, SIZE_EXTRA_LARGE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Size {
    }

    enum ButtonStyle {
    enum ButtonStyle {
        FILLED_NORMAL(0, 0, R.layout.settingslib_expressive_button_filled),
        FILLED_NORMAL(TYPE_FILLED, SIZE_NORMAL, R.layout.settingslib_expressive_button_filled),
        FILLED_LARGE(0, 1, R.layout.settingslib_expressive_button_filled_large),
        FILLED_LARGE(TYPE_FILLED, SIZE_LARGE, R.layout.settingslib_expressive_button_filled_large),
        FILLED_EXTRA(0, 2, R.layout.settingslib_expressive_button_filled_extra),
        FILLED_EXTRA(TYPE_FILLED, SIZE_EXTRA_LARGE,
        TONAL_NORMAL(1, 0, R.layout.settingslib_expressive_button_tonal),
                R.layout.settingslib_expressive_button_filled_extra),
        TONAL_LARGE(1, 1, R.layout.settingslib_expressive_button_tonal_large),
        TONAL_NORMAL(TYPE_TONAL, SIZE_NORMAL, R.layout.settingslib_expressive_button_tonal),
        TONAL_EXTRA(1, 2, R.layout.settingslib_expressive_button_tonal_extra),
        TONAL_LARGE(TYPE_TONAL, SIZE_LARGE, R.layout.settingslib_expressive_button_tonal_large),
        OUTLINE_NORMAL(2, 0, R.layout.settingslib_expressive_button_outline),
        TONAL_EXTRA(TYPE_TONAL, SIZE_EXTRA_LARGE,
        OUTLINE_LARGE(2, 1, R.layout.settingslib_expressive_button_outline_large),
                R.layout.settingslib_expressive_button_tonal_extra),
        OUTLINE_EXTRA(2, 2, R.layout.settingslib_expressive_button_outline_extra);
        OUTLINE_NORMAL(TYPE_OUTLINE, SIZE_NORMAL, R.layout.settingslib_expressive_button_outline),
        OUTLINE_LARGE(TYPE_OUTLINE, SIZE_LARGE,
                R.layout.settingslib_expressive_button_outline_large),
        OUTLINE_EXTRA(TYPE_OUTLINE, SIZE_EXTRA_LARGE,
                R.layout.settingslib_expressive_button_outline_extra);


        private final int mType;
        private final int mType;
        private final int mSize;
        private final int mSize;
@@ -60,7 +86,7 @@ public class ButtonPreference extends Preference implements GroupSectionDividerM
            this.mLayoutId = layoutId;
            this.mLayoutId = layoutId;
        }
        }


        static int getLayoutId(int type, int size) {
        static int getLayoutId(@Type int type, @Size int size) {
            for (ButtonStyle style : values()) {
            for (ButtonStyle style : values()) {
                if (style.mType == type && style.mSize == size) {
                if (style.mType == type && style.mSize == size) {
                    return style.mLayoutId;
                    return style.mLayoutId;
@@ -266,7 +292,7 @@ public class ButtonPreference extends Preference implements GroupSectionDividerM
     *                 <li>2: extra large</li>
     *                 <li>2: extra large</li>
     *             </ul>
     *             </ul>
     */
     */
    public void setButtonStyle(int type, int size) {
    public void setButtonStyle(@Type int type, @Size int size) {
        int layoutId = ButtonStyle.getLayoutId(type, size);
        int layoutId = ButtonStyle.getLayoutId(type, size);
        setLayoutResource(layoutId);
        setLayoutResource(layoutId);
        notifyChanged();
        notifyChanged();