blob: ccaa9d40832ba91d50464edc9226fa9b820325fc (
plain)
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
|
package org.sufficientlysecure.keychain.ui.util;
import android.content.Context;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import org.sufficientlysecure.keychain.R;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Highlighter {
private Context mContext;
private String mQuery;
public Highlighter(Context context, String query) {
mContext = context;
mQuery = query;
}
public Spannable highlight(String text) {
Spannable highlight = Spannable.Factory.getInstance().newSpannable(text);
if (mQuery == null) {
return highlight;
}
String queryPattern = buildPatternFromQuery(mQuery);
Pattern pattern = Pattern.compile("(" + queryPattern + ")", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
int colorEmphasis = FormattingUtils.getColorFromAttr(mContext, R.attr.colorEmphasis);
while (matcher.find()) {
highlight.setSpan(new ForegroundColorSpan(colorEmphasis),
matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return highlight;
}
private static String buildPatternFromQuery(String mQuery) {
String chunks[] = mQuery.split(" *, *");
boolean firstChunk = true;
StringBuilder patternPiece = new StringBuilder();
for (int i = 0; i < chunks.length; ++i) {
patternPiece.append(Pattern.quote(chunks[i]));
if (firstChunk) {
firstChunk = false;
continue;
}
patternPiece.append('|');
}
return patternPiece.toString();
}
}
|