Package org.robolectric.shadows
Class ShadowConnectivityManager
java.lang.Object
org.robolectric.shadows.ShadowConnectivityManager
@Implements(android.net.ConnectivityManager.class)
public class ShadowConnectivityManager
extends Object
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
void
addNetwork
(Network network, NetworkInfo networkInfo) Adds newnetwork
to the list of allNetwork
s.protected boolean
bindProcessToNetwork
(Network network) void
Clears the list of allNetwork
s.protected Network
protected NetworkInfo
protected NetworkInfo[]
protected Network[]
protected boolean
protected Network
protected String
Returns the captive portal URL previously set withsetCaptivePortalServerUrl(java.lang.String)
.protected LinkProperties
getLinkProperties
(Network network) protected NetworkCapabilities
getNetworkCapabilities
(Network network) Gets the network capabilities of a givenNetwork
.protected NetworkInfo
getNetworkInfo
(int networkType) protected NetworkInfo
getNetworkInfo
(Network network) protected int
protected ProxyInfo
getProxyForNetwork
(Network network) Returns a proxy for a givenNetwork
.protected int
Gets the RESTRICT_BACKGROUND_STATUS value.protected boolean
CountsConnectivityManager.TYPE_MOBILE
networks as metered.protected boolean
protected void
registerDefaultNetworkCallback
(ConnectivityManager.NetworkCallback networkCallback) protected void
registerNetworkCallback
(NetworkRequest request, PendingIntent pendingIntent) protected void
registerNetworkCallback
(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback) protected void
registerNetworkCallback
(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback, Handler handler) protected void
void
removeNetwork
(Network network) Removes thenetwork
from the list of allNetwork
s.protected void
reportNetworkConnectivity
(Network network, boolean hasConnectivity) protected void
requestNetwork
(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback) protected void
requestNetwork
(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback, int timeoutMs) protected void
requestNetwork
(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback, Handler handler) protected void
requestNetwork
(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback, Handler handler, int timeoutMs) void
protected void
setAirplaneMode
(boolean enable) Sets the value for enabling/disabling airplane modevoid
setBackgroundDataSetting
(boolean b) void
setCaptivePortalServerUrl
(String captivePortalServerUrl) Sets the captive portal URL, which will be returned ingetCaptivePortalServerUrl()
.void
setDefaultNetworkActive
(boolean isActive) Sets the active state of the default network.void
setLinkProperties
(Network network, LinkProperties linkProperties) Sets the LinkProperties for the given Network.void
setNetworkCapabilities
(Network network, NetworkCapabilities networkCapabilities) Sets network capability and affects the result ofConnectivityManager.getNetworkCapabilities(Network)
void
setNetworkInfo
(int networkType, NetworkInfo networkInfo) protected void
setNetworkPreference
(int preference) void
setProxyForNetwork
(Network network, ProxyInfo proxyInfo) Sets a proxy for a givenNetwork
.void
setRestrictBackgroundStatus
(int status) Sets the next return value forConnectivityManager.getRestrictBackgroundStatus()
.protected void
unregisterNetworkCallback
(PendingIntent pendingIntent) protected void
unregisterNetworkCallback
(ConnectivityManager.NetworkCallback networkCallback)
-
Constructor Details
-
ShadowConnectivityManager
public ShadowConnectivityManager()
-
-
Method Details
-
getNetworkCallbacks
-
getNetworkCallbackPendingIntents
-
getReportedNetworkConnectivity
- Returns:
- networks and their connectivity status which was reported with
reportNetworkConnectivity(android.net.Network, boolean)
.
-
registerNetworkCallback
@Implementation(minSdk=21) protected void registerNetworkCallback(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback) -
registerNetworkCallback
@Implementation(minSdk=26) protected void registerNetworkCallback(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback, Handler handler) -
registerNetworkCallback
@Implementation(minSdk=23) protected void registerNetworkCallback(NetworkRequest request, PendingIntent pendingIntent) -
requestNetwork
@Implementation(minSdk=21) protected void requestNetwork(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback) -
requestNetwork
@Implementation(minSdk=26) protected void requestNetwork(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback, int timeoutMs) -
requestNetwork
@Implementation(minSdk=26) protected void requestNetwork(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback, Handler handler) -
requestNetwork
@Implementation(minSdk=26) protected void requestNetwork(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback, Handler handler, int timeoutMs) -
registerDefaultNetworkCallback
@Implementation(minSdk=24) protected void registerDefaultNetworkCallback(ConnectivityManager.NetworkCallback networkCallback) -
unregisterNetworkCallback
@Implementation(minSdk=21) protected void unregisterNetworkCallback(ConnectivityManager.NetworkCallback networkCallback) -
unregisterNetworkCallback
-
getActiveNetworkInfo
-
getActiveNetwork
-
getAllNetworkInfo
-
getNetworkInfo
-
getNetworkInfo
-
getAllNetworks
-
getBackgroundDataSetting
-
setNetworkPreference
-
getNetworkPreference
-
isActiveNetworkMetered
CountsConnectivityManager.TYPE_MOBILE
networks as metered. Other types will be considered unmetered.- Returns:
- true if the active network is metered, otherwise false.
- See Also:
-
bindProcessToNetwork
-
getBoundNetworkForProcess
-
setNetworkInfo
-
getCaptivePortalServerUrl
Returns the captive portal URL previously set withsetCaptivePortalServerUrl(java.lang.String)
. -
setCaptivePortalServerUrl
Sets the captive portal URL, which will be returned ingetCaptivePortalServerUrl()
.- Parameters:
captivePortalServerUrl
- the url of captive portal.
-
setBackgroundDataSetting
-
setActiveNetworkInfo
-
addNetwork
Adds newnetwork
to the list of allNetwork
s.- Parameters:
network
- The network.networkInfo
- The network info paired with theNetwork
.
-
removeNetwork
Removes thenetwork
from the list of allNetwork
s.- Parameters:
network
- The network.
-
clearAllNetworks
public void clearAllNetworks()Clears the list of allNetwork
s. -
setDefaultNetworkActive
public void setDefaultNetworkActive(boolean isActive) Sets the active state of the default network. By default this is true and affects the result ofConnectivityManager.isActiveNetworkMetered()
,ConnectivityManager.isDefaultNetworkActive()
,ConnectivityManager.getActiveNetwork()
andConnectivityManager.getAllNetworkInfo()
. Calling this method withtrue
after any listeners have been registered withConnectivityManager.addDefaultNetworkActiveListener(OnNetworkActiveListener)
will result in those listeners being fired.- Parameters:
isActive
- The active state of the default network.
-
isDefaultNetworkActive
- Returns:
- true by default, or the value specifed via
setDefaultNetworkActive(boolean)
- See Also:
-
addDefaultNetworkActiveListener
@Implementation(minSdk=21) protected void addDefaultNetworkActiveListener(ConnectivityManager.OnNetworkActiveListener l) -
removeDefaultNetworkActiveListener
@Implementation(minSdk=21) protected void removeDefaultNetworkActiveListener(ConnectivityManager.OnNetworkActiveListener l) -
reportNetworkConnectivity
@Implementation(minSdk=23) protected void reportNetworkConnectivity(Network network, boolean hasConnectivity) -
getNetworkCapabilities
Gets the network capabilities of a givenNetwork
.- Parameters:
network
- TheNetwork
object identifying the network in question.- Returns:
- The
NetworkCapabilities
for the network. - See Also:
-
setNetworkCapabilities
Sets network capability and affects the result ofConnectivityManager.getNetworkCapabilities(Network)
- Parameters:
network
- TheNetwork
object identifying the network in question.networkCapabilities
- TheNetworkCapabilities
for the network.
-
setAirplaneMode
Sets the value for enabling/disabling airplane mode- Parameters:
enable
- new status for airplane mode
-
getLinkProperties
-
setLinkProperties
Sets the LinkProperties for the given Network.A LinkProperties can be constructed by
org.robolectric.util.ReflectionHelpers.callConstructor
in tests. -
getRestrictBackgroundStatus
Gets the RESTRICT_BACKGROUND_STATUS value. Default value is 1 (RESTRICT_BACKGROUND_STATUS_DISABLED). -
setRestrictBackgroundStatus
public void setRestrictBackgroundStatus(int status) Sets the next return value forConnectivityManager.getRestrictBackgroundStatus()
. -
setProxyForNetwork
Sets a proxy for a givenNetwork
.- Parameters:
network
- The network.proxyInfo
- The proxy info.
-
getProxyForNetwork
Returns a proxy for a givenNetwork
.In order
ConnectivityManager.getDefaultProxy()
to work the default network should be set usingConnectivityManager.bindProcessToNetwork(Network)
.
-