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/CompactContactEditorFragment.java b/src/com/android/contacts/editor/CompactContactEditorFragment.java
index a0e0c9e..a000309 100644
--- a/src/com/android/contacts/editor/CompactContactEditorFragment.java
+++ b/src/com/android/contacts/editor/CompactContactEditorFragment.java
@@ -21,17 +21,12 @@
 import com.android.contacts.activities.CompactContactEditorActivity;
 import com.android.contacts.activities.ContactEditorActivity;
 import com.android.contacts.activities.ContactEditorBaseActivity;
-import com.android.contacts.common.model.AccountTypeManager;
 import com.android.contacts.common.model.RawContactDelta;
-import com.android.contacts.common.model.RawContactDeltaList;
 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.detail.PhotoSelectionHandler;
 import com.android.contacts.util.ContactPhotoUtils;
 
 import android.app.Activity;
-import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.net.Uri;
@@ -114,7 +109,10 @@
         final CompactRawContactsEditorView editorView = getContent();
         editorView.setListener(this);
         editorView.setState(mState, getMaterialPalette(), mViewIdGenerator, mPhotoId,
-                mHasNewContact, mIsUserProfile, mAccountWithDataSet);
+                mReadOnlyDisplayName, mHasNewContact, mIsUserProfile, mAccountWithDataSet);
+        if (mReadOnlyDisplayName != null) {
+            mReadOnlyNameEditorView = editorView.getPrimaryNameEditorView();
+        }
 
         // Set up the photo widget
         editorView.setPhotoListener(this);
diff --git a/src/com/android/contacts/editor/CompactKindSectionView.java b/src/com/android/contacts/editor/CompactKindSectionView.java
index 91e5a6d..3ebe27b 100644
--- a/src/com/android/contacts/editor/CompactKindSectionView.java
+++ b/src/com/android/contacts/editor/CompactKindSectionView.java
@@ -18,7 +18,9 @@
 
 import android.content.Context;
 import android.database.Cursor;
+import android.provider.ContactsContract.CommonDataKinds.Event;
 import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
+import android.provider.ContactsContract.CommonDataKinds.Nickname;
 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
 import android.util.AttributeSet;
 import android.view.LayoutInflater;
@@ -30,6 +32,7 @@
 
 import com.android.contacts.R;
 import com.android.contacts.common.model.RawContactDelta;
+import com.android.contacts.common.model.RawContactDeltaList;
 import com.android.contacts.common.model.RawContactModifier;
 import com.android.contacts.common.model.ValuesDelta;
 import com.android.contacts.common.model.account.AccountType;
