@Test public void testNetworkStateVpn() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_VPN); ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); Context context = mock(MockContext.class); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.NOT_ROAMING); }
@NonNull @SuppressWarnings("deprecation") public static JobRequest.NetworkType getNetworkType(@NonNull Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo; try { networkInfo = connectivityManager.getActiveNetworkInfo(); } catch (Throwable t) { return JobRequest.NetworkType.ANY; } if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) { return JobRequest.NetworkType.ANY; } boolean metered = ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager); if (!metered) { return JobRequest.NetworkType.UNMETERED; } if (isRoaming(connectivityManager, networkInfo)) { return JobRequest.NetworkType.CONNECTED; } else { return JobRequest.NetworkType.NOT_ROAMING; } }
Device { @NonNull @SuppressWarnings("deprecation") public static JobRequest.NetworkType getNetworkType(@NonNull Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo; try { networkInfo = connectivityManager.getActiveNetworkInfo(); } catch (Throwable t) { return JobRequest.NetworkType.ANY; } if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) { return JobRequest.NetworkType.ANY; } boolean metered = ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager); if (!metered) { return JobRequest.NetworkType.UNMETERED; } if (isRoaming(connectivityManager, networkInfo)) { return JobRequest.NetworkType.CONNECTED; } else { return JobRequest.NetworkType.NOT_ROAMING; } } }
Device { @NonNull @SuppressWarnings("deprecation") public static JobRequest.NetworkType getNetworkType(@NonNull Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo; try { networkInfo = connectivityManager.getActiveNetworkInfo(); } catch (Throwable t) { return JobRequest.NetworkType.ANY; } if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) { return JobRequest.NetworkType.ANY; } boolean metered = ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager); if (!metered) { return JobRequest.NetworkType.UNMETERED; } if (isRoaming(connectivityManager, networkInfo)) { return JobRequest.NetworkType.CONNECTED; } else { return JobRequest.NetworkType.NOT_ROAMING; } } private  Device(); }
Device { @NonNull @SuppressWarnings("deprecation") public static JobRequest.NetworkType getNetworkType(@NonNull Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo; try { networkInfo = connectivityManager.getActiveNetworkInfo(); } catch (Throwable t) { return JobRequest.NetworkType.ANY; } if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) { return JobRequest.NetworkType.ANY; } boolean metered = ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager); if (!metered) { return JobRequest.NetworkType.UNMETERED; } if (isRoaming(connectivityManager, networkInfo)) { return JobRequest.NetworkType.CONNECTED; } else { return JobRequest.NetworkType.NOT_ROAMING; } } private  Device(); @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) static BatteryStatus getBatteryStatus(Context context); @SuppressWarnings("deprecation") static boolean isIdle(Context context); @NonNull @SuppressWarnings("deprecation") static JobRequest.NetworkType getNetworkType(@NonNull Context context); static boolean isStorageLow(); }
Device { @NonNull @SuppressWarnings("deprecation") public static JobRequest.NetworkType getNetworkType(@NonNull Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo; try { networkInfo = connectivityManager.getActiveNetworkInfo(); } catch (Throwable t) { return JobRequest.NetworkType.ANY; } if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) { return JobRequest.NetworkType.ANY; } boolean metered = ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager); if (!metered) { return JobRequest.NetworkType.UNMETERED; } if (isRoaming(connectivityManager, networkInfo)) { return JobRequest.NetworkType.CONNECTED; } else { return JobRequest.NetworkType.NOT_ROAMING; } } private  Device(); @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) static BatteryStatus getBatteryStatus(Context context); @SuppressWarnings("deprecation") static boolean isIdle(Context context); @NonNull @SuppressWarnings("deprecation") static JobRequest.NetworkType getNetworkType(@NonNull Context context); static boolean isStorageLow();  }