Merge "Delay loading groups until compact editors are bound (E14)" into ub-contactsdialer-a-dev
diff --git a/res/values/integers.xml b/res/values/integers.xml
index 0de1de1..86a8e26 100644
--- a/res/values/integers.xml
+++ b/res/values/integers.xml
@@ -24,4 +24,7 @@
     <!-- Number of lines the QuickContact title can have -->
     <integer name="quickcontact_title_lines">1</integer>
 
+    <!-- Max suggestions limit showing in quick contact suggestion card [CHAR LIMIT=30]-->
+    <integer name="quickcontact_suggestions_limit">10</integer>
+
 </resources>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 7c93df8..6e1b9ee 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -777,7 +777,7 @@
     <!-- Button used in quick contact suggestion card to merge selected contacts. [CHAR LIMIT=30]-->
     <string name="quickcontact_suggestion_merge_button">Merge</string>
 
-    <!-- Suggestions number in quick contact suggestion card [CHAR LIMIT=30] -->
+    <!-- Suggestions number in quick contact suggestion card [CHAR LIMIT=60] -->
     <plurals name="quickcontact_suggestions_number">
         <item quantity="one">1 suggested contact</item>
         <item quantity="other"><xliff:g id="count">%d</xliff:g> suggested contacts</item>
diff --git a/src/com/android/contacts/quickcontact/QuickContactActivity.java b/src/com/android/contacts/quickcontact/QuickContactActivity.java
index 3ac9472..39cf0ec 100644
--- a/src/com/android/contacts/quickcontact/QuickContactActivity.java
+++ b/src/com/android/contacts/quickcontact/QuickContactActivity.java
@@ -255,6 +255,7 @@
     private View mSuggestionSeparator;
     private Button mSuggestionsMergeButton;
     private boolean mIsSuggestionListCollapsed;
+    private long mPreviousSuggestionForContactId = 0;
 
     private MultiShrinkScroller mScroller;
     private SelectAccountDialogFragmentListener mSelectAccountFragmentListener;
@@ -489,44 +490,51 @@
         final int suggestionNumber = mSuggestions.size();
         final String suggestionSummary = getSuggestionAccountSummary(mSuggestions);
 
-        if (suggestionNumber > 0) {
-            mSuggestionCardView.setVisibility(View.VISIBLE);
-
-            // Take the first suggestion 's photo as the summary photo.
-            // TODO: take all suggestions' photos.
-            final Suggestion firstSuggestion = mSuggestions.get(0);
-            if (firstSuggestion.photo != null) {
-                mSuggestionSummaryPhoto.setImageBitmap(BitmapFactory.decodeByteArray(
-                        firstSuggestion.photo, 0, firstSuggestion.photo.length));
-            } else {
-                mSuggestionSummaryPhoto.setImageDrawable(
-                        ContactPhotoManager.getDefaultAvatarDrawableForContact(
-                                getResources(), false, null));
-            }
-
-            mSuggestionForName.setText(suggestionForName);
-            mSuggestionNumber.setText(getResources().getQuantityString(
-                    R.plurals.quickcontact_suggestions_number, suggestionNumber, suggestionNumber));
-            mSuggestionSummary.setText(suggestionSummary);
-
-            for (Suggestion suggestion : mSuggestions) {
-                mSuggestionList.addView(inflateSuggestionListView(suggestion));
-            }
-
-            mSuggestionExpansionButton.setOnClickListener(new View.OnClickListener() {
-                @Override
-                public void onClick(View view) {
-                    if (mIsSuggestionListCollapsed) {
-                        expandSuggestionList();
-                    } else {
-                        collapseSuggestionList();
-                    }
-                }
-            });
-
-        } else {
-            mSuggestionCardView.setVisibility(View.GONE);
+        if (suggestionNumber <= 0) {
+            mSelectedAggregationIds.clear();
+            return;
         }
+
+        mSuggestionCardView.setVisibility(View.VISIBLE);
+
+        // Take the first suggestion 's photo as the summary photo.
+        // TODO: take all suggestions' photos.
+        final Suggestion firstSuggestion = mSuggestions.get(0);
+        if (firstSuggestion.photo != null) {
+            mSuggestionSummaryPhoto.setImageBitmap(BitmapFactory.decodeByteArray(
+                    firstSuggestion.photo, 0, firstSuggestion.photo.length));
+        } else {
+            mSuggestionSummaryPhoto.setImageDrawable(
+                    ContactPhotoManager.getDefaultAvatarDrawableForContact(
+                            getResources(), false, null));
+        }
+
+        mSuggestionForName.setText(suggestionForName);
+        mSuggestionNumber.setText(getResources().getQuantityString(
+                R.plurals.quickcontact_suggestions_number, suggestionNumber, suggestionNumber));
+        mSuggestionSummary.setText(suggestionSummary);
+
+        final Set<Long> suggestionContactIds = new HashSet<>();
+        for (Suggestion suggestion : mSuggestions) {
+            mSuggestionList.addView(inflateSuggestionListView(suggestion));
+            suggestionContactIds.add(suggestion.contactId);
+        }
+
+        // Remove contact Ids that are not suggestions.
+        final Set<Long> selectedSuggestionIds = com.google.common.collect.Sets.intersection(
+                mSelectedAggregationIds, suggestionContactIds);
+        mSelectedAggregationIds = new TreeSet<>(selectedSuggestionIds);
+
+        mSuggestionExpansionButton.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View view) {
+                if (mIsSuggestionListCollapsed) {
+                    expandSuggestionList();
+                } else {
+                    collapseSuggestionList();
+                }
+            }
+        });
     }
 
     private void collapseSuggestionList() {
@@ -563,7 +571,7 @@
             }
         }
 
-        Set<String> accountTypeWithNumber = new HashSet<>();
+        final Set<String> accountTypeWithNumber = new HashSet<>();
         for (String accountType : accountTypeMap.keySet()) {
             final String number = getResources().getQuantityString(
                     R.plurals.quickcontact_suggestion_account_type_number,
@@ -927,9 +935,8 @@
                 if (!mSelectedAggregationIds.contains(mContactData.getId())) {
                     mSelectedAggregationIds.add(mContactData.getId());
                 }
-                TreeSet<Long> mergedContactIds = new TreeSet<Long>(mSelectedAggregationIds);
-                mSelectedAggregationIds.clear(); // Clear selected ids for merged contact.
-                JoinContactsDialogFragment.start(QuickContactActivity.this, mergedContactIds);
+                JoinContactsDialogFragment.start(
+                        QuickContactActivity.this, mSelectedAggregationIds);
             }
         });
 
@@ -1320,11 +1327,18 @@
         if (mAggregationSuggestionEngine == null) {
             mAggregationSuggestionEngine = new AggregationSuggestionEngine(this);
             mAggregationSuggestionEngine.setListener(this);
-            mAggregationSuggestionEngine.setSuggestionsLimit(10);
+            mAggregationSuggestionEngine.setSuggestionsLimit(getResources().getInteger(
+                    R.integer.quickcontact_suggestions_limit));
             mAggregationSuggestionEngine.start();
         }
 
         mAggregationSuggestionEngine.setContactId(mContactData.getId());
+        if (mPreviousSuggestionForContactId != 0
+                && mPreviousSuggestionForContactId != mContactData.getId()) {
+            // Clear selected Ids when listing suggestions for new contact Id.
+            mSelectedAggregationIds.clear();
+        }
+        mPreviousSuggestionForContactId = mContactData.getId();
 
         // Trigger suggestion engine to compute suggestions.
         final ContentValues values = new ContentValues();