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 */
16package com.android.contacts.tests.testauth;
17
18import android.accounts.Account;
19import android.accounts.AccountManager;
20import android.content.AbstractThreadedSyncAdapter;
21import android.content.ContentProviderClient;
22import android.content.ContentResolver;
23import android.content.ContentValues;
24import android.content.Context;
25import android.content.SyncResult;
26import android.os.Bundle;
27import android.provider.ContactsContract.RawContacts;
28import android.util.Log;
29
30/**
31 * Simple (minimal) sync adapter.
32 *
33 */
34public class TestSyncAdapter extends AbstractThreadedSyncAdapter {
35    private final AccountManager mAccountManager;
36
37    private final Context mContext;
38
39    public TestSyncAdapter(Context context, boolean autoInitialize) {
40        super(context, autoInitialize);
41        mContext = context.getApplicationContext();
42        mAccountManager = AccountManager.get(mContext);
43    }
44
45    /**
46     * Doesn't actually sync, but sweep up all existing local-only contacts.
47     */
48    @Override
49    public void onPerformSync(Account account, Bundle extras, String authority,
50            ContentProviderClient provider, SyncResult syncResult) {
51        Log.v(TestauthConstants.LOG_TAG, "TestSyncAdapter.onPerformSync() account=" + account);
52
53        // First, claim all local-only contacts, if any.
54        ContentResolver cr = mContext.getContentResolver();
55        ContentValues values = new ContentValues();
56        values.put(RawContacts.ACCOUNT_NAME, account.name);
57        values.put(RawContacts.ACCOUNT_TYPE, account.type);
58        final int count = cr.update(RawContacts.CONTENT_URI, values,
59                RawContacts.ACCOUNT_NAME + " IS NULL AND " + RawContacts.ACCOUNT_TYPE + " IS NULL",
60                null);
61        if (count > 0) {
62            Log.v(TestauthConstants.LOG_TAG, "Claimed " + count + " local raw contacts");
63        }
64
65        // TODO: Clear isDirty flag
66        // TODO: Remove isDeleted raw contacts
67    }
68}
69