Full text search: preprocessing snippet.

Bug: 2078420
Change-Id: I3332a3218fdf86124287c51fc273cdaa2b570db6
diff --git a/src/com/android/contacts/list/ContactListItemView.java b/src/com/android/contacts/list/ContactListItemView.java
index 7fdedcf..121f90d 100644
--- a/src/com/android/contacts/list/ContactListItemView.java
+++ b/src/com/android/contacts/list/ContactListItemView.java
@@ -866,7 +866,34 @@
         }
 
         String snippet = cursor.getString(summarySnippetColumnIndex);
-        // TODO postprocess snippet
+        if (snippet != null) {
+            int from = 0;
+            int to = snippet.length();
+            int start = snippet.indexOf(DefaultContactListAdapter.SNIPPET_START_MATCH);
+            if (start != -1) {
+                int firstNl = snippet.lastIndexOf('\n', start);
+                if (firstNl != -1) {
+                    from = firstNl + 1;
+                }
+            }
+            int end = snippet.lastIndexOf(DefaultContactListAdapter.SNIPPET_END_MATCH);
+            if (end != -1) {
+                int lastNl = snippet.indexOf('\n', end);
+                if (lastNl != -1) {
+                    to = lastNl;
+                }
+            }
+
+            StringBuilder sb = new StringBuilder();
+            for (int i = from; i < to; i++) {
+                char c = snippet.charAt(i);
+                if (c != DefaultContactListAdapter.SNIPPET_START_MATCH &&
+                        c != DefaultContactListAdapter.SNIPPET_END_MATCH) {
+                    sb.append(c);
+                }
+            }
+            snippet = sb.toString();
+        }
         setSnippet(snippet);
     }
 
diff --git a/src/com/android/contacts/list/DefaultContactListAdapter.java b/src/com/android/contacts/list/DefaultContactListAdapter.java
index 164a2cd..3e8c6f1 100644
--- a/src/com/android/contacts/list/DefaultContactListAdapter.java
+++ b/src/com/android/contacts/list/DefaultContactListAdapter.java
@@ -31,6 +31,7 @@
 import android.provider.ContactsContract.Data;
 import android.provider.ContactsContract.Directory;
 import android.provider.ContactsContract.RawContacts;
+import android.provider.ContactsContract.SearchSnippetColumns;
 import android.text.TextUtils;
 import android.view.View;
 
@@ -42,6 +43,14 @@
  */
 public class DefaultContactListAdapter extends ContactListAdapter {
 
+    public static final char SNIPPET_START_MATCH = '\u0001';
+    public static final char SNIPPET_END_MATCH = '\u0001';
+    public static final String SNIPPET_ELLIPSIS = "\u2026";
+    public static final int SNIPPET_MAX_TOKENS = 5;
+
+    public static final String SNIPPET_ARGS = SNIPPET_START_MATCH + "," + SNIPPET_END_MATCH + ","
+            + SNIPPET_ELLIPSIS + "," + SNIPPET_MAX_TOKENS;
+
     public DefaultContactListAdapter(Context context) {
         super(context);
     }
@@ -71,6 +80,8 @@
                     builder.appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY,
                             String.valueOf(getDirectoryResultLimit()));
                 }
+                builder.appendQueryParameter(SearchSnippetColumns.SNIPPET_ARGS_PARAM_KEY,
+                        SNIPPET_ARGS);
                 applyDataRestriction(builder);
                 loader.setUri(builder.build());
                 loader.setProjection(FILTER_PROJECTION);