Add a name to raw contacts created to edit read-only contacts (E17)

This is a revert of the parts of ag/771915 (E5) where we changed
to moved to always showing all of the names.

Bug 24509375
Bug 23589603

Change-Id: Ief4773592233dc05b40bae8fb425d1c258d0b68f
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index 40574da..091f1cb 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -84,7 +84,7 @@
  */
 public class CompactRawContactsEditorView extends LinearLayout implements View.OnClickListener {
 
-    private static final String TAG = "CompactEditorView";
+    static final String TAG = "CompactEditorView";
 
     private static final KindSectionDataMapEntryComparator
             KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR = new KindSectionDataMapEntryComparator();
@@ -287,9 +287,9 @@
      */
     private static final class NameEditorComparator implements Comparator<KindSectionData> {
 
-        private RawContactDeltaComparator mRawContactDeltaComparator;
-        private MimeTypeComparator mMimeTypeComparator;
-        private RawContactDelta mPrimaryRawContactDelta;
+        private final RawContactDeltaComparator mRawContactDeltaComparator;
+        private final MimeTypeComparator mMimeTypeComparator;
+        private final RawContactDelta mPrimaryRawContactDelta;
 
         private NameEditorComparator(Context context, RawContactDelta primaryRawContactDelta) {
             mRawContactDeltaComparator = new RawContactDeltaComparator(context);
@@ -368,6 +368,7 @@
     private ViewIdGenerator mViewIdGenerator;
     private MaterialColorMapUtils.MaterialPalette mMaterialPalette;
     private long mPhotoId;
+    private String mReadOnlyDisplayName;
     private boolean mHasNewContact;
     private boolean mIsUserProfile;
     private AccountWithDataSet mPrimaryAccount;
@@ -399,6 +400,7 @@
 
     private long mPhotoRawContactId;
     private ValuesDelta mPhotoValuesDelta;
+    private StructuredNameEditorView mPrimaryNameEditorView;
 
     public CompactRawContactsEditorView(Context context) {
         super(context);
@@ -540,6 +542,10 @@
         return mPhotoRawContactId;
     }
 
+    public StructuredNameEditorView getPrimaryNameEditorView() {
+        return mPrimaryNameEditorView;
+    }
+
     /**
      * Returns a data holder for every non-default/non-empty photo from each raw contact, whether
      * the raw contact is writable or not.
@@ -634,8 +640,8 @@
 
     public void setState(RawContactDeltaList rawContactDeltas,
             MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator,
-            long photoId, boolean hasNewContact, boolean isUserProfile,
-            AccountWithDataSet primaryAccount) {
+            long photoId, String readOnlyDisplayName, boolean hasNewContact,
+            boolean isUserProfile, AccountWithDataSet primaryAccount) {
         mKindSectionDataMap.clear();
         mKindSectionViews.removeAllViews();
         mMoreFields.setVisibility(View.VISIBLE);
@@ -643,6 +649,7 @@
         mMaterialPalette = materialPalette;
         mViewIdGenerator = viewIdGenerator;
         mPhotoId = photoId;
+        mReadOnlyDisplayName = readOnlyDisplayName;
         mHasNewContact = hasNewContact;
         mIsUserProfile = isUserProfile;
         mPrimaryAccount = primaryAccount;
@@ -678,10 +685,10 @@
         addAccountInfo(rawContactDeltas);
         addPhotoView();
         addKindSectionViews();
-
-        if (mIsExpanded) {
-            showAllFields();
+        if (mHasNewContact) {
+            maybeCopyPrimaryDisplayName();
         }
+        if (mIsExpanded) showAllFields();
 
         if (mListener != null) mListener.onEditorsBound();
     }
@@ -1021,6 +1028,19 @@
         return kindSectionView;
     }
 
+    private void maybeCopyPrimaryDisplayName() {
+        if (TextUtils.isEmpty(mReadOnlyDisplayName)) return;
+        final List<CompactKindSectionView> kindSectionViews
+                = mKindSectionViewsMap.get(StructuredName.CONTENT_ITEM_TYPE);
+        if (kindSectionViews.isEmpty()) return;
+        final CompactKindSectionView primaryNameKindSectionView = kindSectionViews.get(0);
+        if (primaryNameKindSectionView.isEmptyName()) {
+            vlog("name: using read only display name as primary name");
+            primaryNameKindSectionView.setName(mReadOnlyDisplayName);
+            mPrimaryNameEditorView = primaryNameKindSectionView.getPrimaryNameEditorView();
+        }
+    }
+
     private void showAllFields() {
         // Stop hiding empty editors and allow the user to enter values for all kinds now
         for (int i = 0; i < mKindSectionViews.getChildCount(); i++) {