Put back setting of photo from template

* Mistakenly remove it in ag/790426
* Also looks like we weren't setting from template it to true
  after removing the photo.
* Finally, don't set super primary on the local profile

Bug 24981999
Bug 19697372
Bug 23589603

Change-Id: I3e759d5ea75e10c62108728874b771446ac61631
diff --git a/src/com/android/contacts/editor/CompactContactEditorFragment.java b/src/com/android/contacts/editor/CompactContactEditorFragment.java
index 6e4c4be..6c0ff85 100644
--- a/src/com/android/contacts/editor/CompactContactEditorFragment.java
+++ b/src/com/android/contacts/editor/CompactContactEditorFragment.java
@@ -19,8 +19,6 @@
 import com.android.contacts.ContactSaveService;
 import com.android.contacts.R;
 import com.android.contacts.activities.CompactContactEditorActivity;
-import com.android.contacts.activities.ContactEditorActivity;
-import com.android.contacts.activities.ContactEditorBaseActivity;
 import com.android.contacts.common.model.RawContactDelta;
 import com.android.contacts.common.model.ValuesDelta;
 import com.android.contacts.common.model.account.AccountWithDataSet;
@@ -202,9 +200,7 @@
         if (activity == null || activity.isFinishing()) {
             return;
         }
-        if (!mIsUserProfile) {
-            acquireAggregationSuggestions(activity, rawContactId, valuesDelta);
-        }
+        acquireAggregationSuggestions(activity, rawContactId, valuesDelta);
     }
 
     @Override
diff --git a/src/com/android/contacts/editor/CompactKindSectionView.java b/src/com/android/contacts/editor/CompactKindSectionView.java
index 3ebe27b..fffe464 100644
--- a/src/com/android/contacts/editor/CompactKindSectionView.java
+++ b/src/com/android/contacts/editor/CompactKindSectionView.java
@@ -16,6 +16,13 @@
 
 package com.android.contacts.editor;
 
+import com.android.contacts.R;
+import com.android.contacts.common.model.RawContactDelta;
+import com.android.contacts.common.model.RawContactModifier;
+import com.android.contacts.common.model.ValuesDelta;
+import com.android.contacts.common.model.account.AccountType;
+import com.android.contacts.common.model.dataitem.DataKind;
+
 import android.content.Context;
 import android.database.Cursor;
 import android.provider.ContactsContract.CommonDataKinds.Event;
@@ -30,14 +37,6 @@
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
-import com.android.contacts.R;
-import com.android.contacts.common.model.RawContactDelta;
-import com.android.contacts.common.model.RawContactDeltaList;
-import com.android.contacts.common.model.RawContactModifier;
-import com.android.contacts.common.model.ValuesDelta;
-import com.android.contacts.common.model.account.AccountType;
-import com.android.contacts.common.model.dataitem.DataKind;
-
 import java.util.ArrayList;
 import java.util.List;
 
@@ -152,6 +151,7 @@
     private ViewIdGenerator mViewIdGenerator;
     private CompactRawContactsEditorView.Listener mListener;
 
+    private boolean mIsUserProfile;
     private boolean mShowOneEmptyEditor = false;
     private boolean mHideIfEmpty = true;
 
@@ -190,6 +190,10 @@
         mIcon = (ImageView) findViewById(R.id.kind_icon);
     }
 
+    public void setIsUserProfile(boolean isUserProfile) {
+        mIsUserProfile = isUserProfile;
+    }
+
     /**
      * @param showOneEmptyEditor If true, we will always show one empty editor, otherwise an empty
      *         editor will not be shown until the user enters a value.  Note, this does not apply
@@ -364,8 +368,11 @@
         // Structured name
         final StructuredNameEditorView nameView = (StructuredNameEditorView) mLayoutInflater
                 .inflate(R.layout.structured_name_editor_view, mEditors, /* attachToRoot =*/ false);
-        nameView.setEditorListener(new StructuredNameEditorListener(valuesDelta,
-                rawContactDelta.getRawContactId(), mListener));
+        if (!mIsUserProfile) {
+            // Don't set super primary for the me contact
+            nameView.setEditorListener(new StructuredNameEditorListener(
+                    valuesDelta, rawContactDelta.getRawContactId(), mListener));
+        }
         nameView.setDeletable(false);
         nameView.setValues(
                 accountType.getKindForMimetype(DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME),
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index b3e4349..5388810 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -503,7 +503,7 @@
     }
 
     public void removePhoto() {
-        mPhotoValuesDelta.setFromTemplate(false);
+        mPhotoValuesDelta.setFromTemplate(true);
         mPhotoValuesDelta.put(Photo.PHOTO, (byte[]) null);
 
         mPhotoView.removePhoto();
@@ -517,11 +517,15 @@
     }
 
     public void updatePhoto(Uri photoUri) {
-        // Unset primary for all photos
-        unsetSuperPrimary();
+        mPhotoValuesDelta.setFromTemplate(false);
 
-        // Mark the currently displayed photo as primary
-        mPhotoValuesDelta.setSuperPrimary(true);
+        if (!mIsUserProfile) {
+            // Unset primary for all photos
+            unsetSuperPrimary();
+
+            // Mark the currently displayed photo as primary
+            mPhotoValuesDelta.setSuperPrimary(true);
+        }
 
         mPhotoView.setFullSizedPhoto(photoUri);
     }
@@ -603,9 +607,6 @@
      * UI.
      */
     public void setPrimaryPhoto(CompactPhotoSelectionFragment.Photo photo) {
-        // Unset primary for all photos
-        unsetSuperPrimary();
-
         // Find the values delta to mark as primary
         final KindSectionDataList kindSectionDataList =
                 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
@@ -623,7 +624,13 @@
         }
         final ValuesDelta valuesDelta = valuesDeltaList.get(photo.valuesDeltaListIndex);
         valuesDelta.setFromTemplate(false);
-        valuesDelta.setSuperPrimary(true);
+
+        if (!mIsUserProfile) {
+            // Unset primary for all other photos
+            unsetSuperPrimary();
+
+            valuesDelta.setSuperPrimary(true);
+        }
 
         // Update the UI
         mPhotoView.setPhoto(valuesDelta, mMaterialPalette);
@@ -1029,6 +1036,7 @@
         final CompactKindSectionView kindSectionView = (CompactKindSectionView)
                 mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup,
                         /* attachToRoot =*/ false);
+        kindSectionView.setIsUserProfile(mIsUserProfile);
 
         if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
                 || Email.CONTENT_ITEM_TYPE.equals(mimeType)) {