1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.contacts.tests.testauth; 17 18 import android.accounts.Account; 19 import android.content.AbstractThreadedSyncAdapter; 20 import android.content.ContentProviderClient; 21 import android.content.ContentProviderOperation; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.SyncResult; 25 import android.database.Cursor; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.provider.ContactsContract; 29 import android.provider.ContactsContract.Data; 30 import android.provider.ContactsContract.RawContacts; 31 import android.util.Log; 32 33 import java.util.ArrayList; 34 35 /** 36 * Simple (minimal) sync adapter. 37 * 38 */ 39 public class TestSyncAdapter extends AbstractThreadedSyncAdapter { 40 41 private static final String TEXT_CONTENT_ITEM_TYPE = 42 "vnd.android.cursor.item/vnd.contactstest.profile"; 43 44 private final Context mContext; 45 TestSyncAdapter(Context context, boolean autoInitialize)46 public TestSyncAdapter(Context context, boolean autoInitialize) { 47 super(context, autoInitialize); 48 mContext = context.getApplicationContext(); 49 } 50 51 /** 52 * Doesn't actually sync, but sweep up all existing local-only contacts. 53 */ 54 @Override onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)55 public void onPerformSync(Account account, Bundle extras, String authority, 56 ContentProviderClient provider, SyncResult syncResult) { 57 if (Log.isLoggable(TestauthConstants.LOG_TAG, Log.VERBOSE)) { 58 Log.v(TestauthConstants.LOG_TAG, "TestSyncAdapter.onPerformSync() account=" + account); 59 } 60 61 final ArrayList<ContentProviderOperation> ops = new ArrayList<>(); 62 63 final ContentResolver contentResolver = mContext.getContentResolver(); 64 final Cursor cursor = contentResolver.query(RawContacts.CONTENT_URI, 65 new String[] { RawContacts._ID }, 66 RawContacts.ACCOUNT_NAME + " IS NULL AND " + RawContacts.ACCOUNT_TYPE + " IS NULL", 67 null, null); 68 try { 69 while (cursor.moveToNext()) { 70 final String rawContactId = Long.toString(cursor.getLong(0)); 71 72 // Claim all local-only contacts for the test account 73 ops.add(ContentProviderOperation.newUpdate(RawContacts.CONTENT_URI) 74 .withValue(RawContacts.ACCOUNT_NAME, account.name) 75 .withValue(RawContacts.ACCOUNT_TYPE, account.type) 76 .withSelection(RawContacts._ID+"=?", new String[] { rawContactId }) 77 .build()); 78 79 // Create custom QuickContact action data rows 80 final Uri dataUri = Data.CONTENT_URI.buildUpon() 81 .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true") 82 .build(); 83 ops.add(ContentProviderOperation.newInsert(dataUri) 84 .withValue(Data.RAW_CONTACT_ID, rawContactId) 85 .withValue(Data.MIMETYPE, TEXT_CONTENT_ITEM_TYPE) 86 .withValue(Data.DATA3, "Contacts test action") 87 .withValue(Data.DATA5, "view") 88 .build()); 89 } 90 } finally { 91 cursor.close(); 92 } 93 if (ops.isEmpty()) return; 94 95 // TODO: Clear isDirty flag 96 // TODO: Remove isDeleted raw contacts 97 98 if (Log.isLoggable(TestauthConstants.LOG_TAG, Log.VERBOSE)) { 99 Log.v(TestauthConstants.LOG_TAG, "Claiming " + ops.size() + " local raw contacts"); 100 for (ContentProviderOperation op : ops) { 101 Log.v(TestauthConstants.LOG_TAG, op.toString()); 102 } 103 } 104 try { 105 contentResolver.applyBatch(ContactsContract.AUTHORITY, ops); 106 } catch (Exception e ) { 107 Log.e(TestauthConstants.LOG_TAG, "Failed to claim local raw contacts", e); 108 } 109 } 110 } 111