From fcd365ad815a571c2a8eb5f1525911af30e81935 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 25 Feb 2025 11:22:26 +0100 Subject: [PATCH] Play incoming chat sound file when a message is being received in currently opened conversation --- .../java/org/linphone/core/CorePreferences.kt | 4 +++ .../notifications/NotificationsManager.kt | 30 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/linphone/core/CorePreferences.kt b/app/src/main/java/org/linphone/core/CorePreferences.kt index 72bd1e4ca..36eccce22 100644 --- a/app/src/main/java/org/linphone/core/CorePreferences.kt +++ b/app/src/main/java/org/linphone/core/CorePreferences.kt @@ -345,6 +345,10 @@ class CorePreferences val ssoCacheFile: String get() = context.filesDir.absolutePath + "/auth_state.json" + @get:AnyThread + val messageReceivedInVisibleConversationNotificationSound: String + get() = context.filesDir.absolutePath + "/share/sounds/linphone/incoming_chat.wav" + @UiThread fun copyAssetsFromPackage() { copy("linphonerc_default", configPath) diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt index 0cac92344..2d16bacd2 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt @@ -31,6 +31,7 @@ import android.content.pm.PackageManager import android.graphics.Bitmap import android.media.AudioAttributes import android.media.AudioManager +import android.media.MediaPlayer import android.media.RingtoneManager import android.net.Uri import android.os.Bundle @@ -124,6 +125,8 @@ class NotificationsManager private var currentlyDisplayedChatRoomId: String = "" private var currentlyDisplayedIncomingCallFragment: Boolean = false + private val mediaPlayer: MediaPlayer + private val contactsListener = object : ContactsListener { @WorkerThread override fun onContactsLoaded() { } @@ -267,8 +270,9 @@ class NotificationsManager val id = LinphoneUtils.getConversationId(chatRoom) if (currentlyDisplayedChatRoomId.isNotEmpty() && id == currentlyDisplayedChatRoomId) { Log.i( - "$TAG Do not notify received messages for currently displayed conversation [$id]" + "$TAG Do not notify received messages for currently displayed conversation [$id] but play sound" ) + playMessageReceivedSound() return } @@ -442,6 +446,21 @@ class NotificationsManager previousChatNotifications.add(notification.id) } } + + val soundPath = corePreferences.messageReceivedInVisibleConversationNotificationSound + mediaPlayer = MediaPlayer().apply { + setAudioAttributes( + AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) + .setUsage(AudioAttributes.USAGE_NOTIFICATION) + .build() + ) + setDataSource(soundPath) + try { + prepare() + } catch (e: Exception) { + Log.e("$TAG Failed to prepare message received sound file [$soundPath]: $e") + } + } } @AnyThread @@ -1606,6 +1625,15 @@ class NotificationsManager } } + @WorkerThread + private fun playMessageReceivedSound() { + try { + mediaPlayer.start() + } catch (e: Exception) { + Log.e("$TAG Failed to play message received sound file: $e") + } + } + class Notifiable(val notificationId: Int) { var myself: String? = null