From 92fe70c15cfe38053f785a57e9ab6aa492aefd83 Mon Sep 17 00:00:00 2001
From: ganfra <francoisg@matrix.org>
Date: Wed, 9 Dec 2020 16:28:10 +0100
Subject: [PATCH] VoIP: show in-notif only when necessary

---
 .../java/im/vector/app/core/services/CallService.kt | 13 ++++++++++++-
 .../vector/app/features/popup/IncomingCallAlert.kt  |  1 +
 .../vector/app/features/popup/PopupAlertManager.kt  | 10 ++++++----
 .../im/vector/app/features/popup/VectorAlert.kt     |  3 +++
 4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/vector/src/main/java/im/vector/app/core/services/CallService.kt b/vector/src/main/java/im/vector/app/core/services/CallService.kt
index 9fb353736a..05875f3cf5 100644
--- a/vector/src/main/java/im/vector/app/core/services/CallService.kt
+++ b/vector/src/main/java/im/vector/app/core/services/CallService.kt
@@ -24,12 +24,15 @@ import android.support.v4.media.session.MediaSessionCompat
 import android.view.KeyEvent
 import androidx.core.content.ContextCompat
 import androidx.media.session.MediaButtonReceiver
+import com.airbnb.mvrx.MvRx
 import im.vector.app.core.extensions.vectorComponent
+import im.vector.app.features.call.CallArgs
 import im.vector.app.features.call.VectorCallActivity
 import im.vector.app.features.call.telecom.CallConnection
 import im.vector.app.features.call.webrtc.WebRtcCall
 import im.vector.app.features.call.webrtc.WebRtcCallManager
 import im.vector.app.features.home.AvatarRenderer
+import im.vector.app.features.home.room.detail.RoomDetailActivity
 import im.vector.app.features.notifications.NotificationUtils
 import im.vector.app.features.popup.IncomingCallAlert
 import im.vector.app.features.popup.PopupAlertManager
@@ -169,7 +172,15 @@ class CallService : VectorService(), WiredHeadsetStateReceiver.HeadsetEventListe
         Timber.v("displayIncomingCallNotification : display the dedicated notification")
         if (!fromBg) {
             // Show in-app notification if app is in foreground.
-            val incomingCallAlert = IncomingCallAlert(INCOMING_CALL_ALERT_UID).apply {
+            val incomingCallAlert = IncomingCallAlert(INCOMING_CALL_ALERT_UID,
+                    shouldBeDisplayedIn = { activity ->
+                        if (activity is RoomDetailActivity) {
+                            call.roomId != activity.currentRoomId
+                        } else if(activity is VectorCallActivity) {
+                            activity.intent.getParcelableExtra<CallArgs>(MvRx.KEY_ARG)?.callId != call.callId
+                        } else true
+                    }
+            ).apply {
                 viewBinder = IncomingCallAlert.ViewBinder(
                         matrixItem = opponentMatrixItem,
                         avatarRenderer = avatarRenderer,
diff --git a/vector/src/main/java/im/vector/app/features/popup/IncomingCallAlert.kt b/vector/src/main/java/im/vector/app/features/popup/IncomingCallAlert.kt
index 73f9f32ca4..ec823bc21f 100644
--- a/vector/src/main/java/im/vector/app/features/popup/IncomingCallAlert.kt
+++ b/vector/src/main/java/im/vector/app/features/popup/IncomingCallAlert.kt
@@ -32,6 +32,7 @@ class IncomingCallAlert(uid: String,
     override val priority = PopupAlertManager.INCOMING_CALL_PRIORITY
     override val layoutRes = R.layout.alerter_incoming_call_layout
     override var colorAttribute: Int? = R.attr.riotx_alerter_background
+    override val dismissOnClick: Boolean = false
 
     class ViewBinder(private val matrixItem: MatrixItem?,
                      private val avatarRenderer: AvatarRenderer,
diff --git a/vector/src/main/java/im/vector/app/features/popup/PopupAlertManager.kt b/vector/src/main/java/im/vector/app/features/popup/PopupAlertManager.kt
index 0b7bf2e2c8..db95507436 100644
--- a/vector/src/main/java/im/vector/app/features/popup/PopupAlertManager.kt
+++ b/vector/src/main/java/im/vector/app/features/popup/PopupAlertManager.kt
@@ -230,17 +230,19 @@ class PopupAlertManager @Inject constructor() {
                             }
                         })
                     }
-                    setOnClickListener(View.OnClickListener { _ ->
+                    setOnClickListener { _ ->
                         alert.contentAction?.let {
-                            currentIsDismissed()
-                            Alerter.hide()
+                            if (alert.dismissOnClick) {
+                                currentIsDismissed()
+                                Alerter.hide()
+                            }
                             try {
                                 it.run()
                             } catch (e: java.lang.Exception) {
                                 Timber.e("## failed to perform action")
                             }
                         }
-                    })
+                    }
                 }
                 .setOnHideListener(OnHideAlertListener {
                     // called when dismissed on swipe
diff --git a/vector/src/main/java/im/vector/app/features/popup/VectorAlert.kt b/vector/src/main/java/im/vector/app/features/popup/VectorAlert.kt
index d21755091e..4394ef42d2 100644
--- a/vector/src/main/java/im/vector/app/features/popup/VectorAlert.kt
+++ b/vector/src/main/java/im/vector/app/features/popup/VectorAlert.kt
@@ -35,6 +35,7 @@ interface VectorAlert {
     val description: String
     val iconId: Int?
     val priority: Int
+    val dismissOnClick: Boolean
     val shouldBeDisplayedIn: ((Activity) -> Boolean)
 
     data class Button(val title: String, val action: Runnable, val autoClose: Boolean)
@@ -99,6 +100,8 @@ open class DefaultVectorAlert(
     @LayoutRes
     override val layoutRes = R.layout.alerter_alert_default_layout
 
+    override val dismissOnClick: Boolean = true
+
     override val priority: Int = 0
 
     @ColorRes