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