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();
}
}
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);
}
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();
}
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;
}
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);
}
Aggregations