add handling for storing sent messages

This commit is contained in:
tibbi 2020-04-05 19:12:02 +02:00
parent 420a88c74c
commit e1f1583b17
6 changed files with 59 additions and 8 deletions

View File

@ -111,6 +111,8 @@
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver android:name=".receivers.SmsSentReceiver"/>
<activity-alias <activity-alias
android:name=".activities.SplashActivity.Red" android:name=".activities.SplashActivity.Red"
android:enabled="false" android:enabled="false"

View File

@ -1,5 +1,7 @@
package com.simplemobiletools.smsmessenger.activities package com.simplemobiletools.smsmessenger.activities
import android.app.PendingIntent
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.provider.Telephony import android.provider.Telephony
import android.telephony.SmsManager import android.telephony.SmsManager
@ -12,13 +14,12 @@ import com.simplemobiletools.smsmessenger.adapters.ThreadAdapter
import com.simplemobiletools.smsmessenger.extensions.config import com.simplemobiletools.smsmessenger.extensions.config
import com.simplemobiletools.smsmessenger.extensions.getMessages import com.simplemobiletools.smsmessenger.extensions.getMessages
import com.simplemobiletools.smsmessenger.extensions.markSMSRead import com.simplemobiletools.smsmessenger.extensions.markSMSRead
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID import com.simplemobiletools.smsmessenger.helpers.*
import com.simplemobiletools.smsmessenger.helpers.THREAD_NAME
import com.simplemobiletools.smsmessenger.helpers.THREAD_NUMBER
import com.simplemobiletools.smsmessenger.models.Events import com.simplemobiletools.smsmessenger.models.Events
import com.simplemobiletools.smsmessenger.models.ThreadDateTime import com.simplemobiletools.smsmessenger.models.ThreadDateTime
import com.simplemobiletools.smsmessenger.models.ThreadError import com.simplemobiletools.smsmessenger.models.ThreadError
import com.simplemobiletools.smsmessenger.models.ThreadItem import com.simplemobiletools.smsmessenger.models.ThreadItem
import com.simplemobiletools.smsmessenger.receivers.SmsSentReceiver
import kotlinx.android.synthetic.main.activity_thread.* import kotlinx.android.synthetic.main.activity_thread.*
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.Subscribe
@ -60,6 +61,7 @@ class ThreadActivity : SimpleActivity() {
} }
private fun setupButtons() { private fun setupButtons() {
val threadId = intent.getIntExtra(THREAD_ID, 0)
thread_type_message.setColors(config.textColor, config.primaryColor, config.backgroundColor) thread_type_message.setColors(config.textColor, config.primaryColor, config.backgroundColor)
thread_send_message.applyColorFilter(config.textColor) thread_send_message.applyColorFilter(config.textColor)
@ -69,8 +71,15 @@ class ThreadActivity : SimpleActivity() {
return@setOnClickListener return@setOnClickListener
} }
val intent = Intent(this, SmsSentReceiver::class.java).apply {
putExtra(MESSAGE_BODY, msg)
putExtra(MESSAGE_ADDRESS, targetNumber)
}
val pendingIntent = PendingIntent.getBroadcast(this, threadId, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val smsManager = SmsManager.getDefault() val smsManager = SmsManager.getDefault()
smsManager.sendTextMessage(targetNumber, null, msg, null, null) smsManager.sendTextMessage(targetNumber, null, msg, pendingIntent, null)
thread_type_message.setText("")
} }
thread_send_message.isClickable = false thread_send_message.isClickable = false

View File

@ -162,16 +162,16 @@ fun Context.getNameFromPhoneNumber(number: String): Int? {
return null 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, read: Int, threadId: Long, type: Int) {
val uri = Telephony.Sms.CONTENT_URI val uri = Telephony.Sms.CONTENT_URI
val contentValues = ContentValues().apply { val contentValues = ContentValues().apply {
put(Telephony.Sms.ADDRESS, address) put(Telephony.Sms.ADDRESS, address)
put(Telephony.Sms.SUBJECT, subject) put(Telephony.Sms.SUBJECT, subject)
put(Telephony.Sms.BODY, body) put(Telephony.Sms.BODY, body)
put(Telephony.Sms.DATE, date) put(Telephony.Sms.DATE, date)
put(Telephony.Sms.READ, 0) put(Telephony.Sms.READ, read)
put(Telephony.Sms.THREAD_ID, threadId) put(Telephony.Sms.THREAD_ID, threadId)
put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_INBOX) put(Telephony.Sms.TYPE, type)
} }
contentResolver.insert(uri, contentValues) contentResolver.insert(uri, contentValues)

View File

@ -13,6 +13,10 @@ const val THREAD_RECEIVED_MESSAGE = 2
const val THREAD_SENT_MESSAGE = 3 const val THREAD_SENT_MESSAGE = 3
const val THREAD_SENT_MESSAGE_ERROR = 4 const val THREAD_SENT_MESSAGE_ERROR = 4
// constants used at passing info to SmsSentReceiver
const val MESSAGE_BODY = "message_body"
const val MESSAGE_ADDRESS = "message_address"
fun refreshMessages() { fun refreshMessages() {
EventBus.getDefault().post(Events.RefreshMessages()) EventBus.getDefault().post(Events.RefreshMessages())
} }

View File

@ -38,7 +38,9 @@ class SmsReceiver : BroadcastReceiver() {
val body = it.messageBody val body = it.messageBody
val date = it.timestampMillis val date = it.timestampMillis
val threadId = context.getThreadId(address) val threadId = context.getThreadId(address)
context.insertNewSMS(address, subject, body, date, threadId) val type = Telephony.Sms.MESSAGE_TYPE_INBOX
val read = 0
context.insertNewSMS(address, subject, body, date, read, threadId, type)
showNotification(context, address, body, threadId.toInt()) showNotification(context, address, body, threadId.toInt())
} }

View File

@ -0,0 +1,34 @@
package com.simplemobiletools.smsmessenger.receivers
import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.provider.Telephony
import com.simplemobiletools.smsmessenger.extensions.getThreadId
import com.simplemobiletools.smsmessenger.extensions.insertNewSMS
import com.simplemobiletools.smsmessenger.helpers.MESSAGE_ADDRESS
import com.simplemobiletools.smsmessenger.helpers.MESSAGE_BODY
import com.simplemobiletools.smsmessenger.helpers.refreshMessages
class SmsSentReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val extras = intent.extras
if (extras != null) {
val address = extras.getString(MESSAGE_ADDRESS) ?: ""
val subject = ""
val body = extras.getString(MESSAGE_BODY) ?: ""
val date = System.currentTimeMillis()
val threadId = context.getThreadId(address)
val type = if (resultCode == Activity.RESULT_OK) {
Telephony.Sms.MESSAGE_TYPE_SENT
} else {
Telephony.Sms.MESSAGE_TYPE_FAILED
}
val read = 1
context.insertNewSMS(address, subject, body, date, read, threadId, type)
}
refreshMessages()
}
}