Merge "Remove use of framework resource in configs" into ub-contactsdialer-a-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 3508327..d292bac 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -16,8 +16,8 @@
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.android.contacts"
-    android:versionCode="10303"
-    android:versionName="1.3.03">
+    android:versionCode="10304"
+    android:versionName="1.3.04">
 
     <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="23" />
     <original-package android:name="com.android.contacts" />
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(
diff --git a/src/com/android/contacts/quickcontact/QuickContactActivity.java b/src/com/android/contacts/quickcontact/QuickContactActivity.java
index 12f9061..d552c96 100644
--- a/src/com/android/contacts/quickcontact/QuickContactActivity.java
+++ b/src/com/android/contacts/quickcontact/QuickContactActivity.java
@@ -208,7 +208,6 @@
     private static final String KEY_SELECTED_SUGGESTION_CONTACTS = "selected_suggestion_contacts";
     private static final String KEY_PREVIOUS_CONTACT_ID = "previous_contact_id";
     private static final String KEY_SUGGESTIONS_AUTO_SELECTED = "suggestions_auto_seleted";
-    private static final String KEY_SELECTED_SUGGESTION_NUMBER = "selected_suggestion_number";
 
     private static final int ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION = 150;
     private static final int REQUEST_CODE_CONTACT_EDITOR_ACTIVITY = 1;
@@ -961,7 +960,6 @@
         mSuggestionList = (LinearLayout) findViewById(R.id.suggestion_list);
         mSuggestionsCancelButton= (Button) findViewById(R.id.cancel_button);
         mSuggestionsLinkButton = (Button) findViewById(R.id.link_button);
-        final int previousSelectedSuggestion;
         if (savedInstanceState != null) {
             mIsSuggestionListCollapsed = savedInstanceState.getBoolean(
                     KEY_IS_SUGGESTION_LIST_COLLAPSED, true);
@@ -970,16 +968,14 @@
                     KEY_SUGGESTIONS_AUTO_SELECTED, true);
             mSelectedAggregationIds = (TreeSet<Long>)
                     savedInstanceState.getSerializable(KEY_SELECTED_SUGGESTION_CONTACTS);
-            previousSelectedSuggestion = savedInstanceState.getInt(KEY_SELECTED_SUGGESTION_NUMBER);
         } else {
             mIsSuggestionListCollapsed = true;
             mSelectedAggregationIds.clear();
-            previousSelectedSuggestion = -1;
         }
-        if (previousSelectedSuggestion == -1 || previousSelectedSuggestion > 0) {
-            enableLinkButton();
-        } else {
+        if (mSelectedAggregationIds.isEmpty()) {
             disableLinkButton();
+        } else {
+            enableLinkButton();
         }
         mCollapasedSuggestionHeader.setOnClickListener(new OnClickListener() {
             @Override
@@ -1136,13 +1132,6 @@
                 KEY_SUGGESTIONS_AUTO_SELECTED, mSuggestionsShouldAutoSelected);
         savedInstanceState.putSerializable(
                 KEY_SELECTED_SUGGESTION_CONTACTS, mSelectedAggregationIds);
-        final int selectedSuggestion;
-        if (mSelectedAggregationIds.contains(mContactData.getId())) {
-            selectedSuggestion = mSelectedAggregationIds.size() - 1;
-        } else {
-            selectedSuggestion = mSelectedAggregationIds.size();
-        }
-        savedInstanceState.putInt(KEY_SELECTED_SUGGESTION_NUMBER, selectedSuggestion);
     }
 
     private void processIntent(Intent intent) {