• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settingslib.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, R.attr.footerPreferenceStyle, android.R.attr.preferenceStyle));
53     }
54 
LinkablePreference(Context ctx)55     public LinkablePreference(Context ctx) {
56         this(ctx, null);
57     }
58 
59     @Override
onBindViewHolder(PreferenceViewHolder view)60     public void onBindViewHolder(PreferenceViewHolder view) {
61         super.onBindViewHolder(view);
62 
63         TextView textView = (TextView) view.findViewById(android.R.id.title);
64         if (textView == null) {
65             return;
66         }
67         textView.setSingleLine(false);
68 
69         if (mContentTitle == null || mClickListener == null) {
70             return;
71         }
72 
73         StringBuilder contentBuilder = new StringBuilder().append(mContentTitle);
74         if (mContentDescription != null) {
75             contentBuilder.append("\n\n");
76             contentBuilder.append(mContentDescription);
77         }
78 
79         boolean linked = LinkifyUtils.linkify(textView, contentBuilder, mClickListener);
80         if (linked && mContentTitle != null) {
81             Spannable spannableContent = (Spannable) textView.getText();
82             spannableContent.setSpan(
83                     new TextAppearanceSpan(getContext(), android.R.style.TextAppearance_Small),
84                     0,
85                     mContentTitle.length(),
86                     Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
87             textView.setText(spannableContent);
88             textView.setMovementMethod(new LinkMovementMethod());
89         }
90     }
91 
92     /**
93      * Sets the linkable text for the Preference title.
94      *
95      * @param contentTitle text to set the Preference title.
96      * @param contentDescription description text to append underneath title, can be null.
97      * @param clickListener OnClickListener for the link portion of the text.
98      */
setText( CharSequence contentTitle, @Nullable CharSequence contentDescription, LinkifyUtils.OnClickListener clickListener)99     public void setText(
100             CharSequence contentTitle,
101             @Nullable CharSequence contentDescription,
102             LinkifyUtils.OnClickListener clickListener) {
103         mContentTitle = contentTitle;
104         mContentDescription = contentDescription;
105         mClickListener = clickListener;
106         // sets the title so that the title TextView is not hidden in super.onBindViewHolder()
107         super.setTitle(contentTitle);
108     }
109 
110     /**
111      * Sets the title of the LinkablePreference. resets linkable text for reusability.
112      */
113     @Override
setTitle(int titleResId)114     public void setTitle(int titleResId) {
115         mContentTitle = null;
116         mContentDescription = null;
117         super.setTitle(titleResId);
118     }
119 
120     /**
121      * Sets the title of the LinkablePreference. resets linkable text for reusability.
122      */
123     @Override
setTitle(CharSequence title)124     public void setTitle(CharSequence title) {
125         mContentTitle = null;
126         mContentDescription = null;
127         super.setTitle(title);
128     }
129 }
130