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