aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/notify/Poller.kt
blob: 8f5851245af59cd3b0809b13d473582f2d91d354 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)
}