Merge change 9671

* changes:
  Fixing db versioning which got messed up a bit between donut and master.
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index d8af3da..7deb988 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -54,6 +54,7 @@
 import android.os.RemoteException;
 import android.provider.BaseColumns;
 import android.provider.ContactsContract;
+import android.provider.Contacts.People;
 import android.provider.ContactsContract.AggregationExceptions;
 import android.provider.ContactsContract.CommonDataKinds;
 import android.provider.ContactsContract.Contacts;
@@ -455,6 +456,30 @@
         columns.put(RawContacts.DELETED,
                 OpenHelper.RawContactsColumns.CONCRETE_DELETED
                         + " AS " + RawContacts.DELETED);
+        columns.put(RawContacts.TIMES_CONTACTED,
+                Tables.RAW_CONTACTS + "." + RawContacts.TIMES_CONTACTED
+                        + " AS " + People.TIMES_CONTACTED);
+        columns.put(RawContacts.LAST_TIME_CONTACTED,
+                Tables.RAW_CONTACTS + "." + RawContacts.LAST_TIME_CONTACTED
+                        + " AS " + People.LAST_TIME_CONTACTED);
+        columns.put(RawContacts.CUSTOM_RINGTONE,
+                Tables.RAW_CONTACTS + "." + RawContacts.CUSTOM_RINGTONE
+                        + " AS " + People.CUSTOM_RINGTONE);
+        columns.put(RawContacts.SEND_TO_VOICEMAIL,
+                Tables.RAW_CONTACTS + "." + RawContacts.SEND_TO_VOICEMAIL
+                        + " AS " + People.SEND_TO_VOICEMAIL);
+        columns.put(RawContacts.STARRED,
+                Tables.RAW_CONTACTS + "." + RawContacts.STARRED
+                        + " AS " + People.STARRED);
+        columns.put(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE);
+        columns.put(RawContacts.SYNC1,
+                Tables.RAW_CONTACTS + "." + RawContacts.SYNC1 + " AS " + RawContacts.SYNC1);
+        columns.put(RawContacts.SYNC2,
+                Tables.RAW_CONTACTS + "." + RawContacts.SYNC2 + " AS " + RawContacts.SYNC2);
+        columns.put(RawContacts.SYNC3,
+                Tables.RAW_CONTACTS + "." + RawContacts.SYNC3 + " AS " + RawContacts.SYNC3);
+        columns.put(RawContacts.SYNC4,
+                Tables.RAW_CONTACTS + "." + RawContacts.SYNC4 + " AS " + RawContacts.SYNC4);
         sRawContactsProjectionMap = columns;
 
         columns = new HashMap<String, String>();
@@ -486,6 +511,10 @@
         columns.put(Data.DATA13, "data.data13 as data13");
         columns.put(Data.DATA14, "data.data14 as data14");
         columns.put(Data.DATA15, "data.data15 as data15");
+        columns.put(Data.SYNC1, Tables.DATA + "." + Data.SYNC1 + " AS " + Data.SYNC1);
+        columns.put(Data.SYNC2, Tables.DATA + "." + Data.SYNC2 + " AS " + Data.SYNC2);
+        columns.put(Data.SYNC3, Tables.DATA + "." + Data.SYNC3 + " AS " + Data.SYNC3);
+        columns.put(Data.SYNC4, Tables.DATA + "." + Data.SYNC4 + " AS " + Data.SYNC4);
         columns.put(GroupMembership.GROUP_SOURCE_ID, GroupsColumns.CONCRETE_SOURCE_ID + " AS "
                 + GroupMembership.GROUP_SOURCE_ID);
 
@@ -533,6 +562,12 @@
         columns.put(Groups.TITLE, Groups.TITLE);
         columns.put(Groups.TITLE_RES, Groups.TITLE_RES);
         columns.put(Groups.GROUP_VISIBLE, Groups.GROUP_VISIBLE);
+        columns.put(Groups.SYSTEM_ID, Groups.SYSTEM_ID);
+        columns.put(Groups.NOTES, Groups.NOTES);
+        columns.put(Groups.SYNC1, Tables.GROUPS + "." + Groups.SYNC1 + " AS " + Groups.SYNC1);
+        columns.put(Groups.SYNC2, Tables.GROUPS + "." + Groups.SYNC2 + " AS " + Groups.SYNC2);
+        columns.put(Groups.SYNC3, Tables.GROUPS + "." + Groups.SYNC3 + " AS " + Groups.SYNC3);
+        columns.put(Groups.SYNC4, Tables.GROUPS + "." + Groups.SYNC4 + " AS " + Groups.SYNC4);
         sGroupsProjectionMap = columns;
 
         // RawContacts and groups projection map
