• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.vcard;
17 
18 import android.content.ContentProviderOperation;
19 import android.content.ContentProviderResult;
20 import android.content.ContentResolver;
21 import android.content.OperationApplicationException;
22 import android.net.Uri;
23 import android.os.RemoteException;
24 import android.provider.ContactsContract;
25 import android.util.Log;
26 
27 import java.util.ArrayList;
28 
29 /**
30  * <P>
31  * {@link VCardEntryHandler} implementation which commits the entry to ContentResolver.
32  * </P>
33  * <P>
34  * Note:<BR />
35  * Each vCard may contain big photo images encoded by BASE64,
36  * If we store all vCard entries in memory, OutOfMemoryError may be thrown.
37  * Thus, this class push each VCard entry into ContentResolver immediately.
38  * </P>
39  */
40 public class VCardEntryCommitter implements VCardEntryHandler {
41     public static String LOG_TAG = VCardConstants.LOG_TAG;
42 
43     private final ContentResolver mContentResolver;
44     private long mTimeToCommit;
45     // Set the default maximum batch size to 20
46     private int mMaxBatchSize = 20;
47     private ArrayList<ContentProviderOperation> mOperationList;
48     private final ArrayList<Uri> mCreatedUris = new ArrayList<Uri>();
49 
VCardEntryCommitter(ContentResolver resolver)50     public VCardEntryCommitter(ContentResolver resolver) {
51         mContentResolver = resolver;
52     }
53 
54     @Override
onStart()55     public void onStart() {
56     }
57 
58     @Override
onEnd()59     public void onEnd() {
60         if (mOperationList != null) {
61             mCreatedUris.add(pushIntoContentResolver(mOperationList));
62         }
63 
64         if (VCardConfig.showPerformanceLog()) {
65             Log.d(LOG_TAG, String.format("time to commit entries: %d ms", mTimeToCommit));
66         }
67     }
68 
69     //because the max batch size is 500 defined in ContactsProvider,so we can enlarge this batch
70     //size to reduce db open/close times. From testing results, we can see performance better
71     //when batch size is more bigger.And also each vcardEntry may have some operation records.
72     //So we can set threshold as 450, batch operations will be executed when threshold reached.
73     //Testing result.
74     //batch size                  : 100    200    300    400    450    490    20
75     //consume time(10000 contacts): 178s   143s   127s   124s   119s   117s   195s
76     //consume time (1000 contacts): 17.3s  13.9s  12.6s  12.2s  11.8s  11.6s  19.8s
setMaxBatchSize(int batchSize)77     public void setMaxBatchSize(int batchSize) {
78         mMaxBatchSize = batchSize;
79     }
80 
81     @Override
onEntryCreated(final VCardEntry vcardEntry)82     public void onEntryCreated(final VCardEntry vcardEntry) {
83         final long start = System.currentTimeMillis();
84         mOperationList = vcardEntry.constructInsertOperations(mContentResolver, mOperationList);
85         if (mOperationList != null && mOperationList.size() >= mMaxBatchSize) {
86             mCreatedUris.add(pushIntoContentResolver(mOperationList));
87             mOperationList = null;
88         }
89         mTimeToCommit += System.currentTimeMillis() - start;
90     }
91 
pushIntoContentResolver(ArrayList<ContentProviderOperation> operationList)92     private Uri pushIntoContentResolver(ArrayList<ContentProviderOperation> operationList) {
93         try {
94             final ContentProviderResult[] results = mContentResolver.applyBatch(
95                     ContactsContract.AUTHORITY, operationList);
96 
97             // the first result is always the raw_contact. return it's uri so
98             // that it can be found later. do null checking for badly behaving
99             // ContentResolvers
100             return ((results == null || results.length == 0 || results[0] == null)
101                             ? null : results[0].uri);
102         } catch (RemoteException e) {
103             Log.e(LOG_TAG, String.format("%s: %s", e.toString(), e.getMessage()));
104             return null;
105         } catch (OperationApplicationException e) {
106             Log.e(LOG_TAG, String.format("%s: %s", e.toString(), e.getMessage()));
107             return null;
108         }
109     }
110 
111     /**
112      * Returns the list of created Uris. This list should not be modified by the caller as it is
113      * not a clone.
114      */
getCreatedUris()115    public ArrayList<Uri> getCreatedUris() {
116         return mCreatedUris;
117     }
118 }