hex/android
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Ryan Vogel 81d058fcfe
Android: Hex Preview's update download survives leaving the app; setup says Hex Preview (#2552)
* Android: Hex Preview's setup names "Hex Preview", as Android's lists do

Hex Preview's first run said "Set up Hex", "choose Hex as your keyboard", a
"Hex keyboard" row with "Turn on Hex in Settings" / "Choose Hex in the list",
and "Choose Hex keyboard", while Android's keyboard list, keyboard picker and
microphone prompt say "Hex Preview" (and list both "Hex" and "Hex Preview"
for anyone who also has the Play app), so a person could turn on or pick the
wrong keyboard. Its welcome also said Hex uses the internet only for the
speech model, which isn't true for Hex Preview: it also asks GitHub for
updates.

What changed (preview build type only, src/preview/res, English and Simplified
Chinese; src/main and the Play builds are unchanged):
- setup_title, setup_detail, setup_microphone_detail, setup_keyboard,
  setup_keyboard_enabled, setup_keyboard_off, setup_choose_hex now name
  "Hex Preview".
- keyboard_hint_microphone ("Allow the microphone in Hex Preview"): the
  keyboard's hint tells the person which app to open; MicCapture's sentinel
  sentence still maps to this resource, so no code changed.
- setup_welcome_detail: "Hex Preview uses the internet only to download its
  speech model (190 MB) and to check GitHub for updates. Your voice and words
  stay on this device."
"The Hex key", "Hex menu", "Hex dictation" and the keyboard's TalkBack pane
name stay as they are: they name the key and the keyboard's own parts, not
something to find in Android's lists.

Verified on the Mac Studio (with the next commit): :app:testDebugUnitTest
passes; :app:assemblePreview -PhexPreviewBuild=2300 builds, and aapt2 dump
strings shows the new English and Chinese strings in app-preview.apk.

* Android: Hex Preview's update download keeps going when the person leaves the app

On Android 16 (API 36) the update download, a plain HTTPS read on the
updater's own thread, was cut off about 5 s after the person left Hex
Preview, and the Home card then said "The download stopped..." with Try
again. API 31 was unaffected.

What changed (src/preview only; the Play builds and src/main are unchanged):
- Android 14+ (API 34): the tap on Update (or Download after the mobile-data
  note, or Try again) schedules a user-initiated data transfer job
  (UpdateJob: setUserInitiated, NETWORK_TYPE_ANY, the APK's size as its
  estimated bytes, the release in its extras). The job posts its
  notification first thing, then runs the same download and checks as
  before on the updater's worker (Channel.download's host allow-list, size
  and SHA-256, then Channel.verify's package, build and signing-key checks,
  untouched), updating the Home card and the notification's progress.
  onStopJob ends the download at its next read, which deletes the partial
  file and shows the same "download stopped" card; it is not rescheduled.
  With no network at all, or if Android refuses the job, the download runs
  in-process as before.
- When the job's download is checked: if Hex Preview's window shows, it
  installs exactly as before. If not, nothing installs from the background:
  the verified file stays, a notification says "Hex Preview <version> is
  downloaded" / "Open Hex Preview to install it.", and the Home card shows
  Install (kept across a restart of the process while the file is still
  there). Install checks the file's size, SHA-256 and signing key again
  before the installer session; if Android cleared it from the cache, it
  downloads again (asking first on mobile data). Nothing installs without
  the person's tap.
- Below Android 14 the in-process download is unchanged (the same code,
  moved into Updates.fetch, which both paths share).

New permission (preview manifest only): android.permission.RUN_USER_INITIATED_JOBS,
plus the UpdateJob service guarded by BIND_JOB_SERVICE (not exported). No
POST_NOTIFICATIONS is declared or asked for, so there is no new prompt.

What the person sees: the Home card's progress as before, and Android's
running-apps list (Task Manager) shows Hex Preview while the job runs. The
notification ("Downloading Hex Preview <version>" with progress, in a
low-importance "Updates" channel; tapping opens Hex Preview) appears in the
notification shade only where Hex Preview may post notifications, which by
default it can't on Android 13+.

