From 2a7bc321fcf8783b245e9ff25aa9944b064442f5 Mon Sep 17 00:00:00 2001 From: dcy <182569992@qq.com> Date: Sun, 22 Dec 2024 14:30:18 +0800 Subject: [PATCH] =?UTF-8?q?2024/11/30=20=E5=AF=B9contact=E5=8C=85=E6=94=B9?= =?UTF-8?q?=E8=BF=9B=E7=BC=93=E5=AD=98=E6=9C=BA=E5=88=B6=E5=B9=B6=E5=8A=A0?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/net/micode/notes/data/Contact.java | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/Notes-master/src/net/micode/notes/data/Contact.java b/src/Notes-master/src/net/micode/notes/data/Contact.java index f75313a..5fd3378 100644 --- a/src/Notes-master/src/net/micode/notes/data/Contact.java +++ b/src/Notes-master/src/net/micode/notes/data/Contact.java @@ -24,50 +24,76 @@ import android.telephony.PhoneNumberUtils; import android.util.Log; import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; public class Contact { //联系人 - private static HashMap sContactCache; + // 定义最大缓存数量,可根据实际情况调整,这里假设最多缓存100个联系人信息 + private static final int MAX_CACHE_SIZE = 100; + // 使用LinkedHashMap来实现缓存,它能保持插入顺序,方便后续按顺序清理较旧的缓存项 + private static LinkedHashMap sContactCache; 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 " + + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" + + " AND " + Data.RAW_CONTACT_ID + " IN " + "(SELECT raw_contact_id " + " FROM phone_lookup" + " WHERE min_match = '+')"; + /** + * 根据给定的电话号码获取对应的联系人姓名 + * + * @param context Android应用上下文,用于访问系统资源,如联系人数据库 + * @param phoneNumber 要查询对应联系人姓名的电话号码 + * @return 如果找到对应的联系人,则返回联系人姓名,否则返回null + */ public static String getContact(Context context, String phoneNumber) { - if(sContactCache == null) { - sContactCache = new HashMap(); + // 初始化缓存,如果缓存还未创建,则创建一个新的LinkedHashMap实例 + if (sContactCache == null) { + sContactCache = new LinkedHashMap() { + // 重写removeEldestEntry方法,用于定义在何种情况下移除最老的缓存项 + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + // 当缓存的数量大于等于最大缓存数量时,返回true,表示需要移除最老的缓存项 + return size() >= MAX_CACHE_SIZE; + } + }; } - if(sContactCache.containsKey(phoneNumber)) { + // 先检查缓存中是否已经存在该电话号码对应的联系人姓名 + if (sContactCache.containsKey(phoneNumber)) { return sContactCache.get(phoneNumber); } + // 根据传入的电话号码构建查询条件,替换特定的占位符等 String selection = CALLER_ID_SELECTION.replace("+", PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); Cursor cursor = context.getContentResolver().query( Data.CONTENT_URI, - new String [] { Phone.DISPLAY_NAME }, + new String[]{Phone.DISPLAY_NAME}, selection, - new String[] { phoneNumber }, + new String[]{phoneNumber}, null); - if (cursor != null && cursor.moveToFirst()) { + 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()); return null; } finally { + // 无论是否出现异常,都要关闭游标,释放资源 cursor.close(); } } else { + // 如果没有查询到对应的联系人,记录一条调试日志 Log.d(TAG, "No contact matched with number:" + phoneNumber); return null; } } -} +} \ No newline at end of file