|
|
|
@ -0,0 +1,79 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
public class Contact {//联系人
|
|
|
|
|
//定义了一个静态的HashMap变量sContactCache和一个静态的字符串常量TAG。
|
|
|
|
|
private static HashMap<String, String> sContactCache;
|
|
|
|
|
private static final String TAG = "Contact";
|
|
|
|
|
//定义字符串常量CALLER_ID_SELECTION包含了整个查询的语句,其中使用了安卓提供的一些函数和常量,比如PHONE_NUMBERS_EQUAL函数和Phone.NUMBER常量。
|
|
|
|
|
//该查询语句作用是从小米设备的联系人数据库中设法获取和指定电话号码匹配的联系人的信息。
|
|
|
|
|
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 = '+')";
|
|
|
|
|
|
|
|
|
|
//定义了一个getContact用来接收context对象和电话号码作为参数,该方法先检查sContactCache中是否已经缓存了号码对应联系人信息,是的话直接返回联系人姓名。
|
|
|
|
|
//否则就构造一个查询语句,查询成功后将姓名缓存到sContactCache中,返回姓名;失败的话就返回null
|
|
|
|
|
public static String getContact(Context context, String phoneNumber) {
|
|
|
|
|
if(sContactCache == null) {
|
|
|
|
|
sContactCache = new HashMap<String, String>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(sContactCache.containsKey(phoneNumber)) {
|
|
|
|
|
return sContactCache.get(phoneNumber);
|
|
|
|
|
}
|
|
|
|
|
//将电话号码中的"+"替换为最小匹配的呼叫者ID,以生成新的查询条件。
|
|
|
|
|
//使用查询条件在联系人数据的URI(Data.CONTENT_URI)上进行查询,查询的结果只包含联系人的显示名称(Phone.DISPLAY_NAME)。
|
|
|
|
|
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) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|