From fd4b04f2d822e60f3c61979621a9b8b6c9590fe5 Mon Sep 17 00:00:00 2001
From: Pierre Nicolas <pierre.nicolas@savoirfairelinux.com>
Date: Tue, 28 Nov 2023 16:56:56 -0500
Subject: [PATCH] notification: propage activecalls to notifications

Make swarm:group notification disapear when the call is finished.
GitLab: #1464

Change-Id: I1415be6abfc223b43d03de13d7c4ae1d9ab97a09
---
 .../ring/services/NotificationServiceImpl.kt  | 16 +++++++++----
 .../src/main/kotlin/net/jami/model/Account.kt |  4 ++--
 .../net/jami/services/AccountService.kt       | 24 ++++++++++++++++++-
 .../net/jami/services/ConversationFacade.kt   | 19 +++++++++++----
 .../net/jami/services/NotificationService.kt  |  2 +-
 5 files changed, 52 insertions(+), 13 deletions(-)

diff --git a/jami-android/app/src/main/java/cx/ring/services/NotificationServiceImpl.kt b/jami-android/app/src/main/java/cx/ring/services/NotificationServiceImpl.kt
index 0994717dd..d33bde1f9 100644
--- a/jami-android/app/src/main/java/cx/ring/services/NotificationServiceImpl.kt
+++ b/jami-android/app/src/main/java/cx/ring/services/NotificationServiceImpl.kt
@@ -487,22 +487,30 @@ class NotificationServiceImpl(
     /**
      * Function to show a group call notification.
      */
-    override fun showGroupCallNotification(conversation: Conversation) {
+    override fun showGroupCallNotification(conversation: Conversation, remove: Boolean) {
         // Call the showGroupCallNotification function with the loaded conversation.
         mContactService.getLoadedConversation(conversation)
-            .subscribe({ cvm -> showGroupCallNotification(cvm) })
+            .subscribe({ cvm -> showGroupCallNotification(cvm, remove) })
             { e: Throwable -> Log.w(TAG, "Can't load contact", e) }
     }
 
     /**
      * Function to show a group call notification.
      */
-    private fun showGroupCallNotification(cvm: ConversationItemViewModel) {
+    private fun showGroupCallNotification(cvm: ConversationItemViewModel, remove: Boolean) {
         // Obtain the conversation path and key
         val cpath = ConversationPath(cvm.accountId, cvm.uri)
         val path = cpath.toUri()
         val key = cpath.toKey()
 
+        // Generate a unique notification ID
+        val notificationId = getTextNotificationId(cpath.accountId, cvm.uri)
+
+        if (remove) { // Remove the notification if the call is no longer in progress.
+            CarNotificationManager.from(mContext).cancel(notificationId)
+            return
+        }
+
         // Get the conversation profile
         val conversationProfile = getProfile(cvm)
 
@@ -535,8 +543,6 @@ class NotificationServiceImpl(
             .setColor(ResourcesCompat.getColor(mContext.resources, R.color.color_primary_dark, null))
             .setLargeIcon(conversationProfile.first)
 
-        // Generate a unique notification ID
-        val notificationId = getTextNotificationId(cpath.accountId, cvm.uri)
         // Notify the notification
         CarNotificationManager.from(mContext).notify(notificationId, messageNotificationBuilder)
         // Save the notification builder for future reference
diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt
index 765a1eb3e..071c9a086 100644
--- a/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt
+++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt
@@ -912,8 +912,8 @@ class Account(
         mDataTransfers[fileId] = transfer
     }
 
-    fun setActiveCalls(conversationId: String, activeCalls: List<Map<String, String>>) =
-        getSwarm(conversationId)?.setActiveCalls(activeCalls.map { Conversation.ActiveCall(it) })
+    fun setActiveCalls(conversationId: String, activeCalls: List<Conversation.ActiveCall>) =
+        getSwarm(conversationId)?.setActiveCalls(activeCalls)
 
     private fun updateUnreadConversations() {
         var unread = 0
diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/services/AccountService.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/services/AccountService.kt
index 687f3c379..55e925d0d 100644
--- a/jami-android/libjamiclient/src/main/kotlin/net/jami/services/AccountService.kt
+++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/services/AccountService.kt
@@ -84,6 +84,18 @@ class AccountService(
     private var mHasRingAccount = false
     private val accountsSubject = BehaviorSubject.create<List<Account>>()
     private val observableAccounts: Subject<Account> = PublishSubject.create()
+
+    private val activeCallsSubject: Subject<ConversationActiveCalls> =
+        PublishSubject.create()
+    val activeCallsObservable: Observable<ConversationActiveCalls> =
+        activeCallsSubject
+
+    data class ConversationActiveCalls(
+        val accountId: String,
+        val conversationId: String,
+        val activeCalls: List<Conversation.ActiveCall>
+    )
+
     val currentAccountSubject: Observable<Account> = accountsSubject
         .filter { l -> l.isNotEmpty() }
         .map { l -> l[0] }
@@ -1042,7 +1054,17 @@ class AccountService(
         accountId: String,
         conversationId: String,
         activeCalls: List<Map<String, String>>,
-    ) = getAccount(accountId)?.setActiveCalls(conversationId, activeCalls)
+    ) {
+        val activeCallList = activeCalls.map { Conversation.ActiveCall(it) }
+        activeCallsSubject.onNext(
+            ConversationActiveCalls(
+                accountId,
+                conversationId,
+                activeCallList
+            )
+        )
+        getAccount(accountId)?.setActiveCalls(conversationId, activeCallList)
+    }
 
     fun accountProfileReceived(accountId: String, name: String?, photo: String?) {
         val account = getAccount(accountId) ?: return
diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/services/ConversationFacade.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/services/ConversationFacade.kt
index 63ebac8eb..8c1c4918f 100644
--- a/jami-android/libjamiclient/src/main/kotlin/net/jami/services/ConversationFacade.kt
+++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/services/ConversationFacade.kt
@@ -771,10 +771,21 @@ class ConversationFacade(
         mDisposableBag.add(mAccountService.dataTransfers
                 .subscribe({ transfer: DataTransfer -> handleDataTransferEvent(transfer) },
                      { e: Throwable -> Log.e(TAG, "Error adding data transfer", e) }))
-
-        mDisposableBag.add(mAccountService.incomingGroupCall
-            .subscribe({ c -> mNotificationService.showGroupCallNotification(c) },
-                { e: Throwable -> Log.e(TAG, "Error showing group call notification", e) }))
+        mDisposableBag.add(
+            mAccountService.activeCallsObservable.subscribe(
+                { conversationActiveCall ->
+                    mAccountService.getAccount(accountId = conversationActiveCall.accountId)
+                        ?.getByUri(uri = conversationActiveCall.conversationId)
+                        ?.let {
+                            mNotificationService.showGroupCallNotification(
+                                conversation = it,
+                                remove = conversationActiveCall.activeCalls.isEmpty()
+                            )
+                        }
+                },
+                { e: Throwable -> Log.e(TAG, "Error showing group call notification", e) }
+            )
+        )
     }
 
 }
\ No newline at end of file
diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/services/NotificationService.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/services/NotificationService.kt
index 22a2fc20d..41b84c44e 100644
--- a/jami-android/libjamiclient/src/main/kotlin/net/jami/services/NotificationService.kt
+++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/services/NotificationService.kt
@@ -26,7 +26,7 @@ interface NotificationService {
     fun preparePendingScreenshare(conference: Conference, callback: () -> Unit)
     fun startPendingScreenshare(confId: String)
     fun showMissedCallNotification(call: Call)
-    fun showGroupCallNotification(conversation: Conversation)
+    fun showGroupCallNotification(conversation: Conversation, remove: Boolean = false)
     fun showTextNotification(conversation: Conversation)
     fun cancelTextNotification(accountId: String, contact: Uri)
     fun cancelAll()
-- 
GitLab