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
diff --git a/src/com/android/contacts/common/format/FormatUtils.java b/src/com/android/contacts/common/format/FormatUtils.java
index 6a274de..376ff13 100644
--- a/src/com/android/contacts/common/format/FormatUtils.java
+++ b/src/com/android/contacts/common/format/FormatUtils.java
@@ -138,13 +138,13 @@
      * @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 @@
             // 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 65edf58..ce44d65 100644
--- a/src/com/android/contacts/common/format/PrefixHighlighter.java
+++ b/src/com/android/contacts/common/format/PrefixHighlighter.java
@@ -39,7 +39,7 @@
      * @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 @@
      * @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 22a17a7..9ebf9a2 100644
--- a/src/com/android/contacts/common/list/ContactEntryListAdapter.java
+++ b/src/com/android/contacts/common/list/ContactEntryListAdapter.java
@@ -71,7 +71,7 @@
     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 @@
         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 5c2943f..8354fd9 100644
--- a/src/com/android/contacts/common/list/ContactListItemView.java
+++ b/src/com/android/contacts/common/list/ContactListItemView.java
@@ -139,7 +139,7 @@
 
     private ColorStateList mSecondaryTextColor;
 
-    private char[] mHighlightedPrefix;
+    private String mHighlightedPrefix;
 
     private int mDefaultPhotoViewSize = 0;
     /**
@@ -813,7 +813,7 @@
      * <p>
      * NOTE: must be all upper-case
      */
-    public void setHighlightedPrefix(char[] upperCasePrefix) {
+    public void setHighlightedPrefix(String upperCasePrefix) {
         mHighlightedPrefix = upperCasePrefix;
     }
 
@@ -1176,7 +1176,6 @@
                 }
             }
         } 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 b38019d..8f4f772 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 void testIndexOfWordPrefix_NullText() {
-        assertEquals(-1, FormatUtils.indexOfWordPrefix(null, "TE".toCharArray()));
+        assertEquals(-1, FormatUtils.indexOfWordPrefix(null, "TE"));
     }
 
     public void testIndexOfWordPrefix_MatchingPrefix() {
@@ -109,6 +109,6 @@
      * @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 6eb74db..a452460 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 @@
         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 @@
         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 void testSetSnippet_Prefix() {
         ContactListItemView view = createView();
-        view.setHighlightedPrefix("TEST".toCharArray());
+        view.setHighlightedPrefix("TEST");
         view.setSnippet("This is a test");
 
         CharSequence seq = view.getSnippetView().getText();