Pass the preferred name ID into the contact editor

Bug 21870691

Change-Id: I99cc6bf8d90f32469ca7cf105567dcc3edf48e9f
diff --git a/src/com/android/contacts/activities/ContactEditorBaseActivity.java b/src/com/android/contacts/activities/ContactEditorBaseActivity.java
index eaf607d..fe09bec 100644
--- a/src/com/android/contacts/activities/ContactEditorBaseActivity.java
+++ b/src/com/android/contacts/activities/ContactEditorBaseActivity.java
@@ -165,7 +165,8 @@
          * Invoked after the contact is saved.
          */
         void onSaveCompleted(boolean hadChanges, int saveMode, boolean saveSucceeded,
-                Uri contactLookupUri, Bundle updatedPhotos, boolean backPressed, long photoId);
+                Uri contactLookupUri, Bundle updatedPhotos, boolean backPressed, long photoId,
+                long nameId);
 
         /**
          * Invoked after the contact is joined.
@@ -256,7 +257,8 @@
                     (Bundle) intent.getParcelableExtra(ContactSaveService.EXTRA_UPDATED_PHOTOS),
                     intent.getBooleanExtra(ContactEditorFragment.INTENT_EXTRA_SAVE_BACK_PRESSED,
                             false),
-                    intent.getLongExtra(ContactEditorFragment.INTENT_EXTRA_PHOTO_ID, -1));
+                    intent.getLongExtra(ContactEditorFragment.INTENT_EXTRA_PHOTO_ID, -1),
+                    intent.getLongExtra(ContactEditorFragment.INTENT_EXTRA_NAME_ID, -1));
         } else if (ACTION_JOIN_COMPLETED.equals(action)) {
             mFragment.onJoinCompleted(intent.getData());
         }
diff --git a/src/com/android/contacts/activities/ContactSelectionActivity.java b/src/com/android/contacts/activities/ContactSelectionActivity.java
index 0e78561..36e805c 100644
--- a/src/com/android/contacts/activities/ContactSelectionActivity.java
+++ b/src/com/android/contacts/activities/ContactSelectionActivity.java
@@ -427,7 +427,8 @@
             } else {
                 // Otherwise launch the full contact editor.
                 startActivityAndForwardResult(EditorIntents.createEditContactIntent(
-                        contactLookupUri, /* materialPalette =*/ null, /* photoId =*/ -1));
+                        contactLookupUri, /* materialPalette =*/ null, /* photoId =*/ -1,
+                        /* nameId =*/ -1));
             }
         }
 
diff --git a/src/com/android/contacts/editor/CompactContactEditorFragment.java b/src/com/android/contacts/editor/CompactContactEditorFragment.java
index 1620ed1..9eecaab 100644
--- a/src/com/android/contacts/editor/CompactContactEditorFragment.java
+++ b/src/com/android/contacts/editor/CompactContactEditorFragment.java
@@ -201,7 +201,7 @@
         // Add input fields for the loaded Contact
         final CompactRawContactsEditorView editorView = getContent();
         editorView.setListener(this);
-        editorView.setState(mState, getMaterialPalette(), mViewIdGenerator, mPhotoId);
+        editorView.setState(mState, getMaterialPalette(), mViewIdGenerator, mPhotoId, mNameId);
 
         // Set up the photo widget
         mPhotoHandler = createPhotoHandler();
