When incoming call or message arrives and baresip is not running, start baresip and show notification
This commit is contained in:
@ -121,6 +121,9 @@ class BaresipService: Service() {
|
|||||||
private lateinit var stopState: String
|
private lateinit var stopState: String
|
||||||
private lateinit var quitTimer: CountDownTimer
|
private lateinit var quitTimer: CountDownTimer
|
||||||
|
|
||||||
|
private data class PendingMessage(val sender: String, val body: String, val time: Long)
|
||||||
|
private val pendingMessages = mutableListOf<PendingMessage>()
|
||||||
|
|
||||||
private var vbTimer: Timer? = null
|
private var vbTimer: Timer? = null
|
||||||
private var origVolume = mutableMapOf<Int, Int>()
|
private var origVolume = mutableMapOf<Int, Int>()
|
||||||
private var linkAddresses = mutableMapOf<String, String>()
|
private var linkAddresses = mutableMapOf<String, String>()
|
||||||
@ -473,6 +476,29 @@ class BaresipService: Service() {
|
|||||||
|
|
||||||
"Start" -> {
|
"Start" -> {
|
||||||
|
|
||||||
|
val sender = intent?.getStringExtra("sender")
|
||||||
|
val body = intent?.getStringExtra("body")
|
||||||
|
val time = intent?.getLongExtra("time", 0L) ?: 0L
|
||||||
|
|
||||||
|
if (sender != null && body != null && time != 0L) {
|
||||||
|
Handler(Looper.getMainLooper()).post {
|
||||||
|
if (isNativeReady) {
|
||||||
|
val mobileUa = uas.value.find { it.account.isMobile }
|
||||||
|
if (mobileUa != null)
|
||||||
|
handleIncomingMessage(mobileUa.uap, sender, body, time)
|
||||||
|
else
|
||||||
|
synchronized(pendingMessages) {
|
||||||
|
pendingMessages.add(PendingMessage(sender, body, time))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
synchronized(pendingMessages) {
|
||||||
|
pendingMessages.add(PendingMessage(sender, body, time))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isStartReceived || isServiceRunning) return START_STICKY
|
||||||
isStartReceived = true
|
isStartReceived = true
|
||||||
|
|
||||||
if (VERSION.SDK_INT < 31) {
|
if (VERSION.SDK_INT < 31) {
|
||||||
@ -1646,7 +1672,30 @@ class BaresipService: Service() {
|
|||||||
fun started() {
|
fun started() {
|
||||||
Log.d(TAG, "Received 'started' from baresip")
|
Log.d(TAG, "Received 'started' from baresip")
|
||||||
isNativeReady = true
|
isNativeReady = true
|
||||||
|
|
||||||
|
Handler(Looper.getMainLooper()).post {
|
||||||
addMobileUserAgent()
|
addMobileUserAgent()
|
||||||
|
|
||||||
|
val mobileUa = uas.value.find { it.account.isMobile }
|
||||||
|
if (mobileUa != null)
|
||||||
|
synchronized(pendingMessages) {
|
||||||
|
for (m in pendingMessages)
|
||||||
|
handleIncomingMessage(mobileUa.uap, m.sender, m.body, m.time)
|
||||||
|
pendingMessages.clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process any calls that arrived while baresip is starting
|
||||||
|
InCallService.instance?.let { inCallService ->
|
||||||
|
for (call in inCallService.getCalls()) {
|
||||||
|
val aor = call.details.intentExtras?.getString("aor")
|
||||||
|
if (Call.ofCallp(call.hashCode().toLong()) == null) {
|
||||||
|
Log.d(TAG, "Processing pending external call ${call.hashCode()}")
|
||||||
|
handleExternalCall(call, aor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (VERSION.SDK_INT >= 31 && !telephonyCallbackRegistered) {
|
if (VERSION.SDK_INT >= 31 && !telephonyCallbackRegistered) {
|
||||||
try {
|
try {
|
||||||
telephonyManager.registerTelephonyCallback(mainExecutor, telephonyCallback)
|
telephonyManager.registerTelephonyCallback(mainExecutor, telephonyCallback)
|
||||||
|
|||||||
@ -23,9 +23,18 @@ class InCallService : InCallService() {
|
|||||||
if (handle == baresipHandle) {
|
if (handle == baresipHandle) {
|
||||||
Log.d(TAG, "InCallService: Identified as SIP call")
|
Log.d(TAG, "InCallService: Identified as SIP call")
|
||||||
// SIP call is already managed by ConnectionService/BaresipService
|
// SIP call is already managed by ConnectionService/BaresipService
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
Log.d(TAG, "InCallService: Identified as PSTN call from $handle")
|
Log.d(TAG, "InCallService: Identified as PSTN call from $handle")
|
||||||
val aor = call.details.intentExtras?.getString("aor")
|
val aor = call.details.intentExtras?.getString("aor")
|
||||||
|
|
||||||
|
if (BaresipService.instance == null) {
|
||||||
|
Log.i(TAG, "InCallService: BaresipService not running, starting it")
|
||||||
|
val intent = Intent(this, BaresipService::class.java)
|
||||||
|
intent.action = "Start"
|
||||||
|
androidx.core.content.ContextCompat.startForegroundService(this, intent)
|
||||||
|
}
|
||||||
|
|
||||||
BaresipService.instance?.handleExternalCall(call, aor)
|
BaresipService.instance?.handleExternalCall(call, aor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,24 +39,13 @@ class MmsReceiver : BroadcastReceiver() {
|
|||||||
val body = getMmsText(context, id)
|
val body = getMmsText(context, id)
|
||||||
|
|
||||||
if (body.isNotEmpty()) {
|
if (body.isNotEmpty()) {
|
||||||
Log.d(TAG, "Extracted MMS text from $address: $body")
|
Log.d(TAG, "Extracted MMS text from $address, starting service")
|
||||||
|
val serviceIntent = Intent(context, BaresipService::class.java)
|
||||||
val mobileUa = BaresipService.uas.value.find { it.account.isMobile }
|
serviceIntent.action = "Start"
|
||||||
if (mobileUa != null) {
|
serviceIntent.putExtra("sender", "tel:$address")
|
||||||
// Notify Service for history update, notification, and alert sound
|
serviceIntent.putExtra("body", body)
|
||||||
if (BaresipService.isServiceRunning) {
|
serviceIntent.putExtra("time", date)
|
||||||
BaresipService.instance?.handleIncomingMessage(mobileUa.uap, "tel:$address", body, date)
|
ContextCompat.startForegroundService(context, serviceIntent)
|
||||||
} else {
|
|
||||||
// Service not running, at least save to history
|
|
||||||
val aor = mobileUa.account.aor
|
|
||||||
// Check if this message was already added (simple deduplication by timestamp)
|
|
||||||
val lastMsg = Message.messages().lastOrNull { m -> m.aor == aor }
|
|
||||||
if (lastMsg == null || lastMsg.timeStamp != date || lastMsg.peerUri != "tel:$address") {
|
|
||||||
Message(aor, "tel:$address", body, date, MESSAGE_DOWN, 0, "", true).add()
|
|
||||||
mobileUa.account.unreadMessages = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,11 +80,10 @@ class MmsReceiver : BroadcastReceiver() {
|
|||||||
val type = it.getString(it.getColumnIndexOrThrow("ct"))
|
val type = it.getString(it.getColumnIndexOrThrow("ct"))
|
||||||
if (type == "text/plain") {
|
if (type == "text/plain") {
|
||||||
val data = it.getString(it.getColumnIndexOrThrow("_data"))
|
val data = it.getString(it.getColumnIndexOrThrow("_data"))
|
||||||
val body = if (data != null) {
|
val body = if (data != null)
|
||||||
getPartText(context, it.getString(it.getColumnIndexOrThrow("_id")))
|
getPartText(context, it.getString(it.getColumnIndexOrThrow("_id")))
|
||||||
} else {
|
else
|
||||||
it.getString(it.getColumnIndexOrThrow("text"))
|
it.getString(it.getColumnIndexOrThrow("text"))
|
||||||
}
|
|
||||||
if (body != null) sb.append(body)
|
if (body != null) sb.append(body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
|
||||||
class SmsReceiver : BroadcastReceiver() {
|
class SmsReceiver : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
@ -15,20 +16,14 @@ class SmsReceiver : BroadcastReceiver() {
|
|||||||
val body = messages.joinToString("") { it.displayMessageBody ?: "" }
|
val body = messages.joinToString("") { it.displayMessageBody ?: "" }
|
||||||
val timestamp = messages[0].timestampMillis
|
val timestamp = messages[0].timestampMillis
|
||||||
|
|
||||||
Log.d(TAG, "Received SMS from $sender: $body")
|
Log.d(TAG, "Received SMS from $sender, starting service")
|
||||||
|
|
||||||
val mobileUa = BaresipService.uas.value.find { it.account.isMobile }
|
val serviceIntent = Intent(context, BaresipService::class.java)
|
||||||
if (mobileUa != null) {
|
serviceIntent.action = "Start"
|
||||||
// Notify Service for history update, notification, and alert sound
|
serviceIntent.putExtra("sender", "tel:$sender")
|
||||||
if (BaresipService.isServiceRunning) {
|
serviceIntent.putExtra("body", body)
|
||||||
BaresipService.instance?.handleIncomingMessage(mobileUa.uap, "tel:$sender", body, timestamp)
|
serviceIntent.putExtra("time", timestamp)
|
||||||
} else {
|
ContextCompat.startForegroundService(context, serviceIntent)
|
||||||
// Service not running, at least save to history
|
|
||||||
val aor = mobileUa.account.aor
|
|
||||||
Message(aor, "tel:$sender", body, timestamp, MESSAGE_DOWN, 0, "", true).add()
|
|
||||||
mobileUa.account.unreadMessages = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user