DefaultBandwidthMeter
@UnstableApi
public final class DefaultBandwidthMeter implements BandwidthMeter, TransferListener
Estimates bandwidth by listening to data transfers.
The bandwidth estimate is calculated using a SlidingPercentile and is updated each time a transfer ends. The initial estimate is based on the current operator's network country code or the locale of the user, as well as the network connection type. This can be configured in the Builder.
Summary
Nested types |
|---|
public final class DefaultBandwidthMeter.BuilderBuilder for a bandwidth meter. |
Constants |
|
|---|---|
static final long |
DEFAULT_INITIAL_BITRATE_ESTIMATE = 1000000Default initial bitrate estimate used when the device is offline or the network type cannot be determined, in bits per second. |
static final ImmutableList<Long> |
Default initial 2G bitrate estimates in bits per second. |
static final ImmutableList<Long> |
Default initial 3G bitrate estimates in bits per second. |
static final ImmutableList<Long> |
Default initial 4G bitrate estimates in bits per second. |
static final ImmutableList<Long> |
Default initial 5G-NSA bitrate estimates in bits per second. |
static final ImmutableList<Long> |
Default initial 5G-SA bitrate estimates in bits per second. |
static final ImmutableList<Long> |
Default initial Wifi bitrate estimate in bits per second. |
static final int |
Default maximum weight for the sliding window. |
Public fields |
|
|---|---|
long |
@GuardedBy(value = "this") |
Public methods |
|
|---|---|
void |
addEventListener(Adds an |
synchronized long |
Returns the estimated bitrate. |
synchronized static DefaultBandwidthMeter |
getSingletonInstance(Context context)Returns a singleton instance of a |
TransferListener |
Returns the |
synchronized void |
onBytesTransferred(Called incrementally during a transfer. |
synchronized void |
onTransferEnd(DataSource source, DataSpec dataSpec, boolean isNetwork)Called when a transfer ends. |
void |
onTransferInitializing(Called when a transfer is being initialized. |
synchronized void |
onTransferStart(DataSource source, DataSpec dataSpec, boolean isNetwork)Called when a transfer starts. |
void |
removeEventListener(BandwidthMeter.EventListener eventListener)Removes an |
synchronized void |
setNetworkTypeOverride(@C.NetworkType int networkType)Overrides the network type. |
Inherited methods |
||
|---|---|---|
|
Constants
DEFAULT_INITIAL_BITRATE_ESTIMATE
public static final long DEFAULT_INITIAL_BITRATE_ESTIMATE = 1000000
Default initial bitrate estimate used when the device is offline or the network type cannot be determined, in bits per second.
DEFAULT_INITIAL_BITRATE_ESTIMATES_2G
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_2G
Default initial 2G bitrate estimates in bits per second.
DEFAULT_INITIAL_BITRATE_ESTIMATES_3G
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_3G
Default initial 3G bitrate estimates in bits per second.
DEFAULT_INITIAL_BITRATE_ESTIMATES_4G
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_4G
Default initial 4G bitrate estimates in bits per second.
DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_NSA
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_NSA
Default initial 5G-NSA bitrate estimates in bits per second.
DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_SA
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_SA
Default initial 5G-SA bitrate estimates in bits per second.
DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI
Default initial Wifi bitrate estimate in bits per second.
DEFAULT_SLIDING_WINDOW_MAX_WEIGHT
public static final int DEFAULT_SLIDING_WINDOW_MAX_WEIGHT = 2000
Default maximum weight for the sliding window.
Public fields
Public methods
addEventListener
public void addEventListener(
Handler eventHandler,
BandwidthMeter.EventListener eventListener
)
Adds an EventListener.
| Parameters | |
|---|---|
Handler eventHandler |
A handler for events. |
BandwidthMeter.EventListener eventListener |
A listener of events. |
getSingletonInstance
synchronized public static DefaultBandwidthMeter getSingletonInstance(Context context)
Returns a singleton instance of a DefaultBandwidthMeter with default configuration.
| Returns | |
|---|---|
DefaultBandwidthMeter |
The singleton instance. |
getTransferListener
public TransferListener getTransferListener()
Returns the TransferListener that this instance uses to gather bandwidth information from data transfers. May be null if the implementation does not listen to data transfers.
onBytesTransferred
synchronized public void onBytesTransferred(
DataSource source,
DataSpec dataSpec,
boolean isNetwork,
int bytesTransferred
)
Called incrementally during a transfer.
| Parameters | |
|---|---|
DataSource source |
The source performing the transfer. |
DataSpec dataSpec |
Describes the data being transferred. |
boolean isNetwork |
Whether the data is transferred through a network. |
int bytesTransferred |
The number of bytes transferred since the previous call to this method. |
onTransferEnd
synchronized public void onTransferEnd(DataSource source, DataSpec dataSpec, boolean isNetwork)
Called when a transfer ends.
| Parameters | |
|---|---|
DataSource source |
The source performing the transfer. |
DataSpec dataSpec |
Describes the data being transferred. |
boolean isNetwork |
Whether the data is transferred through a network. |
onTransferInitializing
public void onTransferInitializing(
DataSource source,
DataSpec dataSpec,
boolean isNetwork
)
Called when a transfer is being initialized.
| Parameters | |
|---|---|
DataSource source |
The source performing the transfer. |
DataSpec dataSpec |
Describes the data for which the transfer is initialized. |
boolean isNetwork |
Whether the data is transferred through a network. |
onTransferStart
synchronized public void onTransferStart(DataSource source, DataSpec dataSpec, boolean isNetwork)
Called when a transfer starts.
| Parameters | |
|---|---|
DataSource source |
The source performing the transfer. |
DataSpec dataSpec |
Describes the data being transferred. |
boolean isNetwork |
Whether the data is transferred through a network. |
removeEventListener
public void removeEventListener(BandwidthMeter.EventListener eventListener)
Removes an EventListener.
| Parameters | |
|---|---|
BandwidthMeter.EventListener eventListener |
The listener to be removed. |
setNetworkTypeOverride
synchronized public void setNetworkTypeOverride(@C.NetworkType int networkType)
Overrides the network type. Handled in the same way as if the meter had detected a change from the current network type to the specified network type internally.
Applications should not normally call this method. It is intended for testing purposes.
| Parameters | |
|---|---|
@C.NetworkType int networkType |
The overriding network type. |