Google Summer of Code - Week 10

Having explored much about the schedulers that are available in Android, I felt that there is a need to use many solutions instead of using only a single API. This is exactly what the library - Android-Job. The source can be found in GitHub. This library is created and opensourced by Evernote Corporation. After much exploration about the library and the amount of activity on github, My mentor and I have decided to go on with this library and integrate this with the Fennec. I have made many sample applications / modules understand the working of various libraries.

I have also developed a sample application that integrates Android-Job. Including the library in the build is very simple. Add the following in the Gradle file

dependencies {
    compile 'com.evernote:android-job:1.0.11'
}

This library uses AlarmManager, JobScheduler and GcmNetworkManager. There is an open issue in GitHub for integrating the Firebase JobDispatcher. I also tried to integrate this and create a pull request for the issue. But given the time constraint and complexity of adding entire support for the Firebase JobDispatcher it was difficult to stay on timeline. The library also chooses between the above mentioned API automatically based on the availability of Google Play Services and based on Android Version.

This library uses JobManager as an entry point. The JobManager is a Singleton and should be initialized in the onCreate method of Application Class.


    public class SampleSchedulerApp extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            JobManager.create(this).addJobCreator(new SampleJobCreator());
        }
    }

There is also a JobCreator Class that has to be implemented. As shown above, the JobManager must add a JobCreator implementation. Here in the JobCreator implementation, in the onCreate method we can map the tag to a specific job class.


    public class MyJobCreator implements JobCreator {

        Context context;

        public MyJobCreator() {

        }

        public MyJobCreator(Context context) {
            this.context = context;
        }

        @Override
        public Job create(String tag) {
            switch (tag) {
                case MyJob.TAG:
                    return new MyJob(context);
                default:
                    return null;
            }
        }
    }

Every task that has to be executed must extend the Job. In sample application the job I defined will do nothing but to create a log that says “Running a Scheduled Task”.


    public class MyJob extends Job {
        private static Context context;

        public static final String TAG = "MYJOB_TAG";

        public MyJob() {}

        public MyJob(Context context) {
            this.context = context;
        }


        @NonNull
        @Override
        protected Result onRunJob(Params params) {
            Log.i("MyJob", "Running a Scheduled Task");
            return Result.SUCCESS;
        }
    }

Now we have all the necessary implementation for executing a job and schedule it. So we can trigger a job in any activity. This is done using JobRequest and Builder class. The following is the code I have used to build this existing job as a periodic job which executes every minute.


    private void schedulePeriodicJob() {
        int jobId = new JobRequest.Builder(MyJob.TAG)
                .setPeriodic(60000L)
                .setPersisted(true)
                .build()
                .schedule();
    }

This method can be called in the Activity. This is how the library Android-Job works. Now lies the job of integrating the this library in to the Fennec. At present the Downloadable Content Project works without any kind of scheduling. Now we have to create a JobCreator class and make all the Download Action classes extend the Job Class to make every class as a Single task and select the triggering points for the jobs.

Updated: