Fix some photo selection fragment bugs (E18)

* Hide the photo source dialog when there is only
  1 photo in the aggregate
* Fix reverting to photo selection fragment on
  rotate after taking or selecting a photo
* Unset super primary on other photo valuesdeltas
  after taking a new photo (also fixes full
  res image not loading on quick contacts after
  the editor is closed?)
* Show full resolution photos on photo selector
  even if they have not yet been saved to CP2

Bug 19697372
Bug 23589603

Change-Id: Idd6966ba981d251ae713950ea2f0d2d60c7b30c0
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index ed5dc3b..6f5a9cb 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -36,6 +36,7 @@
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.net.Uri;
+import android.os.Bundle;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.provider.ContactsContract;
@@ -126,6 +127,12 @@
          * Invoked when a rawcontact from merged contacts is selected in editor.
          */
         public void onRawContactSelected(Uri uri, long rawContactId, boolean isReadOnly);
+
+        /**
+         * Returns the map of raw contact IDs to newly taken or selected photos that have not
+         * yet been saved to CP2.
+         */
+        public Bundle getUpdatedPhotos();
     }
 
     /**
@@ -509,27 +516,26 @@
     }
 
     public void updatePhoto(Uri photoUri) {
-        // Even though high-res photos cannot be saved by passing them via
-        // an EntityDeltaList (since they cause the Bundle size limit to be
-        // exceeded), we still pass a low-res thumbnail. This simplifies
-        // code all over the place, because we don't have to test whether
-        // there is a change in EITHER the delta-list OR a changed photo...
-        // this way, there is always a change in the delta-list.
-        mPhotoValuesDelta.setFromTemplate(false);
+        // Unset primary for all photos
+        unsetSuperPrimary();
+
+        // Mark the currently displayed photo as primary
         mPhotoValuesDelta.setSuperPrimary(true);
-        try {
-            final byte[] bytes = EditorUiUtils.getCompressedThumbnailBitmapBytes(
-                    getContext(), photoUri);
-            if (bytes != null) {
-                mPhotoValuesDelta.setPhoto(bytes);
-            }
-        } catch (FileNotFoundException e) {
-            elog("Failed to get bitmap from photo Uri");
-        }
 
         mPhotoView.setFullSizedPhoto(photoUri);
     }
 
+    private void unsetSuperPrimary() {
+        final List<KindSectionData> kindSectionDataList =
+                mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
+        for (KindSectionData kindSectionData : kindSectionDataList) {
+            final List<ValuesDelta> valuesDeltaList = kindSectionData.getValuesDeltas();
+            for (ValuesDelta valuesDelta : valuesDeltaList) {
+                valuesDelta.setSuperPrimary(false);
+            }
+        }
+    }
+
     /**
      * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}.
      */
@@ -555,6 +561,8 @@
     public ArrayList<CompactPhotoSelectionFragment.Photo> getPhotos() {
         final ArrayList<CompactPhotoSelectionFragment.Photo> photos = new ArrayList<>();
 
+        final Bundle updatedPhotos = mListener == null ? null : mListener.getUpdatedPhotos();
+
         final List<KindSectionData> kindSectionDataList =
                 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
         for (int i = 0; i < kindSectionDataList.size(); i++) {
@@ -576,6 +584,12 @@
                 photo.primary = valuesDelta.isSuperPrimary();
                 photo.kindSectionDataListIndex = i;
                 photo.valuesDeltaListIndex = j;
+
+                if (updatedPhotos != null) {
+                    photo.updatedPhotoUri = (Uri) updatedPhotos.get(String.valueOf(
+                            kindSectionData.getRawContactDelta().getRawContactId()));
+                }
+
                 photos.add(photo);
             }
         }
@@ -588,17 +602,12 @@
      * UI.
      */
     public void setPrimaryPhoto(CompactPhotoSelectionFragment.Photo photo) {
-        // Unset primary for all other photos
-        final List<KindSectionData> kindSectionDataList =
-                mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
-        for (KindSectionData kindSectionData : kindSectionDataList) {
-            final List<ValuesDelta> valuesDeltaList = kindSectionData.getValuesDeltas();
-            for (ValuesDelta valuesDelta : valuesDeltaList) {
-                valuesDelta.setSuperPrimary(false);
-            }
-        }
+        // Unset primary for all photos
+        unsetSuperPrimary();
 
         // Find the values delta to mark as primary
+        final KindSectionDataList kindSectionDataList =
+                mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
         if (photo.kindSectionDataListIndex < 0
                 || photo.kindSectionDataListIndex >= kindSectionDataList.size()) {
             wlog("Invalid kind section data list index");
@@ -722,7 +731,7 @@
                     continue;
                 }
 
-                final List<KindSectionData> kindSectionDataList =
+                final KindSectionDataList kindSectionDataList =
                         getOrCreateKindSectionDataList(mimeType);
                 final KindSectionData kindSectionData =
                         new KindSectionData(accountType, dataKind, rawContactDelta);
@@ -740,7 +749,7 @@
         }
     }
 
-    private List<KindSectionData> getOrCreateKindSectionDataList(String mimeType) {
+    private KindSectionDataList getOrCreateKindSectionDataList(String mimeType) {
         // Put structured names and nicknames together
         mimeType = Nickname.CONTENT_ITEM_TYPE.equals(mimeType)
                 ? StructuredName.CONTENT_ITEM_TYPE : mimeType;