communicate: persist inbound SMS instead of dropping the broadcast
- Commit
9608763338cde21d0abb7c3daf44d6ad0c358652- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-22 04:05 UTC (3 months ago)
- Parents
9c52e6a
Diff
communicate/src/main/java/com/vayunmathur/communicate/telephony/SmsDeliverReceiver.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/telephony/SmsDeliverReceiver.kt b/communicate/src/main/java/com/vayunmathur/communicate/telephony/SmsDeliverReceiver.ktindex 28e7052a99..664f631574 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/telephony/SmsDeliverReceiver.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/telephony/SmsDeliverReceiver.kt@@ -1,9 +1,69 @@ package com.vayunmathur.communicate.telephony import android.content.BroadcastReceiver+import android.content.ContentValues import android.content.Context import android.content.Intent+import android.provider.Telephony+import android.util.Log +/**+ * Handles inbound SMS as the default SMS app. Android delivers `SMS_DELIVER` only to the default+ * SMS app, which is then solely responsible for writing the message into the provider — nothing+ * else persists it, so dropping the broadcast loses the message entirely.+ *+ * Multipart messages arrive as several PDUs in one broadcast and are concatenated into a single row,+ * matching how [Telephony.Sms] is read back by the repository.+ */ class SmsDeliverReceiver : BroadcastReceiver() {- override fun onReceive(context: Context, intent: Intent) = Unit+ override fun onReceive(context: Context, intent: Intent) {+ if (intent.action != Telephony.Sms.Intents.SMS_DELIVER_ACTION) return+ val parts = runCatching { Telephony.Sms.Intents.getMessagesFromIntent(intent) }+ .getOrNull()?.takeIf { it.isNotEmpty() } ?: run {+ Log.w(TAG, "SMS_DELIVER with no parsable messages")+ return+ }+ // Present for multi-SIM; absent on single-SIM devices.+ val subscriptionId = intent.getIntExtra("subscription", -1)+ val pending = goAsync()+ Thread {+ runCatching { insertInboundSms(context, parts, subscriptionId) }+ .onFailure { Log.e(TAG, "failed to store inbound SMS", it) }+ pending.finish()+ }.start()+ }++ private fun insertInboundSms(+ context: Context,+ parts: Array<android.telephony.SmsMessage>,+ subscriptionId: Int,+ ) {+ val first = parts.first()+ val address = first.displayOriginatingAddress ?: first.originatingAddress+ val body = parts.joinToString("") { it.displayMessageBody.orEmpty() }+ val threadId = address?.let {+ runCatching { Telephony.Threads.getOrCreateThreadId(context, it) }.getOrNull()+ }+ val values = ContentValues().apply {+ if (threadId != null) put(Telephony.Sms.THREAD_ID, threadId)+ put(Telephony.Sms.ADDRESS, address)+ put(Telephony.Sms.BODY, body)+ // DATE is receipt time; DATE_SENT is the timestamp the sender's network stamped.+ put(Telephony.Sms.DATE, System.currentTimeMillis())+ put(Telephony.Sms.DATE_SENT, first.timestampMillis)+ put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_INBOX)+ put(Telephony.Sms.READ, 0)+ put(Telephony.Sms.SEEN, 0)+ put(Telephony.Sms.PROTOCOL, first.protocolIdentifier)+ put(Telephony.Sms.REPLY_PATH_PRESENT, if (first.isReplyPathPresent) 1 else 0)+ first.serviceCenterAddress?.let { put(Telephony.Sms.SERVICE_CENTER, it) }+ if (subscriptionId >= 0) put(Telephony.Sms.SUBSCRIPTION_ID, subscriptionId)+ }+ context.contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values)+ Log.d(TAG, "stored inbound SMS parts=${parts.size} thread=$threadId")+ }++ companion object {+ private const val TAG = "SmsDeliverReceiver"+ } }