Merge "Fixing an exception thrown when contact is not found"
diff --git a/src/com/android/contacts/views/editor/ContactEditorFragment.java b/src/com/android/contacts/views/editor/ContactEditorFragment.java
index d0f78a5..6699a84 100644
--- a/src/com/android/contacts/views/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/views/editor/ContactEditorFragment.java
@@ -173,7 +173,9 @@
     private static final int REQUEST_CODE_CAMERA_WITH_DATA = 1;
     private static final int REQUEST_CODE_PHOTO_PICKED_WITH_DATA = 2;
 
+    private Bitmap mPhoto = null;
     private long mRawContactIdRequestingPhoto = -1;
+    private long mRawContactIdRequestingPhotoAfterLoad = -1;
 
     private final EntityDeltaComparator mComparator = new EntityDeltaComparator();
 
@@ -1488,26 +1490,14 @@
         if (resultCode != Activity.RESULT_OK) return;
         switch (requestCode) {
             case REQUEST_CODE_PHOTO_PICKED_WITH_DATA: {
-                BaseContactEditorView requestingEditor = null;
-                for (int i = 0; i < mContent.getChildCount(); i++) {
-                    View childView = mContent.getChildAt(i);
-                    if (childView instanceof BaseContactEditorView) {
-                        BaseContactEditorView editor = (BaseContactEditorView) childView;
-                        if (editor.getRawContactId() == mRawContactIdRequestingPhoto) {
-                            requestingEditor = editor;
-                            break;
-                        }
-                    }
-                }
-
-                if (requestingEditor != null) {
-                    final Bitmap photo = data.getParcelableExtra("data");
-                    requestingEditor.setPhotoBitmap(photo);
-                    mRawContactIdRequestingPhoto = -1;
-                } else {
-                    // The contact that requested the photo is no longer present.
-                    // TODO: Show error message
-                }
+                // As we are coming back to this view, the editor will be reloaded automatically,
+                // which will cause the photo that is set here to disappear. To prevent this,
+                // we remember set a flag which is interpreted after loading.
+                // This is set here anyway to reduce flickering
+                mPhoto = data.getParcelableExtra("data");
+                setPhoto(mRawContactIdRequestingPhoto, mPhoto);
+                mRawContactIdRequestingPhotoAfterLoad = mRawContactIdRequestingPhoto;
+                mRawContactIdRequestingPhoto = -1;
 
                 break;
             }
@@ -1525,6 +1515,29 @@
         }
     }
 
+    /**
+     * Sets the photo stored in mPhoto and writes it to the RawContact with the given id
+     */
+    private void setPhoto(long rawContact, Bitmap photo) {
+        BaseContactEditorView requestingEditor = null;
+        for (int i = 0; i < mContent.getChildCount(); i++) {
+            final View childView = mContent.getChildAt(i);
+            if (childView instanceof BaseContactEditorView) {
+                final BaseContactEditorView editor = (BaseContactEditorView) childView;
+                if (editor.getRawContactId() == rawContact) {
+                    requestingEditor = editor;
+                    break;
+                }
+            }
+        }
+
+        if (requestingEditor != null) {
+            requestingEditor.setPhotoBitmap(photo);
+        } else {
+            Log.w(TAG, "The contact that requested the photo is no longer present.");
+        }
+    }
+
     public Uri getLookupUri() {
         return mLookupUri;
     }
@@ -1555,6 +1568,13 @@
             final long setDataStartTime = SystemClock.elapsedRealtime();
             setData(data);
             final long setDataEndTime = SystemClock.elapsedRealtime();
+
+            // If we are coming back from the photo trimmer, this will be set.
+            if (mRawContactIdRequestingPhotoAfterLoad != -1) {
+                setPhoto(mRawContactIdRequestingPhotoAfterLoad, mPhoto);
+                mRawContactIdRequestingPhotoAfterLoad = -1;
+                mPhoto = null;
+            }
             Log.v(TAG, "Time needed for setting UI: " + (setDataEndTime-setDataStartTime));
         }
     };