Fix too many variables SQLiteException

De-dup the set of accounts associated with the raw contacts before
appending to the SQL query.

Bug:18510108
Change-Id: Ia135a656965bdcfc76df28d60f0636d562f76382
diff --git a/src/com/android/contacts/common/model/ContactLoader.java b/src/com/android/contacts/common/model/ContactLoader.java
index eba825b..998fb2d 100644
--- a/src/com/android/contacts/common/model/ContactLoader.java
+++ b/src/com/android/contacts/common/model/ContactLoader.java
@@ -63,9 +63,11 @@
 import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 
 /**
@@ -758,6 +760,34 @@
         }
     }
 
+    static private class AccountKey {
+        private final String mAccountName;
+        private final String mAccountType;
+        private final String mDataSet;
+
+        public AccountKey(String accountName, String accountType, String dataSet) {
+            mAccountName = accountName;
+            mAccountType = accountType;
+            mDataSet = dataSet;
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(mAccountName, mAccountType, mDataSet);
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (!(obj instanceof AccountKey)) {
+                return false;
+            }
+            final AccountKey other = (AccountKey) obj;
+            return Objects.equals(mAccountName, other.mAccountName)
+                && Objects.equals(mAccountType, other.mAccountType)
+                && Objects.equals(mDataSet, other.mDataSet);
+        }
+    }
+
     /**
      * Loads groups meta-data for all groups associated with all constituent raw contacts'
      * accounts.
@@ -765,11 +795,15 @@
     private void loadGroupMetaData(Contact result) {
         StringBuilder selection = new StringBuilder();
         ArrayList<String> selectionArgs = new ArrayList<String>();
+        final HashSet<AccountKey> accountsSeen = new HashSet<>();
         for (RawContact rawContact : result.getRawContacts()) {
             final String accountName = rawContact.getAccountName();
             final String accountType = rawContact.getAccountTypeString();
             final String dataSet = rawContact.getDataSet();
-            if (accountName != null && accountType != null) {
+            final AccountKey accountKey = new AccountKey(accountName, accountType, dataSet);
+            if (accountName != null && accountType != null &&
+                    !accountsSeen.contains(accountKey)) {
+                accountsSeen.add(accountKey);
                 if (selection.length() != 0) {
                     selection.append(" OR ");
                 }