1 /*
2  * Copyright (C) 2018 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.phone.settings;
18 
19 import android.content.Context;
20 import android.preference.Preference;
21 import android.text.Html;
22 import android.text.method.LinkMovementMethod;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.TextView;
26 
27 import com.android.phone.R;
28 
29 /**
30  * Provides a {@link TextView} inside a preference. Useful for displaying static text which may
31  * contain hyperlinks.
32  */
33 public class TextViewPreference extends Preference {
34 
35     /**
36      * The resource ID of the text to be populated in the {@link TextView} when a resource ID is
37      * used.
38      */
39     private int mTextResourceId = 0;
40 
41     /** The text to be populated in the {@link TextView} when a {@link CharSequence} is used. */
42     private CharSequence mText;
43 
44     /** The {@link TextView} containing the text. */
45     private TextView mTextView;
46 
47     /**
48      * Instantiates the {@link TextViewPreference} instance.
49      *
50      * @param context The Context this is associated with, through which it can access the current
51      *     theme, resources, etc.
52      * @param attrs The attributes of the XML tag that is inflating the preference.
53      * @param defStyleAttr An attribute in the current theme that contains a reference to a style
54      *     resource that supplies default values for the view. Can be 0 to not look for defaults.
55      * @param defStyleRes A resource identifier of a style resource that supplies default values for
56      *     the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to
57      *     not look for defaults.
58      */
TextViewPreference( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)59     public TextViewPreference(
60             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
61         super(context, attrs, defStyleAttr, defStyleRes);
62 
63         setLayoutResource(R.layout.text_view_preference);
64     }
65 
66     /**
67      * Instantiates the {@link TextViewPreference} instance.
68      *
69      * @param context The Context this is associated with, through which it can access the current
70      *     theme, resources, etc.
71      * @param attrs The attributes of the XML tag that is inflating the preference.
72      * @param defStyleAttr An attribute in the current theme that contains a reference to a style
73      *     resource that supplies default values for the view. Can be 0 to not look for defaults.
74      */
TextViewPreference(Context context, AttributeSet attrs, int defStyleAttr)75     public TextViewPreference(Context context, AttributeSet attrs, int defStyleAttr) {
76         this(context, attrs, defStyleAttr, 0);
77     }
78 
79     /**
80      * Instantiates the {@link TextViewPreference} instance.
81      *
82      * @param context The Context this is associated with, through which it can access the current
83      *     theme, resources, etc.
84      * @param attrs The attributes of the XML tag that is inflating the preference.
85      */
TextViewPreference(Context context, AttributeSet attrs)86     public TextViewPreference(Context context, AttributeSet attrs) {
87         this(context, attrs, android.R.attr.preferenceStyle, 0);
88     }
89 
90     /**
91      * Instantiates the {@link TextViewPreference} instance.
92      *
93      * @param context The Context this is associated with, through which it can access the current
94      *     theme, resources, etc.
95      */
TextViewPreference(Context context)96     public TextViewPreference(Context context) {
97         super(context, null);
98 
99         setLayoutResource(R.layout.text_view_preference);
100     }
101 
102     /**
103      * Handles binding the preference.
104      *
105      * @param view The view.
106      */
107     @Override
onBindView(View view)108     protected void onBindView(View view) {
109         super.onBindView(view);
110         mTextView = (TextView) view.findViewById(R.id.text);
111         if (mTextResourceId != 0) {
112             setTitle(mTextResourceId);
113         } else if (mText != null) {
114             setTitle(mText);
115         } else if (getTitleRes() != 0) {
116             setTitle(getTitleRes());
117         }
118     }
119 
120     /**
121      * Sets the preference title from a {@link CharSequence}.
122      *
123      * @param text The text.
124      */
125     @Override
setTitle(CharSequence text)126     public void setTitle(CharSequence text) {
127         mTextResourceId = 0;
128         mText = text;
129         if (mTextView == null) {
130             return;
131         }
132 
133         mTextView.setMovementMethod(LinkMovementMethod.getInstance());
134         mTextView.setText(text);
135     }
136 
137     /**
138      * Sets the preference title from a resource id.
139      *
140      * @param textResId The string resource Id.
141      */
142     @Override
setTitle(int textResId)143     public void setTitle(int textResId) {
144         mTextResourceId = textResId;
145         setTitle(Html.fromHtml(getContext().getString(textResId), Html.FROM_HTML_MODE_COMPACT));
146     }
147 }
148 
149