OSDN Git Service

Automatic translation import
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / format / PrefixHighlighter.java
1 /*
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.
10  */
11
12 package com.andrew.apollo.format;
13
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;
19
20 import com.andrew.apollo.utils.PreferenceUtils;
21
22 /**
23  * Highlights the text in a text field.
24  */
25 public class PrefixHighlighter {
26
27     /* Color used when highlighting the prefixes */
28     private final int mPrefixHighlightColor;
29
30     private ForegroundColorSpan mPrefixColorSpan;
31
32     /**
33      * @param prefixHighlightColor The color used to highlight the prefixes.
34      */
35     public PrefixHighlighter(final Context context) {
36         mPrefixHighlightColor = PreferenceUtils.getInstance(context).getDefaultThemeColor(context);
37     }
38
39     /**
40      * Sets the text on the given {@link TextView}, highlighting the word that
41      * matches the given prefix.
42      * 
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
46      */
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) {
49             return;
50         }
51         view.setText(apply(text, prefix));
52     }
53
54     /**
55      * Returns a {@link CharSequence} which highlights the given prefix if found
56      * in the given text.
57      * 
58      * @param text the text to which to apply the highlight
59      * @param prefix the prefix to look for
60      */
61     public CharSequence apply(final CharSequence text, final char[] prefix) {
62         final int mIndex = indexOfWordPrefix(text, prefix);
63         if (mIndex != -1) {
64             if (mPrefixColorSpan == null) {
65                 mPrefixColorSpan = new ForegroundColorSpan(mPrefixHighlightColor);
66             }
67             final SpannableString mResult = new SpannableString(text);
68             mResult.setSpan(mPrefixColorSpan, mIndex, mIndex + prefix.length, 0);
69             return mResult;
70         } else {
71             return text;
72         }
73     }
74
75     /**
76      * Finds the index of the first word that starts with the given prefix. If
77      * not found, returns -1.
78      * 
79      * @param text the text in which to search for the prefix
80      * @param prefix the text to find, in upper case letters
81      */
82     private int indexOfWordPrefix(final CharSequence text, final char[] prefix) {
83         if (TextUtils.isEmpty(text) || prefix == null) {
84             return -1;
85         }
86
87         final int mTextLength = text.length();
88         final int mPrefixLength = prefix.length;
89
90         if (mPrefixLength == 0 || mTextLength < mPrefixLength) {
91             return -1;
92         }
93
94         int i = 0;
95         while (i < mTextLength) {
96             /* Skip non-word characters */
97             while (i < mTextLength && !Character.isLetterOrDigit(text.charAt(i))) {
98                 i++;
99             }
100
101             if (i + mPrefixLength > mTextLength) {
102                 return -1;
103             }
104
105             /* Compare the prefixes */
106             int j;
107             for (j = 0; j < mPrefixLength; j++) {
108                 if (Character.toUpperCase(text.charAt(i + j)) != prefix[j]) {
109                     break;
110                 }
111             }
112             if (j == mPrefixLength) {
113                 return i;
114             }
115
116             /* Skip this word */
117             while (i < mTextLength && Character.isLetterOrDigit(text.charAt(i))) {
118                 i++;
119             }
120         }
121         return -1;
122     }
123
124 }