@Test public void testNetworkStateNotConnected() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(false); when(networkInfo.isConnectedOrConnecting()).thenReturn(false); 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.ANY); }
/** * @return Whether the battery not low requirement is met. That's true either if it's not a requirement * or if the battery actually isn't low. The battery is low, if less than 15% are left and the device isn't * charging. */ protected boolean isRequirementBatteryNotLowMet() { return !(getParams().getRequest().requiresBatteryNotLow() && Device.getBatteryStatus(getContext()).isBatteryLow()); }
/** * @return {@code false} if the {@link Job} requires the device to be idle and it isn't idle. Otherwise * always returns {@code true}. */ protected boolean isRequirementDeviceIdleMet() { return !(getParams().getRequest().requiresDeviceIdle() && !Device.isIdle(getContext())); }
/** * @return Whether the storage not low requirement is met. That's true either if it's not a requirement * or if the storage actually isn't low. */ protected boolean isRequirementStorageNotLowMet() { return !(getParams().getRequest().requiresStorageNotLow() && Device.isStorageLow()); }
if (isRoaming(connectivityManager, networkInfo)) { return JobRequest.NetworkType.CONNECTED; } else {
/** * @return Whether the storage not low requirement is met. That's true either if it's not a requirement * or if the storage actually isn't low. */ protected boolean isRequirementStorageNotLowMet() { return !(getParams().getRequest().requiresStorageNotLow() && Device.isStorageLow()); }
@Test public void testPlatformBug() { ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(connectivityManager.getActiveNetworkInfo()).thenThrow(new NullPointerException()); Context context = mock(MockContext.class); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.ANY); } }
/** * @return {@code false} if the {@link Job} requires the device to be charging and it isn't charging. * Otherwise always returns {@code true}. */ protected boolean isRequirementChargingMet() { return !(getParams().getRequest().requiresCharging() && !Device.getBatteryStatus(getContext()).isCharging()); }
/** * @return {@code false} if the {@link Job} requires the device to be idle and it isn't idle. Otherwise * always returns {@code true}. */ protected boolean isRequirementDeviceIdleMet() { return !(getParams().getRequest().requiresDeviceIdle() && !Device.isIdle(getContext())); }
@Test public void testNetworkStateUnmeteredWifi() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); 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.UNMETERED); }
/** * @return {@code false} if the {@link Job} requires the device to be charging and it isn't charging. * Otherwise always returns {@code true}. */ protected boolean isRequirementChargingMet() { return !(getParams().getRequest().requiresCharging() && !Device.getBatteryStatus(getContext()).isCharging()); }
@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); }
/** * @return Whether the battery not low requirement is met. That's true either if it's not a requirement * or if the battery actually isn't low. The battery is low, if less than 15% are left and the device isn't * charging. */ protected boolean isRequirementBatteryNotLowMet() { return !(getParams().getRequest().requiresBatteryNotLow() && Device.getBatteryStatus(getContext()).isBatteryLow()); }
@Test public void testNetworkStateWifiAndMobile() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); when(networkInfo.isRoaming()).thenReturn(false); 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.UNMETERED); }
@Test public void testNetworkStateNotConnectedWithNullNetworkInfo() { ConnectivityManager connectivityManager = mock(ConnectivityManager.class); Context context = mock(MockContext.class); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.ANY); }
@Test public void testNetworkStateWifiAndRoaming() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); when(networkInfo.isRoaming()).thenReturn(true); 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.UNMETERED); }
@Test public void testNetworkStateRoaming() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE); when(networkInfo.isRoaming()).thenReturn(true); 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.CONNECTED); }
@Test public void testNetworkStateMeteredNotRoaming() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE); when(networkInfo.isRoaming()).thenReturn(false); 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); }
/** * @return {@code false} if the {@link Job} requires the device to be in a specific network state and it * isn't in this state. Otherwise always returns {@code true}. */ protected boolean isRequirementNetworkTypeMet() { JobRequest.NetworkType requirement = getParams().getRequest().requiredNetworkType(); if (requirement == JobRequest.NetworkType.ANY) { return true; } JobRequest.NetworkType current = Device.getNetworkType(getContext()); switch (requirement) { case CONNECTED: return current != JobRequest.NetworkType.ANY; case NOT_ROAMING: return current == JobRequest.NetworkType.NOT_ROAMING || current == JobRequest.NetworkType.UNMETERED || current == JobRequest.NetworkType.METERED; case UNMETERED: return current == JobRequest.NetworkType.UNMETERED; case METERED: return current == JobRequest.NetworkType.CONNECTED || current == JobRequest.NetworkType.NOT_ROAMING; default: throw new IllegalStateException("not implemented"); } }
/*package*/ boolean meetsRequirements(boolean checkRequirementsEnforced) { if (checkRequirementsEnforced && !getParams().getRequest().requirementsEnforced()) { return true; } if (!isRequirementChargingMet()) { CAT.w("Job requires charging, reschedule"); return false; } if (!isRequirementDeviceIdleMet()) { CAT.w("Job requires device to be idle, reschedule"); return false; } if (!isRequirementNetworkTypeMet()) { CAT.w("Job requires network to be %s, but was %s", getParams().getRequest().requiredNetworkType(), Device.getNetworkType(getContext())); return false; } if (!isRequirementBatteryNotLowMet()) { CAT.w("Job requires battery not be low, reschedule"); return false; } if (!isRequirementStorageNotLowMet()) { CAT.w("Job requires storage not be low, reschedule"); return false; } return true; }