From f3857ed933f0e8b0b9abd363aa91171f54f3fe96 Mon Sep 17 00:00:00 2001 From: lisa-signal Date: Fri, 13 Jun 2025 13:45:18 -0400 Subject: [PATCH] Prevent notification sound when voice recording. --- .../securesms/audio/AudioRecorder.java | 5 +++- .../v2/InChatNotificationSoundSuppressor.kt | 23 +++++++++++++++++++ .../notifications/v2/NotificationFactory.kt | 3 ++- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/notifications/v2/InChatNotificationSoundSuppressor.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/audio/AudioRecorder.java b/app/src/main/java/org/thoughtcrime/securesms/audio/AudioRecorder.java index f5f0f803aa..cbfbc427fd 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/audio/AudioRecorder.java +++ b/app/src/main/java/org/thoughtcrime/securesms/audio/AudioRecorder.java @@ -13,6 +13,7 @@ import org.signal.core.util.ThreadUtil; import org.signal.core.util.concurrent.SignalExecutors; import org.signal.core.util.logging.Log; import org.thoughtcrime.securesms.components.voice.VoiceNoteDraft; +import org.thoughtcrime.securesms.notifications.v2.InChatNotificationSoundSuppressor; import org.thoughtcrime.securesms.providers.BlobProvider; import org.thoughtcrime.securesms.util.MediaUtil; @@ -99,6 +100,7 @@ public class AudioRecorder { } recorder.start(fds[1]); this.recordingSubject = recordingSingle; + InChatNotificationSoundSuppressor.suppressNotification(); } catch (IOException | RuntimeException e) { Log.w(TAG, e); recordingUriFuture = null; @@ -120,6 +122,7 @@ public class AudioRecorder { Log.e(TAG, "MediaRecorder was never initialized successfully!"); return; } + InChatNotificationSoundSuppressor.allowNotification(); audioFocusManager.abandonAudioFocus(); recorder.stop(); recordingUriFuture.cancel(true); @@ -138,7 +141,7 @@ public class AudioRecorder { Log.e(TAG, "MediaRecorder was never initialized successfully!"); return; } - + InChatNotificationSoundSuppressor.allowNotification(); audioFocusManager.abandonAudioFocus(); recorder.stop(); diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/v2/InChatNotificationSoundSuppressor.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/v2/InChatNotificationSoundSuppressor.kt new file mode 100644 index 0000000000..8d88103c6f --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/v2/InChatNotificationSoundSuppressor.kt @@ -0,0 +1,23 @@ +package org.thoughtcrime.securesms.notifications.v2 + +import org.signal.core.util.logging.Log + +/** + * Suppressor for notification sounds. + */ +object InChatNotificationSoundSuppressor { + private val TAG = Log.tag(InChatNotificationSoundSuppressor::class.java) + var isSuppressed: Boolean = false + + @JvmStatic + fun suppressNotification() { + isSuppressed = true + Log.d(TAG, "Notification is suppressed.") + } + + @JvmStatic + fun allowNotification() { + isSuppressed = false + Log.d(TAG, "Notification is allowed.") + } +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/v2/NotificationFactory.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/v2/NotificationFactory.kt index c660572c8c..bbb013d370 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/v2/NotificationFactory.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/v2/NotificationFactory.kt @@ -311,7 +311,8 @@ object NotificationFactory { if (!NotificationChannels.getInstance().areNotificationsEnabled() || !SignalStore.settings.isMessageNotificationsInChatSoundsEnabled || ServiceUtil.getAudioManager(context).ringerMode != AudioManager.RINGER_MODE_NORMAL || - (System.currentTimeMillis() - lastAudibleNotification) < DefaultMessageNotifier.MIN_AUDIBLE_PERIOD_MILLIS + (System.currentTimeMillis() - lastAudibleNotification) < DefaultMessageNotifier.MIN_AUDIBLE_PERIOD_MILLIS || + InChatNotificationSoundSuppressor.isSuppressed ) { return }