This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HelloWorldWorker @Inject constructor( | |
private val appContext: Context, | |
private val params: WorkerParameters, | |
private val foo: Foo // test dependence | |
// add more dependencies here | |
) : Worker(appContext, params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HelloWorldWorker( | |
appContext: Context, | |
params: WorkerParameters | |
) : Worker(appContext, params) { | |
@Inject lateinit var foo: Foo | |
override fun doWork(): Result { | |
TODO() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ChildWorkerFactory { | |
fun create(appContext: Context, params: WorkerParameters): ListenableWorker | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo @Inject constructor() // test dependence | |
class HelloWorldWorker( | |
private val appContext: Context, | |
private val params: WorkerParameters, | |
private val foo: Foo // test dependence | |
// add more dependencies here | |
) : Worker(appContext, params) { | |
override fun doWork(): Result { | |
TODO() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MapKey | |
@Target(AnnotationTarget.FUNCTION) | |
@Retention(AnnotationRetention.RUNTIME) | |
annotation class WorkerKey(val value: KClass<out ListenableWorker>) | |
@Module | |
interface WorkerBindingModule { | |
@Binds | |
@IntoMap | |
@WorkerKey(HelloWorldWorker::class) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleWorkerFactory @Inject constructor( | |
private val workerFactories: Map<Class<out ListenableWorker>, @JvmSuppressWildcards Provider<ChildWorkerFactory>> | |
) : WorkerFactory() { | |
override fun createWorker( | |
appContext: Context, | |
workerClassName: String, | |
workerParameters: WorkerParameters | |
): ListenableWorker? { | |
val foundEntry = | |
workerFactories.entries.find { Class.forName(workerClassName).isAssignableFrom(it.key) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HelloWorldWorker @AssistedInject constructor( | |
@Assisted private val params: WorkerParameters, | |
private val appContext: Context, | |
private val foo: Foo | |
) : Worker(appContext, params) { | |
private val TAG = "HelloWorldWorker" | |
override fun doWork(): Result { | |
Log.d(TAG, "Hello world!") | |
Log.d(TAG, "Injected foo: $foo") | |
return Result.success() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module(includes = [AssistedInject_SampleAssistedInjectModule::class]) | |
@AssistedModule | |
interface SampleAssistedInjectModule | |
@Component( | |
modules = [ | |
SampleAssistedInjectModule::class, | |
WorkerBindingModule::class | |
] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleApplication : DaggerApplication() { | |
@Inject lateinit var workerFactory: WorkerFactory | |
override fun onCreate() { | |
super.onCreate() | |
// register ours custom factory to WorkerManager | |
WorkManager.initialize(this, Configuration.Builder().setWorkerFactory(factory).build()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
package="com.sample.daggerworkmanagersample"> | |
// Note: the content has been shortened, demonstrate how to register custom WorkerFactory only | |
<application android:name=".SampleApplication"> | |
<provider |
OlderNewer