blob: 38c5c2d8b71d3025dce7510a73b564e11d2f4962 [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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.format;
Christine Chen3efbe592013-07-08 18:05:03 -070018
19import android.text.SpannableString;
Yorke Lee8b7f84a2013-09-12 12:38:41 -070020import android.text.style.CharacterStyle;
Yorke Lee8b7f84a2013-09-12 12:38:41 -070021import android.text.style.StyleSpan;
Christine Chen3efbe592013-07-08 18:05:03 -070022import android.widget.TextView;
23
Christine Chen3efbe592013-07-08 18:05:03 -070024/**
25 * Highlights the text in a text field.
26 */
27public class TextHighlighter {
28 private final String TAG = TextHighlighter.class.getSimpleName();
29 private final static boolean DEBUG = false;
30
Yorke Lee8b7f84a2013-09-12 12:38:41 -070031 private int mTextStyle;
Christine Chen3efbe592013-07-08 18:05:03 -070032
Yorke Lee8b7f84a2013-09-12 12:38:41 -070033 private CharacterStyle mTextStyleSpan;
Christine Chen3efbe592013-07-08 18:05:03 -070034
Yorke Lee8b7f84a2013-09-12 12:38:41 -070035 public TextHighlighter(int textStyle) {
36 mTextStyle = textStyle;
37 mTextStyleSpan = getStyleSpan();
Christine Chen3efbe592013-07-08 18:05:03 -070038 }
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 */
47 public void setPrefixText(TextView view, String text, String prefix) {
48 view.setText(applyPrefixHighlight(text, prefix));
49 }
50
Yorke Lee8b7f84a2013-09-12 12:38:41 -070051 private CharacterStyle getStyleSpan() {
52 return new StyleSpan(mTextStyle);
53 }
54
Christine Chen3efbe592013-07-08 18:05:03 -070055 /**
Christine Chen3efbe592013-07-08 18:05:03 -070056 * Applies highlight span to the text.
57 * @param text Text sequence to be highlighted.
Christine Chenccba9502013-07-12 12:04:54 -070058 * @param start Start position of the highlight sequence.
59 * @param end End position of the highlight sequence.
Christine Chen3efbe592013-07-08 18:05:03 -070060 */
Christine Chenccba9502013-07-12 12:04:54 -070061 public void applyMaskingHighlight(SpannableString text, int start, int end) {
Christine Chen3efbe592013-07-08 18:05:03 -070062 /** Sets text color of the masked locations to be highlighted. */
Yorke Lee8b7f84a2013-09-12 12:38:41 -070063 text.setSpan(getStyleSpan(), start, end, 0);
Christine Chen3efbe592013-07-08 18:05:03 -070064 }
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 */
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) {
Christine Chen3efbe592013-07-08 18:05:03 -070087 final SpannableString result = new SpannableString(text);
Yorke Lee8b7f84a2013-09-12 12:38:41 -070088 result.setSpan(mTextStyleSpan, index, index + trimmedPrefix.length(), 0 /* flags */);
Christine Chen3efbe592013-07-08 18:05:03 -070089 return result;
90 } else {
91 return text;
92 }
93 }
94}