Read contacts from phone is very slow

428 views Asked by At

I am trying to get all contact from my phone, including get all numbers from contacts with multiple numbers.

So i've build query that while not over run all over contacts, and build Contact user, and have inside query with id selection to get all numbers for each user. but since my inside query is including selection it takes a long time. any other idea?

private Cursor initPhoneCursor() {
    try {

        // get the contacts URI
        final Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        // get the name column's name depending on the Android Version
        final String nameColumn = Contact.COLUMN_NAME_PHONE;


        // declare columns object - init later depending on version
        String selection = getQuerySelectionForCursor();

        String[] columns = getColumnSelectionForCursor(nameColumn);

        if (mApp != null) {
            // return cursor from contentresolver
            return mApp.getContentResolver().query(phoneUri, columns, selection, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        }
    } catch (Exception e) {
        // couldn't read phone cursor

        CaughtExceptionHandler.reportException(e);
    }
    return null;
}

private void importContactsFromCursor(Cursor cursor, boolean isSimCard) {


    mCurrentContactCursor = initPhoneCursor();

    // check cursor is alive
    if (cursor != null && !cursor.isClosed()) {

        while (cursor.moveToNext() && shouldContinueImport()) {

//                // as log as we have contacts, move through them
            importContact(cursor, isSimCard);

            mCurrentContact++;

        }
        // when done - close the cursor
        cursor.close();
    }
}

private void importContact(Cursor cursor, boolean simCard) {

    // create Contact object
    Contact row = new Contact(cursor, simCard);

//        mContactsTimer.onContactCreated();


    if (simCard) {
        // if simCard, contact must have number

        // validate number and create contact
        row = validateAndCheckNumber(row, cursor);

    }
    else {
        // if not sim card (phone cursor), a contact might have no numbers,
        // single or multiple phone numberss

        // let's check if this contact has any numbers
        if (hasPhoneNumbers(cursor)) {

            // get all of the contact's phone numbers
            row = importAllNumbersForContact(row);

        }

    }

    // check if this is valid
    final boolean isValidForSaving = row != null && row.hasName() && row.hasNumbers();

    if (isValidForSaving && !sStopRequested) {

        mContactsToSave.add(row);
    }

}

private Contact importAllNumbersForContact(Contact contact) {

    // uri of contact phones
    Uri contentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

    // contact_id = ?
    String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?";
    String[] selectionArgs = {String.valueOf(contact.getOriginalId())};

    // do the query
    Cursor phoneCursor = mApp.getContentResolver().query(contentUri, null, selection, selectionArgs, null);

    if (phoneCursor != null) {

        // save numbers if we got anything
        contact = loopThroughContactNumbers(contact, phoneCursor);


        // close cursor when done
        phoneCursor.close();

    }
    return contact;
}
1

There are 1 answers

3
Muhammad Saad Rafique On

Go with the following solution:

Map<String,Contact> contactsMap = new TreeMap<>();
    contacts = new ArrayList<>();

    Cursor phones = getBaseContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
    assert phones != null;
    while (phones.moveToNext())
    {
        Contact contact = new Contact();
        contact.setDisplayName(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
        contact.setPhoneNumber(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        contact.setDisplayPicture(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI)));
        contactsMap.put(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)),contact);
    }

    contacts.addAll(contactsMap.values());
    phones.close();

Modify it for all numbers of a contact. You are good to go with.