/*
 * 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;//引入context类，用于提供全局信息环境
import android.database.Cursor;//引入cursor类，表示数据库的结果集
import android.provider.ContactsContract.CommonDataKinds.Phone;//引入其中的Phone子类，包含了关于联系人电话号码的常量定义
import android.provider.ContactsContract.Data;//引入ContactsContract类中的Data类，用于访问设备上的联系人数据
import android.telephony.PhoneNumberUtils;//引入PhoneNumberUtils类，提供电话号码处理的实用方法
import android.util.Log;//引入Log类，它提供在代码中打印日志信息的方法

import java.util.HashMap;


//用于实现根据给定的手机号码获取对应的联系人姓名
public class Contact {
    private static HashMap<String, String> sContactCache;//用于缓存已查询的联系人信息，键是手机号码，值是对应的联系人姓名
    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 = '+')";

    public static String getContact(Context context, String phoneNumber) {  //用于根据给定的电话号码查询联系人
        if(sContactCache == null) {   // 如果sContactCache为空，则创建一个新的HashMap对象
            sContactCache = new HashMap<String, String>();
        }
// 如果sContactCache中已经包含了phoneNumber对应的联系人姓名，则直接返回缓存中的姓名
        if(sContactCache.containsKey(phoneNumber)) {
            return sContactCache.get(phoneNumber);
        }

        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);
// 如果查询结果不为空同时在移动到第一条记录的情况下，将从Cursor中获取联系人姓名
        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 {
            // 如果查询结果为空，打印日志并返回null表示没有找到匹配的联系人
            Log.d(TAG, "No contact matched with number:" + phoneNumber);
            return null;
        }
    }
}
