Class ShadowConnectivityManager


  • @Implements(android.net.ConnectivityManager.class)
    public class ShadowConnectivityManager
    extends java.lang.Object
    • Constructor Detail

      • ShadowConnectivityManager

        public ShadowConnectivityManager()
    • Method Detail

      • getNetworkCallbacks

        public java.util.Set<android.net.ConnectivityManager.NetworkCallback> getNetworkCallbacks()
      • registerNetworkCallback

        @Implementation(minSdk=21)
        protected void registerNetworkCallback​(android.net.NetworkRequest request,
                                               android.net.ConnectivityManager.NetworkCallback networkCallback)
      • registerNetworkCallback

        @Implementation(minSdk=26)
        protected void registerNetworkCallback​(android.net.NetworkRequest request,
                                               android.net.ConnectivityManager.NetworkCallback networkCallback,
                                               android.os.Handler handler)
      • requestNetwork

        @Implementation(minSdk=21)
        protected void requestNetwork​(android.net.NetworkRequest request,
                                      android.net.ConnectivityManager.NetworkCallback networkCallback)
      • registerDefaultNetworkCallback

        @Implementation(minSdk=24)
        protected void registerDefaultNetworkCallback​(android.net.ConnectivityManager.NetworkCallback networkCallback)
      • unregisterNetworkCallback

        @Implementation(minSdk=21)
        protected void unregisterNetworkCallback​(android.net.ConnectivityManager.NetworkCallback networkCallback)
      • getActiveNetworkInfo

        @Implementation
        protected android.net.NetworkInfo getActiveNetworkInfo()
      • getNetworkInfo

        @Implementation
        protected android.net.NetworkInfo getNetworkInfo​(int networkType)
      • getNetworkInfo

        @Implementation(minSdk=21)
        protected android.net.NetworkInfo getNetworkInfo​(android.net.Network network)
      • getAllNetworks

        @Implementation(minSdk=21)
        protected android.net.Network[] getAllNetworks()
      • getBackgroundDataSetting

        @Implementation
        protected boolean getBackgroundDataSetting()
      • setNetworkPreference

        @Implementation
        protected void setNetworkPreference​(int preference)
      • getNetworkPreference

        @Implementation
        protected int getNetworkPreference()
      • isActiveNetworkMetered

        @Implementation
        protected boolean isActiveNetworkMetered()
        Counts ConnectivityManager.TYPE_MOBILE networks as metered. Other types will be considered unmetered.
        Returns:
        true if the active network is metered, otherwise false.
        See Also:
        setActiveNetworkInfo(NetworkInfo), setDefaultNetworkActive(boolean)
      • bindProcessToNetwork

        @Implementation(minSdk=23)
        protected boolean bindProcessToNetwork​(android.net.Network network)
      • getBoundNetworkForProcess

        @Implementation(minSdk=23)
        protected android.net.Network getBoundNetworkForProcess()
      • setNetworkInfo

        public void setNetworkInfo​(int networkType,
                                   android.net.NetworkInfo networkInfo)
      • setCaptivePortalServerUrl

        public void setCaptivePortalServerUrl​(java.lang.String captivePortalServerUrl)
        Sets the captive portal URL, which will be returned in getCaptivePortalServerUrl().
        Parameters:
        captivePortalServerUrl - the url of captive portal.
      • setBackgroundDataSetting

        @HiddenApi
        @Implementation
        public void setBackgroundDataSetting​(boolean b)
      • setActiveNetworkInfo

        public void setActiveNetworkInfo​(android.net.NetworkInfo info)
      • addNetwork

        public void addNetwork​(android.net.Network network,
                               android.net.NetworkInfo networkInfo)
        Adds new network to the list of all Networks.
        Parameters:
        network - The network.
        networkInfo - The network info paired with the Network.
      • removeNetwork

        public void removeNetwork​(android.net.Network network)
        Removes the network from the list of all Networks.
        Parameters:
        network - The network.
      • clearAllNetworks

        public void clearAllNetworks()
        Clears the list of all Networks.
      • setDefaultNetworkActive

        public void setDefaultNetworkActive​(boolean isActive)
        Sets the active state of the default network. By default this is true and affects the result of ConnectivityManager.isActiveNetworkMetered(), ConnectivityManager.isDefaultNetworkActive(), ConnectivityManager.getActiveNetwork() and ConnectivityManager.getAllNetworkInfo(). Calling this method with true after any listeners have been registered with ConnectivityManager.addDefaultNetworkActiveListener(OnNetworkActiveListener) will result in those listeners being fired.
        Parameters:
        isActive - The active state of the default network.
      • addDefaultNetworkActiveListener

        @Implementation(minSdk=21)
        protected void addDefaultNetworkActiveListener​(android.net.ConnectivityManager.OnNetworkActiveListener l)
      • removeDefaultNetworkActiveListener

        @Implementation(minSdk=21)
        protected void removeDefaultNetworkActiveListener​(android.net.ConnectivityManager.OnNetworkActiveListener l)
      • reportNetworkConnectivity

        @Implementation(minSdk=23)
        protected void reportNetworkConnectivity​(android.net.Network network,
                                                 boolean hasConnectivity)
      • getNetworkCapabilities

        @Implementation(minSdk=21)
        protected android.net.NetworkCapabilities getNetworkCapabilities​(android.net.Network network)
        Gets the network capabilities of a given Network.
        Parameters:
        network - The Network object identifying the network in question.
        Returns:
        The NetworkCapabilities for the network.
        See Also:
        setNetworkCapabilities(Network, NetworkCapabilities)
      • setNetworkCapabilities

        public void setNetworkCapabilities​(android.net.Network network,
                                           android.net.NetworkCapabilities networkCapabilities)
        Sets network capability and affects the result of ConnectivityManager.getNetworkCapabilities(Network)
        Parameters:
        network - The Network object identifying the network in question.
        networkCapabilities - The NetworkCapabilities for the network.
      • setAirplaneMode

        @Implementation(minSdk=19)
        protected void setAirplaneMode​(boolean enable)
        Sets the value for enabling/disabling airplane mode
        Parameters:
        enable - new status for airplane mode
      • setLinkProperties

        public void setLinkProperties​(android.net.Network network,
                                      android.net.LinkProperties linkProperties)
        Sets the LinkProperties for the given Network.

        A LinkProperties can be constructed by org.robolectric.util.ReflectionHelpers.callConstructor in tests.

      • getRestrictBackgroundStatus

        @Implementation(minSdk=24)
        protected int 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 for ConnectivityManager.getRestrictBackgroundStatus().
      • setProxyForNetwork

        public void setProxyForNetwork​(android.net.Network network,
                                       android.net.ProxyInfo proxyInfo)
        Sets a proxy for a given Network.
        Parameters:
        network - The network.
        proxyInfo - The proxy info.
      • getProxyForNetwork

        @Implementation(minSdk=23)
        protected android.net.ProxyInfo getProxyForNetwork​(android.net.Network network)
        Returns a proxy for a given Network.

        In order ConnectivityManager.getDefaultProxy() to work the default network should be set using ConnectivityManager.bindProcessToNetwork(Network).