|
|
/*
|
|
|
* 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;//当前文件所在的包名
|
|
|
/*接下来引用android的一些库*/
|
|
|
import android.content.Context;//调用Android自带的类
|
|
|
import android.database.Cursor;//从数据库导入Cursor
|
|
|
import android.provider.ContactsContract.CommonDataKinds.Phone;//导入联系人电话
|
|
|
import android.provider.ContactsContract.Data;//存储通讯信息
|
|
|
import android.telephony.PhoneNumberUtils;//格式化用户输入的电话号码
|
|
|
import android.util.Log;//安卓日志输出工具类
|
|
|
|
|
|
import java.util.HashMap;//用hashmap存储联系人信息
|
|
|
|
|
|
public class Contact {//存放联系人信息
|
|
|
private static HashMap<String, String> sContactCache;//存储联系人及对应的电话号码
|
|
|
private static final String TAG = "Contact";//定义字符串常量 TAG指向Contact
|
|
|
|
|
|
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER//定义 CALLER_ID_SELECTION字符串,用于匹配联系人
|
|
|
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
|
|
|
+ " AND " + Data.RAW_CONTACT_ID + " IN "//定义的callerIDSELEDTION所包含的各项数据
|
|
|
+ "(SELECT raw_contact_id "
|
|
|
+ " FROM phone_lookup"
|
|
|
+ " WHERE min_match = '+')";
|
|
|
/*获取联系人参数Context context内容 phoneNumber和电话号码
|
|
|
*/
|
|
|
public static String getContact(Context context, String phoneNumber) {//获取联系人
|
|
|
if(sContactCache == null) {//如果当前还没有联系人,那么新建一个HashMap存储联系人
|
|
|
sContactCache = new HashMap<String, String>();//创建哈希表
|
|
|
}
|
|
|
|
|
|
if(sContactCache.containsKey(phoneNumber)) {//如果hashmap实例里包含电话号码,则返回电话号码
|
|
|
return sContactCache.get(phoneNumber);//如果根据电话号码phoneNumber索引到了对应的联系人,那么把对应的联系人的信息返回
|
|
|
}
|
|
|
|
|
|
String selection = CALLER_ID_SELECTION.replace("+",//将CALLER_ID_SELECTION的“+”号替代为号码的后MIN_MATCH位
|
|
|
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
|
|
|
Cursor cursor = context.getContentResolver().query(//通过selection的查询语句去数据库查询
|
|
|
Data.CONTENT_URI,//提供联系人的内容的地址
|
|
|
new String [] { Phone.DISPLAY_NAME },//返回联系人名字
|
|
|
selection,//设置条件,相当于whlie
|
|
|
new String[] { phoneNumber },//get..函数返回字符串类phoneNUmber(手机号码)
|
|
|
null);//判定查询结果
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {// 判断查询结果,如果找到,并且成功返回第一天数据
|
|
|
try {//得到cursor的第一个字符串name
|
|
|
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为红色,可以想到error错误,这里仅显示红色的错误信息
|
|
|
return null;
|
|
|
} finally {//关闭数据库的访问
|
|
|
cursor.close();//关闭数据库的访问
|
|
|
}
|
|
|
} else {//未找到相关信息,返回空指针
|
|
|
Log.d(TAG, "No contact matched with number:" + phoneNumber);//如果没有找到信息,返回没有匹配到该电话
|
|
|
return null;//返回空
|
|
|
}
|
|
|
}
|
|
|
}
|