Merge "Place phonetic name under display name in QuickContact."
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 824f6a4..5ed0acd 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -38,6 +38,7 @@
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />
     <uses-permission android:name="android.permission.VIBRATE" />
     <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
     <!-- Following used for QuickContacts -->
     <uses-permission android:name="android.permission.READ_CALL_LOG" />
@@ -489,6 +490,13 @@
             </intent-filter>
         </activity>
 
+        <activity android:name="com.android.contacts.common.dialog.CallSubjectHistory"
+                  android:theme="@style/Theme.CallSubjectSelector">
+            <intent-filter>
+                <action android:name="android.intent.action.VIEW"/>
+            </intent-filter>
+        </activity>
+
         <!-- Service that is exclusively for the Phone application that sends out a view
              notification. This service might be removed in future versions of the app.
 
diff --git a/res/values-ro/strings.xml b/res/values-ro/strings.xml
index 79aa0f8..763bb2e 100644
--- a/res/values-ro/strings.xml
+++ b/res/values-ro/strings.xml
@@ -256,7 +256,7 @@
     <string name="untitled_event" msgid="3484859385405939366">"(Eveniment fără titlu)"</string>
     <string name="date_time_set" msgid="4761419824439606690">"Setați"</string>
     <string name="header_im_entry" msgid="3581720979640225615">"IM"</string>
-    <string name="header_organization_entry" msgid="8515394955666265406">"Organizaţie"</string>
+    <string name="header_organization_entry" msgid="8515394955666265406">"Organizație"</string>
     <string name="header_nickname_entry" msgid="6743561883967451485">"Pseudonim"</string>
     <string name="header_note_entry" msgid="4320190426480612344">"Notă"</string>
     <string name="header_website_entry" msgid="1411467850000824745">"Site web"</string>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index bd9e341..d992e81 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -529,8 +529,8 @@
     <!-- Format string that combines the name and the phonetic name for the widget. if the phonetic name is empty, only the display name is used instead [CHAR LIMIT=25] -->
     <string name="widget_name_and_phonetic"><xliff:g id="display_name" example="John Huber">%1$s</xliff:g> (<xliff:g id="phonetic_name">%2$s</xliff:g>)</string>
 
-    <!-- Checkbox whether to provide a year for a birthday [CHAR LIMIT=30] -->
-    <string name="date_year_toggle">Provide a year</string>
+    <!-- Checkbox whether to include a year for a birthday [CHAR LIMIT=30] -->
+    <string name="date_year_toggle">Include year</string>
 
     <!-- Label for the widget that shows picture and social status of a contact [CHAR LIMIT=20] -->
     <string name="social_widget_label">Contact</string>
diff --git a/src/com/android/contacts/ContactSaveService.java b/src/com/android/contacts/ContactSaveService.java
index df5c697..634e29a 100755
--- a/src/com/android/contacts/ContactSaveService.java
+++ b/src/com/android/contacts/ContactSaveService.java
@@ -16,6 +16,7 @@
 
 package com.android.contacts;
 
+import static android.Manifest.permission.WRITE_CONTACTS;
 import android.app.Activity;
 import android.app.IntentService;
 import android.content.ContentProviderOperation;
@@ -54,6 +55,7 @@
 import com.android.contacts.common.model.RawContactDeltaList;
 import com.android.contacts.common.model.RawContactModifier;
 import com.android.contacts.common.model.account.AccountWithDataSet;
+import com.android.contacts.common.util.PermissionsUtil;
 import com.android.contacts.editor.ContactEditorFragment;
 import com.android.contacts.util.ContactPhotoUtils;
 
@@ -142,6 +144,8 @@
 
     private static final int PERSIST_TRIES = 3;
 
+    private static final int MAX_CONTACTS_PROVIDER_BATCH_SIZE = 499;
+
     public interface Listener {
         public void onServiceCompleted(Intent callbackIntent);
     }
@@ -185,6 +189,14 @@
             Log.d(TAG, "onHandleIntent: could not handle null intent");
             return;
         }
+        if (!PermissionsUtil.hasPermission(this, WRITE_CONTACTS)) {
+            Log.w(TAG, "No WRITE_CONTACTS permission, unable to write to CP2");
+            // TODO: add more specific error string such as "Turn on Contacts
+            // permission to update your contacts"
+            showToast(R.string.contactSavedErrorToast);
+            return;
+        }
+
         // Call an appropriate method. If we're sure it affects how incoming phone calls are
         // handled, then notify the fact to in-call screen.
         String action = intent.getAction();
@@ -1081,23 +1093,39 @@
 
         // For each pair of raw contacts, insert an aggregation exception
         final ContentResolver resolver = getContentResolver();
