Messaging: Don't show numbers of contacts multiple times

* Somehow there are multiple entries per some numbers, sometimes stored
  with, sometimes without spaces and/or invisible characters
* This leads to the same number being shown multiple times
* Don't add those entries by comparing number and type of all
  already added entries with the staging one

Change-Id: Ic815393d3d4f4ba56107f42c9d8940309564554c
diff --git a/src/com/android/messaging/datamodel/FrequentContactsCursorBuilder.java b/src/com/android/messaging/datamodel/FrequentContactsCursorBuilder.java
index 8f65156..fad4040 100644
--- a/src/com/android/messaging/datamodel/FrequentContactsCursorBuilder.java
+++ b/src/com/android/messaging/datamodel/FrequentContactsCursorBuilder.java
@@ -74,7 +74,7 @@
      * are both ready to be consumed.
      * @return the frequent contact cursor if built successfully, or null if it can't be built yet.
      */
-    public Cursor build() {
+    public Cursor build(boolean getAllContacts) {
         if (mFrequentContactsCursor != null && mAllContactsCursor != null) {
             Assert.isTrue(!mFrequentContactsCursor.isClosed());
             Assert.isTrue(!mAllContactsCursor.isClosed());
@@ -108,7 +108,7 @@
             mAllContactsCursor.moveToPosition(-1);
             while (mAllContactsCursor.moveToNext()) {
                 final String lookupKey = mAllContactsCursor.getString(ContactUtil.INDEX_LOOKUP_KEY);
-                if (lookupKeyToRankMap.containsKey(lookupKey)) {
+                if (lookupKeyToRankMap.containsKey(lookupKey) || getAllContacts) {
                     final Object[] row = new Object[ContactUtil.PhoneQuery.PROJECTION.length];
                     row[ContactUtil.INDEX_DATA_ID] =
                             mAllContactsCursor.getLong(ContactUtil.INDEX_DATA_ID);
@@ -121,16 +121,38 @@
                     row[ContactUtil.INDEX_PHOTO_URI] =
                             mAllContactsCursor.getString(ContactUtil.INDEX_PHOTO_URI);
                     row[ContactUtil.INDEX_PHONE_EMAIL] =
-                            mAllContactsCursor.getString(ContactUtil.INDEX_PHONE_EMAIL);
+                            mAllContactsCursor.getString(ContactUtil.INDEX_PHONE_EMAIL)
+                                    .replaceAll("[^\\d+]", "");
                     row[ContactUtil.INDEX_PHONE_EMAIL_TYPE] =
                             mAllContactsCursor.getInt(ContactUtil.INDEX_PHONE_EMAIL_TYPE);
                     row[ContactUtil.INDEX_PHONE_EMAIL_LABEL] =
                             mAllContactsCursor.getString(ContactUtil.INDEX_PHONE_EMAIL_LABEL);
-                    rows.add(row);
+
+                    boolean numberAlreadyAdded = false;
+                    for (Object[] oldRow : rows) {
+                        final int idxType = ContactUtil.INDEX_PHONE_EMAIL_TYPE;
+                        final int idxPhone = ContactUtil.INDEX_PHONE_EMAIL;
+                        if (oldRow[idxType] == row[idxType] &&
+                                oldRow[idxPhone].toString().equals(row[idxPhone].toString())) {
+                            numberAlreadyAdded = true;
+                            break;
+                        }
+                    }
+                    if (!numberAlreadyAdded) {
+                        rows.add(row);
+                    }
                 }
             }
             mAllContactsCursor.moveToPosition(oldPosition);
 
+            // We can return all rows at this point, no sorting or further filtering needed
+            if (getAllContacts) {
+                for (final Object[] row : rows) {
+                    retCursor.addRow(row);
+                }
+                return retCursor;
+            }
+
             // Now we have a list of rows containing frequent contacts in alphabetical order.
             // Therefore, sort all the rows according to their actual ranks in the frequents list.
             Collections.sort(rows, new Comparator<Object[]>() {
diff --git a/src/com/android/messaging/datamodel/data/ContactPickerData.java b/src/com/android/messaging/datamodel/data/ContactPickerData.java
index fd6fca0..bf057db 100644
--- a/src/com/android/messaging/datamodel/data/ContactPickerData.java
+++ b/src/com/android/messaging/datamodel/data/ContactPickerData.java
@@ -96,7 +96,6 @@
         if (isBound(cursorLoader.getBindingId())) {
             switch (loader.getId()) {
                 case ALL_CONTACTS_LOADER:
-                    mListener.onAllContactsCursorUpdated(data);
                     mFrequentContactsCursorBuilder.setAllContacts(data);
                     break;
                 case FREQUENT_CONTACTS_LOADER:
@@ -115,10 +114,14 @@
                 // all contacts and frequent contacts loader, and we don't know which will finish
                 // first. Therefore, try to build the cursor and notify the listener if it's
                 // successfully built.
-                final Cursor frequentContactsCursor = mFrequentContactsCursorBuilder.build();
+                final Cursor frequentContactsCursor = mFrequentContactsCursorBuilder.build(false);
                 if (frequentContactsCursor != null) {
                     mListener.onFrequentContactsCursorUpdated(frequentContactsCursor);
+
+                    final Cursor allContactsCursor = mFrequentContactsCursorBuilder.build(true);
+                    mListener.onAllContactsCursorUpdated(allContactsCursor);
                 }
+
             }
         } else {
             LogUtil.w(LogUtil.BUGLE_TAG, "Loader finished after unbinding the contacts list");