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

Commit ba3cd9f7 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add a Builder to to creat a dynamic FooterPreference easily"

parents eead4e11 16b49f5c
Loading
Loading
Loading
Loading
+57 −0
Original line number Diff line number Diff line
@@ -16,12 +16,14 @@

package com.android.settingslib.widget;

import android.annotation.StringRes;
import android.content.Context;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.res.TypedArrayUtils;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
@@ -80,4 +82,59 @@ public class FooterPreference extends Preference {
            setKey(KEY_FOOTER);
        }
    }

    /**
     * The builder is convenient to creat a dynamic FooterPreference.
     */
    public static class Builder {
        private Context mContext;
        private String mKey;
        private CharSequence mTitle;

        public Builder(@NonNull Context context) {
            mContext = context;
        }

        /**
         * To set the key value of the {@link FooterPreference}.
         * @param key The key value.
         */
        public Builder setKey(String key) {
            mKey = key;
            return this;
        }

        /**
         * To set the title of the {@link FooterPreference}.
         * @param title The title.
         */
        public Builder setTitle(CharSequence title) {
            mTitle = title;
            return this;
        }

        /**
         * To set the title of the {@link FooterPreference}.
         * @param titleResId The resource id of the title.
         */
        public Builder setTitle(@StringRes int titleResId) {
            mTitle = mContext.getText(titleResId);
            return this;
        }

        /**
         * To generate the {@link FooterPreference}.
         */
        public FooterPreference build() {
            final FooterPreference footerPreference = new FooterPreference(mContext);
            footerPreference.setSelectable(false);
            if (!TextUtils.isEmpty(mKey)) {
                footerPreference.setKey(mKey);
            }
            if (!TextUtils.isEmpty(mTitle)) {
                footerPreference.setTitle(mTitle);
            }
            return footerPreference;
        }
    }
}