diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt index 03e4d591d..1240e989d 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt @@ -318,11 +318,6 @@ class NotificationsManager Log.i("$TAG Received ${messages.size} aggregated messages") if (corePreferences.disableChat) return - if (!ShortcutUtils.isShortcutToChatRoomAlreadyCreated(context, chatRoom)) { - Log.i("$TAG A message was received in a chat room for which there is no dynamic shortcut, let's create it") - ShortcutUtils.createDynamicShortcutToChatRoom(context, chatRoom) - } - val id = LinphoneUtils.getConversationId(chatRoom) if (currentlyDisplayedChatRoomId.isNotEmpty() && id == currentlyDisplayedChatRoomId) { Log.i( @@ -1057,6 +1052,12 @@ class NotificationsManager } } + if (notifiable.messages.count() == messages.size) { + Log.i("$TAG Creating or updating chat room shortcut") + // Only do it the first time we create the notification + ShortcutUtils.createOrUpdateChatRoomShortcut(context, chatRoom) + } + if (notifiable.messages.isNotEmpty()) { val me = coreContext.contactsManager.getMePerson(chatRoom.localAddress) val pendingIntent = getChatRoomPendingIntent(chatRoom, notifiable.notificationId) @@ -1124,6 +1125,11 @@ class NotificationsManager notifiable.messages.add(notifiableMessage) if (notifiable.messages.isNotEmpty()) { + if (notifiable.messages.count() == 1) { + // Only do it the first time we create the notification + ShortcutUtils.createOrUpdateChatRoomShortcut(context, chatRoom) + } + val me = coreContext.contactsManager.getMePerson(chatRoom.localAddress) val pendingIntent = getChatRoomPendingIntent(chatRoom, notifiable.notificationId) val notification = createMessageNotification( diff --git a/app/src/main/java/org/linphone/ui/main/chat/fragment/ConversationFragment.kt b/app/src/main/java/org/linphone/ui/main/chat/fragment/ConversationFragment.kt index 7b9e53a75..08441843f 100644 --- a/app/src/main/java/org/linphone/ui/main/chat/fragment/ConversationFragment.kt +++ b/app/src/main/java/org/linphone/ui/main/chat/fragment/ConversationFragment.kt @@ -96,6 +96,7 @@ import org.linphone.utils.showKeyboard import androidx.core.net.toUri import org.linphone.ui.main.chat.adapter.ConversationParticipantsAdapter import org.linphone.ui.main.chat.model.MessageDeleteDialogModel +import org.linphone.utils.ShortcutUtils import kotlin.collections.arrayListOf @UiThread @@ -529,6 +530,7 @@ open class ConversationFragment : SlidingPaneChildFragment() { } } else { sharedViewModel.displayedChatRoom = viewModel.chatRoom + ShortcutUtils.reportChatRoomShortcutHasBeenUsed(requireContext(), viewModel.conversationId) sendMessageViewModel.configureChatRoom(viewModel.chatRoom) adapter.setIsConversationSecured(viewModel.isEndToEndEncrypted.value == true) diff --git a/app/src/main/java/org/linphone/utils/ShortcutUtils.kt b/app/src/main/java/org/linphone/utils/ShortcutUtils.kt index 6a5cc8821..6e75f08b9 100644 --- a/app/src/main/java/org/linphone/utils/ShortcutUtils.kt +++ b/app/src/main/java/org/linphone/utils/ShortcutUtils.kt @@ -23,6 +23,7 @@ import android.content.Context import android.content.Intent import android.content.pm.ShortcutInfo import android.os.Bundle +import androidx.annotation.AnyThread import androidx.annotation.WorkerThread import androidx.collection.ArraySet import androidx.core.app.Person @@ -101,7 +102,6 @@ class ShortcutUtils { private fun createChatRoomShortcut(context: Context, chatRoom: ChatRoom): ShortcutInfoCompat? { val peerAddress = chatRoom.peerAddress val id = LinphoneUtils.getConversationId(chatRoom) - Log.i("$TAG Creating dynamic shortcut for chat room [$id]") try { val categories: ArraySet = ArraySet() @@ -192,5 +192,47 @@ class ShortcutUtils { Log.d("$TAG Dynamic shortcut for chat room with ID [$id] ${if (found != null) "exists" else "doesn't exists"}") return found != null } + + @WorkerThread + fun createOrUpdateChatRoomShortcut(context: Context, chatRoom: ChatRoom) { + val id = LinphoneUtils.getConversationId(chatRoom) + val found = ShortcutManagerCompat.getDynamicShortcuts(context).find { + it.id == id + } + val shortcut: ShortcutInfoCompat? = createChatRoomShortcut(context, chatRoom) + if (shortcut == null) { + Log.e("$TAG Failed to create shortcut info for chat room [$id]") + return + } + + if (found == null) { + Log.i("$TAG Created dynamic shortcut for ${shortcut.shortLabel}, pushing it") + try { + ShortcutManagerCompat.pushDynamicShortcut(context, shortcut) + } catch (e: Exception) { + Log.e("$TAG Failed to push dynamic shortcut for ${shortcut.shortLabel}: $e") + } + } else { + Log.i("$TAG Updating dynamic shortcut for ${shortcut.shortLabel}") + try { + ShortcutManagerCompat.updateShortcuts(context, listOf(shortcut)) + } catch (e: Exception) { + Log.e("$TAG Failed to update dynamic shortcut for ${shortcut.shortLabel}: $e") + } + } + } + + @AnyThread + fun reportChatRoomShortcutHasBeenUsed(context: Context, chatRoomId: String) { + val found = ShortcutManagerCompat.getDynamicShortcuts(context).find { + it.id == chatRoomId + } + if (found != null) { + Log.i("$TAG Reporting shortcut for chat room [$chatRoomId] was used") + ShortcutManagerCompat.reportShortcutUsed(context, chatRoomId) + } else { + Log.w("$TAG No shortcut was found for chat room [$chatRoomId]") + } + } } }