Additional change to IS_SUPER_PRIMARY

Noticed clearSuperPrimarySetting() sometimes clears all
mimetypes's is_super_primary flag values sometimes, even
mimetypes that aren't contained in both raw contacts.

This doesn't appear to have been the intended behavior.
Looks like a simple bug. Wrote a unit test and fixed
the bug. Added a new method to DataUtil used inside the
unit test. This method wasn't strictly necessary. But
it is very useful in later CLs.

(Part #2 or "Remove NAME_VERIFIED" series of CLs)

Bug: 5080996
Bug: 18777272
Change-Id: I354ffe51ea64cc532387d7ba79fbb6d2389d662e
diff --git a/src/com/android/providers/contacts/aggregation/ContactAggregator.java b/src/com/android/providers/contacts/aggregation/ContactAggregator.java
index ab87c7e..5c7858e 100644
--- a/src/com/android/providers/contacts/aggregation/ContactAggregator.java
+++ b/src/com/android/providers/contacts/aggregation/ContactAggregator.java
@@ -940,11 +940,8 @@
                         " WHERE " + RawContacts.CONTACT_ID + "=?1)" +
                         " OR " +  Data.RAW_CONTACT_ID + "=?2)";
 
-        if (index > 0) {
-            mimeTypeCondition.append(')');
-            superPrimaryUpdateSql += mimeTypeCondition.toString();
-        }
-
+        mimeTypeCondition.append(')');
+        superPrimaryUpdateSql += mimeTypeCondition.toString();
         db.execSQL(superPrimaryUpdateSql, args);
     }
 
diff --git a/tests/src/com/android/providers/contacts/aggregation/ContactAggregatorTest.java b/tests/src/com/android/providers/contacts/aggregation/ContactAggregatorTest.java
index 09ee207..9aecd13 100644
--- a/tests/src/com/android/providers/contacts/aggregation/ContactAggregatorTest.java
+++ b/tests/src/com/android/providers/contacts/aggregation/ContactAggregatorTest.java
@@ -1615,6 +1615,28 @@
         assertSuperPrimary(ContentUris.parseId(uri_org2), false);
     }
 
+    public void testAggregation_clearSuperPrimarySingleMimetype() {
+        // Setup: two raw contacts, each has a single name. One of the names is super primary.
+        long rawContactId1 = RawContactUtil.createRawContact(mResolver, ACCOUNT_1);
+        long rawContactId2 = RawContactUtil.createRawContact(mResolver, ACCOUNT_1);
+        final Uri uri = DataUtil.insertStructuredName(mResolver, rawContactId1, "name1",
+                null, null, /* isSuperPrimary = */ true);
+        final Uri uri2 = DataUtil.insertStructuredName(mResolver, rawContactId2, "name2",
+                null, null, /* isSuperPrimary = */ false);
+
+        // Sanity check.
+        assertStoredValue(uri, Data.IS_SUPER_PRIMARY, 1);
+        assertStoredValue(uri2, Data.IS_SUPER_PRIMARY, 0);
+
+        // Action: aggregate
+        setAggregationException(AggregationExceptions.TYPE_KEEP_TOGETHER, rawContactId1,
+                rawContactId2);
+
+        // Verify: super primary values are unchanged
+        assertStoredValue(uri, Data.IS_SUPER_PRIMARY, 1);
+        assertStoredValue(uri2, Data.IS_SUPER_PRIMARY, 0);
+    }
+
     public void testNotAggregate_TooManyRawContactsInCandidate() {
         long preId= 0;
         for (int i = 0; i < ContactAggregator.AGGREGATION_CONTACT_SIZE_LIMIT; i++) {
diff --git a/tests/src/com/android/providers/contacts/testutil/DataUtil.java b/tests/src/com/android/providers/contacts/testutil/DataUtil.java
index 194e67d..1f4f35a 100644
--- a/tests/src/com/android/providers/contacts/testutil/DataUtil.java
+++ b/tests/src/com/android/providers/contacts/testutil/DataUtil.java
@@ -22,6 +22,7 @@
 import android.net.Uri;
 import android.provider.ContactsContract;
 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
+import android.provider.ContactsContract.Data;
 import android.test.mock.MockContentResolver;
 
 /**
@@ -59,6 +60,13 @@
     public static Uri insertStructuredName(
             ContentResolver resolver, long rawContactId, String givenName, String familyName,
             String phoneticGiven) {
+        return insertStructuredName(resolver, rawContactId, givenName, familyName, phoneticGiven,
+                /* isSuperPrimary = true */ false);
+    }
+
+    public static Uri insertStructuredName(
+            ContentResolver resolver, long rawContactId, String givenName, String familyName,
+            String phoneticGiven, boolean isSuperPrimary) {
         ContentValues values = new ContentValues();
         StringBuilder sb = new StringBuilder();
         if (givenName != null) {
@@ -79,7 +87,11 @@
         if (phoneticGiven != null) {
             values.put(StructuredName.PHONETIC_GIVEN_NAME, phoneticGiven);
         }
+        if (isSuperPrimary) {
+            values.put(Data.IS_PRIMARY, 1);
+            values.put(Data.IS_SUPER_PRIMARY, 1);
+        }
 
         return insertStructuredName(resolver, rawContactId, values);
     }
-}
+}
\ No newline at end of file