Toast fixes for deletion and save

Bug: 32243988
Test: Save contact from editor in alternative display mode.
Test: Delete contact from editor in alternative display mode.
Test: Save contact from editor with only phonetic or email or company.
Test: Delete contacts from list view with default and alternative
display mode.

Change-Id: I82df2377692ea129a54a3f0f00948201d4d1411e
diff --git a/src/com/android/contacts/ContactSaveService.java b/src/com/android/contacts/ContactSaveService.java
index 2e98932..1dcc838 100755
--- a/src/com/android/contacts/ContactSaveService.java
+++ b/src/com/android/contacts/ContactSaveService.java
@@ -119,6 +119,7 @@
     public static final String EXTRA_CONTACT_IDS = "contactIds";
     public static final String EXTRA_STARRED_FLAG = "starred";
     public static final String EXTRA_DISPLAY_NAME = "extraDisplayName";
+    public static final String EXTRA_DISPLAY_NAME_ARRAY = "extraDisplayNameArray";
 
     public static final String ACTION_SET_SUPER_PRIMARY = "setSuperPrimary";
     public static final String ACTION_CLEAR_PRIMARY = "clearPrimary";
@@ -1148,10 +1149,11 @@
      * Creates an intent that can be sent to this service to delete multiple contacts.
      */
     public static Intent createDeleteMultipleContactsIntent(Context context,
-            long[] contactIds) {
+            long[] contactIds, final String[] names) {
         Intent serviceIntent = new Intent(context, ContactSaveService.class);
         serviceIntent.setAction(ContactSaveService.ACTION_DELETE_MULTIPLE_CONTACTS);
         serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_IDS, contactIds);
+        serviceIntent.putExtra(ContactSaveService.EXTRA_DISPLAY_NAME_ARRAY, names);
         return serviceIntent;
     }
 
@@ -1175,8 +1177,22 @@
             final Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
             getContentResolver().delete(contactUri, null, null);
         }
