From 0caad7f348ae51fb4fec36ebc934065302c7303e Mon Sep 17 00:00:00 2001 From: Christian Ascheberg Date: Wed, 16 Dec 2015 17:43:16 +0100 Subject: [PATCH] Do not automatically enable bluetooth audio routing Fixes some call volume problems Fixes #4773 Fixes #4188 Closes #4885 --- src/org/thoughtcrime/redphone/RedPhoneService.java | 9 ++++++--- .../thoughtcrime/redphone/audio/CallAudioManager.java | 1 + .../thoughtcrime/redphone/audio/IncomingRinger.java | 7 ++++--- .../thoughtcrime/redphone/audio/OutgoingRinger.java | 5 +++-- src/org/thoughtcrime/redphone/util/AudioUtils.java | 11 +++++++---- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/org/thoughtcrime/redphone/RedPhoneService.java b/src/org/thoughtcrime/redphone/RedPhoneService.java index a94798efe8..0757d02c74 100644 --- a/src/org/thoughtcrime/redphone/RedPhoneService.java +++ b/src/org/thoughtcrime/redphone/RedPhoneService.java @@ -293,14 +293,17 @@ public class RedPhoneService extends Service implements CallStateListener, CallS AudioManager audioManager = ServiceUtil.getAudioManager(this); AudioUtils.resetConfiguration(this); - audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, - AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); + Log.d(TAG, "request STREAM_VOICE_CALL audio focus"); + audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN); } private void shutdownAudio() { - AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE); + Log.d(TAG, "reset audio mode and abandon focus"); + AudioUtils.resetConfiguration(this); + AudioManager am = ServiceUtil.getAudioManager(this); am.setMode(AudioManager.MODE_NORMAL); am.abandonAudioFocus(null); + am.stopBluetoothSco(); } public int getState() { diff --git a/src/org/thoughtcrime/redphone/audio/CallAudioManager.java b/src/org/thoughtcrime/redphone/audio/CallAudioManager.java index 34a5c8b7a8..cf320514ab 100644 --- a/src/org/thoughtcrime/redphone/audio/CallAudioManager.java +++ b/src/org/thoughtcrime/redphone/audio/CallAudioManager.java @@ -45,6 +45,7 @@ public class CallAudioManager { public void start(@NonNull Context context) throws AudioException { if (Build.VERSION.SDK_INT >= 11) { + Log.d(TAG, "set MODE_IN_COMMUNICATION audio mode"); ServiceUtil.getAudioManager(context).setMode(AudioManager.MODE_IN_COMMUNICATION); } else { // ServiceUtil.getAudioManager(context).setMode(AudioManager.MODE_IN_CALL); diff --git a/src/org/thoughtcrime/redphone/audio/IncomingRinger.java b/src/org/thoughtcrime/redphone/audio/IncomingRinger.java index b3d33edc42..9a96afb74d 100644 --- a/src/org/thoughtcrime/redphone/audio/IncomingRinger.java +++ b/src/org/thoughtcrime/redphone/audio/IncomingRinger.java @@ -17,7 +17,6 @@ package org.thoughtcrime.redphone.audio; -import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Context; import android.media.AudioManager; @@ -91,6 +90,7 @@ public class IncomingRinger { } if (player != null && ringerMode == AudioManager.RINGER_MODE_NORMAL ) { + Log.d(TAG, "set MODE_RINGTONE audio mode"); audioManager.setMode(AudioManager.MODE_RINGTONE); try { if(!player.isPlaying()) { @@ -117,6 +117,7 @@ public class IncomingRinger { Log.d(TAG, "Cancelling vibrator"); vibrator.cancel(); + Log.d(TAG, "reset audio mode"); AudioManager audioManager = ServiceUtil.getAudioManager(context); audioManager.setMode(AudioManager.MODE_NORMAL); } @@ -134,7 +135,7 @@ public class IncomingRinger { @TargetApi(Build.VERSION_CODES.HONEYCOMB) private boolean shouldVibrateNew(Context context) { - AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + AudioManager audioManager = ServiceUtil.getAudioManager(context); Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); if (vibrator == null || !vibrator.hasVibrator()) { @@ -151,7 +152,7 @@ public class IncomingRinger { } private boolean shouldVibrateOld(Context context) { - AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + AudioManager audioManager = ServiceUtil.getAudioManager(context); return audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER); } } diff --git a/src/org/thoughtcrime/redphone/audio/OutgoingRinger.java b/src/org/thoughtcrime/redphone/audio/OutgoingRinger.java index 156cea1f09..7890f9122b 100644 --- a/src/org/thoughtcrime/redphone/audio/OutgoingRinger.java +++ b/src/org/thoughtcrime/redphone/audio/OutgoingRinger.java @@ -24,6 +24,7 @@ import android.net.Uri; import android.util.Log; import org.thoughtcrime.securesms.R; +import org.thoughtcrime.securesms.util.ServiceUtil; import java.io.IOException; @@ -133,12 +134,12 @@ public class OutgoingRinger implements MediaPlayer.OnCompletionListener, MediaPl public void onPrepared(MediaPlayer mp) { mediaPlayer.setLooping(loopEnabled); - AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + AudioManager am = ServiceUtil.getAudioManager(context); if (am.isBluetoothScoAvailableOffCall()) { + Log.d(TAG, "bluetooth sco is available"); try { am.startBluetoothSco(); - am.setBluetoothScoOn(true); } catch (NullPointerException e) { // Lollipop bug (https://stackoverflow.com/questions/26642218/audiomanager-startbluetoothsco-crashes-on-android-lollipop) } diff --git a/src/org/thoughtcrime/redphone/util/AudioUtils.java b/src/org/thoughtcrime/redphone/util/AudioUtils.java index 406682f386..242b44e359 100644 --- a/src/org/thoughtcrime/redphone/util/AudioUtils.java +++ b/src/org/thoughtcrime/redphone/util/AudioUtils.java @@ -5,6 +5,8 @@ import android.media.AudioManager; import android.os.Build; import android.util.Log; +import org.thoughtcrime.securesms.util.ServiceUtil; + /** * Utilities for manipulating device audio configuration * @@ -13,22 +15,23 @@ import android.util.Log; public class AudioUtils { private static final String TAG = AudioUtils.class.getName(); public static void enableDefaultRouting(Context context) { - AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + AudioManager am = ServiceUtil.getAudioManager(context); am.setSpeakerphoneOn(false); am.setBluetoothScoOn(false); Log.d(TAG, "Set default audio routing"); } public static void enableSpeakerphoneRouting(Context context) { - AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + AudioManager am = ServiceUtil.getAudioManager(context); am.setSpeakerphoneOn(true); Log.d(TAG, "Set speakerphone audio routing"); } public static void enableBluetoothRouting(Context context) { - AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + AudioManager am = ServiceUtil.getAudioManager(context); am.startBluetoothSco(); am.setBluetoothScoOn(true); + Log.d(TAG, "Set bluetooth audio routing"); } public static void resetConfiguration(Context context) { @@ -42,7 +45,7 @@ public class AudioUtils { } public static AudioMode getCurrentAudioMode(Context context) { - AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + AudioManager am = ServiceUtil.getAudioManager(context); if (am.isBluetoothScoOn()) { return AudioMode.HEADSET; } else if (am.isSpeakerphoneOn()) {