diff options
| author | Chiao Cheng <chiaocheng@google.com> | 2012-12-21 15:45:54 -0800 |
|---|---|---|
| committer | Chiao Cheng <chiaocheng@google.com> | 2012-12-26 11:17:32 -0800 |
| commit | bd80fd64b9ff94c9ffbdb843beb4b363bb209463 (patch) | |
| tree | 57f62c04aed26e114930c314187a4e129c0914bf | |
| parent | 05897c8e016942e10f0ef6f0da02d339480d4748 (diff) | |
| download | android_packages_apps_ContactsCommon-bd80fd64b9ff94c9ffbdb843beb4b363bb209463.tar.gz android_packages_apps_ContactsCommon-bd80fd64b9ff94c9ffbdb843beb4b363bb209463.tar.bz2 android_packages_apps_ContactsCommon-bd80fd64b9ff94c9ffbdb843beb4b363bb209463.zip | |
Fixing missing punctuation for punctuation search case.
Previous CL I9cbdf10d21c79f53bc621bacb7eeeb95a6a2435f fixed missing
punctuation at the start when searching without punctuation. (e.g.
{hello})
This CL fixes the case where leading punctuation is missing if you searched
with a leading punctuation. (e.g. {'hello}). The content provider
uses a different code path when it detects multi-words and snippeting
is actually done in sqlite using the FTS snippet method. The check for
multi-word was treating {'hello} as two words.
This means that multi-word searches will still have this issue as it still
uses the sqlite snippet method. Leaving this to a separate CL since it's
a riskier change.
Bug: 5929143
Change-Id: I1883621bb64452726cd92035d30001c29b478574
6 files changed, 29 insertions, 18 deletions
diff --git a/src/com/android/contacts/common/format/FormatUtils.java b/src/com/android/contacts/common/format/FormatUtils.java index 6a274de8..376ff13f 100644 --- a/src/com/android/contacts/common/format/FormatUtils.java +++ b/src/com/android/contacts/common/format/FormatUtils.java @@ -138,13 +138,13 @@ public class FormatUtils { * @param text the text in which to search for the prefix * @param prefix the text to find, in upper case letters */ - public static int indexOfWordPrefix(CharSequence text, char[] prefix) { + public static int indexOfWordPrefix(CharSequence text, String prefix) { if (prefix == null || text == null) { return -1; } int textLength = text.length(); - int prefixLength = prefix.length; + int prefixLength = prefix.length(); if (prefixLength == 0 || textLength < prefixLength) { return -1; @@ -164,7 +164,7 @@ public class FormatUtils { // Compare the prefixes int j; for (j = 0; j < prefixLength; j++) { - if (Character.toUpperCase(text.charAt(i + j)) != prefix[j]) { + if (Character.toUpperCase(text.charAt(i + j)) != prefix.charAt(j)) { break; } } diff --git a/src/com/android/contacts/common/format/PrefixHighlighter.java b/src/com/android/contacts/common/format/PrefixHighlighter.java index 65edf587..ce44d651 100644 --- a/src/com/android/contacts/common/format/PrefixHighlighter.java +++ b/src/com/android/contacts/common/format/PrefixHighlighter.java @@ -39,7 +39,7 @@ public class PrefixHighlighter { * @param text the string to use as the text * @param prefix the prefix to look for */ - public void setText(TextView view, String text, char[] prefix) { + public void setText(TextView view, String text, String prefix) { view.setText(apply(text, prefix)); } @@ -49,15 +49,27 @@ public class PrefixHighlighter { * @param text the text to which to apply the highlight * @param prefix the prefix to look for */ - public CharSequence apply(CharSequence text, char[] prefix) { - int index = FormatUtils.indexOfWordPrefix(text, prefix); + 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 + prefix.length, 0 /* flags */); + result.setSpan(mPrefixColorSpan, index, index + trimmedPrefix.length(), 0 /* flags */); return result; } else { return text; diff --git a/src/com/android/contacts/common/list/ContactEntryListAdapter.java b/src/com/android/contacts/common/list/ContactEntryListAdapter.java index 22a17a74..9ebf9a2b 100644 --- a/src/com/android/contacts/common/list/ContactEntryListAdapter.java +++ b/src/com/android/contacts/common/list/ContactEntryListAdapter.java @@ -71,7 +71,7 @@ public abstract class ContactEntryListAdapter extends IndexerListAdapter { private ContactPhotoManager mPhotoLoader; private String mQueryString; - private char[] mUpperCaseQueryString; + private String mUpperCaseQueryString; private boolean mSearchMode; private int mDirectorySearchMode; private int mDirectoryResultLimit = Integer.MAX_VALUE; @@ -225,11 +225,11 @@ public abstract class ContactEntryListAdapter extends IndexerListAdapter { if (TextUtils.isEmpty(queryString)) { mUpperCaseQueryString = null; } else { - mUpperCaseQueryString = queryString.toUpperCase().toCharArray(); + mUpperCaseQueryString = queryString.toUpperCase(); } } - public char[] getUpperCaseQueryString() { + public String getUpperCaseQueryString() { return mUpperCaseQueryString; } diff --git a/src/com/android/contacts/common/list/ContactListItemView.java b/src/com/android/contacts/common/list/ContactListItemView.java index 5c2943f9..8354fd9d 100644 --- a/src/com/android/contacts/common/list/ContactListItemView.java +++ b/src/com/android/contacts/common/list/ContactListItemView.java @@ -139,7 +139,7 @@ public class ContactListItemView extends ViewGroup private ColorStateList mSecondaryTextColor; - private char[] mHighlightedPrefix; + private String mHighlightedPrefix; private int mDefaultPhotoViewSize = 0; /** @@ -813,7 +813,7 @@ public class ContactListItemView extends ViewGroup * <p> * NOTE: must be all upper-case */ - public void setHighlightedPrefix(char[] upperCasePrefix) { + public void setHighlightedPrefix(String upperCasePrefix) { mHighlightedPrefix = upperCasePrefix; } @@ -1176,7 +1176,6 @@ public class ContactListItemView extends ViewGroup } } } else { - if (snippet != null) { int from = 0; int to = snippet.length(); diff --git a/tests/src/com/android/contacts/common/format/FormatUtilsTests.java b/tests/src/com/android/contacts/common/format/FormatUtilsTests.java index b38019dc..8f4f7728 100644 --- a/tests/src/com/android/contacts/common/format/FormatUtilsTests.java +++ b/tests/src/com/android/contacts/common/format/FormatUtilsTests.java @@ -76,7 +76,7 @@ public class FormatUtilsTests extends AndroidTestCase { } public void testIndexOfWordPrefix_NullText() { - assertEquals(-1, FormatUtils.indexOfWordPrefix(null, "TE".toCharArray())); + assertEquals(-1, FormatUtils.indexOfWordPrefix(null, "TE")); } public void testIndexOfWordPrefix_MatchingPrefix() { @@ -109,6 +109,6 @@ public class FormatUtilsTests extends AndroidTestCase { * @param expectedIndex the expected value to be returned by the function */ private void checkIndexOfWordPrefix(String text, String wordPrefix, int expectedIndex) { - assertEquals(expectedIndex, FormatUtils.indexOfWordPrefix(text, wordPrefix.toCharArray())); + assertEquals(expectedIndex, FormatUtils.indexOfWordPrefix(text, wordPrefix)); } } diff --git a/tests/src/com/android/contacts/common/list/ContactListItemViewTest.java b/tests/src/com/android/contacts/common/list/ContactListItemViewTest.java index 6eb74db3..a4524609 100644 --- a/tests/src/com/android/contacts/common/list/ContactListItemViewTest.java +++ b/tests/src/com/android/contacts/common/list/ContactListItemViewTest.java @@ -76,7 +76,7 @@ public class ContactListItemViewTest extends AndroidTestCase { Cursor cursor = createCursor("John Doe", "Doe John"); ContactListItemView view = createView(); - view.setHighlightedPrefix("DOE".toCharArray()); + view.setHighlightedPrefix("DOE"); view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY); CharSequence seq = view.getNameTextView().getText(); @@ -88,7 +88,7 @@ public class ContactListItemViewTest extends AndroidTestCase { Cursor cursor = createCursor("John Doe", "Doe John"); ContactListItemView view = createView(); - view.setHighlightedPrefix("DOE".toCharArray()); + view.setHighlightedPrefix("DOE"); view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_ALTERNATIVE); CharSequence seq = view.getNameTextView().getText(); @@ -98,7 +98,7 @@ public class ContactListItemViewTest extends AndroidTestCase { public void testSetSnippet_Prefix() { ContactListItemView view = createView(); - view.setHighlightedPrefix("TEST".toCharArray()); + view.setHighlightedPrefix("TEST"); view.setSnippet("This is a test"); CharSequence seq = view.getSnippetView().getText(); |
