1 /*
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 
17 package com.android.contacts.common.format;
18 
19 import android.graphics.Typeface;
20 import android.text.SpannableString;
21 import android.text.style.CharacterStyle;
22 import android.text.style.ForegroundColorSpan;
23 import android.text.style.StyleSpan;
24 import android.widget.TextView;
25 
26 import com.google.common.base.Preconditions;
27 
28 /**
29  * Highlights the text in a text field.
30  */
31 public class TextHighlighter {
32     private final String TAG = TextHighlighter.class.getSimpleName();
33     private final static boolean DEBUG = false;
34 
35     private int mTextStyle;
36 
37     private CharacterStyle mTextStyleSpan;
38 
TextHighlighter(int textStyle)39     public TextHighlighter(int textStyle) {
40         mTextStyle = textStyle;
41         mTextStyleSpan = getStyleSpan();
42     }
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      */
setPrefixText(TextView view, String text, String prefix)51     public void setPrefixText(TextView view, String text, String prefix) {
52         view.setText(applyPrefixHighlight(text, prefix));
53     }
54 
getStyleSpan()55     private CharacterStyle getStyleSpan() {
56         return new StyleSpan(mTextStyle);
57     }
58 
59     /**
60      * Applies highlight span to the text.
61      * @param text Text sequence to be highlighted.
62      * @param start Start position of the highlight sequence.
63      * @param end End position of the highlight sequence.
64      */
applyMaskingHighlight(SpannableString text, int start, int end)65     public void applyMaskingHighlight(SpannableString text, int start, int end) {
66         /** Sets text color of the masked locations to be highlighted. */
67         text.setSpan(getStyleSpan(), start, end, 0);
68     }
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      */
applyPrefixHighlight(CharSequence text, String prefix)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) {
91             final SpannableString result = new SpannableString(text);
92             result.setSpan(mTextStyleSpan, index, index + trimmedPrefix.length(), 0 /* flags */);
93             return result;
94         } else {
95             return text;
96         }
97     }
98 }
99