private void handleStopMessage(Message message) { Builder builder = GooglePlayReceiver.getJobCoder().decode(message.getData()); if (builder == null) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Invalid stop execution message."); } return; } JobInvocation job = builder.build(); ExecutionDelegator.stopJob(job, true); } }
private void verifyExecuteJob(JobInvocation input) throws Exception { when(mockContext.getPackageName()).thenReturn("com.example.foo"); receiver.setLatch(new CountDownLatch(1)); when(mockContext.bindService( any(Intent.class), any(JobServiceConnection.class), eq(BIND_AUTO_CREATE))) .thenReturn(true); executionDelegator.executeJob(input); verify(mockContext) .bindService(intentCaptor.capture(), connCaptor.capture(), eq(BIND_AUTO_CREATE)); final Intent result = intentCaptor.getValue(); // verify the intent was sent to the right place assertEquals(input.getService(), result.getComponent().getClassName()); assertEquals(JobService.ACTION_EXECUTE, result.getAction()); final JobServiceConnection connection = connCaptor.getValue(); final AtomicReference<JobParameters> jobParametersAtomicReference = new AtomicReference<>(); IRemoteJobService.Stub jobServiceBinder = new IRemoteJobService.Stub() { @Override public void start(Bundle invocationData, IJobCallback callback) { jobParametersAtomicReference.set( GooglePlayReceiver.getJobCoder().decode(invocationData).build()); } @Override public void stop(Bundle invocationData, boolean needToSendResult) {} }; connection.onServiceConnected(null, jobServiceBinder); TestUtil.assertJobsEqual(input, jobParametersAtomicReference.get()); // Clean up started job. Otherwise new job won't be started. ExecutionDelegator.stopJob(input, false); }
/** * Stops the job if it is running. * * <p>Needed to avoid possibility of sending job result before the reschedule request is received * by Google Play services. */ static void onSchedule(Job job) { // Stop if running synchronized (callbacks) { SimpleArrayMap<String, JobCallback> jobs = callbacks.get(job.getService()); if (jobs == null) { // not running return; } JobCallback jobCallback = jobs.get(job.getTag()); if (jobCallback == null) { // not running return; } } JobInvocation key = new JobInvocation.Builder() .setTag(job.getTag()) .setService(job.getService()) .setTrigger(job.getTrigger()) .build(); ExecutionDelegator.stopJob(key, false /* must not send the result */); } }