@@ -1896,7 +1931,7 @@
         long contactId = values.getAsInteger(AggregationExceptions.CONTACT_ID);
         long rawContactId = values.getAsInteger(AggregationExceptions.RAW_CONTACT_ID);
 
-        // First, we build a list of contactID-contactID pairs for the given contact and contact.
+        // First, we build a list of rawContactID-rawContactID pairs for the given contact.
         ArrayList<RawContactPair> pairs = new ArrayList<RawContactPair>();
         Cursor c = db.query(ContactsQuery.TABLE, ContactsQuery.PROJECTION, RawContacts.CONTACT_ID
                 + "=" + contactId, null, null, null, null);
diff --git a/src/com/android/providers/contacts/OpenHelper.java b/src/com/android/providers/contacts/OpenHelper.java
index 01c41d4..0352b8b 100644
--- a/src/com/android/providers/contacts/OpenHelper.java
+++ b/src/com/android/providers/contacts/OpenHelper.java
@@ -58,7 +58,7 @@
 /* package */ class OpenHelper extends SQLiteOpenHelper {
     private static final String TAG = "OpenHelper";
 
-    private static final int DATABASE_VERSION = 58;
+    private static final int DATABASE_VERSION = 59;
     private static final String DATABASE_NAME = "contacts2.db";
     private static final String DATABASE_PRESENCE = "presence_db";
 
@@ -541,7 +541,11 @@
                 RawContacts.TIMES_CONTACTED + " INTEGER NOT NULL DEFAULT 0," +
                 RawContacts.LAST_TIME_CONTACTED + " INTEGER," +
                 RawContacts.STARRED + " INTEGER NOT NULL DEFAULT 0," +
-                RawContactsColumns.DISPLAY_NAME + " TEXT" +
+                RawContactsColumns.DISPLAY_NAME + " TEXT," +
+                RawContacts.SYNC1 + " TEXT, " +
+                RawContacts.SYNC2 + " TEXT, " +
+                RawContacts.SYNC3 + " TEXT, " +
+                RawContacts.SYNC4 + " TEXT " +
         ");");
 
         // Package name mapping table
@@ -579,7 +583,11 @@
                 Data.DATA12 + " TEXT," +
                 Data.DATA13 + " TEXT," +
                 Data.DATA14 + " TEXT," +
