summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristine Chen <christinech@google.com>2013-07-08 18:05:03 -0700
committerChristine Chen <christinech@google.com>2013-07-10 11:32:10 -0700
commita81953a2b1818066ef5e44817f374ac288bab343 (patch)
tree9c2723a0cf793531d72c8efbbeed7f5426fca58b
parent0f290ef69262b8a5032fe287e9ef2c5ac8c53985 (diff)
downloadandroid_packages_apps_ContactsCommon-a81953a2b1818066ef5e44817f374ac288bab343.tar.gz
android_packages_apps_ContactsCommon-a81953a2b1818066ef5e44817f374ac288bab343.tar.bz2
android_packages_apps_ContactsCommon-a81953a2b1818066ef5e44817f374ac288bab343.zip
Changes to support smart dial in search.
- Extends functionalities in text highligher. - Changes pemissions in parent classes. Change-Id: Ib5717fe00760b5a88c747e1ff8fda598d987fa98
-rw-r--r--src/com/android/contacts/common/format/PrefixHighlighter.java78
-rw-r--r--src/com/android/contacts/common/format/SpannedTestUtils.java7
-rw-r--r--src/com/android/contacts/common/format/TextHighlighter.java124
-rw-r--r--src/com/android/contacts/common/list/ContactEntryListFragment.java4
-rw-r--r--src/com/android/contacts/common/list/ContactListItemView.java97
-rw-r--r--src/com/android/contacts/common/list/PhoneNumberListAdapter.java12
-rw-r--r--tests/src/com/android/contacts/common/format/PrefixHighligherTest.java84
-rw-r--r--tests/src/com/android/contacts/common/format/TextHighlighterTest.java110
8 files changed, 333 insertions, 183 deletions
diff --git a/src/com/android/contacts/common/format/PrefixHighlighter.java b/src/com/android/contacts/common/format/PrefixHighlighter.java
deleted file mode 100644
index ce44d651..00000000
--- a/src/com/android/contacts/common/format/PrefixHighlighter.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.contacts.common.format;
-
-import android.text.SpannableString;
-import android.text.style.ForegroundColorSpan;
-import android.widget.TextView;
-
-/**
- * Highlights the text in a text field.
- */
-public class PrefixHighlighter {
- private final int mPrefixHighlightColor;
-
- private ForegroundColorSpan mPrefixColorSpan;
-
- public PrefixHighlighter(int prefixHighlightColor) {
- mPrefixHighlightColor = prefixHighlightColor;
- }
-
- /**
- * Sets the text on the given text view, highlighting the word that matches the given prefix.
- *
- * @param view the view on which to set the text
- * @param text the string to use as the text
- * @param prefix the prefix to look for
- */
- public void setText(TextView view, String text, String prefix) {
- view.setText(apply(text, prefix));
- }
-
- /**
- * Returns a CharSequence which highlights the given prefix if found in the given text.
- *
- * @param text the text to which to apply the highlight
- * @param prefix the prefix to look for
- */
- public CharSequence apply(CharSequence text, String prefix) {
- if (prefix == null) {
- return text;
- }
-
- // Skip non-word characters at the beginning of prefix.
- int prefixStart = 0;
- while (prefixStart < prefix.length() &&
- !Character.isLetterOrDigit(prefix.charAt(prefixStart))) {
- prefixStart++;
- }
- final String trimmedPrefix = prefix.substring(prefixStart);
-
- int index = FormatUtils.indexOfWordPrefix(text, trimmedPrefix);
- if (index != -1) {
- if (mPrefixColorSpan == null) {
- mPrefixColorSpan = new ForegroundColorSpan(mPrefixHighlightColor);
- }
-
- SpannableString result = new SpannableString(text);
- result.setSpan(mPrefixColorSpan, index, index + trimmedPrefix.length(), 0 /* flags */);
- return result;
- } else {
- return text;
- }
- }
-}
diff --git a/src/com/android/contacts/common/format/SpannedTestUtils.java b/src/com/android/contacts/common/format/SpannedTestUtils.java
index 8c2a22d0..d385c921 100644
--- a/src/com/android/contacts/common/format/SpannedTestUtils.java
+++ b/src/com/android/contacts/common/format/SpannedTestUtils.java
@@ -80,4 +80,11 @@ public class SpannedTestUtils {
Assert.assertFalse(seq instanceof Spanned);
Assert.assertEquals(expected, seq);
}
+
+ public static int getNextTransition(CharSequence seq, int start) {
+ Assert.assertTrue(seq instanceof Spanned);
+ final Spanned spannable = (Spanned) seq;
+
+ return spannable.nextSpanTransition(start, seq.length(), ForegroundColorSpan.class);
+ }
}
diff --git a/src/com/android/contacts/common/format/TextHighlighter.java b/src/com/android/contacts/common/format/TextHighlighter.java
new file mode 100644
index 00000000..e0eb28c8
--- /dev/null
+++ b/src/com/android/contacts/common/format/TextHighlighter.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.common.format;
+
+import android.text.SpannableString;
+import android.text.style.ForegroundColorSpan;
+import android.util.Log;
+import android.widget.TextView;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * Highlights the text in a text field.
+ */
+public class TextHighlighter {
+ private final String TAG = TextHighlighter.class.getSimpleName();
+ private final static boolean DEBUG = false;
+
+ private final int mTextHighlightColor;
+
+ private ForegroundColorSpan mTextColorSpan;
+
+ public TextHighlighter(int textHighlightColor) {
+ mTextHighlightColor = textHighlightColor;
+ }
+
+ /**
+ * Sets the text on the given text view, highlighting the word that matches the given prefix.
+ *
+ * @param view the view on which to set the text
+ * @param text the string to use as the text
+ * @param prefix the prefix to look for
+ */
+ public void setPrefixText(TextView view, String text, String prefix) {
+ view.setText(applyPrefixHighlight(text, prefix));
+ }
+
+ /**
+ * Sets a mask for text highlighting. The mask should be a string of the same length as text,
+ * where each character is either 0 or 1. If the character is 1, the letter in text at the same
+ * position should be highlighted. Otherwise the letter should not be highlighted.
+ *
+ * @param view TextView where the highlighted text should go.
+ * @param text Text to be highlighted.
+ * @param mask Mask indicating which letter to highlight.
+ */
+ public void setMaskingText(TextView view, String text, String mask) {
+ view.setText(applyMaskingHighlight(text, mask));
+ }
+
+ /**
+ * Applies highlight span to the text.
+ * @param text Text sequence to be highlighted.
+ * @param mask Mask indicating where highlight should be.
+ * @return Highlighted text sequence.
+ */
+ public CharSequence applyMaskingHighlight(CharSequence text, String mask) {
+ Preconditions.checkNotNull(text);
+ Preconditions.checkNotNull(mask);
+
+ if (text.length() != mask.length() || text.length() == 0) {
+ if (DEBUG) {
+ Log.v(TAG, "Mask size mismatch or text length is 0" + text + " " + mask);
+ }
+ return text;
+ }
+
+ /** Sets text color of the masked locations to be highlighted. */
+ final SpannableString result = new SpannableString(text);
+ for (int i = 0; i < mask.length(); ++i) {
+ if (mask.charAt(i) == '1') {
+ result.setSpan(new ForegroundColorSpan(mTextHighlightColor), i, i + 1, 0);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Returns a CharSequence which highlights the given prefix if found in the given text.
+ *
+ * @param text the text to which to apply the highlight
+ * @param prefix the prefix to look for
+ */
+ public CharSequence applyPrefixHighlight(CharSequence text, String prefix) {
+ if (prefix == null) {
+ return text;
+ }
+
+ // Skip non-word characters at the beginning of prefix.
+ int prefixStart = 0;
+ while (prefixStart < prefix.length() &&
+ !Character.isLetterOrDigit(prefix.charAt(prefixStart))) {
+ prefixStart++;
+ }
+ final String trimmedPrefix = prefix.substring(prefixStart);
+
+ int index = FormatUtils.indexOfWordPrefix(text, trimmedPrefix);
+ if (index != -1) {
+ if (mTextColorSpan == null) {
+ mTextColorSpan = new ForegroundColorSpan(mTextHighlightColor);
+ }
+
+ final SpannableString result = new SpannableString(text);
+ result.setSpan(mTextColorSpan, index, index + trimmedPrefix.length(), 0 /* flags */);
+ return result;
+ } else {
+ return text;
+ }
+ }
+}
diff --git a/src/com/android/contacts/common/list/ContactEntryListFragment.java b/src/com/android/contacts/common/list/ContactEntryListFragment.java
index a8066b81..4b01b620 100644
--- a/src/com/android/contacts/common/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/common/list/ContactEntryListFragment.java
@@ -626,6 +626,10 @@ public abstract class ContactEntryListFragment<T extends ContactEntryListAdapter
}
}
+ public int getDirectoryLoaderId() {
+ return DIRECTORY_LOADER_ID;
+ }
+
public int getDirectorySearchMode() {
return mDirectorySearchMode;
}
diff --git a/src/com/android/contacts/common/list/ContactListItemView.java b/src/com/android/contacts/common/list/ContactListItemView.java
index c65a766d..85d6bf85 100644
--- a/src/com/android/contacts/common/list/ContactListItemView.java
+++ b/src/com/android/contacts/common/list/ContactListItemView.java
@@ -47,7 +47,7 @@ import android.widget.TextView;
import com.android.contacts.common.ContactPresenceIconUtil;
import com.android.contacts.common.ContactStatusUtil;
import com.android.contacts.common.R;
-import com.android.contacts.common.format.PrefixHighlighter;
+import com.android.contacts.common.format.TextHighlighter;
import com.android.contacts.common.util.SearchUtil;
import com.google.common.collect.Lists;
@@ -110,6 +110,13 @@ public class ContactListItemView extends ViewGroup
private Drawable mHorizontalDividerDrawable;
private int mHorizontalDividerHeight;
+ // Highlighting Masks for the name and number.
+ private String mNameHighlightMask;
+ private String mNumberHighlightMask;
+
+ // Highlighting prefix for names.
+ private String mHighlightedPrefix;
+
/**
* Where to put contact photo. This affects the other Views' layout or look-and-feel.
*
@@ -155,7 +162,7 @@ public class ContactListItemView extends ViewGroup
private ColorStateList mSecondaryTextColor;
- private String mHighlightedPrefix;
+
private int mDefaultPhotoViewSize = 0;
/**
@@ -210,14 +217,14 @@ public class ContactListItemView extends ViewGroup
private Rect mBoundsWithoutHeader = new Rect();
/** A helper used to highlight a prefix in a text field. */
- private PrefixHighlighter mPrefixHighlighter;
+ private final TextHighlighter mTextHighlighter;
private CharSequence mUnknownNameText;
public ContactListItemView(Context context) {
super(context);
mContext = context;
- mPrefixHighlighter = new PrefixHighlighter(Color.GREEN);
+ mTextHighlighter = new TextHighlighter(Color.GREEN);
}
public ContactListItemView(Context context, AttributeSet attrs) {
@@ -286,7 +293,7 @@ public class ContactListItemView extends ViewGroup
final int prefixHighlightColor = a.getColor(
R.styleable.ContactListItemView_list_item_prefix_highlight_color, Color.GREEN);
- mPrefixHighlighter = new PrefixHighlighter(prefixHighlightColor);
+ mTextHighlighter = new TextHighlighter(prefixHighlightColor);
a.recycle();
a = getContext().obtainStyledAttributes(android.R.styleable.Theme);
@@ -853,12 +860,32 @@ public class ContactListItemView extends ViewGroup
/**
* Sets a word prefix that will be highlighted if encountered in fields like
- * name and search snippet.
+ * name and search snippet. This will disable the mask highlighting for names.
* <p>
* NOTE: must be all upper-case
*/
public void setHighlightedPrefix(String upperCasePrefix) {
mHighlightedPrefix = upperCasePrefix;
+ mNameHighlightMask = null;
+ }
+
+ /**
+ * Sets a highlighting mask for names. This will disable the prefix highlighting.
+ *
+ * @param highlightMask A string of 0 and 1's to indicate which letter to highlight
+ */
+ public void setHighlightMask(String highlightMask) {
+ mNameHighlightMask = highlightMask;
+ mHighlightedPrefix = null;
+ }
+
+ /**
+ * Sets a highlighting mask for numbers.
+ *
+ * @param highlightMask A string of 0 and 1's to indicate which digit to highlight.
+ */
+ public void setNumberHighlightMask(String highlightMask) {
+ mNumberHighlightMask = highlightMask;
}
/**
@@ -954,7 +981,7 @@ public class ContactListItemView extends ViewGroup
/**
* Adds or updates a text view for the data element.
*/
- public void setData(char[] text, int size, int dataColumnIndex) {
+ public void setData(char[] text, int size) {
if (text == null || size == 0) {
if (mDataView != null) {
mDataView.setVisibility(View.GONE);
@@ -963,14 +990,32 @@ public class ContactListItemView extends ViewGroup
getDataView();
setMarqueeText(mDataView, text, size);
mDataView.setVisibility(VISIBLE);
- // Check if this is a phone number. This code works also for the legacy phone number
- // coming from LegacyPhoneNumberListAdapter.PHONE_NUMBER_COLUMN_INDEX because they are
- // the exact same constant value (3)
- if (dataColumnIndex == PhoneNumberListAdapter.PhoneQuery.PHONE_NUMBER) {
- // We have a phone number as "mDataView" so make it always LTR and VIEW_START
- mDataView.setTextDirection(View.TEXT_DIRECTION_LTR);
- mDataView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
+ }
+ }
+
+ /**
+ * Sets phone number for a list item. This takes care of number highlighting if the highlight
+ * mask exists.
+ */
+ public void setPhoneNumber(String text) {
+ if (text == null) {
+ if (mDataView != null) {
+ mDataView.setVisibility(View.GONE);
}
+ } else {
+ getDataView();
+ // Sets phone number texts for display after highlighting it, if applicable.
+ CharSequence textToSet = text;
+ if (mNumberHighlightMask != null) {
+ textToSet = mTextHighlighter.applyMaskingHighlight(text,
+ mNumberHighlightMask);
+ }
+ setMarqueeText(mDataView, textToSet);
+ mDataView.setVisibility(VISIBLE);
+
+ // We have a phone number as "mDataView" so make it always LTR and VIEW_START
+ mDataView.setTextDirection(View.TEXT_DIRECTION_LTR);
+ mDataView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
}
}
@@ -1020,7 +1065,12 @@ public class ContactListItemView extends ViewGroup
mSnippetView.setVisibility(View.GONE);
}
} else {
- mPrefixHighlighter.setText(getSnippetView(), text, mHighlightedPrefix);
+ // Chooses a highlighting method for text highlighting.
+ if (mHighlightedPrefix != null) {
+ mTextHighlighter.setPrefixText(getSnippetView(), text, mHighlightedPrefix);
+ } else if (mNameHighlightMask != null) {
+ mTextHighlighter.setMaskingText(getSnippetView(), text, mNameHighlightMask);
+ }
mSnippetView.setVisibility(VISIBLE);
}
}
@@ -1132,7 +1182,12 @@ public class ContactListItemView extends ViewGroup
public void showDisplayName(Cursor cursor, int nameColumnIndex, int displayOrder) {
CharSequence name = cursor.getString(nameColumnIndex);
if (!TextUtils.isEmpty(name)) {
- name = mPrefixHighlighter.apply(name, mHighlightedPrefix);
+ // Chooses the available highlighting method for highlighting.
+ if (mHighlightedPrefix != null) {
+ name = mTextHighlighter.applyPrefixHighlight(name, mHighlightedPrefix);
+ } else if (mNameHighlightMask != null) {
+ name = mTextHighlighter.applyMaskingHighlight(name, mNameHighlightMask);
+ }
} else {
name = mUnknownNameText;
}
@@ -1389,7 +1444,15 @@ public class ContactListItemView extends ViewGroup
*/
public void showData(Cursor cursor, int dataColumnIndex) {
cursor.copyStringToBuffer(dataColumnIndex, mDataBuffer);
- setData(mDataBuffer.data, mDataBuffer.sizeCopied, dataColumnIndex);
+ // Check if this is a phone number. This code works also for the legacy phone number
+ // coming from LegacyPhoneNumberListAdapter.PHONE_NUMBER_COLUMN_INDEX because they are
+ // the exact same constant value (3)
+ if (dataColumnIndex != PhoneNumberListAdapter.PhoneQuery.PHONE_NUMBER) {
+ setData(mDataBuffer.data, mDataBuffer.sizeCopied);
+ } else {
+ // If the data is phone number, highlights the number and aligns text before showing.
+ setPhoneNumber(cursor.getString(dataColumnIndex));
+ }
}
public void setActivatedStateSupported(boolean flag) {
diff --git a/src/com/android/contacts/common/list/PhoneNumberListAdapter.java b/src/com/android/contacts/common/list/PhoneNumberListAdapter.java
index c62b6b1f..aa96e29d 100644
--- a/src/com/android/contacts/common/list/PhoneNumberListAdapter.java
+++ b/src/com/android/contacts/common/list/PhoneNumberListAdapter.java
@@ -50,8 +50,8 @@ import java.util.List;
public class PhoneNumberListAdapter extends ContactEntryListAdapter {
private static final String TAG = PhoneNumberListAdapter.class.getSimpleName();
- protected static class PhoneQuery {
- private static final String[] PROJECTION_PRIMARY = new String[] {
+ public static class PhoneQuery {
+ public static final String[] PROJECTION_PRIMARY = new String[] {
Phone._ID, // 0
Phone.TYPE, // 1
Phone.LABEL, // 2
@@ -62,7 +62,7 @@ public class PhoneNumberListAdapter extends ContactEntryListAdapter {
Phone.DISPLAY_NAME_PRIMARY, // 7
};
- private static final String[] PROJECTION_ALTERNATIVE = new String[] {
+ public static final String[] PROJECTION_ALTERNATIVE = new String[] {
Phone._ID, // 0
Phone.TYPE, // 1
Phone.LABEL, // 2
@@ -217,11 +217,15 @@ public class PhoneNumberListAdapter extends ContactEntryListAdapter {
return view;
}
+ protected void setHighlight(ContactListItemView view, Cursor cursor) {
+ view.setHighlightedPrefix(isSearchMode() ? getUpperCaseQueryString() : null);
+ }
+
@Override
protected void bindView(View itemView, int partition, Cursor cursor, int position) {
ContactListItemView view = (ContactListItemView)itemView;
- view.setHighlightedPrefix(isSearchMode() ? getUpperCaseQueryString() : null);
+ setHighlight(view, cursor);
// Look at elements before and after this position, checking if contact IDs are same.
// If they have one same contact ID, it means they can be grouped.
diff --git a/tests/src/com/android/contacts/common/format/PrefixHighligherTest.java b/tests/src/com/android/contacts/common/format/PrefixHighligherTest.java
deleted file mode 100644
index d3bacdb9..00000000
--- a/tests/src/com/android/contacts/common/format/PrefixHighligherTest.java
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.contacts.common.format;
-
-import android.test.suitebuilder.annotation.SmallTest;
-
-import junit.framework.TestCase;
-
-/**
- * Unit tests for {@link com.android.contacts.common.format.PrefixHighlighter}.
- */
-@SmallTest
-public class PrefixHighligherTest extends TestCase {
- private static final int TEST_PREFIX_HIGHLIGHT_COLOR = 0xFF0000;
-
- /** The object under test. */
- private PrefixHighlighter mPrefixHighlighter;
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- mPrefixHighlighter = new PrefixHighlighter(TEST_PREFIX_HIGHLIGHT_COLOR);
- }
-
- public void testApply_EmptyPrefix() {
- CharSequence seq = mPrefixHighlighter.apply("", "");
- SpannedTestUtils.assertNotSpanned(seq, "");
-
- seq = mPrefixHighlighter.apply("test", "");
- SpannedTestUtils.assertNotSpanned(seq, "test");
- }
-
- public void testSetText_MatchingPrefix() {
- final String prefix = "TE";
-
- CharSequence seq = mPrefixHighlighter.apply("test", prefix);
- SpannedTestUtils.assertPrefixSpan(seq, 0, 1);
-
- seq = mPrefixHighlighter.apply("Test", prefix);
- SpannedTestUtils.assertPrefixSpan(seq, 0, 1);
-
- seq = mPrefixHighlighter.apply("TEst", prefix);
- SpannedTestUtils.assertPrefixSpan(seq, 0, 1);
-
- seq = mPrefixHighlighter.apply("a test", prefix);
- SpannedTestUtils.assertPrefixSpan(seq, 2, 3);
- }
-
- public void testSetText_NotMatchingPrefix() {
- final CharSequence seq = mPrefixHighlighter.apply("test", "TA");
- SpannedTestUtils.assertNotSpanned(seq, "test");
- }
-
- public void testSetText_FirstMatch() {
- final CharSequence seq = mPrefixHighlighter.apply("a test's tests are not tests", "TE");
- SpannedTestUtils.assertPrefixSpan(seq, 2, 3);
- }
-
- public void testSetText_NoMatchingMiddleOfWord() {
- final String prefix = "TE";
- CharSequence seq = mPrefixHighlighter.apply("atest", prefix);
- SpannedTestUtils.assertNotSpanned(seq, "atest");
-
- seq = mPrefixHighlighter.apply("atest otest", prefix);
- SpannedTestUtils.assertNotSpanned(seq, "atest otest");
-
- seq = mPrefixHighlighter.apply("atest test", prefix);
- SpannedTestUtils.assertPrefixSpan(seq, 6, 7);
- }
-}
diff --git a/tests/src/com/android/contacts/common/format/TextHighlighterTest.java b/tests/src/com/android/contacts/common/format/TextHighlighterTest.java
new file mode 100644
index 00000000..2009c317
--- /dev/null
+++ b/tests/src/com/android/contacts/common/format/TextHighlighterTest.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.common.format;
+
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.contacts.common.format.SpannedTestUtils;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for {@link TextHighlighter}.
+ */
+@SmallTest
+public class TextHighlighterTest extends TestCase {
+ private static final int TEST_PREFIX_HIGHLIGHT_COLOR = 0xFF0000;
+
+ /** The object under test. */
+ private TextHighlighter mTextHighlighter;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mTextHighlighter = new TextHighlighter(TEST_PREFIX_HIGHLIGHT_COLOR);
+ }
+
+ public void testApply_EmptyPrefix() {
+ CharSequence seq = mTextHighlighter.applyPrefixHighlight("", "");
+ SpannedTestUtils.assertNotSpanned(seq, "");
+
+ seq = mTextHighlighter.applyPrefixHighlight("test", "");
+ SpannedTestUtils.assertNotSpanned(seq, "test");
+ }
+
+ public void testSetText_MatchingPrefix() {
+ final String prefix = "TE";
+
+ CharSequence seq = mTextHighlighter.applyPrefixHighlight("test", prefix);
+ SpannedTestUtils.assertPrefixSpan(seq, 0, 1);
+
+ seq = mTextHighlighter.applyPrefixHighlight("Test", prefix);
+ SpannedTestUtils.assertPrefixSpan(seq, 0, 1);
+
+ seq = mTextHighlighter.applyPrefixHighlight("TEst", prefix);
+ SpannedTestUtils.assertPrefixSpan(seq, 0, 1);
+
+ seq = mTextHighlighter.applyPrefixHighlight("a test", prefix);
+ SpannedTestUtils.assertPrefixSpan(seq, 2, 3);
+ }
+
+ public void testSetText_NotMatchingPrefix() {
+ final CharSequence seq = mTextHighlighter.applyPrefixHighlight("test", "TA");
+ SpannedTestUtils.assertNotSpanned(seq, "test");
+ }
+
+ public void testSetText_FirstMatch() {
+ final CharSequence seq = mTextHighlighter.applyPrefixHighlight(
+ "a test's tests are not tests", "TE");
+ SpannedTestUtils.assertPrefixSpan(seq, 2, 3);
+ }
+
+ public void testSetText_NoMatchingMiddleOfWord() {
+ final String prefix = "TE";
+ CharSequence seq = mTextHighlighter.applyPrefixHighlight("atest", prefix);
+ SpannedTestUtils.assertNotSpanned(seq, "atest");
+
+ seq = mTextHighlighter.applyPrefixHighlight("atest otest", prefix);
+ SpannedTestUtils.assertNotSpanned(seq, "atest otest");
+
+ seq = mTextHighlighter.applyPrefixHighlight("atest test", prefix);
+ SpannedTestUtils.assertPrefixSpan(seq, 6, 7);
+ }
+
+ public void testSetMask_LengthMismatch() {
+ final String mask = "001101";
+ CharSequence seq = mTextHighlighter.applyMaskingHighlight("atest", mask);
+ SpannedTestUtils.assertNotSpanned(seq, "atest");
+
+ seq = mTextHighlighter.applyMaskingHighlight("alongtest", mask);
+ SpannedTestUtils.assertNotSpanned(seq, "alongtest");
+
+ seq = mTextHighlighter.applyMaskingHighlight("", mask);
+ SpannedTestUtils.assertNotSpanned(seq, "");
+ }
+
+ public void testSetMask_Highlight() {
+ final String mask = "001101011";
+ CharSequence seq = mTextHighlighter.applyMaskingHighlight("alongtest", mask);
+ assertEquals(SpannedTestUtils.getNextTransition(seq, 0), 2);
+ assertEquals(SpannedTestUtils.getNextTransition(seq, 2), 4);
+ assertEquals(SpannedTestUtils.getNextTransition(seq, 4), 5);
+ assertEquals(SpannedTestUtils.getNextTransition(seq, 5), 6);
+ assertEquals(SpannedTestUtils.getNextTransition(seq, 6), 7);
+ assertEquals(SpannedTestUtils.getNextTransition(seq, 7), 9);
+ }
+}