BatchOperation.java revision 15ef1a8091c1557f175575671a5af62420088944
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 */ 16package com.example.android.samplesync.platform; 17 18import android.content.ContentProviderOperation; 19import android.content.ContentProviderResult; 20import android.content.ContentResolver; 21import android.content.Context; 22import android.content.OperationApplicationException; 23import android.net.Uri; 24import android.os.RemoteException; 25import android.provider.ContactsContract; 26import android.util.Log; 27 28import java.util.ArrayList; 29 30/** 31 * This class handles execution of batch mOperations on Contacts provider. 32 */ 33final public class BatchOperation { 34 35 private final String TAG = "BatchOperation"; 36 37 private final ContentResolver mResolver; 38 39 // List for storing the batch mOperations 40 private final ArrayList<ContentProviderOperation> mOperations; 41 42 public BatchOperation(Context context, ContentResolver resolver) { 43 mResolver = resolver; 44 mOperations = new ArrayList<ContentProviderOperation>(); 45 } 46 47 public int size() { 48 return mOperations.size(); 49 } 50 51 public void add(ContentProviderOperation cpo) { 52 mOperations.add(cpo); 53 } 54 55 public Uri execute() { 56 Uri result = null; 57 58 if (mOperations.size() == 0) { 59 return result; 60 } 61 // Apply the mOperations to the content provider 62 try { 63 ContentProviderResult[] results = mResolver.applyBatch(ContactsContract.AUTHORITY, 64 mOperations); 65 if ((results != null) && (results.length > 0)) 66 result = results[0].uri; 67 } catch (final OperationApplicationException e1) { 68 Log.e(TAG, "storing contact data failed", e1); 69 } catch (final RemoteException e2) { 70 Log.e(TAG, "storing contact data failed", e2); 71 } 72 mOperations.clear(); 73 return result; 74 } 75} 76