Skip to content

Commit

Permalink
Merge pull request #650 from android/tj/manual-sync
Browse files Browse the repository at this point in the history
Initialize sync manually
  • Loading branch information
tunjid committed Apr 11, 2023
2 parents f3faec8 + 489502b commit 67dae0c
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 48 deletions.
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ androidx-metrics = { group = "androidx.metrics", name = "metrics-performance", v
androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "androidxNavigation" }
androidx-navigation-testing = { group = "androidx.navigation", name = "navigation-testing", version.ref = "androidxNavigation" }
androidx-profileinstaller = { group = "androidx.profileinstaller", name = "profileinstaller", version.ref = "androidxProfileinstaller" }
androidx-startup = { group = "androidx.startup", name = "startup-runtime", version.ref = "androidxStartup" }
androidx-test-core = { group = "androidx.test", name = "core", version.ref = "androidxTestCore" }
androidx-test-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidxEspresso" }
androidx-test-ext = { group = "androidx.test.ext", name = "junit-ktx", version.ref = "androidxTestExt" }
Expand Down
1 change: 0 additions & 1 deletion sync/work/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ dependencies {
implementation(project(":core:datastore"))
implementation(project(":core:model"))
implementation(libs.androidx.lifecycle.livedata.ktx)
implementation(libs.androidx.startup)
implementation(libs.androidx.tracing.ktx)
implementation(libs.androidx.work.ktx)
implementation(libs.firebase.cloud.messaging)
Expand Down
11 changes: 0 additions & 11 deletions sync/work/src/demo/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@
xmlns:tools="http://schemas.android.com/tools">

<application>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<!-- TODO: b/2173216 Disable auto sync startup till it works well with instrumented tests -->
<meta-data
android:name="com.google.samples.apps.nowinandroid.sync.initializers.SyncInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
<service
android:name=".services.SyncNotificationsService"
android:exported="false"
Expand Down
11 changes: 0 additions & 11 deletions sync/work/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@
xmlns:tools="http://schemas.android.com/tools">

<application>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<!-- TODO: b/2173216 Disable auto sync startup till it works well with instrumented tests -->
<meta-data
android:name="com.google.samples.apps.nowinandroid.sync.initializers.SyncInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
<service
android:name=".services.SyncNotificationsService"
android:exported="false">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,14 @@
package com.google.samples.apps.nowinandroid.sync.initializers

import android.content.Context
import androidx.startup.AppInitializer
import androidx.startup.Initializer
import androidx.work.ExistingWorkPolicy
import androidx.work.WorkManager
import androidx.work.WorkManagerInitializer
import com.google.samples.apps.nowinandroid.sync.workers.SyncWorker

object Sync {
// This method is a workaround to manually initialize the sync process instead of relying on
// automatic initialization with Androidx Startup. It is called from the app module's
// Application.onCreate() and should be only done once.
// This method is initializes sync, the process that keeps the app's data current.
// It is called from the app module's Application.onCreate() and should be only done once.
fun initialize(context: Context) {
AppInitializer.getInstance(context)
.initializeComponent(SyncInitializer::class.java)
}
}

// This name should not be changed otherwise the app may have concurrent sync requests running
internal const val SyncWorkName = "SyncWorkName"

/**
* Registers work to sync the data layer periodically on app startup.
*/
class SyncInitializer : Initializer<Sync> {
override fun create(context: Context): Sync {
WorkManager.getInstance(context).apply {
// Run sync on app startup and ensure only one sync worker runs at any time
enqueueUniqueWork(
Expand All @@ -50,10 +33,8 @@ class SyncInitializer : Initializer<Sync> {
SyncWorker.startUpSyncWork(),
)
}

return Sync
}

override fun dependencies(): List<Class<out Initializer<*>>> =
listOf(WorkManagerInitializer::class.java)
}

// This name should not be changed otherwise the app may have concurrent sync requests running
internal const val SyncWorkName = "SyncWorkName"

0 comments on commit 67dae0c

Please sign in to comment.