Merge pull request '1.2和data包下第一个类的contact函数注释' (#4) from dongjiaqi_branch into develop

develop
ps249eph7 12 months ago
commit 41e40b6e0c

Binary file not shown.

@ -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;
}
}
}

@ -25,10 +25,12 @@ import android.util.Log;
import java.util.HashMap; import java.util.HashMap;
public class Contact { public class Contact {//联系人
//定义了一个静态的HashMap变量sContactCache和一个静态的字符串常量TAG。
private static HashMap<String, String> sContactCache; private static HashMap<String, String> sContactCache;
private static final String TAG = "Contact"; 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 private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
+ " AND " + Data.RAW_CONTACT_ID + " IN " + " AND " + Data.RAW_CONTACT_ID + " IN "
@ -36,6 +38,8 @@ public class Contact {
+ " FROM phone_lookup" + " FROM phone_lookup"
+ " WHERE min_match = '+')"; + " WHERE min_match = '+')";
//定义了一个getContact用来接收context对象和电话号码作为参数该方法先检查sContactCache中是否已经缓存了号码对应联系人信息是的话直接返回联系人姓名。
//否则就构造一个查询语句查询成功后将姓名缓存到sContactCache中,返回姓名失败的话就返回null
public static String getContact(Context context, String phoneNumber) { public static String getContact(Context context, String phoneNumber) {
if(sContactCache == null) { if(sContactCache == null) {
sContactCache = new HashMap<String, String>(); sContactCache = new HashMap<String, String>();
@ -44,7 +48,8 @@ public class Contact {
if(sContactCache.containsKey(phoneNumber)) { if(sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber); return sContactCache.get(phoneNumber);
} }
//将电话号码中的"+"替换为最小匹配的呼叫者ID以生成新的查询条件。
//使用查询条件在联系人数据的URI(Data.CONTENT_URI)上进行查询,查询的结果只包含联系人的显示名称(Phone.DISPLAY_NAME)。
String selection = CALLER_ID_SELECTION.replace("+", String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
Cursor cursor = context.getContentResolver().query( Cursor cursor = context.getContentResolver().query(
@ -54,6 +59,7 @@ public class Contact {
new String[] { phoneNumber }, new String[] { phoneNumber },
null); null);
//如果查询结果不为空将第一个结果的显示名称存储在缓存中并返回该名称否则记录日志并返回null。
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
try { try {
String name = cursor.getString(0); String name = cursor.getString(0);

Loading…
Cancel
Save