Cleanup FetchPushForegroundService (#6577)

* Rename `PushHandlingWakeLock` to `FetchPushForegroundServiceManager`. Move the start/stop logic from `FetchPushForegroundService.Companion` to it.

* Add more tests using Robolectric.

* Remove `FeatureFlags.SyncNotificationsWithWorkManager` and associated code: this should have been removed in one of the previous refactors, since we don't have the 2 ways to sync notifications anymore, everything uses the `WorkManager`

---------

Co-authored-by: Benoit Marty <benoit@matrix.org>
This commit is contained in:
Jorge Martin Espinosa 2026-04-20 16:03:12 +02:00 committed by GitHub
parent 8853f160e2
commit f80a140cf9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 329 additions and 215 deletions

View file

@ -15,7 +15,7 @@ import dev.zacsweers.metro.Inject
import io.element.android.libraries.architecture.bindings
import io.element.android.libraries.core.log.logger.LoggerTag
import io.element.android.libraries.di.annotations.AppCoroutineScope
import io.element.android.libraries.push.api.push.PushHandlingWakeLock
import io.element.android.libraries.push.api.push.FetchPushForegroundServiceManager
import io.element.android.libraries.pushproviders.api.PushHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
@ -27,7 +27,7 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() {
@Inject lateinit var firebaseNewTokenHandler: FirebaseNewTokenHandler
@Inject lateinit var pushParser: FirebasePushParser
@Inject lateinit var pushHandler: PushHandler
@Inject lateinit var pushHandlingWakeLock: PushHandlingWakeLock
@Inject lateinit var fetchPushForegroundServiceManager: FetchPushForegroundServiceManager
@AppCoroutineScope
@Inject lateinit var coroutineScope: CoroutineScope
@ -49,7 +49,7 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() {
val isHighPriority = message.priority == PRIORITY_HIGH
if (isHighPriority) {
// Acquire wakelock to ensure the device stays awake while we handle the push and schedule and run the work
pushHandlingWakeLock.lock()
fetchPushForegroundServiceManager.start()
}
coroutineScope.launch {
@ -63,7 +63,7 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() {
},
)
if (isHighPriority) {
pushHandlingWakeLock.unlock()
fetchPushForegroundServiceManager.stop()
}
} else {
val handled = pushHandler.handle(
@ -73,7 +73,7 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() {
// If we failed to handle the push, we should release the wakelock early to avoid keeping the device awake for too long.
if (!handled && isHighPriority) {
pushHandlingWakeLock.unlock()
fetchPushForegroundServiceManager.stop()
}
}
}

View file

@ -15,7 +15,7 @@ import com.google.firebase.messaging.RemoteMessage
import io.element.android.libraries.matrix.test.AN_EVENT_ID
import io.element.android.libraries.matrix.test.A_ROOM_ID
import io.element.android.libraries.matrix.test.A_SECRET
import io.element.android.libraries.push.test.push.FakePushHandlingWakeLock
import io.element.android.libraries.push.test.push.FakeFetchPushForegroundServiceManager
import io.element.android.libraries.push.test.test.FakePushHandler
import io.element.android.libraries.pushproviders.api.PushData
import io.element.android.libraries.pushproviders.api.PushHandler
@ -29,7 +29,6 @@ import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import kotlin.time.Duration
@RunWith(RobolectricTestRunner::class)
class VectorFirebaseMessagingServiceTest {
@ -81,11 +80,11 @@ class VectorFirebaseMessagingServiceTest {
@Test
fun `test pushHandler returning true locks and does not unlock the wakelock so it continues running`() = runTest {
val lockLambda = lambdaRecorder<Duration, Unit> { _ -> }
val unlockLambda = lambdaRecorder<Unit> { }
val lockLambda = lambdaRecorder<Boolean> { true }
val unlockLambda = lambdaRecorder<Boolean> { true }
val vectorFirebaseMessagingService = createVectorFirebaseMessagingService(
pushHandler = FakePushHandler(handleResult = { _, _ -> true }),
pushHandlingWakeLock = FakePushHandlingWakeLock(
pushHandlingWakeLock = FakeFetchPushForegroundServiceManager(
lock = lockLambda,
unlock = unlockLambda
)
@ -113,11 +112,11 @@ class VectorFirebaseMessagingServiceTest {
@Test
fun `test pushHandler returning false locks and unlocks the wakelock early`() = runTest {
val lockLambda = lambdaRecorder<Duration, Unit> { _ -> }
val unlockLambda = lambdaRecorder<Unit> { }
val lockLambda = lambdaRecorder<Boolean> { true }
val unlockLambda = lambdaRecorder<Boolean> { true }
val vectorFirebaseMessagingService = createVectorFirebaseMessagingService(
pushHandler = FakePushHandler(handleResult = { _, _ -> false }),
pushHandlingWakeLock = FakePushHandlingWakeLock(
pushHandlingWakeLock = FakeFetchPushForegroundServiceManager(
lock = lockLambda,
unlock = unlockLambda
)
@ -145,11 +144,11 @@ class VectorFirebaseMessagingServiceTest {
@Test
fun `test pushHandler with a remote message with normal priority won't lock the wakelock`() = runTest {
val lockLambda = lambdaRecorder<Duration, Unit> { _ -> }
val unlockLambda = lambdaRecorder<Unit> { }
val lockLambda = lambdaRecorder<Boolean> { true }
val unlockLambda = lambdaRecorder<Boolean> { true }
val vectorFirebaseMessagingService = createVectorFirebaseMessagingService(
pushHandler = FakePushHandler(handleResult = { _, _ -> false }),
pushHandlingWakeLock = FakePushHandlingWakeLock(
pushHandlingWakeLock = FakeFetchPushForegroundServiceManager(
lock = lockLambda,
unlock = unlockLambda
)
@ -186,14 +185,14 @@ class VectorFirebaseMessagingServiceTest {
private fun TestScope.createVectorFirebaseMessagingService(
firebaseNewTokenHandler: FirebaseNewTokenHandler = FakeFirebaseNewTokenHandler(),
pushHandler: PushHandler = FakePushHandler(),
pushHandlingWakeLock: FakePushHandlingWakeLock = FakePushHandlingWakeLock(),
pushHandlingWakeLock: FakeFetchPushForegroundServiceManager = FakeFetchPushForegroundServiceManager(),
): VectorFirebaseMessagingService {
return VectorFirebaseMessagingService().apply {
this.firebaseNewTokenHandler = firebaseNewTokenHandler
this.pushParser = FirebasePushParser()
this.pushHandler = pushHandler
this.coroutineScope = this@createVectorFirebaseMessagingService
this.pushHandlingWakeLock = pushHandlingWakeLock
this.fetchPushForegroundServiceManager = pushHandlingWakeLock
}
}
}