PhonebookPullRequest.java revision cc9b268c333198db613bd98656da15f29e325155
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.bluetooth.pbapclient;
17
18import com.android.vcard.VCardEntry;
19
20import android.accounts.Account;
21import android.content.ContentProviderOperation;
22import android.content.Context;
23import android.content.OperationApplicationException;
24import android.provider.ContactsContract;
25import android.database.Cursor;
26import android.net.Uri;
27import android.os.Bundle;
28import android.os.RemoteException;
29import android.provider.ContactsContract.Data;
30import android.provider.ContactsContract.RawContacts;
31import android.provider.ContactsContract.RawContactsEntity;
32import android.provider.ContactsContract.Contacts.Entity;
33import android.provider.ContactsContract.CommonDataKinds.Phone;
34import android.provider.ContactsContract.CommonDataKinds.StructuredName;
35import android.util.Log;
36
37import com.android.vcard.VCardEntry;
38
39import java.lang.InterruptedException;
40import java.util.ArrayList;
41import java.util.HashMap;
42import java.util.List;
43
44public class PhonebookPullRequest extends PullRequest {
45    private static final int MAX_OPS = 200;
46    private static final boolean DBG = true;
47    private static final String TAG = "PbapPhonebookPullRequest";
48
49    private final Account mAccount;
50    private final Context mContext;
51    public boolean complete = false;
52
53    public PhonebookPullRequest(Context context, Account account) {
54        mContext = context;
55        mAccount = account;
56        path = PbapClientConnectionHandler.PB_PATH;
57    }
58
59    // TODO: Apply operations together if possible.
60    private void addContact(VCardEntry e)
61        throws RemoteException, OperationApplicationException, InterruptedException {
62        ArrayList<ContentProviderOperation> ops =
63                e.constructInsertOperations(mContext.getContentResolver(), null);
64        mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
65        ops.clear();
66    }
67
68    @Override
69    public void onPullComplete() {
70        if (mEntries == null) {
71            Log.e(TAG, "onPullComplete entries is null.");
72            return;
73        }
74
75        if (DBG) {
76            Log.d(TAG, "onPullComplete with " + mEntries.size() + " count.");
77        }
78        try {
79            for (VCardEntry e : mEntries) {
80                if (Thread.currentThread().isInterrupted()) {
81                    throw new InterruptedException();
82                }
83                addContact(e);
84            }
85            Log.d(TAG, "Sync complete: add=" + mEntries.size());
86        } catch (OperationApplicationException | RemoteException | NumberFormatException e) {
87            Log.d(TAG, "Got exception: ", e);
88        } catch (InterruptedException e) {
89            Log.d(TAG, "Interrupted durring insert.");
90        } finally {
91            complete = true;
92        }
93    }
94}
95