PBAP: Apply listStartOffset in the final list.

Precondition
Have 2 numbers with same name, with different handles, for example
1.vcf and 5.vcf on DUT

Usecase
1) Connect from carkit for PBAP
2) Search by name, with offset - 0
3) Search by name, with offset - 1
4) Search by name, with offset - 2

Expected behaviour
Carkit is able to show correct list with offset as specified by user

Observed result
 ListStartOffset does not work as expected for PullvCardListing Function.

Fix:
This change will apply the offet value in the final list of vcard values prepared after comparing
with the search value as per the specification. ListStartOffset was being applied on the original
list of names earlier returning extra set of values of names while Vcardlisting.

Test: Perform the usecase as described above and at step2, 1.vcf and 5.vcf are shown,
at step3, 5.vcf are shown and at step4, Nothing is shown.

Bug: 35026764
Change-Id: I36d757a12d0cc94628d4f4ad91c9370397035c4e
diff --git a/android/app/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java b/android/app/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
old mode 100644
new mode 100755
index 247a76d..979becd
--- a/android/app/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
+++ b/android/app/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
@@ -817,50 +817,65 @@
         }
 
         if (type.equals("number")) {
+            ArrayList<Integer> savedPosList = new ArrayList<>();
+            ArrayList<String> selectedNameList = new ArrayList<String>();
             // query the number, to get the names
             ArrayList<String> names =
                     mVcardManager.getContactNamesByNumber(appParamValue.searchValue);
             if (mOrderBy == ORDER_BY_ALPHABETICAL) Collections.sort(names);
             for (int i = 0; i < names.size(); i++) {
                 compareValue = names.get(i).trim();
-                if (D) {
-                    Log.d(TAG, "compareValue=" + compareValue);
-                }
-                for (int pos = appParamValue.listStartOffset;
-                        pos < listSize && itemsFound < requestSize; pos++) {
+                if (D) Log.d(TAG, "compareValue=" + compareValue);
+                for (int pos = 0; pos < listSize; pos++) {
                     currentValue = nameList.get(pos);
                     if (V) {
                         Log.d(TAG, "currentValue=" + currentValue);
                     }
                     if (currentValue.equals(compareValue)) {
-                        itemsFound++;
                         if (currentValue.contains(",")) {
                             currentValue = currentValue.substring(0, currentValue.lastIndexOf(','));
                         }
-                        writeVCardEntry(pos, currentValue, result);
+                        selectedNameList.add(currentValue);
+                        savedPosList.add(pos);
                     }
                 }
-                if (itemsFound >= requestSize) {
-                    break;
-                }
             }
+
+            for (int j = appParamValue.listStartOffset;
+                    j < selectedNameList.size() && itemsFound < requestSize; j++) {
+                itemsFound++;
+                writeVCardEntry(savedPosList.get(j), selectedNameList.get(j), result);
+            }
+
         } else {
+            ArrayList<Integer> savedPosList = new ArrayList<>();
+            ArrayList<String> selectedNameList = new ArrayList<String>();
             if (appParamValue.searchValue != null) {
                 compareValue = appParamValue.searchValue.trim().toLowerCase();
             }
-            for (int pos = appParamValue.listStartOffset;
-                    pos < listSize && itemsFound < requestSize; pos++) {
+
+            for (int pos = 0; pos < listSize; pos++) {
                 currentValue = nameList.get(pos);
+
                 if (currentValue.contains(",")) {
                     currentValue = currentValue.substring(0, currentValue.lastIndexOf(','));
                 }
 
-                if (appParamValue.searchValue.isEmpty() || ((currentValue.toLowerCase()).startsWith(
-                        compareValue))) {
-                    itemsFound++;
-                    writeVCardEntry(pos, currentValue, result);
+                if (appParamValue.searchValue != null) {
+                    if (appParamValue.searchValue.isEmpty()
+                            || ((currentValue.toLowerCase())
+                                               .startsWith(compareValue.toLowerCase()))) {
+                        selectedNameList.add(currentValue);
+                        savedPosList.add(pos);
+                    }
                 }
             }
+
+            for (int i = appParamValue.listStartOffset;
+                    i < selectedNameList.size() && itemsFound < requestSize; i++) {
+                itemsFound++;
+                writeVCardEntry(savedPosList.get(i), selectedNameList.get(i), result);
+            }
         }
         return itemsFound;
     }
diff --git a/android/app/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java b/android/app/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
old mode 100644
new mode 100755
index e58fa2e..5ba2b4b
--- a/android/app/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
+++ b/android/app/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
@@ -332,12 +332,18 @@
             contactCursor = mResolver.query(myUri, PHONES_CONTACTS_PROJECTION, null, null,
                     Phone.CONTACT_ID);
 
+            ArrayList<String> contactNameIdList = new ArrayList<String>();
+            appendDistinctNameIdList(contactNameIdList,
+                    mContext.getString(android.R.string.unknownName), contactCursor);
+
             if (contactCursor != null) {
                 if (!composer.initWithCallback(contactCursor,
                         new EnterpriseRawContactEntitlesInfoCallback())) {
                     return nameList;
                 }
 
+                int i = 0;
+                contactCursor.moveToFirst();
                 while (!composer.isAfterLast()) {
                     String vcard = composer.createOneEntry();
                     if (vcard == null) {
@@ -362,8 +368,9 @@
                         if (TextUtils.isEmpty(name)) {
                             name = mContext.getString(android.R.string.unknownName);
                         }
-                        nameList.add(name);
+                        nameList.add(contactNameIdList.get(i));
                     }
+                    i++;
                 }
                 if (orderByWhat == BluetoothPbapObexServer.ORDER_BY_INDEXED) {
                     if (V) {