Search in sources :

Example 1 with Network

use of android.net.Network in project robolectric by robolectric.

the class ShadowConnectivityManagerTest method getNetworkInfo_shouldReturnSomeForAllNetworks.

@Test
@Config(minSdk = LOLLIPOP)
public void getNetworkInfo_shouldReturnSomeForAllNetworks() throws Exception {
    Network[] allNetworks = connectivityManager.getAllNetworks();
    for (Network network : allNetworks) {
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
        assertThat(networkInfo).isNotNull();
    }
}
Also used : NetworkInfo(android.net.NetworkInfo) Network(android.net.Network) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 2 with Network

use of android.net.Network in project robolectric by robolectric.

the class ShadowConnectivityManagerTest method getNetworkInfo_shouldReturnAddedNetwork.

@Test
@Config(minSdk = LOLLIPOP)
public void getNetworkInfo_shouldReturnAddedNetwork() throws Exception {
    Network vpnNetwork = ShadowNetwork.newInstance(123);
    NetworkInfo vpnNetworkInfo = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_VPN, 0, true, true);
    shadowConnectivityManager.addNetwork(vpnNetwork, vpnNetworkInfo);
    NetworkInfo returnedNetworkInfo = connectivityManager.getNetworkInfo(vpnNetwork);
    assertThat(returnedNetworkInfo).isSameAs(vpnNetworkInfo);
}
Also used : NetworkInfo(android.net.NetworkInfo) Network(android.net.Network) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 3 with Network

use of android.net.Network in project Conversations by siacs.

the class DNSHelper method getDnsServers.

@TargetApi(21)
private static List<InetAddress> getDnsServers(Context context) {
    List<InetAddress> servers = new ArrayList<>();
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks();
    if (networks == null) {
        return getDnsServersPreLollipop();
    }
    for (int i = 0; i < networks.length; ++i) {
        LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]);
        if (linkProperties != null) {
            if (hasDefaultRoute(linkProperties)) {
                servers.addAll(0, getIPv4First(linkProperties.getDnsServers()));
            } else {
                servers.addAll(getIPv4First(linkProperties.getDnsServers()));
            }
        }
    }
    if (servers.size() > 0) {
        Log.d(Config.LOGTAG, "used lollipop variant to discover dns servers in " + networks.length + " networks");
    }
    return servers.size() > 0 ? servers : getDnsServersPreLollipop();
}
Also used : ConnectivityManager(android.net.ConnectivityManager) Network(android.net.Network) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) LinkProperties(android.net.LinkProperties) TargetApi(android.annotation.TargetApi)

Example 4 with Network

use of android.net.Network in project platform_frameworks_base by android.

the class ProvisionObserver method onStartJob.

@Override
public boolean onStartJob(JobParameters jobParameters) {
    switch(jobParameters.getJobId()) {
        case PROVISION_OBSERVER_REEVALUATION_JOB_ID:
            if (isProvisioned(this)) {
                Log.d(TAG, "device provisioned, force network re-evaluation");
                final ConnectivityManager connMgr = ConnectivityManager.from(this);
                Network[] info = connMgr.getAllNetworks();
                for (Network nw : info) {
                    final NetworkCapabilities nc = connMgr.getNetworkCapabilities(nw);
                    if (nc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) && nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
                        // force connectivity re-evaluation to assume skipped carrier actions.
                        // one of the following calls will match the last evaluation.
                        connMgr.reportNetworkConnectivity(nw, true);
                        connMgr.reportNetworkConnectivity(nw, false);
                        break;
                    }
                }
            }
        default:
            break;
    }
    return false;
}
Also used : ConnectivityManager(android.net.ConnectivityManager) Network(android.net.Network) NetworkCapabilities(android.net.NetworkCapabilities)

Example 5 with Network

use of android.net.Network in project platform_frameworks_base by android.

the class CaptivePortalLoginActivity method requestNetworkForCaptivePortal.

private void requestNetworkForCaptivePortal() {
    NetworkRequest request = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED).build();
    mNetworkCallback = new ConnectivityManager.NetworkCallback() {

        @Override
        public void onAvailable(Network network) {
            if (DBG)
                logd("Network available: " + network);
            mCm.bindProcessToNetwork(network);
            mNetwork = network;
            runOnUiThreadIfNotFinishing(() -> {
                // Start initial page load so WebView finishes loading proxy settings.
                // Actual load of mUrl is initiated by MyWebViewClient.
                mWebView.loadData("", "text/html", null);
            });
        }

        @Override
        public void onUnavailable() {
            if (DBG)
                logd("Network unavailable");
            runOnUiThreadIfNotFinishing(() -> {
                // Instead of not loading anything in webview, simply load the page and return
                // HTTP error page in the absence of network connection.
                mWebView.loadUrl(mUrl.toString());
            });
        }
    };
    logd("request Network for captive portal");
    mCm.requestNetwork(request, mNetworkCallback, NETWORK_REQUEST_TIMEOUT_MS);
}
Also used : ConnectivityManager(android.net.ConnectivityManager) Network(android.net.Network) NetworkRequest(android.net.NetworkRequest) NetworkCallback(android.net.ConnectivityManager.NetworkCallback)

Aggregations

Network (android.net.Network)90 SmallTest (android.test.suitebuilder.annotation.SmallTest)28 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)25 NetworkCapabilities (android.net.NetworkCapabilities)22 NetworkInfo (android.net.NetworkInfo)17 LinkProperties (android.net.LinkProperties)16 NetworkRequest (android.net.NetworkRequest)16 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)11 ConnectivityManager (android.net.ConnectivityManager)9 InetAddress (java.net.InetAddress)6 Test (org.junit.Test)6 Nullable (android.annotation.Nullable)5 NetworkMisc (android.net.NetworkMisc)5 NetworkState (android.net.NetworkState)5 WifiConfiguration (android.net.wifi.WifiConfiguration)5 WifiInfo (android.net.wifi.WifiInfo)5 ConditionVariable (android.os.ConditionVariable)5 WebSettings (android.webkit.WebSettings)5 HomeSP (com.android.hotspot2.pps.HomeSP)5 LegacyVpnInfo (com.android.internal.net.LegacyVpnInfo)5