create a Conversations model, separate it from Messages

This commit is contained in:
tibbi 2020-04-11 19:15:31 +02:00
parent 9cf479c496
commit 882b75324d
2 changed files with 72 additions and 4 deletions

View File

@ -37,10 +37,7 @@ import com.simplemobiletools.smsmessenger.activities.ThreadActivity
import com.simplemobiletools.smsmessenger.helpers.Config
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
import com.simplemobiletools.smsmessenger.helpers.letterBackgroundColors
import com.simplemobiletools.smsmessenger.models.Contact
import com.simplemobiletools.smsmessenger.models.Message
import com.simplemobiletools.smsmessenger.models.MessageAttachment
import com.simplemobiletools.smsmessenger.models.NamePhoto
import com.simplemobiletools.smsmessenger.models.*
import java.util.*
import kotlin.collections.ArrayList
@ -147,6 +144,37 @@ fun Context.getMMS(threadId: Int? = null, sortOrder: String? = null): ArrayList<
return messages
}
fun Context.getConversations(): ArrayList<Conversation> {
val uri = Uri.parse("${Threads.CONTENT_URI}?simple=true")
val projection = arrayOf(
Threads._ID,
Threads.SNIPPET,
Threads.DATE,
Threads.READ,
Threads.RECIPIENT_IDS
)
val conversations = ArrayList<Conversation>()
queryCursor(uri, projection, null, null, showErrors = true) { cursor ->
val id = cursor.getIntValue(Threads._ID)
val snippet = cursor.getStringValue(Threads.SNIPPET) ?: ""
var date = cursor.getLongValue(Threads.DATE)
if (date.toString().length > 10) {
date /= 1000
}
val read = cursor.getIntValue(Threads.READ) == 1
val rawIds = cursor.getStringValue(Threads.RECIPIENT_IDS)
val recipientIds = rawIds.split(" ").filter { it.areDigitsOnly() }.map { it.toInt() }.toMutableList()
val phoneNumbers = getThreadPhoneNumbers(recipientIds)
val names = getThreadContactNames(phoneNumbers)
val title = TextUtils.join(", ", names.toTypedArray())
val conversation = Conversation(id, snippet, date.toInt(), read, title)
conversations.add(conversation)
}
return conversations
}
// based on https://stackoverflow.com/a/6446831/1967672
@SuppressLint("NewApi")
fun Context.getMmsAttachment(id: Int): MessageAttachment? {
@ -219,6 +247,22 @@ fun Context.getThreadParticipants(threadId: Int, contactsMap: HashMap<Int, Conta
return participants
}
fun Context.getThreadPhoneNumbers(recipientIds: List<Int>): ArrayList<String> {
val numbers = ArrayList<String>()
recipientIds.forEach {
numbers.add(getPhoneNumberFromAddressId(it))
}
return numbers
}
fun Context.getThreadContactNames(phoneNumbers: List<String>): ArrayList<String> {
val names = ArrayList<String>()
phoneNumbers.forEach {
names.add(getNameFromPhoneNumber(it))
}
return names
}
fun Context.getPhoneNumberFromAddressId(canonicalAddressId: Int): String {
val uri = Uri.withAppendedPath(MmsSms.CONTENT_URI, "canonical-addresses")
val projection = arrayOf(
@ -295,6 +339,27 @@ fun Context.getNameAndPhotoFromPhoneNumber(number: String): NamePhoto? {
return NamePhoto(number, null)
}
fun Context.getNameFromPhoneNumber(number: String): String {
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> {
val contacts = ArrayList<Contact>()
val uri = ContactsContract.Data.CONTENT_URI

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.smsmessenger.models
data class Conversation(val id: Int, val snippet: String, val date: Int, val read: Boolean, val title: String)