package com.sogouchat.smsmms; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Vector; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds; import android.util.Log; public class ContactsMgr { private static ContactsMgr s_lpthis = new ContactsMgr(); private static final String TAG = "ContactsMgr"; public static ContactsMgr GetI(){ return s_lpthis; } private Context context; private Boolean bInit = false; private Vector<ContactsInfo> contactsInfos =null; private HashMap<Integer, ContactsInfo> maps = null; private HashMap<String, ContactsInfo> Numbermaps = null; public Vector<ContactsInfo> contactsInfosShow = null; public Vector<ContactsInfo> contactsInfosShowList = null; public int[] nLocPosList = new int[27]; public String[] nLocPosStrList = new String[27]; private ContactsMgr() { // TODO Auto-generated constructor stub Log.d(TAG, "()"); } public void Init(Context context){ this.context = context; bInit = true; } public void LoadContacts(){ Cursor cursor = null; ContentResolver cr =context.getContentResolver(); //String strSelection =ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+strContactID; cursor =cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, ContactsContract.Contacts._ID); int nIndex=0; while (cursor.moveToNext()) { nIndex++; String strName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); int nID = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID)); //String strLookupString = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); //String strNum=PhoneBookHelper.GetNumByID(context,String.valueOf(nID)); String strNum =""; ContactsInfo info = new ContactsInfo(strName,strNum,nID); contactsInfos.add(info); maps.put(nID, info); Log.d("LoadContacts",""+nIndex +" "+nID+ " "+strName + " "+strNum); } } public void LoadContactsEx(){ long tm = System.currentTimeMillis(); ContentResolver cr =context.getContentResolver(); Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] { CommonDataKinds.Phone.NUMBER, CommonDataKinds.Phone.DISPLAY_NAME ,CommonDataKinds.Phone.CONTACT_ID}, null, null, ContactsContract.Contacts._ID); int nIndex=0; while (cursor.moveToNext()) { nIndex++; String strName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); int nID = cursor.getInt(cursor.getColumnIndex(CommonDataKinds.Phone.CONTACT_ID)); //int nID = 10; //String strLookupString = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); String strNum=cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER)); ContactsInfo info = new ContactsInfo(strName,strNum,nID); contactsInfos.add(info); maps.put(nID, info); Numbermaps.put(strNum, info); Log.d("LoadContactsEx",""+nIndex +" "+nID+ " "+strName + " "+strNum); } long tm2 = System.currentTimeMillis(); Log.d("LoadContactsEx","total:"+(tm2-tm)); } public void Load(){ if (!bInit) { return; } if (contactsInfos==null) { contactsInfos = new Vector<ContactsInfo>(); } if (maps==null) { maps = new HashMap<Integer, ContactsInfo>(); } if (Numbermaps ==null) { Numbermaps = new HashMap<String, ContactsInfo>(); } if (!maps.isEmpty()) { maps.clear(); } if (!contactsInfos.isEmpty()) { contactsInfos.clear(); } LoadContactsEx(); } public ContactsInfo GetContactByNumber(String strNumber){ if (strNumber==null) return null; ContactsInfo info = null; info = Numbermaps.get(strNumber); if (info ==null) { if (strNumber.length()>3) { if (strNumber.substring(0,3).equals("+86")) { String strFix = strNumber.substring(3); info = Numbermaps.get(strFix); }; } } return info; } public ContactsInfo GetContactByID(int nID){ return maps.get(nID); } private void AddList(){ ContactsInfo info = new ContactsInfo(null, null, 0); contactsInfosShow.add(0,info); } public void LoadShow(){ if (contactsInfosShow ==null) { contactsInfosShow = new Vector<ContactsInfo>(); } if (!contactsInfosShow.isEmpty()) { contactsInfosShow.clear(); } for (int i = 0; i < contactsInfos.size(); i++) { ContactsInfo info = contactsInfos.get(i); boolean bExist = false; for (int j = 0; j < contactsInfosShow.size(); j++) { ContactsInfo info2 = contactsInfosShow.get(j); if (info.phoneNumber.equals(info2.phoneNumber) && info.displayName.equals(info2.displayName)) { bExist = true; break; } } if (!bExist) { info.ParsePy(); //info.strPY = HanZiToPinYin.StrToPinYin(info.displayName); contactsInfosShow.add(info); } } sortByBy(); //AddList(); CreateShowList(); } public void sortByBy(){ ContactCompa compa = new ContactCompa(); Collections.sort(contactsInfosShow, compa); } private int nFirstPos = 0; public void CreateShowList(){ if (contactsInfosShowList ==null) { contactsInfosShowList = new Vector<ContactsInfo>(); } if (!contactsInfosShowList.isEmpty()) { contactsInfosShowList.clear(); } char chCur = 0; char chOrc = 0; int nIndexCur = 0; int nIndexOrc = 0; for (int i = nFirstPos; i < contactsInfosShow.size(); i++) { ContactsInfo info = contactsInfosShow.get(i); if (info.firstCh<'a' || info.firstCh>'z') { chCur = '#'; } else { chCur = info.firstCh; } if (i==nFirstPos ||chCur!=chOrc) { chOrc = chCur; int nIndex = chOrc-'a'; ContactsInfo tempInfo = new ContactsInfo(null, null, 0); tempInfo.firstCh = chCur; tempInfo.bFirst = true; for (int j = nIndexOrc; j < nIndex+1 && j<27; j++) { nLocPosList[j]= i; nLocPosStrList[j] = (""+chOrc).toUpperCase(); } if (chCur == '#') { nLocPosList[26] = i; } nIndexOrc = nIndex+1; contactsInfosShow.add(i,tempInfo); i++; } } } class ContactCompa implements Comparator<ContactsInfo>{ public int compare(ContactsInfo lhs, ContactsInfo rhs) { // TODO Auto-generated method stub if ( (lhs.firstCh<'a' ||lhs.firstCh>'z')&& (rhs.firstCh<='z' &&rhs.firstCh>='a')) { return 1; } if ( (rhs.firstCh<'a' ||rhs.firstCh>'z')&& (lhs.firstCh<='z' &&lhs.firstCh>='a')) { return -1; } if (lhs.firstCh>rhs.firstCh) { return 1; }else if (lhs.firstCh==rhs.firstCh) { return 0; } else { return -1; } } } }