Skip to content

Commit bd9a187

Browse files
author
ganfra
committed
Fix sticky end call notification #4019
1 parent d6b261c commit bd9a187

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

changelog.d/4019.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix sticky end call notification

vector/src/main/java/im/vector/app/core/services/CallService.kt

+16-13
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private val loggerTag = LoggerTag("CallService", LoggerTag.VOIP)
5050
class CallService : VectorService() {
5151

5252
private val connections = mutableMapOf<String, CallConnection>()
53-
private val knownCalls = mutableSetOf<CallInformation>()
53+
private val knownCalls = mutableMapOf<String, CallInformation>()
5454
private val connectedCallIds = mutableSetOf<String>()
5555

5656
private lateinit var notificationManager: NotificationManagerCompat
@@ -190,28 +190,30 @@ class CallService : VectorService() {
190190
} else {
191191
notificationManager.notify(callId.hashCode(), notification)
192192
}
193-
knownCalls.add(callInformation)
193+
knownCalls[callId] = callInformation
194194
}
195195

196196
private fun handleCallTerminated(intent: Intent) {
197197
val callId = intent.getStringExtra(EXTRA_CALL_ID) ?: ""
198198
val endCallReason = intent.getSerializableExtra(EXTRA_END_CALL_REASON) as EndCallReason
199199
val rejected = intent.getBooleanExtra(EXTRA_END_CALL_REJECTED, false)
200200
alertManager.cancelAlert(callId)
201-
val terminatedCall = knownCalls.firstOrNull { it.callId == callId }
201+
val terminatedCall = knownCalls.remove(callId)
202202
if (terminatedCall == null) {
203-
Timber.tag(loggerTag.value).v("Call terminated for unknown call $callId$")
203+
Timber.tag(loggerTag.value).v("Call terminated for unknown call $callId")
204204
handleUnexpectedState(callId)
205205
return
206206
}
207-
knownCalls.remove(terminatedCall)
207+
val notification = notificationUtils.buildCallEndedNotification(false)
208+
val notificationId = callId.hashCode()
209+
startForeground(notificationId, notification)
208210
if (knownCalls.isEmpty()) {
211+
Timber.tag(loggerTag.value).v("No more call, stop the service")
212+
stopForeground(true)
209213
mediaSession?.isActive = false
210214
myStopSelf()
211215
}
212216
val wasConnected = connectedCallIds.remove(callId)
213-
val notification = notificationUtils.buildCallEndedNotification(terminatedCall.isVideoCall)
214-
notificationManager.notify(callId.hashCode(), notification)
215217
if (!wasConnected && !terminatedCall.isOutgoing && !rejected && endCallReason != EndCallReason.ANSWERED_ELSEWHERE) {
216218
val missedCallNotification = notificationUtils.buildCallMissedNotification(terminatedCall)
217219
notificationManager.notify(MISSED_CALL_TAG, terminatedCall.nativeRoomId.hashCode(), missedCallNotification)
@@ -243,7 +245,7 @@ class CallService : VectorService() {
243245
} else {
244246
notificationManager.notify(callId.hashCode(), notification)
245247
}
246-
knownCalls.add(callInformation)
248+
knownCalls[callId] = callInformation
247249
}
248250

249251
/**
@@ -267,18 +269,19 @@ class CallService : VectorService() {
267269
} else {
268270
notificationManager.notify(callId.hashCode(), notification)
269271
}
270-
knownCalls.add(callInformation)
272+
knownCalls[callId] = callInformation
271273
}
272274

273275
private fun handleUnexpectedState(callId: String?) {
274276
Timber.tag(loggerTag.value).v("Fallback to clear everything")
275277
callRingPlayerIncoming?.stop()
276278
callRingPlayerOutgoing?.stop()
279+
val notification = notificationUtils.buildCallEndedNotification(false)
277280
if (callId != null) {
278-
notificationManager.cancel(callId.hashCode())
281+
startForeground(callId.hashCode(), notification)
282+
} else {
283+
startForeground(DEFAULT_NOTIFICATION_ID, notification)
279284
}
280-
val notification = notificationUtils.buildCallEndedNotification(false)
281-
startForeground(DEFAULT_NOTIFICATION_ID, notification)
282285
if (knownCalls.isEmpty()) {
283286
mediaSession?.isActive = false
284287
myStopSelf()
@@ -371,7 +374,7 @@ class CallService : VectorService() {
371374
putExtra(EXTRA_END_CALL_REASON, endCallReason)
372375
putExtra(EXTRA_END_CALL_REJECTED, rejected)
373376
}
374-
ContextCompat.startForegroundService(context, intent)
377+
context.startService(intent)
375378
}
376379
}
377380

vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt

-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@ class NotificationUtils @Inject constructor(private val context: Context,
468468
setSmallIcon(R.drawable.ic_call_answer)
469469
}
470470
}
471-
// This is a trick to make the previous notification with same id disappear as cancel notification is not working with Foreground Service.
472471
.setTimeoutAfter(1)
473472
.setColor(ThemeUtils.getColor(context, android.R.attr.colorPrimary))
474473
.setCategory(NotificationCompat.CATEGORY_CALL)

0 commit comments

Comments
 (0)