Use i18n phone number lib when comparing two phone numbers

PhoneNumberUtils#compare() returns false positive result.

Bug: 2586019
Change-Id: Ia4ebd9adb36eca15f879dd3ec45870fa0c555f55
diff --git a/src/com/android/contacts/ContactsUtils.java b/src/com/android/contacts/ContactsUtils.java
index 0e75a7f..76cbc7d 100644
--- a/src/com/android/contacts/ContactsUtils.java
+++ b/src/com/android/contacts/ContactsUtils.java
@@ -16,6 +16,11 @@
 
 package com.android.contacts;
 
+import com.google.i18n.phonenumbers.NumberParseException;
+import com.google.i18n.phonenumbers.PhoneNumberUtil;
+import com.google.i18n.phonenumbers.PhoneNumberUtil.MatchType;
+import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
+
 import android.content.Context;
 import android.content.Intent;
 import android.location.CountryDetector;
@@ -114,9 +119,19 @@
             if (dataParts1.length != dataParts2.length) {
                 return false;
             }
+            PhoneNumberUtil util = PhoneNumberUtil.getInstance();
             for (int i = 0; i < dataParts1.length; i++) {
-                if (!PhoneNumberUtils.compare(context, dataParts1[i], dataParts2[i])) {
-                    return false;
+                try {
+                    PhoneNumber phoneNumber1 = util.parse(dataParts1[i], "ZZ" /* Unknown */);
+                    PhoneNumber phoneNumber2 = util.parse(dataParts2[i], "ZZ" /* Unknown */);
+                    MatchType matchType = util.isNumberMatch(phoneNumber1, phoneNumber2);
+                    if (matchType != MatchType.SHORT_NSN_MATCH) {
+                        return false;
+                    }
+                } catch (NumberParseException e) {
+                    if (!TextUtils.equals(dataParts1[i], dataParts2[i])) {
+                        return false;
+                    }
                 }
             }
 
diff --git a/src/com/android/contacts/interactions/PhoneNumberInteraction.java b/src/com/android/contacts/interactions/PhoneNumberInteraction.java
index 277fa56..4448e28 100644
--- a/src/com/android/contacts/interactions/PhoneNumberInteraction.java
+++ b/src/com/android/contacts/interactions/PhoneNumberInteraction.java
@@ -16,6 +16,11 @@
 package com.android.contacts.interactions;
 
 
+import com.google.i18n.phonenumbers.NumberParseException;
+import com.google.i18n.phonenumbers.PhoneNumberUtil;
+import com.google.i18n.phonenumbers.PhoneNumberUtil.MatchType;
+import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
+
 import com.android.contacts.Collapser;
 import com.android.contacts.Collapser.Collapsible;
 import com.android.contacts.ContactSaveService;
@@ -46,7 +51,7 @@
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.Data;
 import android.provider.ContactsContract.RawContacts;
-import android.telephony.PhoneNumberUtils;
+import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -112,8 +117,16 @@
         }
 
         public boolean shouldCollapseWith(PhoneItem phoneItem) {
-            if (PhoneNumberUtils.compareStrictly(phoneNumber, phoneItem.phoneNumber)) {
-                return true;
+            try {
+                PhoneNumberUtil util = PhoneNumberUtil.getInstance();
+                PhoneNumber phoneNumber1 = util.parse(phoneNumber, "ZZ" /* Unknown */);
+                PhoneNumber phoneNumber2 = util.parse(phoneItem.phoneNumber, "ZZ" /* Unknown */);
+                MatchType matchType = util.isNumberMatch(phoneNumber1, phoneNumber2);
+                if (matchType == MatchType.SHORT_NSN_MATCH) {
+                    return true;
+                }
+            } catch (NumberParseException e) {
+                return TextUtils.equals(phoneNumber, phoneItem.phoneNumber);
             }
             return false;
         }
diff --git a/tests/src/com/android/contacts/interactions/PhoneNumberInteractionTest.java b/tests/src/com/android/contacts/interactions/PhoneNumberInteractionTest.java
index 43e844e..c401a8c 100644
--- a/tests/src/com/android/contacts/interactions/PhoneNumberInteractionTest.java
+++ b/tests/src/com/android/contacts/interactions/PhoneNumberInteractionTest.java
@@ -124,6 +124,26 @@
         assertEquals("sms:456", interaction.startedIntent.getDataString());
     }
 
+    public void testShouldCollapseWith() {
+        PhoneNumberInteraction.PhoneItem phoneItem1 = new PhoneNumberInteraction.PhoneItem();
+        PhoneNumberInteraction.PhoneItem phoneItem2 = new PhoneNumberInteraction.PhoneItem();
+
+        phoneItem1.phoneNumber = "123";
+        phoneItem2.phoneNumber = "123";
+
+        assertTrue(phoneItem1.shouldCollapseWith(phoneItem2));
+
+        phoneItem1.phoneNumber = "123";
+        phoneItem2.phoneNumber = "456";
+
+        assertFalse(phoneItem1.shouldCollapseWith(phoneItem2));
+
+        phoneItem1.phoneNumber = "123#,123";
+        phoneItem2.phoneNumber = "123#,456";
+
+        assertFalse(phoneItem1.shouldCollapseWith(phoneItem2));
+    }
+
     public void testCallNumberWhenThereAreDuplicates() {
         Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
         expectQuery(contactUri)