Files
AFFiNE/packages/frontend/hosts/android/AGENTS.md
T
DarkSky f150cb5604 fix(mobile,hosts): fix keyboard insets, native selection UI, and lifecycle retries
The shell padded the bottom edge by the keyboard height, and the embedded
page editor lifted its keyboard toolbar by the same height again. In Page
mode the editor now owns the bottom edge. The shell gives the editor its
resolved keyboard height, so the toolbar sits directly on the keyboard.

- Hide the TabBar while the software keyboard is up, as 0.27.5 AppTabs
  does. The accessibility tree follows the same rule.
- Move the focus from the editor to the shell when an overlay sheet opens
  over a page, so the keyboard goes down. The drawer keeps its own focus.
- Add NativeSelectionUi to MobileShellView. The iOS shell lets
  UITextInteraction draw the handles, caret, and selection. The Android
  shell draws the handles natively and keeps the GPU caret.
- Scroll a long Search query or drawer filter only as far as necessary to
  keep the caret or the end of the composition in view. The renderer, the
  scroll, and the IME rects and touch offsets read one shaped layout of
  the line, and place it in the field with one transform. A line that fits
  sits at the start edge of the locale, and its glyphs always run left to
  right in text order, also in RTL locales.
- Retry the Android bootstrap from onResume and onNewIntent while the
  allowlist is empty or the shell has not mounted the workspace that
  bootstrap opened. Stop after three automatic retries.
- Send accessibility events only while an accessibility service runs.
  The system clears the focus after the last service stops, and the
  event send threw at that time.
2026-12-05 04:10:13 +09:00

9.5 KiB

Working in packages/frontend/hosts/android/ (for agents)

This directory holds the thin Android host for AFFiNE (affine-android-host). It combines a hand-written JNI Rust crate with a minimal Kotlin SurfaceView activity.