-        final ArrayList<ContentProviderOperation> operations
-                = new ArrayList<ContentProviderOperation>();
+        // The maximum number of operations per batch (aka yield point) is 500. See b/22480225
+        final int batchSize = MAX_CONTACTS_PROVIDER_BATCH_SIZE;
+        final ArrayList<ContentProviderOperation> operations = new ArrayList<>(batchSize);
         for (int i = 0; i < rawContactIds.length; i++) {
             for (int j = 0; j < rawContactIds.length; j++) {
                 if (i != j) {
                     buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]);
                 }
+                // Before we get to 500 we need to flush the operations list
+                if (operations.size() > 0 && operations.size() % batchSize == 0) {
+                    if (!applyJoinOperations(resolver, operations)) {
+                        return;
+                    }
+                    operations.clear();
+                }
             }
         }
+        if (operations.size() > 0 && !applyJoinOperations(resolver, operations)) {
+            return;
+        }
+        showToast(R.string.contactsJoinedMessage);
+    }
 
-        // Apply all aggregation exceptions as one batch
+    /** Returns true if the batch was successfully applied and false otherwise. */
+    private boolean applyJoinOperations(ContentResolver resolver,
+            ArrayList<ContentProviderOperation> operations) {
         try {
             resolver.applyBatch(ContactsContract.AUTHORITY, operations);
-            showToast(R.string.contactsJoinedMessage);
+            return true;
         } catch (RemoteException | OperationApplicationException e) {
             Log.e(TAG, "Failed to apply aggregation exception batch", e);
             showToast(R.string.contactSavedErrorToast);
+            return false;
         }
     }
 
diff --git a/src/com/android/contacts/ContactsApplication.java b/src/com/android/contacts/ContactsApplication.java
index eae078e..798614c 100644
--- a/src/com/android/contacts/ContactsApplication.java
+++ b/src/com/android/contacts/ContactsApplication.java
@@ -130,10 +130,12 @@
         protected Void doInBackground(Void... params) {
             final Context context = ContactsApplication.this;
 
-            // Warm up the preferences, the account type manager and the contacts provider.
+            // Warm up the preferences and the contacts provider.  We delay initialization
+            // of the account type manager because we may not have the contacts group permission
+            // (and thus not have the get accounts permission).
             PreferenceManager.getDefaultSharedPreferences(context);
-            AccountTypeManager.getInstance(context);
             getContentResolver().getType(ContentUris.withAppendedId(Contacts.CONTENT_URI, 1));
+
             return null;
         }
 
diff --git a/src/com/android/contacts/list/MultiSelectContactsListFragment.java b/src/com/android/contacts/list/MultiSelectContactsListFragment.java
index a58a8ee..bbb0d84 100644
--- a/src/com/android/contacts/list/MultiSelectContactsListFragment.java
+++ b/src/com/android/contacts/list/MultiSelectContactsListFragment.java
@@ -23,6 +23,7 @@
 
 import android.net.Uri;
 import android.os.Bundle;
+import android.provider.ContactsContract;
 import android.text.TextUtils;
 
 import java.util.TreeSet;
