Pass the preferred photo ID into the contact editor

Bug 21498424

Change-Id: I35e8fa3cff4d963f8017bdd596132fe749479a17
diff --git a/src/com/android/contacts/activities/ContactEditorBaseActivity.java b/src/com/android/contacts/activities/ContactEditorBaseActivity.java
index 41b0c6b..1704ebe 100644
--- a/src/com/android/contacts/activities/ContactEditorBaseActivity.java
+++ b/src/com/android/contacts/activities/ContactEditorBaseActivity.java
@@ -163,7 +163,7 @@
          * Invoked after the contact is saved.
          */
         void onSaveCompleted(boolean hadChanges, int saveMode, boolean saveSucceeded,
-                Uri contactLookupUri, Bundle updatedPhotos, boolean backPressed);
+                Uri contactLookupUri, Bundle updatedPhotos, boolean backPressed, long photoId);
 
         /**
          * Invoked after the contact is joined.
@@ -243,7 +243,8 @@
                     intent.getData(),
                     (Bundle) intent.getParcelableExtra(ContactSaveService.EXTRA_UPDATED_PHOTOS),
                     intent.getBooleanExtra(ContactEditorFragment.INTENT_EXTRA_SAVE_BACK_PRESSED,
-                            false));
+                            false),
+                    intent.getLongExtra(ContactEditorFragment.INTENT_EXTRA_PHOTO_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 e147b76..0e78561 100644
--- a/src/com/android/contacts/activities/ContactSelectionActivity.java
+++ b/src/com/android/contacts/activities/ContactSelectionActivity.java
@@ -47,6 +47,7 @@
 import com.android.contacts.common.activity.RequestPermissionsActivity;
 import com.android.contacts.common.list.ContactEntryListFragment;
 import com.android.contacts.common.util.ImplicitIntentsUtil;
+import com.android.contacts.editor.EditorIntents;
 import com.android.contacts.list.ContactPickerFragment;
 import com.android.contacts.list.ContactsIntentResolver;
 import com.android.contacts.list.ContactsRequest;
@@ -425,7 +426,8 @@
                 startActivityForResult(intent, SUBACTIVITY_ADD_TO_EXISTING_CONTACT);
             } else {
                 // Otherwise launch the full contact editor.
-                startActivityAndForwardResult(new Intent(Intent.ACTION_EDIT, contactLookupUri));
+                startActivityAndForwardResult(EditorIntents.createEditContactIntent(
+                        contactLookupUri, /* materialPalette =*/ null, /* photoId =*/ -1));
             }
         }
 
diff --git a/src/com/android/contacts/editor/CompactContactEditorFragment.java b/src/com/android/contacts/editor/CompactContactEditorFragment.java
index 10efe10..64bcf86 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);
+        editorView.setState(mState, getMaterialPalette(), mViewIdGenerator, mPhotoId);
 
         // Set up the photo widget
         mPhotoHandler = createPhotoHandler();
@@ -357,7 +357,8 @@
         final Intent intent = isInsert
                 ? EditorIntents.createInsertContactIntent(
                         mState, getDisplayName(), getPhoneticName(), mUpdatedPhotos)