-                Data.DATA15 + " TEXT" +
+                Data.DATA15 + " TEXT," +
+                Data.SYNC1 + " TEXT, " +
+                Data.SYNC2 + " TEXT, " +
+                Data.SYNC3 + " TEXT, " +
+                Data.SYNC4 + " TEXT " +
         ");");
 
         /**
@@ -692,7 +700,11 @@
                 Groups.TITLE_RES + " INTEGER," +
                 Groups.NOTES + " TEXT," +
                 Groups.SYSTEM_ID + " TEXT," +
-                Groups.GROUP_VISIBLE + " INTEGER" +
+                Groups.GROUP_VISIBLE + " INTEGER," +
+                Groups.SYNC1 + " TEXT, " +
+                Groups.SYNC2 + " TEXT, " +
+                Groups.SYNC3 + " TEXT, " +
+                Groups.SYNC4 + " TEXT " +
         ");");
 
         db.execSQL("CREATE TRIGGER " + Tables.GROUPS + "_updated1 "
diff --git a/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java b/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java
index cc27428..2c85675 100644
--- a/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java
+++ b/tests/src/com/android/providers/contacts/BaseContactsProvider2Test.java
@@ -117,8 +117,12 @@
     }
 
     protected long createRawContactWithName() {
+        return createRawContactWithName("John", "Doe");
+    }
+
+    protected long createRawContactWithName(String firstName, String lastName) {
         long rawContactId = createRawContact(null);
-        insertStructuredName(rawContactId, "John", "Doe");
+        insertStructuredName(rawContactId, firstName, lastName);
         return rawContactId;
     }
 
@@ -475,6 +479,7 @@
         assertTrue(c.moveToNext());
         assertEquals(state, c.getLong(0) != 0);
         assertFalse(c.moveToNext());
+        c.close();
     }
 
     protected long getVersion(Uri uri) {
@@ -482,6 +487,7 @@
         assertTrue(c.moveToNext());
         long version = c.getLong(0);
         assertFalse(c.moveToNext());
+        c.close();
         return version;
     }
 
diff --git a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
index f6d5bd7..ee1a981 100644
--- a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
+++ b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
@@ -28,6 +28,7 @@
 import android.provider.ContactsContract.AggregationExceptions;
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.Data;
+import android.provider.ContactsContract.Groups;
 import android.provider.ContactsContract.Presence;
 import android.provider.ContactsContract.RawContacts;
 import android.provider.ContactsContract.CommonDataKinds.Email;
@@ -49,6 +50,86 @@
 @LargeTest
 public class ContactsProvider2Test extends BaseContactsProvider2Test {
 
+    public void testRawContactsInsert() {
+        ContentValues values = new ContentValues();
+
+        values.put(RawContacts.ACCOUNT_NAME, "a");
+        values.put(RawContacts.ACCOUNT_TYPE, "b");
+        values.put(RawContacts.SOURCE_ID, "c");
+        values.put(RawContacts.VERSION, 42);
+        values.put(RawContacts.DIRTY, 1);
+        values.put(RawContacts.DELETED, 1);
+        values.put(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DISABLED);
+        values.put(RawContacts.CUSTOM_RINGTONE, "d");
+        values.put(RawContacts.SEND_TO_VOICEMAIL, 1);
+        values.put(RawContacts.LAST_TIME_CONTACTED, 12345);
+        values.put(RawContacts.STARRED, 1);
+        values.put(RawContacts.SYNC1, "e");
+        values.put(RawContacts.SYNC2, "f");
+        values.put(RawContacts.SYNC3, "g");
+        values.put(RawContacts.SYNC4, "h");
+
+        Uri rowUri = mResolver.insert(RawContacts.CONTENT_URI, values);
+
+        assertStoredValues(rowUri, values);
+    }
+
+    public void testDataInsert() {
+        long rawContactId = createRawContact();
+
+        ContentValues values = new ContentValues();
+        values.put(Data.RAW_CONTACT_ID, rawContactId);
+        values.put(Data.MIMETYPE, "testmimetype");
+        values.put(Data.RES_PACKAGE, "oldpackage");
+        values.put(Data.IS_PRIMARY, 1);
+        values.put(Data.IS_SUPER_PRIMARY, 1);
+        values.put(Data.DATA1, "one");
+        values.put(Data.DATA2, "two");
+        values.put(Data.DATA3, "three");
+        values.put(Data.DATA4, "four");
+        values.put(Data.DATA5, "five");
+        values.put(Data.DATA6, "six");
+        values.put(Data.DATA7, "seven");
+        values.put(Data.DATA8, "eight");
+        values.put(Data.DATA9, "nine");
+        values.put(Data.DATA10, "ten");
+        values.put(Data.DATA11, "eleven");
+        values.put(Data.DATA12, "twelve");
+        values.put(Data.DATA13, "thirteen");
+        values.put(Data.DATA14, "fourteen");
+        values.put(Data.DATA15, "fifteen");
+        values.put(Data.SYNC1, "sync1");
+        values.put(Data.SYNC2, "sync2");
+        values.put(Data.SYNC3, "sync3");
+        values.put(Data.SYNC4, "sync4");
+        Uri uri = mResolver.insert(Data.CONTENT_URI, values);
+        assertStoredValues(uri, values);
+    }
+
+    public void testGroupInsert() {
+        ContentValues values = new ContentValues();
+
+        values.put(Groups.ACCOUNT_NAME, "a");
+        values.put(Groups.ACCOUNT_TYPE, "b");
+        values.put(Groups.SOURCE_ID, "c");
+        values.put(Groups.VERSION, 42);
+        values.put(Groups.DIRTY, 1);
+        values.put(Groups.GROUP_VISIBLE, 1);
+        values.put(Groups.TITLE, "d");
+        values.put(Groups.TITLE_RES, 1234);
+        values.put(Groups.NOTES, "e");
+        values.put(Groups.RES_PACKAGE, "f");
+        values.put(Groups.SYSTEM_ID, "g");
+        values.put(Groups.SYNC1, "h");
+        values.put(Groups.SYNC2, "i");
+        values.put(Groups.SYNC3, "j");
+        values.put(Groups.SYNC4, "k");
+
+        Uri rowUri = mResolver.insert(Groups.CONTENT_URI, values);
+
+        assertStoredValues(rowUri, values);
+    }
+
     public void testDisplayNameParsingWhenPartsUnspecified() {
         long rawContactId = createRawContact();
         ContentValues values = new ContentValues();
@@ -88,11 +169,11 @@
     }
 
     public void testSendToVoicemailAndRingtoneAfterAggregation() {
-        long rawContactId1 = createRawContactWithName();
+        long rawContactId1 = createRawContactWithName("a", "b");
         long contactId1 = queryContactId(rawContactId1);
         updateSendToVoicemailAndRingtone(contactId1, true, "foo");
 
-        long rawContactId2 = createRawContactWithName();
+        long rawContactId2 = createRawContactWithName("c", "d");
         long contactId2 = queryContactId(rawContactId2);
         updateSendToVoicemailAndRingtone(contactId2, true, "bar");
 
@@ -104,11 +185,11 @@
     }
 
     public void testDoNotSendToVoicemailAfterAggregation() {
-        long rawContactId1 = createRawContactWithName();
+        long rawContactId1 = createRawContactWithName("e", "f");
         long contactId1 = queryContactId(rawContactId1);
         updateSendToVoicemailAndRingtone(contactId1, true, null);
 
-        long rawContactId2 = createRawContactWithName();
+        long rawContactId2 = createRawContactWithName("g", "h");
         long contactId2 = queryContactId(rawContactId2);
         updateSendToVoicemailAndRingtone(contactId2, false, null);
 
@@ -120,11 +201,11 @@
     }
 
     public void testSetSendToVoicemailAndRingtonePreservedAfterJoinAndSplit() {
-        long rawContactId1 = createRawContactWithName();
+        long rawContactId1 = createRawContactWithName("i", "j");
         long contactId1 = queryContactId(rawContactId1);
         updateSendToVoicemailAndRingtone(contactId1, true, "foo");
 
-        long rawContactId2 = createRawContactWithName();
+        long rawContactId2 = createRawContactWithName("k", "l");
         long contactId2 = queryContactId(rawContactId2);
         updateSendToVoicemailAndRingtone(contactId2, false, "bar");
 
@@ -134,7 +215,7 @@
         // Split them
         setAggregationException(AggregationExceptions.TYPE_KEEP_OUT, contactId1, rawContactId2);
 
-        assertSendToVoicemailAndRingtone(contactId1, true, "foo");
+        assertSendToVoicemailAndRingtone(queryContactId(rawContactId1), true, "foo");
         assertSendToVoicemailAndRingtone(queryContactId(rawContactId2), false, "bar");
     }
 
@@ -229,10 +310,10 @@
         long groupId1 = createGroup(mAccount, "gsid1", "title1");
         long groupId2 = createGroup(mAccount, "gsid2", "title2");
 
-        long c0 = id = createRawContact(mAccount, RawContacts.SOURCE_ID, "c0");
-        Uri id_0_0 = insertGroupMembership(id, "gsid1");
-        Uri id_0_1 = insertEmail(id, "c0@email.com");
-        Uri id_0_2 = insertPhoneNumber(id, "5551212c0");
+        id = createRawContact(mAccount, RawContacts.SOURCE_ID, "c0");
+        insertGroupMembership(id, "gsid1");
+        insertEmail(id, "c0@email.com");
+        insertPhoneNumber(id, "5551212c0");
 
         long c1 = id = createRawContact(mAccount, RawContacts.SOURCE_ID, "c1");
         Uri id_1_0 = insertGroupMembership(id, "gsid1");
@@ -245,7 +326,7 @@
         Uri id_2_1 = insertEmail(id, "c2@email.com");
         Uri id_2_2 = insertPhoneNumber(id, "5551212c2");
 
-        long c3 = id = createRawContact(mAccount);
+        long c3 = id = createRawContact(mAccount, RawContacts.SOURCE_ID, "c3");
         Uri id_3_0 = insertGroupMembership(id, groupId2);
         Uri id_3_1 = insertEmail(id, "c3@email.com");
         Uri id_3_2 = insertPhoneNumber(id, "5551212c3");
@@ -305,6 +386,7 @@
                 Email.DATA, "5551212c3");
 
         assertFalse(iterator.hasNext());
+        iterator.close();
     }
 
     public void testDataCreateUpdateDeleteByMimeType() throws Exception {