simplifying cursor querying

This commit is contained in:
tibbi
2020-04-07 12:39:34 +02:00
parent 3b83e93192
commit 10bc1d1278
2 changed files with 102 additions and 134 deletions

View File

@ -36,7 +36,7 @@ android {
}
dependencies {
implementation 'com.simplemobiletools:commons:5.24.18'
implementation 'com.simplemobiletools:commons:5.24.19'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
implementation 'org.greenrobot:eventbus:3.2.0'
}

View File

@ -3,7 +3,6 @@ package com.simplemobiletools.smsmessenger.extensions
import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.provider.ContactsContract
import android.provider.ContactsContract.CommonDataKinds
import android.provider.Telephony
@ -47,11 +46,7 @@ fun Context.getMessages(threadID: Int? = null): ArrayList<Message> {
arrayOf(threadID.toString())
}
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Telephony.Sms._ID)
val subject = cursor.getStringValue(Telephony.Sms.SUBJECT) ?: ""
val body = cursor.getStringValue(Telephony.Sms.BODY)
@ -76,12 +71,6 @@ fun Context.getMessages(threadID: Int? = null): ArrayList<Message> {
val message = Message(id, subject, body, type, senderName, senderNumber, date, read, thread)
messages.add(message)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
showErrorToast(e)
} finally {
cursor?.close()
}
return messages
}
@ -95,10 +84,10 @@ fun Context.getThreadInfo(id: Int): MessagingThread? {
)
val selection = "${Telephony.Sms.THREAD_ID} = ?"
val selectionArgs = arrayOf(id.toString())
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
val person = cursor.getIntValue(Telephony.Sms.PERSON)
val address = cursor.getStringValue(Telephony.Sms.ADDRESS)
var title = address
@ -114,9 +103,8 @@ fun Context.getThreadInfo(id: Int): MessagingThread? {
return MessagingThread(id, title, address)
}
}
} catch (e: Exception) {
} finally {
cursor?.close()
}
return null
}
@ -142,10 +130,10 @@ fun Context.getPersonsName(id: Int): String? {
id.toString()
)
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
do {
val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE)
val isPerson = mimetype == CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE
@ -171,10 +159,9 @@ fun Context.getPersonsName(id: Int): String? {
}
} while (cursor.moveToNext())
}
}
} catch (e: Exception) {
showErrorToast(e)
} finally {
cursor?.close()
}
return null
@ -218,19 +205,16 @@ fun Context.getNameFromPhoneNumber(number: String): Int? {
val selection = "${CommonDataKinds.Phone.NUMBER} = ? OR ${CommonDataKinds.Phone.NORMALIZED_NUMBER} = ?"
val selectionArgs = arrayOf(number, number)
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor.use {
if (cursor?.moveToFirst() == true) {
return cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
}
}
} catch (e: Exception) {
showErrorToast(e)
} finally {
cursor?.close()
}
return null
}
@ -256,11 +240,7 @@ fun Context.getContactNames(): List<Contact> {
CommonDataKinds.Organization.CONTENT_ITEM_TYPE
)
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
queryCursor(uri, projection, selection, selectionArgs) { cursor ->
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE)
val photoUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
@ -289,11 +269,6 @@ fun Context.getContactNames(): List<Contact> {
contacts.add(contact)
}
}
} while (cursor.moveToNext())
}
} catch (ignored: Exception) {
} finally {
cursor?.close()
}
return contacts
}
@ -306,20 +281,13 @@ fun Context.getContactPhoneNumbers(): ArrayList<Contact> {
CommonDataKinds.Phone.NORMALIZED_NUMBER
)
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, null, null, null)
if (cursor?.moveToFirst() == true) {
do {
queryCursor(uri, projection) { cursor ->
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val phoneNumber = cursor.getStringValue(CommonDataKinds.Phone.NORMALIZED_NUMBER) ?: continue
val phoneNumber = cursor.getStringValue(CommonDataKinds.Phone.NORMALIZED_NUMBER)
if (phoneNumber != null) {
val contact = Contact(id, "", "", phoneNumber, false)
contacts.add(contact)
} while (cursor.moveToNext())
}
} catch (ignored: Exception) {
} finally {
cursor?.close()
}
return contacts
}