Hard boundaries

  • Keep zero JNA and zero UniFFI bindings. Export hand-written extern "C" JNI symbols under #[cfg(any(target_os = "android", test, feature = "host-jni"))] across src/jni_bridge.rs, src/jni_editor.rs, src/jni_interaction.rs, src/jni_shell.rs, and src/jni_storage.rs.
  • MainActivity creates its host with nativeCreateShellHost, which mounts the affine-mobile MobileShellView; nativeCreateHost keeps one bare PlaygroundView for editor-only parity sequences. src/runtime_root.rs resolves the editor that owns the InputConnection and selection handles: the root editor, or the shell's embedded page_editor() while the Page route shows with no sheet or drawer above it. The shell's focused EntityInputHandler takes InputConnection text on the Search route with no sheet above it and in the drawer once its filter holds search_focus; while any other sheet, or the drawer without a focused filter, covers the shell, the runtime drops text and composition edits. nativeHandleBack lets the page editor close its overlay first, then runs MobileBackCoordinator, and returns false at the root route. Every bootstrap result (first start, retried start, and retryFailedWorkspaces) goes through MainActivity.applyBootstrapOutcome, which mounts the first allowlisted workspace that bootstrap opened (AndroidBootstrapCoordinator.shellWorkspaceId) with nativeShellMountWorkspace (src/shell_workspace.rs): the mount loads only the root document and db$* tables, each page loads when the shell opens it (a failed load retries the next time the page shows), a scheduler task commits edits every second (zero SQLite access without pending changes), and nativeShellFlushWorkspace commits them in onStop. onConfigurationChanged and onResume pass the system dark mode to nativeShellSetSystemDark. AffineSurfaceView publishes the shell's AccessibilityTree (nativeShellAccessibilityJson, parsed by ShellAccessibilityNode) as virtual views through AccessibilityNodeProvider with stable virtual view IDs per node ID and accessibility focus (ShellAccessibilityViews), and an accessibility click sends a tap at the node center. ShellAccessibilityViews sends accessibility events only while AccessibilityManager.isEnabled is true, so the system can clear the focus after the last service stops. A retried bootstrap reads the allowlist again while no shell workspace is mounted (AndroidBootstrapCoordinator.retryPlan). ShellBootstrapState keeps the bootstrap state of the activity: onResume and onNewIntent retry as retryPlan plans while no shell workspace is mounted (an incomplete outcome, an allowlist without a workspace yet, or a failed mount), at most three times in a row, and drain shares otherwise.
  • Keep the main app shell free of any persistent WebView and free of SurfaceHolder.lockCanvas calls.
  • Mount the live pure-Rust PlaygroundView (src/editor_platform.rs, src/editor_runtime/mod.rs, src/editor_selection.rs, src/jni_editor.rs, with native_selection_ui = true) inside MainActivity (AffineSurfaceView + DocumentSelectionHandleView, with HostTextPreviewView and the custom Copy | Cut capsule removed from MainActivity). Drive GPU document-level teardrop handles, system android.widget.Magnifier (API 28+), system ActionMode.TYPE_FLOATING (ActionMode.Callback2.onGetContentRect + onPrepareActionMode gated by Rust action_mask), and multi-MIME ClipboardManager (ClipData.newHtmlText carrying embedded data-blocksuite-snapshot + plain text) from Rust PlaygroundView, hiding handles/Magnifier/ActionMode whenever documentBufferController.isAutofillFocused() is true. Forward MotionEvent touch pointers (nativeOnTouchEvent), InputConnection.sendKeyEvent (nativeOnKeyEvent), and FormFactor::from_android_smallest_width_dp (< 600 -> Phone, >= 600 -> Tablet, Stage-1 edgeless_readonly = true, database_readonly = true).
  • Enforce zero host text copy (AffineInputConnection reads straight from PlatformInputHandler), push editor mutations to InputMethodManager.updateSelection / restartInput / updateCursorAnchorInfo, and preserve exact [start, end) ranges in setComposingRegion via replace_and_mark_text_in_range.
  • Use scripts/package-debug-apk.sh to build and sign AffineAndroidHost-debug.apk directly with Android SDK CLI tools (aapt2, kotlinc, d8, zipalign, apksigner) without Gradle daemons. Compute teardrop handle geometry, line-midpoint grab-offset drag, UTF-16 anchor-authoritative handle ownership (nativeApplyTextDragEndpoints), continuous zero-length crossover inversion, off-screen hiding, line-midpoint magnifier focus, and Copy/Cut/Paste/SelectAll actions wired to ClipboardManager (HostClipboard / AndroidClipboardAdapter, checking primaryClipDescription metadata without reading primaryClip during draw, and writing before deleting on Cut) and InputConnection.performContextMenuAction in src/selection.rs, src/ime.rs, and MainActivity.kt. AndroidSelectionOverlayView fills a pre-allocated 24-float JNI buffer (nativeFillSelectionOverlayGeometry) and draws teardrop Path handles, getSelectionPath loupe highlights with centered focus carets, and Copy | Cut | Paste pills with dividers using zero per-frame allocations, zero Binder IPC in onDraw, and zero SurfaceHolder.lockCanvas calls.
  • Clear the default surface canvas to light #FAFAFC (DEFAULT_CLEAR_COLOR_ARGB = 0xFFFA_FAFC) and adapt HostTextPreviewView text and selection colors to surface luminance. Preserve viewport size, display density, scroll offset, IME state, and texture generation across surfaceDestroyed -> surfaceCreated -> surfaceChanged recreation while gating is_ready() on per-generation surfaceChanged configuration (configured_for_generation). SurfaceLifecycleState models Vulkan -> Gles3 -> CpuFallback downgrade on device loss; the current android_window runtime probe returns (false, true) and starts on EGL/GLES3 with CPU fallback.
  • Gate AutofillManager virtual view structure and per-field isolated value write-back at API 26 (should_expose_autofill(sdk_int) returns sdk_int >= 26, IMPORTANT_FOR_AUTOFILL_YES on AffineSurfaceView, implemented in src/autofill.rs and AndroidAutofillController). Never emit synthetic placeholder asterisks or spaces in ViewStructure, update IME only when autofillId == focusedFieldId, sync manual InputConnection edits back to the focused Autofill field via onFocusedTextEdited, isolate form credentials from HostDocumentStore and HostTextPreviewView via HostDocumentBufferController, redact sensitive values in Debug, and zero cached plaintext on commit, cancel, and clear_fields.
  • Dispatch four-phase predictive back (Start, Progress, Cancel, Commit) on API 34+ (OnBackAnimationCallback, including standalone button/key onBackInvoked calls) and single-step atomic commit on API 24-33 (OnBackInvokedCallback / onBackPressed) against a real destination route stack (BackNavigationState) and HostDocumentStore driven by MainActivity.navigateTo and MainActivity.openDocument, blocking re-entrant commits and returning false at the root route.
  • Render PDF pages in place through android.graphics.pdf.PdfRenderer (AndroidPdfPageRenderer) with pre-validated Rust bounds, OOM cap (4096x4096), clamped dpi_bucket >= 72 cache keys, and zero-Rust-heap chunked streaming ARGB_8888 -> RGBA8 conversion with per-call JNI ExceptionCheck in src/pdf.rs and src/jni_interaction.rs.
  • Open in-place SQLite WAL databases only from the ordered affine-local-workspace allowlist. Never scan disk *.db files to discover local workspaces.
  • Store auth sessions in <filesDir>/datastore/auth-sessions.preferences_pb using v1:<base64_iv>:<base64_ct_tag> AES-GCM envelopes, 1970 Unix millisecond i64 timestamps, and the persisted auth-installation-id key.
  • Parse legacy https://localhost localStorage probe and rollback write-back payloads through parse_legacy_local_storage_probe using an explicit key allowlist only. Block window.indexedDB.open and window.indexedDB.deleteDatabase in every probe script.
  • Keep every .rs file at or below 800 non-test lines, free of non-test super::super::, and free of wall-clock calls.

Verification

Always clear inherited root rustflags:

CARGO_ENCODED_RUSTFLAGS="" cargo test --locked -p affine-android-host --all-features
CARGO_ENCODED_RUSTFLAGS="" cargo build --locked -p affine-android-host --features host-jni
CARGO_ENCODED_RUSTFLAGS="" cargo check --locked -p affine-android-host --target aarch64-linux-android

To run the 33-sequence Kotlin-to-JNI parity harness on a host with kotlinc and ANDROID_HOME:

kotlinc -classpath "$ANDROID_JAR" -d /tmp/affine-android-kotlin \
  hosts/android/kotlin/app/affine/pro/host/MainActivity.kt \
  hosts/android/kotlin/app/affine/pro/host/LegacyLocalStorageProbe.kt \
  hosts/android/kotlin/app/affine/pro/host/AndroidBootstrapCoordinator.kt \
  hosts/android/kotlin/app/affine/pro/host/AuthDataStore.kt \
  hosts/android/kotlin/app/affine/pro/host/ShareIntentBridge.kt \
  hosts/android/kotlin/app/affine/pro/host/ShareReceiverActivity.kt \
  hosts/android/tests/ImeHostParityCheck.kt
kotlin -J-Djava.library.path="$PWD/target/debug" -cp "/tmp/affine-android-kotlin:$ANDROID_JAR" ImeHostParityCheckKt