From 0dcd2262d853c2011e11617a8efba6758370c41f Mon Sep 17 00:00:00 2001 From: Liejun Tao Date: Tue, 5 Oct 2010 12:21:25 -0500 Subject: [PATCH] Make "search by number" function work Previous "search by number" is wrong. Re-implement it to make it work. Add flexible to handle searchValue as both null-terminated and not null-terminated. Change-Id: Ie50f8d29dfcbae868c43d482467b09b08ed2be0f --- .../pbap/BluetoothPbapCallLogComposer.java | 0 .../bluetooth/pbap/BluetoothPbapObexServer.java | 67 ++++++++++++++++------ .../bluetooth/pbap/BluetoothPbapVcardManager.java | 39 +++++++------ 3 files changed, 74 insertions(+), 32 deletions(-) mode change 100755 => 100644 src/com/android/bluetooth/pbap/BluetoothPbapCallLogComposer.java 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 cc14634f..fbfd8236 100644 --- a/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java +++ b/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java @@ -490,7 +490,15 @@ public class BluetoothPbapObexServer extends ServerRequestHandler { 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 @@ public class BluetoothPbapObexServer extends ServerRequestHandler { // Phonebook listing request if (type == ContentType.PHONEBOOK) { if (searchAttr.equals("0")) { // search by name - ArrayList nameList = mVcardManager.getPhonebookNameList(mOrderBy ); itemsFound = createList(maxListCount, listStartOffset, searchValue, result, - nameList, "name"); + "name"); } else if (searchAttr.equals("1")) { // search by number - ArrayList 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 @@ public class BluetoothPbapObexServer extends ServerRequestHandler { } private int createList(final int maxListCount, final int listStartOffset, - final String searchValue, StringBuilder result, - ArrayList dataList, String type) { + final String searchValue, StringBuilder result, String type) { int itemsFound = 0; - int requestSize = dataList.size() >= maxListCount ? maxListCount : dataList.size(); + ArrayList 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(""); + if (type.equals("number")) { + // query the number, to get the names + ArrayList 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(""); + } + } + 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(""); + } } } return itemsFound; @@ -791,7 +826,7 @@ public class BluetoothPbapObexServer extends ServerRequestHandler { 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 8db15030..b134b429 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.CommonDataKinds; 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 @@ public class BluetoothPbapVcardManager { 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 @@ public class BluetoothPbapVcardManager { return nameList; } - public final ArrayList getPhonebookNumberList() { - ArrayList numberList = new ArrayList(); - numberList.add(BluetoothPbapService.getLocalPhoneNum()); + public final ArrayList getContactNamesByNumber(final String phoneNumber) { + ArrayList nameList = new ArrayList(); + + Cursor contactCursor = null; + final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, + Uri.encode(phoneNumber)); - final Uri myUri = Phone.CONTENT_URI; - Cursor phoneCursor = null; 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, -- 2.11.0