• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 package com.android.bluetooth.pbapclient;
17 
18 import com.android.vcard.VCardEntry;
19 
20 import android.accounts.Account;
21 import android.content.ContentProviderOperation;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.OperationApplicationException;
25 import android.provider.ContactsContract;
26 import android.database.Cursor;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.os.RemoteException;
30 import android.provider.ContactsContract.Data;
31 import android.provider.ContactsContract.RawContacts;
32 import android.provider.ContactsContract.RawContactsEntity;
33 import android.provider.ContactsContract.Contacts.Entity;
34 import android.provider.ContactsContract.CommonDataKinds.Phone;
35 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
36 import android.util.Log;
37 
38 import com.android.vcard.VCardEntry;
39 
40 import java.lang.InterruptedException;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
44 
45 public class PhonebookPullRequest extends PullRequest {
46     private static final int MAX_OPS = 250;
47     private static final boolean VDBG = false;
48     private static final String TAG = "PbapPhonebookPullRequest";
49 
50     private final Account mAccount;
51     private final Context mContext;
52     public boolean complete = false;
53 
PhonebookPullRequest(Context context, Account account)54     public PhonebookPullRequest(Context context, Account account) {
55         mContext = context;
56         mAccount = account;
57         path = PbapClientConnectionHandler.PB_PATH;
58     }
59 
60 
61     @Override
onPullComplete()62     public void onPullComplete() {
63         if (mEntries == null) {
64             Log.e(TAG, "onPullComplete entries is null.");
65             return;
66         }
67         if (VDBG) {
68             Log.d(TAG, "onPullComplete with " + mEntries.size() + " count.");
69         }
70 
71         try {
72             ContentResolver contactsProvider = mContext.getContentResolver();
73             ArrayList<ContentProviderOperation> insertOperations = new ArrayList<>();
74             ArrayList<ContentProviderOperation> currentContactOperations;
75             // Group insert operations together to minimize inter process communication and improve
76             // processing time.
77             for (VCardEntry e : mEntries) {
78                 if (Thread.currentThread().isInterrupted()) {
79                     Log.e(TAG, "Interrupted durring insert.");
80                     break;
81                 }
82                 int numberOfOperations = insertOperations.size();
83                 // Append current vcard to list of insert operations.
84                 e.constructInsertOperations(contactsProvider, insertOperations);
85                 if (insertOperations.size() >= MAX_OPS) {
86                     // If we have exceded the limit to the insert operation remove the latest vcard
87                     // and submit.
88                     insertOperations.subList(numberOfOperations, insertOperations.size()).clear();
89                     contactsProvider.applyBatch(ContactsContract.AUTHORITY, insertOperations);
90                     insertOperations = e.constructInsertOperations(contactsProvider, null);
91                     if (insertOperations.size() >= MAX_OPS) {
92                         // Current VCard has more than 500 attributes, drop the card.
93                         insertOperations.clear();
94                     }
95                 }
96             }
97             if (insertOperations.size() > 0) {
98                 // Apply any unsubmitted vcards.
99                 contactsProvider.applyBatch(ContactsContract.AUTHORITY, insertOperations);
100                 insertOperations.clear();
101             }
102             if (VDBG) {
103                 Log.d(TAG, "Sync complete: add=" + mEntries.size());
104             }
105         } catch (OperationApplicationException | RemoteException | NumberFormatException e) {
106             Log.e(TAG, "Got exception: ", e);
107         } finally {
108             complete = true;
109         }
110     }
111 }
112