@@ -355,7 +355,7 @@
                 ? EditorIntents.createInsertContactIntent(
                         mState, getDisplayName(), getPhoneticName(), mUpdatedPhotos)
                 : EditorIntents.createEditContactIntent(mLookupUri, getMaterialPalette(),
-                        mPhotoId);
+                        mPhotoId, mNameId);
         ImplicitIntentsUtil.startActivityInApp(getActivity(), intent);
 
         getActivity().finish();
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index df6cc3f..8c86189 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -256,7 +256,7 @@
 
     public void setState(RawContactDeltaList rawContactDeltas,
             MaterialColorMapUtils.MaterialPalette materialPalette,
-            ViewIdGenerator viewIdGenerator, long photoId) {
+            ViewIdGenerator viewIdGenerator, long photoId, long nameId) {
         mNames.removeAllViews();
         mPhoneticNames.removeAllViews();
         mNicknames.removeAllViews();
@@ -275,7 +275,7 @@
 
         vlog("Setting compact editor state from " + rawContactDeltas);
         addPhotoView(rawContactDeltas, viewIdGenerator, photoId);
-        addStructuredNameView(rawContactDeltas);
+        addStructuredNameView(rawContactDeltas, nameId);
         addEditorViews(rawContactDeltas);
         removeExtraEmptyTextFields(mPhoneNumbers);
         removeExtraEmptyTextFields(mEmails);
@@ -292,7 +292,6 @@
             RawContactModifier.ensureKindExists(
                     rawContactDelta, accountType, Photo.CONTENT_ITEM_TYPE);
 
-            // Look for a non-empty super primary photo
             final DataKind dataKind = accountType.getKindForMimetype(Photo.CONTENT_ITEM_TYPE);
             if (dataKind != null) {
                 for (ValuesDelta valuesDelta
@@ -365,7 +364,8 @@
         mPhoto.setVisibility(View.GONE);
     }
 
-    private void addStructuredNameView(RawContactDeltaList rawContactDeltas) {
+    private void addStructuredNameView(RawContactDeltaList rawContactDeltas, long nameId) {
+        // Look for a match for the photo ID that was passed in
         for (RawContactDelta rawContactDelta : rawContactDeltas) {
             if (!rawContactDelta.isVisible()) continue;
             final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
@@ -379,6 +379,28 @@
                     DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME);
             if (dataKind == null || !dataKind.editable) continue;
 
+            for (ValuesDelta valuesDelta : rawContactDelta.getMimeEntries(
+                    StructuredName.CONTENT_ITEM_TYPE)) {
+                if (valuesDelta != null && valuesDelta.getId() != null
+                        && valuesDelta.getId().equals(nameId)) {
+                    mNameValuesDelta = valuesDelta;
+                    final NameEditorListener nameEditorListener = new NameEditorListener(
+                            mNameValuesDelta, rawContactDelta.getRawContactId(), mListener);
+                    mNames.addView(inflateStructuredNameEditorView(mNames, accountType,
+                            mNameValuesDelta, rawContactDelta, nameEditorListener));
+                    return;
+                }
+            }
+        }
+        // Look for a super primary name
+        for (RawContactDelta rawContactDelta : rawContactDeltas) {
+            if (!rawContactDelta.isVisible()) continue;
+            final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
+
+            final DataKind dataKind = accountType.getKindForMimetype(
+                    DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME);
+            if (dataKind == null || !dataKind.editable) continue;
+
             final ValuesDelta superPrimaryValuesDelta = getNonEmptySuperPrimaryValuesDeltas(
                     rawContactDelta, StructuredName.CONTENT_ITEM_TYPE, dataKind);
             if (superPrimaryValuesDelta != null) {
diff --git a/src/com/android/contacts/editor/ContactEditorBaseFragment.java b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
index 65efb04..a103fd3 100644
--- a/src/com/android/contacts/editor/ContactEditorBaseFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
@@ -113,6 +113,7 @@
     private static final String KEY_NEW_LOCAL_PROFILE = "newLocalProfile";
     private static final String KEY_MATERIAL_PALETTE = "materialPalette";
     private static final String KEY_PHOTO_ID = "photoId";
+    private static final String KEY_NAME_ID = "nameId";
 
     private static final String KEY_VIEW_ID_GENERATOR = "viewidgenerator";
 
@@ -179,6 +180,11 @@
     public static final String INTENT_EXTRA_PHOTO_ID = "photo_id";
 
     /**
+     * Intent key to pass the ID of the name to display on the editor.
+     */
+    public static final String INTENT_EXTRA_NAME_ID = "name_id";
+
+    /**
      * Intent extra to specify a {@link ContactEditor.SaveMode}.
      */
     public static final String SAVE_MODE_EXTRA_KEY = "saveMode";
@@ -315,6 +321,7 @@
     protected boolean mNewLocalProfile;
     protected MaterialColorMapUtils.MaterialPalette mMaterialPalette;
     protected long mPhotoId = -1;
+    protected long mNameId = -1;
 
     //
     // Helpers
@@ -465,6 +472,7 @@
             mNewLocalProfile = savedState.getBoolean(KEY_NEW_LOCAL_PROFILE);
             mMaterialPalette = savedState.getParcelable(KEY_MATERIAL_PALETTE);
             mPhotoId = savedState.getLong(KEY_PHOTO_ID);
+            mNameId = savedState.getLong(KEY_NAME_ID);
 
             mRawContacts = ImmutableList.copyOf(savedState.<RawContact>getParcelableArrayList(
                     KEY_RAW_CONTACTS));
@@ -586,6 +594,7 @@
             outState.putParcelable(KEY_MATERIAL_PALETTE, mMaterialPalette);
         }
         outState.putLong(KEY_PHOTO_ID, mPhotoId);
+        outState.putLong(KEY_NAME_ID, mNameId);
 
         outState.putParcelable(KEY_VIEW_ID_GENERATOR, mViewIdGenerator);
 
@@ -917,7 +926,7 @@
             }
             onSaveCompleted(/* hadChanges =*/ false, saveMode,
                     /* saveSucceeded =*/ mLookupUri != null, mLookupUri,
-                    /* updatedPhotos =*/ null, backPressed, mPhotoId);
+                    /* updatedPhotos =*/ null, backPressed, mPhotoId, mNameId);
             return true;
         }
 
@@ -1308,6 +1317,7 @@
                 mUpdatedPhotos = mIntentExtras.getParcelable(INTENT_EXTRA_UPDATED_PHOTOS);
             }
             mPhotoId = mIntentExtras.getLong(INTENT_EXTRA_PHOTO_ID);
