Merge "Avoid saving contact when user doesn't intend to do so." into ub-contactsdialer-a-dev
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index 6f5a9cb..f5ce6e3 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -30,9 +30,7 @@
 import com.android.contacts.util.UiClosables;
 
 import android.content.ContentUris;
-import android.content.ContentValues;
 import android.content.Context;
-import android.content.Intent;
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.net.Uri;
@@ -74,7 +72,8 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
-import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -169,8 +168,12 @@
             // For email addresses, we don't want to truncate at end, which might cut off the domain
             // name.
             final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
-            text2.setText(accountName);
-            text2.setEllipsize(TextUtils.TruncateAt.MIDDLE);
+            if (TextUtils.isEmpty(accountName)) {
+                text2.setVisibility(View.GONE);
+            } else {
+                text2.setText(accountName);
+                text2.setEllipsize(TextUtils.TruncateAt.MIDDLE);
+            }
 
             final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
             icon.setImageDrawable(accountType.getDisplayIcon(mContext));
@@ -864,6 +867,8 @@
     private void addRawContactAccountSelector(final RawContactDeltaList rawContactDeltas) {
         mRawContactContainer.setVisibility(View.VISIBLE);
 
+        Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
+
         final String accountsSummary = getRawContactsAccountsSummary(
                 getContext(), rawContactDeltas);
         mRawContactSummary.setText(accountsSummary);
@@ -913,7 +918,7 @@
 
     private static String getRawContactsAccountsSummary(
             Context context, RawContactDeltaList rawContactDeltas) {
-        final Map<String, Integer> accountTypeNumber = new HashMap<>();
+        final LinkedHashMap<String, Integer> accountTypeNumber = new LinkedHashMap<>();
         for (RawContactDelta rawContactDelta : rawContactDeltas) {
             if (rawContactDelta.isVisible()) {
                 final AccountType accountType = rawContactDelta.getRawContactAccountType(context);
@@ -928,7 +933,7 @@
             }
         }
 
-        final Set<String> linkedAccounts = new HashSet<>();
+        final LinkedHashSet<String> linkedAccounts = new LinkedHashSet<>();
         for (String accountTypeLabel : accountTypeNumber.keySet()) {
             final String number = context.getResources().getQuantityString(
                     R.plurals.quickcontact_suggestion_account_type_number,
diff --git a/src/com/android/contacts/editor/ContactEditorBaseFragment.java b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
index 481383e..718128b 100644
--- a/src/com/android/contacts/editor/ContactEditorBaseFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
@@ -1362,7 +1362,7 @@
             }
             mPhotoId = mIntentExtras.getLong(INTENT_EXTRA_PHOTO_ID);
             mRawContactIdToDisplayAlone = mIntentExtras.getLong(
-                    INTENT_EXTRA_RAW_CONTACT_ID_TO_DISPLAY_ALONE);
+                    INTENT_EXTRA_RAW_CONTACT_ID_TO_DISPLAY_ALONE, -1);
             mRawContactDisplayAloneIsReadOnly = mIntentExtras.getBoolean(
                     INTENT_EXTRA_RAW_CONTACT_DISPLAY_ALONE_IS_READ_ONLY);
         }
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index c602a1d..a3830ef 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -114,7 +114,7 @@
             mCurrentPhotoUri = savedState.getParcelable(KEY_CURRENT_PHOTO_URI);
             mUpdatedPhotos = savedState.getParcelable(KEY_UPDATED_PHOTOS);
             mRawContactIdToDisplayAlone = savedState.getLong(
-                    ContactEditorBaseFragment.INTENT_EXTRA_RAW_CONTACT_ID_TO_DISPLAY_ALONE);
+                    ContactEditorBaseFragment.INTENT_EXTRA_RAW_CONTACT_ID_TO_DISPLAY_ALONE, -1);
         }
     }
 
@@ -123,7 +123,7 @@
         super.load(action, lookupUri, intentExtras);
         if (intentExtras != null) {
             mRawContactIdToDisplayAlone = intentExtras.getLong(
-                    ContactEditorBaseFragment.INTENT_EXTRA_RAW_CONTACT_ID_TO_DISPLAY_ALONE);
+                    ContactEditorBaseFragment.INTENT_EXTRA_RAW_CONTACT_ID_TO_DISPLAY_ALONE, -1);
         }
     }