diff options
author | Walter Jang <wjang@google.com> | 2016-09-18 09:42:38 -0700 |
---|---|---|
committer | Walter Jang <wjang@google.com> | 2016-09-19 09:31:41 -0700 |
commit | 574601f587dbc33bdd3cd5ea9faec72e9161bac3 (patch) | |
tree | 46eacd5d83e39f32e35bdbd3f1d092a7c7ad082b /tests/src/com | |
parent | 98d41d714f8631e0988624df5b566eb62d63a8e5 (diff) | |
download | Contacts-574601f587dbc33bdd3cd5ea9faec72e9161bac3.tar.gz |
Add basic ContactsDataKind to ContactsTest (1/2)
* Use the same icons for the test app as the
regular app so that custom QuickContacts
actions have it.
* Make a copy of the fallback xml and add
a ContactsDataKind to it
* Update the TestSyncAdapter to add
the custom mimetype data row for null
contacts that it sweeps up.
Test: Add null contacts, add test accounts, grant
contacts permission to test account, toggle
contacts sync for test account
Bug: 31549157
Change-Id: I59f0798c286e7043e0b40ca02f4336533534fae7
Diffstat (limited to 'tests/src/com')
-rw-r--r-- | tests/src/com/android/contacts/tests/testauth/TestSyncAdapter.java | 61 |
1 files changed, 51 insertions, 10 deletions
diff --git a/tests/src/com/android/contacts/tests/testauth/TestSyncAdapter.java b/tests/src/com/android/contacts/tests/testauth/TestSyncAdapter.java index feef8ec30..076baeae4 100644 --- a/tests/src/com/android/contacts/tests/testauth/TestSyncAdapter.java +++ b/tests/src/com/android/contacts/tests/testauth/TestSyncAdapter.java @@ -18,20 +18,29 @@ package com.android.contacts.tests.testauth; import android.accounts.Account; import android.content.AbstractThreadedSyncAdapter; import android.content.ContentProviderClient; +import android.content.ContentProviderOperation; import android.content.ContentResolver; -import android.content.ContentValues; import android.content.Context; import android.content.SyncResult; +import android.database.Cursor; +import android.net.Uri; import android.os.Bundle; +import android.provider.ContactsContract; +import android.provider.ContactsContract.Data; import android.provider.ContactsContract.RawContacts; import android.util.Log; +import java.util.ArrayList; + /** * Simple (minimal) sync adapter. * */ public class TestSyncAdapter extends AbstractThreadedSyncAdapter { + private static final String TEXT_CONTENT_ITEM_TYPE = + "vnd.android.cursor.item/vnd.contactstest.profile"; + private final Context mContext; public TestSyncAdapter(Context context, boolean autoInitialize) { @@ -47,19 +56,51 @@ public class TestSyncAdapter extends AbstractThreadedSyncAdapter { ContentProviderClient provider, SyncResult syncResult) { Log.v(TestauthConstants.LOG_TAG, "TestSyncAdapter.onPerformSync() account=" + account); - // First, claim all local-only contacts, if any. - ContentResolver cr = mContext.getContentResolver(); - ContentValues values = new ContentValues(); - values.put(RawContacts.ACCOUNT_NAME, account.name); - values.put(RawContacts.ACCOUNT_TYPE, account.type); - final int count = cr.update(RawContacts.CONTENT_URI, values, + final ArrayList<ContentProviderOperation> ops = new ArrayList<>(); + + final ContentResolver contentResolver = mContext.getContentResolver(); + final Cursor cursor = contentResolver.query(RawContacts.CONTENT_URI, + new String[] { RawContacts._ID }, RawContacts.ACCOUNT_NAME + " IS NULL AND " + RawContacts.ACCOUNT_TYPE + " IS NULL", - null); - if (count > 0) { - Log.v(TestauthConstants.LOG_TAG, "Claimed " + count + " local raw contacts"); + null, null); + try { + while (cursor.moveToNext()) { + final String rawContactId = Long.toString(cursor.getLong(0)); + + // Claim all local-only contacts for the test account + ops.add(ContentProviderOperation.newUpdate(RawContacts.CONTENT_URI) + .withValue(RawContacts.ACCOUNT_NAME, account.name) + .withValue(RawContacts.ACCOUNT_TYPE, account.type) + .withSelection(RawContacts._ID+"=?", new String[] { rawContactId }) + .build()); + + // Create custom QuickContact action data rows + final Uri dataUri = Data.CONTENT_URI.buildUpon() + .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true") + .build(); + ops.add(ContentProviderOperation.newInsert(dataUri) + .withValue(Data.RAW_CONTACT_ID, rawContactId) + .withValue(Data.MIMETYPE, TEXT_CONTENT_ITEM_TYPE) + .withValue(Data.DATA3, "Contacts test action") + .withValue(Data.DATA5, "view") + .build()); + } + } finally { + cursor.close(); } + if (ops.isEmpty()) return; // TODO: Clear isDirty flag // TODO: Remove isDeleted raw contacts + + Log.v(TestauthConstants.LOG_TAG, "Claiming " + ops.size() + " local raw contacts"); + for (ContentProviderOperation op : ops) { + Log.v(TestauthConstants.LOG_TAG, op.toString()); + } + try { + contentResolver.applyBatch(ContactsContract.AUTHORITY, ops); + } catch (Exception e ) { + Log.e(TestauthConstants.LOG_TAG, "Failed to claim local raw contacts", e); + } } } |