aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBai Tao <michaelbai@google.com>2010-03-26 05:38:41 +0800
committerBai Tao <michaelbai@google.com>2010-03-26 09:00:18 +0800
commitf8536aaa7a52b9a7a353bc54e158becdbe79ec87 (patch)
tree90c530695676436a10c11428f0ac5dbb5e05646c
parent5ecb42daaef10e8790f9ed7683fae93cf7bed988 (diff)
downloadContactsProvider-f8536aaa7a52b9a7a353bc54e158becdbe79ec87.tar.gz
Put the contacts on the phone into My Contacts group.
Change-Id: I71964d3bcb026ea2c3d09438bcbcf0ed474c1886
-rw-r--r--src/com/android/providers/contacts/ContactsProvider2.java52
1 files changed, 51 insertions, 1 deletions
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index a87d7d56..fa738094 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -147,6 +147,7 @@ public class ContactsProvider2 extends SQLiteContentProvider implements OnAccoun
/** Default for the maximum number of returned aggregation suggestions. */
private static final int DEFAULT_MAX_SUGGESTIONS = 5;
+ private static final String GOOGLE_MY_CONTACTS_GROUP_TITLE = "My Contacts";
/**
* Property key for the legacy contact import version. The need for a version
* as opposed to a boolean flag is that if we discover bugs in the contact import process,
@@ -3947,7 +3948,35 @@ public class ContactsProvider2 extends SQLiteContentProvider implements OnAccoun
return 1;
}
+ /**
+ * Check whether GOOGLE_MY_CONTACTS_GROUP exists, otherwise create it.
+ *
+ * @return the group id
+ */
+ private long getOrCreateMyContactsGroupInTransaction(String accountName, String accountType) {
+ Cursor cursor = mDb.query(Tables.GROUPS, new String[] {"_id"},
+ Groups.ACCOUNT_NAME + " =? AND " + Groups.ACCOUNT_TYPE + " =? AND "
+ + Groups.TITLE + " =?",
+ new String[] {accountName, accountType, GOOGLE_MY_CONTACTS_GROUP_TITLE},
+ null, null, null);
+ try {
+ if(cursor.moveToNext()) {
+ return cursor.getLong(0);
+ }
+ } finally {
+ cursor.close();
+ }
+
+ ContentValues values = new ContentValues();
+ values.put(Groups.TITLE, GOOGLE_MY_CONTACTS_GROUP_TITLE);
+ values.put(Groups.ACCOUNT_NAME, accountName);
+ values.put(Groups.ACCOUNT_TYPE, accountType);
+ values.put(Groups.GROUP_VISIBLE, "1");
+ return mDb.insert(Tables.GROUPS, null, values);
+ }
+
public void onAccountsUpdated(Account[] accounts) {
+ // TODO : Check the unit test.
HashSet<Account> existingAccounts = new HashSet<Account>();
boolean hasUnassignedContacts[] = new boolean[]{false};
mDb.beginTransaction();
@@ -4010,7 +4039,28 @@ public class ContactsProvider2 extends SQLiteContentProvider implements OnAccoun
if (primaryAccount != null) {
String[] params = new String[] {primaryAccount.name, primaryAccount.type};
-
+ if (primaryAccount.type.equals(DEFAULT_ACCOUNT_TYPE)) {
+ long groupId = getOrCreateMyContactsGroupInTransaction(
+ primaryAccount.name, primaryAccount.type);
+ if (groupId != -1) {
+ long mimeTypeId = mDbHelper.getMimeTypeId(
+ GroupMembership.CONTENT_ITEM_TYPE);
+ mDb.execSQL(
+ "INSERT INTO " + Tables.DATA + "(" + DataColumns.MIMETYPE_ID +
+ ", " + Data.RAW_CONTACT_ID + ", "
+ + GroupMembership.GROUP_ROW_ID + ") " +
+ "SELECT " + mimeTypeId + ", " + Data._ID + ", " + groupId +
+ " FROM " + Tables.RAW_CONTACTS +
+ " WHERE " + RawContacts.ACCOUNT_NAME + " IS NULL" +
+ " AND " + RawContacts.ACCOUNT_TYPE + " IS NULL" +
+ " AND " + Data._ID + " NOT EXIST (" +
+ "SELECT " + Data.RAW_CONTACT_ID +
+ " FROM " + Tables.DATA +
+ " WHERE " + DataColumns.MIMETYPE_ID + " = " + mimeTypeId +
+ ")"
+ );
+ }
+ }
mDb.execSQL(
"UPDATE " + Tables.RAW_CONTACTS +
" SET " + RawContacts.ACCOUNT_NAME + "=?,"