From 8c790929c4b1377b338e5736674d18365be1c1d0 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 5 Jan 2022 12:17:31 +0100 Subject: [PATCH] Fixed chat bubbles sometimes closing when opened --- .../chat_bubble/ChatBubbleActivity.kt | 10 +++-- .../notifications/NotificationsManager.kt | 40 ++++++++++++------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/linphone/activities/chat_bubble/ChatBubbleActivity.kt b/app/src/main/java/org/linphone/activities/chat_bubble/ChatBubbleActivity.kt index 714773897..a9405ef01 100644 --- a/app/src/main/java/org/linphone/activities/chat_bubble/ChatBubbleActivity.kt +++ b/app/src/main/java/org/linphone/activities/chat_bubble/ChatBubbleActivity.kt @@ -94,10 +94,6 @@ class ChatBubbleActivity : GenericActivity() { return } - // Workaround for the removed notification when a chat room is marked as read - coreContext.notificationsManager.disableDismissNotificationUponReadForChatRoom(chatRoom) - chatRoom.markAsRead() - viewModel = ViewModelProvider( this, ChatRoomViewModelFactory(chatRoom) @@ -158,6 +154,7 @@ class ChatBubbleActivity : GenericActivity() { binding.setOpenAppClickListener { coreContext.notificationsManager.currentlyDisplayedChatRoomAddress = null + coreContext.notificationsManager.changeDismissNotificationUponReadForChatRoom(viewModel.chatRoom, false) val intent = Intent(this, MainActivity::class.java) intent.putExtra("RemoteSipUri", remoteSipUri) @@ -182,6 +179,10 @@ class ChatBubbleActivity : GenericActivity() { viewModel.chatRoom.addListener(listener) + // Workaround for the removed notification when a chat room is marked as read + coreContext.notificationsManager.changeDismissNotificationUponReadForChatRoom(viewModel.chatRoom, true) + viewModel.chatRoom.markAsRead() + val peerAddress = viewModel.chatRoom.peerAddress.asStringUriOnly() coreContext.notificationsManager.currentlyDisplayedChatRoomAddress = peerAddress coreContext.notificationsManager.resetChatNotificationCounterForSipUri(peerAddress) @@ -197,6 +198,7 @@ class ChatBubbleActivity : GenericActivity() { viewModel.chatRoom.removeListener(listener) coreContext.notificationsManager.currentlyDisplayedChatRoomAddress = null + coreContext.notificationsManager.changeDismissNotificationUponReadForChatRoom(viewModel.chatRoom, false) super.onPause() } diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt index d82e59015..4de859bb7 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt @@ -63,7 +63,6 @@ class Notifiable(val notificationId: Int) { var localIdentity: String? = null var myself: String? = null var remoteAddress: String? = null - var dismissNotificationUponReadChatRoom: Boolean = true } class NotifiableMessage( @@ -102,6 +101,7 @@ class NotificationsManager(private val context: Context) { private val chatNotificationsMap: HashMap = HashMap() private val callNotificationsMap: HashMap = HashMap() private val previousChatNotifications: ArrayList = arrayListOf() + private val chatBubbleNotifications: ArrayList = arrayListOf() private var currentForegroundServiceNotificationId: Int = 0 private var serviceNotification: Notification? = null @@ -174,15 +174,20 @@ class NotificationsManager(private val context: Context) { val address = chatRoom.peerAddress.asStringUriOnly() val notifiable = chatNotificationsMap[address] if (notifiable != null) { - if (notifiable.dismissNotificationUponReadChatRoom) { + if (chatBubbleNotifications.contains(notifiable.notificationId)) { + Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read, not removing notification because of a chat bubble") + } else { Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read, removing notification if any") dismissChatNotification(chatRoom) - } else { - Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read, not removing notification, maybe because of a chat bubble?") } } else { - Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read but no notifiable found, removing notification if any") - dismissChatNotification(chatRoom) + val notificationId = chatRoom.creationTime.toInt() + if (chatBubbleNotifications.contains(notificationId)) { + Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read but no notifiable found, not removing notification because of a chat bubble") + } else { + Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read but no notifiable found, removing notification if any") + dismissChatNotification(chatRoom) + } } } } @@ -696,21 +701,26 @@ class NotificationsManager(private val context: Context) { } else { val previousNotificationId = previousChatNotifications.find { id -> id == room.creationTime.toInt() } if (previousNotificationId != null) { - Log.i("[Notifications Manager] Found previous notification with same ID [$previousNotificationId], canceling it") - cancel(previousNotificationId, CHAT_TAG) + if (chatBubbleNotifications.contains(previousNotificationId)) { + Log.i("[Notifications Manager] Found previous notification with same ID [$previousNotificationId] but not cancelling it as it's ID is in chat bubbles list") + } else { + Log.i("[Notifications Manager] Found previous notification with same ID [$previousNotificationId], canceling it") + cancel(previousNotificationId, CHAT_TAG) + } return true } } return false } - fun disableDismissNotificationUponReadForChatRoom(chatRoom: ChatRoom) { - val address = chatRoom.peerAddress.asStringUriOnly() - val notifiable: Notifiable? = chatNotificationsMap[address] - if (notifiable != null) { - Log.i("[Notifications Manager] Prevent notification with id [${notifiable.notificationId}] from being dismissed when chat room will be marked as read") - notifiable.dismissNotificationUponReadChatRoom = false - chatNotificationsMap[address] = notifiable + fun changeDismissNotificationUponReadForChatRoom(chatRoom: ChatRoom, dismiss: Boolean) { + val notificationId = chatRoom.creationTime.toInt() + if (dismiss) { + Log.i("[Notifications Manager] Allow notification with id [$notificationId] to be dismissed when chat room will be marked as read, used for chat bubble") + chatBubbleNotifications.add(notificationId) + } else { + Log.i("[Notifications Manager] Prevent notification with id [$notificationId] from being dismissed when chat room will be marked as read, used for chat bubble") + chatBubbleNotifications.remove(notificationId) } }