-                : EditorIntents.createEditContactIntent(mLookupUri, getMaterialPalette());
+                : EditorIntents.createEditContactIntent(mLookupUri, getMaterialPalette(),
+                        mPhotoId);
         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 fdb29ab..b841f62 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -263,7 +263,7 @@
 
     public void setState(RawContactDeltaList rawContactDeltas,
             MaterialColorMapUtils.MaterialPalette materialPalette,
-            ViewIdGenerator viewIdGenerator) {
+            ViewIdGenerator viewIdGenerator, long photoId) {
         mNames.removeAllViews();
         mPhoneticNames.removeAllViews();
         mNicknames.removeAllViews();
@@ -281,7 +281,7 @@
         mMaterialPalette = materialPalette;
 
         vlog("Setting compact editor state from " + rawContactDeltas);
-        addPhotoView(rawContactDeltas, viewIdGenerator);
+        addPhotoView(rawContactDeltas, viewIdGenerator, photoId);
         addStructuredNameView(rawContactDeltas);
         addEditorViews(rawContactDeltas);
         removeExtraEmptyTextFields(mPhoneNumbers);
@@ -289,7 +289,8 @@
     }
 
     private void addPhotoView(RawContactDeltaList rawContactDeltas,
-            ViewIdGenerator viewIdGenerator) {
+            ViewIdGenerator viewIdGenerator, long photoId) {
+        // 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);
@@ -301,6 +302,26 @@
             // Look for a non-empty super primary photo
             final DataKind dataKind = accountType.getKindForMimetype(Photo.CONTENT_ITEM_TYPE);
             if (dataKind != null) {
+                for (ValuesDelta valuesDelta
+                        : rawContactDelta.getMimeEntries(Photo.CONTENT_ITEM_TYPE)) {
+                    if (valuesDelta != null && valuesDelta.getId() != null
+                            && valuesDelta.getId().equals(photoId)) {
+                        mPhotoRawContactId = rawContactDelta.getRawContactId();
+                        mPhoto.setValues(dataKind, valuesDelta, rawContactDelta,
+                                /* readOnly =*/ !dataKind.editable, mMaterialPalette,
+                                viewIdGenerator);
+                        return;
+                    }
+                }
+            }
+        }
+
+        // Look for a non-empty super primary photo
+        for (RawContactDelta rawContactDelta : rawContactDeltas) {
+            if (!rawContactDelta.isVisible()) continue;
+            final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
+            final DataKind dataKind = accountType.getKindForMimetype(Photo.CONTENT_ITEM_TYPE);
+            if (dataKind != null) {
                 final ValuesDelta valuesDelta = getNonEmptySuperPrimaryValuesDeltas(
                         rawContactDelta, Photo.CONTENT_ITEM_TYPE, dataKind);
                 if (valuesDelta != null) {
diff --git a/src/com/android/contacts/editor/ContactEditorBaseFragment.java b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
index d0750e0..5488de5 100644
--- a/src/com/android/contacts/editor/ContactEditorBaseFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
@@ -112,6 +112,7 @@
     private static final String KEY_DISABLE_DELETE_MENU_OPTION = "disableDeleteMenuOption";
     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_VIEW_ID_GENERATOR = "viewidgenerator";
 
@@ -173,6 +174,11 @@
     public static final String INTENT_EXTRA_UPDATED_PHOTOS = "updated_photos";
 
     /**
+     * Intent key to pass the ID of the photo to display on the editor.
+     */
+    public static final String INTENT_EXTRA_PHOTO_ID = "photo_id";
+
+    /**
      * Intent extra to specify a {@link ContactEditor.SaveMode}.
      */
     public static final String SAVE_MODE_EXTRA_KEY = "saveMode";
@@ -308,6 +314,7 @@
     protected boolean mDisableDeleteMenuOption;
     protected boolean mNewLocalProfile;
     protected MaterialColorMapUtils.MaterialPalette mMaterialPalette;
+    protected long mPhotoId = -1;
 
     //
     // Helpers
@@ -464,6 +471,7 @@
             mDisableDeleteMenuOption = savedState.getBoolean(KEY_DISABLE_DELETE_MENU_OPTION);
             mNewLocalProfile = savedState.getBoolean(KEY_NEW_LOCAL_PROFILE);
             mMaterialPalette = savedState.getParcelable(KEY_MATERIAL_PALETTE);
+            mPhotoId = savedState.getLong(KEY_PHOTO_ID);
 
             mRawContacts = ImmutableList.copyOf(savedState.<RawContact>getParcelableArrayList(
                     KEY_RAW_CONTACTS));
@@ -584,6 +592,7 @@
         if (mMaterialPalette != null) {
             outState.putParcelable(KEY_MATERIAL_PALETTE, mMaterialPalette);
         }
+        outState.putLong(KEY_PHOTO_ID, mPhotoId);
 
         outState.putParcelable(KEY_VIEW_ID_GENERATOR, mViewIdGenerator);
 
@@ -915,7 +924,7 @@
             }
             onSaveCompleted(/* hadChanges =*/ false, saveMode,
                     /* saveSucceeded =*/ mLookupUri != null, mLookupUri,
-                    /* updatedPhotos =*/ null, backPressed);
+                    /* updatedPhotos =*/ null, backPressed, mPhotoId);
             return true;
         }
 