@@ -216,10 +219,65 @@
     }
 
     /**
+     * Whether this is a name kind section view and all name fields (structured, phonetic,
+     * and nicknames) are empty.
+     */
+    public boolean isEmptyName() {
+        if (!StructuredName.CONTENT_ITEM_TYPE.equals(mKindSectionDataList.getMimeType())) {
+            return false;
+        }
+        for (int i = 0; i < mEditors.getChildCount(); i++) {
+            final View view = mEditors.getChildAt(i);
+            if (view instanceof Editor) {
+                final Editor editor = (Editor) view;
+                if (!editor.isEmpty()) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Sets the given display name as the structured name as if the user input it, but
+     * without informing editor listeners.
+     */
+    public void setName(String displayName) {
+        if (!StructuredName.CONTENT_ITEM_TYPE.equals(mKindSectionDataList.getMimeType())) {
+            return;
+        }
+        for (int i = 0; i < mEditors.getChildCount(); i++) {
+            final View view = mEditors.getChildAt(i);
+            if (view instanceof StructuredNameEditorView) {
+                final StructuredNameEditorView editor = (StructuredNameEditorView) view;
+
+                // Detach listeners since so we don't show suggested aggregations
+                final Editor.EditorListener editorListener = editor.getEditorListener();
+                editor.setEditorListener(null);
+
+                editor.setDisplayName(displayName);
+
+                // Reattach listeners
+                editor.setEditorListener(editorListener);
+
+                return;
+            }
+        }
+    }
+
+    public StructuredNameEditorView getPrimaryNameEditorView() {
+        if (!StructuredName.CONTENT_ITEM_TYPE.equals(mKindSectionDataList.getMimeType())
+            || mEditors.getChildCount() == 0) {
+            return null;
+        }
+        return (StructuredNameEditorView) mEditors.getChildAt(0);
+    }
+
+    /**
      * Binds views for the given {@link KindSectionData} list.
      *
      * We create a structured name and phonetic name editor for each {@link DataKind} with a
-     * {@link }StructuredName#CONTENT_ITEM_TYPE} mime type.  The number and order of editors are
+     * {@link StructuredName#CONTENT_ITEM_TYPE} mime type.  The number and order of editors are
      * rendered as they are given to {@link #setState}.
      *
      * Empty name editors are never added and at least one structured name editor is always
@@ -262,9 +320,9 @@
                         kindSectionData.getDataKind());
             } else {
                 final Editor.EditorListener editorListener;
-                if (kindSectionData.isNicknameDataKind()) {
+                if (Nickname.CONTENT_ITEM_TYPE.equals(kindSectionData.getDataKind().mimeType)) {
                     editorListener = new OtherNameKindEditorListener();
-                } else if (kindSectionData.isEventDataKind()) {
+                } else if (Event.CONTENT_ITEM_TYPE.equals(kindSectionData.getDataKind().mimeType)) {
                     editorListener = new EventEditorListener();
                 } else {
                     editorListener = new NonNameEditorListener();
@@ -386,7 +444,6 @@
         if (isNameKindSection) {
             // The name kind section is always visible
             setVisibility(VISIBLE);
-
             updateEmptyNameEditors(shouldAnimate);
         } else if (isGroupKindSection) {
             // Check whether metadata has been bound for all group views
@@ -425,29 +482,37 @@
 
         for (int i = 0; i < mEditors.getChildCount(); i++) {
             final View view = mEditors.getChildAt(i);
-            if (!(view instanceof Editor)) continue; // Skip read-only names
-            final Editor editor = (Editor) view;
-            if (view instanceof StructuredNameEditorView) {
-                // We always show one empty structured name view
-                if (editor.isEmpty()) {
-                    if (isEmptyNameEditorVisible) {
-                        // If we're already showing an empty editor then hide any other empties
-                        if (mHideIfEmpty) {
-                            view.setVisibility(View.GONE);
+            if (view instanceof Editor) {
+                final Editor editor = (Editor) view;
+                if (view instanceof StructuredNameEditorView) {
+                    // We always show one empty structured name view
+                    if (editor.isEmpty()) {
+                        if (isEmptyNameEditorVisible) {
+                            // If we're already showing an empty editor then hide any other empties
+                            if (mHideIfEmpty) {
+                                view.setVisibility(View.GONE);
+                            }
+                        } else {
+                            isEmptyNameEditorVisible = true;
                         }
                     } else {
+                        showView(view, shouldAnimate);
                         isEmptyNameEditorVisible = true;
                     }
                 } else {
-                    showView(view, shouldAnimate);
-                    isEmptyNameEditorVisible = true;
+                    // Since we can't add phonetic names and nicknames, just show or hide them
+                    if (mHideIfEmpty && editor.isEmpty()) {
+                        hideView(view);
+                    } else {
+                        showView(view, /* shouldAnimate =*/ false); // Animation here causes jank
+                    }
                 }
             } else {
-                // For phonetic names and nicknames, which can't be added, just show or hide them
-                if (mHideIfEmpty && editor.isEmpty()) {
+                // For read only names, only show them if we're not hiding empty views
+                if (mHideIfEmpty) {
                     hideView(view);
                 } else {
-                    showView(view, /* shouldAnimate =*/ false); // Animation here causes jank
+                    showView(view, shouldAnimate);
                 }
             }
         }
@@ -488,8 +553,9 @@
             final RawContactDelta rawContactDelta =
                     mKindSectionDataList.get(0).getRawContactDelta();
             final ValuesDelta values = RawContactModifier.insertChild(rawContactDelta, dataKind);
