1 /*
2  * Copyright (C) 2020 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.widget;
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.util.AttributeSet;
25 import android.view.View;
26 import android.widget.TextView;
27 
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceViewHolder;
30 
31 /** A preference which supports linkify text in the summary **/
32 public class LinkifySummaryPreference extends Preference {
33 
LinkifySummaryPreference(Context context, AttributeSet attrs)34     public LinkifySummaryPreference(Context context, AttributeSet attrs) {
35         super(context, attrs);
36     }
37 
LinkifySummaryPreference(Context context)38     public LinkifySummaryPreference(Context context) {
39         super(context);
40     }
41 
42     @Override
onBindViewHolder(PreferenceViewHolder holder)43     public void onBindViewHolder(PreferenceViewHolder holder) {
44         super.onBindViewHolder(holder);
45 
46         final TextView summaryView = (TextView) holder.findViewById(android.R.id.summary);
47         if (summaryView == null || summaryView.getVisibility() != View.VISIBLE) {
48             return;
49         }
50 
51         final CharSequence summary = getSummary();
52         if (!TextUtils.isEmpty(summary)) {
53             final SpannableString spannableSummary = new SpannableString(summary);
54             if (spannableSummary.getSpans(0, spannableSummary.length(), ClickableSpan.class)
55                     .length > 0) {
56                 summaryView.setMovementMethod(LinkMovementMethod.getInstance());
57             }
58         }
59     }
60 }
61