OSDN Git Service

Reflect vCard API change
authorDaisuke Miyakawa <dmiyakawa@google.com>
Thu, 17 Mar 2011 15:20:27 +0000 (08:20 -0700)
committerDaisuke Miyakawa <dmiyakawa@google.com>
Wed, 30 Mar 2011 21:21:40 +0000 (14:21 -0700)
Remove dependency toward OneEntryHandler in vCard code.

This change doesn't take care of oom for simplicity.

See also Id623814e8f5961aaf9cf9b4dcc92f75ace401148

Bug: 4066223
Change-Id: I3c5a58fef5a404b992a3c17578eeb63518294231

src/com/android/bluetooth/pbap/BluetoothPbapCallLogComposer.java
src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java

index bbf77af..34fca79 100644 (file)
@@ -32,11 +32,8 @@ import com.android.vcard.VCardBuilder;
 import com.android.vcard.VCardConfig;
 import com.android.vcard.VCardConstants;
 import com.android.vcard.VCardUtils;
-import com.android.vcard.VCardComposer.OneEntryHandler;
 
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.List;
 
 /**
  * VCard composer especially for Call Log used in Bluetooth.
@@ -82,18 +79,14 @@ public class BluetoothPbapCallLogComposer {
     private final Context mContext;
     private ContentResolver mContentResolver;
     private Cursor mCursor;
-    private final boolean mCareHandlerErrors;
 
     private boolean mTerminateIsCalled;
-    private final List<OneEntryHandler> mHandlerList;
 
     private String mErrorReason = NO_ERROR;
 
-    public BluetoothPbapCallLogComposer(final Context context, boolean careHandlerErrors) {
+    public BluetoothPbapCallLogComposer(final Context context) {
         mContext = context;
         mContentResolver = context.getContentResolver();
-        mCareHandlerErrors = careHandlerErrors;
-        mHandlerList = new ArrayList<OneEntryHandler>();
     }
 
     public boolean init(final Uri contentUri, final String selection,
@@ -114,24 +107,6 @@ public class BluetoothPbapCallLogComposer {
             return false;
         }
 
-        if (mCareHandlerErrors) {
-            List<OneEntryHandler> finishedList = new ArrayList<OneEntryHandler>(
-                    mHandlerList.size());
-            for (OneEntryHandler handler : mHandlerList) {
-                if (!handler.onInit(mContext)) {
-                    for (OneEntryHandler finished : finishedList) {
-                        finished.onTerminate();
-                    }
-                    return false;
-                }
-            }
-        } else {
-            // Just ignore the false returned from onInit().
-            for (OneEntryHandler handler : mHandlerList) {
-                handler.onInit(mContext);
-            }
-        }
-
         if (mCursor.getCount() == 0 || !mCursor.moveToFirst()) {
             try {
                 mCursor.close();
@@ -147,42 +122,16 @@ public class BluetoothPbapCallLogComposer {
         return true;
     }
 
-    public void addHandler(OneEntryHandler handler) {
-        if (handler != null) {
-            mHandlerList.add(handler);
-        }
-    }
-
-    public boolean createOneEntry() {
+    public String createOneEntry() {
         if (mCursor == null || mCursor.isAfterLast()) {
             mErrorReason = FAILURE_REASON_NOT_INITIALIZED;
-            return false;
+            return null;
         }
-
-        final String vcard;
         try {
-            vcard = createOneCallLogEntryInternal();
-        } catch (OutOfMemoryError error) {
-            Log.e(TAG, "OutOfMemoryError occured. Ignore the entry");
-            System.gc();
-            return true;
+            return createOneCallLogEntryInternal();
         } finally {
             mCursor.moveToNext();
         }
-
-        if (mCareHandlerErrors) {
-            for (OneEntryHandler handler : mHandlerList) {
-                if (!handler.onEntryCreated(vcard)) {
-                    return false;
-                }
-            }
-        } else {
-            for (OneEntryHandler handler : mHandlerList) {
-                handler.onEntryCreated(vcard);
-            }
-        }
-
-        return true;
     }
 
     private String createOneCallLogEntryInternal() {
@@ -290,10 +239,6 @@ public class BluetoothPbapCallLogComposer {
     }
 
     public void terminate() {
-        for (OneEntryHandler handler : mHandlerList) {
-            handler.onTerminate();
-        }
-
         if (mCursor != null) {
             try {
                 mCursor.close();
index ba2bbe2..d18b827 100644 (file)
@@ -49,7 +49,6 @@ import android.util.Log;
 import com.android.bluetooth.R;
 import com.android.vcard.VCardComposer;
 import com.android.vcard.VCardConfig;
-import com.android.vcard.VCardComposer.OneEntryHandler;
 
 import java.io.IOException;
 import java.io.OutputStream;
@@ -105,7 +104,7 @@ public class BluetoothPbapVcardManager {
     }
 
     public final String getOwnerPhoneNumberVcard(final boolean vcardType21) {
-        BluetoothPbapCallLogComposer composer = new BluetoothPbapCallLogComposer(mContext, false);
+        BluetoothPbapCallLogComposer composer = new BluetoothPbapCallLogComposer(mContext);
         String name = BluetoothPbapService.getLocalPhoneName();
         String number = BluetoothPbapService.getLocalPhoneNum();
         String vcard = composer.composeVCardForPhoneOwnNumber(Phone.TYPE_MOBILE, name, number,
@@ -429,6 +428,7 @@ public class BluetoothPbapVcardManager {
 
         if (isContacts) {
             VCardComposer composer = null;
+            HandlerForStringBuffer buffer = null;
             try {
                 // Currently only support Generic Vcard 2.1 and 3.0
                 int vcardType;
@@ -441,8 +441,9 @@ public class BluetoothPbapVcardManager {
                 vcardType |= VCardConfig.FLAG_REFRAIN_PHONE_NUMBER_FORMATTING;
 
                 composer = new VCardComposer(mContext, vcardType, true);
-                composer.addHandler(new HandlerForStringBuffer(op, ownerVCard));
-                if (!composer.init(Contacts.CONTENT_URI, selection, null, Contacts._ID)) {
+                buffer = new HandlerForStringBuffer(op, ownerVCard);
+                if (!composer.init(Contacts.CONTENT_URI, selection, null, Contacts._ID) ||
+                        !buffer.onInit(mContext)) {
                     return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
                 }
 
@@ -452,24 +453,35 @@ public class BluetoothPbapVcardManager {
                         BluetoothPbapObexServer.sIsAborted = false;
                         break;
                     }
-                    if (!composer.createOneEntryLegacy()) {
+                    String vcard = composer.createOneEntry();
+                    if (vcard == null) {
                         Log.e(TAG, "Failed to read a contact. Error reason: "
                                 + composer.getErrorReason());
                         return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
                     }
+                    if (!buffer.onEntryCreated(vcard)) {
+                        // onEntryCreate() already emits error.
+                        return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
+                    }
                 }
             } finally {
                 if (composer != null) {
                     composer.terminate();
                 }
+                if (buffer != null) {
+                    buffer.onTerminate();
+                }
             }
         } else { // CallLog
             BluetoothPbapCallLogComposer composer = null;
+            HandlerForStringBuffer buffer = null;
             try {
-                composer = new BluetoothPbapCallLogComposer(mContext, true);
-                composer.addHandler(new HandlerForStringBuffer(op, ownerVCard));
+
+                composer = new BluetoothPbapCallLogComposer(mContext);
+                buffer = new HandlerForStringBuffer(op, ownerVCard);
                 if (!composer.init(CallLog.Calls.CONTENT_URI, selection, null,
-                                   CALLLOG_SORT_ORDER)) {
+                                   CALLLOG_SORT_ORDER) ||
+                                   !buffer.onInit(mContext)) {
                     return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
                 }
 
@@ -479,16 +491,21 @@ public class BluetoothPbapVcardManager {
                         BluetoothPbapObexServer.sIsAborted = false;
                         break;
                     }
-                    if (!composer.createOneEntry()) {
+                    String vcard = composer.createOneEntry();
+                    if (vcard == null) {
                         Log.e(TAG, "Failed to read a contact. Error reason: "
                                 + composer.getErrorReason());
                         return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
                     }
+                    buffer.onEntryCreated(vcard);
                 }
             } finally {
                 if (composer != null) {
                     composer.terminate();
                 }
+                if (buffer != null) {
+                    buffer.onTerminate();
+                }
             }
         }
 
@@ -501,7 +518,7 @@ public class BluetoothPbapVcardManager {
     /**
      * Handler to emit VCard String to PCE once size grow to maxPacketSize.
      */
-    public class HandlerForStringBuffer implements OneEntryHandler {
+    public class HandlerForStringBuffer {
         private Operation operation;
 
         private OutputStream outputStream;