private synchronized void stopJob(boolean needToSendResult, JobInvocation jobInvocation) { try { binder.stop(encodeJob(jobInvocation), needToSendResult); } catch (RemoteException remoteException) { Log.e(TAG, "Failed to stop a job", remoteException); unbind(); } }
private void requestRetryForJob(JobInvocation job) { try { callback.jobFinished(encodeJob(job), JobService.RESULT_FAIL_RETRY); } catch (RemoteException e) { Log.e(TAG, "Error sending result for job " + job.getTag() + ": " + e); } } }
@Override public synchronized void onServiceConnected(ComponentName name, IBinder service) { if (wasUnbound()) { Log.w(TAG, "Connection have been used already."); return; } binder = IRemoteJobService.Stub.asInterface(service); Set<JobInvocation> startedJobs = new HashSet<>(); for (Entry<JobInvocation, Boolean> entry : jobStatuses.entrySet()) { if (Boolean.FALSE.equals(entry.getValue())) { try { binder.start(encodeJob(entry.getKey()), callback); startedJobs.add(entry.getKey()); } catch (RemoteException remoteException) { Log.e(TAG, "Failed to start job " + entry.getKey(), remoteException); unbind(); // TODO(user) notify a driver about the fail and release a wakelock. return; } } } // Mark jobs as started. for (JobInvocation invocation : startedJobs) { jobStatuses.put(invocation, true); } }
/** Returns {@code true} if the job was started. */ synchronized boolean startJob(JobInvocation jobInvocation) { if (wasUnbound()) { requestRetryForJob(jobInvocation); } boolean connected = isConnected(); if (connected) { // Need to stop running job Boolean isRunning = jobStatuses.get(jobInvocation); if (Boolean.TRUE.equals(isRunning)) { Log.w(TAG, "Received an execution request for already running job " + jobInvocation); stopJob(/* Do not send result because it is new execution request. */ false, jobInvocation); } try { binder.start(encodeJob(jobInvocation), callback); } catch (RemoteException e) { Log.e(TAG, "Failed to start the job " + jobInvocation, e); unbind(); return false; } } jobStatuses.put(jobInvocation, connected); return connected; }