Skip to content

Commit 11001c3

Browse files
jmtriviAndroid (Google) Code Review
authored andcommitted
Merge "Bug 5567648 disassociate audio mode and audio focus"
2 parents b975ae4 + 4dd3674 commit 11001c3

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

media/java/android/media/AudioManager.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,11 +1649,46 @@ public int requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int d
16491649
mAudioFocusDispatcher, getIdForAudioFocusListener(l),
16501650
mContext.getPackageName() /* package name */);
16511651
} catch (RemoteException e) {
1652-
Log.e(TAG, "Can't call requestAudioFocus() from AudioService due to "+e);
1652+
Log.e(TAG, "Can't call requestAudioFocus() on AudioService due to "+e);
16531653
}
16541654
return status;
16551655
}
16561656

1657+
/**
1658+
* @hide
1659+
* Used internally by telephony package to request audio focus. Will cause the focus request
1660+
* to be associated with the "voice communication" identifier only used in AudioService
1661+
* to identify this use case.
1662+
* @param streamType use STREAM_RING for focus requests when ringing, VOICE_CALL for
1663+
* the establishment of the call
1664+
* @param durationHint the type of focus request. AUDIOFOCUS_GAIN_TRANSIENT is recommended so
1665+
* media applications resume after a call
1666+
*/
1667+
public void requestAudioFocusForCall(int streamType, int durationHint) {
1668+
IAudioService service = getService();
1669+
try {
1670+
service.requestAudioFocus(streamType, durationHint, mICallBack, null,
1671+
AudioService.IN_VOICE_COMM_FOCUS_ID,
1672+
"system" /* dump-friendly package name */);
1673+
} catch (RemoteException e) {
1674+
Log.e(TAG, "Can't call requestAudioFocusForCall() on AudioService due to "+e);
1675+
}
1676+
}
1677+
1678+
/**
1679+
* @hide
1680+
* Used internally by telephony package to abandon audio focus, typically after a call or
1681+
* when ringing ends and the call is rejected or not answered.
1682+
* Should match one or more calls to {@link #requestAudioFocusForCall(int, int)}.
1683+
*/
1684+
public void abandonAudioFocusForCall() {
1685+
IAudioService service = getService();
1686+
try {
1687+
service.abandonAudioFocus(null, AudioService.IN_VOICE_COMM_FOCUS_ID);
1688+
} catch (RemoteException e) {
1689+
Log.e(TAG, "Can't call abandonAudioFocusForCall() on AudioService due to "+e);
1690+
}
1691+
}
16571692

16581693
/**
16591694
* Abandon audio focus. Causes the previous focus owner, if any, to receive focus.
@@ -1668,7 +1703,7 @@ public int abandonAudioFocus(OnAudioFocusChangeListener l) {
16681703
status = service.abandonAudioFocus(mAudioFocusDispatcher,
16691704
getIdForAudioFocusListener(l));
16701705
} catch (RemoteException e) {
1671-
Log.e(TAG, "Can't call abandonAudioFocus() from AudioService due to "+e);
1706+
Log.e(TAG, "Can't call abandonAudioFocus() on AudioService due to "+e);
16721707
}
16731708
return status;
16741709
}

media/java/android/media/AudioService.java

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -992,8 +992,6 @@ int setModeInt(int mode, IBinder cb, int pid) {
992992
if (mode != mMode) {
993993
status = AudioSystem.setPhoneState(mode);
994994
if (status == AudioSystem.AUDIO_STATUS_OK) {
995-
// automatically handle audio focus for mode changes
996-
handleFocusForCalls(mMode, mode, cb);
997995
mMode = mode;
998996
} else {
999997
if (hdlr != null) {
@@ -1024,40 +1022,6 @@ int setModeInt(int mode, IBinder cb, int pid) {
10241022
return newModeOwnerPid;
10251023
}
10261024

1027-
/** pre-condition: oldMode != newMode */
1028-
private void handleFocusForCalls(int oldMode, int newMode, IBinder cb) {
1029-
// if ringing
1030-
if (newMode == AudioSystem.MODE_RINGTONE) {
1031-
// if not ringing silently
1032-
int ringVolume = AudioService.this.getStreamVolume(AudioManager.STREAM_RING);
1033-
if (ringVolume > 0) {
1034-
// request audio focus for the communication focus entry
1035-
requestAudioFocus(AudioManager.STREAM_RING,
1036-
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, cb,
1037-
null /* IAudioFocusDispatcher allowed to be null only for this clientId */,
1038-
IN_VOICE_COMM_FOCUS_ID /*clientId*/,
1039-
"system");
1040-
1041-
}
1042-
}
1043-
// if entering call
1044-
else if ((newMode == AudioSystem.MODE_IN_CALL)
1045-
|| (newMode == AudioSystem.MODE_IN_COMMUNICATION)) {
1046-
// request audio focus for the communication focus entry
1047-
// (it's ok if focus was already requested during ringing)
1048-
requestAudioFocus(AudioManager.STREAM_RING,
1049-
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, cb,
1050-
null /* IAudioFocusDispatcher allowed to be null only for this clientId */,
1051-
IN_VOICE_COMM_FOCUS_ID /*clientId*/,
1052-
"system");
1053-
}
1054-
// if exiting call
1055-
else if (newMode == AudioSystem.MODE_NORMAL) {
1056-
// abandon audio focus for communication focus entry
1057-
abandonAudioFocus(null, IN_VOICE_COMM_FOCUS_ID);
1058-
}
1059-
}
1060-
10611025
/** @see AudioManager#getMode() */
10621026
public int getMode() {
10631027
return mMode;
@@ -2896,9 +2860,10 @@ public void onReceive(Context context, Intent intent) {
28962860
//==========================================================================================
28972861

28982862
/* constant to identify focus stack entry that is used to hold the focus while the phone
2899-
* is ringing or during a call
2863+
* is ringing or during a call. Used by com.android.internal.telephony.CallManager when
2864+
* entering and exiting calls.
29002865
*/
2901-
private final static String IN_VOICE_COMM_FOCUS_ID = "AudioFocus_For_Phone_Ring_And_Calls";
2866+
public final static String IN_VOICE_COMM_FOCUS_ID = "AudioFocus_For_Phone_Ring_And_Calls";
29022867

29032868
private final static Object mAudioFocusLock = new Object();
29042869

0 commit comments

Comments
 (0)