mirror of
https://github.com/SimpleMobileTools/Simple-SMS-Messenger.git
synced 2025-06-05 21:49:22 +02:00
adding some improvements related to fetching recipient names
This commit is contained in:
@@ -10,9 +10,9 @@ import android.provider.ContactsContract
|
|||||||
import android.provider.ContactsContract.CommonDataKinds
|
import android.provider.ContactsContract.CommonDataKinds
|
||||||
import android.provider.ContactsContract.CommonDataKinds.Organization
|
import android.provider.ContactsContract.CommonDataKinds.Organization
|
||||||
import android.provider.ContactsContract.CommonDataKinds.StructuredName
|
import android.provider.ContactsContract.CommonDataKinds.StructuredName
|
||||||
|
import android.provider.ContactsContract.PhoneLookup
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
import android.provider.Telephony.Mms
|
import android.provider.Telephony.*
|
||||||
import android.provider.Telephony.Sms
|
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
import com.simplemobiletools.commons.extensions.*
|
import com.simplemobiletools.commons.extensions.*
|
||||||
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
|
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
|
||||||
@@ -38,7 +38,6 @@ fun Context.getMessages(threadId: Int? = null): ArrayList<Message> {
|
|||||||
Sms.BODY,
|
Sms.BODY,
|
||||||
Sms.TYPE,
|
Sms.TYPE,
|
||||||
Sms.ADDRESS,
|
Sms.ADDRESS,
|
||||||
Sms.PERSON,
|
|
||||||
Sms.DATE,
|
Sms.DATE,
|
||||||
Sms.READ,
|
Sms.READ,
|
||||||
Sms.THREAD_ID
|
Sms.THREAD_ID
|
||||||
@@ -62,24 +61,11 @@ fun Context.getMessages(threadId: Int? = null): ArrayList<Message> {
|
|||||||
val subject = cursor.getStringValue(Sms.SUBJECT) ?: ""
|
val subject = cursor.getStringValue(Sms.SUBJECT) ?: ""
|
||||||
val body = cursor.getStringValue(Sms.BODY)
|
val body = cursor.getStringValue(Sms.BODY)
|
||||||
val type = cursor.getIntValue(Sms.TYPE)
|
val type = cursor.getIntValue(Sms.TYPE)
|
||||||
var senderName = cursor.getStringValue(Sms.ADDRESS)
|
val senderNumber = cursor.getStringValue(Sms.ADDRESS)
|
||||||
val senderNumber = senderName
|
val senderName = getNameFromPhoneNumber(senderNumber)
|
||||||
val date = (cursor.getLongValue(Sms.DATE) / 1000).toInt()
|
val date = (cursor.getLongValue(Sms.DATE) / 1000).toInt()
|
||||||
val read = cursor.getIntValue(Sms.READ) == 1
|
val read = cursor.getIntValue(Sms.READ) == 1
|
||||||
val person = cursor.getIntValue(Sms.PERSON)
|
|
||||||
val thread = cursor.getIntValue(Sms.THREAD_ID)
|
val thread = cursor.getIntValue(Sms.THREAD_ID)
|
||||||
|
|
||||||
if (hasContactsPermission) {
|
|
||||||
if (senderName != null && person != 0) {
|
|
||||||
senderName = getPersonsName(person) ?: senderName
|
|
||||||
} else if (senderName.areDigitsOnly()) {
|
|
||||||
val contactId = getNameFromPhoneNumber(senderName)
|
|
||||||
if (contactId != null) {
|
|
||||||
senderName = getPersonsName(contactId) ?: senderName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
@@ -95,7 +81,6 @@ fun Context.getMessages(threadId: Int? = null): ArrayList<Message> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun Context.getMMS(threadId: Int? = null): ArrayList<Message> {
|
fun Context.getMMS(threadId: Int? = null): ArrayList<Message> {
|
||||||
val hasContactsPermission = hasPermission(PERMISSION_READ_CONTACTS)
|
|
||||||
val uri = Mms.CONTENT_URI
|
val uri = Mms.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
Mms._ID,
|
Mms._ID,
|
||||||
@@ -126,17 +111,9 @@ fun Context.getMMS(threadId: Int? = null): ArrayList<Message> {
|
|||||||
val date = cursor.getLongValue(Mms.DATE).toInt()
|
val date = cursor.getLongValue(Mms.DATE).toInt()
|
||||||
val read = cursor.getIntValue(Mms.READ) == 1
|
val read = cursor.getIntValue(Mms.READ) == 1
|
||||||
val thread = cursor.getIntValue(Mms.THREAD_ID)
|
val thread = cursor.getIntValue(Mms.THREAD_ID)
|
||||||
var senderName = getMmsAddress(id)
|
val senderName = getThreadRecipients(thread)
|
||||||
val senderNumber = senderName
|
val senderNumber = senderName
|
||||||
val mms = getMmsContent(id)
|
val mms = getMmsContent(id)
|
||||||
|
|
||||||
if (hasContactsPermission) {
|
|
||||||
val contactId = getNameFromPhoneNumber(senderName)
|
|
||||||
if (contactId != null) {
|
|
||||||
senderName = getPersonsName(contactId) ?: senderName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val message = Message(id, subject, mms?.text ?: "", type, senderName, senderNumber, date, read, thread)
|
val message = Message(id, subject, mms?.text ?: "", type, senderName, senderNumber, date, read, thread)
|
||||||
messages.add(message)
|
messages.add(message)
|
||||||
}
|
}
|
||||||
@@ -174,19 +151,44 @@ fun Context.getMmsContent(id: Int): MMS? {
|
|||||||
return mms
|
return mms
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.getMmsAddress(id: Int): String {
|
fun Context.getThreadRecipients(threadId: Int): String {
|
||||||
val addressUri = Uri.withAppendedPath(Mms.CONTENT_URI, "$id/addr")
|
val uri = Uri.parse("${MmsSms.CONTENT_CONVERSATIONS_URI}?simple=true")
|
||||||
|
val projection = arrayOf(
|
||||||
|
ThreadsColumns.RECIPIENT_IDS
|
||||||
|
)
|
||||||
|
val selection = "${Mms._ID} = ?"
|
||||||
|
val selectionArgs = arrayOf(threadId.toString())
|
||||||
|
try {
|
||||||
|
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||||
|
cursor?.use {
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
|
val address = cursor.getStringValue(ThreadsColumns.RECIPIENT_IDS)
|
||||||
|
var recipient = ""
|
||||||
|
address.split(" ").filter { it.areDigitsOnly() }.forEach {
|
||||||
|
recipient += "${getNameFromAddressId(it.toInt())}, "
|
||||||
|
}
|
||||||
|
return recipient.removeSuffix(", ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
showErrorToast(e)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Context.getNameFromAddressId(canonicalAddressId: Int): String {
|
||||||
|
val uri = Uri.withAppendedPath(MmsSms.CONTENT_URI, "canonical-addresses")
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
Mms.Addr.ADDRESS
|
Mms.Addr.ADDRESS
|
||||||
)
|
)
|
||||||
val selection = "${Mms.Addr.MSG_ID} = ?"
|
val selection = "${Mms._ID} = ?"
|
||||||
val selectionArgs = arrayOf(id.toString())
|
val selectionArgs = arrayOf(canonicalAddressId.toString())
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val cursor = contentResolver.query(addressUri, projection, selection, selectionArgs, null)
|
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||||
cursor?.use {
|
cursor?.use {
|
||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
return cursor.getStringValue(Mms.Addr.ADDRESS)
|
val phoneNumber = cursor.getStringValue(Mms.Addr.ADDRESS)
|
||||||
|
return getNameFromPhoneNumber(phoneNumber)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -227,7 +229,7 @@ fun Context.getThreadInfo(id: Int): MessagingThread? {
|
|||||||
if (title != null && person != 0) {
|
if (title != null && person != 0) {
|
||||||
title = getPersonsName(person) ?: title
|
title = getPersonsName(person) ?: title
|
||||||
} else if (title.areDigitsOnly()) {
|
} else if (title.areDigitsOnly()) {
|
||||||
val contactId = getNameFromPhoneNumber(title)
|
val contactId = getContactIdFromPhoneNumber(title)
|
||||||
if (contactId != null) {
|
if (contactId != null) {
|
||||||
title = getPersonsName(contactId) ?: title
|
title = getPersonsName(contactId) ?: title
|
||||||
}
|
}
|
||||||
@@ -329,7 +331,7 @@ fun Context.getAvailableContacts(callback: (ArrayList<Contact>) -> Unit) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.getNameFromPhoneNumber(number: String): Int? {
|
fun Context.getContactIdFromPhoneNumber(number: String): Int? {
|
||||||
val uri = CommonDataKinds.Phone.CONTENT_URI
|
val uri = CommonDataKinds.Phone.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
ContactsContract.Data.CONTACT_ID
|
ContactsContract.Data.CONTACT_ID
|
||||||
@@ -350,6 +352,29 @@ fun Context.getNameFromPhoneNumber(number: String): Int? {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun Context.getNameFromPhoneNumber(number: String): String {
|
||||||
|
if (!hasPermission(PERMISSION_READ_CONTACTS)) {
|
||||||
|
return number
|
||||||
|
}
|
||||||
|
|
||||||
|
val uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number))
|
||||||
|
val projection = arrayOf(
|
||||||
|
PhoneLookup.DISPLAY_NAME
|
||||||
|
)
|
||||||
|
|
||||||
|
try {
|
||||||
|
val cursor = contentResolver.query(uri, projection, null, null, null)
|
||||||
|
cursor.use {
|
||||||
|
if (cursor?.moveToFirst() == true) {
|
||||||
|
return cursor.getStringValue(PhoneLookup.DISPLAY_NAME)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
showErrorToast(e)
|
||||||
|
}
|
||||||
|
return number
|
||||||
|
}
|
||||||
|
|
||||||
fun Context.getContactNames(): List<Contact> {
|
fun Context.getContactNames(): List<Contact> {
|
||||||
val contacts = ArrayList<Contact>()
|
val contacts = ArrayList<Contact>()
|
||||||
val uri = ContactsContract.Data.CONTENT_URI
|
val uri = ContactsContract.Data.CONTENT_URI
|
||||||
|
Reference in New Issue
Block a user