Home | History | Annotate | Download | only in vcard
      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     private int mCounter;
     46     private ArrayList<ContentProviderOperation> mOperationList;
     47     private final ArrayList<Uri> mCreatedUris = new ArrayList<Uri>();
     48 
     49     public VCardEntryCommitter(ContentResolver resolver) {
     50         mContentResolver = resolver;
     51     }
     52 
     53     @Override
     54     public void onStart() {
     55     }
     56 
     57     @Override
     58     public void onEnd() {
     59         if (mOperationList != null) {
     60             mCreatedUris.add(pushIntoContentResolver(mOperationList));
     61         }
     62 
     63         if (VCardConfig.showPerformanceLog()) {
     64             Log.d(LOG_TAG, String.format("time to commit entries: %d ms", mTimeToCommit));
     65         }
     66     }
     67 
     68     @Override
     69     public void onEntryCreated(final VCardEntry vcardEntry) {
     70         final long start = System.currentTimeMillis();
     71         mOperationList = vcardEntry.constructInsertOperations(mContentResolver, mOperationList);
     72         mCounter++;
     73         if (mCounter >= 20) {
     74             mCreatedUris.add(pushIntoContentResolver(mOperationList));
     75             mCounter = 0;
     76             mOperationList = null;
     77         }
     78         mTimeToCommit += System.currentTimeMillis() - start;
     79     }
     80 
     81     private Uri pushIntoContentResolver(ArrayList<ContentProviderOperation> operationList) {
     82         try {
     83             final ContentProviderResult[] results = mContentResolver.applyBatch(
     84                     ContactsContract.AUTHORITY, operationList);
     85 
     86             // the first result is always the raw_contact. return it's uri so
     87             // that it can be found later. do null checking for badly behaving
     88             // ContentResolvers
     89             return ((results == null || results.length == 0 || results[0] == null)
     90                             ? null : results[0].uri);
     91         } catch (RemoteException e) {
     92             Log.e(LOG_TAG, String.format("%s: %s", e.toString(), e.getMessage()));
     93             return null;
     94         } catch (OperationApplicationException e) {
     95             Log.e(LOG_TAG, String.format("%s: %s", e.toString(), e.getMessage()));
     96             return null;
     97         }
     98     }
     99 
    100     /**
    101      * Returns the list of created Uris. This list should not be modified by the caller as it is
    102      * not a clone.
    103      */
    104    public ArrayList<Uri> getCreatedUris() {
    105         return mCreatedUris;
    106     }
    107 }