Fix collapsing to do fuzzy phone number matching.

-Also use Collapser to collapse the numbers shown in the context menu
from the A-Z list.

Fixes http://b/issue?id=2047514 and http://b/issue?id=2144616

Change-Id: Ice18ecc306c2f30fd1525418bc9f7408c4435a50
diff --git a/src/com/android/contacts/Collapser.java b/src/com/android/contacts/Collapser.java
index db1da1f..3872dfd 100644
--- a/src/com/android/contacts/Collapser.java
+++ b/src/com/android/contacts/Collapser.java
@@ -39,38 +39,42 @@
      */
     public interface Collapsible<T> {
         public boolean collapseWith(T t);
-        public String getCollapseKey();
+        public boolean shouldCollapseWith(T t);
     }
 
     /**
      * Collapses a list of Collapsible items into a list of collapsed items. Items are collapsed
-     * if they produce equal collapseKeys {@Link Collapsible#getCollapseKey()}, and are collapsed
-     * through the {@Link Collapsible#doCollapseWith(Object)} function implemented by the data item.
+     * if {@link Collapsible#shouldCollapseWith(Object) return strue, and are collapsed
+     * through the {@Link Collapsible#collapseWith(Object)} function implemented by the data item.
      *
      * @param list ArrayList of Objects of type <T extends Collapsible<T>> to be collapsed.
      */
     public static <T extends Collapsible<T>> void collapseList(ArrayList<T> list) {
-        HashMap<String, T> collapseMap = new HashMap<String, T>();
-        ArrayList<String> collapseKeys = new ArrayList<String>();
 
         int listSize = list.size();
-        for (int j = 0; j < listSize; j++) {
-            T entry = list.get(j);
-            String collapseKey = entry.getCollapseKey();
-            if (!collapseMap.containsKey(collapseKey)) {
-                collapseMap.put(collapseKey, entry);
-                collapseKeys.add(collapseKey);
-            } else {
-                collapseMap.get(collapseKey).collapseWith(entry);
+
+        for (int i = 0; i < listSize; i++) {
+            T iItem = list.get(i);
+            if (iItem != null) {
+                for (int j = i + 1; j < listSize; j++) {
+                    T jItem = list.get(j);
+                    if (jItem != null) {
+                        if (iItem.shouldCollapseWith(jItem)) {
+                            iItem.collapseWith(jItem);
+                            list.set(j, null);
+                        }
+                    }
+                }
             }
         }
 
-        if (collapseKeys.size() < listSize) {
-            list.clear();
-            Iterator<String> itr = collapseKeys.iterator();
-            while (itr.hasNext()) {
-                list.add(collapseMap.get(itr.next()));
+        // Remove the null items
+        Iterator<T> itr = list.iterator();
+        while (itr.hasNext()) {
+            if (itr.next() == null) {
+                itr.remove();
             }
         }
+
     }
 }