@Test @Config(minSdk = LOLLIPOP) public void getLinkProperties_shouldReturnNull() { Network network = shadowOf(connectivityManager).getActiveNetwork(); shadowOf(connectivityManager).setLinkProperties(network, null); assertThat(connectivityManager.getLinkProperties(network)).isNull(); } }
@Test @Config(minSdk = LOLLIPOP) public void getLinkProperties() { Network network = shadowOf(connectivityManager).getActiveNetwork(); LinkProperties lp = ReflectionHelpers.callConstructor(LinkProperties.class); shadowOf(connectivityManager).setLinkProperties(network, lp); assertThat(connectivityManager.getLinkProperties(network)).isEqualTo(lp); }
private static Set<InetAddress> getDnsServers(Context context) throws VpnNetworkException { Set<InetAddress> out = new HashSet<>(); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(VpnService.CONNECTIVITY_SERVICE); // Seriously, Android? Seriously? NetworkInfo activeInfo = cm.getActiveNetworkInfo(); if (activeInfo == null) throw new VpnNetworkException("No DNS Server"); for (Network nw : cm.getAllNetworks()) { NetworkInfo ni = cm.getNetworkInfo(nw); if (ni == null || !ni.isConnected() || ni.getType() != activeInfo.getType() || ni.getSubtype() != activeInfo.getSubtype()) continue; for (InetAddress address : cm.getLinkProperties(nw).getDnsServers()) out.add(address); } return out; }
private Network findVpnNetwork() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = cm.getAllNetworks(); for (Network network : networks) { LinkProperties linkProperties = cm.getLinkProperties(network); List<LinkAddress> addresses = linkProperties.getLinkAddresses(); for (LinkAddress addr : addresses) { if (addr.getAddress().equals(VPN_ADDRESS)) { return network; } } } return null; }
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); for (Network network : connectivityManager.getAllNetworks()) { NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); if (networkInfo.isConnected()) { LinkProperties linkProperties = connectivityManager.getLinkProperties(network); Log.d("DnsInfo", "iface = " + linkProperties.getInterfaceName()); Log.d("DnsInfo", "dns = " + linkProperties.getDnsServers()); return linkProperties.getDnsServers(); } }
@SuppressLint("NewApi") private NetworkInformation networkToInfo(Network network) { LinkProperties linkProperties = connectivityManager.getLinkProperties(network);