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.
MiNote/src/data/Contact.java

77 lines
4.8 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.

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 { //联系人
// 定义一个私有的静态的HashMap对象用于缓存联系人信息。键为电话号码值为联系人的名字。
private static HashMap<String, String> sContactCache;
// 定义一个私有的常量String作为日志的标签方便调试。
private static final String TAG = "Contact";
// 定义一个私有的静态常量字符串CALLER_ID_SELECTION包含用于查询联系人的SQL语句。这个语句基于电话号码和MIME类型进行查询。
// 这是一个复杂的查询它从电话号码和MIME类型中选择联系人信息并且它使用了子查询来获取匹配的电话号码。
// 这个查询的具体含义和用法需要依赖它所在的上下文和相关的数据库表结构。
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL("
+ Phone.NUMBER // Phone.NUMBER应该是Phone类中的一个属性表示电话号码。
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" // 数据中的MIME类型等于电话内容项类型。这通常用于确定联系人的信息。
+ " AND " + Data.RAW_CONTACT_ID + " IN " // 数据中的原始联系人ID在...中
+ "(SELECT raw_contact_id " // 子查询选择所有的原始联系人ID
+ " FROM phone_lookup" // 从phone_lookup表中
+ " WHERE min_match = '+')"; // 其中min_match等于'+'。这个min_match字段的含义需要依赖具体的数据库表结构。
// 定义一个公开的静态方法getContact输入参数为Context上下文对象和String电话号码返回值为String联系人的名字。该方法用于获取联系人的名字。
// 如果sContactCache中已经存在该电话号码的信息则直接返回该信息。否则查询数据库获取该电话号码对应的信息并存入sContactCache中。
// 如果查询出现异常或者没有找到相关信息会返回null。
// 获取联系人
public static String getContact(Context context, String phoneNumber) {
// 如果sContactCache为空则初始化它。
if(sContactCache == null) {
sContactCache = new HashMap<String, String>();
}
// 检查sContactCache中是否已经有该phoneNumber的信息。如果有则直接返回该信息。
if(sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber);
}
// 用电话号码替换CALLER_ID_SELECTION中的"+"并存入selection变量中。这个字符串是用于从数据库中查询信息的SQL语句。
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
// 使用selection从数据库中查询phoneNumber的信息。查询的结果存储在Cursor对象中。
// 查找数据库中phoneNumber的信息
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI, // 数据内容URI通常用于访问联系人数据。
new String [] { Phone.DISPLAY_NAME }, // 要获取的字段,这里是要获取联系人的名字。
selection, // 查询的条件这里是电话号码和MIME类型。
new String[] { phoneNumber }, // 查询的参数,这里是电话号码。
null); // 排序方式,这里不排序。
// 判断查询结果。如果Cursor不为空且移动到第一条记录则说明找到了相关信息。
// moveToFirst()返回第一条
if (cursor != null && cursor.moveToFirst()) {
try {
// 获取联系人的名字并存入sContactCache中然后返回该名字。
// 如果获取过程中出现异常则捕获异常并记录错误日志然后返回null。
// 找到相关信息
String name = cursor.getString(0);
sContactCache.put(phoneNumber, name);
return name;
// 异常
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, " Cursor get string error " + e.toString()); // 记录错误日志。
return null; // 返回null。
} finally {
cursor.close(); // 关闭Cursor对象。释放资源。无论是否