去重并且合并联系人

来源:互联网 发布:俄罗斯社交软件vk 编辑:程序博客网 时间:2024/05/16 05:12
  /**     * 获取系统联系人的信息并排序     */    public ArrayList<ContactInfo> getContacts() {        ArrayList<ContactInfo> listsContacts = new ArrayList<>();        ContactInfo member = null;        ArrayList<String> contactPhones = new ArrayList<>();        Cursor cursor = null;        Cursor noteCur = null;        ArrayList<ContactInfo> tempContacts = new ArrayList<>();;        try {            long oldContactId = -1;//            Uri uri = ContactsContractOutUri.CommonDataKindPhoneContentUri;            Uri uri = Uri.parse("content://com.android.contacts");            Uri dataUri = Uri.withAppendedPath(uri, "data");            Uri commonPhoneUri = Uri.withAppendedPath(dataUri, "phones");            cursor = context.getContentResolver().query(commonPhoneUri, contact_projection, null, null,                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID);            if (cursor != null && cursor.getCount() > 0) {                while (cursor.moveToNext()) {                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));                    String oldSortKey = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY));                    String newSortKey = oldSortKey;                    if (isChinese(oldSortKey.charAt(0))) {                        newSortKey = CharacterParser.getInstance().getSelling(oldSortKey);                    }                    String sortKey = getSortKey(newSortKey);                    long contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                    if (contactId == oldContactId) {                        contactPhones.add(number);                        member.setContactPhones(contactPhones);                        continue;                    } else {                        member = new ContactInfo();                        contactPhones = new ArrayList<String>();                        contactPhones.add(number);                    }                    int photoId = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID));                    /** 头像*/                    if (photoId != 0) {                        Uri photoUri = Uri.parse("content://com.android.contacts/contacts/" + contactId);                        InputStream input = ContactsContract.Contacts                                .openContactPhotoInputStream(context.getContentResolver(), photoUri);                        byte[] photoByte = null;                        if (input != null) {                            photoByte = toByteArray(input);                            member.setPhoto(photoByte);                        }                    }                    String note = "";                    String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";                    String[] noteWhereParams = new String[]{"" + contactId, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};                    Uri noteBaseUri = Uri.parse("content://com.android.contacts");                    Uri noteUri = Uri.withAppendedPath(noteBaseUri, "data");                    noteCur = context.getContentResolver().query(noteUri, null, noteWhere, noteWhereParams, null);                    if (noteCur != null && noteCur.moveToFirst()) {                        note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));                    }                    member.setRemark(note);                    member.setSortKey(sortKey);                    member.setContactPhones(contactPhones);                    member.setName(name);                    member.setContactId(contactId);                    boolean needAdd = true;                    for(ContactInfo tempContactInfo:tempContacts){                        String infoName = tempContactInfo.getName();                        String memberName = member.getName();                        if(!infoName.equalsIgnoreCase(memberName)){                            needAdd = true;                            break;                        }                        List<String> infoPhones = tempContactInfo.getPhones();                        List<String> memberPhones = member.getPhones();                        if(infoPhones.size() != memberPhones.size() || !infoPhones.containsAll(memberPhones)){                            needAdd = true;                            break;                        }                        needAdd = false;                        byte[] infoIcon = tempContactInfo.getPhoto();                        byte[] memberIcon = member.getPhoto();                        if(infoIcon == null || infoIcon.length == 0 && memberIcon != null && memberIcon.length > 0){                            tempContactInfo.setPhoto(memberIcon);                        }                        String infoRemark = tempContactInfo.getRemark();                        String memberReamrk = member.getRemark();                        if(TextUtils.isEmpty(infoRemark) && !TextUtils.isEmpty(memberReamrk)){                            tempContactInfo.setRemark(memberReamrk);                        }                    }                    if(needAdd){                        tempContacts.add(member);                    }                    oldContactId = contactId;                }            }            for (ContactInfo c: tempContacts){                if (!ApplicationSQLManger.getInstance().insertContactIdIsExistInLauncher(c)) {                    listsContacts.add(c);                }            }            if (listsContacts != null) {                Collections.sort(listsContacts, new Pycomparator());            }        } catch (Exception e) {            LogUtils.i("ContactsManager", "Exception");            e.printStackTrace();        } finally {            if (cursor != null) {                cursor.close();            }            if (noteCur != null) {                noteCur.close();            }        }        return listsContacts;    }
0 0
原创粉丝点击