From 91877394507b6464cabb15cfa078cdcffed8ff35 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 3 Apr 2023 16:00:02 +0200 Subject: [PATCH] play dial tones on STREAM_MUSIC when phone is silent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit when the phone is silent only the first ~three tones are played when attempting to play out the tone over STREAM_VOICE_CALL it’s unclear exactly why this is the case (in the past we went back and forth between STREAM_VOICE_CALL and STREAM_MUSIC) exactly to fix issues around silent mode. Apparently we failed to test this past three sounds. This commit changes the stream back to music - but not generally as this was in the past - but only for when the phone is on silent --- .../xmpp/jingle/ToneManager.java | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/ToneManager.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/ToneManager.java index 02c1f6fe1..b64f963d0 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/ToneManager.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/ToneManager.java @@ -14,9 +14,11 @@ import eu.siacs.conversations.Config; import static java.util.Arrays.asList; +import androidx.core.content.ContextCompat; + class ToneManager { - private final ToneGenerator toneGenerator; + private ToneGenerator toneGenerator; private final Context context; private ToneState state = null; @@ -26,14 +28,6 @@ class ToneManager { private boolean appRtcAudioManagerHasControl = false; ToneManager(final Context context) { - ToneGenerator toneGenerator; - try { - toneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, 60); - } catch (final RuntimeException e) { - Log.e(Config.LOGTAG, "unable to instantiate ToneGenerator", e); - toneGenerator = null; - } - this.toneGenerator = toneGenerator; this.context = context; } @@ -172,10 +166,28 @@ class ToneManager { } private void startTone(final int toneType, final int durationMs) { + if (this.toneGenerator != null) { + this.toneGenerator.release();; + + } + final AudioManager audioManager = ContextCompat.getSystemService(context, AudioManager.class); + final boolean ringerModeNormal = audioManager == null || audioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL; + this.toneGenerator = getToneGenerator(ringerModeNormal); if (toneGenerator != null) { this.toneGenerator.startTone(toneType, durationMs); - } else { - Log.e(Config.LOGTAG, "failed to start tone. ToneGenerator doesn't exist"); + } + } + + private static ToneGenerator getToneGenerator(final boolean ringerModeNormal) { + try { + if (ringerModeNormal) { + return new ToneGenerator(AudioManager.STREAM_VOICE_CALL,60); + } else { + return new ToneGenerator(AudioManager.STREAM_MUSIC,100); + } + } catch (final Exception e) { + Log.d(Config.LOGTAG,"could not create tone generator",e); + return null; } }