add check that missed call notification is not shown accidentally

for example when call is hangup on mobile and immediately after on web, the loop in "checkIfCallIsActive" is still active and might trigger to send the "missed call" notification. Because of this, there is now another check if the "ongoing call" notification is still visible. It makes only sense to show the missed call notification, when the ongoing call notification is still visible.

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-11-14 15:32:43 +01:00 committed by Tim Krüger
parent 9c4b0a00c6
commit e121d32984
No known key found for this signature in database
GPG Key ID: FECE3A7222C52A4E

View File

@ -256,7 +256,7 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
sendNotification(pushMessage.timestamp.toInt(), notification) sendNotification(pushMessage.timestamp.toInt(), notification)
checkIfCallIsActive(signatureVerification, pushMessage) checkIfCallIsActive(signatureVerification)
} }
private fun initNcApiAndCredentials() { private fun initNcApiAndCredentials() {
@ -661,10 +661,7 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
notificationManager.cancel(notificationId) notificationManager.cancel(notificationId)
} }
private fun checkIfCallIsActive( private fun checkIfCallIsActive(signatureVerification: SignatureVerification) {
signatureVerification: SignatureVerification,
decryptedPushMessage: DecryptedPushMessage
) {
Log.d(TAG, "checkIfCallIsActive") Log.d(TAG, "checkIfCallIsActive")
var hasParticipantsInCall = true var hasParticipantsInCall = true
var inCallOnDifferentDevice = false var inCallOnDifferentDevice = false
@ -681,7 +678,7 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
ApiUtils.getUrlForCall( ApiUtils.getUrlForCall(
apiVersion, apiVersion,
signatureVerification.user!!.baseUrl, signatureVerification.user!!.baseUrl,
decryptedPushMessage.id pushMessage.id
) )
) )
.repeatWhen { completed -> .repeatWhen { completed ->
@ -708,18 +705,18 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
} }
if (inCallOnDifferentDevice) { if (inCallOnDifferentDevice) {
Log.d(TAG, "inCallOnDifferentDevice is true") Log.d(TAG, "inCallOnDifferentDevice is true")
removeNotification(decryptedPushMessage.timestamp.toInt()) removeNotification(pushMessage.timestamp.toInt())
} }
if (!hasParticipantsInCall) { if (!hasParticipantsInCall) {
showMissedCallNotification() showMissedCallNotification()
Log.d(TAG, "no participants in call") Log.d(TAG, "no participants in call")
removeNotification(decryptedPushMessage.timestamp.toInt()) removeNotification(pushMessage.timestamp.toInt())
} }
isCallNotificationVisible = NotificationUtils.isNotificationVisible( isCallNotificationVisible = NotificationUtils.isNotificationVisible(
context, context,
decryptedPushMessage.timestamp.toInt() pushMessage.timestamp.toInt()
) )
} }
@ -734,71 +731,78 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
showMissedCallNotification() showMissedCallNotification()
} }
removeNotification(decryptedPushMessage.timestamp.toInt()) removeNotification(pushMessage.timestamp.toInt())
} }
}) })
} }
fun showMissedCallNotification() { fun showMissedCallNotification() {
val apiVersion = ApiUtils.getConversationApiVersion( val isOngoingCallNotificationVisible = NotificationUtils.isNotificationVisible(
signatureVerification.user, context,
intArrayOf( pushMessage.timestamp.toInt()
ApiUtils.APIv4,
ApiUtils.APIv3, 1
)
) )
ncApi.getRoom(
credentials, if (isOngoingCallNotificationVisible) {
ApiUtils.getUrlForRoom( val apiVersion = ApiUtils.getConversationApiVersion(
apiVersion, signatureVerification.user?.baseUrl, signatureVerification.user,
pushMessage.id intArrayOf(
ApiUtils.APIv4,
ApiUtils.APIv3, 1
)
) )
) ncApi.getRoom(
.subscribeOn(Schedulers.io()) credentials,
.retry(GET_ROOM_RETRY_COUNT) ApiUtils.getUrlForRoom(
.observeOn(AndroidSchedulers.mainThread()) apiVersion, signatureVerification.user?.baseUrl,
.subscribe(object : Observer<RoomOverall> { pushMessage.id
override fun onSubscribe(d: Disposable) { )
// unused atm )
} .subscribeOn(Schedulers.io())
.retry(GET_ROOM_RETRY_COUNT)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Observer<RoomOverall> {
override fun onSubscribe(d: Disposable) {
// unused atm
}
override fun onNext(roomOverall: RoomOverall) { override fun onNext(roomOverall: RoomOverall) {
val currentConversation = roomOverall.ocs!!.data val currentConversation = roomOverall.ocs!!.data
val notificationBuilder: NotificationCompat.Builder? val notificationBuilder: NotificationCompat.Builder?
notificationBuilder = NotificationCompat.Builder( notificationBuilder = NotificationCompat.Builder(
context!!, context!!,
NotificationUtils.NotificationChannels NotificationUtils.NotificationChannels
.NOTIFICATION_CHANNEL_MESSAGES_V4.name .NOTIFICATION_CHANNEL_MESSAGES_V4.name
)
val notification: Notification = notificationBuilder
.setContentTitle(
String.format(
context!!.resources.getString(R.string.nc_missed_call),
currentConversation!!.displayName
)
) )
.setSmallIcon(R.drawable.ic_baseline_phone_missed_24)
.setOngoing(false)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setContentIntent(getIntentToOpenConversation())
.build()
val notificationId: Int = SystemClock.uptimeMillis().toInt() val notification: Notification = notificationBuilder
notificationManager.notify(notificationId, notification) .setContentTitle(
Log.d(TAG, "'you missed a call' notification was created") String.format(
} context!!.resources.getString(R.string.nc_missed_call),
currentConversation!!.displayName
)
)
.setSmallIcon(R.drawable.ic_baseline_phone_missed_24)
.setOngoing(false)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setContentIntent(getIntentToOpenConversation())
.build()
override fun onError(e: Throwable) { val notificationId: Int = SystemClock.uptimeMillis().toInt()
Log.e(TAG, "An error occurred while fetching room for the 'missed call' notification", e) notificationManager.notify(notificationId, notification)
} Log.d(TAG, "'you missed a call' notification was created")
}
override fun onComplete() { override fun onError(e: Throwable) {
// unused atm Log.e(TAG, "An error occurred while fetching room for the 'missed call' notification", e)
} }
})
override fun onComplete() {
// unused atm
}
})
}
} }
private fun getIntentToOpenConversation(): PendingIntent? { private fun getIntentToOpenConversation(): PendingIntent? {