merge in lmp-mr1-release history after reset to lmp-mr1-dev
diff --git a/src/com/android/providers/contacts/aggregation/ContactAggregator.java b/src/com/android/providers/contacts/aggregation/ContactAggregator.java
index 8dcabca..d030687 100644
--- a/src/com/android/providers/contacts/aggregation/ContactAggregator.java
+++ b/src/com/android/providers/contacts/aggregation/ContactAggregator.java
@@ -929,6 +929,10 @@
             c.close();
         }
 
+        if (index == 0) {
+            return;
+        }
+
         // Clear is_super_primary setting for all the mime-types exist in both raw contact
         // of rawContactId and raw contacts of contactId
         String superPrimaryUpdateSql = "UPDATE " + Tables.DATA +
@@ -938,11 +942,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 78f1b80..0e0264c 100644
--- a/tests/src/com/android/providers/contacts/aggregation/ContactAggregatorTest.java
+++ b/tests/src/com/android/providers/contacts/aggregation/ContactAggregatorTest.java
@@ -1620,6 +1620,24 @@
         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);
+
+        // Sanity check.
+        assertStoredValue(uri, Data.IS_SUPER_PRIMARY, 1);
+
+        // Action: aggregate
+        setAggregationException(AggregationExceptions.TYPE_KEEP_TOGETHER, rawContactId1,
+                rawContactId2);
+
+        // Verify: name is still super primary
+        assertStoredValue(uri, Data.IS_SUPER_PRIMARY, 1);
+    }
+
     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