adding support for below api 26 when scheduling jobs
This commit is contained in:
parent
7a025b9d9c
commit
7e82cf1569
|
@ -8,15 +8,15 @@ interface TaskRunner {
|
|||
suspend fun run(tasks: List<RunnableWorkTask>): List<TaskResult>
|
||||
|
||||
data class RunnableWorkTask(
|
||||
val source: JobWorkItem,
|
||||
val source: JobWorkItem?,
|
||||
val task: WorkTask
|
||||
)
|
||||
|
||||
sealed interface TaskResult {
|
||||
val source: JobWorkItem
|
||||
val source: JobWorkItem?
|
||||
|
||||
data class Success(override val source: JobWorkItem) : TaskResult
|
||||
data class Failure(override val source: JobWorkItem, val canRetry: Boolean) : TaskResult
|
||||
data class Success(override val source: JobWorkItem?) : TaskResult
|
||||
data class Failure(override val source: JobWorkItem?, val canRetry: Boolean) : TaskResult
|
||||
}
|
||||
|
||||
}
|
|
@ -3,6 +3,7 @@ package app.dapk.st.work
|
|||
import android.app.job.JobParameters
|
||||
import android.app.job.JobService
|
||||
import android.app.job.JobWorkItem
|
||||
import android.os.Build
|
||||
import app.dapk.st.core.extensions.Scope
|
||||
import app.dapk.st.core.extensions.unsafeLazy
|
||||
import app.dapk.st.core.module
|
||||
|
@ -24,11 +25,15 @@ class WorkAndroidService : JobService() {
|
|||
when (it) {
|
||||
is TaskRunner.TaskResult.Failure -> {
|
||||
if (!it.canRetry) {
|
||||
params.completeWork(it.source)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
params.completeWork(it.source!!)
|
||||
}
|
||||
}
|
||||
}
|
||||
is TaskRunner.TaskResult.Success -> {
|
||||
params.completeWork(it.source)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
params.completeWork(it.source!!)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -40,24 +45,37 @@ class WorkAndroidService : JobService() {
|
|||
}
|
||||
|
||||
private fun JobParameters.collectAllTasks(): List<RunnableWorkTask> {
|
||||
var work: JobWorkItem?
|
||||
val tasks = mutableListOf<RunnableWorkTask>()
|
||||
do {
|
||||
work = this.dequeueWork()
|
||||
work?.intent?.also { intent ->
|
||||
tasks.add(
|
||||
RunnableWorkTask(
|
||||
source = work,
|
||||
task = WorkTask(
|
||||
jobId = this.jobId,
|
||||
type = intent.getStringExtra("task-type")!!,
|
||||
jsonPayload = intent.getStringExtra("task-payload")!!,
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
var work: JobWorkItem?
|
||||
val tasks = mutableListOf<RunnableWorkTask>()
|
||||
do {
|
||||
work = this.dequeueWork()
|
||||
work?.intent?.also { intent ->
|
||||
tasks.add(
|
||||
RunnableWorkTask(
|
||||
source = work,
|
||||
task = WorkTask(
|
||||
jobId = this.jobId,
|
||||
type = intent.getStringExtra("task-type")!!,
|
||||
jsonPayload = intent.getStringExtra("task-payload")!!,
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
} while (work != null)
|
||||
return tasks
|
||||
} else {
|
||||
return listOf(
|
||||
RunnableWorkTask(
|
||||
source = null,
|
||||
task = WorkTask(
|
||||
jobId = this.jobId,
|
||||
type = this.extras.getString("task-type")!!,
|
||||
jsonPayload = this.extras.getString("task-payload")!!,
|
||||
)
|
||||
)
|
||||
}
|
||||
} while (work != null)
|
||||
return tasks
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStopJob(params: JobParameters): Boolean {
|
||||
|
|
|
@ -6,6 +6,7 @@ import android.app.job.JobWorkItem
|
|||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
|
||||
internal class WorkSchedulingJobScheduler(
|
||||
private val context: Context,
|
||||
|
@ -23,12 +24,17 @@ internal class WorkSchedulingJobScheduler(
|
|||
.setRequiresDeviceIdle(false)
|
||||
.build()
|
||||
|
||||
val item = JobWorkItem(
|
||||
Intent()
|
||||
.putExtra("task-type", task.type)
|
||||
.putExtra("task-payload", task.jsonPayload)
|
||||
)
|
||||
|
||||
jobScheduler.enqueue(job, item)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val item = JobWorkItem(
|
||||
Intent()
|
||||
.putExtra("task-type", task.type)
|
||||
.putExtra("task-payload", task.jsonPayload)
|
||||
)
|
||||
jobScheduler.enqueue(job, item)
|
||||
} else {
|
||||
job.extras.putString("task-type", task.type)
|
||||
job.extras.putString("task-payload", task.jsonPayload)
|
||||
jobScheduler.schedule(job)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue