Catch exceptions when retrieving current device from audio manager

We can just continue to assume that we don't know the current device.
This case happens on the latest Robolectric release where this method
call isn't implemented yet. As we not generally assume that the
method can throw, this workaround can be removed once Robolectric
is updated again.

#minor-release

PiperOrigin-RevId: 600426851
(cherry picked from commit 81615dd5b5)
This commit is contained in:
tonihei 2024-01-22 05:17:35 -08:00 committed by SheenaChhabra
parent 05b8e633e3
commit 6632531c4a

View file

@ -486,10 +486,18 @@ public final class AudioCapabilities {
@DoNotInline
public static AudioDeviceInfoApi23 getDefaultRoutedDeviceForAttributes(
AudioManager audioManager, AudioAttributes audioAttributes) {
List<AudioDeviceInfo> audioDevices =
checkNotNull(audioManager)
.getAudioDevicesForAttributes(
audioAttributes.getAudioAttributesV21().audioAttributes);
List<AudioDeviceInfo> audioDevices;
try {
audioDevices =
checkNotNull(audioManager)
.getAudioDevicesForAttributes(
audioAttributes.getAudioAttributesV21().audioAttributes);
} catch (RuntimeException e) {
// Audio manager failed to retrieve devices.
// TODO: b/306324391 - Remove once https://github.com/robolectric/robolectric/commit/442dff
// is released.
return null;
}
if (audioDevices.isEmpty()) {
// Can't find current device.
return null;