changing message id from Int to Long

This commit is contained in:
tibbi 2020-12-21 12:40:29 +01:00
parent f412f401d3
commit ceb9896988
6 changed files with 27 additions and 29 deletions

View File

@ -81,9 +81,9 @@ class ThreadAdapter(activity: SimpleActivity, var messages: ArrayList<ThreadItem
override fun getIsItemSelectable(position: Int) = !isThreadDateTime(position) override fun getIsItemSelectable(position: Int) = !isThreadDateTime(position)
override fun getItemSelectionKey(position: Int) = (messages.getOrNull(position) as? Message)?.id override fun getItemSelectionKey(position: Int) = (messages.getOrNull(position) as? Message)?.hashCode()
override fun getItemKeyPosition(key: Int) = messages.indexOfFirst { (it as? Message)?.id == key } override fun getItemKeyPosition(key: Int) = messages.indexOfFirst { (it as? Message)?.hashCode() == key }
override fun onActionModeCreated() {} override fun onActionModeCreated() {}
@ -169,7 +169,7 @@ class ThreadAdapter(activity: SimpleActivity, var messages: ArrayList<ThreadItem
return return
} }
val messagesToRemove = messages.filter { selectedKeys.contains((it as? Message)?.id ?: 0) } as ArrayList<ThreadItem> val messagesToRemove = getSelectedItems()
val positions = getSelectedItemPositions() val positions = getSelectedItemPositions()
messagesToRemove.forEach { messagesToRemove.forEach {
activity.deleteMessage((it as Message).id, it.isMMS) activity.deleteMessage((it as Message).id, it.isMMS)
@ -186,7 +186,7 @@ class ThreadAdapter(activity: SimpleActivity, var messages: ArrayList<ThreadItem
} }
} }
private fun getSelectedItems() = messages.filter { selectedKeys.contains((it as? Message)?.id ?: 0) } as ArrayList<ThreadItem> private fun getSelectedItems() = messages.filter { selectedKeys.contains((it as? Message)?.hashCode() ?: 0) } as ArrayList<ThreadItem>
private fun isThreadDateTime(position: Int) = messages.getOrNull(position) is ThreadDateTime private fun isThreadDateTime(position: Int) = messages.getOrNull(position) is ThreadDateTime
@ -199,7 +199,7 @@ class ThreadAdapter(activity: SimpleActivity, var messages: ArrayList<ThreadItem
private fun setupView(view: View, message: Message) { private fun setupView(view: View, message: Message) {
view.apply { view.apply {
thread_message_holder.isSelected = selectedKeys.contains(message.id) thread_message_holder.isSelected = selectedKeys.contains(message.hashCode())
thread_message_body.apply { thread_message_body.apply {
text = message.body text = message.body
setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize) setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)

View File

@ -76,12 +76,12 @@ fun Context.getMessages(threadId: Int): ArrayList<Message> {
return@queryCursor return@queryCursor
} }
val id = cursor.getIntValue(Sms._ID) val id = cursor.getLongValue(Sms._ID)
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 namePhoto = getNameAndPhotoFromPhoneNumber(senderNumber) val namePhoto = getNameAndPhotoFromPhoneNumber(senderNumber)
val senderName = namePhoto?.name ?: "" val senderName = namePhoto.name
val photoUri = namePhoto?.photoUri ?: "" val photoUri = namePhoto.photoUri ?: ""
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)
@ -127,7 +127,7 @@ fun Context.getMMS(threadId: Int? = null, sortOrder: String? = null): ArrayList<
val contactsMap = HashMap<Int, SimpleContact>() val contactsMap = HashMap<Int, SimpleContact>()
val threadParticipants = HashMap<Int, ArrayList<SimpleContact>>() val threadParticipants = HashMap<Int, ArrayList<SimpleContact>>()
queryCursor(uri, projection, selection, selectionArgs, sortOrder, showErrors = true) { cursor -> queryCursor(uri, projection, selection, selectionArgs, sortOrder, showErrors = true) { cursor ->
val mmsId = cursor.getIntValue(Mms._ID) val mmsId = cursor.getLongValue(Mms._ID)
val type = cursor.getIntValue(Mms.MESSAGE_BOX) val type = cursor.getIntValue(Mms.MESSAGE_BOX)
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
@ -143,17 +143,15 @@ fun Context.getMMS(threadId: Int? = null, sortOrder: String? = null): ArrayList<
val isMMS = true val isMMS = true
val attachment = getMmsAttachment(mmsId) val attachment = getMmsAttachment(mmsId)
val body = attachment?.text ?: "" val body = attachment.text
var senderName = "" var senderName = ""
var senderPhotoUri = "" var senderPhotoUri = ""
if (type != Mms.MESSAGE_BOX_SENT && type != Mms.MESSAGE_BOX_FAILED) { if (type != Mms.MESSAGE_BOX_SENT && type != Mms.MESSAGE_BOX_FAILED) {
val number = getMMSSender(mmsId) val number = getMMSSender(mmsId)
val namePhoto = getNameAndPhotoFromPhoneNumber(number) val namePhoto = getNameAndPhotoFromPhoneNumber(number)
if (namePhoto != null) { senderName = namePhoto.name
senderName = namePhoto.name senderPhotoUri = namePhoto.photoUri ?: ""
senderPhotoUri = namePhoto.photoUri ?: ""
}
} }
val message = Message(mmsId, body, type, participants, date, read, threadId, isMMS, attachment, senderName, senderPhotoUri, subscriptionId) val message = Message(mmsId, body, type, participants, date, read, threadId, isMMS, attachment, senderName, senderPhotoUri, subscriptionId)
@ -167,7 +165,7 @@ fun Context.getMMS(threadId: Int? = null, sortOrder: String? = null): ArrayList<
return messages return messages
} }
fun Context.getMMSSender(msgId: Int): String { fun Context.getMMSSender(msgId: Long): String {
val uri = Uri.parse("${Mms.CONTENT_URI}/$msgId/addr") val uri = Uri.parse("${Mms.CONTENT_URI}/$msgId/addr")
val projection = arrayOf( val projection = arrayOf(
Mms.Addr.ADDRESS Mms.Addr.ADDRESS
@ -241,7 +239,7 @@ fun Context.getConversations(threadId: Long? = null, privateContacts: ArrayList<
// based on https://stackoverflow.com/a/6446831/1967672 // based on https://stackoverflow.com/a/6446831/1967672
@SuppressLint("NewApi") @SuppressLint("NewApi")
fun Context.getMmsAttachment(id: Int): MessageAttachment? { fun Context.getMmsAttachment(id: Long): MessageAttachment {
val uri = if (isQPlus()) { val uri = if (isQPlus()) {
Mms.Part.CONTENT_URI Mms.Part.CONTENT_URI
} else { } else {
@ -335,8 +333,8 @@ fun Context.getThreadParticipants(threadId: Int, contactsMap: HashMap<Int, Simpl
val phoneNumber = getPhoneNumberFromAddressId(addressId) val phoneNumber = getPhoneNumberFromAddressId(addressId)
val namePhoto = getNameAndPhotoFromPhoneNumber(phoneNumber) val namePhoto = getNameAndPhotoFromPhoneNumber(phoneNumber)
val name = namePhoto?.name ?: "" val name = namePhoto.name
val photoUri = namePhoto?.photoUri ?: "" val photoUri = namePhoto.photoUri ?: ""
val contact = SimpleContact(addressId, addressId, name, photoUri, arrayListOf(phoneNumber), ArrayList(), ArrayList()) val contact = SimpleContact(addressId, addressId, name, photoUri, arrayListOf(phoneNumber), ArrayList(), ArrayList())
participants.add(contact) participants.add(contact)
} }
@ -410,9 +408,9 @@ fun Context.getSuggestedContacts(privateContacts: ArrayList<SimpleContact>): Arr
queryCursor(uri, projection, selection, selectionArgs, sortOrder, showErrors = true) { cursor -> queryCursor(uri, projection, selection, selectionArgs, sortOrder, showErrors = true) { cursor ->
val senderNumber = cursor.getStringValue(Sms.ADDRESS) ?: return@queryCursor val senderNumber = cursor.getStringValue(Sms.ADDRESS) ?: return@queryCursor
val namePhoto = getNameAndPhotoFromPhoneNumber(senderNumber) val namePhoto = getNameAndPhotoFromPhoneNumber(senderNumber)
var senderName = namePhoto?.name ?: "" var senderName = namePhoto.name
var photoUri = namePhoto?.photoUri ?: "" var photoUri = namePhoto.photoUri ?: ""
if (namePhoto == null || isNumberBlocked(senderNumber, blockedNumbers)) { if (isNumberBlocked(senderNumber, blockedNumbers)) {
return@queryCursor return@queryCursor
} else if (namePhoto.name == senderNumber) { } else if (namePhoto.name == senderNumber) {
if (privateContacts.isNotEmpty()) { if (privateContacts.isNotEmpty()) {
@ -437,7 +435,7 @@ fun Context.getSuggestedContacts(privateContacts: ArrayList<SimpleContact>): Arr
return contacts return contacts
} }
fun Context.getNameAndPhotoFromPhoneNumber(number: String): NamePhoto? { fun Context.getNameAndPhotoFromPhoneNumber(number: String): NamePhoto {
if (!hasPermission(PERMISSION_READ_CONTACTS)) { if (!hasPermission(PERMISSION_READ_CONTACTS)) {
return NamePhoto(number, null) return NamePhoto(number, null)
} }
@ -497,7 +495,7 @@ fun Context.deleteConversation(threadId: Int) {
conversationsDB.deleteThreadId(threadId.toLong()) conversationsDB.deleteThreadId(threadId.toLong())
} }
fun Context.deleteMessage(id: Int, isMMS: Boolean) { fun Context.deleteMessage(id: Long, isMMS: Boolean) {
val uri = if (isMMS) Mms.CONTENT_URI else Sms.CONTENT_URI val uri = if (isMMS) Mms.CONTENT_URI else Sms.CONTENT_URI
val selection = "${Sms._ID} = ?" val selection = "${Sms._ID} = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(id.toString())
@ -508,7 +506,7 @@ fun Context.deleteMessage(id: Int, isMMS: Boolean) {
} }
} }
fun Context.markMessageRead(id: Int, isMMS: Boolean) { fun Context.markMessageRead(id: Long, isMMS: Boolean) {
val uri = if (isMMS) Mms.CONTENT_URI else Sms.CONTENT_URI val uri = if (isMMS) Mms.CONTENT_URI else Sms.CONTENT_URI
val contentValues = ContentValues().apply { val contentValues = ContentValues().apply {
put(Sms.READ, 1) put(Sms.READ, 1)
@ -581,7 +579,7 @@ fun Context.getThreadId(addresses: Set<String>): Long {
fun Context.showReceivedMessageNotification(address: String, body: String, threadID: Int, bitmap: Bitmap?) { fun Context.showReceivedMessageNotification(address: String, body: String, threadID: Int, bitmap: Bitmap?) {
val privateCursor = getMyContactsCursor()?.loadInBackground() val privateCursor = getMyContactsCursor()?.loadInBackground()
ensureBackgroundThread { ensureBackgroundThread {
var sender = getNameAndPhotoFromPhoneNumber(address)?.name ?: "" var sender = getNameAndPhotoFromPhoneNumber(address).name
if (address == sender) { if (address == sender) {
val privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor) val privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor)
sender = privateContacts.firstOrNull { it.doesContainPhoneNumber(address) }?.name ?: address sender = privateContacts.firstOrNull { it.doesContainPhoneNumber(address) }?.name ?: address

View File

@ -4,7 +4,7 @@ import android.provider.Telephony
import com.simplemobiletools.commons.models.SimpleContact import com.simplemobiletools.commons.models.SimpleContact
data class Message( data class Message(
val id: Int, val body: String, val type: Int, val participants: ArrayList<SimpleContact>, val date: Int, val read: Boolean, val thread: Int, val id: Long, val body: String, val type: Int, val participants: ArrayList<SimpleContact>, val date: Int, val read: Boolean, val thread: Int,
val isMMS: Boolean, val attachment: MessageAttachment?, var senderName: String, val senderPhotoUri: String, val subscriptionId: Int) : ThreadItem() { val isMMS: Boolean, val attachment: MessageAttachment?, var senderName: String, val senderPhotoUri: String, val subscriptionId: Int) : ThreadItem() {
fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX
} }

View File

@ -1,3 +1,3 @@
package com.simplemobiletools.smsmessenger.models package com.simplemobiletools.smsmessenger.models
data class MessageAttachment(val id: Int, var text: String, var attachments: ArrayList<Attachment>) data class MessageAttachment(val id: Long, var text: String, var attachments: ArrayList<Attachment>)

View File

@ -1,3 +1,3 @@
package com.simplemobiletools.smsmessenger.models package com.simplemobiletools.smsmessenger.models
data class ThreadError(val messageID: Int) : ThreadItem() data class ThreadError(val messageID: Long) : ThreadItem()

View File

@ -1,4 +1,4 @@
package com.simplemobiletools.smsmessenger.models package com.simplemobiletools.smsmessenger.models
// show a check after the latest message, if it is a sent one and succeeded // show a check after the latest message, if it is a sent one and succeeded
data class ThreadSuccess(val messageID: Int) : ThreadItem() data class ThreadSuccess(val messageID: Long) : ThreadItem()