Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 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 | |
Yorke Lee | 8b7f84a | 2013-09-12 12:38:41 -0700 | [diff] [blame] | 19 | import android.graphics.Typeface; |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 20 | import android.text.SpannableString; |
Yorke Lee | 8b7f84a | 2013-09-12 12:38:41 -0700 | [diff] [blame] | 21 | import android.text.style.CharacterStyle; |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 22 | import android.text.style.ForegroundColorSpan; |
Yorke Lee | 8b7f84a | 2013-09-12 12:38:41 -0700 | [diff] [blame] | 23 | import android.text.style.StyleSpan; |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 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 | |
Yorke Lee | 8b7f84a | 2013-09-12 12:38:41 -0700 | [diff] [blame] | 35 | private int mTextStyle; |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 36 | |
Yorke Lee | 8b7f84a | 2013-09-12 12:38:41 -0700 | [diff] [blame] | 37 | private CharacterStyle mTextStyleSpan; |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 38 | |
Yorke Lee | 8b7f84a | 2013-09-12 12:38:41 -0700 | [diff] [blame] | 39 | public TextHighlighter(int textStyle) { |
| 40 | mTextStyle = textStyle; |
| 41 | mTextStyleSpan = getStyleSpan(); |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 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 | */ |
| 51 | public void setPrefixText(TextView view, String text, String prefix) { |
| 52 | view.setText(applyPrefixHighlight(text, prefix)); |
| 53 | } |
| 54 | |
Yorke Lee | 8b7f84a | 2013-09-12 12:38:41 -0700 | [diff] [blame] | 55 | private CharacterStyle getStyleSpan() { |
| 56 | return new StyleSpan(mTextStyle); |
| 57 | } |
| 58 | |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 59 | /** |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 60 | * Applies highlight span to the text. |
| 61 | * @param text Text sequence to be highlighted. |
Christine Chen | ccba950 | 2013-07-12 12:04:54 -0700 | [diff] [blame] | 62 | * @param start Start position of the highlight sequence. |
| 63 | * @param end End position of the highlight sequence. |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 64 | */ |
Christine Chen | ccba950 | 2013-07-12 12:04:54 -0700 | [diff] [blame] | 65 | public void applyMaskingHighlight(SpannableString text, int start, int end) { |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 66 | /** Sets text color of the masked locations to be highlighted. */ |
Yorke Lee | 8b7f84a | 2013-09-12 12:38:41 -0700 | [diff] [blame] | 67 | text.setSpan(getStyleSpan(), start, end, 0); |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 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 | */ |
| 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 Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 91 | final SpannableString result = new SpannableString(text); |
Yorke Lee | 8b7f84a | 2013-09-12 12:38:41 -0700 | [diff] [blame] | 92 | result.setSpan(mTextStyleSpan, index, index + trimmedPrefix.length(), 0 /* flags */); |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 93 | return result; |
| 94 | } else { |
| 95 | return text; |
| 96 | } |
| 97 | } |
| 98 | } |