Last active
July 9, 2018 02:36
-
-
Save b1acKr0se/46dd560768257ccb70b4452138c1cebb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class Job { | |
| public JobType type; | |
| public JobListener listener; | |
| abstract public void start(); | |
| public void setListener(JobListener listener) { | |
| this.listener = listener; | |
| } | |
| public void finishJob(JobReport jobReport) { | |
| if (listener != null) { | |
| listener.onJobFinished(jobReport); | |
| listener = null; | |
| } | |
| } | |
| public void publishProgress(String strProgress) { | |
| if (listener != null) { | |
| listener.publishProgress(type, strProgress); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface JobListener { | |
| void publishProgress(JobType type, String strProgress); | |
| void onJobFinished(JobReport jobReport); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class JobManager implements JobListener { | |
| private Queue<Job> jobs = new ArrayDeque<>(); | |
| private List<JobReport> jobReports = new ArrayList<>(); | |
| private Context context; | |
| public JobManager(Context context) { | |
| this.context = context; | |
| } | |
| public void addJob(Job job) { | |
| job.setListener(this); | |
| jobs.add(job); | |
| } | |
| public void startQueue() { | |
| executeHeadJob(); | |
| } | |
| private void executeHeadJob() { | |
| if (jobs.isEmpty()) { | |
| //queue is completed, notify the caller | |
| EventBus.getDefault().post(new QueueFinishEvent(jobReports)); | |
| return; | |
| } | |
| //Poll the head job in the queue, and remove it | |
| Job job = jobs.poll(); | |
| //Notify the caller that job is started | |
| EventBus.getDefault().post(new JobStartEvent(job.type)); | |
| job.start(); | |
| } | |
| @Override | |
| public void publishProgress(JobType type, String strProgress) { | |
| //progress report | |
| } | |
| @Override | |
| public void onJobFinished(JobReport jobReport) { | |
| //Add the job report to a list | |
| jobReports.add(jobReport); | |
| //Notify the caller that said job is completed | |
| EventBus.getDefault().post(new JobFinishEvent(jobReport)); | |
| //Check for new job and start | |
| executeHeadJob(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment