Show Address Instead of Number in Nearby Places

Change-Id: Ib1a96240e70628ff76e849ea4f6ca359791deebd
diff --git a/src/com/android/contacts/common/GeoUtil.java b/src/com/android/contacts/common/GeoUtil.java
index aaf4715..5ca04b0 100644
--- a/src/com/android/contacts/common/GeoUtil.java
+++ b/src/com/android/contacts/common/GeoUtil.java
@@ -20,6 +20,11 @@
 import android.location.Country;
 import android.location.CountryDetector;
 
+import com.android.i18n.phonenumbers.NumberParseException;
+import com.android.i18n.phonenumbers.PhoneNumberUtil;
+import com.android.i18n.phonenumbers.Phonenumber;
+import com.android.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
+
 import java.util.Locale;
 
 /**
@@ -31,7 +36,7 @@
      * @return The ISO 3166-1 two letters country code of the country the user
      *         is in.
      */
-    public static final String getCurrentCountryIso(Context context) {
+    public static String getCurrentCountryIso(Context context) {
         final CountryDetector detector =
                 (CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR);
         if (detector != null) {
@@ -43,4 +48,19 @@
         // Fallback to Locale if have issues with CountryDetector
         return Locale.getDefault().getCountry();
     }
+
+    public static String getGeocodedLocationFor(Context context,  String phoneNumber) {
+        final PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
+        final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
+        final CountryDetector countryDetector =
+                (CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR);
+        try {
+            final Phonenumber.PhoneNumber structuredPhoneNumber =
+                    phoneNumberUtil.parse(phoneNumber, getCurrentCountryIso(context));
+            final Locale locale = context.getResources().getConfiguration().locale;
+            return geocoder.getDescriptionForNumber(structuredPhoneNumber, locale);
+        } catch (NumberParseException e) {
+            return null;
+        }
+    }
 }
diff --git a/src/com/android/contacts/common/list/DirectoryPartition.java b/src/com/android/contacts/common/list/DirectoryPartition.java
index 1340af4..ca0dc11 100644
--- a/src/com/android/contacts/common/list/DirectoryPartition.java
+++ b/src/com/android/contacts/common/list/DirectoryPartition.java
@@ -38,6 +38,7 @@
     private boolean mPriorityDirectory;
     private boolean mPhotoSupported;
     private int mResultLimit = RESULT_LIMIT_DEFAULT;
+    private boolean mDisplayNumber = true;
 
     private String mLabel;
 
@@ -163,4 +164,16 @@
                 ", mLabel='" + mLabel + '\'' +
                 '}';
     }
+
+    /**
+     * Whether or not to display the phone number in app that have that option - Dialer. If false,
+     * Phone Label should be used instead of Phone Number.
+     */
+    public boolean isDisplayNumber() {
+        return mDisplayNumber;
+    }
+
+    public void setDisplayNumber(boolean displayNumber) {
+        mDisplayNumber = displayNumber;
+    }
 }
diff --git a/src/com/android/contacts/common/list/PhoneNumberListAdapter.java b/src/com/android/contacts/common/list/PhoneNumberListAdapter.java
index 889a091..2af055e 100644
--- a/src/com/android/contacts/common/list/PhoneNumberListAdapter.java
+++ b/src/com/android/contacts/common/list/PhoneNumberListAdapter.java
@@ -33,6 +33,7 @@
 import android.view.View;
 import android.view.ViewGroup;
 
+import com.android.contacts.common.GeoUtil;
 import com.android.contacts.common.R;
 import com.android.contacts.common.extensions.ExtendedPhoneDirectoriesManager;
 import com.android.contacts.common.extensions.ExtensionsFactory;
@@ -259,7 +260,8 @@
     }
 
     public Uri getDataUri(int partitionIndex, Cursor cursor) {
-        final long directoryId = ((DirectoryPartition)getPartition(partitionIndex)).getDirectoryId();
+        final long directoryId =
+                ((DirectoryPartition)getPartition(partitionIndex)).getDirectoryId();
         if (!isRemoteDirectory(directoryId)) {
             final long phoneId = cursor.getLong(PhoneQuery.PHONE_ID);
             return ContentUris.withAppendedId(Data.CONTENT_URI, phoneId);
@@ -352,13 +354,15 @@
 
             view.removePhotoView(true, false);
         }
-        bindPhoneNumber(view, cursor);
+
+        final DirectoryPartition directory = (DirectoryPartition) getPartition(partition);
+        bindPhoneNumber(view, cursor, directory.isDisplayNumber());
         view.setDividerVisible(showBottomDivider);
     }
 
-    protected void bindPhoneNumber(ContactListItemView view, Cursor cursor) {
+    protected void bindPhoneNumber(ContactListItemView view, Cursor cursor, boolean displayNumber) {
         CharSequence label = null;
-        if (!cursor.isNull(PhoneQuery.PHONE_TYPE)) {
+        if (displayNumber &&  !cursor.isNull(PhoneQuery.PHONE_TYPE)) {
             final int type = cursor.getInt(PhoneQuery.PHONE_TYPE);
             final String customLabel = cursor.getString(PhoneQuery.PHONE_LABEL);
 
@@ -366,7 +370,20 @@
             label = Phone.getTypeLabel(getContext().getResources(), type, customLabel);
         }
         view.setLabel(label);
-        view.showPhoneNumber(cursor, PhoneQuery.PHONE_NUMBER);
+        final String text;
+        if (displayNumber) {
+            text = cursor.getString(PhoneQuery.PHONE_NUMBER);
+        } else {
+            // Display phone label. If that's null, display geocoded location for the number
+            final String phoneLabel = cursor.getString(PhoneQuery.PHONE_LABEL);
+            if (phoneLabel != null) {
+                text = phoneLabel;
+            } else {
+                final String phoneNumber = cursor.getString(PhoneQuery.PHONE_NUMBER);
+                text = GeoUtil.getGeocodedLocationFor(mContext, phoneNumber);
+            }
+        }
+        view.setPhoneNumber(text);
     }
 
     protected void bindSectionHeaderAndDivider(final ContactListItemView view, int position) {