1 /* 2 * Copyright (C) 2010 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 */ 16 17 package com.example.android.samplesync.platform; 18 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 java.util.ArrayList; 28 29 /** 30 * This class handles execution of batch mOperations on Contacts provider. 31 */ 32 public class BatchOperation { 33 private final String TAG = "BatchOperation"; 34 35 private final ContentResolver mResolver; 36 // List for storing the batch mOperations 37 ArrayList<ContentProviderOperation> mOperations; 38 BatchOperation(Context context, ContentResolver resolver)39 public BatchOperation(Context context, ContentResolver resolver) { 40 mResolver = resolver; 41 mOperations = new ArrayList<ContentProviderOperation>(); 42 } 43 size()44 public int size() { 45 return mOperations.size(); 46 } 47 add(ContentProviderOperation cpo)48 public void add(ContentProviderOperation cpo) { 49 mOperations.add(cpo); 50 } 51 execute()52 public void execute() { 53 if (mOperations.size() == 0) { 54 return; 55 } 56 // Apply the mOperations to the content provider 57 try { 58 mResolver.applyBatch(ContactsContract.AUTHORITY, mOperations); 59 } catch (final OperationApplicationException e1) { 60 Log.e(TAG, "storing contact data failed", e1); 61 } catch (final RemoteException e2) { 62 Log.e(TAG, "storing contact data failed", e2); 63 } 64 mOperations.clear(); 65 } 66 67 } 68