Verified on the Mac Studio: :app:testDebugUnitTest passes;
:app:assemblePreview -PhexPreviewBuild=2300 builds (lintVital included);
aapt2 dump badging shows ly.anoma.hex.preview, versionCode 2300, label
"Hex Preview"; aapt2 dump permissions lists RUN_USER_INITIATED_JOBS; the
manifest has the UpdateJob service with BIND_JOB_SERVICE; apksigner verify
passes with certificate SHA-256 133568f6...35d98e. Not yet run on a phone
or emulator.
2026-11-23 12:59:55 -05:00
..
app Android: Hex Preview's update download survives leaving the app; setup says Hex Preview (#2552) 2026-11-23 12:59:55 -05:00
benchmark iOS keyboard: Polish autocorrects with Hex's own model, word list and capitals (#2423) 2026-11-22 14:45:55 -05:00
gradle/wrapper Hex for Android: on-device dictation keyboard (#2074) 2026-11-20 17:27:17 +00:00
play Android Play README: 0.2.0 (build 3) release-candidate numbers and checks (#2296) 2026-11-21 19:57:44 -05:00
scripts Android: preview-release.sh --dry-run (#2535) 2026-11-23 11:48:53 -05:00
.gitignore Hex for Android: on-device dictation keyboard (#2074) 2026-11-20 17:27:17 +00:00
build.gradle.kts Hex for Android: on-device dictation keyboard (#2074) 2026-11-20 17:27:17 +00:00
gradle.properties Hex for Android: on-device dictation keyboard (#2074) 2026-11-20 17:27:17 +00:00
gradlew Hex for Android: on-device dictation keyboard (#2074) 2026-11-20 17:27:17 +00:00
gradlew.bat Hex for Android: on-device dictation keyboard (#2074) 2026-11-20 17:27:17 +00:00
README.md Android: Hex Preview, an APK outside Google Play that updates itself (#2531) 2026-11-23 11:37:51 -05:00
settings.gradle.kts Hex for Android: on-device dictation keyboard (#2074) 2026-11-20 17:27:17 +00:00

Hex for Android

Hold the Hex key, speak and release to insert text in the current app. A tap starts hands-free dictation; ✓ finishes and ✕ discards. The Hex key sits at the strip’s trailing end, where Gboard has its mic; while dictating it becomes ✓ and the live dot waveform grows out of it, above the keys. While Hex transcribes, a spinner takes the checkmark’s place and ✕ still cancels. The keys are Gboard’s size and place on a Pixel (measured on an emulator), with ?123, comma, space, period and enter in the bottom row; the system’s own switcher in the navigation bar, or Hex menu › Next keyboard, changes keyboards.

Speech model and privacy

Whisper small Q5_1 is the default; multilingual tiny Q5_1 is bundled and works from first launch in airplane mode. The Android benchmark compares tiny, base, small, Parakeet and Android’s strictly on-device provider on the same 40 clips. Hex’s small configuration reduced WER from 17.54% to 5.26% and used 344 MiB peak native RSS on short clips. Norwegian remains the weakest language.

Welcome explains that the internet is used only to fetch the 190 MB speech model. Get started begins a fixed HTTPS download; setup shows progress and lets you continue with tiny. The partial file is kept: when Android closes the connection because Hex went to the background, or Hex is closed or the phone restarts, the download continues with an HTTP range request the next time a Hex screen is open. A real failure (no connection, server error, full storage) says so and offers Retry, which also continues. ModelStore verifies the exact byte count and SHA-256 before atomically installing it in private, non-backed-up storage. The keyboard never makes a network request. Missing or unusable small weights fall back to tiny. Voice, words and translations stay on the device; Hex has no telemetry, transcript history or network speech service.

Keyboard and styles

Automatic uses the first supported Android system language, or English if none is supported. Hold the language-labelled space bar to switch immediately between Automatic, English, Español, Deutsch and Norsk (Bokmål). Slide on the Hex key or tap Hex menu to change the spoken language or style:

  • Polished (default): trims standalone hesitation sounds, fixes spacing/capitals and closes sentences.
  • Casual: lowercase texting, without the period at the end of a single statement.
  • Notes: “bullet point,” “next bullet,” “new line” and “new paragraph” become list items and breaks.
  • Verbatim: keeps the recognizer’s words and punctuation; no cleanup is applied.

Clean up what I say is off by default. The deterministic rules remove hesitations and resolve explicit self-corrections against nearby words of the same kind. Spanish, German and Norwegian use the iOS word lists and pause boundaries; English uses explicit markers, not iOS’s learned tagger or an LLM. Ambiguous repairs stay as spoken. Ordinary negation and language-specific words such as German “eh” and Norwegian “er” stay intact.

Translate what I say is also off by default. With Español and translation enabled, Next dictation offers Paste in English or Paste as spoken. The choice is captured when recording begins; after a one-off spoken-language insertion, the next dictation returns to English. Translation is multilingual Whisper’s native speech-to-English task, not a second text model or online SDK. The tested Spanish WAV inserted both “El café todavía está caliente.” and “The coffee is still hot.” offline.

Typing assistance

All four layouts have a bundled, memory-mapped HXTM v3 word and word-pair model. English and Spanish keep the original iOS Google Books files. German uses Google Books; Bokmål uses the National Library of Norway’s book counts filtered by Norsk ordbank. Start typing to see ranked suggestions; tap one to insert it. A space brings up next-word predictions. Passwords, addresses and editors that disable suggestions use literal typing.

Keyboard › Keyboard layout follows dictation by default; an explicit layout can differ from the space bar’s spoken language. Spanish has a dedicated ñ and ¿/¡ on the second symbols page. German is QWERTZ with ü, ö and ä; hold s for ß. Bokmål has å, ø and æ.

Keyboard › Suggest swear words is off by default on every layout. Slurs on the curated list are never offered, even when swear words are enabled. The filter applies to completions, next words, autocorrect and both halves of a missed-space correction. Typing a listed word in full keeps your exact spelling; its literal chip accepts what you typed. The filter never changes the HXTM models.

On space, the Kotlin decoder compares each tap with the measured key centres and combines those distances with the previous word’s probability. It can recover a missing space. A known spelling requires a higher confidence margin than a nonword. Immediately press Delete to restore exactly what you typed, without the space. Undo the same replacement twice and Hex keeps that spelling in later sessions. Only rejection counts keyed by hashes are saved locally; there is no typing journal.

Keyboard settings and Hex menu → Typing feedback control key pop-ups and key haptics independently. Both default on; Hex follows system keyboard vibration. Hold a letter, slide to an accent, and release. Turning off pop-ups leaves the deliberate accent picker available.

English/Spanish typing benchmark, model sources and licences

Settings

After setup, Home shows Ready or Finish setup, a Dictate bar and four destinations, following the iOS navigation in #2054. Tap Dictate, speak, then Finish dictation: Hex displays the local result on Home. Dictation controls style, language, translation, cleanup and the offline speech model. Keyboard has layout, autocorrect, key pop-ups, haptics and a practice field. Privacy explains storage and links the model credits. Experiments › Spatial autocorrect controls tap-based correction; it starts on to preserve existing Android behavior. Turning it off keeps dictionary completions and the fixed English typo/contraction rules; turning off Keyboard › Autocorrect disables all automatic replacements.

Privacy › Privacy policy opens the Android policy text even in airplane mode.

Phase 4 verification

On identical held-out synthetic taps and unchanged English and Spanish iOS models, Android’s English word error rate fell from 10.53% to 7.40%, against 8.39% for iOS R1 without offsets. Spanish was 10.33% against 10.43% for iOS (phase 3 Android: 10.06%). The step-by-step benchmark explains the original 298 extra English errors; the replay table gives raw counts, exact sentences and timing.

The Studio verified Spanish, German and Bokmål suggestions and space-triggered corrections in a separate Android app, including ¿¡, ß and æøå. The German and Bokmål assets were also copied, byte-for-byte, into the iOS typing-model folder in local commit 230a437e. The production Swift reader checked all 95,000 words per language and every stored pair score; no iOS source or existing English/Spanish model changed.

With airplane mode on and Wi-Fi off, fixed audio went through the emulator microphone and Whisper small into that editor. Spanish “Nos vemos mañana en la estación” and German “Wir treffen uns morgen am Bahnhof” matched. Bokmål “Tusen takk for hjelpen” also matched; on corpus clip 40 Hex inserted “Biblioteket stenger tydelig på lørdag” instead of “Biblioteket stenger tidlig på lørdag.” The same Spanish clip also produced a transcript from Home’s Dictate bar.

Five unit tests passed in each of the debug and release variants. Home, the four settings pages and all four detail pages were captured in light and dark. Deselecting Hex as the current keyboard changed Ready to Finish setup · 1 step; selecting it again through setup restored Ready. The local APK, full-size screenshots, replay results and Studio log are under build/phase4-proof/.

How it works

  • App: Kotlin and Jetpack Compose. Welcome → microphone permission and Android’s keyboard settings/picker → a real practice field. Setup reads live system state; it remembers the page, not the words.
  • Keyboard: HexKeyboard is an InputMethodService with native key views. Letters, shift/caps lock, symbols, delete, editor actions and next keyboard use Android input APIs. It commits through the active InputConnection; changing editors or hiding the keyboard discards pending results. Password fields allow typing only. Typing lives in KeyboardEngine, which reaches the editor only through KeyboardTextTarget; the views are KeyboardView (strip, keys or Hex menu), StripView and KeyPlaneView; each layout's letters are data in KeyboardLetters.
  • Audio: MicCapture records up to 60 s of mono 16 kHz PCM in RAM and releases the microphone before CPU inference. The recognizer interface operates on that capture and its language/task. Hex requests no audio focus and does not change playback routing.
  • Type: unmodified InterVariable.ttf from Rasmus Andersson and the Inter Project, v4.1, licensed under SIL OFL 1.1. One Compose font family and native TextView helper cover Hex’s text, including keys, menus, strip labels and tabular digits (tnum). Android supplies missing glyphs; permission dialogs and the system keyboard picker use the OS font. Gradle verifies the bundled TTF’s SHA-256.
  • Licences: OpenAI Whisper weights and whisper.cpp/ggml are MIT. Kotlin/AndroidX are Apache 2.0; libc++ is Apache 2.0 with LLVM exceptions. Bundled credits and licence texts. No ML Kit, Parakeet or third-party language packs are shipped.

Build and run

Use the Studio’s JDK 21, SDK platform 36, NDK 27.2.12479018 and CMake 3.22.1. The Play packaging checks use Build Tools 36.0.0 and bundletool 1.18.3. From the synced android/ directory, with an ARM64 emulator or phone running:

export JAVA_HOME="/opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk/Contents/Home"
export ANDROID_HOME="$HOME/Library/Android/sdk"
export PATH="$ANDROID_HOME/platform-tools:$JAVA_HOME/bin:$PATH"
./gradlew assembleDebug
./gradlew test
adb install -r app/build/outputs/apk/debug/app-debug.apk
adb shell am start -n ly.anoma.hex/.MainActivity

On the headless Studio, start an ARM64 AVD with -audio none -grpc 8566 -grpc-use-token. inject.py uses the matching emulator discovery record and streams paced mono PCM16 into the guest microphone. Generate the bindings from the SDK emulator’s lib/emulator_controller.proto with grpcio-tools, then put them on PYTHONPATH. No recording or transcript is injected directly into the application.

For the upload bundle, run ./gradlew :app:bundleRelease. Release packaging requires a private properties file at ~/.config/hex-android/keystore.properties or the path in HEX_ANDROID_KEYSTORE_PROPERTIES. It must contain storeFile (relative to that file, or absolute), keyAlias, storePassword and keyPassword. Release uses R8 and resource shrinking and never falls back to the debug key. Gradle writes app/build/outputs/bundle/release/app-release.aab.

Hex Preview (outside Google Play)

Hex Preview is the preview build type: the release build as an APK, application id ly.anoma.hex.preview, named Hex Preview in the launcher and in Android's keyboard list, so it installs beside Hex from Google Play with its own settings, words and speech model. It is signed with its own key (never the Play upload key), read from ~/.config/hex-android-preview/keystore.properties or HEX_ANDROID_PREVIEW_KEYSTORE_PROPERTIES; every build must be, because Android refuses an update signed with any other. The certificate's SHA-256 is 13:35:68:F6:F0:61:4F:E5:7B:C8:8D:AE:CA:32:AF:A7:2C:6A:B7:40:20:26:29:F7:52:FA:A0:33:BE:35:D9:8E.

Its updater lives only in app/src/preview (code, permissions, strings); debug and release build app/src/play's empty Updates, which is inlined away, because Google Play forbids an app updating itself. It reads latest.json on the android-preview branch when the app opens and at most once a day when the keyboard opens (never in a password field; Settings › Check for updates automatically). An update downloads only after Update is tapped, and goes to PackageInstaller only once its size, SHA-256 and signing certificate match.

scripts/preview-release.sh publishes a build: the build number is the count of commits up to the built commit of dev, the version name <versionName>-preview.<build>, the tag android-preview-<build> (a pre-release, never moved), and the channel branch android-preview holds only latest.json and the README people land on.

Scope: Android 12+ (min API 31), target 36, ARM64, four typing models and four dictation languages. See the Play release evidence, listing drafts, phone screenshots and real-device checklist. The Studio also exercised offline fallback, all four styles, per-dictation translation, both appearances and insertion into a separate app with music playing in earlier phases.