Merge "Set read only display name as primary in fragment not view (DN 2/2)" into ub-contactsdialer-a-dev
diff --git a/src/com/android/contacts/editor/CompactContactEditorFragment.java b/src/com/android/contacts/editor/CompactContactEditorFragment.java
index 9698ada..54ed0f2 100644
--- a/src/com/android/contacts/editor/CompactContactEditorFragment.java
+++ b/src/com/android/contacts/editor/CompactContactEditorFragment.java
@@ -30,6 +30,7 @@
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.Bundle;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.MenuItem;
@@ -98,9 +99,10 @@
         final CompactRawContactsEditorView editorView = getContent();
         editorView.setListener(this);
         editorView.setState(mState, getMaterialPalette(), mViewIdGenerator, mPhotoId,
-                mReadOnlyDisplayName, mHasNewContact, mIsUserProfile, mAccountWithDataSet);
-        if (mReadOnlyDisplayName != null) {
+                mHasNewContact, mIsUserProfile, mAccountWithDataSet);
+        if (mHasNewContact && !TextUtils.isEmpty(mReadOnlyDisplayName)) {
             mReadOnlyNameEditorView = editorView.getPrimaryNameEditorView();
+            editorView.maybeSetReadOnlyDisplayNameAsPrimary(mReadOnlyDisplayName);
         }
 
         // Set up the photo widget
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index 1eba880..d6ffdb9 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -324,7 +324,6 @@
     private ViewIdGenerator mViewIdGenerator;
     private MaterialColorMapUtils.MaterialPalette mMaterialPalette;
     private long mPhotoId = -1;
-    private String mReadOnlyDisplayName;
     private boolean mHasNewContact;
     private boolean mIsUserProfile;
     private AccountWithDataSet mPrimaryAccount;
@@ -357,7 +356,6 @@
     private ValuesDelta mPhotoValuesDelta;
 
     private Pair<KindSectionData, ValuesDelta> mPrimaryNameKindSectionData;
