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 package com.android.settingslib.widget;
17 
18 import android.content.Context;
19 import android.text.Spanned;
20 import android.text.method.LinkMovementMethod;
21 import android.text.style.ClickableSpan;
22 import android.util.AttributeSet;
23 import android.widget.TextView;
24 
25 import com.android.settingslib.widget.preference.footer.R;
26 
27 /**
28  * Copied from setup wizard. This TextView performed two functions. The first is to make it so the
29  * link behaves properly and becomes clickable. The second was that it made the link visible to
30  * accessibility services, but from O forward support for links is provided natively.
31  */
32 public class LinkTextView extends TextView {
33 
LinkTextView(Context context)34     public LinkTextView(Context context) {
35         this(context, null);
36     }
37 
LinkTextView(Context context, AttributeSet attrs)38     public LinkTextView(Context context, AttributeSet attrs) {
39         super(context, attrs);
40     }
41 
42     @Override
setText(CharSequence text, BufferType type)43     public void setText(CharSequence text, BufferType type) {
44         super.setText(text, type);
45         if (text instanceof Spanned) {
46             final ClickableSpan[] spans =
47                     ((Spanned) text).getSpans(0, text.length(), ClickableSpan.class);
48             if (spans.length > 0) {
49                 setMovementMethod(LinkMovementMethod.getInstance());
50             }
51         }
52     }
53 }
54