diff --git a/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt b/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt index 21b3fa5cb..297469355 100644 --- a/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt +++ b/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt @@ -87,7 +87,7 @@ class BackgroundServices( syncPeriodInMinutes = 240L) // four hours } - private val pushService by lazy { FirebasePush() } + val pushService by lazy { FirebasePush() } val push by lazy { makePushConfig()?.let { makePush(it) } } diff --git a/app/src/migration/java/org/mozilla/fenix/MigratingFenixApplication.kt b/app/src/migration/java/org/mozilla/fenix/MigratingFenixApplication.kt index 85a010ecc..813f9de29 100644 --- a/app/src/migration/java/org/mozilla/fenix/MigratingFenixApplication.kt +++ b/app/src/migration/java/org/mozilla/fenix/MigratingFenixApplication.kt @@ -29,6 +29,14 @@ class MigratingFenixApplication : FenixApplication() { val migrationStore by lazy { MigrationStore() } + val migrationPushSubscriber by lazy { + MigrationPushSubscriber( + this, + components.backgroundServices.pushService, + migrationStore + ) + } + override fun setupInMainProcessOnly() { // These migrations need to run before regular initialization happens. migrateBlocking() @@ -37,6 +45,7 @@ class MigratingFenixApplication : FenixApplication() { super.setupInMainProcessOnly() // The rest of the migrations can happen now. + migrationPushSubscriber.start() migrator.startMigrationIfNeeded(migrationStore, MigrationService::class.java) } diff --git a/app/src/migration/java/org/mozilla/fenix/MigrationPushSubscriber.kt b/app/src/migration/java/org/mozilla/fenix/MigrationPushSubscriber.kt new file mode 100644 index 000000000..55c8e813a --- /dev/null +++ b/app/src/migration/java/org/mozilla/fenix/MigrationPushSubscriber.kt @@ -0,0 +1,38 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix + +import android.content.Context +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.collect +import mozilla.components.concept.push.PushService +import mozilla.components.lib.state.ext.flowScoped +import mozilla.components.support.migration.state.MigrationProgress +import mozilla.components.support.migration.state.MigrationStore + +/** + * Migration-aware subscriber that disables the push service during an active migration + * and re-enables when complete. + */ +class MigrationPushSubscriber( + private val context: Context, + private val service: PushService, + private val store: MigrationStore +) { + @UseExperimental(ExperimentalCoroutinesApi::class) + fun start() { + // Stop the service if it is already started. + service.stop() + + // Observe for migration completed. + store.flowScoped { flow -> + flow.collect { state -> + if (state.progress == MigrationProgress.COMPLETED) { + service.start(context) + } + } + } + } +}