Package org.robolectric.shadows
Class ShadowAudioManager
- java.lang.Object
-
- org.robolectric.shadows.ShadowAudioManager
-
@Implements(value=android.media.AudioManager.class, looseSignatures=true) public class ShadowAudioManager extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ShadowAudioManager.AudioFocusRequest
-
Field Summary
Fields Modifier and Type Field Description static com.google.common.collect.ImmutableList<Integer>
ALL_STREAMS
static int
DEFAULT_MAX_VOLUME
static int
DEFAULT_VOLUME
static int
FLAG_NO_ACTION
static int
INVALID_VOLUME
static int
MAX_VOLUME_MUSIC_DTMF
static int
MIN_VOLUME
-
Constructor Summary
Constructors Constructor Description ShadowAudioManager()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int
abandonAudioFocus(AudioManager.OnAudioFocusChangeListener l)
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.protected void
adjustStreamVolume(int streamType, int direction, int flags)
ImplementsAudioManager.adjustStreamVolume(int, int, int)
.AudioRecordingConfiguration
createActiveRecordingConfiguration(int sessionId, int audioSource, String clientPackageName)
Creates simple active recording configuration.protected AudioPlaybackConfiguration
createAudioPlaybackConfiguration(AudioAttributes audioAttributes)
protected int
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.protected List<AudioPlaybackConfiguration>
getActivePlaybackConfigurations()
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.protected List<Object>
getDevicesForAttributes(AudioAttributes attributes)
Returns the devices associated with the given audio stream.AudioManager.OnAudioFocusChangeListener
getLastAbandonedAudioFocusListener()
AudioFocusRequest
getLastAbandonedAudioFocusRequest()
ShadowAudioManager.AudioFocusRequest
getLastAudioFocusRequest()
protected int
getMode()
String
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
getRingerMode()
protected int
getStreamMaxVolume(int streamType)
protected int
getStreamVolume(int streamType)
protected float
getStreamVolumeDb(int streamType, int index, int deviceType)
boolean
isAnyAudioPolicyRegistered()
Returns true if at least one audio policy is registered with this manager, and false otherwise.protected boolean
isBluetoothA2dpOn()
protected boolean
isBluetoothScoAvailableOffCall()
protected boolean
isBluetoothScoOn()
protected boolean
isMicrophoneMute()
protected boolean
isMusicActive()
protected boolean
isSpeakerphoneOn()
protected boolean
isStreamMute(int streamType)
static boolean
isValidRingerMode(int ringerMode)
protected boolean
isWiredHeadsetOn()
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>)
.protected int
registerAudioPolicy(Object audioPolicy)
Registers anAudioPolicy
to allow that policy to control audio routing and audio focus.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)
.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.protected void
setBluetoothA2dpOn(boolean on)
protected void
setBluetoothScoOn(boolean isBluetoothScoOn)
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
setIsBluetoothScoAvailableOffCall(boolean isBluetoothScoAvailableOffCall)
void
setIsMusicActive(boolean isMusicActive)
void
setIsStreamMute(int streamType, boolean isMuted)
protected void
setMicrophoneMute(boolean on)
protected void
setMode(int mode)
void
setNextFocusRequestResponse(int nextResponseValue)
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
unregisterAudioPlaybackCallback(AudioManager.AudioPlaybackCallback cb)
Unregisters callback listening to changes made to list of active playback configurations.protected void
unregisterAudioPolicy(Object audioPolicy)
protected void
unregisterAudioRecordingCallback(AudioManager.AudioRecordingCallback cb)
Unregisters callback listening to changes made to list of active recording configurations.
-
-
-
Field Detail
-
MAX_VOLUME_MUSIC_DTMF
public static final int MAX_VOLUME_MUSIC_DTMF
- See Also:
- Constant Field Values
-
DEFAULT_MAX_VOLUME
public static final int DEFAULT_MAX_VOLUME
- See Also:
- Constant Field Values
-
MIN_VOLUME
public static final int MIN_VOLUME
- See Also:
- Constant Field Values
-
DEFAULT_VOLUME
public static final int DEFAULT_VOLUME
- See Also:
- Constant Field Values
-
INVALID_VOLUME
public static final int INVALID_VOLUME
- See Also:
- Constant Field Values
-
FLAG_NO_ACTION
public static final int FLAG_NO_ACTION
- See Also:
- Constant Field Values
-
ALL_STREAMS
public static final com.google.common.collect.ImmutableList<Integer> ALL_STREAMS
-
-
Method Detail
-
getStreamMaxVolume
@Implementation protected int getStreamMaxVolume(int streamType)
-
getStreamVolume
@Implementation protected int getStreamVolume(int streamType)
-
getStreamVolumeDb
@Implementation(minSdk=28) protected float getStreamVolumeDb(int streamType, int index, int deviceType)
-
setStreamVolume
@Implementation protected void setStreamVolume(int streamType, int index, int flags)
-
isBluetoothScoAvailableOffCall
@Implementation protected boolean isBluetoothScoAvailableOffCall()
-
requestAudioFocus
@Implementation protected int requestAudioFocus(AudioManager.OnAudioFocusChangeListener l, int streamType, int durationHint)
-
requestAudioFocus
@Implementation(minSdk=26) 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.
-
abandonAudioFocus
@Implementation protected int abandonAudioFocus(AudioManager.OnAudioFocusChangeListener l)
-
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
@Implementation protected int getRingerMode()
-
setRingerMode
@Implementation protected void setRingerMode(int ringerMode)
-
isValidRingerMode
@Implementation public static boolean isValidRingerMode(int ringerMode)
-
setMode
@Implementation protected void setMode(int mode)
-
getMode
@Implementation protected int getMode()
-
setStreamMaxVolume
public void setStreamMaxVolume(int streamMaxVolume)
-
setStreamVolume
public void setStreamVolume(int streamVolume)
-
setWiredHeadsetOn
@Implementation protected void setWiredHeadsetOn(boolean on)
-
isWiredHeadsetOn
@Implementation protected boolean isWiredHeadsetOn()
-
setBluetoothA2dpOn
@Implementation protected void setBluetoothA2dpOn(boolean on)
-
isBluetoothA2dpOn
@Implementation protected boolean isBluetoothA2dpOn()
-
setSpeakerphoneOn
@Implementation protected void setSpeakerphoneOn(boolean on)
-
isSpeakerphoneOn
@Implementation protected boolean isSpeakerphoneOn()
-
setMicrophoneMute
@Implementation protected void setMicrophoneMute(boolean on)
-
isMicrophoneMute
@Implementation protected boolean isMicrophoneMute()
-
isBluetoothScoOn
@Implementation protected boolean isBluetoothScoOn()
-
setBluetoothScoOn
@Implementation protected void setBluetoothScoOn(boolean isBluetoothScoOn)
-
isMusicActive
@Implementation protected boolean isMusicActive()
-
getActivePlaybackConfigurations
@Implementation(minSdk=26) protected List<AudioPlaybackConfiguration> getActivePlaybackConfigurations()
-
setParameters
@Implementation protected void setParameters(String keyValuePairs)
-
getParameters
@Implementation protected String getParameters(String keys)
The expected composition for keys is not well defined.For testing purposes this method call always returns null.
-
getParameter
public String getParameter(String key)
Returns a single parameter that was set viasetParameters(String)
.
-
adjustStreamVolume
@Implementation protected void adjustStreamVolume(int streamType, int direction, int flags)
ImplementsAudioManager.adjustStreamVolume(int, int, int)
.Currently supports only the directions
AudioManager.ADJUST_MUTE
,AudioManager.ADJUST_UNMUTE
,AudioManager.ADJUST_LOWER
andAudioManager.ADJUST_RAISE
.
-
isStreamMute
@Implementation(minSdk=23) protected boolean isStreamMute(int streamType)
-
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
public void setDefaultDevicesForAttributes(com.google.common.collect.ImmutableList<Object> devices)
Sets the devices to use as default for all audio streams.
-
setActivePlaybackConfigurationsFor
public void setActivePlaybackConfigurationsFor(List<AudioAttributes> audioAttributes)
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
public ShadowAudioManager.AudioFocusRequest getLastAudioFocusRequest()
-
setNextFocusRequestResponse
public void setNextFocusRequestResponse(int nextResponseValue)
-
getLastAbandonedAudioFocusListener
public AudioManager.OnAudioFocusChangeListener getLastAbandonedAudioFocusListener()
-
getLastAbandonedAudioFocusRequest
public AudioFocusRequest 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
@HiddenApi @Implementation(minSdk=28) protected int registerAudioPolicy(Object audioPolicy)
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
@HiddenApi @Implementation(minSdk=29) protected void unregisterAudioPolicy(Object audioPolicy)
-
isAnyAudioPolicyRegistered
public boolean isAnyAudioPolicyRegistered()
Returns true if at least one audio policy is registered with this manager, and false otherwise.
-
generateAudioSessionId
@Implementation(minSdk=21) protected int 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.
-
-