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