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

@ -14,7 +14,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 io.element.android.libraries.pushproviders.unifiedpush.registration.EndpointRegistrationHandler
import io.element.android.libraries.pushproviders.unifiedpush.registration.RegistrationResult
@ -38,7 +38,7 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
@Inject lateinit var newGatewayHandler: UnifiedPushNewGatewayHandler
@Inject lateinit var removedGatewayHandler: UnifiedPushRemovedGatewayHandler
@Inject lateinit var endpointRegistrationHandler: EndpointRegistrationHandler
@Inject lateinit var pushHandlingWakeLock: PushHandlingWakeLock
@Inject lateinit var fetchPushForegroundServiceManager: FetchPushForegroundServiceManager
@AppCoroutineScope
@Inject lateinit var coroutineScope: CoroutineScope
@ -59,8 +59,8 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
* @param instance connection, for multi-account
*/
override fun onMessage(context: Context, message: PushMessage, instance: String) {
// Acquire wakelock to ensure the device stays awake while we handle the push and schedule and run the work
pushHandlingWakeLock.lock()
// Start the foreground service to ensure the device stays awake while we handle the push and schedule and run the work.
fetchPushForegroundServiceManager.start()
Timber.tag(loggerTag.value).d("New message, decrypted: ${message.decrypted}")
coroutineScope.launch {
@ -71,16 +71,16 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
providerInfo = "${UnifiedPushConfig.NAME} - $instance",
data = String(message.content),
)
pushHandlingWakeLock.unlock()
fetchPushForegroundServiceManager.stop()
} else {
val handled = pushHandler.handle(
pushData = pushData,
providerInfo = "${UnifiedPushConfig.NAME} - $instance",
)
// If we failed to handle the push, we should release the wakelock early to avoid keeping the device awake for too long.
// If we failed to handle the push, we should stop the foreground service early to avoid keeping the device awake for too long.
if (!handled) {
pushHandlingWakeLock.unlock()
fetchPushForegroundServiceManager.stop()
}
}
}