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