Merge "Make "search by number" function work" into gingerbread
diff --git a/src/com/android/bluetooth/pbap/BluetoothPbapCallLogComposer.java b/src/com/android/bluetooth/pbap/BluetoothPbapCallLogComposer.java
old mode 100755
new mode 100644
diff --git a/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java b/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
index cc14634..fbfd823 100644
--- a/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
+++ b/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
@@ -490,7 +490,15 @@
                     i += 1; // length field in triplet
                     // length of search value is variable
                     int length = appParam[i];
-                    appParamValue.searchValue = new String(appParam, i + 1, length);
+                    if (length == 0) {
+                        parseOk = false;
+                        break;
+                    }
+                    if (appParam[i+length] == 0x0) {
+                        appParamValue.searchValue = new String(appParam, i + 1, length-1);
+                    } else {
+                        appParamValue.searchValue = new String(appParam, i + 1, length);
+                    }
                     i += length;
                     i += 1;
                     break;
@@ -549,13 +557,11 @@
         // Phonebook listing request
         if (type == ContentType.PHONEBOOK) {
             if (searchAttr.equals("0")) { // search by name
-                ArrayList<String> nameList = mVcardManager.getPhonebookNameList(mOrderBy );
                 itemsFound = createList(maxListCount, listStartOffset, searchValue, result,
-                        nameList, "name");
+                        "name");
             } else if (searchAttr.equals("1")) { // search by number
-                ArrayList<String> numberList = mVcardManager.getPhonebookNumberList();
                 itemsFound = createList(maxListCount, listStartOffset, searchValue, result,
-                        numberList, "number");
+                        "number");
             }// end of search by number
             else {
                 return ResponseCodes.OBEX_HTTP_PRECON_FAILED;
@@ -587,20 +593,49 @@
     }
 
     private int createList(final int maxListCount, final int listStartOffset,
-            final String searchValue, StringBuilder result,
-            ArrayList<String> dataList, String type) {
+            final String searchValue, StringBuilder result, String type) {
         int itemsFound = 0;
-        int requestSize = dataList.size() >= maxListCount ? maxListCount : dataList.size();
+        ArrayList<String> nameList = mVcardManager.getPhonebookNameList(mOrderBy);
+        final int requestSize = nameList.size() >= maxListCount ? maxListCount : nameList.size();
+        final int listSize = nameList.size();
+        String compareValue = "", currentValue;
 
-        if (D) Log.d(TAG, "search by " + type + ", size=" + requestSize + " offset="
+        if (D) Log.d(TAG, "search by " + type + ", requestSize=" + requestSize + " offset="
                     + listStartOffset + " searchValue=" + searchValue);
 
-        for (int pos = listStartOffset; pos < dataList.size() && itemsFound < requestSize; pos++) {
-            String currentValue = dataList.get(pos);
-            if (searchValue == null || currentValue.startsWith(searchValue.trim())) {
-                itemsFound++;
-                result.append("<card handle=\"" + pos + ".vcf\" " + type + "=\""
-                        + currentValue + "\"" + "/>");
+        if (type.equals("number")) {
+            // query the number, to get the names
+            ArrayList<String> names = mVcardManager.getContactNamesByNumber(searchValue);
+            for (int i = 0; i < names.size(); i++) {
+                compareValue = names.get(i).trim();
+                if (D) Log.d(TAG, "compareValue=" + compareValue);
+                for (int pos = listStartOffset; pos < listSize &&
+                        itemsFound < requestSize; pos++) {
+                    currentValue = nameList.get(pos);
+                    if (D) Log.d(TAG, "currentValue=" + currentValue);
+                    if (currentValue.startsWith(compareValue)) {
+                        itemsFound++;
+                        result.append("<card handle=\"" + pos + ".vcf\" name=\""
+                                + currentValue + "\"" + "/>");
+                    }
+                }
+                if (itemsFound >= requestSize) {
+                    break;
+                }
+            }
+        } else {
+            if (searchValue != null) {
+                compareValue = searchValue.trim();
+            }
+            for (int pos = listStartOffset; pos < listSize &&
+                    itemsFound < requestSize; pos++) {
+                currentValue = nameList.get(pos);
+                if (D) Log.d(TAG, "currentValue=" + currentValue);
+                if (searchValue == null || currentValue.startsWith(compareValue)) {
+                    itemsFound++;
+                    result.append("<card handle=\"" + pos + ".vcf\" name=\""
+                            + currentValue + "\"" + "/>");
+                }
             }
         }
         return itemsFound;
@@ -791,7 +826,7 @@
         String orderPara = appParamValue.order.trim();
         if (TextUtils.isEmpty(orderPara)) {
             // If order parameter is not set by PCE, set default value per spec.
-            appParamValue.order = "0";
+            orderPara = "0";
             if (D) Log.d(TAG, "Order parameter is not set by PCE. " +
                        "Assume order by 'Indexed' by default");
         } else if (!orderPara.equals("0") && !orderPara.equals("1")) {
diff --git a/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java b/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
index 8db1503..b134b42 100644
--- a/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
+++ b/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
@@ -45,6 +45,7 @@
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.Data;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
+import android.provider.ContactsContract.PhoneLookup;
 import android.text.TextUtils;
 import android.util.Log;
 
@@ -211,9 +212,11 @@
         Cursor contactCursor = null;
         try {
             if (orderByWhat == BluetoothPbapObexServer.ORDER_BY_INDEXED) {
+                if (V) Log.v(TAG, "getPhonebookNameList, order by index");
                 contactCursor = mResolver.query(myUri, CONTACTS_PROJECTION, CLAUSE_ONLY_VISIBLE,
                         null, Contacts._ID);
             } else if (orderByWhat == BluetoothPbapObexServer.ORDER_BY_ALPHABETICAL) {
+                if (V) Log.v(TAG, "getPhonebookNameList, order by alpha");
                 contactCursor = mResolver.query(myUri, CONTACTS_PROJECTION, CLAUSE_ONLY_VISIBLE,
                         null, Contacts.DISPLAY_NAME);
             }
@@ -235,31 +238,35 @@
         return nameList;
     }
 
-    public final ArrayList<String> getPhonebookNumberList() {
-        ArrayList<String> numberList = new ArrayList<String>();
-        numberList.add(BluetoothPbapService.getLocalPhoneNum());
+    public final ArrayList<String> getContactNamesByNumber(final String phoneNumber) {
+        ArrayList<String> nameList = new ArrayList<String>();
 
-        final Uri myUri = Phone.CONTENT_URI;
-        Cursor phoneCursor = null;
+        Cursor contactCursor = null;
+        final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
+                Uri.encode(phoneNumber));
+
         try {
-            phoneCursor = mResolver.query(myUri, PHONES_PROJECTION, CLAUSE_ONLY_VISIBLE, null,
-                    SORT_ORDER_PHONE_NUMBER);
-            if (phoneCursor != null) {
-                for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor
+            contactCursor = mResolver.query(uri, CONTACTS_PROJECTION, CLAUSE_ONLY_VISIBLE,
+                        null, Contacts._ID);
+
+            if (contactCursor != null) {
+                for (contactCursor.moveToFirst(); !contactCursor.isAfterLast(); contactCursor
                         .moveToNext()) {
-                    String number = phoneCursor.getString(PHONE_NUMBER_COLUMN_INDEX);
-                    if (TextUtils.isEmpty(number)) {
-                        number = mContext.getString(R.string.defaultnumber);
+                    String name = contactCursor.getString(CONTACTS_NAME_COLUMN_INDEX);
+                    long id = contactCursor.getLong(CONTACTS_ID_COLUMN_INDEX);
+                    if (TextUtils.isEmpty(name)) {
+                        name = mContext.getString(android.R.string.unknownName);
                     }
-                    numberList.add(number);
+                    if (V) Log.v(TAG, "got name " + name + " by number " + phoneNumber + " @" + id);
+                    nameList.add(name);
                 }
             }
         } finally {
-            if (phoneCursor != null) {
-                phoneCursor.close();
+            if (contactCursor != null) {
+                contactCursor.close();
             }
         }
-        return numberList;
+        return nameList;
     }
 
     public final int composeAndSendCallLogVcards(final int type, Operation op,