Skip to content

Commit 0ee1aff

Browse files
pfmaggicalren
andauthored
Update WorkManager and add InputData & Custom Configuration. (android#680)
Co-authored-by: Caren <[email protected]>
1 parent 2025538 commit 0ee1aff

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
-->
1717

1818
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
xmlns:tools="http://schemas.android.com/tools"
1920
package="com.google.samples.apps.sunflower">
2021

2122
<uses-permission android:name="android.permission.INTERNET" />
@@ -38,6 +39,11 @@
3839
<category android:name="android.intent.category.LAUNCHER" />
3940
</intent-filter>
4041
</activity>
42+
43+
<provider
44+
android:name="androidx.work.impl.WorkManagerInitializer"
45+
android:authorities="${applicationId}.workmanager-init"
46+
tools:node="remove" />
4147
</application>
4248

4349
</manifest>

app/src/main/java/com/google/samples/apps/sunflower/MainApplication.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
package com.google.samples.apps.sunflower
1818

1919
import android.app.Application
20+
import androidx.work.Configuration
2021
import dagger.hilt.android.HiltAndroidApp
2122

2223
@HiltAndroidApp
23-
class MainApplication : Application()
24+
class MainApplication : Application(), Configuration.Provider {
25+
override fun getWorkManagerConfiguration(): Configuration =
26+
Configuration.Builder()
27+
.setMinimumLoggingLevel(if (BuildConfig.DEBUG) android.util.Log.DEBUG else android.util.Log.ERROR)
28+
.build()
29+
}

app/src/main/java/com/google/samples/apps/sunflower/data/AppDatabase.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ import androidx.room.TypeConverters
2424
import androidx.sqlite.db.SupportSQLiteDatabase
2525
import androidx.work.OneTimeWorkRequestBuilder
2626
import androidx.work.WorkManager
27+
import androidx.work.workDataOf
2728
import com.google.samples.apps.sunflower.utilities.DATABASE_NAME
29+
import com.google.samples.apps.sunflower.utilities.PLANT_DATA_FILENAME
2830
import com.google.samples.apps.sunflower.workers.SeedDatabaseWorker
31+
import com.google.samples.apps.sunflower.workers.SeedDatabaseWorker.Companion.KEY_FILENAME
2932

3033
/**
3134
* The Room database for this app
@@ -55,7 +58,9 @@ abstract class AppDatabase : RoomDatabase() {
5558
object : RoomDatabase.Callback() {
5659
override fun onCreate(db: SupportSQLiteDatabase) {
5760
super.onCreate(db)
58-
val request = OneTimeWorkRequestBuilder<SeedDatabaseWorker>().build()
61+
val request = OneTimeWorkRequestBuilder<SeedDatabaseWorker>()
62+
.setInputData(workDataOf(KEY_FILENAME to PLANT_DATA_FILENAME))
63+
.build()
5964
WorkManager.getInstance(context).enqueue(request)
6065
}
6166
}

app/src/main/java/com/google/samples/apps/sunflower/workers/SeedDatabaseWorker.kt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,31 @@ import com.google.gson.reflect.TypeToken
2525
import com.google.gson.stream.JsonReader
2626
import com.google.samples.apps.sunflower.data.AppDatabase
2727
import com.google.samples.apps.sunflower.data.Plant
28-
import com.google.samples.apps.sunflower.utilities.PLANT_DATA_FILENAME
29-
import kotlinx.coroutines.coroutineScope
28+
import kotlinx.coroutines.Dispatchers
29+
import kotlinx.coroutines.withContext
3030

3131
class SeedDatabaseWorker(
32-
context: Context,
33-
workerParams: WorkerParameters
32+
context: Context,
33+
workerParams: WorkerParameters
3434
) : CoroutineWorker(context, workerParams) {
35-
override suspend fun doWork(): Result = coroutineScope {
35+
override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
3636
try {
37-
applicationContext.assets.open(PLANT_DATA_FILENAME).use { inputStream ->
38-
JsonReader(inputStream.reader()).use { jsonReader ->
39-
val plantType = object : TypeToken<List<Plant>>() {}.type
40-
val plantList: List<Plant> = Gson().fromJson(jsonReader, plantType)
37+
val filename = inputData.getString(KEY_FILENAME)
38+
if (filename != null) {
39+
applicationContext.assets.open(filename).use { inputStream ->
40+
JsonReader(inputStream.reader()).use { jsonReader ->
41+
val plantType = object : TypeToken<List<Plant>>() {}.type
42+
val plantList: List<Plant> = Gson().fromJson(jsonReader, plantType)
4143

42-
val database = AppDatabase.getInstance(applicationContext)
43-
database.plantDao().insertAll(plantList)
44+
val database = AppDatabase.getInstance(applicationContext)
45+
database.plantDao().insertAll(plantList)
4446

45-
Result.success()
47+
Result.success()
48+
}
4649
}
50+
} else {
51+
Log.e(TAG, "Error seeding database - no valid filename")
52+
Result.failure()
4753
}
4854
} catch (ex: Exception) {
4955
Log.e(TAG, "Error seeding database", ex)
@@ -53,5 +59,6 @@ class SeedDatabaseWorker(
5359

5460
companion object {
5561
private const val TAG = "SeedDatabaseWorker"
62+
const val KEY_FILENAME = "PLANT_DATA_FILENAME"
5663
}
5764
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ buildscript {
5050
testExtJunit = '1.1.0'
5151
uiAutomatorVersion = '2.2.0'
5252
viewPagerVersion = '1.0.0'
53-
workVersion = '2.1.0'
53+
workVersion = '2.4.0'
5454
}
5555

5656
repositories {

0 commit comments

Comments
 (0)