Skip to content

Android: model download resumes instead of failing in the background - #2300

Merged
R44VC0RP merged 2 commits into
devfrom
android-model-download-resumes
Nov 22, 2026
Merged

R44VC0RP merged 2 commits into
devfrom
android-model-download-resumes

Conversation

@R44VC0RP

Copy link
Copy Markdown
Collaborator

Setup's Get started starts the 190 MB Whisper small download and then sends the person to Android's Settings. On Android 15+ the system closes a backgrounded app's sockets about 5 s later, so setup said the download stopped and Try again started from 0.

Now: the partial file is kept and the download continues with an HTTP Range request. A connection that drops while Hex is in the background counts as a pause, not an error. The download picks up again as soon as a Hex screen is in front, including after a swipe from recents or a reboot. Real failures say what went wrong: "No connection", "Not enough storage" or "Download stopped" (server or checksum), each with Try again, which also resumes.

  • ModelStore keeps whisper-small-<sha256>.part and re-hashes the kept bytes, so the final byte count and SHA-256 check is unchanged. If the server ignores the range (200), sends the wrong range (206) or answers 416 for an incomplete file, the download restarts from 0. A resumed file that fails the checksum is deleted and fetched once more from 0.
  • Foreground tracking uses ActivityLifecycleCallbacks, registered from ModelStore.refresh(). MainActivity's lifecycle is unchanged.
  • State.Retry is now Retry(problem). Setup-screen lines touched: modelStatus() and modelAction() in MainActivity.kt (about 5 lines), plus 2 strings each in values and values-zh-rCN.
  • No new permissions (still only RECORD_AUDIO and INTERNET), no foreground service, no new dependencies.

Verified on API 36 and API 31 emulators with the debug build, on top of #2291's flow (Turn on, Hex switched on in Settings, picker on return, Hex picked). Every run finished with the right SHA-256:

Case API 36 API 31
Settings 10 s / 30 s / 90 s, then back continued (stalled in the background, resumed at 54% / 43% / 38%) continued (API 31 keeps downloading in the background)
Swipe from recents, reopen resumed at 31% resumed at 21%
Airplane mode in the foreground "No connection…", Try again resumed same
Airplane mode while in Settings continued on return same
Reboot mid-download resumed at 23% resumed at 25%
16 corrupted bytes in the partial file checksum failed, re-fetched from 0, hash OK same

On API 36 today's build got interrupted about 5 s after leaving for Settings and fetched 234.5 MB in total. With this change it fetches 190.1 MB and shows no error. The signed release build also finished the 30 s Settings case on API 36. Its DEX has no android.util.Log references. Dictation from Home used the installed small model. ./gradlew test lintDebug passes.

…ckground

Setup starts the 190 MB Whisper small download and then sends the person to
Android's Settings. On Android 15+ (seen on API 36) the system closes a
backgrounded app's sockets about 5 s later, so the plain HttpsURLConnection
failed, setup said the download stopped and Try again started from 0.

ModelStore now:
- keeps the partial file (whisper-small-<sha256>.part) and continues with an
  HTTP Range request; it re-hashes the kept bytes so the final SHA-256 and
  byte-count check is unchanged. A 200 reply (range ignored), a 206 for the
  wrong range or a 416 for an incomplete file falls back to a clean restart.
  A resumed file that fails the checksum is deleted and fetched once more
  from 0; a fresh one that fails reports a failure.
- treats a connection error while Hex was in the background (or left the
  foreground during the attempt) as a pause, not an error: the progress stays
  and the download continues as soon as a Hex activity starts again
  (ActivityLifecycleCallbacks registered from refresh()). The same hook picks
  up a partial file after the app is swiped away or the phone restarts.
- reports real failures by cause. State.Retry is now a data class carrying a
  Problem (Connection, Server, Space); modelStatus() maps them to the new
  model_no_connection / model_no_space strings and the existing
  model_stopped, and modelAction() offers Try again, which now resumes.
- the old restart-every-time path (part.delete() in finally) is removed.
No new permissions (still only RECORD_AUDIO and INTERNET), no foreground
service, no new dependencies.

Verified on API 36 and API 31 emulators with the debug build, rebased on
#2291's setup flow (Turn on, Hex switched on in Settings, the picker that
opens on return, Hex picked): Settings for 10/30/90 s, swipe from recents,
airplane mode in the foreground (shows "No connection", Try again resumes)
and in the background (continues on return), reboot, and 16 corrupted bytes
in the partial file (detected, re-fetched, final SHA-256 matches). Every run
finished with the right hash; dictation from Home used the installed small
model on both. Signed release build: Settings 30 s on API 36 finished, DEX
has no android.util.Log references, permissions are RECORD_AUDIO and
INTERNET only. Baseline on API 36: interrupted ~5 s after Settings, 234.5 MB
fetched; now 190.1 MB and no error. ./gradlew test passes (64 tests);
lintDebug's only error is the existing LogTagMismatch in MicCapture.kt
(same on origin/dev), plus one UsableSpace warning from the extra check.
#2295 added values-zh-rCN while this branch was open. This adds model_no_connection
and model_no_space there in the same style as model_stopped. Verified: assembleDebug,
test and lintDebug pass on the rebased branch (lint now has no errors).
@R44VC0RP
R44VC0RP merged commit 1f7bc95 into dev Nov 22, 2026
1 check passed
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