From 2ade576148fe33c5ff6098d30ccfbcf28df70e8e Mon Sep 17 00:00:00 2001 From: Jean-Michel Trivi Date: Sat, 11 Dec 2010 13:18:30 -0800 Subject: Fix bug 3275151 Request and abandon audio focus with audio mode changes Before the introduction of the MODE_IN_COMMUNICATION audio mode, "calls" were only made through telephony and the PhoneStateListener was used to request and abandon audio focus as calls where started and ended. VoIP applications do not cause phone state events to be broadcast, and set the audio mode to MODE_IN_COMMUNICATION directly. This change monitors mode changes to automatically grab and abandon audio focus. Change-Id: Ie9a4193e35ba8447764fac66a988959a93c4909f --- media/java/android/media/AudioService.java | 57 ++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 19 deletions(-) (limited to 'media') diff --git a/media/java/android/media/AudioService.java b/media/java/android/media/AudioService.java index 2836005..6d47c44 100644 --- a/media/java/android/media/AudioService.java +++ b/media/java/android/media/AudioService.java @@ -752,6 +752,10 @@ public class AudioService extends IAudioService.Stub { mode = mMode; } if (mode != mMode) { + + // automatically handle audio focus for mode changes + handleFocusForCalls(mMode, mode); + if (AudioSystem.setPhoneState(mode) == AudioSystem.AUDIO_STATUS_OK) { mMode = mode; @@ -807,6 +811,38 @@ public class AudioService extends IAudioService.Stub { } } + /** pre-condition: oldMode != newMode */ + private void handleFocusForCalls(int oldMode, int newMode) { + // if ringing + if (newMode == AudioSystem.MODE_RINGTONE) { + // if not ringing silently + int ringVolume = AudioService.this.getStreamVolume(AudioManager.STREAM_RING); + if (ringVolume > 0) { + // request audio focus for the communication focus entry + requestAudioFocus(AudioManager.STREAM_RING, + AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, + null, null /* both allowed to be null only for this clientId */, + IN_VOICE_COMM_FOCUS_ID /*clientId*/); + + } + } + // if entering call + else if ((newMode == AudioSystem.MODE_IN_CALL) + || (newMode == AudioSystem.MODE_IN_COMMUNICATION)) { + // request audio focus for the communication focus entry + // (it's ok if focus was already requested during ringing) + requestAudioFocus(AudioManager.STREAM_RING, + AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, + null, null /* both allowed to be null only for this clientId */, + IN_VOICE_COMM_FOCUS_ID /*clientId*/); + } + // if exiting call + else if (newMode == AudioSystem.MODE_NORMAL) { + // abandon audio focus for communication focus entry + abandonAudioFocus(null, IN_VOICE_COMM_FOCUS_ID); + } + } + /** @see AudioManager#getMode() */ public int getMode() { return mMode; @@ -2093,28 +2129,11 @@ public class AudioService extends IAudioService.Stub { synchronized(mRingingLock) { mIsRinging = true; } - int ringVolume = AudioService.this.getStreamVolume(AudioManager.STREAM_RING); - if (ringVolume > 0) { - requestAudioFocus(AudioManager.STREAM_RING, - AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, - null, null /* both allowed to be null only for this clientId */, - IN_VOICE_COMM_FOCUS_ID /*clientId*/); - } - } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) { - //Log.v(TAG, " CALL_STATE_OFFHOOK"); - synchronized(mRingingLock) { - mIsRinging = false; - } - requestAudioFocus(AudioManager.STREAM_RING, - AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, - null, null /* both allowed to be null only for this clientId */, - IN_VOICE_COMM_FOCUS_ID /*clientId*/); - } else if (state == TelephonyManager.CALL_STATE_IDLE) { - //Log.v(TAG, " CALL_STATE_IDLE"); + } else if ((state == TelephonyManager.CALL_STATE_OFFHOOK) + || (state == TelephonyManager.CALL_STATE_IDLE)) { synchronized(mRingingLock) { mIsRinging = false; } - abandonAudioFocus(null, IN_VOICE_COMM_FOCUS_ID); } } }; -- cgit v1.1