Delete 'Contact.java'

pcpgfftbm 2 months ago
parent eca2242f2f
commit a6984d6bac

@ -1,87 +0,0 @@
/*
* 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; // 导入Android上下文类用于访问系统资源
import android.database.Cursor; // 导入Android游标类用于查询数据库
import android.provider.ContactsContract.CommonDataKinds.Phone; // 导入Android联系人电话信息类
import android.provider.ContactsContract.Data; // 导入Android联系人数据类
import android.telephony.PhoneNumberUtils; // 导入Android电话号码工具类
import android.util.Log; // 导入Android日志工具类
import java.util.HashMap; // 导入Java哈希表类用于缓存查询结果
public class Contact {
// 静态的哈希表,用于缓存电话号码和对应的联系人姓名
private static HashMap<String, String> sContactCache;
// 日志标签,用于在日志中标识来自该类的消息
private static final String TAG = "Contact";
// SQL查询条件用于查找与给定电话号码匹配的联系人信息
// 使用PHONE_NUMBERS_EQUAL函数比较电话号码确保号码格式的一致性
// 限制数据类型为电话号码类型,并通过子查询确保电话号码的最小匹配字符为'+'
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); // 如果存在,则直接返回缓存中的姓名
}
// 构造SQL查询条件将占位符"+"替换为电话号码的最小匹配字符
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
// 执行数据库查询,获取与电话号码匹配的联系人姓名
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI, // 查询联系人数据表
new String [] { Phone.DISPLAY_NAME }, // 需要查询的字段:联系人显示名称
selection, // 查询条件
new String[] { phoneNumber }, // 查询条件中的占位符参数
null); // 排序条件这里为null表示不排序
// 检查查询结果是否为空且是否可以移动到第一行
if (cursor != null && cursor.moveToFirst()) {
try {
String name = cursor.getString(0); // 获取联系人显示名称
sContactCache.put(phoneNumber, name); // 将查询结果存入缓存
return name; // 返回联系人姓名
} catch (IndexOutOfBoundsException e) {
// 如果发生数组越界异常记录错误日志并返回null
Log.e(TAG, " Cursor get string error " + e.toString());
return null;
} finally {
cursor.close(); // 关闭游标,释放资源
}
} else {
// 如果没有找到匹配的联系人记录调试日志并返回null
Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null;
}
}
}
Loading…
Cancel
Save