Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
<application>
<activity android:name="androidx.core.TestActivity"/>
<activity android:name="androidx.core.TestPreferenceActivity"/>
<service android:name="androidx.core.content.ContextTest$Companion$TestService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
</application>
</manifest>
34 changes: 33 additions & 1 deletion src/androidTest/java/androidx/core/content/ContextTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

package androidx.core.content

import android.app.job.JobParameters
import android.app.job.JobScheduler
import android.app.job.JobService
import android.content.ContextWrapper
import android.support.test.InstrumentationRegistry
import android.support.test.filters.SdkSuppress
import androidx.core.ktx.test.R
import androidx.core.getAttributeSet
import androidx.core.ktx.test.R
import org.junit.Assert.assertEquals
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Test
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit.MILLISECONDS

class ContextTest {
private val context = InstrumentationRegistry.getContext()
Expand Down Expand Up @@ -70,4 +75,31 @@ class ContextTest {
assertTrue(getInt(R.styleable.SampleAttrs_sample, -1) != -1)
}
}

@Test fun testScheduleJob() {
val result = context.scheduleJob<TestService>(0) {
setOverrideDeadline(1)
}

latch.await(20, MILLISECONDS)

assertTrue(called)
assertEquals(JobScheduler.RESULT_SUCCESS, result)
}

companion object {
private val latch = CountDownLatch(1)
private var called = false

class TestService : JobService() {
override fun onStartJob(params: JobParameters): Boolean {
called = true
latch.countDown()
jobFinished(params, false)
return true
}

override fun onStopJob(params: JobParameters): Boolean = false
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/androidx/core/content/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package androidx.core.content

import android.app.Service
import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
Expand Down Expand Up @@ -91,3 +95,21 @@ inline fun Context.withStyledAttributes(
typedArray.recycle()
}
}

/**
* Schedules a job with the Service [T].
*
* @param jobId the id of the job being scheduled
* @param buildSequence a lambda to set the appropriate JobInfo.Builder params
*/
@RequiresApi(21)
inline fun <reified T : Service> Context.scheduleJob(
jobId: Int,
buildSequence: JobInfo.Builder.() -> Unit
): Int {
val info = JobInfo.Builder(jobId, ComponentName(this, T::class.java))
.apply(buildSequence)
.build()
val scheduler = this.getSystemService(Context.JOB_SCHEDULER_SERVICE) as? JobScheduler
return scheduler?.schedule(info) ?: JobScheduler.RESULT_FAILURE
}