+            mNameId = mIntentExtras.getLong(INTENT_EXTRA_NAME_ID);
         }
     }
 
@@ -1331,12 +1341,13 @@
     @Override
     public void onJoinCompleted(Uri uri) {
         onSaveCompleted(false, SaveMode.RELOAD, uri != null, uri, /* updatedPhotos =*/ null,
-                /* backPressed =*/ false, /* photoId =*/ mPhotoId);
+                /* backPressed =*/ false, mPhotoId, mNameId);
     }
 
     @Override
     public void onSaveCompleted(boolean hadChanges, int saveMode, boolean saveSucceeded,
-            Uri contactLookupUri, Bundle updatedPhotos, boolean backPressed, long photoId) {
+            Uri contactLookupUri, Bundle updatedPhotos, boolean backPressed, long photoId,
+            long nameId) {
         if (hadChanges) {
             if (saveSucceeded) {
                 if (saveMode != SaveMode.JOIN) {
@@ -1373,7 +1384,8 @@
                             ? EditorIntents.createCompactInsertContactIntent(
                                     mState, getDisplayName(), getPhoneticName(), updatedPhotos)
                             : EditorIntents.createCompactEditContactIntent(
-                                    lookupUri, getMaterialPalette(), updatedPhotos, photoId);
+                                    lookupUri, getMaterialPalette(), updatedPhotos, photoId,
+                                    nameId);
                     resultIntent.putExtra(INTENT_EXTRA_SAVE_BACK_PRESSED, true);
                     mStatus = Status.CLOSING;
                     if (mListener != null) mListener.onSaveFinished(resultIntent);
diff --git a/src/com/android/contacts/editor/EditorIntents.java b/src/com/android/contacts/editor/EditorIntents.java
index b4e2fce..1ec6db9 100644
--- a/src/com/android/contacts/editor/EditorIntents.java
+++ b/src/com/android/contacts/editor/EditorIntents.java
@@ -44,11 +44,12 @@
      * existing contact.
      */
     public static Intent createCompactEditContactIntent(Uri contactLookupUri,
-            MaterialPalette materialPalette, Bundle updatedPhotos, long photoId) {
+            MaterialPalette materialPalette, Bundle updatedPhotos, long photoId, long nameId) {
         final Intent intent = new Intent(Intent.ACTION_EDIT, contactLookupUri);
         putMaterialPalette(intent, materialPalette);
         putUpdatedPhotos(intent, updatedPhotos);
         putPhotoId(intent, photoId);
+        putNameId(intent, nameId);
         return intent;
     }
 
@@ -97,11 +98,12 @@
      * new contact.
      */
     public static Intent createEditContactIntent(Uri contactLookupUri,
-            MaterialPalette materialPalette, long photoId) {
+            MaterialPalette materialPalette, long photoId, long nameId) {
         final Intent intent = new Intent(ContactEditorBaseActivity.ACTION_EDIT, contactLookupUri);
         addContactIntentFlags(intent);
         putMaterialPalette(intent, materialPalette);
         putPhotoId(intent, photoId);
+        putNameId(intent, nameId);
         return intent;
     }
 
@@ -144,6 +146,11 @@
         }
     }
 
+    private static void putNameId(Intent intent, long nameId) {
+        if (nameId >= 0) {
+            intent.putExtra(ContactEditorBaseFragment.INTENT_EXTRA_NAME_ID, nameId);
+        }
+    }
 
     private static void putRawContactDeltaValues(Intent intent,
             RawContactDeltaList rawContactDeltaList, String displayName, String phoneticName) {
diff --git a/src/com/android/contacts/quickcontact/QuickContactActivity.java b/src/com/android/contacts/quickcontact/QuickContactActivity.java
index 97185ea..5bfbd10 100644
--- a/src/com/android/contacts/quickcontact/QuickContactActivity.java
+++ b/src/com/android/contacts/quickcontact/QuickContactActivity.java
@@ -2211,7 +2211,8 @@
                 mHasComputedThemeColor
                         ? new MaterialPalette(mColorFilterColor, mStatusBarColor) : null,
                 /* updatedPhotos =*/ null,
-                mContactData.getPhotoId());
+                mContactData.getPhotoId(),
+                mContactData.getNameRawContactId());
     }
 
     private void editContact() {