package com.vanta.smsgateway import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.provider.Telephony import kotlinx.coroutines.* /** * Receives incoming SMS when app is set as default SMS app. * Forwards each message to cPanel /inbox endpoint. * Does NOT show any UI — Google Messages handles display once restored as default. */ class SmsReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (intent.action != Telephony.Sms.Intents.SMS_DELIVER_ACTION && intent.action != Telephony.Sms.Intents.SMS_RECEIVED_ACTION) return val messages = Telephony.Sms.Intents.getMessagesFromIntent(intent) if (messages.isNullOrEmpty()) return // Group multi-part messages by originating address val grouped = mutableMapOf() for (msg in messages) { val from = msg.originatingAddress ?: continue grouped.getOrPut(from) { StringBuilder() }.append(msg.messageBody) } val prefs = context.getSharedPreferences(SmsService.PREF_NAME, Context.MODE_PRIVATE) val url = prefs.getString(SmsService.PREF_URL, "") ?: "" val apiKey = prefs.getString(SmsService.PREF_KEY, "") ?: "" if (url.isEmpty() || apiKey.isEmpty()) return val client = ApiClient(url, apiKey) CoroutineScope(Dispatchers.IO).launch { grouped.forEach { (from, body) -> client.reportIncoming(from, body.toString()) } } } }