@@ -115,7 +116,9 @@
     protected boolean onItemLongClick(int position, long id) {
         final int previouslySelectedCount = getAdapter().getSelectedContactIds().size();
         final Uri uri = getAdapter().getContactUri(position);
-        if (uri != null && (position > 0 || !getAdapter().hasProfile())) {
+        final int partition = getAdapter().getPartitionForPosition(position);
+        if (uri != null && (partition == ContactsContract.Directory.DEFAULT
+                && (position > 0 || !getAdapter().hasProfile()))) {
             final String contactId = uri.getLastPathSegment();
             if (!TextUtils.isEmpty(contactId)) {
                 if (mCheckBoxListListener != null) {
diff --git a/src/com/android/contacts/list/MultiSelectEntryContactListAdapter.java b/src/com/android/contacts/list/MultiSelectEntryContactListAdapter.java
index 7c6f7bc..3ba86db 100644
--- a/src/com/android/contacts/list/MultiSelectEntryContactListAdapter.java
+++ b/src/com/android/contacts/list/MultiSelectEntryContactListAdapter.java
@@ -21,6 +21,7 @@
 
 import android.content.Context;
 import android.database.Cursor;
+import android.provider.ContactsContract;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.CheckBox;
@@ -108,17 +109,19 @@
     @Override
     protected void bindView(View itemView, int partition, Cursor cursor, int position) {
         super.bindView(itemView, partition, cursor, position);
-        final ContactListItemView view = (ContactListItemView)itemView;
-        bindCheckBox(view, cursor, position);
+        final ContactListItemView view = (ContactListItemView) itemView;
+        bindCheckBox(view, cursor, position, partition == ContactsContract.Directory.DEFAULT);
     }
 
-    private void bindCheckBox(ContactListItemView view, Cursor cursor, int position) {
-        // Disable clicking on the first entry when showing check boxes. We do this by
-        // telling the view to handle clicking itself.
-        view.setClickable(position == 0 && hasProfile() && mDisplayCheckBoxes);
+    private void bindCheckBox(ContactListItemView view, Cursor cursor, int position,
+            boolean isLocalDirectory) {
+        // Disable clicking on the ME profile and all contacts from remote directories
+        // when showing check boxes. We do this by telling the view to handle clicking itself.
+        view.setClickable((position == 0 && hasProfile() || !isLocalDirectory)
+                && mDisplayCheckBoxes);
         // Only show checkboxes if mDisplayCheckBoxes is enabled. Also, never show the
-        // checkbox for the Me profile entry.
-        if (position == 0 && hasProfile() || !mDisplayCheckBoxes) {
+        // checkbox for the Me profile entry and other directory contacts except local directory.
+        if (position == 0 && hasProfile() || !mDisplayCheckBoxes || !isLocalDirectory) {
             view.hideCheckBox();
             return;
         }
diff --git a/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java b/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java
index 10b2898..10887cb 100644
--- a/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java
+++ b/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java
@@ -19,12 +19,14 @@
 import android.animation.Animator.AnimatorListener;
 import android.animation.AnimatorSet;
 import android.animation.ObjectAnimator;
+import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.content.res.Resources;
 import android.graphics.ColorFilter;
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
+import android.os.Bundle;
 import android.support.v7.widget.CardView;
 import android.text.Spannable;
 import android.text.TextUtils;
@@ -50,6 +52,7 @@
 import android.widget.TextView;
 
 import com.android.contacts.R;
+import com.android.contacts.common.dialog.CallSubjectDialog;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -87,6 +90,12 @@
      * Entry data.
      */
     public static final class Entry {
+        // No action when clicking a button is specified.
+        public static final int ACTION_NONE = 1;
+        // Button action is an intent.
+        public static final int ACTION_INTENT = 2;
+        // Button action will open the call with subject dialog.
+        public static final int ACTION_CALL_WITH_SUBJECT = 3;
 
         private final int mId;
         private final Drawable mIcon;
@@ -107,6 +116,8 @@
         private final Intent mThirdIntent;
         private final String mThirdContentDescription;
         private final int mIconResourceId;
+        private final int mThirdAction;
+        private final Bundle mThirdExtras;
 
         public Entry(int id, Drawable mainIcon, String header, String subHeader,
                 Drawable subHeaderIcon, String text, Drawable textIcon,
@@ -114,7 +125,8 @@
                 Drawable alternateIcon, Intent alternateIntent, String alternateContentDescription,
                 boolean shouldApplyColor, boolean isEditable,
                 EntryContextMenuInfo entryContextMenuInfo, Drawable thirdIcon, Intent thirdIntent,
-                String thirdContentDescription, int iconResourceId) {
+                String thirdContentDescription, int thirdAction, Bundle thirdExtras,
+                int iconResourceId) {
             mId = id;
             mIcon = mainIcon;
             mHeader = header;
@@ -133,6 +145,8 @@
             mThirdIcon = thirdIcon;
             mThirdIntent = thirdIntent;
             mThirdContentDescription = thirdContentDescription;
+            mThirdAction = thirdAction;
+            mThirdExtras = thirdExtras;
             mIconResourceId = iconResourceId;
         }
 
@@ -211,6 +225,14 @@
         int getIconResourceId() {
             return mIconResourceId;
         }
+
+        public int getThirdAction() {
+            return mThirdAction;
+        }
+
+        public Bundle getThirdExtras() {
+            return mThirdExtras;
+        }
     }
 
     public interface ExpandingEntryCardViewListener {
@@ -761,10 +783,28 @@
             alternateIcon.setContentDescription(entry.getAlternateContentDescription());
         }
 
-        if (entry.getThirdIcon() != null && entry.getThirdIntent() != null) {
+        if (entry.getThirdIcon() != null && entry.getThirdAction() != Entry.ACTION_NONE) {
             thirdIcon.setImageDrawable(entry.getThirdIcon());
-            thirdIcon.setOnClickListener(mOnClickListener);
-            thirdIcon.setTag(new EntryTag(entry.getId(), entry.getThirdIntent()));
+            if (entry.getThirdAction() == Entry.ACTION_INTENT) {
+                thirdIcon.setOnClickListener(mOnClickListener);
+                thirdIcon.setTag(new EntryTag(entry.getId(), entry.getThirdIntent()));
+            } else if (entry.getThirdAction() == Entry.ACTION_CALL_WITH_SUBJECT) {
+                thirdIcon.setOnClickListener(new View.OnClickListener() {
+                    @Override
+                    public void onClick(View v) {
+                        Object tag = v.getTag();
+                        if (!(tag instanceof Bundle)) {
+                            return;
+                        }
+
+                        Context context = getContext();
+                        if (context instanceof Activity) {
+                            CallSubjectDialog.start((Activity) context, entry.getThirdExtras());
+                        }
+                    }
+                });
+                thirdIcon.setTag(entry.getThirdExtras());
+            }
             thirdIcon.setVisibility(View.VISIBLE);
             thirdIcon.setContentDescription(entry.getThirdContentDescription());
         }
diff --git a/src/com/android/contacts/quickcontact/QuickContactActivity.java b/src/com/android/contacts/quickcontact/QuickContactActivity.java
index 009f7fb..64d8aec 100644
--- a/src/com/android/contacts/quickcontact/QuickContactActivity.java
+++ b/src/com/android/contacts/quickcontact/QuickContactActivity.java
@@ -96,6 +96,7 @@
 import com.android.contacts.common.Collapser;
 import com.android.contacts.common.ContactsUtils;
 import com.android.contacts.common.activity.RequestPermissionsActivity;
+import com.android.contacts.common.dialog.CallSubjectDialog;
 import com.android.contacts.common.editor.SelectAccountDialogFragment;
 import com.android.contacts.common.interactions.TouchPointManager;
 import com.android.contacts.common.lettertiles.LetterTileDrawable;
@@ -125,6 +126,7 @@
 import com.android.contacts.common.util.DateUtils;
 import com.android.contacts.common.util.MaterialColorMapUtils;
 import com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
+import com.android.contacts.common.util.UriUtils;
 import com.android.contacts.common.util.ViewUtil;
 import com.android.contacts.detail.ContactDisplayUtils;
 import com.android.contacts.editor.ContactEditorFragment;
@@ -1136,7 +1138,9 @@
                     /* thirdIcon = */ null,
                     /* thirdIntent = */ null,
                     /* thirdContentDescription = */ null,
-                    /* iconResourceId = */ 0);
+                    /* thirdAction = */ Entry.ACTION_NONE,
+                    /* thirdExtras = */ null,
+                    /* iconResourceId = */  0);
             List<Entry> phoneticList = new ArrayList<>();
             phoneticList.add(phoneticEntry);
             // Phonetic name comes after nickname. Check to see if the first entry type is nickname
@@ -1188,7 +1192,10 @@
                 /* alternateContentDescription = */ null, /* shouldApplyColor = */ true,
                 /* isEditable = */ false, /* EntryContextMenuInfo = */ null,
                 /* thirdIcon = */ null, /* thirdIntent = */ null,
-                /* thirdContentDescription = */ null, R.drawable.ic_phone_24dp);
+                /* thirdContentDescription = */ null,
+                /* thirdAction = */ Entry.ACTION_NONE,
+                /* thirdExtras = */ null,
+                R.drawable.ic_phone_24dp);
 
         final Drawable emailIcon = getResources().getDrawable(
                 R.drawable.ic_email_24dp).mutate();
@@ -1201,6 +1208,7 @@
                 /* shouldApplyColor = */ true, /* isEditable = */ false,
                 /* EntryContextMenuInfo = */ null, /* thirdIcon = */ null,
                 /* thirdIntent = */ null, /* thirdContentDescription = */ null,
+                /* thirdAction = */ Entry.ACTION_NONE, /* thirdExtras = */ null,
                 R.drawable.ic_email_24dp);
 
         final List<List<Entry>> promptEntries = new ArrayList<>();
@@ -1367,7 +1375,9 @@
         EntryContextMenuInfo entryContextMenuInfo = null;
         Drawable thirdIcon = null;
         Intent thirdIntent = null;
+        int thirdAction = Entry.ACTION_NONE;
         String thirdContentDescription = null;
+        Bundle thirdExtras = null;
         int iconResourceId = 0;
 
         context = context.getApplicationContext();
@@ -1480,6 +1490,7 @@
             }
         } else if (dataItem instanceof PhoneDataItem) {
             final PhoneDataItem phone = (PhoneDataItem) dataItem;
+            String phoneLabel = null;
             if (!TextUtils.isEmpty(phone.getNumber())) {
                 primaryContentDescription.append(res.getString(R.string.call_other)).append(" ");
                 header = sBidiFormatter.unicodeWrap(phone.buildDataStringForDisplay(context, kind),
@@ -1490,10 +1501,12 @@
                 if (phone.hasKindTypeColumn(kind)) {
                     final int kindTypeColumn = phone.getKindTypeColumn(kind);
                     final String label = phone.getLabel();
+                    phoneLabel = label;
                     if (kindTypeColumn == Phone.TYPE_CUSTOM && TextUtils.isEmpty(label)) {
                         text = "";
                     } else {
                         text = Phone.getTypeLabel(res, kindTypeColumn, label).toString();
+                        phoneLabel= text;
                         primaryContentDescription.append(text).append(" ");
                     }
                 }
@@ -1509,9 +1522,33 @@
                 alternateIcon = res.getDrawable(R.drawable.ic_message_24dp);
                 alternateContentDescription.append(res.getString(R.string.sms_custom, header));
 
-                // Add video call button if supported
-                if (CallUtil.isVideoEnabled(context)) {
+                if (CallUtil.isCallWithSubjectSupported(context)) {
+                    thirdIcon = res.getDrawable(R.drawable.ic_call_note_white_24dp);
+                    thirdAction = Entry.ACTION_CALL_WITH_SUBJECT;
+                    thirdContentDescription =
+                            res.getString(R.string.call_with_a_note);
+
+                    // Create a bundle containing the data the call subject dialog requires.
+                    thirdExtras = new Bundle();
+                    thirdExtras.putLong(CallSubjectDialog.ARG_PHOTO_ID,
+                            contactData.getPhotoId());
+                    thirdExtras.putParcelable(CallSubjectDialog.ARG_PHOTO_URI,
+                            UriUtils.parseUriOrNull(contactData.getPhotoUri()));
+                    thirdExtras.putParcelable(CallSubjectDialog.ARG_CONTACT_URI,
+                            contactData.getLookupUri());
+                    thirdExtras.putString(CallSubjectDialog.ARG_NAME_OR_NUMBER,
+                            contactData.getDisplayName());
+                    thirdExtras.putBoolean(CallSubjectDialog.ARG_IS_BUSINESS, false);
+                    thirdExtras.putString(CallSubjectDialog.ARG_NUMBER,
+                            phone.getNumber());
+                    thirdExtras.putString(CallSubjectDialog.ARG_DISPLAY_NUMBER,
+                            phone.getFormattedPhoneNumber());
+                    thirdExtras.putString(CallSubjectDialog.ARG_NUMBER_LABEL,
+                            phoneLabel);
+                } else if (CallUtil.isVideoEnabled(context)) {
+                    // Add video call button if supported
                     thirdIcon = res.getDrawable(R.drawable.ic_videocam);
+                    thirdAction = Entry.ACTION_INTENT;
                     thirdIntent = CallUtil.getVideoCallIntent(phone.getNumber(),
                             CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY);
                     thirdContentDescription =
@@ -1708,8 +1745,8 @@
                 new SpannableString(primaryContentDescription.toString()),
                 intent, alternateIcon, alternateIntent,
                 alternateContentDescription.toString(), shouldApplyColor, isEditable,
-                entryContextMenuInfo, thirdIcon, thirdIntent, thirdContentDescription,
-                iconResourceId);
+                entryContextMenuInfo, thirdIcon, thirdIntent, thirdContentDescription, thirdAction,
+                thirdExtras, iconResourceId);
     }
 
     private List<Entry> dataItemsToEntries(List<DataItem> dataItems,
@@ -1980,6 +2017,8 @@
                     /* thirdIcon = */ null,
                     /* thirdIntent = */ null,
                     /* thirdContentDescription = */ null,
+                    /* thirdAction = */ Entry.ACTION_NONE,
+                    /* thirdActionExtras = */ null,
                     interaction.getIconResourceId()));
         }
         return entries;
@@ -2275,11 +2314,8 @@
         final String lookupKey = mContactData.getLookupKey();
         final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
         final Intent intent = new Intent(Intent.ACTION_SEND);
-        // Even though the data doesn't need to be set for ACTION_SEND, it does need
-        // to be set so that FLAG_GRANT_READ_URI_PERMISSION can create a URI permission grant.
-        intent.setDataAndType(shareUri, Contacts.CONTENT_VCARD_TYPE);
+        intent.setType(Contacts.CONTENT_VCARD_TYPE);
         intent.putExtra(Intent.EXTRA_STREAM, shareUri);
-        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 
         // Launch chooser to share contact via
         final CharSequence chooseTitle = getText(R.string.share_via);