Restore the phone disambiguation dialog

As discussed:
1) If a contact has more than one phone number and no default set, don't
show the phone number type. Instead, just center the contact name.
2) Clicking on a contact with more than one phone number and no default will
launch the disambiguation dialog.
3) If a contact has a default phone number set or only has one phone number,
show the phone number type.
4) Add phone number type to the top 3 contacts, if one is present.

Change-Id: I6a60fc6111e8f9494eca5e35d7cfbaf40aa99e57
diff --git a/res/layout/phone_favorite_regular_row_view.xml b/res/layout/phone_favorite_regular_row_view.xml
index b3c37d8..2e4dde2 100644
--- a/res/layout/phone_favorite_regular_row_view.xml
+++ b/res/layout/phone_favorite_regular_row_view.xml
@@ -37,37 +37,34 @@
             android:scaleType="centerCrop"
             android:focusable="true" />
 
-        <TextView
-            android:id="@id/contact_tile_name"
+        <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginLeft="8dip"
             android:layout_marginStart="8dip"
-            android:textAppearance="?android:attr/textAppearanceMedium"
-            android:layout_marginTop="8dip"
             android:layout_toRightOf="@id/contact_tile_quick"
             android:layout_toEndOf="@id/contact_tile_quick"
-            android:singleLine="true"
-            android:fadingEdge="horizontal"
-            android:fadingEdgeLength="3dip"
-            android:ellipsize="marquee"
-            android:textAlignment="viewStart" />
-
-        <TextView
-            android:id="@+id/contact_tile_phone_type"
-            android:layout_height="wrap_content"
-            android:layout_width="wrap_content"
-            android:layout_below="@id/contact_tile_name"
-            android:layout_toRightOf="@id/contact_tile_quick"
-            android:layout_toEndOf="@id/contact_tile_quick"
-            android:textSize="12sp"
-            android:ellipsize="marquee"
-            android:singleLine="true"
-            android:textColor="@color/dialtacts_secondary_text_color"
-            android:layout_marginLeft="8dip"
-            android:layout_marginStart="8dip"
-            android:layout_gravity="bottom" />
-
+            android:layout_centerVertical="true"
+            android:orientation="vertical">
+            <TextView
+                android:id="@id/contact_tile_name"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:textAppearance="?android:attr/textAppearanceMedium"
+                android:singleLine="true"
+                android:fadingEdge="horizontal"
+                android:fadingEdgeLength="3dip"
+                android:ellipsize="marquee"
+                android:textAlignment="viewStart" />
+            <TextView
+                android:id="@+id/contact_tile_phone_type"
+                android:layout_height="wrap_content"
+                android:layout_width="wrap_content"
+                android:textSize="12sp"
+                android:ellipsize="marquee"
+                android:singleLine="true"
+                android:textColor="@color/dialtacts_secondary_text_color" />
+        </LinearLayout>
         <ImageView
             android:id="@+id/contact_favorite_star"
             android:layout_width="wrap_content"
@@ -78,7 +75,6 @@
             android:layout_marginRight="7dip"
             android:layout_marginEnd="7dip"
             android:layout_marginBottom="7dip"
-            android:layout_centerVertical="true"
             android:src="@drawable/ic_star_marked_as_fav"
             android:visibility="gone" />
 
diff --git a/res/layout/phone_favorite_tile_view.xml b/res/layout/phone_favorite_tile_view.xml
index 1e79a10..5f8733d 100644
--- a/res/layout/phone_favorite_tile_view.xml
+++ b/res/layout/phone_favorite_tile_view.xml
@@ -39,25 +39,43 @@
             android:layout_height="match_parent"
             android:background="@drawable/shadow_contact_photo"/>
 
-        <TextView
-            android:id="@+id/contact_tile_name"
+        <LinearLayout
             android:layout_width="match_parent"
-            android:layout_height="@dimen/contact_tile_info_button_height_and_width"
-            android:gravity="center_vertical"
-            android:textColor="@color/contact_tile_name_color"
-            android:fontFamily="sans-serif-light"
-            android:singleLine="true"
-            android:textSize="16sp"
-            android:fadingEdge="horizontal"
-            android:fadingEdgeLength="3dip"
-            android:ellipsize="marquee"
-            android:layout_alignParentBottom="true"
+            android:layout_height="wrap_content"
             android:paddingLeft="8dp"
             android:paddingRight="@dimen/contact_tile_info_button_height_and_width"
             android:paddingStart="8dp"
             android:paddingEnd="@dimen/contact_tile_info_button_height_and_width"
-            android:textAlignment="viewStart" />
-
+            android:paddingBottom="4dp"
+            android:layout_alignParentBottom="true"
+            android:orientation="vertical" >
+            <TextView
+                android:id="@+id/contact_tile_name"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:gravity="center_vertical"
+                android:textColor="@color/contact_tile_name_color"
+                android:fontFamily="sans-serif-light"
+                android:singleLine="true"
+                android:textSize="16sp"
+                android:fadingEdge="horizontal"
+                android:fadingEdgeLength="3dip"
+                android:ellipsize="marquee"
+                android:textAlignment="viewStart" />
+            <TextView
+                android:id="@+id/contact_tile_phone_type"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:gravity="center_vertical"
+                android:textColor="@color/contact_tile_name_color"
+                android:fontFamily="sans-serif-light"
+                android:singleLine="true"
+                android:textSize="12sp"
+                android:fadingEdge="horizontal"
+                android:fadingEdgeLength="3dip"
+                android:ellipsize="marquee"
+                android:textAlignment="viewStart" />
+        </LinearLayout>
         <View
             android:id="@+id/contact_tile_push_state"
             android:layout_width="match_parent"
diff --git a/src/com/android/dialer/list/PhoneFavoritesTileAdapter.java b/src/com/android/dialer/list/PhoneFavoritesTileAdapter.java
index 1247d35..64387fa 100644
--- a/src/com/android/dialer/list/PhoneFavoritesTileAdapter.java
+++ b/src/com/android/dialer/list/PhoneFavoritesTileAdapter.java
@@ -109,6 +109,7 @@
     private int mPhoneNumberIndex;
     private int mPhoneNumberTypeIndex;
     private int mPhoneNumberLabelIndex;
+    private int mIsDefaultNumberIndex;
     protected int mPinnedIndex;
     protected int mContactIdIndex;
 
@@ -203,6 +204,7 @@
         mPhoneNumberIndex = ContactTileLoaderFactory.PHONE_NUMBER;
         mPhoneNumberTypeIndex = ContactTileLoaderFactory.PHONE_NUMBER_TYPE;
         mPhoneNumberLabelIndex = ContactTileLoaderFactory.PHONE_NUMBER_LABEL;
+        mIsDefaultNumberIndex = ContactTileLoaderFactory.IS_DEFAULT_NUMBER;
         mPinnedIndex = ContactTileLoaderFactory.PINNED;
         mContactIdIndex = ContactTileLoaderFactory.CONTACT_ID_FOR_DATA;
     }
@@ -256,11 +258,6 @@
 
         final LongSparseArray<Object> duplicates = new LongSparseArray<Object>(cursor.getCount());
 
-        // Dummy object that we're inserting into the sparse array as a value so that we can use
-        // the sparse array as a set to check for duplicates
-
-        final Object dummy = new Object();
-
         // Track the length of {@link #mContactEntries} and compare to {@link #TILES_SOFT_LIMIT}.
         int counter = 0;
 
@@ -277,9 +274,14 @@
                 id = cursor.getLong(mContactIdIndex);
             }
 
-            if (duplicates.get(id) == null) {
-                duplicates.put(id, dummy);
-            } else {
+            final ContactEntry existing = (ContactEntry) duplicates.get(id);
+            if (existing != null) {
+                // Check if the existing number is a default number. If not, clear the phone number
+                // and label fields so that the disambiguation dialog will show up.
+                if (!existing.isDefaultNumber) {
+                    existing.phoneLabel = null;
+                    existing.phoneNumber = null;
+                }
                 continue;
             }
 
@@ -288,6 +290,7 @@
             final int pinned = cursor.getInt(mPinnedIndex);
             final String name = cursor.getString(mNameIndex);
             final boolean isStarred = cursor.getInt(mStarredIndex) > 0;
+            final boolean isDefaultNumber = cursor.getInt(mIsDefaultNumberIndex) > 0;
 
             final ContactEntry contact = new ContactEntry();
 
@@ -298,8 +301,9 @@
             contact.lookupKey = ContentUris.withAppendedId(
                     Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey), id);
             contact.isFavorite = isStarred;
+            contact.isDefaultNumber = isDefaultNumber;
 
-            // Set phone number, label and status
+            // Set phone number and label
             final int phoneNumberType = cursor.getInt(mPhoneNumberTypeIndex);
             final String phoneNumberCustomLabel = cursor.getString(mPhoneNumberLabelIndex);
             contact.phoneLabel = (String) Phone.getTypeLabel(mResources, phoneNumberType,
@@ -309,6 +313,8 @@
             contact.pinned = pinned;
             mContactEntries.add(contact);
 
+            duplicates.put(id, contact);
+
             counter++;
         }