Fix EMAIL_FILTER ranking for duplicate emails in different accounts

When there are there are two identical email data items, each belonging
to a different sync adapter account, the "SELECT DISTINCT" operator
causes the two emails to be incorrectly grouped before the account
prioritization sort order is applied.

This can cause the email address to be labelled with the wrong account,
causing it to be incorrectly ranked as an email address from another account.

To fix this, account name and type is added to the group by clause to
ensure that each email address is grouped correctly based on their account
before ranking them by account priority.

Bug: 12207447
Change-Id: Ib79f6c36ee6a83838bcaa82487f2f7fb9716bbf4
(cherry picked from commit 82d88d65f65a0e3eb816cd1ae9d81a0fd78d8a17)
diff --git a/src/com/android/providers/contacts/ b/src/com/android/providers/contacts/
index 2f6bf45..d0b696c 100644
--- a/src/com/android/providers/contacts/
+++ b/src/com/android/providers/contacts/
@@ -5886,7 +5886,12 @@
-                groupBy = Email.DATA + "," + RawContacts.CONTACT_ID;
+                // Group by a unique email address on a per account basis, to make sure that
+                // account promotion sort order correctly ranks email addresses that are in
+                // multiple accounts
+                groupBy = Email.DATA + "," + RawContacts.CONTACT_ID + "," +
+                        RawContacts.ACCOUNT_NAME + "," + RawContacts.ACCOUNT_TYPE;
                 if (sortOrder == null) {
                     final String accountPromotionSortOrder = getAccountPromotionSortOrder(uri);
                     if (!TextUtils.isEmpty(accountPromotionSortOrder)) {