From 46579550e477360f246f3eaf9a1acf2ce6ff3cc1 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 10 May 2020 17:54:13 +0200 Subject: [PATCH] fixed weird ToneGenerator crash. fixes #3712 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit obviously tones won’t work then anymore --- .../xmpp/jingle/ToneManager.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 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 38fc60388..b4e67cd36 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/ToneManager.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/ToneManager.java @@ -21,7 +21,14 @@ class ToneManager { private ScheduledFuture currentTone; ToneManager() { - this.toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 35); + ToneGenerator toneGenerator; + try { + toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 35); + } catch (final RuntimeException e) { + Log.e(Config.LOGTAG, "unable to instantiate ToneGenerator", e); + toneGenerator = null; + } + this.toneGenerator = toneGenerator; } void transition(final RtpEndUserState state) { @@ -86,25 +93,25 @@ class ToneManager { private void scheduleConnected() { this.currentTone = JingleConnectionManager.SCHEDULED_EXECUTOR_SERVICE.schedule(() -> { - this.toneGenerator.startTone(ToneGenerator.TONE_PROP_PROMPT, 200); + startTone(ToneGenerator.TONE_PROP_PROMPT, 200); }, 0, TimeUnit.SECONDS); } private void scheduleEnding() { this.currentTone = JingleConnectionManager.SCHEDULED_EXECUTOR_SERVICE.schedule(() -> { - this.toneGenerator.startTone(ToneGenerator.TONE_CDMA_CALLDROP_LITE, 375); + startTone(ToneGenerator.TONE_CDMA_CALLDROP_LITE, 375); }, 0, TimeUnit.SECONDS); } private void scheduleBusy() { this.currentTone = JingleConnectionManager.SCHEDULED_EXECUTOR_SERVICE.schedule(() -> { - this.toneGenerator.startTone(ToneGenerator.TONE_CDMA_NETWORK_BUSY, 2500); + startTone(ToneGenerator.TONE_CDMA_NETWORK_BUSY, 2500); }, 0, TimeUnit.SECONDS); } private void scheduleWaitingTone() { this.currentTone = JingleConnectionManager.SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(() -> { - this.toneGenerator.startTone(ToneGenerator.TONE_CDMA_DIAL_TONE_LITE, 750); + startTone(ToneGenerator.TONE_CDMA_DIAL_TONE_LITE, 750); }, 0, 3, TimeUnit.SECONDS); } @@ -112,7 +119,17 @@ class ToneManager { if (currentTone != null) { currentTone.cancel(true); } - toneGenerator.stopTone(); + if (toneGenerator != null) { + toneGenerator.stopTone(); + } + } + + private void startTone(final int toneType, final int durationMs) { + if (toneGenerator != null) { + this.toneGenerator.startTone(toneType, durationMs); + } else { + Log.e(Config.LOGTAG, "failed to start tone. ToneGenerator doesn't exist"); + } } private enum ToneState {