You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
text123/Contact.java

104 lines
5.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.data;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import java.util.HashMap;
// Contact类负责处理与联系人信息获取相关的功能
public class Contact {
// 用于缓存联系人电话号码和对应姓名的映射关系,提高查询效率
private static HashMap<String, String> sContactCache;
// 用于在日志输出中标识该类
private static final String TAG = "Contact";
// 定义一个SQL查询选择条件字符串用于查询联系人数据库中与指定电话号码匹配的记录
// 这里使用了ContactsContract中的相关常量来构建复杂的查询条件
// PHONE_NUMBERS_EQUAL函数用于比较电话号码
// Data.MIMETYPE 限定数据类型为电话号码类型
// Data.RAW_CONTACT_ID 用于关联查询确保原始联系人ID在特定查询结果范围内
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
+ " AND " + Data.RAW_CONTACT_ID + " IN "
+ "(SELECT raw_contact_id "
+ " FROM phone_lookup"
+ " WHERE min_match = '+')";
// 根据提供的上下文和电话号码,从联系人数据库中获取对应的联系人姓名
public static String getContact(Context context, String phoneNumber) {
// 如果缓存尚未初始化创建一个新的HashMap用于缓存联系人信息
if (sContactCache == null) {
sContactCache = new HashMap<String, String>();
}
// 检查缓存中是否已经存在该电话号码对应的联系人姓名
// 如果存在,直接从缓存中返回该姓名,避免重复查询数据库
if (sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber);
}
// 将通用的查询选择条件中的“+”替换为根据给定电话号码生成的最小匹配格式
// 以便构建针对该电话号码的具体查询条件
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
// 使用ContentResolver从联系人数据库中查询符合条件的记录
// Data.CONTENT_URI 是联系人数据的URI
// 只请求返回Phone.DISPLAY_NAME这一列即联系人的显示名称
// selection 是构建好的查询条件
// new String[]{phoneNumber} 是查询条件中的参数,即要查询的电话号码
// null 表示不指定排序方式
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI,
new String[]{Phone.DISPLAY_NAME},
selection,
new String[]{phoneNumber},
null);
// 如果查询结果不为空且游标能够移动到第一条记录(表示查询到了匹配的联系人)
if (cursor!= null && cursor.moveToFirst()) {
try {
// 从查询结果的第一条记录中获取联系人的显示名称
String name = cursor.getString(0);
// 将查询到的电话号码和联系人姓名存入缓存,以便后续查询使用
sContactCache.put(phoneNumber, name);
// 返回查询到的联系人姓名
return name;
} catch (IndexOutOfBoundsException e) {
// 如果在获取字符串时发生索引越界异常(可能是查询结果结构与预期不符)
// 记录错误日志,包含异常信息
Log.e(TAG, " Cursor get string error " + e.toString());
// 返回null表示获取联系人姓名失败
return null;
} finally {
// 无论是否发生异常,都要关闭游标,释放资源
cursor.close();
}
} else {
// 如果没有查询到匹配的联系人记录
// 记录调试日志,说明没有找到与给定电话号码匹配的联系人
Log.d(TAG, "No contact matched with number:" + phoneNumber);
// 返回null表示没有找到匹配的联系人姓名
return null;
}
}
}