Pass the preferred photo ID into the contact editor

Bug 21498424

Change-Id: I35e8fa3cff4d963f8017bdd596132fe749479a17
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) {