@@ -1312,6 +1321,7 @@
             if (mIntentExtras.containsKey(INTENT_EXTRA_UPDATED_PHOTOS)) {
                 mUpdatedPhotos = mIntentExtras.getParcelable(INTENT_EXTRA_UPDATED_PHOTOS);
             }
+            mPhotoId = mIntentExtras.getLong(INTENT_EXTRA_PHOTO_ID);
         }
     }
 
@@ -1335,12 +1345,12 @@
     @Override
     public void onJoinCompleted(Uri uri) {
         onSaveCompleted(false, SaveMode.RELOAD, uri != null, uri, /* updatedPhotos =*/ null,
-                /* backPressed =*/ false);
+                /* backPressed =*/ false, /* photoId =*/ mPhotoId);
     }
 
     @Override
     public void onSaveCompleted(boolean hadChanges, int saveMode, boolean saveSucceeded,
-            Uri contactLookupUri, Bundle updatedPhotos, boolean backPressed) {
+            Uri contactLookupUri, Bundle updatedPhotos, boolean backPressed, long photoId) {
         if (hadChanges) {
             if (saveSucceeded) {
                 if (saveMode != SaveMode.JOIN && mShowToastAfterSave) {
@@ -1377,7 +1387,7 @@
                             ? EditorIntents.createCompactInsertContactIntent(
                                     mState, getDisplayName(), getPhoneticName(), updatedPhotos)
                             : EditorIntents.createCompactEditContactIntent(
-                                    lookupUri, getMaterialPalette(), updatedPhotos);
+                                    lookupUri, getMaterialPalette(), updatedPhotos, photoId);
                     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 5d7edd3..b4e2fce 100644
--- a/src/com/android/contacts/editor/EditorIntents.java
+++ b/src/com/android/contacts/editor/EditorIntents.java
@@ -44,10 +44,11 @@
      * existing contact.
      */
     public static Intent createCompactEditContactIntent(Uri contactLookupUri,
-            MaterialPalette materialPalette, Bundle updatedPhotos) {
+            MaterialPalette materialPalette, Bundle updatedPhotos, long photoId) {
         final Intent intent = new Intent(Intent.ACTION_EDIT, contactLookupUri);
         putMaterialPalette(intent, materialPalette);
         putUpdatedPhotos(intent, updatedPhotos);
+        putPhotoId(intent, photoId);
         return intent;
     }
 
@@ -96,10 +97,11 @@
      * new contact.
      */
     public static Intent createEditContactIntent(Uri contactLookupUri,
-            MaterialPalette materialPalette) {
+            MaterialPalette materialPalette, long photoId) {
         final Intent intent = new Intent(ContactEditorBaseActivity.ACTION_EDIT, contactLookupUri);
         addContactIntentFlags(intent);
         putMaterialPalette(intent, materialPalette);
+        putPhotoId(intent, photoId);
         return intent;
     }
 
@@ -136,6 +138,13 @@
         }
     }
 
+    private static void putPhotoId(Intent intent, long photoId) {
+        if (photoId >= 0) {
+            intent.putExtra(ContactEditorBaseFragment.INTENT_EXTRA_PHOTO_ID, photoId);
+        }
+    }
+
+
     private static void putRawContactDeltaValues(Intent intent,
             RawContactDeltaList rawContactDeltaList, String displayName, String phoneticName) {
         // Pass on all the data that has been entered so far
diff --git a/src/com/android/contacts/quickcontact/QuickContactActivity.java b/src/com/android/contacts/quickcontact/QuickContactActivity.java
index 405d8f0..8baf93e 100644
--- a/src/com/android/contacts/quickcontact/QuickContactActivity.java
+++ b/src/com/android/contacts/quickcontact/QuickContactActivity.java
@@ -2210,7 +2210,8 @@
                 mContactData.getLookupUri(),
                 mHasComputedThemeColor
                         ? new MaterialPalette(mColorFilterColor, mStatusBarColor) : null,
-                /* updatedPhotos =*/ null);
+                /* updatedPhotos =*/ null,
+                mContactData.getPhotoId());
     }
 
     private void editContact() {