use of android.net.Network in project NetGuard by M66B.
the class Util method getNetworkInfo.
public static String getNetworkInfo(Context context) {
StringBuilder sb = new StringBuilder();
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ani = cm.getActiveNetworkInfo();
List<NetworkInfo> listNI = new ArrayList<>();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
listNI.addAll(Arrays.asList(cm.getAllNetworkInfo()));
else
for (Network network : cm.getAllNetworks()) {
NetworkInfo ni = cm.getNetworkInfo(network);
if (ni != null)
listNI.add(ni);
}
for (NetworkInfo ni : listNI) {
sb.append(ni.getTypeName()).append('/').append(ni.getSubtypeName()).append(' ').append(ni.getDetailedState()).append(TextUtils.isEmpty(ni.getExtraInfo()) ? "" : " " + ni.getExtraInfo()).append(ni.getType() == ConnectivityManager.TYPE_MOBILE ? " " + Util.getNetworkGeneration(ni.getSubtype()) : "").append(ni.isRoaming() ? " R" : "").append(ani != null && ni.getType() == ani.getType() && ni.getSubtype() == ani.getSubtype() ? " *" : "").append("\r\n");
}
try {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
if (nis != null)
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
if (ni != null && !ni.isLoopback()) {
List<InterfaceAddress> ias = ni.getInterfaceAddresses();
if (ias != null)
for (InterfaceAddress ia : ias) sb.append(ni.getName()).append(' ').append(ia.getAddress().getHostAddress()).append('/').append(ia.getNetworkPrefixLength()).append(' ').append(ni.getMTU()).append(' ').append(ni.isUp() ? '^' : 'v').append("\r\n");
}
}
} catch (Throwable ex) {
sb.append(ex.toString()).append("\r\n");
}
if (sb.length() > 2)
sb.setLength(sb.length() - 2);
return sb.toString();
}
use of android.net.Network in project robolectric by robolectric.
the class ShadowConnectivityManagerTest method getNetworkInfo_shouldNotReturnRemovedNetwork.
@Test
@Config(minSdk = LOLLIPOP)
public void getNetworkInfo_shouldNotReturnRemovedNetwork() throws Exception {
Network wifiNetwork = ShadowNetwork.newInstance(ShadowConnectivityManager.NET_ID_WIFI);
shadowConnectivityManager.removeNetwork(wifiNetwork);
NetworkInfo returnedNetworkInfo = connectivityManager.getNetworkInfo(wifiNetwork);
assertThat(returnedNetworkInfo).isNull();
}
use of android.net.Network in project robolectric by robolectric.
the class ShadowConnectivityManagerTest method getAllNetworks_shouldReturnAddedNetworks.
@Test
@Config(minSdk = LOLLIPOP)
public void getAllNetworks_shouldReturnAddedNetworks() throws Exception {
// Let's start clear.
shadowConnectivityManager.clearAllNetworks();
// Add a "VPN network".
Network vpnNetwork = ShadowNetwork.newInstance(123);
NetworkInfo vpnNetworkInfo = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_VPN, 0, true, true);
shadowConnectivityManager.addNetwork(vpnNetwork, vpnNetworkInfo);
Network[] networks = connectivityManager.getAllNetworks();
assertThat(networks).hasSize(1);
Network returnedNetwork = networks[0];
assertThat(returnedNetwork).isSameAs(vpnNetwork);
NetworkInfo returnedNetworkInfo = connectivityManager.getNetworkInfo(returnedNetwork);
assertThat(returnedNetworkInfo).isSameAs(vpnNetworkInfo);
}
use of android.net.Network in project robolectric by robolectric.
the class ShadowConnectivityManagerTest method getAllNetworks_shouldNotReturnRemovedNetworks.
@Test
@Config(minSdk = LOLLIPOP)
public void getAllNetworks_shouldNotReturnRemovedNetworks() throws Exception {
Network wifiNetwork = ShadowNetwork.newInstance(ShadowConnectivityManager.NET_ID_WIFI);
shadowConnectivityManager.removeNetwork(wifiNetwork);
Network[] networks = connectivityManager.getAllNetworks();
assertThat(networks).hasSize(1);
Network returnedNetwork = networks[0];
ShadowNetwork shadowReturnedNetwork = Shadows.shadowOf(returnedNetwork);
assertThat(shadowReturnedNetwork.getNetId()).isNotEqualTo(ShadowConnectivityManager.NET_ID_WIFI);
}
use of android.net.Network in project android_frameworks_base by ResurrectionRemix.
the class ConnectivityServiceTest method testRequestBenchmark.
@SmallTest
public void testRequestBenchmark() throws Exception {
// Benchmarks connecting and switching performance in the presence of a large number of
// NetworkRequests.
// 1. File NUM_REQUESTS requests.
// 2. Have a network connect. Wait for NUM_REQUESTS onAvailable callbacks to fire.
// 3. Have a new network connect and outscore the previous. Wait for NUM_REQUESTS onLosing
// and NUM_REQUESTS onAvailable callbacks to fire.
// See how long it took.
final int NUM_REQUESTS = 90;
final NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build();
final NetworkCallback[] callbacks = new NetworkCallback[NUM_REQUESTS];
final CountDownLatch availableLatch = new CountDownLatch(NUM_REQUESTS);
final CountDownLatch losingLatch = new CountDownLatch(NUM_REQUESTS);
final int REGISTER_TIME_LIMIT_MS = 100;
long startTime = System.currentTimeMillis();
for (int i = 0; i < NUM_REQUESTS; i++) {
callbacks[i] = new NetworkCallback() {
@Override
public void onAvailable(Network n) {
availableLatch.countDown();
}
@Override
public void onLosing(Network n, int t) {
losingLatch.countDown();
}
};
mCm.registerNetworkCallback(request, callbacks[i]);
}
long timeTaken = System.currentTimeMillis() - startTime;
String msg = String.format("Register %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, REGISTER_TIME_LIMIT_MS);
Log.d(TAG, msg);
assertTrue(msg, timeTaken < REGISTER_TIME_LIMIT_MS);
final int CONNECT_TIME_LIMIT_MS = 30;
mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
// Don't request that the network validate, because otherwise connect() will block until
// the network gets NET_CAPABILITY_VALIDATED, after all the callbacks below have fired,
// and we won't actually measure anything.
mCellNetworkAgent.connect(false);
startTime = System.currentTimeMillis();
if (!availableLatch.await(CONNECT_TIME_LIMIT_MS, TimeUnit.MILLISECONDS)) {
fail(String.format("Only dispatched %d/%d onAvailable callbacks in %dms", NUM_REQUESTS - availableLatch.getCount(), NUM_REQUESTS, CONNECT_TIME_LIMIT_MS));
}
timeTaken = System.currentTimeMillis() - startTime;
Log.d(TAG, String.format("Connect, %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, CONNECT_TIME_LIMIT_MS));
final int SWITCH_TIME_LIMIT_MS = 30;
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
// Give wifi a high enough score that we'll linger cell when wifi comes up.
mWiFiNetworkAgent.adjustScore(40);
mWiFiNetworkAgent.connect(false);
startTime = System.currentTimeMillis();
if (!losingLatch.await(SWITCH_TIME_LIMIT_MS, TimeUnit.MILLISECONDS)) {
fail(String.format("Only dispatched %d/%d onLosing callbacks in %dms", NUM_REQUESTS - losingLatch.getCount(), NUM_REQUESTS, SWITCH_TIME_LIMIT_MS));
}
timeTaken = System.currentTimeMillis() - startTime;
Log.d(TAG, String.format("Linger, %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, SWITCH_TIME_LIMIT_MS));
final int UNREGISTER_TIME_LIMIT_MS = 10;
startTime = System.currentTimeMillis();
for (int i = 0; i < NUM_REQUESTS; i++) {
mCm.unregisterNetworkCallback(callbacks[i]);
}
timeTaken = System.currentTimeMillis() - startTime;
msg = String.format("Unregister %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, UNREGISTER_TIME_LIMIT_MS);
Log.d(TAG, msg);
assertTrue(msg, timeTaken < UNREGISTER_TIME_LIMIT_MS);
}
Aggregations