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
This commit is contained in:
Jean-Michel Trivi
2010-12-11 13:18:30 -08:00
parent dfcaeb1677
commit 2ade576148

View File

@ -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");
} else if ((state == TelephonyManager.CALL_STATE_OFFHOOK)
|| (state == TelephonyManager.CALL_STATE_IDLE)) {
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");
synchronized(mRingingLock) {
mIsRinging = false;
}
abandonAudioFocus(null, IN_VOICE_COMM_FOCUS_ID);
}
}
};