Class ShadowAudioManager
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionstatic final com.google.common.collect.ImmutableList<Integer>
static final int
static final int
static final int
static final int
static final int
static final int
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected int
protected int
abandonAudioFocusRequest
(AudioFocusRequest audioFocusRequest) Provides a mock like interface for the abandonAudioFocusRequest method by storing the request object for later inspection and returning the value specified in setNextFocusRequestResponse.void
addAvailableCommunicationDevice
(AudioDeviceInfo communicationDevice, boolean notifyAudioDeviceCallbacks) Adds an available communicationAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was not present before and indicated bynotifyAudioDeviceCallbacks
.void
addInputDevice
(AudioDeviceInfo inputDevice, boolean notifyAudioDeviceCallbacks) Adds an inputAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was not present before and indicated bynotifyAudioDeviceCallbacks
.void
addOutputDevice
(AudioDeviceInfo outputDevice, boolean notifyAudioDeviceCallbacks) Adds an outputAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was not present before and indicated bynotifyAudioDeviceCallbacks
.void
addOutputDeviceWithDirectProfiles
(AudioDeviceInfo outputDevice) Adds an outputdevice
with direct profiles and notifies the list ofAudioDeviceCallback
if the device was not present before.protected void
adjustStreamVolume
(int streamType, int direction, int flags) ImplementsAudioManager.adjustStreamVolume(int, int, int)
.protected void
void
createActiveRecordingConfiguration
(int sessionId, int audioSource, String clientPackageName) Creates simple active recording configuration.protected AudioPlaybackConfiguration
createAudioPlaybackConfiguration
(AudioAttributes audioAttributes) protected void
dispatchMediaKeyEvent
(KeyEvent keyEvent) Sends a simulated key event for a media button.protected int
Provides a mock like interface for theAudioManager.generateAudioSessionId()
method by returning positive distinct values, orAudioManager.ERROR
if all possible values have already been returned.protected List<AudioPlaybackConfiguration>
protected List<AudioRecordingConfiguration>
Returns list of active recording configurations that was set bysetActiveRecordingConfigurations(java.util.List<android.media.AudioRecordingConfiguration>, boolean)
or empty list otherwise.protected List<AudioDeviceInfo>
getAudioDevicesForAttributes
(AudioAttributes attributes) Returns the audio devices that would be used for the routing of the given audio attributes.protected List<AudioDeviceInfo>
protected AudioDeviceInfo
getDevices
(int flags) getDevicesForAttributes
(AudioAttributes attributes) Returns the devices associated with the given audio stream.protected List<AudioProfile>
getDirectProfilesForAttributes
(AudioAttributes attributes) Returns the list of profiles supported for direct playback.protected int
getMode()
getParameter
(String key) Returns a single parameter that was set viasetParameters(String)
.protected String
getParameters
(String keys) The expected composition for keys is not well defined.protected int
protected int
getStreamMaxVolume
(int streamType) protected int
getStreamVolume
(int streamType) protected float
getStreamVolumeDb
(int streamType, int index, int deviceType) boolean
Returns true if at least one audio policy is registered with this manager, and false otherwise.protected boolean
protected boolean
protected boolean
protected boolean
isHotwordStreamSupported
(boolean lookbackAudio) protected boolean
protected boolean
protected boolean
protected boolean
isStreamMute
(int streamType) static boolean
isValidRingerMode
(int ringerMode) protected boolean
void
lockCommunicationDevice
(boolean lockCommunicationDevice) Sets whether subsequent calls tosetCommunicationDevice(android.media.AudioDeviceInfo)
will succeed.void
lockMode
(boolean lockMode) Sets whether subsequent calls tosetMode(int)
will succeed or not.protected void
registerAudioDeviceCallback
(AudioDeviceCallback callback, Handler handler) Registers anAudioDeviceCallback
object to receive notifications of changes to the set of connected audio devices.protected void
Registers callback that will receive changes made to the list of active playback configurations bysetActivePlaybackConfigurationsFor(java.util.List<android.media.AudioAttributes>)
.protected int
registerAudioPolicy
(Object audioPolicy) Registers anAudioPolicy
to allow that policy to control audio routing and audio focus.protected void
Registers callback that will receive changes made to the list of active recording configurations bysetActiveRecordingConfigurations(java.util.List<android.media.AudioRecordingConfiguration>,boolean)
.void
removeAvailableCommunicationDevice
(AudioDeviceInfo communicationDevice, boolean notifyAudioDeviceCallbacks) Removes an available communicationAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was present before and indicated bynotifyAudioDeviceCallbacks
.void
removeInputDevice
(AudioDeviceInfo inputDevice, boolean notifyAudioDeviceCallbacks) Removes an inputAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was present before and indicated bynotifyAudioDeviceCallbacks
.void
removeOutputDevice
(AudioDeviceInfo outputDevice, boolean notifyAudioDeviceCallbacks) Removes an outputAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was present before and indicated bynotifyAudioDeviceCallbacks
.void
removeOutputDeviceWithDirectProfiles
(AudioDeviceInfo outputDevice) Removes an outputdevice
with direct profiles and notifies the list ofAudioDeviceCallback
if the device was present before.protected int
requestAudioFocus
(AudioFocusRequest audioFocusRequest) Provides a mock like interface for the requestAudioFocus method by storing the request object for later inspection and returning the value specified in setNextFocusRequestResponse.protected int
requestAudioFocus
(AudioManager.OnAudioFocusChangeListener l, int streamType, int durationHint) void
setActivePlaybackConfigurationsFor
(List<AudioAttributes> audioAttributes) Sets active playback configurations that will be served byAudioManager.getActivePlaybackConfigurations()
.void
setActivePlaybackConfigurationsFor
(List<AudioAttributes> audioAttributes, boolean notifyCallbackListeners) Same assetActivePlaybackConfigurationsFor(List)
, but also notifies callbacks if notifyCallbackListeners is true.void
setActiveRecordingConfigurations
(List<AudioRecordingConfiguration> activeRecordingConfigurations, boolean notifyCallbackListeners) Sets active recording configurations that will be served byAudioManager.getActiveRecordingConfigurations()
and notifies callback listeners about that change.void
setAudioDevicesForAttributes
(AudioAttributes attributes, com.google.common.collect.ImmutableList<AudioDeviceInfo> devices) Sets the audio devices returned fromgetAudioDevicesForAttributes(android.media.AudioAttributes)
.void
setAvailableCommunicationDevices
(List<AudioDeviceInfo> availableCommunicationDevices) Sets the list of available communication devices represented byAudioDeviceInfo
.protected void
setBluetoothA2dpOn
(boolean on) protected void
setBluetoothScoOn
(boolean isBluetoothScoOn) protected boolean
setCommunicationDevice
(AudioDeviceInfo communicationDevice) Note that this method can silently fail.void
setDefaultDevicesForAttributes
(com.google.common.collect.ImmutableList<Object> devices) Sets the devices to use as default for all audio streams.void
setDevicesForAttributes
(AudioAttributes attributes, com.google.common.collect.ImmutableList<Object> devices) Sets the devices associated with the given audio stream.void
setHotwordStreamSupported
(boolean lookbackAudio, boolean isSupported) void
setInputDevices
(List<AudioDeviceInfo> inputDevices) Sets the list of connected input devices represented byAudioDeviceInfo
.void
setIsBluetoothScoAvailableOffCall
(boolean isBluetoothScoAvailableOffCall) void
setIsMusicActive
(boolean isMusicActive) void
setIsStreamMute
(int streamType, boolean isMuted) protected void
setMicrophoneMute
(boolean on) protected void
setMode
(int mode) Note that this method can silently fail.void
setNextFocusRequestResponse
(int nextResponseValue) void
setOutputDevices
(List<AudioDeviceInfo> outputDevices) Sets the list of connected output devices represented byAudioDeviceInfo
.protected void
setParameters
(String keyValuePairs) protected void
setRingerMode
(int ringerMode) protected void
setSpeakerphoneOn
(boolean on) void
setStreamMaxVolume
(int streamMaxVolume) void
setStreamVolume
(int streamVolume) protected void
setStreamVolume
(int streamType, int index, int flags) protected void
setWiredHeadsetOn
(boolean on) protected void
Unregisters anAudioDeviceCallback
object which has been previously registered to receive notifications of changes to the set of connected audio devices.protected void
Unregisters callback listening to changes made to list of active playback configurations.protected void
unregisterAudioPolicy
(Object audioPolicy) protected void
Unregisters callback listening to changes made to list of active recording configurations.
-
Field Details
-
MAX_VOLUME_MUSIC_DTMF
public static final int MAX_VOLUME_MUSIC_DTMF- See Also:
-
DEFAULT_MAX_VOLUME
public static final int DEFAULT_MAX_VOLUME- See Also:
-
MIN_VOLUME
public static final int MIN_VOLUME- See Also:
-
DEFAULT_VOLUME
public static final int DEFAULT_VOLUME- See Also:
-
INVALID_VOLUME
public static final int INVALID_VOLUME- See Also:
-
FLAG_NO_ACTION
public static final int FLAG_NO_ACTION- See Also:
-
ALL_STREAMS
-
-
Constructor Details
-
ShadowAudioManager
public ShadowAudioManager()
-
-
Method Details
-
getStreamMaxVolume
-
getStreamVolume
-
getStreamVolumeDb
@Implementation(minSdk=28) protected float getStreamVolumeDb(int streamType, int index, int deviceType) -
setStreamVolume
-
isBluetoothScoAvailableOffCall
-
requestAudioFocus
@Implementation protected int requestAudioFocus(AudioManager.OnAudioFocusChangeListener l, int streamType, int durationHint) -
requestAudioFocus
Provides a mock like interface for the requestAudioFocus method by storing the request object for later inspection and returning the value specified in setNextFocusRequestResponse. -
abandonAudioFocus
-
abandonAudioFocusRequest
@Implementation(minSdk=26) protected int abandonAudioFocusRequest(AudioFocusRequest audioFocusRequest) Provides a mock like interface for the abandonAudioFocusRequest method by storing the request object for later inspection and returning the value specified in setNextFocusRequestResponse. -
getRingerMode
-
setRingerMode
-
isValidRingerMode
-
setMode
Note that this method can silently fail. SeelockMode
. -
lockMode
public void lockMode(boolean lockMode) Sets whether subsequent calls tosetMode(int)
will succeed or not. -
getMode
-
setStreamMaxVolume
public void setStreamMaxVolume(int streamMaxVolume) -
setStreamVolume
public void setStreamVolume(int streamVolume) -
setWiredHeadsetOn
-
isWiredHeadsetOn
-
setBluetoothA2dpOn
-
isBluetoothA2dpOn
-
setSpeakerphoneOn
-
isSpeakerphoneOn
-
setMicrophoneMute
-
isMicrophoneMute
-
isBluetoothScoOn
-
setBluetoothScoOn
-
isMusicActive
-
getActivePlaybackConfigurations
@Implementation(minSdk=26) protected List<AudioPlaybackConfiguration> getActivePlaybackConfigurations() -
setParameters
-
getParameters
The expected composition for keys is not well defined.For testing purposes this method call always returns null.
-
getParameter
Returns a single parameter that was set viasetParameters(String)
. -
adjustStreamVolume
ImplementsAudioManager.adjustStreamVolume(int, int, int)
.Currently supports only the directions
AudioManager.ADJUST_MUTE
,AudioManager.ADJUST_UNMUTE
,AudioManager.ADJUST_LOWER
andAudioManager.ADJUST_RAISE
. -
isStreamMute
-
setIsBluetoothScoAvailableOffCall
public void setIsBluetoothScoAvailableOffCall(boolean isBluetoothScoAvailableOffCall) -
setIsStreamMute
public void setIsStreamMute(int streamType, boolean isMuted) -
registerAudioPlaybackCallback
@Implementation(minSdk=26) protected void registerAudioPlaybackCallback(AudioManager.AudioPlaybackCallback cb, Handler handler) Registers callback that will receive changes made to the list of active playback configurations bysetActivePlaybackConfigurationsFor(java.util.List<android.media.AudioAttributes>)
. -
unregisterAudioPlaybackCallback
@Implementation(minSdk=26) protected void unregisterAudioPlaybackCallback(AudioManager.AudioPlaybackCallback cb) Unregisters callback listening to changes made to list of active playback configurations. -
getDevicesForAttributes
@Implementation(minSdk=30) protected List<Object> getDevicesForAttributes(AudioAttributes attributes) Returns the devices associated with the given audio stream.In this shadow-implementation the devices returned are either
- devices set through
setDevicesForAttributes(android.media.AudioAttributes, com.google.common.collect.ImmutableList<java.lang.Object>)
, or - devices set through
setDefaultDevicesForAttributes(com.google.common.collect.ImmutableList<java.lang.Object>)
, or - an empty list.
- devices set through
-
setDevicesForAttributes
public void setDevicesForAttributes(AudioAttributes attributes, com.google.common.collect.ImmutableList<Object> devices) Sets the devices associated with the given audio stream. -
setDefaultDevicesForAttributes
Sets the devices to use as default for all audio streams. -
getAudioDevicesForAttributes
@Implementation(minSdk=33) protected List<AudioDeviceInfo> getAudioDevicesForAttributes(AudioAttributes attributes) Returns the audio devices that would be used for the routing of the given audio attributes.Devices can be added with
setAudioDevicesForAttributes(android.media.AudioAttributes, com.google.common.collect.ImmutableList<android.media.AudioDeviceInfo>)
. Note thatsetDevicesForAttributes(android.media.AudioAttributes, com.google.common.collect.ImmutableList<java.lang.Object>)
andsetDefaultDevicesForAttributes(com.google.common.collect.ImmutableList<java.lang.Object>)
have no effect on the return value of this method. -
setAudioDevicesForAttributes
public void setAudioDevicesForAttributes(AudioAttributes attributes, com.google.common.collect.ImmutableList<AudioDeviceInfo> devices) Sets the audio devices returned fromgetAudioDevicesForAttributes(android.media.AudioAttributes)
. -
setInputDevices
Sets the list of connected input devices represented byAudioDeviceInfo
.The previous list of input devices is replaced and no notifications of the list of
AudioDeviceCallback
is done.To add/remove devices one by one and trigger notifications for the list of
AudioDeviceCallback
please use one of the following methodsaddInputDevice(AudioDeviceInfo, boolean)
,removeInputDevice(AudioDeviceInfo, boolean)
. -
setOutputDevices
Sets the list of connected output devices represented byAudioDeviceInfo
.The previous list of output devices is replaced and no notifications of the list of
AudioDeviceCallback
is done.To add/remove devices one by one and trigger notifications for the list of
AudioDeviceCallback
please use one of the following methodsaddOutputDevice(AudioDeviceInfo, boolean)
,removeOutputDevice(AudioDeviceInfo, boolean)
. -
setAvailableCommunicationDevices
Sets the list of available communication devices represented byAudioDeviceInfo
.The previous list of communication devices is replaced and no notifications of the list of
AudioDeviceCallback
is done.To add/remove devices one by one and trigger notifications for the list of
AudioDeviceCallback
please use one of the following methodsaddOutputDevice(AudioDeviceInfo, boolean)
,removeOutputDevice(AudioDeviceInfo, boolean)
. -
addInputDevice
Adds an inputAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was not present before and indicated bynotifyAudioDeviceCallbacks
. -
removeInputDevice
Removes an inputAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was present before and indicated bynotifyAudioDeviceCallbacks
. -
addOutputDevice
Adds an outputAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was not present before and indicated bynotifyAudioDeviceCallbacks
. -
removeOutputDevice
Removes an outputAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was present before and indicated bynotifyAudioDeviceCallbacks
. -
addAvailableCommunicationDevice
public void addAvailableCommunicationDevice(AudioDeviceInfo communicationDevice, boolean notifyAudioDeviceCallbacks) Adds an available communicationAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was not present before and indicated bynotifyAudioDeviceCallbacks
. -
removeAvailableCommunicationDevice
public void removeAvailableCommunicationDevice(AudioDeviceInfo communicationDevice, boolean notifyAudioDeviceCallbacks) Removes an available communicationAudioDeviceInfo
and notifies the list ofAudioDeviceCallback
if the device was present before and indicated bynotifyAudioDeviceCallbacks
. -
registerAudioDeviceCallback
@Implementation(minSdk=23) protected void registerAudioDeviceCallback(AudioDeviceCallback callback, Handler handler) Registers anAudioDeviceCallback
object to receive notifications of changes to the set of connected audio devices.The
handler
is ignored.- See Also:
-
addInputDevice(AudioDeviceInfo, boolean)
addOutputDevice(AudioDeviceInfo, boolean)
addAvailableCommunicationDevice(AudioDeviceInfo, boolean)
removeInputDevice(AudioDeviceInfo, boolean)
removeOutputDevice(AudioDeviceInfo, boolean)
removeAvailableCommunicationDevice(AudioDeviceInfo, boolean)
addOutputDeviceWithDirectProfiles(AudioDeviceInfo)
removeOutputDeviceWithDirectProfiles(AudioDeviceInfo)
-
unregisterAudioDeviceCallback
@Implementation(minSdk=23) protected void unregisterAudioDeviceCallback(AudioDeviceCallback callback) Unregisters anAudioDeviceCallback
object which has been previously registered to receive notifications of changes to the set of connected audio devices.- See Also:
-
addInputDevice(AudioDeviceInfo, boolean)
addOutputDevice(AudioDeviceInfo, boolean)
addAvailableCommunicationDevice(AudioDeviceInfo, boolean)
removeInputDevice(AudioDeviceInfo, boolean)
removeOutputDevice(AudioDeviceInfo, boolean)
removeAvailableCommunicationDevice(AudioDeviceInfo, boolean)
addOutputDeviceWithDirectProfiles(AudioDeviceInfo)
removeOutputDeviceWithDirectProfiles(AudioDeviceInfo)
-
setCommunicationDevice
@Implementation(minSdk=31) protected boolean setCommunicationDevice(AudioDeviceInfo communicationDevice) Note that this method can silently fail. SeelockCommunicationDevice
. -
lockCommunicationDevice
public void lockCommunicationDevice(boolean lockCommunicationDevice) Sets whether subsequent calls tosetCommunicationDevice(android.media.AudioDeviceInfo)
will succeed. -
getCommunicationDevice
-
clearCommunicationDevice
-
getAvailableCommunicationDevices
-
isHotwordStreamSupported
-
setHotwordStreamSupported
public void setHotwordStreamSupported(boolean lookbackAudio, boolean isSupported) -
getDevices
-
setActivePlaybackConfigurationsFor
Sets active playback configurations that will be served byAudioManager.getActivePlaybackConfigurations()
.Note that there is no public
AudioPlaybackConfiguration
constructor, so the configurations returned are specified by their audio attributes only. -
setActivePlaybackConfigurationsFor
public void setActivePlaybackConfigurationsFor(List<AudioAttributes> audioAttributes, boolean notifyCallbackListeners) Same assetActivePlaybackConfigurationsFor(List)
, but also notifies callbacks if notifyCallbackListeners is true. -
createAudioPlaybackConfiguration
protected AudioPlaybackConfiguration createAudioPlaybackConfiguration(AudioAttributes audioAttributes) -
setIsMusicActive
public void setIsMusicActive(boolean isMusicActive) -
getLastAudioFocusRequest
-
setNextFocusRequestResponse
public void setNextFocusRequestResponse(int nextResponseValue) -
getLastAbandonedAudioFocusListener
-
getLastAbandonedAudioFocusRequest
-
getActiveRecordingConfigurations
@Implementation(minSdk=24) protected List<AudioRecordingConfiguration> getActiveRecordingConfigurations()Returns list of active recording configurations that was set bysetActiveRecordingConfigurations(java.util.List<android.media.AudioRecordingConfiguration>, boolean)
or empty list otherwise. -
registerAudioRecordingCallback
@Implementation(minSdk=24) protected void registerAudioRecordingCallback(AudioManager.AudioRecordingCallback cb, Handler handler) Registers callback that will receive changes made to the list of active recording configurations bysetActiveRecordingConfigurations(java.util.List<android.media.AudioRecordingConfiguration>,boolean)
. -
unregisterAudioRecordingCallback
@Implementation(minSdk=24) protected void unregisterAudioRecordingCallback(AudioManager.AudioRecordingCallback cb) Unregisters callback listening to changes made to list of active recording configurations. -
setActiveRecordingConfigurations
public void setActiveRecordingConfigurations(List<AudioRecordingConfiguration> activeRecordingConfigurations, boolean notifyCallbackListeners) Sets active recording configurations that will be served byAudioManager.getActiveRecordingConfigurations()
and notifies callback listeners about that change. -
createActiveRecordingConfiguration
public AudioRecordingConfiguration createActiveRecordingConfiguration(int sessionId, int audioSource, String clientPackageName) Creates simple active recording configuration. The resulting configuration will returnnull
forAudioRecordingConfiguration.getAudioDevice()
. -
registerAudioPolicy
Registers anAudioPolicy
to allow that policy to control audio routing and audio focus.Note: this implementation does NOT ensure that we have the permissions necessary to register the given
AudioPolicy
.- Returns:
AudioManager.ERROR
if the given policy has already been registered, andAudioManager.SUCCESS
otherwise.
-
unregisterAudioPolicy
-
isAnyAudioPolicyRegistered
public boolean isAnyAudioPolicyRegistered()Returns true if at least one audio policy is registered with this manager, and false otherwise. -
getDirectProfilesForAttributes
@Implementation(minSdk=33) protected List<AudioProfile> getDirectProfilesForAttributes(AudioAttributes attributes) Returns the list of profiles supported for direct playback.In this shadow-implementation the list returned are profiles set through
addOutputDeviceWithDirectProfiles(AudioDeviceInfo)
,removeOutputDeviceWithDirectProfiles(AudioDeviceInfo)
. -
addOutputDeviceWithDirectProfiles
Adds an outputdevice
with direct profiles and notifies the list ofAudioDeviceCallback
if the device was not present before. -
removeOutputDeviceWithDirectProfiles
Removes an outputdevice
with direct profiles and notifies the list ofAudioDeviceCallback
if the device was present before. -
generateAudioSessionId
Provides a mock like interface for theAudioManager.generateAudioSessionId()
method by returning positive distinct values, orAudioManager.ERROR
if all possible values have already been returned. -
dispatchMediaKeyEvent
Sends a simulated key event for a media button.Instead of sending the media event to the media system service, from where it would be routed to a media app, this shadow method only records the events to be verified through
getDispatchedMediaKeyEvents()
. -
getDispatchedMediaKeyEvents
-
clearDispatchedMediaKeyEvents
public void clearDispatchedMediaKeyEvents()
-