blob: 496dcdae28d45d914d3c150d57b6723eb114f9b2 [file] [log] [blame]
Christine Chen3efbe592013-07-08 18:05:03 -07001/*
2 * Copyright (C) 2011 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
17package com.android.contacts.common.format;
18
Yorke Lee8b7f84a2013-09-12 12:38:41 -070019import android.graphics.Typeface;
Christine Chen3efbe592013-07-08 18:05:03 -070020import android.text.SpannableString;
Yorke Lee8b7f84a2013-09-12 12:38:41 -070021import android.text.style.CharacterStyle;
Christine Chen3efbe592013-07-08 18:05:03 -070022import android.text.style.ForegroundColorSpan;
Yorke Lee8b7f84a2013-09-12 12:38:41 -070023import android.text.style.StyleSpan;
Christine Chen3efbe592013-07-08 18:05:03 -070024import android.widget.TextView;
25
26import com.google.common.base.Preconditions;
27
28/**
29 * Highlights the text in a text field.
30 */
31public class TextHighlighter {
32 private final String TAG = TextHighlighter.class.getSimpleName();
33 private final static boolean DEBUG = false;
34
Yorke Lee8b7f84a2013-09-12 12:38:41 -070035 private int mTextStyle;
Christine Chen3efbe592013-07-08 18:05:03 -070036
Yorke Lee8b7f84a2013-09-12 12:38:41 -070037 private CharacterStyle mTextStyleSpan;
Christine Chen3efbe592013-07-08 18:05:03 -070038
Yorke Lee8b7f84a2013-09-12 12:38:41 -070039 public TextHighlighter(int textStyle) {
40 mTextStyle = textStyle;
41 mTextStyleSpan = getStyleSpan();
Christine Chen3efbe592013-07-08 18:05:03 -070042 }
43
44 /**
45 * Sets the text on the given text view, highlighting the word that matches the given prefix.
46 *
47 * @param view the view on which to set the text
48 * @param text the string to use as the text
49 * @param prefix the prefix to look for
50 */
51 public void setPrefixText(TextView view, String text, String prefix) {
52 view.setText(applyPrefixHighlight(text, prefix));
53 }
54
Yorke Lee8b7f84a2013-09-12 12:38:41 -070055 private CharacterStyle getStyleSpan() {
56 return new StyleSpan(mTextStyle);
57 }
58
Christine Chen3efbe592013-07-08 18:05:03 -070059 /**
Christine Chen3efbe592013-07-08 18:05:03 -070060 * Applies highlight span to the text.
61 * @param text Text sequence to be highlighted.
Christine Chenccba9502013-07-12 12:04:54 -070062 * @param start Start position of the highlight sequence.
63 * @param end End position of the highlight sequence.
Christine Chen3efbe592013-07-08 18:05:03 -070064 */
Christine Chenccba9502013-07-12 12:04:54 -070065 public void applyMaskingHighlight(SpannableString text, int start, int end) {
Christine Chen3efbe592013-07-08 18:05:03 -070066 /** Sets text color of the masked locations to be highlighted. */
Yorke Lee8b7f84a2013-09-12 12:38:41 -070067 text.setSpan(getStyleSpan(), start, end, 0);
Christine Chen3efbe592013-07-08 18:05:03 -070068 }
69
70 /**
71 * Returns a CharSequence which highlights the given prefix if found in the given text.
72 *
73 * @param text the text to which to apply the highlight
74 * @param prefix the prefix to look for
75 */
76 public CharSequence applyPrefixHighlight(CharSequence text, String prefix) {
77 if (prefix == null) {
78 return text;
79 }
80
81 // Skip non-word characters at the beginning of prefix.
82 int prefixStart = 0;
83 while (prefixStart < prefix.length() &&
84 !Character.isLetterOrDigit(prefix.charAt(prefixStart))) {
85 prefixStart++;
86 }
87 final String trimmedPrefix = prefix.substring(prefixStart);
88
89 int index = FormatUtils.indexOfWordPrefix(text, trimmedPrefix);
90 if (index != -1) {
Christine Chen3efbe592013-07-08 18:05:03 -070091 final SpannableString result = new SpannableString(text);
Yorke Lee8b7f84a2013-09-12 12:38:41 -070092 result.setSpan(mTextStyleSpan, index, index + trimmedPrefix.length(), 0 /* flags */);
Christine Chen3efbe592013-07-08 18:05:03 -070093 return result;
94 } else {
95 return text;
96 }
97 }
98}