Browse Source

优化通讯录查询工具

weicu
liwei1dao 1 year ago
parent
commit
2237cd5741
  1. 21
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt

21
local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt

@ -87,17 +87,7 @@ class SystemFunctionHandler(private val context: Context? = null) {
description = "获取用户通信录联系人列表", description = "获取用户通信录联系人列表",
parameters = mapOf( parameters = mapOf(
"type" to "object", "type" to "object",
"properties" to mapOf( "properties" to emptyMap<String, Any>(),
"limit" to mapOf(
"type" to "integer",
"description" to "返回联系人数量限制,默认20个",
"default" to 20
),
"search" to mapOf(
"type" to "string",
"description" to "搜索关键词,可选参数"
)
),
"required" to emptyList<String>() "required" to emptyList<String>()
), ),
handler = GetContactsListHandler(context) handler = GetContactsListHandler(context)
@ -1378,10 +1368,7 @@ private class GetContactsListHandler(private val context: Context?) : FunctionHa
} }
return try { return try {
val limit = (arguments["limit"] as? Number)?.toInt() ?: 20 val contacts = getContactsList(context)
val searchKeyword = arguments["search"] as? String
val contacts = getContactsList(context, limit, searchKeyword)
JSONObject().apply { JSONObject().apply {
put("result", "成功获取联系人列表") put("result", "成功获取联系人列表")
@ -1406,7 +1393,7 @@ private class GetContactsListHandler(private val context: Context?) : FunctionHa
* @param searchKeyword 搜索关键词 * @param searchKeyword 搜索关键词
* @return 联系人列表 * @return 联系人列表
*/ */
private fun getContactsList(context: Context, limit: Int, searchKeyword: String?): List<Map<String, String>> { private fun getContactsList(context: Context): List<Map<String, String>> {
val contacts = mutableListOf<Map<String, String>>() val contacts = mutableListOf<Map<String, String>>()
try { try {
@ -1433,7 +1420,7 @@ private class GetContactsListHandler(private val context: Context?) : FunctionHa
val nameColumn = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME) val nameColumn = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
val hasPhoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER) val hasPhoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)
while (cursor.moveToNext() && contacts.size < limit) { while (cursor.moveToNext()) {
val contactId = cursor.getString(idColumn) val contactId = cursor.getString(idColumn)
val displayName = cursor.getString(nameColumn) ?: "未知联系人" val displayName = cursor.getString(nameColumn) ?: "未知联系人"
val hasPhoneNumber = cursor.getInt(hasPhoneColumn) > 0 val hasPhoneNumber = cursor.getInt(hasPhoneColumn) > 0

Loading…
Cancel
Save