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.
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,
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();
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() {
}
public void terminate() {
- for (OneEntryHandler handler : mHandlerList) {
- handler.onTerminate();
- }
-
if (mCursor != null) {
try {
mCursor.close();
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;
}
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,
if (isContacts) {
VCardComposer composer = null;
+ HandlerForStringBuffer buffer = null;
try {
// Currently only support Generic Vcard 2.1 and 3.0
int vcardType;
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;
}
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;
}
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();
+ }
}
}
/**
* 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;