fix: keep the scan's foreground service inside Android's start budget

ScanController called stopService() from a Dispatchers.Default coroutine right
after startForegroundService(), so a scan that finished at once or a Cancel tap
could beat the pending foreground start on the main thread. ScanService then
never reached startForeground() and Android killed the process a few seconds
later with
RemoteServiceException$ForegroundServiceDidNotStartInTimeException - a stack
with no app frames.

- ScanService enters the foreground in onCreate() and at the top of every
  onStartCommand(), before the Cancel early-return.
- A refused startForeground() (start not allowed, missing service type or
  permission, rejected notification) is logged instead of crashing, and a
  notification error can no longer kill the state collector.
- The service stops itself when it observes scanning == false; nothing outside
  it calls stopService() any more.
- New Robolectric ScanServiceTest covers onCreate, the idle and cancel paths, a
  failing startForeground() and the Android 15 onTimeout path.
- Version 1.1.2 / versionCode 7: versionCode 6 (1.1.1) is already on a Play
  track. CHANGELOG, AGENTS.md, codemap, ROADMAP and the release checklist
  (1.1.2 artefact record and paste-ready notes) are updated.
This commit is contained in:
2026-11-09 00:25:56 +01:00
parent 4739069737
commit 6d031082a3
9 changed files with 336 additions and 57 deletions
+14 -2
View File
@@ -178,7 +178,7 @@ sequenceDiagram
SE-->>SC: ScanProgress.Finished(ScanResult)
SC-->>VM: StateFlow<ScanState>
VM-->>UI: groups, summary, selection
SC->>SVC: stop foreground service
SVC->>SVC: sees "not scanning" and stops itself
```
### Phases
@@ -196,6 +196,16 @@ keeps going when the app is backgrounded because `ScanService` holds a `dataSync
foreground service for the duration. The ViewModel only mirrors progress; it never
owns the scan.
`ScanService` enters the foreground in `onCreate()` *and* at the top of every
`onStartCommand()`, before any early return. `startForegroundService()` obliges the
service to call `startForeground()` within a few seconds, and a start that is
cancelled (notification Cancel, in-app Cancel) or already finished is still a start.
For the same reason nothing outside the service calls `stopService()`: an external
stop can land while the start is still queued on the main thread, which leaves
`startForeground()` un-called and surfaces as
`RemoteServiceException.ForegroundServiceDidNotStartInTimeException`. The service
stops itself as soon as it observes `ScanState.scanning == false`.
Three failure boundaries matter:
- `BoundedCall` gives every provider call a deadline, so one wedged file cannot
@@ -472,7 +482,9 @@ Paths are relative to `app/src/main/java/de/softwareapp_hb/fotodup/finder/` unle
`BannerAdSlot` owns the `AdView` lifecycle and requests exactly one banner.
- **MediaDeleter** owns the system consent request. No other class may delete a
photo or bypass the two-step staging flow.
- **ScanService** mirrors `ScanController`; it contains no scanning logic.
- **ScanService** mirrors `ScanController`; it contains no scanning logic. It owns the
foreground contract (enter the foreground in `onCreate`, stop itself when idle) and
is never stopped from outside.
- **core/** stays pure Kotlin. A new Android import there is an architecture bug.
## 11. Keeping the map accurate