From 16b49f5cb3409986d46b277fdc327c72b7695c6f Mon Sep 17 00:00:00 2001 From: Sunny Shao Date: Fri, 9 Aug 2019 10:55:24 +0800 Subject: [PATCH] Add a Builder to to creat a dynamic FooterPreference easily Fixes: 139163212 Test: compilation Change-Id: I09c455770d1de5b30cfba55fbd9f23c95fef39cd --- .../settingslib/widget/FooterPreference.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/packages/SettingsLib/src/com/android/settingslib/widget/FooterPreference.java b/packages/SettingsLib/src/com/android/settingslib/widget/FooterPreference.java index fcd7ff81ba6fc..e48becf18d7f1 100644 --- a/packages/SettingsLib/src/com/android/settingslib/widget/FooterPreference.java +++ b/packages/SettingsLib/src/com/android/settingslib/widget/FooterPreference.java @@ -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; + } + } }