diff options
-rw-r--r-- | media/java/android/media/AudioService.java | 57 |
1 files changed, 38 insertions, 19 deletions
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); } } }; |