package org.meshbay.client.notify import android.app.job.JobInfo import android.app.job.JobScheduler import android.content.ComponentName import android.content.Context /** * The periodic fetch, through the system's own job scheduler: no library and * nothing the platform does not already run. Android decides the exact moment * (fifteen minutes is the shortest period it allows, and Doze stretches it); * a phone that also gets pushes keeps a slow fetch as a net under them. */ object Poller { private const val JOB_ID = 4208 private const val FETCHING_MS = 15L * 60 * 1000 private const val UNDER_PUSH_MS = 4L * 3600 * 1000 fun schedule(context: Context, pushed: Boolean) { val job = JobInfo.Builder(JOB_ID, ComponentName(context, PollJob::class.java)) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setPeriodic(if (pushed) UNDER_PUSH_MS else FETCHING_MS) .setPersisted(true) .build() context.getSystemService(JobScheduler::class.java).schedule(job) } fun cancel(context: Context) = context.getSystemService(JobScheduler::class.java).cancel(JOB_ID) }