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.format;
18 
19 import android.text.SpannableString;
20 import android.text.style.CharacterStyle;
21 import android.text.style.StyleSpan;
22 import android.widget.TextView;
23 
24 /**
25  * Highlights the text in a text field.
26  */
27 public class TextHighlighter {
28     private final String TAG = TextHighlighter.class.getSimpleName();
29     private final static boolean DEBUG = false;
30 
31     private int mTextStyle;
32 
33     private CharacterStyle mTextStyleSpan;
34 
TextHighlighter(int textStyle)35     public TextHighlighter(int textStyle) {
36         mTextStyle = textStyle;
37         mTextStyleSpan = getStyleSpan();
38     }
39 
40     /**
41      * Sets the text on the given text view, highlighting the word that matches the given prefix.
42      *
43      * @param view the view on which to set the text
44      * @param text the string to use as the text
45      * @param prefix the prefix to look for
46      */
setPrefixText(TextView view, String text, String prefix)47     public void setPrefixText(TextView view, String text, String prefix) {
48         view.setText(applyPrefixHighlight(text, prefix));
49     }
50 
getStyleSpan()51     private CharacterStyle getStyleSpan() {
52         return new StyleSpan(mTextStyle);
53     }
54 
55     /**
56      * Applies highlight span to the text.
57      * @param text Text sequence to be highlighted.
58      * @param start Start position of the highlight sequence.
59      * @param end End position of the highlight sequence.
60      */
applyMaskingHighlight(SpannableString text, int start, int end)61     public void applyMaskingHighlight(SpannableString text, int start, int end) {
62         /** Sets text color of the masked locations to be highlighted. */
63         text.setSpan(getStyleSpan(), start, end, 0);
64     }
65 
66     /**
67      * Returns a CharSequence which highlights the given prefix if found in the given text.
68      *
69      * @param text the text to which to apply the highlight
70      * @param prefix the prefix to look for
71      */
applyPrefixHighlight(CharSequence text, String prefix)72     public CharSequence applyPrefixHighlight(CharSequence text, String prefix) {
73         if (prefix == null) {
74             return text;
75         }
76 
77         // Skip non-word characters at the beginning of prefix.
78         int prefixStart = 0;
79         while (prefixStart < prefix.length() &&
80                 !Character.isLetterOrDigit(prefix.charAt(prefixStart))) {
81             prefixStart++;
82         }
83         final String trimmedPrefix = prefix.substring(prefixStart);
84 
85         int index = FormatUtils.indexOfWordPrefix(text, trimmedPrefix);
86         if (index != -1) {
87             final SpannableString result = new SpannableString(text);
88             result.setSpan(mTextStyleSpan, index, index + trimmedPrefix.length(), 0 /* flags */);
89             return result;
90         } else {
91             return text;
92         }
93     }
94 }
95