1 /* 2 * Copyright (C) 2017 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.settings.wifi; 18 19 import android.content.Context; 20 import android.text.Spannable; 21 import android.text.method.LinkMovementMethod; 22 import android.text.style.TextAppearanceSpan; 23 import android.util.AttributeSet; 24 import android.widget.TextView; 25 26 import androidx.annotation.Nullable; 27 import androidx.core.content.res.TypedArrayUtils; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceViewHolder; 30 31 import com.android.settings.LinkifyUtils; 32 import com.android.settings.R; 33 34 /** 35 * A preference with a title that can have linkable content on click. 36 */ 37 public class LinkablePreference extends Preference { 38 39 private LinkifyUtils.OnClickListener mClickListener; 40 private CharSequence mContentTitle; 41 private CharSequence mContentDescription; 42 43 LinkablePreference(Context ctx, AttributeSet attrs, int defStyle)44 public LinkablePreference(Context ctx, AttributeSet attrs, int defStyle) { 45 super(ctx, attrs, defStyle); 46 setIcon(R.drawable.ic_info_outline_24dp); 47 setSelectable(false); 48 } 49 LinkablePreference(Context ctx, AttributeSet attrs)50 public LinkablePreference(Context ctx, AttributeSet attrs) { 51 this(ctx, attrs, TypedArrayUtils.getAttr( 52 ctx, com.android.settingslib.widget.theme.R.attr.footerPreferenceStyle, 53 android.R.attr.preferenceStyle)); 54 } 55 LinkablePreference(Context ctx)56 public LinkablePreference(Context ctx) { 57 this(ctx, null); 58 } 59 60 @Override onBindViewHolder(PreferenceViewHolder view)61 public void onBindViewHolder(PreferenceViewHolder view) { 62 super.onBindViewHolder(view); 63 64 TextView textView = (TextView) view.findViewById(android.R.id.title); 65 if (textView == null) { 66 return; 67 } 68 textView.setSingleLine(false); 69 70 if (mContentTitle == null || mClickListener == null) { 71 return; 72 } 73 74 StringBuilder contentBuilder = new StringBuilder().append(mContentTitle); 75 if (mContentDescription != null) { 76 contentBuilder.append("\n\n"); 77 contentBuilder.append(mContentDescription); 78 } 79 80 boolean linked = LinkifyUtils.linkify(textView, contentBuilder, mClickListener); 81 if (linked && mContentTitle != null) { 82 Spannable spannableContent = (Spannable) textView.getText(); 83 spannableContent.setSpan( 84 new TextAppearanceSpan(getContext(), android.R.style.TextAppearance_Small), 85 0, 86 mContentTitle.length(), 87 Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 88 textView.setText(spannableContent); 89 textView.setMovementMethod(new LinkMovementMethod()); 90 } 91 } 92 93 /** 94 * Sets the linkable text for the Preference title. 95 * 96 * @param contentTitle text to set the Preference title. 97 * @param contentDescription description text to append underneath title, can be null. 98 * @param clickListener OnClickListener for the link portion of the text. 99 */ setText( CharSequence contentTitle, @Nullable CharSequence contentDescription, LinkifyUtils.OnClickListener clickListener)100 public void setText( 101 CharSequence contentTitle, 102 @Nullable CharSequence contentDescription, 103 LinkifyUtils.OnClickListener clickListener) { 104 mContentTitle = contentTitle; 105 mContentDescription = contentDescription; 106 mClickListener = clickListener; 107 // sets the title so that the title TextView is not hidden in super.onBindViewHolder() 108 super.setTitle(contentTitle); 109 } 110 111 /** 112 * Sets the title of the LinkablePreference. resets linkable text for reusability. 113 */ 114 @Override setTitle(int titleResId)115 public void setTitle(int titleResId) { 116 mContentTitle = null; 117 mContentDescription = null; 118 super.setTitle(titleResId); 119 } 120 121 /** 122 * Sets the title of the LinkablePreference. resets linkable text for reusability. 123 */ 124 @Override setTitle(CharSequence title)125 public void setTitle(CharSequence title) { 126 mContentTitle = null; 127 mContentDescription = null; 128 super.setTitle(title); 129 } 130 } 131