mirror of
https://github.com/SimpleMobileTools/Simple-SMS-Messenger.git
synced 2025-02-17 20:20:53 +01:00
properly fetch contact photos too
This commit is contained in:
parent
e92576f876
commit
bbc1f53a74
@ -33,6 +33,7 @@ import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
|
|||||||
import com.simplemobiletools.smsmessenger.models.Contact
|
import com.simplemobiletools.smsmessenger.models.Contact
|
||||||
import com.simplemobiletools.smsmessenger.models.Message
|
import com.simplemobiletools.smsmessenger.models.Message
|
||||||
import com.simplemobiletools.smsmessenger.models.MessageAttachment
|
import com.simplemobiletools.smsmessenger.models.MessageAttachment
|
||||||
|
import com.simplemobiletools.smsmessenger.models.NamePhoto
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.collections.ArrayList
|
import kotlin.collections.ArrayList
|
||||||
|
|
||||||
@ -68,7 +69,7 @@ fun Context.getMessages(threadId: Int? = null): ArrayList<Message> {
|
|||||||
val body = cursor.getStringValue(Sms.BODY)
|
val body = cursor.getStringValue(Sms.BODY)
|
||||||
val type = cursor.getIntValue(Sms.TYPE)
|
val type = cursor.getIntValue(Sms.TYPE)
|
||||||
val senderNumber = cursor.getStringValue(Sms.ADDRESS)
|
val senderNumber = cursor.getStringValue(Sms.ADDRESS)
|
||||||
val senderName = getNameFromPhoneNumber(senderNumber)
|
val senderName = getNameAndPhotoFromPhoneNumber(senderNumber)?.name ?: ""
|
||||||
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 thread = cursor.getIntValue(Sms.THREAD_ID)
|
val thread = cursor.getIntValue(Sms.THREAD_ID)
|
||||||
@ -190,8 +191,10 @@ fun Context.getThreadParticipants(threadId: Int, contactsMap: HashMap<Int, Conta
|
|||||||
}
|
}
|
||||||
|
|
||||||
val phoneNumber = getPhoneNumberFromAddressId(addressId)
|
val phoneNumber = getPhoneNumberFromAddressId(addressId)
|
||||||
val name = getNameFromPhoneNumber(phoneNumber)
|
val namePhoto = getNameAndPhotoFromPhoneNumber(phoneNumber)
|
||||||
val contact = Contact(addressId, name, "", phoneNumber, false)
|
val name = namePhoto?.name ?: ""
|
||||||
|
val photoUri = namePhoto?.photoUri ?: ""
|
||||||
|
val contact = Contact(addressId, name, photoUri, phoneNumber, false)
|
||||||
participants.add(contact)
|
participants.add(contact)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -253,27 +256,31 @@ fun Context.getAvailableContacts(callback: (ArrayList<Contact>) -> Unit) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.getNameFromPhoneNumber(number: String): String {
|
fun Context.getNameAndPhotoFromPhoneNumber(number: String): NamePhoto? {
|
||||||
if (!hasPermission(PERMISSION_READ_CONTACTS)) {
|
if (!hasPermission(PERMISSION_READ_CONTACTS)) {
|
||||||
return number
|
return NamePhoto(number, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
val uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number))
|
val uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number))
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
PhoneLookup.DISPLAY_NAME
|
PhoneLookup.DISPLAY_NAME,
|
||||||
|
PhoneLookup.PHOTO_URI
|
||||||
)
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val cursor = contentResolver.query(uri, projection, null, null, null)
|
val cursor = contentResolver.query(uri, projection, null, null, null)
|
||||||
cursor.use {
|
cursor.use {
|
||||||
if (cursor?.moveToFirst() == true) {
|
if (cursor?.moveToFirst() == true) {
|
||||||
return cursor.getStringValue(PhoneLookup.DISPLAY_NAME)
|
val name = cursor.getStringValue(PhoneLookup.DISPLAY_NAME)
|
||||||
|
val photoUri = cursor.getStringValue(PhoneLookup.PHOTO_URI)
|
||||||
|
return NamePhoto(name, photoUri)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
showErrorToast(e)
|
showErrorToast(e)
|
||||||
}
|
}
|
||||||
return number
|
|
||||||
|
return NamePhoto(number, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.getContactNames(): List<Contact> {
|
fun Context.getContactNames(): List<Contact> {
|
||||||
@ -452,7 +459,7 @@ fun Context.showReceivedMessageNotification(address: String, body: String, threa
|
|||||||
|
|
||||||
val pendingIntent = PendingIntent.getActivity(this, threadID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
val pendingIntent = PendingIntent.getActivity(this, threadID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||||
val summaryText = getString(R.string.new_message)
|
val summaryText = getString(R.string.new_message)
|
||||||
val sender = getNameFromPhoneNumber(address)
|
val sender = getNameAndPhotoFromPhoneNumber(address)?.name ?: ""
|
||||||
|
|
||||||
val largeIcon = bitmap ?: getNotificationLetterIcon(sender.toCharArray().getOrNull(0)?.toString() ?: "S")
|
val largeIcon = bitmap ?: getNotificationLetterIcon(sender.toCharArray().getOrNull(0)?.toString() ?: "S")
|
||||||
val builder = NotificationCompat.Builder(this, channelId)
|
val builder = NotificationCompat.Builder(this, channelId)
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.simplemobiletools.smsmessenger.models
|
||||||
|
|
||||||
|
data class NamePhoto(val name: String, val photoUri: String?)
|
Loading…
x
Reference in New Issue
Block a user