-        final String deleteToastMessage = getResources().getQuantityString(R.plurals
-                .contacts_deleted_toast, contactIds.length);
+        final String[] names = intent.getStringArrayExtra(
+                ContactSaveService.EXTRA_DISPLAY_NAME_ARRAY);
+        final String deleteToastMessage;
+        if (names.length == 0) {
+            deleteToastMessage = getResources().getQuantityString(
+                    R.plurals.contacts_deleted_toast, contactIds.length);
+        } else if (names.length == 1) {
+            deleteToastMessage = getResources().getString(
+                    R.string.contacts_deleted_one_named_toast, names);
+        } else if (names.length == 2) {
+            deleteToastMessage = getResources().getString(
+                    R.string.contacts_deleted_two_named_toast, names);
+        } else {
+            deleteToastMessage = getResources().getString(
+                    R.string.contacts_deleted_many_named_toast, names);
+        }
         mMainHandler.post(new Runnable() {
             @Override
             public void run() {
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index e013da4..4276105 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -21,6 +21,7 @@
 import android.app.Fragment;
 import android.app.LoaderManager;
 import android.content.ActivityNotFoundException;
+import android.content.ContentResolver;
 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.Context;
@@ -75,6 +76,8 @@
 import com.android.contacts.common.model.ValuesDelta;
 import com.android.contacts.common.model.account.AccountType;
 import com.android.contacts.common.model.account.AccountWithDataSet;
+import com.android.contacts.common.preference.ContactsPreferences;
+import com.android.contacts.common.util.ContactDisplayUtils;
 import com.android.contacts.common.util.ImplicitIntentsUtil;
 import com.android.contacts.common.util.MaterialColorMapUtils;
 import com.android.contacts.editor.AggregationSuggestionEngine.Suggestion;
@@ -1510,6 +1513,28 @@
         onSaveCompleted(false, SaveMode.RELOAD, uri != null, uri, /* joinContactId */ null);
     }
 
+
+    private String getNameToDisplay(Uri contactUri) {
+        final ContentResolver resolver = mContext.getContentResolver();
+        final Cursor cursor = resolver.query(contactUri, new String[]{
+                ContactsContract.Contacts.DISPLAY_NAME,
+                ContactsContract.Contacts.DISPLAY_NAME_ALTERNATIVE}, null, null, null);
+        try {
+            if (cursor.moveToFirst()) {
+                final String displayName = cursor.getString(0);
+                final String displayNameAlt = cursor.getString(1);
+                cursor.close();
+                return ContactDisplayUtils.getPreferredDisplayName(displayName, displayNameAlt,
+                        new ContactsPreferences(mContext));
+            }
+        } finally {
+            cursor.close();
+        }
+
+        return null;
+    }
+
+
     @Override
     public void onSaveCompleted(boolean hadChanges, int saveMode, boolean saveSucceeded,
             Uri contactLookupUri, Long joinContactId) {
@@ -1523,8 +1548,7 @@
                                 .show();
                         break;
                     default:
-                        final String displayName = getContent().getNameEditorView()
-                                .getDisplayName();
+                        final String displayName = getNameToDisplay(contactLookupUri);
                         final String toastMessage;
                         if (!TextUtils.isEmpty(displayName)) {
                             toastMessage = getResources().getString(
diff --git a/src/com/android/contacts/interactions/ContactDeletionInteraction.java b/src/com/android/contacts/interactions/ContactDeletionInteraction.java
index ec500f3..9b3d0f8 100644
--- a/src/com/android/contacts/interactions/ContactDeletionInteraction.java
+++ b/src/com/android/contacts/interactions/ContactDeletionInteraction.java
@@ -40,6 +40,8 @@
 import com.android.contacts.R;
 import com.android.contacts.common.model.AccountTypeManager;
 import com.android.contacts.common.model.account.AccountType;
+import com.android.contacts.common.preference.ContactsPreferences;
+import com.android.contacts.common.util.ContactDisplayUtils;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Sets;
 
@@ -67,6 +69,7 @@
         Entity.CONTACT_ID, // 3
         Entity.LOOKUP_KEY, // 4
         Entity.DISPLAY_NAME, // 5
+        Entity.DISPLAY_NAME_ALTERNATIVE, // 6
     };
 
     private static final int COLUMN_INDEX_RAW_CONTACT_ID = 0;
@@ -75,10 +78,12 @@
     private static final int COLUMN_INDEX_CONTACT_ID = 3;
     private static final int COLUMN_INDEX_LOOKUP_KEY = 4;
     private static final int COLUMN_INDEX_DISPLAY_NAME = 5;
+    private static final int COLUMN_INDEX_DISPLAY_NAME_ALT = 6;
 
     private boolean mActive;
     private Uri mContactUri;
     private String mDisplayName;
+    private String mDisplayNameAlt;
     private boolean mFinishActivityWhenDone;
     private Context mContext;
     private AlertDialog mDialog;
@@ -252,6 +257,7 @@
             contactId = cursor.getLong(COLUMN_INDEX_CONTACT_ID);
             lookupKey = cursor.getString(COLUMN_INDEX_LOOKUP_KEY);
             mDisplayName = cursor.getString(COLUMN_INDEX_DISPLAY_NAME);
+            mDisplayNameAlt = cursor.getString(COLUMN_INDEX_DISPLAY_NAME_ALT);
             AccountType type = accountTypes.getAccountType(accountType, dataSet);
             boolean writable = type == null || type.areContactsWritable();
             if (writable) {
@@ -343,12 +349,14 @@
             getActivity().setResult(RESULT_CODE_DELETED);
             getActivity().finish();
             final String deleteToastMessage;
-            if (mDisplayName == null) {
+            final String name = ContactDisplayUtils.getPreferredDisplayName(mDisplayName,
+                    mDisplayNameAlt, new ContactsPreferences(mContext));
+            if (TextUtils.isEmpty(name)) {
                 deleteToastMessage = getResources().getQuantityString(
                         R.plurals.contacts_deleted_toast, /* quantity */ 1);
             } else {
                 deleteToastMessage = getResources().getString(
-                        R.string.contact_deleted_named_toast, mDisplayName);
+                        R.string.contacts_deleted_one_named_toast, name);
             }
             Toast.makeText(mContext, deleteToastMessage, Toast.LENGTH_LONG).show();
         }
diff --git a/src/com/android/contacts/interactions/ContactMultiDeletionInteraction.java b/src/com/android/contacts/interactions/ContactMultiDeletionInteraction.java
index ff0d978..b63eacd 100644
--- a/src/com/android/contacts/interactions/ContactMultiDeletionInteraction.java
+++ b/src/com/android/contacts/interactions/ContactMultiDeletionInteraction.java
@@ -16,6 +16,8 @@
 
 package com.android.contacts.interactions;
 
+import com.android.contacts.common.preference.ContactsPreferences;
+import com.android.contacts.common.util.ContactDisplayUtils;
 import com.google.common.collect.Sets;
 
 import com.android.contacts.ContactSaveService;
@@ -64,12 +66,16 @@
             RawContacts.ACCOUNT_TYPE,
             RawContacts.DATA_SET,
             RawContacts.CONTACT_ID,
+            RawContacts.DISPLAY_NAME_PRIMARY,
+            RawContacts.DISPLAY_NAME_ALTERNATIVE
     };
 
     private static final int COLUMN_INDEX_RAW_CONTACT_ID = 0;
     private static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
     private static final int COLUMN_INDEX_DATA_SET = 2;
     private static final int COLUMN_INDEX_CONTACT_ID = 3;
+    private static final int COLUMN_INDEX_DISPLAY_NAME = 4;
+    private static final int COLUMN_INDEX_DISPLAY_NAME_ALT = 5;
 
     private boolean mIsLoaderActive;
     private TreeSet<Long> mContactIds;
@@ -193,6 +199,9 @@
         final HashSet<Long> readOnlyRawContacts = Sets.newHashSet();
         final HashSet<Long> writableRawContacts = Sets.newHashSet();
         final HashSet<Long> contactIds = Sets.newHashSet();
+        final HashSet<String> names = Sets.newHashSet();
+
+        final ContactsPreferences contactsPreferences = new ContactsPreferences(mContext);
 
         AccountTypeManager accountTypes = AccountTypeManager.getInstance(getActivity());
         cursor.moveToPosition(-1);
@@ -201,6 +210,12 @@
             final String accountType = cursor.getString(COLUMN_INDEX_ACCOUNT_TYPE);
             final String dataSet = cursor.getString(COLUMN_INDEX_DATA_SET);
             final long contactId = cursor.getLong(COLUMN_INDEX_CONTACT_ID);
+            final String displayName = cursor.getString(COLUMN_INDEX_DISPLAY_NAME);
+            final String displayNameAlt = cursor.getString(COLUMN_INDEX_DISPLAY_NAME_ALT);
+
+            names.add(ContactDisplayUtils.getPreferredDisplayName(displayName, displayNameAlt,
+                    contactsPreferences));
+
             contactIds.add(contactId);
             final AccountType type = accountTypes.getAccountType(accountType, dataSet);
             boolean writable = type == null || type.areContactsWritable();
@@ -237,7 +252,8 @@
             contactIdArray[i] = contactIdObjectArray[i];
         }
 
-        showDialog(messageId, positiveButtonId, contactIdArray);
+        final String[] namesArray = names.toArray(new String[names.size()]);
+        showDialog(messageId, positiveButtonId, contactIdArray, namesArray);
 
         // We don't want onLoadFinished() calls any more, which may come when the database is
         // updating.
@@ -248,7 +264,8 @@
     public void onLoaderReset(Loader<Cursor> loader) {
     }
 
-    private void showDialog(int messageId, int positiveButtonId, final long[] contactIds) {
+    private void showDialog(int messageId, int positiveButtonId, final long[] contactIds,
+            final String[] namesArray) {
         mDialog = new AlertDialog.Builder(getActivity())
                 .setIconAttribute(android.R.attr.alertDialogIcon)
                 .setMessage(messageId)
@@ -257,7 +274,7 @@
                     new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int whichButton) {
-                            doDeleteContact(contactIds);
+                            doDeleteContact(contactIds, namesArray);
                         }
                     }
                 )
@@ -289,9 +306,9 @@
         }
     }
 
-    protected void doDeleteContact(long[] contactIds) {
+    protected void doDeleteContact(long[] contactIds, final String[] names) {
         mContext.startService(ContactSaveService.createDeleteMultipleContactsIntent(mContext,
-                contactIds));
+                contactIds, names));
         mListener.onDeletionFinished();
     }