creating a helper function for fetching the thread id

This commit is contained in:
tibbi 2020-04-05 17:38:25 +02:00
parent 3373ce66e1
commit 420a88c74c
3 changed files with 19 additions and 14 deletions

View File

@ -50,8 +50,8 @@ class ThreadActivity : SimpleActivity() {
}
private fun setupAdapter() {
val threadID = intent.getIntExtra(THREAD_ID, 0)
val items = getThreadItems(threadID)
val threadId = intent.getIntExtra(THREAD_ID, 0)
val items = getThreadItems(threadId)
runOnUiThread {
val adapter = ThreadAdapter(this, items, thread_messages_list, thread_messages_fastscroller) {}

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.smsmessenger.extensions
import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
@ -9,6 +10,7 @@ import android.provider.Telephony
import android.text.TextUtils
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.isMarshmallowPlus
import com.simplemobiletools.smsmessenger.helpers.Config
import com.simplemobiletools.smsmessenger.models.Message
@ -160,7 +162,7 @@ fun Context.getNameFromPhoneNumber(number: String): Int? {
return null
}
fun Context.insertNewSMS(address: String, subject: String, body: String, date: Long, threadID: Long) {
fun Context.insertNewSMS(address: String, subject: String, body: String, date: Long, threadId: Long) {
val uri = Telephony.Sms.CONTENT_URI
val contentValues = ContentValues().apply {
put(Telephony.Sms.ADDRESS, address)
@ -168,7 +170,7 @@ fun Context.insertNewSMS(address: String, subject: String, body: String, date: L
put(Telephony.Sms.BODY, body)
put(Telephony.Sms.DATE, date)
put(Telephony.Sms.READ, 0)
put(Telephony.Sms.THREAD_ID, threadID)
put(Telephony.Sms.THREAD_ID, threadId)
put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_INBOX)
}
@ -198,3 +200,12 @@ fun Context.markSMSRead(id: Int) {
val selectionArgs = arrayOf(id.toString(), "0")
contentResolver.update(uri, contentValues, selection, selectionArgs)
}
@SuppressLint("NewApi")
fun Context.getThreadId(address: String): Long {
return if (isMarshmallowPlus()) {
Telephony.Threads.getOrCreateThreadId(this, address)
} else {
0
}
}

View File

@ -19,10 +19,10 @@ import android.widget.TextView
import androidx.core.app.NotificationCompat
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.getContrastColor
import com.simplemobiletools.commons.helpers.isMarshmallowPlus
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.ThreadActivity
import com.simplemobiletools.smsmessenger.extensions.getThreadId
import com.simplemobiletools.smsmessenger.extensions.insertNewSMS
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
import com.simplemobiletools.smsmessenger.helpers.THREAD_NAME
@ -30,7 +30,6 @@ import com.simplemobiletools.smsmessenger.helpers.THREAD_NUMBER
import com.simplemobiletools.smsmessenger.helpers.refreshMessages
class SmsReceiver : BroadcastReceiver() {
@SuppressLint("NewApi")
override fun onReceive(context: Context, intent: Intent) {
val messages = Telephony.Sms.Intents.getMessagesFromIntent(intent)
messages.forEach {
@ -38,14 +37,9 @@ class SmsReceiver : BroadcastReceiver() {
val subject = it.pseudoSubject
val body = it.messageBody
val date = it.timestampMillis
val threadID = if (isMarshmallowPlus()) {
Telephony.Threads.getOrCreateThreadId(context, address)
} else {
0
}
context.insertNewSMS(address, subject, body, date, threadID)
showNotification(context, address, body, threadID.toInt())
val threadId = context.getThreadId(address)
context.insertNewSMS(address, subject, body, date, threadId)
showNotification(context, address, body, threadId.toInt())
}
refreshMessages()