Don't show call timer notification on locked screen after call is closed

This commit is contained in:
Juha Heinanen
2026-05-13 10:33:58 +03:00
parent b7bd1181e0
commit 70a4efd5ba
3 changed files with 131 additions and 99 deletions

View File

@ -1089,6 +1089,7 @@ class BaresipService: Service() {
Handler(Looper.getMainLooper()).postDelayed({
connection.destroy()
ConnectionService.connections.remove(callp)
updateStatusNotification()
}, 500)
}
nm.cancel(CALL_NOTIFICATION_ID)
@ -1565,18 +1566,32 @@ class BaresipService: Service() {
fun updateStatusNotification() {
val activeCall = Call.calls().find {
Handler(Looper.getMainLooper()).post {
val activeCall = synchronized(calls) {
calls.find {
it.status.value == "connected" || it.status.value == "outgoing" || it.status.value == "answered"
}
}
val builder = NotificationCompat.Builder(this, LOW_CHANNEL_ID)
val intent = Intent(applicationContext, MainActivity::class.java)
.setAction(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_LAUNCHER)
val pi = PendingIntent.getActivity(applicationContext, STATUS_REQ_CODE, intent, PendingIntent.FLAG_IMMUTABLE)
val deleteIntent = Intent(this, BaresipService::class.java).setAction("Notification Dismissed")
val dpi = PendingIntent.getService(this, STATUS_REQ_CODE, deleteIntent, PendingIntent.FLAG_IMMUTABLE)
val pi = PendingIntent.getActivity(
applicationContext,
STATUS_REQ_CODE,
intent,
PendingIntent.FLAG_IMMUTABLE
)
val deleteIntent =
Intent(this, BaresipService::class.java).setAction("Notification Dismissed")
val dpi = PendingIntent.getService(
this,
STATUS_REQ_CODE,
deleteIntent,
PendingIntent.FLAG_IMMUTABLE
)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_notification_b)
@ -1592,8 +1607,10 @@ class BaresipService: Service() {
val hangupIntent = Intent(this, BaresipService::class.java)
hangupIntent.action = "Call Hangup"
hangupIntent.putExtra("callp", activeCall.callp)
val hpi = PendingIntent.getService(this, REJECT_REQ_CODE, hangupIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
val hpi = PendingIntent.getService(
this, REJECT_REQ_CODE, hangupIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
builder.setStyle(NotificationCompat.CallStyle.forOngoingCall(person, hpi))
.setCategory(Notification.CATEGORY_CALL)
@ -1649,28 +1666,36 @@ class BaresipService: Service() {
builder.setOngoing(true)
val notification = builder.build()
notification.flags = notification.flags or Notification.FLAG_NO_CLEAR or Notification.FLAG_ONGOING_EVENT
notification.flags =
notification.flags or Notification.FLAG_NO_CLEAR or Notification.FLAG_ONGOING_EVENT
try {
if (activeCall != null) {
if (VERSION.SDK_INT >= 29)
startForeground(STATUS_NOTIFICATION_ID, notification, foregroundServiceType(activeCall))
startForeground(
STATUS_NOTIFICATION_ID,
notification,
foregroundServiceType(activeCall)
)
else
startForeground(STATUS_NOTIFICATION_ID, notification)
isNotificationInCall = true
} else {
// If we are currently in a call notification mode, downgrade to standby FGS
// but DO NOT call stopForeground as that would drop FGS status.
// If we are currently in a call notification mode, downgrade to standby FGS.
// We call stopForeground(REMOVE) to explicitly clear the system's telephony UI context
// that might be persisting the timer/peer info on the lock screen.
if (isNotificationInCall) {
stopForeground(STOP_FOREGROUND_REMOVE)
isNotificationInCall = false
}
if (VERSION.SDK_INT >= 29)
startForeground(STATUS_NOTIFICATION_ID, notification, foregroundServiceType())
startForeground(
STATUS_NOTIFICATION_ID,
notification,
foregroundServiceType()
)
else
startForeground(STATUS_NOTIFICATION_ID, notification)
isNotificationInCall = false
} else {
// Already in standby, just update the notification content
nm.notify(STATUS_NOTIFICATION_ID, notification)
}
}
} catch (e: Exception) {
Log.e(TAG, "Failed to update foreground notification: ${e.message}")
@ -1678,6 +1703,7 @@ class BaresipService: Service() {
nm.notify(STATUS_NOTIFICATION_ID, notification)
}
}
}
private fun foregroundServiceType(activeCall: Call? = null): Int {
var type = 0
@ -1841,6 +1867,7 @@ class BaresipService: Service() {
proximitySensing(false)
stopMediaPlayer()
}
updateStatusNotification()
messageUpdate.postValue(System.currentTimeMillis())
}

View File

@ -48,12 +48,16 @@ open class Call(val callp: Long, val ua: UserAgent, val peerUri: String, val dir
val focusDtmf: MutableState<Boolean> = mutableStateOf(false)
fun add() {
synchronized(BaresipService.calls) {
BaresipService.calls.add(this)
}
}
fun remove() {
synchronized(BaresipService.calls) {
BaresipService.calls.remove(this)
}
}
open fun connect(uri: String): Boolean {
return Api.call_connect(callp, uri) == 0

View File

@ -195,6 +195,7 @@ class ConnectionService : ConnectionService() {
if (call != null)
Api.ua_hangup(uap, callp, 0, "")
setDisconnected(DisconnectCause(DisconnectCause.LOCAL))
connections.remove(callp)
destroy()
// Allow other disconnects after a short period to prevent the "Telecom Cascade" effect
android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({