simplifying cursor querying
This commit is contained in:
parent
3b83e93192
commit
10bc1d1278
|
@ -36,7 +36,7 @@ android {
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.simplemobiletools:commons:5.24.18'
|
implementation 'com.simplemobiletools:commons:5.24.19'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
|
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
|
||||||
implementation 'org.greenrobot:eventbus:3.2.0'
|
implementation 'org.greenrobot:eventbus:3.2.0'
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package com.simplemobiletools.smsmessenger.extensions
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.database.Cursor
|
|
||||||
import android.provider.ContactsContract
|
import android.provider.ContactsContract
|
||||||
import android.provider.ContactsContract.CommonDataKinds
|
import android.provider.ContactsContract.CommonDataKinds
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
|
@ -47,11 +46,7 @@ fun Context.getMessages(threadID: Int? = null): ArrayList<Message> {
|
||||||
arrayOf(threadID.toString())
|
arrayOf(threadID.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
var cursor: Cursor? = null
|
queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
|
||||||
try {
|
|
||||||
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
|
||||||
if (cursor?.moveToFirst() == true) {
|
|
||||||
do {
|
|
||||||
val id = cursor.getIntValue(Telephony.Sms._ID)
|
val id = cursor.getIntValue(Telephony.Sms._ID)
|
||||||
val subject = cursor.getStringValue(Telephony.Sms.SUBJECT) ?: ""
|
val subject = cursor.getStringValue(Telephony.Sms.SUBJECT) ?: ""
|
||||||
val body = cursor.getStringValue(Telephony.Sms.BODY)
|
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)
|
val message = Message(id, subject, body, type, senderName, senderNumber, date, read, thread)
|
||||||
messages.add(message)
|
messages.add(message)
|
||||||
} while (cursor.moveToNext())
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
showErrorToast(e)
|
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
return messages
|
return messages
|
||||||
}
|
}
|
||||||
|
@ -95,10 +84,10 @@ fun Context.getThreadInfo(id: Int): MessagingThread? {
|
||||||
)
|
)
|
||||||
val selection = "${Telephony.Sms.THREAD_ID} = ?"
|
val selection = "${Telephony.Sms.THREAD_ID} = ?"
|
||||||
val selectionArgs = arrayOf(id.toString())
|
val selectionArgs = arrayOf(id.toString())
|
||||||
var cursor: Cursor? = null
|
|
||||||
try {
|
try {
|
||||||
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||||
if (cursor?.moveToFirst() == true) {
|
cursor?.use {
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
val person = cursor.getIntValue(Telephony.Sms.PERSON)
|
val person = cursor.getIntValue(Telephony.Sms.PERSON)
|
||||||
val address = cursor.getStringValue(Telephony.Sms.ADDRESS)
|
val address = cursor.getStringValue(Telephony.Sms.ADDRESS)
|
||||||
var title = address
|
var title = address
|
||||||
|
@ -114,9 +103,8 @@ fun Context.getThreadInfo(id: Int): MessagingThread? {
|
||||||
|
|
||||||
return MessagingThread(id, title, address)
|
return MessagingThread(id, title, address)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
@ -142,10 +130,10 @@ fun Context.getPersonsName(id: Int): String? {
|
||||||
id.toString()
|
id.toString()
|
||||||
)
|
)
|
||||||
|
|
||||||
var cursor: Cursor? = null
|
|
||||||
try {
|
try {
|
||||||
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||||
if (cursor?.moveToFirst() == true) {
|
cursor?.use {
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
do {
|
do {
|
||||||
val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE)
|
val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE)
|
||||||
val isPerson = mimetype == CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE
|
val isPerson = mimetype == CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE
|
||||||
|
@ -171,10 +159,9 @@ fun Context.getPersonsName(id: Int): String? {
|
||||||
}
|
}
|
||||||
} while (cursor.moveToNext())
|
} while (cursor.moveToNext())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
showErrorToast(e)
|
showErrorToast(e)
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
|
@ -218,19 +205,16 @@ fun Context.getNameFromPhoneNumber(number: String): Int? {
|
||||||
|
|
||||||
val selection = "${CommonDataKinds.Phone.NUMBER} = ? OR ${CommonDataKinds.Phone.NORMALIZED_NUMBER} = ?"
|
val selection = "${CommonDataKinds.Phone.NUMBER} = ? OR ${CommonDataKinds.Phone.NORMALIZED_NUMBER} = ?"
|
||||||
val selectionArgs = arrayOf(number, number)
|
val selectionArgs = arrayOf(number, number)
|
||||||
|
|
||||||
var cursor: Cursor? = null
|
|
||||||
try {
|
try {
|
||||||
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||||
|
cursor.use {
|
||||||
if (cursor?.moveToFirst() == true) {
|
if (cursor?.moveToFirst() == true) {
|
||||||
return cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
return cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
showErrorToast(e)
|
showErrorToast(e)
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,11 +240,7 @@ fun Context.getContactNames(): List<Contact> {
|
||||||
CommonDataKinds.Organization.CONTENT_ITEM_TYPE
|
CommonDataKinds.Organization.CONTENT_ITEM_TYPE
|
||||||
)
|
)
|
||||||
|
|
||||||
var cursor: Cursor? = null
|
queryCursor(uri, projection, selection, selectionArgs) { cursor ->
|
||||||
try {
|
|
||||||
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
|
||||||
if (cursor?.moveToFirst() == true) {
|
|
||||||
do {
|
|
||||||
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
||||||
val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE)
|
val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE)
|
||||||
val photoUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
|
val photoUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
|
||||||
|
@ -289,11 +269,6 @@ fun Context.getContactNames(): List<Contact> {
|
||||||
contacts.add(contact)
|
contacts.add(contact)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (cursor.moveToNext())
|
|
||||||
}
|
|
||||||
} catch (ignored: Exception) {
|
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
return contacts
|
return contacts
|
||||||
}
|
}
|
||||||
|
@ -306,20 +281,13 @@ fun Context.getContactPhoneNumbers(): ArrayList<Contact> {
|
||||||
CommonDataKinds.Phone.NORMALIZED_NUMBER
|
CommonDataKinds.Phone.NORMALIZED_NUMBER
|
||||||
)
|
)
|
||||||
|
|
||||||
var cursor: Cursor? = null
|
queryCursor(uri, projection) { cursor ->
|
||||||
try {
|
|
||||||
cursor = contentResolver.query(uri, projection, null, null, null)
|
|
||||||
if (cursor?.moveToFirst() == true) {
|
|
||||||
do {
|
|
||||||
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
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)
|
val contact = Contact(id, "", "", phoneNumber, false)
|
||||||
contacts.add(contact)
|
contacts.add(contact)
|
||||||
} while (cursor.moveToNext())
|
|
||||||
}
|
}
|
||||||
} catch (ignored: Exception) {
|
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
return contacts
|
return contacts
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue