diff --git a/src/java/net/micode/notes/data/Contact.java b/src/java/net/micode/notes/data/Contact.java index e4c22e9..33b6628 100644 --- a/src/java/net/micode/notes/data/Contact.java +++ b/src/java/net/micode/notes/data/Contact.java @@ -1,17 +1,15 @@ /* - * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * 版权所有 (c) 2010-2011, MiCode 开源社区 (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 + * 本文件授权使用 Apache License, Version 2.0(以下简称“许可证”); + * 除非符合许可证规定,否则不得使用此文件。 + * 您可以从以下网址获取许可证副本: * * 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; @@ -25,22 +23,24 @@ import android.util.Log; import java.util.HashMap; - -import android.content.Context; -import android.content.ContentResolver; -import android.database.Cursor; -import android.provider.ContactsContract; -import android.provider.ContactsContract.CommonDataKinds.Phone; -import android.provider.ContactsContract.Data; -import android.util.Log; -import android.telephony.PhoneNumberUtils; -import java.util.HashMap; - +/** + * 提供联系人信息查询功能的类。 + * 该类通过电话号码查询对应的联系人姓名,并使用缓存机制提高查询效率。 + */ public class Contact { + /** + * 存储电话号码与联系人姓名映射关系的缓存。 + */ private static HashMap sContactCache; + /** + * 用于日志记录的标识符。 + */ private static final String TAG = "Contact"; - // 用于查询联系人的SQL语句 + /** + * 构建用于查询联系人的 SQL 语句。 + * 该语句用于匹配电话号码并过滤出相应的联系人信息。 + */ 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 " @@ -49,28 +49,28 @@ public class Contact { + " WHERE min_match = '+')"; /** - * 根据电话号码获取联系人姓名。 - * - * @param context 应用上下文 - * @param phoneNumber 电话号码 - * @return 联系人姓名,如果未找到则返回null + * 通过电话号码查询对应的联系人姓名。 + * + * @param context 应用程序的上下文环境。 + * @param phoneNumber 要查询的电话号码。 + * @return 查询到的联系人姓名,若未找到则返回 null。 */ public static String getContact(Context context, String phoneNumber) { - // 初始化联系人缓存 + // 初始化缓存 if (sContactCache == null) { sContactCache = new HashMap(); } - // 检查缓存中是否已有该电话号码对应的联系人姓名 + // 首先检查缓存中是否存在该电话号码对应的联系人姓名 if (sContactCache.containsKey(phoneNumber)) { return sContactCache.get(phoneNumber); } - // 替换SQL语句中的占位符为实际的电话号码匹配模式 + // 将 SQL 语句中的占位符替换为实际的电话号码匹配模式 String selection = CALLER_ID_SELECTION.replace("+", PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); - // 查询联系人数据库 + // 执行数据库查询操作 Cursor cursor = context.getContentResolver().query( Data.CONTENT_URI, new String[]{Phone.DISPLAY_NAME}, @@ -81,46 +81,23 @@ public class Contact { // 处理查询结果 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()); + // 记录异常信息 + Log.e(TAG, "游标获取字符串时出错:" + e.toString()); return null; } finally { - // 关闭游标 + // 确保游标关闭,释放资源 cursor.close(); } } else { - // 日志记录未找到匹配的联系人 - Log.d(TAG, "No contact matched with number:" + phoneNumber); + // 记录未找到匹配联系人的信息 + Log.d(TAG, "未找到与号码匹配的联系人:" + phoneNumber); return null; } } } -/** - * 类注释 -Contact 类用于根据电话号码查询联系人姓名。它使用一个静态缓存 sContactCache 来存储已查询过的电话号码和对应的联系人姓名,以提高查询效率。 -静态变量 -sContactCache:用于缓存电话号码和联系人姓名的映射。 -TAG:用于日志记录的标签。 -CALLER_ID_SELECTION:用于查询联系人的SQL语句,包含电话号码匹配和MIME类型过滤。 -getContact 方法 -参数 -context:当前应用的上下文。 -phoneNumber:需要查询的电话号码。 -返回值 -返回联系人姓名,如果未找到则返回null。 -功能 -首先检查缓存中是否已有该电话号码对应的联系人姓名,如果有则直接返回。 -如果缓存中没有,构造SQL查询语句,查询联系人数据库。 -如果查询到结果,将联系人姓名缓存起来并返回。 -如果未查询到结果,记录日志并返回null。 -异常处理 -使用try-catch块捕获IndexOutOfBoundsException,防止游标操作异常。 -使用finally块确保游标在使用后关闭,释放资源。 - */ \ No newline at end of file