PBAP takes Account so that VCardList can add them to operation list.

Since VCardList can create the commits for the accounts database, we
need to add Account information to the parser. This avoids having to do
this in client code.

Bug: b/26862739

Change-Id: I7690489c4e133c09dd4da6c8e3736ea71bb23a4b
diff --git a/src/android/bluetooth/client/pbap/BluetoothPbapClient.java b/src/android/bluetooth/client/pbap/BluetoothPbapClient.java
index 5e212e8..b1fc441 100644
--- a/src/android/bluetooth/client/pbap/BluetoothPbapClient.java
+++ b/src/android/bluetooth/client/pbap/BluetoothPbapClient.java
@@ -16,6 +16,7 @@
 
 package android.bluetooth.client.pbap;
 
+import android.accounts.Account;
 import android.bluetooth.BluetoothDevice;
 import android.os.Handler;
 import android.os.Message;
@@ -72,6 +73,8 @@
  * connection and disconnection happens automatically internally.
  */
 public class BluetoothPbapClient {
+    private static final boolean DBG = true;
+
     private static final String TAG = "BluetoothPbapClient";
 
     /**
@@ -372,6 +375,7 @@
         DISCONNECTED, CONNECTING, CONNECTED, DISCONNECTING;
     }
 
+    private final Account mAccount;
     private final Handler mClientHandler;
     private final BluetoothPbapSession mSession;
     private ConnectionState mConnectionState = ConnectionState.DISCONNECTED;
@@ -503,14 +507,20 @@
      *
      * @param device BluetoothDevice that corresponds to remote acting in PSE
      *            role
+     * @param account the account to which contacts will be added {@see #pullPhoneBook}.
      * @param handler the handle that will be used by PCE to notify events and
      *            results to application
      * @throws NullPointerException
      */
-    public BluetoothPbapClient(BluetoothDevice device, Handler handler) {
-        if (device == null) {
-            throw new NullPointerException("BluetothDevice is null");
+    public BluetoothPbapClient(BluetoothDevice device, Account account, Handler handler) {
+        if (DBG) {
+            Log.d(TAG, " device " + device + " account " + account);
         }
+        if (device == null) {
+            throw new NullPointerException("BluetoothDevice is null");
+        }
+
+        mAccount = account;
 
         mClientHandler = handler;
 
@@ -696,8 +706,8 @@
      */
     public boolean pullPhoneBook(String pbName, long filter, byte format, int maxListCount,
             int listStartOffset) {
-        BluetoothPbapRequest req = new BluetoothPbapRequestPullPhoneBook(pbName, filter, format,
-                maxListCount, listStartOffset);
+        BluetoothPbapRequest req = new BluetoothPbapRequestPullPhoneBook(
+                pbName, mAccount, filter, format, maxListCount, listStartOffset);
         return mSession.makeRequest(req);
     }
 
@@ -835,7 +845,8 @@
      * @link #EVENT_PULL_VCARD_ERROR} in case of failure
      */
     public boolean pullVcardEntry(String handle, long filter, byte format) {
-        BluetoothPbapRequest req = new BluetoothPbapRequestPullVcardEntry(handle, filter, format);
+        BluetoothPbapRequest req =
+                new BluetoothPbapRequestPullVcardEntry(handle, mAccount, filter, format);
         return mSession.makeRequest(req);
     }
 
diff --git a/src/android/bluetooth/client/pbap/BluetoothPbapRequestPullPhoneBook.java b/src/android/bluetooth/client/pbap/BluetoothPbapRequestPullPhoneBook.java
index 3d9f018..411a8de 100644
--- a/src/android/bluetooth/client/pbap/BluetoothPbapRequestPullPhoneBook.java
+++ b/src/android/bluetooth/client/pbap/BluetoothPbapRequestPullPhoneBook.java
@@ -16,6 +16,7 @@
 
 package android.bluetooth.client.pbap;
 
+import android.accounts.Account;
 import android.util.Log;
 
 import com.android.vcard.VCardEntry;
@@ -37,13 +38,16 @@
 
     private BluetoothPbapVcardList mResponse;
 
+    private Account mAccount;
+
     private int mNewMissedCalls = -1;
 
     private final byte mFormat;
 
-    public BluetoothPbapRequestPullPhoneBook(String pbName, long filter, byte format,
+    public BluetoothPbapRequestPullPhoneBook(
+            String pbName, Account account, long filter, byte format,
             int maxListCount, int listStartOffset) {
-
+        mAccount = account;
         if (maxListCount < 0 || maxListCount > 65535) {
             throw new IllegalArgumentException("maxListCount should be [0..65535]");
         }
@@ -93,7 +97,7 @@
     protected void readResponse(InputStream stream) throws IOException {
         Log.v(TAG, "readResponse");
 
-        mResponse = new BluetoothPbapVcardList(stream, mFormat);
+        mResponse = new BluetoothPbapVcardList(mAccount, stream, mFormat);
         if (DBG) {
             Log.d(TAG, "Read " + mResponse.getCount() + " entries.");
         }
diff --git a/src/android/bluetooth/client/pbap/BluetoothPbapRequestPullVcardEntry.java b/src/android/bluetooth/client/pbap/BluetoothPbapRequestPullVcardEntry.java
index 42b6692..ed823d5 100644
--- a/src/android/bluetooth/client/pbap/BluetoothPbapRequestPullVcardEntry.java
+++ b/src/android/bluetooth/client/pbap/BluetoothPbapRequestPullVcardEntry.java
@@ -16,6 +16,7 @@
 
 package android.bluetooth.client.pbap;
 
+import android.accounts.Account;
 import android.util.Log;
 
 import com.android.vcard.VCardEntry;
@@ -35,9 +36,14 @@
 
     private BluetoothPbapVcardList mResponse;
 
+    private final Account mAccount;
+
     private final byte mFormat;
 
-    public BluetoothPbapRequestPullVcardEntry(String handle, long filter, byte format) {
+    public BluetoothPbapRequestPullVcardEntry(
+            String handle, Account account, long filter, byte format) {
+        mAccount = account;
+
         mHeaderSet.setHeader(HeaderSet.NAME, handle);
 
         mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);
@@ -64,7 +70,7 @@
     protected void readResponse(InputStream stream) throws IOException {
         Log.v(TAG, "readResponse");
 
-        mResponse = new BluetoothPbapVcardList(stream, mFormat);
+        mResponse = new BluetoothPbapVcardList(mAccount, stream, mFormat);
     }
     @Override
     protected void checkResponseCode(int responseCode) throws IOException {
diff --git a/src/android/bluetooth/client/pbap/BluetoothPbapVcardList.java b/src/android/bluetooth/client/pbap/BluetoothPbapVcardList.java
index 8e23e1a..6bc75de 100644
--- a/src/android/bluetooth/client/pbap/BluetoothPbapVcardList.java
+++ b/src/android/bluetooth/client/pbap/BluetoothPbapVcardList.java
@@ -16,6 +16,9 @@
 
 package android.bluetooth.client.pbap;
 
+import android.accounts.Account;
+
+import com.android.vcard.VCardConfig;
 import com.android.vcard.VCardEntry;
 import com.android.vcard.VCardEntryConstructor;
 import com.android.vcard.VCardEntryCounter;
@@ -32,6 +35,7 @@
 class BluetoothPbapVcardList {
 
     private final ArrayList<VCardEntry> mCards = new ArrayList<VCardEntry>();
+    private final Account mAccount;
 
     class CardEntryHandler implements VCardEntryHandler {
         @Override
@@ -48,7 +52,8 @@
         }
     }
 
-    public BluetoothPbapVcardList(InputStream in, byte format) throws IOException {
+    public BluetoothPbapVcardList(Account account, InputStream in, byte format) throws IOException {
+        mAccount = account;
         parse(in, format);
     }
 
@@ -61,7 +66,8 @@
             parser = new VCardParser_V21();
         }
 
-        VCardEntryConstructor constructor = new VCardEntryConstructor();
+        VCardEntryConstructor constructor =
+            new VCardEntryConstructor(VCardConfig.VCARD_TYPE_V21_GENERIC, mAccount);
         VCardEntryCounter counter = new VCardEntryCounter();
         CardEntryHandler handler = new CardEntryHandler();