-            final Editor.EditorListener editorListener = mKindSectionDataList.get(0)
-                    .isEventDataKind() ? new EventEditorListener() : new NonNameEditorListener();
+            final String mimeType = mKindSectionDataList.getMimeType();
+            final Editor.EditorListener editorListener = Event.CONTENT_ITEM_TYPE.equals(mimeType)
+                    ? new EventEditorListener() : new NonNameEditorListener();
             final View view = addNonNameEditorView(rawContactDelta, dataKind, values,
                     editorListener);
             showView(view, shouldAnimate);
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++) {
diff --git a/src/com/android/contacts/editor/ContactEditorBaseFragment.java b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
index c5e6afd..4ea6c15 100644
--- a/src/com/android/contacts/editor/ContactEditorBaseFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
@@ -363,6 +363,17 @@
     // Join Activity
     protected long mContactIdForJoin;
 
+    //
+    // Not saved/restored on rotates
+    //
+
+    // Used to pre-populate the editor with a display name when a user edits a read-only contact.
+    protected String mReadOnlyDisplayName;
+
+    // The name editor view for the new raw contact that was created so that the user can
+    // edit a read-only contact (to which the new raw contact was joined)
+    protected StructuredNameEditorView mReadOnlyNameEditorView;
+
     /**
      * The contact data loader listener.
      */
@@ -946,12 +957,42 @@
      * Return true if there are any edits to the current contact which need to
      * be saved.
      */
-    protected boolean hasPendingChanges() {
+    protected boolean hasPendingRawContactChanges() {
         final AccountTypeManager accountTypes = AccountTypeManager.getInstance(mContext);
         return RawContactModifier.hasChanges(mState, accountTypes);
     }
 
     /**
+     * Determines if changes were made in the editor that need to be saved, while taking into
+     * account that name changes are not real for read-only contacts.
+     * See go/editing-read-only-contacts
+     */
+    protected boolean hasPendingChanges() {
+        if (mReadOnlyNameEditorView == null || mReadOnlyDisplayName == null) {
+            return hasPendingRawContactChanges();
+        }
+        // We created a new raw contact delta with a default display name.
+        // We must test for pending changes while ignoring the default display name.
+        final String displayName = mReadOnlyNameEditorView.getDisplayName();
+        if (mReadOnlyDisplayName.equals(displayName)) {
+            // The user did not modify the default display name, erase it and
+            // check if the user made any other changes
+            mReadOnlyNameEditorView.clearAllFields();
+            if (hasPendingRawContactChanges()) {
+                // Other changes were made to the aggregate contact, restore
+                // the display name and proceed.
+                mReadOnlyNameEditorView.setDisplayName(displayName);
+                return true;
+            } else {
+                // No other changes were made to the aggregate contact. Don't add back
+                // the displayName so that a "bogus" contact is not created.
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
      * Whether editor inputs and the options menu should be enabled.
      */
     protected boolean isEnabled() {
@@ -1061,6 +1102,7 @@
             }
         }
 
+        String readOnlyDisplayName = null;
         // Check for writable raw contacts.  If there are none, then we need to create one so user
         // can edit.  For the user profile case, there is already an editable contact.
         if (!contact.isUserProfile() && !contact.isWritableContact(mContext)) {
@@ -1068,11 +1110,13 @@
 
             // This is potentially an asynchronous call and will add deltas to list.
             selectAccountAndCreateContact();
+
+            readOnlyDisplayName = contact.getDisplayName();
         }
 
         // This also adds deltas to list.  If readOnlyDisplayName is null at this point it is
         // simply ignored later on by the editor.
-        setStateForExistingContact(contact.isUserProfile(), mRawContacts);
+        setStateForExistingContact(readOnlyDisplayName, contact.isUserProfile(), mRawContacts);
     }
 
     /**
@@ -1143,9 +1187,10 @@
     /**
      * Prepare {@link #mState} for an existing contact.
      */
-    protected void setStateForExistingContact(boolean isUserProfile,
+    protected void setStateForExistingContact(String readOnlyDisplayName, boolean isUserProfile,
             ImmutableList<RawContact> rawContacts) {
         setEnabled(true);
+        mReadOnlyDisplayName = readOnlyDisplayName;
 
         mState.addAll(rawContacts.iterator());
         setIntentExtras(mIntentExtras);
@@ -1257,7 +1302,8 @@
             setStateForNewContact(newAccount, newAccountType, oldState, oldAccountType,
                     isEditingUserProfile());
             if (mIsEdit) {
-                setStateForExistingContact(isEditingUserProfile(), mRawContacts);
+                setStateForExistingContact(mReadOnlyDisplayName, isEditingUserProfile(),
+                        mRawContacts);
             }
         }
     }
diff --git a/src/com/android/contacts/editor/KindSectionData.java b/src/com/android/contacts/editor/KindSectionData.java
index 6c33601..8921099 100644
--- a/src/com/android/contacts/editor/KindSectionData.java
+++ b/src/com/android/contacts/editor/KindSectionData.java
@@ -97,18 +97,6 @@
         return mDataKind;
     }
 
-    public boolean isNameDataKind() {
-        return StructuredName.CONTENT_ITEM_TYPE.equals(mDataKind.mimeType);
-    }
-
-    public boolean isNicknameDataKind() {
-        return Nickname.CONTENT_ITEM_TYPE.equals(mDataKind.mimeType);
-    }
-
-    public boolean isEventDataKind() {
-        return Event.CONTENT_ITEM_TYPE.equals(mDataKind.mimeType);
-    }
-
     public RawContactDelta getRawContactDelta() {
         return mRawContactDelta;
     }
diff --git a/src/com/android/contacts/editor/KindSectionDataList.java b/src/com/android/contacts/editor/KindSectionDataList.java
index 180e8b2..3658f21 100644
--- a/src/com/android/contacts/editor/KindSectionDataList.java
+++ b/src/com/android/contacts/editor/KindSectionDataList.java
@@ -36,7 +36,7 @@
  */
 public class KindSectionDataList extends ArrayList<KindSectionData> {
 
-    private static final String TAG = "CompactEditorView";
+    private static final String TAG = CompactRawContactsEditorView.TAG;
 
     /**
      * Returns the mime type for all DataKinds in this List.
diff --git a/src/com/android/contacts/editor/LabeledEditorView.java b/src/com/android/contacts/editor/LabeledEditorView.java
index 244b682..b68310a 100644
--- a/src/com/android/contacts/editor/LabeledEditorView.java
+++ b/src/com/android/contacts/editor/LabeledEditorView.java
@@ -380,7 +380,9 @@
             mWasEmpty = isEmpty;
 
             // Update the label text color
-            mEditTypeAdapter.notifyDataSetChanged();
+            if (mEditTypeAdapter != null) {
+                mEditTypeAdapter.notifyDataSetChanged();
+            }
         }
     }
 
diff --git a/src/com/android/contacts/editor/StructuredNameEditorView.java b/src/com/android/contacts/editor/StructuredNameEditorView.java
index 4cc8003..23cb4c1 100644
--- a/src/com/android/contacts/editor/StructuredNameEditorView.java
+++ b/src/com/android/contacts/editor/StructuredNameEditorView.java
@@ -225,6 +225,22 @@
         super.setValue(0, name);
     }
 
+    /**
+     * Returns the display name currently displayed in the editor.
+     */
+    public String getDisplayName() {
+        final ValuesDelta valuesDelta = getValues();
+        if (hasShortAndLongForms() && areOptionalFieldsVisible()) {
+            final Map<String, String> structuredNameMap = valuesToStructuredNameMap(valuesDelta);
+            final String displayName = NameConverter.structuredNameToDisplayName(
+                    getContext(), structuredNameMap);
+            if (!TextUtils.isEmpty(displayName)) {
+                return displayName;
+            }
+        }
+        return valuesDelta.getDisplayName();
+    }
+
     @Override
     protected Parcelable onSaveInstanceState() {
         SavedState state = new SavedState(super.onSaveInstanceState());