1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.utils;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.text.Annotation;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.View;
import java.util.Arrays;
import java.util.Comparator;
/**
* This class is used to add {@link View.OnClickListener} for the text been wrapped by
* annotation.
*/
public class AnnotationSpan extends URLSpan {
private final View.OnClickListener mClickListener;
private AnnotationSpan(View.OnClickListener lsn) {
super((String) null);
mClickListener = lsn;
}
@Override
public void onClick(View widget) {
if (mClickListener != null) {
mClickListener.onClick(widget);
}
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(true);
}
public static CharSequence linkify(CharSequence rawText, LinkInfo... linkInfos) {
SpannableString msg = new SpannableString(rawText);
Annotation[] spans = msg.getSpans(0, msg.length(), Annotation.class);
SpannableStringBuilder builder = new SpannableStringBuilder(msg);
for (Annotation annotation : spans) {
final String key = annotation.getValue();
int start = msg.getSpanStart(annotation);
int end = msg.getSpanEnd(annotation);
AnnotationSpan link = null;
for (LinkInfo linkInfo : linkInfos) {
if (linkInfo.mAnnotation.equals(key)) {
link = new AnnotationSpan(linkInfo.mListener);
break;
}
}
if (link != null) {
builder.setSpan(link, start, end, msg.getSpanFlags(link));
}
}
return builder;
}
/**
* get the text part without having text for link part
*/
public static CharSequence textWithoutLink(CharSequence encodedText) {
SpannableString msg = new SpannableString(encodedText);
Annotation[] spans = msg.getSpans(0, msg.length(), Annotation.class);
if (spans == null) {
return encodedText;
}
Arrays.sort(spans, Comparator.comparingInt(span -> -msg.getSpanStart(span)));
StringBuilder msgWithoutLink = new StringBuilder(msg.toString());
for (Annotation span : spans) {
msgWithoutLink.delete(msg.getSpanStart(span), msg.getSpanEnd(span));
}
return msgWithoutLink.toString();
}
/**
* Data class to store the annotation and the click action
*/
public static class LinkInfo {
private static final String TAG = "AnnotationSpan.LinkInfo";
public static final String DEFAULT_ANNOTATION = "link";
private final String mAnnotation;
private final Boolean mActionable;
private final View.OnClickListener mListener;
public LinkInfo(String annotation, View.OnClickListener listener) {
mAnnotation = annotation;
mListener = listener;
mActionable = true; // assume actionable
}
public LinkInfo(Context context, String annotation, Intent intent) {
mAnnotation = annotation;
if (intent != null) {
mActionable = context.getPackageManager()
.resolveActivity(intent, 0 /* flags */) != null;
} else {
mActionable = false;
}
if (!mActionable) {
mListener = null;
} else {
mListener = view -> {
try {
view.startActivityForResult(intent, 0);
} catch (ActivityNotFoundException e) {
Log.w(TAG, "Activity was not found for intent, " + intent);
}
};
}
}
public boolean isActionable() {
return mActionable;
}
}
}
|