-    private StructuredNameEditorView mPrimaryNameEditorView;
 
     public CompactRawContactsEditorView(Context context) {
         super(context);
@@ -515,7 +513,9 @@
     }
 
     public StructuredNameEditorView getPrimaryNameEditorView() {
-        return mPrimaryNameEditorView;
+        final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
+        return primaryNameKindSectionView == null
+                ? null : primaryNameKindSectionView.getPrimaryNameEditorView();
     }
 
     /**
@@ -614,8 +614,8 @@
 
     public void setState(RawContactDeltaList rawContactDeltas,
             MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator,
-            long photoId, String readOnlyDisplayName, boolean hasNewContact,
-            boolean isUserProfile, AccountWithDataSet primaryAccount) {
+            long photoId, boolean hasNewContact, boolean isUserProfile,
+            AccountWithDataSet primaryAccount) {
         mKindSectionDataMap.clear();
         mKindSectionViews.removeAllViews();
         mMoreFields.setVisibility(View.VISIBLE);
@@ -623,7 +623,6 @@
         mMaterialPalette = materialPalette;
         mViewIdGenerator = viewIdGenerator;
         mPhotoId = photoId;
-        mReadOnlyDisplayName = readOnlyDisplayName;
 
         mHasNewContact = hasNewContact;
         mIsUserProfile = isUserProfile;
@@ -667,9 +666,7 @@
         addAccountInfo(rawContactDeltas);
         addPhotoView();
         addKindSectionViews();
-        if (mHasNewContact) {
-            maybeCopyPrimaryDisplayName();
-        }
+
         if (mIsExpanded) showAllFields();
 
         if (mListener != null) mListener.onEditorsBound();
@@ -1055,17 +1052,20 @@
         return kindSectionView;
     }
 
-    private void maybeCopyPrimaryDisplayName() {
-        if (TextUtils.isEmpty(mReadOnlyDisplayName)) return;
+    void maybeSetReadOnlyDisplayNameAsPrimary(String readOnlyDisplayName) {
+        if (TextUtils.isEmpty(readOnlyDisplayName)) return;
+        final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
+        if (primaryNameKindSectionView != null && primaryNameKindSectionView.isEmptyName()) {
+            vlog("name: using read only display name as primary name");
+            primaryNameKindSectionView.setName(readOnlyDisplayName);
+        }
+    }
+
+    private CompactKindSectionView getPrimaryNameKindSectionView() {
         final List<CompactKindSectionView> kindSectionViews
                 = mKindSectionViewsMap.get(StructuredName.CONTENT_ITEM_TYPE);
-        if (kindSectionViews == null || 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();
-        }
+        return kindSectionViews == null || kindSectionViews.isEmpty()
+                ? null : kindSectionViews.get(0);
     }
 
     private void showAllFields() {
diff --git a/src/com/android/contacts/editor/ContactEditorBaseFragment.java b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
index 320ebe9..633ee34 100644
--- a/src/com/android/contacts/editor/ContactEditorBaseFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
@@ -65,6 +65,7 @@
 import android.provider.ContactsContract.CommonDataKinds.Event;
 import android.provider.ContactsContract.CommonDataKinds.Organization;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
+import android.provider.ContactsContract.CommonDataKinds.StructuredName;
 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.Intents;
@@ -83,7 +84,9 @@
 import android.widget.Toast;
 
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 /**
  * Base Fragment for contact editors.
@@ -145,6 +148,8 @@
     // Join Activity
     private static final String KEY_CONTACT_ID_FOR_JOIN = "contactidforjoin";
 
+    private static final String KEY_READ_ONLY_DISPLAY_NAME = "readOnlyDisplayName";
+
     protected static final int REQUEST_CODE_JOIN = 0;
     protected static final int REQUEST_CODE_ACCOUNTS_CHANGED = 1;
     protected static final int REQUEST_CODE_PICK_RINGTONE = 2;
@@ -380,13 +385,13 @@
     // Join Activity
     protected long mContactIdForJoin;
 
+    // Used to pre-populate the editor with a display name when a user edits a read-only contact.
+    protected String mReadOnlyDisplayName;
+
     //
     // 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;
@@ -515,6 +520,8 @@
 
             // Join Activity
             mContactIdForJoin = savedState.getLong(KEY_CONTACT_ID_FOR_JOIN);
+
+            mReadOnlyDisplayName = savedState.getString(KEY_READ_ONLY_DISPLAY_NAME);
         }
 
         // mState can still be null because it may not have have finished loading before
@@ -634,6 +641,8 @@
         // Join Activity
         outState.putLong(KEY_CONTACT_ID_FOR_JOIN, mContactIdForJoin);
 
+        outState.putString(KEY_READ_ONLY_DISPLAY_NAME, mReadOnlyDisplayName);
+
         super.onSaveInstanceState(outState);
     }
 
@@ -972,9 +981,9 @@
      * Return true if there are any edits to the current contact which need to
      * be saved.
      */
-    protected boolean hasPendingRawContactChanges() {
+    protected boolean hasPendingRawContactChanges(Set<String> excludedMimeTypes) {
         final AccountTypeManager accountTypes = AccountTypeManager.getInstance(mContext);
-        return RawContactModifier.hasChanges(mState, accountTypes);
+        return RawContactModifier.hasChanges(mState, accountTypes, excludedMimeTypes);
     }
 
     /**
@@ -996,28 +1005,18 @@
      * 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;
+        if (mReadOnlyNameEditorView != null && mReadOnlyDisplayName != null) {
+            // 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)) {
+                final Set<String> excludedMimeTypes = new HashSet<>();
+                excludedMimeTypes.add(StructuredName.CONTENT_ITEM_TYPE);
+                return hasPendingRawContactChanges(excludedMimeTypes);
             }
+            return true;
         }
-        return true;
+        return hasPendingRawContactChanges(/* excludedMimeTypes =*/ null);
     }
 
     /**