Use selected photo ID as the "photo to display" lookup parameter

Don't use the photo ID passed in on the Intent to open the
editor since that will never change.

Bug 25729444

Change-Id: I84082245ac491c9481cc469d0b7342755250ec66
diff --git a/src/com/android/contacts/editor/CompactContactEditorFragment.java b/src/com/android/contacts/editor/CompactContactEditorFragment.java
index 8b4e260..cd7f0ab 100644
--- a/src/com/android/contacts/editor/CompactContactEditorFragment.java
+++ b/src/com/android/contacts/editor/CompactContactEditorFragment.java
@@ -215,6 +215,9 @@
 
     public void setPrimaryPhoto(CompactPhotoSelectionFragment.Photo photo) {
         getContent().setPrimaryPhoto(photo);
+
+        // Update the photo ID we will try to match when selecting the photo to display
+        mPhotoId = photo.photoId;
     }
 
     @Override
diff --git a/src/com/android/contacts/editor/CompactPhotoSelectionFragment.java b/src/com/android/contacts/editor/CompactPhotoSelectionFragment.java
index 930475e..bd6444f 100644
--- a/src/com/android/contacts/editor/CompactPhotoSelectionFragment.java
+++ b/src/com/android/contacts/editor/CompactPhotoSelectionFragment.java
@@ -108,8 +108,6 @@
          */
         public boolean primary;
 
-        public long rawContactId;
-
         /**
          * Pointer back to the KindSectionDataList this photo came from.
          * See {@link CompactRawContactsEditorView#getPhotos}
@@ -121,6 +119,8 @@
         /** Newly taken or selected photo that has not yet been saved to CP2. */
         public Uri updatedPhotoUri;
 
+        public long photoId;
+
         @Override
         public int describeContents() {
             return 0;
@@ -136,6 +136,7 @@
             dest.writeInt(kindSectionDataListIndex);
             dest.writeInt(valuesDeltaListIndex);
             dest.writeParcelable(updatedPhotoUri, flags);
+            dest.writeLong(photoId);
         }
 
         private void readFromParcel(Parcel source) {
@@ -148,6 +149,7 @@
             kindSectionDataListIndex = source.readInt();
             valuesDeltaListIndex = source.readInt();
             updatedPhotoUri = source.readParcelable(classLoader);
+            photoId = source.readLong();
         }
     }
 
@@ -197,18 +199,14 @@
                 if (convertView == null || convertView.findViewById(R.id.account_type) != null) {
                     return mLayoutInflater.inflate(R.layout.take_a_photo_button, /* root =*/ null);
                 }
-                else{
-                    return convertView;
-                }
+                return convertView;
             }
 
             if (getItemViewType(position) == 1) {
                 if (convertView == null || convertView.findViewById(R.id.account_type) != null) {
                     return mLayoutInflater.inflate(R.layout.all_photos_button, /* root =*/ null);
                 }
-                else {
-                    return convertView;
-                }
+                return convertView;
             }
 
             // when position greater than 1, we should make sure account_type *is* in convertView
@@ -298,8 +296,8 @@
         mGridView.setAdapter(photoAdapter);
         mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-                final PhotoSourceDialogFragment.Listener listener = (PhotoSourceDialogFragment.Listener)
-                        getActivity();
+                final PhotoSourceDialogFragment.Listener listener =
+                        (PhotoSourceDialogFragment.Listener) getActivity();
                 if (position == 0){
                     listener.onTakePhotoChosen();
                 } else if (position == 1) {
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index 0ccc2a7..43f39bf 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -549,6 +549,7 @@
                 photo.valuesDeltaListIndex = j;
                 photo.accountType = accountType.getDisplayLabel(getContext()).toString();
                 photo.accountName = kindSectionData.getRawContactDelta().getAccountName();
+                photo.photoId = valuesDelta.getId();
 
                 if (updatedPhotos != null) {
                     photo.updatedPhotoUri = (Uri) updatedPhotos.get(String.valueOf(
@@ -582,12 +583,13 @@
             wlog("Invalid values delta list index");
             return;
         }
+
+        // Update values delta
         final ValuesDelta valuesDelta = valuesDeltaList.get(photo.valuesDeltaListIndex);
         valuesDelta.setFromTemplate(false);
-        // Unset primary for all photos
         unsetSuperPrimaryFromAllPhotos();
-        // Mark the currently displayed photo as primary
         valuesDelta.setSuperPrimary(true);
+
         // Update the UI
         mPhotoView.setPhoto(valuesDelta, mMaterialPalette);
     }
@@ -944,15 +946,15 @@
         mPhotoView.setPhoto(photoToDisplay.second, mMaterialPalette);
 
         // Find the raw contact ID and values delta that will be written when the photo is edited
-        final Pair<KindSectionData, ValuesDelta> pair = kindSectionDataList.getEntryToWrite(
+        final Pair<KindSectionData, ValuesDelta> photoToWrite = kindSectionDataList.getEntryToWrite(
                 mPhotoId, mPrimaryAccount, mIsUserProfile);
-        if (pair == null) {
+        if (photoToWrite == null) {
             mPhotoView.setReadOnly(true);
             return;
         }
         mPhotoView.setReadOnly(false);
-        mPhotoRawContactId = pair.first.getRawContactDelta().getRawContactId();
-        mPhotoValuesDelta = pair.second;
+        mPhotoRawContactId = photoToWrite.first.getRawContactDelta().getRawContactId();
+        mPhotoValuesDelta = photoToWrite.second;
     }
 
     private void addKindSectionViews() {
diff --git a/src/com/android/contacts/editor/ContactEditorBaseFragment.java b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
index e17b2cd..0d9b3a9 100644
--- a/src/com/android/contacts/editor/ContactEditorBaseFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
@@ -1398,7 +1398,10 @@
                         mIntentExtras.getInt(INTENT_EXTRA_MATERIAL_PALETTE_PRIMARY_COLOR),
                         mIntentExtras.getInt(INTENT_EXTRA_MATERIAL_PALETTE_SECONDARY_COLOR));
             }
-            mPhotoId = mIntentExtras.getLong(INTENT_EXTRA_PHOTO_ID);
+            // If the user selected a different photo, don't restore the one from the Intent
+            if (mPhotoId < 0) {
+                mPhotoId = mIntentExtras.getLong(INTENT_EXTRA_PHOTO_ID);
+            }
             mRawContactIdToDisplayAlone = mIntentExtras.getLong(
                     INTENT_EXTRA_RAW_CONTACT_ID_TO_DISPLAY_ALONE, -1);
             mRawContactDisplayAloneIsReadOnly = mIntentExtras.getBoolean(