Skip to content

Keep chapter downloads running after the app leaves the screen - #82

Merged
Aatricks merged 1 commit into
mainfrom
fix/download-foreground-service
Oct 31, 2026
Merged

Aatricks merged 1 commit into
mainfrom
fix/download-foreground-service

Conversation

@Aatricks

Copy link
Copy Markdown
Owner

Problem

Downloads seemed to give up partway through. On the S22, Samsung's app sleep sets the package to DISABLED_UNTIL_USED and force-stops it about 65 seconds after the app goes to the background (am_kill ... change io.aatricks.novelscraper). JobScheduler then drops the queued jobs. WorkManager still holds the work, so nothing is lost, but nothing runs until the app is opened again. The library database was not involved: on relaunch WorkManager logs "Found unfinished work, scheduling it" and the chapters pick up where they stopped.

Change

  • ChapterDownloadWorker calls setForeground with a dataSync foreground service before it waits on ChapterDownloadLimiter. Workers queued behind the limiter keep the service alive between chapters.
  • All chapter workers share one notification ID, so the user sees a single silent "Downloading chapters" notification on a low-importance channel.
  • If Android refuses the promotion because the app is already in the background, the worker logs it and downloads as a plain job instead of failing.
  • Manifest: FOREGROUND_SERVICE and FOREGROUND_SERVICE_DATA_SYNC permissions, plus a dataSync type on WorkManager's SystemForegroundService.
  • Notification strings in English and French.

Test plan

  • 648 unit tests green (testStandardDebugUnitTest), detekt clean, standard and ai flavors assemble
  • On-device (S22), release build installed in place: before the fix, the process was killed between 65 and 90 seconds after HOME, every time. With the fix, the foreground service stayed up, the package stayed enabled, the PID did not change, and 23 chapters finished during 3.5 minutes in the background.

Not covered

Three mangabats chapters (Hand Jumper 40.1, 40.5, 41.1) still fail with 0 images. Their images are served only from img-r2.2xstorage.com, which returns HTTP 503 to the app's request. Chapters on imgs-2.2xstorage.com download normally. The worker retries with backoff and then shows the failure badge, which is the intended behavior.

On Samsung devices, app sleep force-stops the process about a minute after
the app leaves the screen, and queued downloads sit idle until the next
launch. Each chapter worker now promotes itself to a dataSync foreground
service before it waits for a download slot, so the service stays up
between chapters. If Android refuses the promotion, the download runs as
a plain job.
@Aatricks
Aatricks merged commit 5a199f5 into main Oct 31, 2026
5 checks passed
@Aatricks
Aatricks deleted the fix/download-foreground-service branch October 31, 2026 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant