fix(tracker): harden ActivityWatcherService foreground start against platform rejection - #244
Merged
Merged
Conversation
…platform rejection ActivityWatcherService.startForegroundCompat had no exception handling around startForeground() at all, unlike TrackerService's already-hardened equivalent (tryStartForeground). This service can be started from background contexts (e.g. after activity-recognition transitions), so an Android 12+ ForegroundServiceStartNotAllowedException or a SecurityException on the declared foreground-service type would crash it with an uncaught exception. Extracts the tolerant-start logic into two top-level, directly testable functions mirroring TrackerService's pattern: isForegroundServiceStartRestriction (pure class-name/SDK check) and startForegroundTolerant (try/catch + Reporter logging). onCreate() now stops the service cleanly via stopSelf() instead of continuing setup when the foreground declaration is rejected. Supersedes PR #221 (closed, stale/conflicting): dev/v10 already contains a far more thorough version of that PR's core request (foreground-service type derived from active tracking config/tier rather than permission possession, with dedicated tests) via TrackerService.foregroundServiceTypeCandidates / resolveInitialPolicyTier. Service.onTimeout(int, int) (Android 15+) was verified inapplicable here -- it only fires for dataSync/mediaProcessing foreground-service types, not location/health/specialUse -- so it was dropped from scope rather than added as dead code. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 48e20dfd-a1bb-4266-bba5-2646e115c5cf
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.
Summary
Supersedes #221 (closed, stale/conflicting against current
dev/v10).Re-investigated the original PR's scope against current
dev/v10before writing any code:TrackerService.foregroundServiceTypeCandidates()/resolveInitialPolicyTier(), with dedicated tests (ForegroundServiceTypeCandidatesTest,TierConfigurationTest) and fullSecurityException/ForegroundServiceStartNotAllowedExceptionhandling (tryStartForeground) plus a user-visible failure notification. Nothing to port here.Service.onTimeout(int, int)(Android 15+) only fires fordataSync/mediaProcessingforeground-service types per current platform docs, notlocation/health/specialUse-- which is allTrackerServiceandActivityWatcherServicedeclare. Implementing it would be dead code the system never calls.ActivityWatcherService.startForegroundCompat()had no exception handling at all aroundstartForeground(), unlikeTrackerService's already-hardened equivalent. This service can be started from background contexts (e.g. after an activity-recognition transition), so an Android 12+ForegroundServiceStartNotAllowedExceptionor aSecurityExceptionon the declared type would crash it with an uncaught exception.Changes
ActivityWatcherService.kt, mirroringTrackerService's pattern:isForegroundServiceStartRestriction(sdkInt, exceptionClassName)-- pure string/SDK check, plain JUnit5 test (IsForegroundServiceStartRestrictionTest).startForegroundTolerant(sdkInt, tag, start)-- try/catch +Reporterlogging, Robolectric test (StartForegroundTolerantTest).onCreate()now callsstopSelf()and returns instead of continuing setup when the foreground declaration is rejected.Verification
.\gradlew.bat :tracker:engine:testDebugUnitTest(no filters, full module): BUILD SUCCESSFUL, 955 tests, 0 failures, 0 errors (includes the 7 new test cases).dev/v10tip (post source-evidence-tracking refactor); no conflicts.