PBAPS: Get vcard when asked in alphabetical order

Precondition:
=============
- PSE should have atleast 40 contacts with all alphabetical permutations

Step by Step procedure:
=======================
1. Establish PBAP session. Navigate to "Browse" Tab
2. Select telecom/pb.vcf on remote device and select "SETPHONEBOOK"
3. Select "Get Size" from remote device
4. Select default sorting, maxlistcount = 5000 and click "Search" to
   browse phonebook
5. Select "Alphabetical" sorting, maxlistcount = 5000 and click "Search"
   to browse phonebook

Actual Result
============================
After alphabetical sorting the contacts are displayed according to index
but not with sorted request

Expected result:
=========================
When contacts are sorted according to alphabets the contact info should
also be displayed according to contacts.

Root Cause:
========================
While quering vcard entry from database, proper checks of the order (indexed/alphabetical)
was not there in M Stock code compared to L Stock code.

Change-Id: Ie7990992b4dd22add3526aa0050121e680567bb8
diff --git a/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java b/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
index 748e7a5..702eb2d 100644
--- a/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
+++ b/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
@@ -261,23 +261,23 @@
 
         final Uri myUri = DevicePolicyUtils.getEnterprisePhoneUri(mContext);
         Cursor contactCursor = null;
+        // By default order is indexed
+        String orderBy = Phone.CONTACT_ID;
         try {
-            contactCursor = mResolver.query(myUri, PHONES_CONTACTS_PROJECTION, CLAUSE_ONLY_VISIBLE,
-                    null, Phone.CONTACT_ID);
+            if (orderByWhat == BluetoothPbapObexServer.ORDER_BY_ALPHABETICAL) {
+                orderBy = Phone.DISPLAY_NAME;
+            }
+            contactCursor = mResolver.query(myUri, PHONES_CONTACTS_PROJECTION,
+                CLAUSE_ONLY_VISIBLE, null, orderBy);
             if (contactCursor != null) {
                 appendDistinctNameIdList(nameList,
                         mContext.getString(android.R.string.unknownName),
                         contactCursor);
-                if (orderByWhat == BluetoothPbapObexServer.ORDER_BY_INDEXED) {
-                    if (V) Log.v(TAG, "getPhonebookNameList, order by index");
-                    // Do not need to do anything, as we sort it by index already
-                } else if (orderByWhat == BluetoothPbapObexServer.ORDER_BY_ALPHABETICAL) {
-                    if (V) Log.v(TAG, "getPhonebookNameList, order by alpha");
-                    Collections.sort(nameList);
-                }
             }
+        } catch (CursorWindowAllocationException e) {
+            Log.e(TAG, "CursorWindowAllocationException while getting phonebook name list");
         } catch (Exception e) {
-            Log.e(TAG, "Exception while getting Phonebook name list", e);
+            Log.e(TAG, "Exception while getting phonebook name list", e);
         } finally {
             if (contactCursor != null) {
                 contactCursor.close();
@@ -445,17 +445,22 @@
         Cursor contactIdCursor = new MatrixCursor(new String[] {
             Phone.CONTACT_ID
         });
+        // By default order is indexed
+        String orderBy = Phone.CONTACT_ID;
         try {
+            if (orderByWhat == BluetoothPbapObexServer.ORDER_BY_ALPHABETICAL) {
+                orderBy = Phone.DISPLAY_NAME;
+            }
             contactCursor = mResolver.query(myUri, PHONES_CONTACTS_PROJECTION,
-                    CLAUSE_ONLY_VISIBLE, null, Phone.CONTACT_ID);
-            contactIdCursor = ContactCursorFilter.filterByOffset(contactCursor, offset);
-
+                CLAUSE_ONLY_VISIBLE, null, orderBy);
         } catch (CursorWindowAllocationException e) {
             Log.e(TAG,
-                    "CursorWindowAllocationException while composing phonebook one vcard");
+                "CursorWindowAllocationException while composing phonebook one vcard");
         } finally {
             if (contactCursor != null) {
+                contactIdCursor = ContactCursorFilter.filterByOffset(contactCursor, offset);
                 contactCursor.close();
+                contactCursor = null;
             }
         }
         return composeContactsAndSendVCards(op, contactIdCursor, vcardType21, ownerVCard,