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
+18 -5
View File
@@ -12,7 +12,7 @@ definition of done for this repository.
|---|---|
| Product | Foto Dup Finder — offline duplicate/near-duplicate photo finder |
| Model | **Freemium**: free tier with one AdMob banner, one-time Pro purchase removes ads |
| Current version | `1.1.1` / `versionCode 6` (`app/build.gradle.kts`) |
| Current version | `1.1.2` / `versionCode 7` (`app/build.gradle.kts`) |
| Platform | Android, Kotlin + Jetpack Compose, single `:app` module |
| Application id | `de.softwareapp_hb.fotodup.finder.free` (debug `.free.debug`); namespace/code package remains `de.softwareapp_hb.fotodup.finder` |
| SDKs | `minSdk 26`, `compileSdk`/`targetSdk` 36 |
@@ -30,7 +30,8 @@ Roadmap: [`docs/ROADMAP.md`](docs/ROADMAP.md)
Release checklist: [`docs/PLAY_RELEASE_CHECKLIST.md`](docs/PLAY_RELEASE_CHECKLIST.md)
Privacy policy: [`PRIVACY.md`](PRIVACY.md)
**Current gate:** the freemium release (`1.1.1`) is implemented in code, but the
**Current gate:** the freemium release (`1.1.2`, the foreground-service bugfix on top of
the `1.1.1` freemium build) is implemented in code, but the
Play Console work (change the app to free, create `pro_no_ads`, AdMob app/unit,
UMP message, Data Safety, Ads/Advertising ID declarations) is manual. Do not
describe the app as "ad-free", "no internet" or "paid once" — those claims belong
@@ -166,7 +167,9 @@ Never use `./gradlew --offline` for a fresh dependency check, and never commit
stuck provider must produce an unreadable photo, not a frozen scan.
5. **The scan is process-scoped.** `ScanController` owns the job and result; the
ViewModel mirrors `ScanState`. `ScanService` keeps a `dataSync` foreground
service alive while it runs.
service alive while it runs. It calls `startForeground()` in `onCreate()` and at
the top of every `onStartCommand()`, and **nothing outside it calls
`stopService()`** — the service stops itself when it sees `scanning == false`.
6. **Thresholds are fitted, then clamped.** `DuplicatePolicy` supplies ceilings;
`AdaptiveThreshold` fits the pHash cut to the candidate population; exact
grouping does not decode anything.
@@ -297,8 +300,9 @@ Never use `./gradlew --offline` for a fresh dependency check, and never commit
9. **AOSP emulators do not have Play services.** An empty ad slot or a "purchases
unavailable" message there is expected; it is not a code failure.
10. **`versionCode` must increase** for every Play upload. `1.0.2` used 3, the
1.1.0 internal-testing build used 4, the first 1.1.1 upload used 5, and the corrected
freemium build is 6. Update `CHANGELOG.md` and the release checklist too.
1.1.0 internal-testing build used 4, the first 1.1.1 upload used 5, the corrected
1.1.1 freemium build used 6, and the foreground-service fix is `1.1.2` / 7. Update
`CHANGELOG.md` and the release checklist too.
11. **Do not call `navigationBarsPadding()` twice.** The bottom slot owns the inset;
`BottomActionBar` must not add it again.
12. **The old paid-app docs are historical.** Do not copy "pay once", "no ads" or
@@ -308,6 +312,15 @@ Never use `./gradlew --offline` for a fresh dependency check, and never commit
removed by R8 unless `app/proguard-rules.pro` keeps it, producing a release-only
crash before `Application.onCreate`. Keep the rule and smoke-test a minified
build; debug builds do not reproduce it.
14. **Never `stopService()` a foreground start that may still be pending.** The
service's `onCreate`/`onStartCommand` run on the main thread, so a
`stopService()` issued straight after `startForegroundService()` (a scan that
finished at once, a Cancel tap, a stop from a `Dispatchers.Default` coroutine)
can win the race. The service then never calls `startForeground()` and Android
throws `RemoteServiceException$ForegroundServiceDidNotStartInTimeException` on
the app's main thread ~5–10 s later, with no app frames in the stack.
`ScanService` therefore enters the foreground in `onCreate()` and stops itself
from its own state collector; `ScanServiceTest` pins both halves down.
## 12. Definition of done