Made default items show first in contact details/quick contact lists

Bug:5779998
Change-Id: If4c3ff5b8b332dccb0f07fe1662832b875233c44
diff --git a/src/com/android/contacts/ContactLoader.java b/src/com/android/contacts/ContactLoader.java
index aed377b..ddebf4e 100644
--- a/src/com/android/contacts/ContactLoader.java
+++ b/src/com/android/contacts/ContactLoader.java
@@ -744,14 +744,20 @@
                 return Result.forNotFound(mRequestedUri);
             }
 
+            // Create the loaded result starting with the Contact data.
+            Result result = loadContactHeaderData(cursor, contactUri);
+
+            // Fill in the raw contacts, which is wrapped in an Entity and any
+            // status data.  Initially, result has empty entities and statuses.
             long currentRawContactId = -1;
             Entity entity = null;
-            Result result = loadContactHeaderData(cursor, contactUri);
             ArrayList<Entity> entities = result.getEntities();
             LongSparseArray<DataStatus> statuses = result.getStatuses();
             for (; !cursor.isAfterLast(); cursor.moveToNext()) {
                 long rawContactId = cursor.getLong(ContactQuery.RAW_CONTACT_ID);
                 if (rawContactId != currentRawContactId) {
+                    // First time to see this raw contact id, so create a new entity, and
+                    // add it to the result's entities.
                     currentRawContactId = rawContactId;
                     entity = new android.content.Entity(loadRawContact(cursor));
                     entities.add(entity);
diff --git a/src/com/android/contacts/detail/ContactDetailFragment.java b/src/com/android/contacts/detail/ContactDetailFragment.java
index 238055c..ca6fa63 100644
--- a/src/com/android/contacts/detail/ContactDetailFragment.java
+++ b/src/com/android/contacts/detail/ContactDetailFragment.java
@@ -627,13 +627,26 @@
                     if (isSuperPrimary) mPrimaryPhoneUri = entry.uri;
 
                     entry.isPrimary = isSuperPrimary;
-                    mPhoneEntries.add(entry);
+
+                    // If the entry is a primary entry, then render it first in the view.
+                    if (entry.isPrimary) {
+                        // add to beginning of list so that this phone number shows up first
+                        mPhoneEntries.add(0, entry);
+                    } else {
+                        // add to end of list
+                        mPhoneEntries.add(entry);
+                    }
                 } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
                     // Build email entries
                     entry.intent = new Intent(Intent.ACTION_SENDTO,
                             Uri.fromParts(Constants.SCHEME_MAILTO, entry.data, null));
                     entry.isPrimary = isSuperPrimary;
-                    mEmailEntries.add(entry);
+                    // If entry is a primary entry, then render it first in the view.
+                    if (entry.isPrimary) {
+                        mEmailEntries.add(0, entry);
+                    } else {
+                        mEmailEntries.add(entry);
+                    }
 
                     // When Email rows have status, create additional Im row
                     final DataStatus status = mContactData.getStatuses().get(entry.id);
diff --git a/src/com/android/contacts/quickcontact/ActionMultiMap.java b/src/com/android/contacts/quickcontact/ActionMultiMap.java
index 21234ce..ff9d677 100644
--- a/src/com/android/contacts/quickcontact/ActionMultiMap.java
+++ b/src/com/android/contacts/quickcontact/ActionMultiMap.java
@@ -25,12 +25,26 @@
  */
 public class ActionMultiMap extends HashMap<String, ArrayList<Action>> {
     public void put(String mimeType, Action info) {
-        // Create list for this MIME-type when needed
+       put(mimeType, info, false);
+    }
+
+    /**
+     * Puts the (mimeType,Action) tuple into the multimap at the front if
+     * the 'front' flag is set to true
+     */
+    public void put(String mimeType, Action info, boolean front) {
+        // Put the info first
         ArrayList<Action> collectList = get(mimeType);
+
+        // Create list for this MIME-type if needed
         if (collectList == null) {
             collectList = new ArrayList<Action>();
             put(mimeType, collectList);
         }
-        collectList.add(info);
+        if (front) {
+            collectList.add(0, info);
+        } else {
+            collectList.add(info);
+        }
     }
 }
diff --git a/src/com/android/contacts/quickcontact/QuickContactActivity.java b/src/com/android/contacts/quickcontact/QuickContactActivity.java
index e00f051..f914aa1 100644
--- a/src/com/android/contacts/quickcontact/QuickContactActivity.java
+++ b/src/com/android/contacts/quickcontact/QuickContactActivity.java
@@ -345,7 +345,7 @@
                     // along with all others of this MIME-type.
                     final Action action = new DataAction(context, mimeType, kind, dataId,
                             entryValues);
-                    final boolean wasAdded = considerAdd(action, cache);
+                    final boolean wasAdded = considerAdd(action, cache, isSuperPrimary);
                     if (wasAdded) {
                         // Remember the default
                         if (isSuperPrimary || (isPrimary && (mDefaultsMap.get(mimeType) == null))) {
@@ -363,7 +363,7 @@
                         final DataAction action = new DataAction(context, Im.CONTENT_ITEM_TYPE,
                                 imKind, dataId, entryValues);
                         action.setPresence(status.getPresence());
-                        considerAdd(action, cache);
+                        considerAdd(action, cache, isSuperPrimary);
                     }
                 }
             }
@@ -421,11 +421,14 @@
      * Consider adding the given {@link Action}, which will only happen if
      * {@link PackageManager} finds an application to handle
      * {@link Action#getIntent()}.
+     * @param action the action to handle
+     * @param resolveCache cache of applications that can handle actions
+     * @param front indicates whether to add the action to the front of the list
      * @return true if action has been added
      */
-    private boolean considerAdd(Action action, ResolveCache resolveCache) {
+    private boolean considerAdd(Action action, ResolveCache resolveCache, boolean front) {
         if (resolveCache.hasResolve(action)) {
-            mActions.put(action.getMimeType(), action);
+            mActions.put(action.getMimeType(), action, front);
             return true;
         }
         return false;