2 * Copyright (C) 2011 The Android Open Source Project Licensed under the Apache
3 * License, Version 2.0 (the "License"); you may not use this file except in
4 * compliance with the License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
12 package com.andrew.apollo.format;
14 import android.content.Context;
15 import android.text.SpannableString;
16 import android.text.TextUtils;
17 import android.text.style.ForegroundColorSpan;
18 import android.widget.TextView;
20 import com.andrew.apollo.utils.PreferenceUtils;
23 * Highlights the text in a text field.
25 public class PrefixHighlighter {
27 /* Color used when highlighting the prefixes */
28 private final int mPrefixHighlightColor;
30 private ForegroundColorSpan mPrefixColorSpan;
33 * @param prefixHighlightColor The color used to highlight the prefixes.
35 public PrefixHighlighter(final Context context) {
36 mPrefixHighlightColor = PreferenceUtils.getInstance(context).getDefaultThemeColor(context);
40 * Sets the text on the given {@link TextView}, highlighting the word that
41 * matches the given prefix.
43 * @param view The {@link TextView} on which to set the text
44 * @param text The string to use as the text
45 * @param prefix The prefix to look for
47 public void setText(final TextView view, final String text, final char[] prefix) {
48 if (view == null || TextUtils.isEmpty(text) || prefix == null || prefix.length == 0) {
51 view.setText(apply(text, prefix));
55 * Returns a {@link CharSequence} which highlights the given prefix if found
58 * @param text the text to which to apply the highlight
59 * @param prefix the prefix to look for
61 public CharSequence apply(final CharSequence text, final char[] prefix) {
62 final int mIndex = indexOfWordPrefix(text, prefix);
64 if (mPrefixColorSpan == null) {
65 mPrefixColorSpan = new ForegroundColorSpan(mPrefixHighlightColor);
67 final SpannableString mResult = new SpannableString(text);
68 mResult.setSpan(mPrefixColorSpan, mIndex, mIndex + prefix.length, 0);
76 * Finds the index of the first word that starts with the given prefix. If
77 * not found, returns -1.
79 * @param text the text in which to search for the prefix
80 * @param prefix the text to find, in upper case letters
82 private int indexOfWordPrefix(final CharSequence text, final char[] prefix) {
83 if (TextUtils.isEmpty(text) || prefix == null) {
87 final int mTextLength = text.length();
88 final int mPrefixLength = prefix.length;
90 if (mPrefixLength == 0 || mTextLength < mPrefixLength) {
95 while (i < mTextLength) {
96 /* Skip non-word characters */
97 while (i < mTextLength && !Character.isLetterOrDigit(text.charAt(i))) {
101 if (i + mPrefixLength > mTextLength) {
105 /* Compare the prefixes */
107 for (j = 0; j < mPrefixLength; j++) {
108 if (Character.toUpperCase(text.charAt(i + j)) != prefix[j]) {
112 if (j == mPrefixLength) {
117 while (i < mTextLength && Character.isLetterOrDigit(text.charAt(i))) {