1273399bf829133a8385332ad43add3c34c889102Chiao Cheng/* 2273399bf829133a8385332ad43add3c34c889102Chiao Cheng * Copyright (C) 2011 The Android Open Source Project 3273399bf829133a8385332ad43add3c34c889102Chiao Cheng * 4273399bf829133a8385332ad43add3c34c889102Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5273399bf829133a8385332ad43add3c34c889102Chiao Cheng * use this file except in compliance with the License. You may obtain a copy of 6273399bf829133a8385332ad43add3c34c889102Chiao Cheng * the License at 7273399bf829133a8385332ad43add3c34c889102Chiao Cheng * 8273399bf829133a8385332ad43add3c34c889102Chiao Cheng * http://www.apache.org/licenses/LICENSE-2.0 9273399bf829133a8385332ad43add3c34c889102Chiao Cheng * 10273399bf829133a8385332ad43add3c34c889102Chiao Cheng * Unless required by applicable law or agreed to in writing, software 11273399bf829133a8385332ad43add3c34c889102Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12273399bf829133a8385332ad43add3c34c889102Chiao Cheng * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13273399bf829133a8385332ad43add3c34c889102Chiao Cheng * License for the specific language governing permissions and limitations under 14273399bf829133a8385332ad43add3c34c889102Chiao Cheng * the License. 15273399bf829133a8385332ad43add3c34c889102Chiao Cheng */ 16273399bf829133a8385332ad43add3c34c889102Chiao Chengpackage com.android.contacts.common.tests.testauth; 17273399bf829133a8385332ad43add3c34c889102Chiao Cheng 18273399bf829133a8385332ad43add3c34c889102Chiao Chengimport android.accounts.Account; 19612b408f55e4a42635925c52f93c4b8f1a0916a7Ihab Awadimport android.accounts.AccountManager; 20273399bf829133a8385332ad43add3c34c889102Chiao Chengimport android.content.AbstractThreadedSyncAdapter; 21273399bf829133a8385332ad43add3c34c889102Chiao Chengimport android.content.ContentProviderClient; 22273399bf829133a8385332ad43add3c34c889102Chiao Chengimport android.content.ContentResolver; 23273399bf829133a8385332ad43add3c34c889102Chiao Chengimport android.content.ContentValues; 24273399bf829133a8385332ad43add3c34c889102Chiao Chengimport android.content.Context; 25273399bf829133a8385332ad43add3c34c889102Chiao Chengimport android.content.SyncResult; 26273399bf829133a8385332ad43add3c34c889102Chiao Chengimport android.os.Bundle; 27273399bf829133a8385332ad43add3c34c889102Chiao Chengimport android.provider.ContactsContract.RawContacts; 28273399bf829133a8385332ad43add3c34c889102Chiao Chengimport android.util.Log; 29273399bf829133a8385332ad43add3c34c889102Chiao Cheng 30273399bf829133a8385332ad43add3c34c889102Chiao Cheng/** 31273399bf829133a8385332ad43add3c34c889102Chiao Cheng * Simple (minimal) sync adapter. 32273399bf829133a8385332ad43add3c34c889102Chiao Cheng * 33273399bf829133a8385332ad43add3c34c889102Chiao Cheng */ 34273399bf829133a8385332ad43add3c34c889102Chiao Chengpublic class TestSyncAdapter extends AbstractThreadedSyncAdapter { 35612b408f55e4a42635925c52f93c4b8f1a0916a7Ihab Awad private final AccountManager mAccountManager; 36273399bf829133a8385332ad43add3c34c889102Chiao Cheng 37273399bf829133a8385332ad43add3c34c889102Chiao Cheng private final Context mContext; 38273399bf829133a8385332ad43add3c34c889102Chiao Cheng 39273399bf829133a8385332ad43add3c34c889102Chiao Cheng public TestSyncAdapter(Context context, boolean autoInitialize) { 40273399bf829133a8385332ad43add3c34c889102Chiao Cheng super(context, autoInitialize); 41273399bf829133a8385332ad43add3c34c889102Chiao Cheng mContext = context.getApplicationContext(); 42612b408f55e4a42635925c52f93c4b8f1a0916a7Ihab Awad mAccountManager = AccountManager.get(mContext); 43273399bf829133a8385332ad43add3c34c889102Chiao Cheng } 44273399bf829133a8385332ad43add3c34c889102Chiao Cheng 45273399bf829133a8385332ad43add3c34c889102Chiao Cheng /** 46273399bf829133a8385332ad43add3c34c889102Chiao Cheng * Doesn't actually sync, but sweep up all existing local-only contacts. 47273399bf829133a8385332ad43add3c34c889102Chiao Cheng */ 48273399bf829133a8385332ad43add3c34c889102Chiao Cheng @Override 49273399bf829133a8385332ad43add3c34c889102Chiao Cheng public void onPerformSync(Account account, Bundle extras, String authority, 50273399bf829133a8385332ad43add3c34c889102Chiao Cheng ContentProviderClient provider, SyncResult syncResult) { 51273399bf829133a8385332ad43add3c34c889102Chiao Cheng Log.v(TestauthConstants.LOG_TAG, "TestSyncAdapter.onPerformSync() account=" + account); 52273399bf829133a8385332ad43add3c34c889102Chiao Cheng 53273399bf829133a8385332ad43add3c34c889102Chiao Cheng // First, claim all local-only contacts, if any. 54273399bf829133a8385332ad43add3c34c889102Chiao Cheng ContentResolver cr = mContext.getContentResolver(); 55273399bf829133a8385332ad43add3c34c889102Chiao Cheng ContentValues values = new ContentValues(); 56273399bf829133a8385332ad43add3c34c889102Chiao Cheng values.put(RawContacts.ACCOUNT_NAME, account.name); 57273399bf829133a8385332ad43add3c34c889102Chiao Cheng values.put(RawContacts.ACCOUNT_TYPE, account.type); 58273399bf829133a8385332ad43add3c34c889102Chiao Cheng final int count = cr.update(RawContacts.CONTENT_URI, values, 59273399bf829133a8385332ad43add3c34c889102Chiao Cheng RawContacts.ACCOUNT_NAME + " IS NULL AND " + RawContacts.ACCOUNT_TYPE + " IS NULL", 60273399bf829133a8385332ad43add3c34c889102Chiao Cheng null); 61273399bf829133a8385332ad43add3c34c889102Chiao Cheng if (count > 0) { 62273399bf829133a8385332ad43add3c34c889102Chiao Cheng Log.v(TestauthConstants.LOG_TAG, "Claimed " + count + " local raw contacts"); 63273399bf829133a8385332ad43add3c34c889102Chiao Cheng } 64273399bf829133a8385332ad43add3c34c889102Chiao Cheng 65273399bf829133a8385332ad43add3c34c889102Chiao Cheng // TODO: Clear isDirty flag 66273399bf829133a8385332ad43add3c34c889102Chiao Cheng // TODO: Remove isDeleted raw contacts 67273399bf829133a8385332ad43add3c34c889102Chiao Cheng } 68273399bf829133a8385332ad43add3c34c889102Chiao Cheng} 69