1 /*
2  * Copyright (C) 2021 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.calling;
18 
19 import android.content.Context;
20 import android.text.SpannableString;
21 import android.text.TextUtils;
22 import android.text.method.LinkMovementMethod;
23 import android.text.style.ClickableSpan;
24 import android.text.util.Linkify;
25 import android.util.AttributeSet;
26 import android.view.View;
27 import android.widget.TextView;
28 
29 import androidx.core.text.util.LinkifyCompat;
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceViewHolder;
32 
33 import com.android.settings.R;
34 
35 /** A preference which supports linkify text as a description in the summary **/
36 public class LinkifyDescriptionPreference extends Preference {
37 
LinkifyDescriptionPreference(Context context)38     public LinkifyDescriptionPreference(Context context) {
39         this(context, null);
40     }
41 
LinkifyDescriptionPreference(Context context, AttributeSet attrs)42     public LinkifyDescriptionPreference(Context context, AttributeSet attrs) {
43       super(context, attrs);
44     }
45 
46     @Override
onBindViewHolder(PreferenceViewHolder holder)47     public void onBindViewHolder(PreferenceViewHolder holder) {
48         super.onBindViewHolder(holder);
49 
50         final TextView summaryView = (TextView) holder.findViewById(android.R.id.summary);
51         if (summaryView == null || summaryView.getVisibility() != View.VISIBLE) {
52             return;
53         }
54 
55         final CharSequence summary = getSummary();
56         if (TextUtils.isEmpty(summary)) {
57             return;
58         }
59 
60         summaryView.setMaxLines(Integer.MAX_VALUE);
61 
62         final SpannableString spannableSummary = new SpannableString(summary);
63         if (spannableSummary.getSpans(0, spannableSummary.length(), ClickableSpan.class)
64                 .length > 0) {
65             summaryView.setMovementMethod(LinkMovementMethod.getInstance());
66         }
67         LinkifyCompat.addLinks(summaryView,
68                 Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS);
69     }
70 }
71