From 7e82cf15691dd33555d6c6a5a419f1968969e094 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 17 Mar 2022 19:35:10 +0000 Subject: [PATCH] adding support for below api 26 when scheduling jobs --- .../kotlin/app/dapk/st/work/TaskRunner.kt | 8 +-- .../app/dapk/st/work/WorkAndroidService.kt | 52 +++++++++++++------ .../st/work/WorkSchedulingJobScheduler.kt | 20 ++++--- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/domains/android/work/src/main/kotlin/app/dapk/st/work/TaskRunner.kt b/domains/android/work/src/main/kotlin/app/dapk/st/work/TaskRunner.kt index a1dff08..e60c0ac 100644 --- a/domains/android/work/src/main/kotlin/app/dapk/st/work/TaskRunner.kt +++ b/domains/android/work/src/main/kotlin/app/dapk/st/work/TaskRunner.kt @@ -8,15 +8,15 @@ interface TaskRunner { suspend fun run(tasks: List): List 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 } } \ No newline at end of file diff --git a/domains/android/work/src/main/kotlin/app/dapk/st/work/WorkAndroidService.kt b/domains/android/work/src/main/kotlin/app/dapk/st/work/WorkAndroidService.kt index e712235..edb4b38 100644 --- a/domains/android/work/src/main/kotlin/app/dapk/st/work/WorkAndroidService.kt +++ b/domains/android/work/src/main/kotlin/app/dapk/st/work/WorkAndroidService.kt @@ -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 { - var work: JobWorkItem? - val tasks = mutableListOf() - 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() + 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 { diff --git a/domains/android/work/src/main/kotlin/app/dapk/st/work/WorkSchedulingJobScheduler.kt b/domains/android/work/src/main/kotlin/app/dapk/st/work/WorkSchedulingJobScheduler.kt index b4a70ef..ce93d61 100644 --- a/domains/android/work/src/main/kotlin/app/dapk/st/work/WorkSchedulingJobScheduler.kt +++ b/domains/android/work/src/main/kotlin/app/dapk/st/work/WorkSchedulingJobScheduler.kt @@ -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) + } } }