private void setupNetworkRequirement(Job job, JobRequest.NetworkType requirement, boolean connected, int networkType, boolean roaming) { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(connected); when(networkInfo.isConnectedOrConnecting()).thenReturn(connected); when(networkInfo.getType()).thenReturn(networkType); when(networkInfo.isRoaming()).thenReturn(roaming); ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(job.getContext().getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); when(job.getParams().getRequest().requiredNetworkType()).thenReturn(requirement); }
private Job createMockedJob() { Context context = mock(MockContext.class); JobRequest request = mock(JobRequest.class); Job.Params params = mock(Job.Params.class); when(params.getRequest()).thenReturn(request); Job job = spy(new DummyJobs.SuccessJob()); when(job.getParams()).thenReturn(params); doReturn(context).when(job).getContext(); return job; } }
@SuppressWarnings("deprecation") private void setupDeviceIdle(Job job, boolean requirement, boolean deviceIdle) { PowerManager powerManager = mock(PowerManager.class); when(powerManager.isDeviceIdleMode()).thenReturn(deviceIdle); when(powerManager.isInteractive()).thenReturn(!deviceIdle); when(powerManager.isScreenOn()).thenReturn(!deviceIdle); when(powerManager.isInteractive()).thenReturn(!deviceIdle); when(job.getParams().getRequest().requiresDeviceIdle()).thenReturn(requirement); when(job.getContext().getSystemService(Context.POWER_SERVICE)).thenReturn(powerManager); }
JobRequest request = params.getRequest(); if (result == DailyJobResult.SUCCESS) { CAT.i("Rescheduling daily job %s", request);
/*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; }
private void handleResult(Job job, Job.Result result) { JobRequest request = mJob.getParams().getRequest(); boolean incFailureCount = false; boolean updateLastRun = false; if (!request.isPeriodic() && Job.Result.RESCHEDULE.equals(result) && !job.isDeleted()) { request = request.reschedule(true, true); mJob.onReschedule(request.getJobId()); updateLastRun = true; } else if (request.isPeriodic()) { updateLastRun = true; if (!Job.Result.SUCCESS.equals(result)) { incFailureCount = true; } } if (!job.isDeleted()) { // otherwise it would be persisted again if (incFailureCount || updateLastRun) { //noinspection ConstantConditions request.updateStats(incFailureCount, updateLastRun); } } } }
/** * @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"); } }
/** * @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 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()); }
/** * @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())); }
JobRequest request = params.getRequest(); if (result == DailyJobResult.SUCCESS) { CAT.i("Rescheduling daily job %s", request);
/*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; }
private void handleResult(Job job, Job.Result result) { JobRequest request = mJob.getParams().getRequest(); boolean incFailureCount = false; boolean updateLastRun = false; if (!request.isPeriodic() && Job.Result.RESCHEDULE.equals(result) && !job.isDeleted()) { request = request.reschedule(true, true); mJob.onReschedule(request.getJobId()); updateLastRun = true; } else if (request.isPeriodic()) { updateLastRun = true; if (!Job.Result.SUCCESS.equals(result)) { incFailureCount = true; } } if (!job.isDeleted()) { // otherwise it would be persisted again if (incFailureCount || updateLastRun) { //noinspection ConstantConditions request.updateStats(incFailureCount, updateLastRun); } } } }
/** * @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"); } }
@Test public void verifyMeetsRequirementsEnforcedIgnored() { Job job = createMockedJob(); when(job.getParams().getRequest().requirementsEnforced()).thenReturn(false); setupDeviceIdle(job, true, false); setupNetworkRequirement(job, JobRequest.NetworkType.CONNECTED, false, ConnectivityManager.TYPE_WIFI, false); assertThat(job.isRequirementDeviceIdleMet()).isFalse(); assertThat(job.isRequirementNetworkTypeMet()).isFalse(); assertThat(job.meetsRequirements()).isFalse(); assertThat(job.meetsRequirements(true)).isTrue(); }
/** * @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()); }
/** * @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()); }