1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settingslib.widget; 18 19 import android.annotation.StringRes; 20 import android.content.Context; 21 import android.text.TextUtils; 22 import android.text.method.LinkMovementMethod; 23 import android.util.AttributeSet; 24 import android.widget.TextView; 25 26 import androidx.annotation.NonNull; 27 import androidx.core.content.res.TypedArrayUtils; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceViewHolder; 30 31 import com.android.settingslib.R; 32 33 /** 34 * A custom preference acting as "footer" of a page. It has a field for icon and text. It is added 35 * to screen as the last preference. 36 */ 37 public class FooterPreference extends Preference { 38 39 static final int ORDER_FOOTER = Integer.MAX_VALUE - 1; 40 public static final String KEY_FOOTER = "footer_preference"; 41 FooterPreference(Context context, AttributeSet attrs)42 public FooterPreference(Context context, AttributeSet attrs) { 43 super(context, attrs, TypedArrayUtils.getAttr( 44 context, R.attr.footerPreferenceStyle, android.R.attr.preferenceStyle)); 45 init(); 46 } 47 FooterPreference(Context context)48 public FooterPreference(Context context) { 49 this(context, null); 50 } 51 52 @Override onBindViewHolder(PreferenceViewHolder holder)53 public void onBindViewHolder(PreferenceViewHolder holder) { 54 super.onBindViewHolder(holder); 55 TextView title = holder.itemView.findViewById(android.R.id.title); 56 title.setMovementMethod(new LinkMovementMethod()); 57 title.setClickable(false); 58 title.setLongClickable(false); 59 } 60 61 @Override setSummary(CharSequence summary)62 public void setSummary(CharSequence summary) { 63 setTitle(summary); 64 } 65 66 @Override setSummary(int summaryResId)67 public void setSummary(int summaryResId) { 68 setTitle(summaryResId); 69 } 70 71 @Override getSummary()72 public CharSequence getSummary() { 73 return getTitle(); 74 } 75 init()76 private void init() { 77 if (getIcon() == null) { 78 setIcon(R.drawable.ic_info_outline_24); 79 } 80 setOrder(ORDER_FOOTER); 81 if (TextUtils.isEmpty(getKey())) { 82 setKey(KEY_FOOTER); 83 } 84 } 85 86 /** 87 * The builder is convenient to creat a dynamic FooterPreference. 88 */ 89 public static class Builder { 90 private Context mContext; 91 private String mKey; 92 private CharSequence mTitle; 93 Builder(@onNull Context context)94 public Builder(@NonNull Context context) { 95 mContext = context; 96 } 97 98 /** 99 * To set the key value of the {@link FooterPreference}. 100 * @param key The key value. 101 */ setKey(@onNull String key)102 public Builder setKey(@NonNull String key) { 103 mKey = key; 104 return this; 105 } 106 107 /** 108 * To set the title of the {@link FooterPreference}. 109 * @param title The title. 110 */ setTitle(CharSequence title)111 public Builder setTitle(CharSequence title) { 112 mTitle = title; 113 return this; 114 } 115 116 /** 117 * To set the title of the {@link FooterPreference}. 118 * @param titleResId The resource id of the title. 119 */ setTitle(@tringRes int titleResId)120 public Builder setTitle(@StringRes int titleResId) { 121 mTitle = mContext.getText(titleResId); 122 return this; 123 } 124 125 /** 126 * To generate the {@link FooterPreference}. 127 */ build()128 public FooterPreference build() { 129 final FooterPreference footerPreference = new FooterPreference(mContext); 130 footerPreference.setSelectable(false); 131 if (TextUtils.isEmpty(mTitle)) { 132 throw new IllegalArgumentException("Footer title cannot be empty!"); 133 } 134 footerPreference.setTitle(mTitle); 135 if (!TextUtils.isEmpty(mKey)) { 136 footerPreference.setKey(mKey); 137 } 138 return footerPreference; 139 } 140 } 141 } 142