From bc511c1bc1bcc921a27e877d610d3d113a08add5 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 23 Mar 2021 15:32:39 +0100 Subject: [PATCH] Simplified remote SIP address search for notification's action --- .../NotificationBroadcastReceiver.kt | 22 ++++---- .../notifications/NotificationsManager.kt | 53 ++++++++----------- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.kt b/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.kt index a8b9b5fe2..f0161a4c3 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.kt @@ -35,16 +35,14 @@ class NotificationBroadcastReceiver : BroadcastReceiver() { if (intent.action == NotificationsManager.INTENT_REPLY_NOTIF_ACTION || intent.action == NotificationsManager.INTENT_MARK_AS_READ_ACTION) { handleChatIntent(intent, notificationId) } else if (intent.action == NotificationsManager.INTENT_ANSWER_CALL_NOTIF_ACTION || intent.action == NotificationsManager.INTENT_HANGUP_CALL_NOTIF_ACTION) { - handleCallIntent(intent, notificationId) + handleCallIntent(intent) } } private fun handleChatIntent(intent: Intent, notificationId: Int) { - val localIdentity = intent.getStringExtra(NotificationsManager.INTENT_LOCAL_IDENTITY) - - val remoteSipAddress: String? = coreContext.notificationsManager.getSipUriForChatNotificationId(notificationId) + val remoteSipAddress = intent.getStringExtra(NotificationsManager.INTENT_REMOTE_ADDRESS) if (remoteSipAddress == null) { - Log.e("[Notification Broadcast Receiver] Couldn't find remote address $remoteSipAddress for notification id $notificationId") + Log.e("[Notification Broadcast Receiver] Remote SIP address is null for notification id $notificationId") return } val core: Core = coreContext.core @@ -55,6 +53,7 @@ class NotificationBroadcastReceiver : BroadcastReceiver() { return } + val localIdentity = intent.getStringExtra(NotificationsManager.INTENT_LOCAL_IDENTITY) if (localIdentity == null) { Log.e("[Notification Broadcast Receiver] Local identity is null for notification id $notificationId") return @@ -89,14 +88,19 @@ class NotificationBroadcastReceiver : BroadcastReceiver() { } } - private fun handleCallIntent(intent: Intent, notificationId: Int) { - val remote: String = coreContext.notificationsManager.getSipUriForCallNotificationId(notificationId) ?: "" + private fun handleCallIntent(intent: Intent) { + val remoteSipAddress = intent.getStringExtra(NotificationsManager.INTENT_REMOTE_ADDRESS) + if (remoteSipAddress == null) { + Log.e("[Notification Broadcast Receiver] Remote SIP address is null for notification") + return + } + val core: Core = coreContext.core - val remoteAddress = core.interpretUrl(remote) + val remoteAddress = core.interpretUrl(remoteSipAddress) val call = if (remoteAddress != null) core.getCallByRemoteAddress2(remoteAddress) else null if (call == null) { - Log.e("[Notification Broadcast Receiver] Couldn't find call from remote address $remoteAddress") + Log.e("[Notification Broadcast Receiver] Couldn't find call from remote address $remoteSipAddress") return } diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt index 22a3eff23..eb981cdb1 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt @@ -60,6 +60,7 @@ private class Notifiable(val notificationId: Int) { var groupTitle: String? = null var localIdentity: String? = null var myself: String? = null + var remoteAddress: String? = null } private class NotifiableMessage( @@ -81,8 +82,9 @@ class NotificationsManager(private val context: Context) { const val INTENT_REPLY_NOTIF_ACTION = "org.linphone.REPLY_ACTION" const val INTENT_HANGUP_CALL_NOTIF_ACTION = "org.linphone.HANGUP_CALL_ACTION" const val INTENT_ANSWER_CALL_NOTIF_ACTION = "org.linphone.ANSWER_CALL_ACTION" - const val INTENT_LOCAL_IDENTITY = "LOCAL_IDENTITY" const val INTENT_MARK_AS_READ_ACTION = "org.linphone.MARK_AS_READ_ACTION" + const val INTENT_LOCAL_IDENTITY = "LOCAL_IDENTITY" + const val INTENT_REMOTE_ADDRESS = "REMOTE_ADDRESS" private const val SERVICE_NOTIF_ID = 1 private const val MISSED_CALLS_NOTIF_ID = 2 @@ -230,24 +232,6 @@ class NotificationsManager(private val context: Context) { notificationManager.cancel(id) } - fun getSipUriForChatNotificationId(notificationId: Int): String? { - for (address in chatNotificationsMap.keys) { - if (chatNotificationsMap[address]?.notificationId == notificationId) { - return address - } - } - return null - } - - fun getSipUriForCallNotificationId(notificationId: Int): String? { - for (address in callNotificationsMap.keys) { - if (callNotificationsMap[address]?.notificationId == notificationId) { - return address - } - } - return null - } - fun cancelChatNotificationIdForSipUri(sipUri: String) { val notifiable: Notifiable? = chatNotificationsMap[sipUri] if (notifiable != null) { @@ -363,6 +347,8 @@ class NotificationsManager(private val context: Context) { var notifiable: Notifiable? = callNotificationsMap[address] if (notifiable == null) { notifiable = Notifiable(lastNotificationId) + notifiable.remoteAddress = call.remoteAddress.asStringUriOnly() + lastNotificationId += 1 callNotificationsMap[address] = notifiable } @@ -410,8 +396,8 @@ class NotificationsManager(private val context: Context) { .setOngoing(true) .setColor(ContextCompat.getColor(context, R.color.primary_color)) .setFullScreenIntent(pendingIntent, true) - .addAction(getCallDeclineAction(notifiable.notificationId)) - .addAction(getCallAnswerAction(notifiable.notificationId)) + .addAction(getCallDeclineAction(notifiable)) + .addAction(getCallAnswerAction(notifiable)) .setCustomHeadsUpContentView(notificationLayoutHeadsUp) if (!corePreferences.preventInterfaceFromShowingUp) { @@ -530,7 +516,7 @@ class NotificationsManager(private val context: Context) { .setShowWhen(true) .setOngoing(true) .setColor(ContextCompat.getColor(context, R.color.notification_led_color)) - .addAction(getCallDeclineAction(notifiable.notificationId)) + .addAction(getCallDeclineAction(notifiable)) if (!corePreferences.preventInterfaceFromShowingUp) { builder.setContentIntent(pendingIntent) @@ -623,9 +609,6 @@ class NotificationsManager(private val context: Context) { notifiable.groupTitle = room.subject } - notifiable.myself = LinphoneUtils.getDisplayName(room.localAddress) - notifiable.localIdentity = room.localAddress.asStringUriOnly() - displayChatNotifiable(room, notifiable) } @@ -634,6 +617,10 @@ class NotificationsManager(private val context: Context) { var notifiable: Notifiable? = chatNotificationsMap[address] if (notifiable == null) { notifiable = Notifiable(lastNotificationId) + notifiable.myself = LinphoneUtils.getDisplayName(room.localAddress) + notifiable.localIdentity = room.localAddress.asStringUriOnly() + notifiable.remoteAddress = room.peerAddress.asStringUriOnly() + lastNotificationId += 1 chatNotificationsMap[address] = notifiable } @@ -758,13 +745,14 @@ class NotificationsManager(private val context: Context) { /* Notifications actions */ - private fun getCallAnswerAction(callId: Int): NotificationCompat.Action { + private fun getCallAnswerAction(notifiable: Notifiable): NotificationCompat.Action { val answerIntent = Intent(context, NotificationBroadcastReceiver::class.java) answerIntent.action = INTENT_ANSWER_CALL_NOTIF_ACTION - answerIntent.putExtra(INTENT_NOTIF_ID, callId) + answerIntent.putExtra(INTENT_NOTIF_ID, notifiable.notificationId) + answerIntent.putExtra(INTENT_REMOTE_ADDRESS, notifiable.remoteAddress) val answerPendingIntent = PendingIntent.getBroadcast( - context, callId, answerIntent, PendingIntent.FLAG_UPDATE_CURRENT + context, notifiable.notificationId, answerIntent, PendingIntent.FLAG_UPDATE_CURRENT ) return NotificationCompat.Action.Builder( @@ -774,13 +762,14 @@ class NotificationsManager(private val context: Context) { ).build() } - private fun getCallDeclineAction(callId: Int): NotificationCompat.Action { + private fun getCallDeclineAction(notifiable: Notifiable): NotificationCompat.Action { val hangupIntent = Intent(context, NotificationBroadcastReceiver::class.java) hangupIntent.action = INTENT_HANGUP_CALL_NOTIF_ACTION - hangupIntent.putExtra(INTENT_NOTIF_ID, callId) + hangupIntent.putExtra(INTENT_NOTIF_ID, notifiable.notificationId) + hangupIntent.putExtra(INTENT_REMOTE_ADDRESS, notifiable.remoteAddress) val hangupPendingIntent = PendingIntent.getBroadcast( - context, callId, hangupIntent, PendingIntent.FLAG_UPDATE_CURRENT + context, notifiable.notificationId, hangupIntent, PendingIntent.FLAG_UPDATE_CURRENT ) return NotificationCompat.Action.Builder( @@ -800,6 +789,7 @@ class NotificationsManager(private val context: Context) { replyIntent.action = INTENT_REPLY_NOTIF_ACTION replyIntent.putExtra(INTENT_NOTIF_ID, notifiable.notificationId) replyIntent.putExtra(INTENT_LOCAL_IDENTITY, notifiable.localIdentity) + replyIntent.putExtra(INTENT_REMOTE_ADDRESS, notifiable.remoteAddress) val replyPendingIntent = PendingIntent.getBroadcast( context, @@ -823,6 +813,7 @@ class NotificationsManager(private val context: Context) { markAsReadIntent.action = INTENT_MARK_AS_READ_ACTION markAsReadIntent.putExtra(INTENT_NOTIF_ID, notifiable.notificationId) markAsReadIntent.putExtra(INTENT_LOCAL_IDENTITY, notifiable.localIdentity) + markAsReadIntent.putExtra(INTENT_REMOTE_ADDRESS, notifiable.remoteAddress) return PendingIntent.getBroadcast( context,