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
+55 -12
View File
@@ -12,15 +12,16 @@ the rest are things you must do in the Play Console / with your own accounts.
`namespace` deliberately stay `de.softwareapp_hb.fotodup.finder`, and debug builds
append `.debug` → `de.softwareapp_hb.fotodup.finder.free.debug`.
- [x] Generate an **upload keystore** (keep it safe; if you lose it you cannot
publish updates) — **[code]** done: `fotodup-upload.jks` in the project root
(git-ignored, `alias upload`), `CN=Hadrian Burkhardt, L=Potsdam, C=DE`.
publish updates) — **[code]** done: `fotodup-upload.jks` in the central keystore
folder `/home/ubuntu/keystore/` (outside the repo, mode `600`, shared with the paid
listing, `alias upload`), `CN=Hadrian Burkhardt, L=Potsdam, C=DE`.
- [x] Copy `keystore.properties.example` to `keystore.properties` and fill it in.
**[code]** done — the file is in place, and the Gradle build picks it up automatically and
signs release builds; without it the release APK stays unsigned.
- [ ] Enable **Play App Signing** for the **new** app in the Play Console and upload the
certificate of your *upload* key (the same upload key can sign both listings):
```bash
keytool -exportcert -alias upload -keystore fotodup-upload.jks -rfc > upload_cert.pem
keytool -exportcert -alias upload -keystore /home/ubuntu/keystore/fotodup-upload.jks -rfc > upload_cert.pem
```
Upload key fingerprint (SHA-256), to confirm the right key was registered:
`AA:8C:3D:6C:D0:55:46:50:48:ED:04:3E:AF:34:2B:66:B4:13:87:C9:E0:DA:29:70:5A:0D:18:47:08:1A:4C:7A`.
@@ -30,11 +31,12 @@ the rest are things you must do in the Play Console / with your own accounts.
## 2. Versioning
- [x] Set `versionName` (user-visible) **[code]** — `1.1.1` in `app/build.gradle.kts`.
- [x] `versionCode` **[code]** — currently **6**. It is the counter Play orders uploads by, and
- [x] Set `versionName` (user-visible) **[code]** — `1.1.2` in `app/build.gradle.kts`.
- [x] `versionCode` **[code]** — currently **7**. It is the counter Play orders uploads by, and
every later upload must be higher. Codes 1–3 were consumed by 1.0.0–1.0.2, **4** by
the 1.1.0 internal-testing build (which crashed on startup), and **5** by an earlier
1.1.1 upload, so the corrected freemium build takes **6**.
the 1.1.0 internal-testing build (which crashed on startup), **5** by an earlier
1.1.1 upload, and **6** by the corrected freemium build that is already on a track, so
this foreground-service fix takes **7**.
## 3. Build the release artefacts
@@ -59,6 +61,16 @@ the rest are things you must do in the Play Console / with your own accounts.
APK (`sha256 6043ec92…`) / 7,752,264-byte AAB (`sha256 4766ca49…`); `dexdump`
confirms the `WorkDatabase_Impl` public no-arg constructor is present. Rebuild
and re-measure if the ids or cutoff change.
- [x] **Build and record the 1.1.2 artefacts** (the `ForegroundServiceDidNotStartInTime`
fix; same real ids and cutoff as 1.1.1, so the size moved by the code alone): a
**3,688,330**-byte APK (`sha256 5d43c515…`) and a **7,753,331**-byte AAB
(`sha256 51c83829…`), signed with the upload key from step 1
(`CN=Hadrian Burkhardt`, SHA-256 `AA:8C:3D:…:1A:4C:7A`) and R8-minified.
`jarsigner -verify` says *jar verified* for the `.aab`; `aapt2 dump badging`
reports package `de.softwareapp_hb.fotodup.finder.free`,
`versionCode=7 versionName=1.1.2 targetSdk=36`; the merged release manifest
contains no AdMob test id. Rebuild and re-measure after any code, id or cutoff
change.
- [ ] **Launch the minified build before uploading.** Do not rely on
`assembleDebug`/unit tests: they do not run R8. Install the release APK on a
real device (or a temporarily minified debug build side by side) and confirm it
@@ -100,9 +112,15 @@ action except the items marked **[code]**.
2. *Monetise > Products > In-app products* → create **`pro_no_ads`**, type **One-time
product**, set the price, **Activate** it. The product id must match
`ProProduct.ID` exactly; an inactive product is not returned by Play.
3. AdMob → create/link the Android app, one **Banner** unit and a **UMP message**.
Put the ids in `local.properties` (`admob.appId`, `admob.bannerUnitId`) or pass
`-Padmob.appId=...`; never commit them.
3. AdMob → link the Android app (`…finder.free`), create one **Banner** unit, and
**publish** a UMP message under *Privacy & messaging* (the European-regulations
message that *Maximize message coverage* auto-creates is fine once it is
published, not a draft). Put the ids in `local.properties` (`admob.appId`,
`admob.bannerUnitId`) or pass `-Padmob.appId=...`; never commit them. Host
`app-ads.txt` at the root of the developer website listed on the Play listing.
App verification is followed by a separate AdMob ad-serving review (2–3 days,
sometimes longer) with **limited serving** until it clears: an empty banner in
that window is expected, not a code defect.
4. Set `legacyPro.cutoffEpochMs` to the instant of the first freemium release.
Installs older than that are grandfathered into Pro; a `0` cutoff disables the
migration. Because the new app has a different applicationId, this only works
@@ -462,7 +480,11 @@ action except the items marked **[code]**.
virtual instruction counter dilates the guest clock and made it work exactly once
(`-qemu -icount shift=1,sleep=off`, guest clock measured 2.30× slow) but not
reproducibly, so that is a lead rather than a recipe; `SETTLE_SECONDS` / `WARM_SECONDS`
scale this script's waits if it is ever retried.
scale this script's waits if it is ever retried. Note that part of that crash was a real
app bug (see pitfall 14 in `AGENTS.md`): a scan that reports idle straight away used to
let `stopService()` beat the pending foreground start, so `startForeground()` was never
called. Since the fix, that specific race no longer occurs and `ScanServiceTest` covers
it, but the emulator remains too slow to prove a *device* recording.
The declaration is **per type**: if a new foreground-service type is ever added to
the manifest, it has to be declared here too.
@@ -538,7 +560,9 @@ action except the items marked **[code]**.
## 7. Release notes ("What's new") and rollout
⚠️ **Which block applies:** the freemium app is a **new `.free` listing**, so its first
release uses the “new freemium listing” block below. The 1.0.2 and initial-release
release uses the “new freemium listing” block below. **The current upload is the 1.1.2
update, so use the 1.1.2 block** (its notes belong to that release; the first-release
block stays here for the listing's initial submission). The 1.0.2 and initial-release
blocks are kept for the history of the old paid listing; the old initial block still
says “no ads”, which was true only of the paid build. The 1.1.0-update block has been
replaced because the plan is a new listing, not an in-place price change.
@@ -565,6 +589,25 @@ Rules (per Google's *Prepare and roll out a release* guide):
- For app updates list only user-visible changes; the full developer history belongs
in [CHANGELOG.md](../CHANGELOG.md).
Ready-to-paste block for the **1.1.2 update** (`versionCode` 7) — the foreground-service
fix (223 chars):
<en-US>
Fixed a crash that could happen right after starting or cancelling a scan.
• The scan notification now appears immediately, so a running scan keeps going while you use other apps
• Cancelling a scan no longer stops the app
</en-US>
Optional German block for the same update (252 chars):
<de-DE>
Ein Absturz direkt nach dem Starten oder Abbrechen eines Scans ist behoben.
• Die Scan-Benachrichtigung erscheint jetzt sofort, damit ein laufender Scan weiterläuft, während du andere Apps benutzt
• Das Abbrechen eines Scans beendet die App nicht mehr
</de-DE>
Ready-to-paste block for the **new freemium listing's first release** (313 chars):
<en-US>
+4 -3
View File
@@ -48,9 +48,10 @@ Legend: ✅ done · 🚧 in progress · ⏳ not started · 💡 candidate (propo
## Suggested order
1. ✅ **Done** — the scan-stall hardening and the `BoundedCall` fix shipped as **1.0.1**
(`versionCode` 2); **1.0.2** (`versionCode` 3) was the last paid build. The next upload is
**1.1.1** (`versionCode` 6), the fixed freemium build, which also needs the Play Console changes in
[MONETIZATION](MONETIZATION.md). Only the on-device re-check of the 5,363-photo scan remains.
(`versionCode` 2); **1.0.2** (`versionCode` 3) was the last paid build. The freemium
listing went out as **1.1.1** (`versionCode` 6); the current upload is **1.1.2**
(`versionCode` 7), the `ForegroundServiceDidNotStartInTime` fix, which still needs the
Play Console changes in [MONETIZATION](MONETIZATION.md).
2. Finish the Play Console paperwork and roll out through **internal → closed → production**.
3. ✅ **Done** — **M10 store assets**: the icon and feature graphic are generated from the
app's own vectors, the screenshot normaliser enforces Play's 2:1 limit, the listing copy is
+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