ADD file via upload

shenyifei
p3o5nagmu 8 months ago
parent 19aa954d80
commit b04237380e

@ -0,0 +1,92 @@
/*
* 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;
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 {
// 用于缓存联系人信息的哈希表,以电话号码为键,联系人姓名为值,目的是避免重复查询数据库获取相同联系人信息
private static HashMap<String, String> sContactCache;
// 用于日志记录的标签方便在Logcat中识别该类输出的日志信息
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 = '+')";
// 根据给定的上下文通常是Activity等组件的上下文环境和电话号码获取对应的联系人姓名
public static String getContact(Context context, String phoneNumber) {
// 如果联系人缓存为空则创建一个新的HashMap用于缓存联系人信息
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));
// 通过上下文的内容解析器ContentResolver发起一个数据库查询操作
// 查询的是联系人数据的通用URI指定要获取的列这里只获取联系人的显示姓名传入构建好的查询条件以及对应的参数电话号码最后一个参数null表示按默认顺序排序
Cursor cursor = context.getContentResolver().query(
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) {
// 如果在获取游标数据时发生越界异常记录错误日志并返回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