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.
xiaomi-Notes/Contact.java

119 lines
5.4 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;//导入Context类用于访问应用环境
import android.database.Cursor;// 导入 Cursor 类,用于操作查询结果
import android.provider.ContactsContract.CommonDataKinds.Phone;// 导入电话常量
import android.provider.ContactsContract.Data;// 导入数据常量
import android.telephony.PhoneNumberUtils;// 导入电话工具类
import android.util.Log; // 导入日志工具类
import java.util.HashMap;// 导入 HashMap 类,用于存储联系人缓存
public class Contact { // 定义 Contact 类
private static HashMap<String, String> sContactCache;
// 静态 HashMap 用于缓存联系人姓名
private static final String TAG = "Contact";
// 日志标签,用于标识日志信息
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) {
//初始化联系人缓存
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));
//执行内容解析器查询,获取与电话号码匹配的联系人姓名
Cursor cursor = context.getContentResolver().query(
//查询的 UIR
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;
}
}
}
/*
缓存检查:
首先检查 sContactCache 是否已初始化,如果未初始化,则创建一个新的 HashMap。
然后检查缓存中是否已经存有该电话号码的联系人姓名。如果有,直接返回缓存中的结果。
查询联系人:
如果缓存中没有该联系人,构建一个查询条件 selection并用 PhoneNumberUtils.toCallerIDMinMatch(phoneNumber) 方法将 + 符号替换为号码的最小匹配形式。
通过 context.getContentResolver().query 方法查询联系人数据库,查询条件是 selection查询的列为联系人显示姓名Phone.DISPLAY_NAME
结果处理:
如果查询返回有效的游标(即找到联系人信息),则尝试获取第一个结果的联系人姓名。
将获取到的联系人姓名缓存起来,方便下次快速查询。
错误处理:
如果发生异常(例如游标获取数据时出现 IndexOutOfBoundsException则记录错误日志并返回 null。
如果查询没有返回任何联系人数据,则记录调试日志,并返回 null。
资源释放:
确保游标在操作完成后关闭,以释放数据库资源。
总结
这段代码的功能是通过电话号码查询并返回对应的联系人姓名。为了提高效率,查询结果会被缓存,以避免重复查询数据库。代码充分利用了 ContentResolver 进行联系人数据的查询,并在出现问题时进行了异常处理和日志记录。
*/