'Some devices like OPPO don't support reading contacts in pages (pagination) Android

I face an issues that some devices like OPPO don't support reading contacts in pages like if I make "LIMIT 100 OFFSET 0", it returns all rows in the contacts table.

val cursor = contentResolver.query(
            ContactsContract.Contacts.CONTENT_URI,
            null,
            null,
            null,
            "${ContactsContract.Contacts._ID} desc LIMIT $PAGE_LIMIT OFFSET $offset"
)

I had to make a workaround by checking if the first page size equals to the all rows in the table size so I stop reading the contacts in pages to avoid infinite loop.


private fun isReadingContactsInPagesSupported(): Boolean {
        val firstPageContactsSize = getQueryCount(" LIMIT $PAGE_LIMIT OFFSET 0")
        val allContactsSize = getQueryCount(null)
        return firstPageContactsSize != allContactsSize
}

@SuppressLint("Range")
private fun getQueryCount(sort: String?): Int {
        val contentResolver: ContentResolver = context.contentResolver
        val cursor = contentResolver.query(
            ContactsContract.Contacts.CONTENT_URI,
            null,
            null,
            null,
            sort
        )
        val count = cursor?.count ?: 0
        cursor?.close()
        return count
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source