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