feat(service): Introduce fallback worker to ensure service stays alive#4295
Merged
Conversation
This commit introduces `ServiceKeepAliveWorker`, an expedited `CoroutineWorker`, to ensure the `MeshService` can be reliably started from the background. This worker acts as a fallback mechanism for Android 14+ devices, which may block `startForegroundService` calls due to new restrictions.
When a foreground service start is denied, the app now schedules this one-time expedited worker. The worker has temporary permission to start a foreground service, ensuring the `MeshService` is launched successfully.
### Key Changes:
- **`ServiceKeepAliveWorker`:**
- A new `HiltWorker` that runs as an expedited task to start `MeshService`.
- Displays a temporary "Resuming Mesh Service" notification while active, as required for expedited workers on newer Android versions.
- **`MeshServiceStarter.kt`:**
- When `context.startForegroundService()` fails with `ForegroundServiceStartNotAllowedException`, it now logs a warning and schedules the `ServiceKeepAliveWorker` as a fallback.
- **`AndroidManifest.xml` & `MeshService.kt`:**
- Added `FOREGROUND_SERVICE_DATA_SYNC` permission and service type to comply with foreground service requirements and align with the worker's function.
- **Crashlytics Logging (`GooglePlatformAnalytics.kt`):**
- Improved Crashlytics logging to filter out `CancellationException` and only report genuine errors or exceptions, reducing noise from standard coroutine behavior.
- Non-fatal exceptions are now only recorded for log severities of `Error` or higher, unless an explicit `Throwable` is provided.
Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4295 +/- ##
=====================================
Coverage 0.00% 0.00%
=====================================
Files 3 3
Lines 23 23
Branches 7 7
=====================================
Misses 23 23 ☔ View full report in Codecov by Sentry. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit introduces
ServiceKeepAliveWorker, an expeditedCoroutineWorker, to ensure theMeshServicecan be reliably started from the background. This worker acts as a fallback mechanism for Android 14+ devices, which may blockstartForegroundServicecalls due to new restrictions.When a foreground service start is denied, the app now schedules this one-time expedited worker. The worker has temporary permission to start a foreground service, ensuring the
MeshServiceis launched successfully.Key Changes:
ServiceKeepAliveWorker:HiltWorkerthat runs as an expedited task to startMeshService.MeshServiceStarter.kt:context.startForegroundService()fails withForegroundServiceStartNotAllowedException, it now logs a warning and schedules theServiceKeepAliveWorkeras a fallback.AndroidManifest.xml&MeshService.kt:FOREGROUND_SERVICE_DATA_SYNCpermission and service type to comply with foreground service requirements and align with the worker's function.Crashlytics Logging (
GooglePlatformAnalytics.kt):CancellationExceptionand only report genuine errors or exceptions, reducing noise from standard coroutine behavior.Erroror higher, unless an explicitThrowableis provided.