Skip to content

The only downloader you need. 下载器的集大成者。

docs(android): 添加 Android 相关技术文档和构建说明

XiaoYouChR75a88237f5f5
diff --git a/CONTEXT.md b/CONTEXT.md
index 0778c7b3..195b5e13 100644
--- a/CONTEXT.md
+++ b/CONTEXT.md
@@ -219,7 +219,7 @@ _Avoid_: `directory` in project-owned names (only when mirroring an external API
### Boolean names
-Booleans use `is*`, `has*`, `can*`, or `should*`.
+Booleans are named with `is*`, `has*`, `can*`, or `should*` prefixes.
_Avoid_: bare adjectives (`available`), action-looking booleans (`enableX`),
third-person verbs (`supportsX`).
@@ -262,22 +262,23 @@ _Avoid_: `find*` for in-memory lookup (keep `find*` for disk/PATH search).
### Parser routing
-`featureService.parse(options)` iterates parsers by priority (higher checked
-first) and calls the first match:
+`featureService.parse(options)` iterates parsers in ascending priority order;
+the first `match(options)` wins. Specific parsers have low priority numbers
+and are checked first; HttpParser (100) is the fallback, checked last.
| Parser | Priority | Match condition |
|---|---|---|
-| HttpParser | 100 | any `http`/`https` URL (fallback) |
-| FtpParser | 95 | `ftp`/`ftps` scheme |
-| GitHubParser | 90 | GitHub file URL + proxy configured |
-| HuggingFaceParser | 85 | HuggingFace domain |
-| TorrentParser | 85 | `magnet:` scheme or `.torrent` |
-| M3U8Parser | 80 | `.m3u8`/`.mpd` in URL or local manifest |
-| YouTubeParser | 70 | YouTube domain |
-| MergeParser | 60 | `isinstance(options, MergeTaskOptions)` |
-| InstallParser | 55 | `isinstance(options, BinaryInstallOptions)` |
-| BilibiliParser | 50 | Bilibili domain |
| ED2kParser | 45 | `ed2k://` scheme |
+| BilibiliParser | 50 | Bilibili domain |
+| InstallParser | 55 | `isinstance(options, BinaryInstallOptions)` |
+| MergeParser | 60 | `isinstance(options, MergeTaskOptions)` |
+| YouTubeParser | 70 | YouTube domain |
+| M3U8Parser | 80 | `.m3u8`/`.mpd` in URL or local manifest |
+| TorrentParser | 85 | `magnet:` scheme or `.torrent` |
+| HuggingFaceParser | 85 | HuggingFace domain |
+| GitHubParser | 90 | GitHub file URL + proxy configured |
+| FtpParser | 95 | `ftp`/`ftps` scheme |
+| HttpParser | 100 | any `http`/`https` URL (fallback) |
Delegation patterns:
- **GitHubParser** rewrites URL through proxy, then calls
@@ -324,7 +325,7 @@ loop. Services hold no View references; Views connect to service signals.
**Browser Service** is a protocol adapter: receive WebSocket messages, translate
to Task Service verbs, send results back. It holds no MainWindow reference.
-View connection is purely via signals wired in the entry script.
+View connection is purely via signals bound in the entry script.
**Dependency direction** (leaf → top):
@@ -336,7 +337,7 @@ featureService holds packs + parsers; parse() returns Task
taskDraft uses coroutineRunner + featureService
browserService uses taskService + featureService; emits signals for View
categoryService standalone; taskService calls matchByName on add
-View / Entry wires everything; connects signals
+View / Entry binds everything; connects signals
```
## Module topology
@@ -367,7 +368,7 @@ Pack capabilities: `parsers()`, `taskCard()`, `draftCard()`, `optionCards()`,
`editCards()`, `fileTypes()`, `pages()`, `settingGroups()`, `start()`, `stop()`.
Module singletons created at import time (configs, runtimes, services, actors)
-must not read `cfg` values at construction — use lazy properties. `qconfig.load`
+do not read `cfg` values at construction — they use lazy properties. `qconfig.load`
happens before `featureService.load`.
Notable pack-level actors:
@@ -381,7 +382,7 @@ Notable pack-level actors:
### View layer
**MainWindow** owns a `TaskDraft` service instance and a lazy
-`TaskDraftDialog`. It does not own task lifecycle — it wires
+`TaskDraftDialog`. It does not own task lifecycle — it binds
`draft.taskConfirmed → taskService.add`, which is the only task-creation path.
**TaskPage** subscribes to `taskService.*` signals for card lifecycle. Virtual
@@ -445,26 +446,25 @@ setupEnvironment():
startApp(application, isSilent):
sys.excepthook → signalBus.exceptionCaught
- load translator
- coroutineRunner.start()
+ loadEngine(application) → translator + coroutineRunner.start
MainWindow()
- featureService.load() → discover + import packs
- PackConfig.load() → register pack ConfigItems
- window.setupPacks() → add pack pages/settings
- taskService.taskStarted → speedMeter.start
- tasksAllCompleted → speedMeter.stop
- taskService.resumeSaved()
- featureService.start() → pack.start() on each
- wire signalBus, browserService, clipboardListener, tray/dock
- taskService.taskCompleted → notifyTaskCompleted
- tasksAllCompleted → plan.trigger
- application.aboutToQuit.connect(stopApp)
+ splash (if not silent)
+ loadPacks() → featureService.load + PackConfig.load
+ window.setupPacks()
+ startEngine() → speedMeter bind + resumeSaved + featureService.start
+ bind signalBus, browserService, clipboardListener, tray/dock
+ bindNotifications(completed, diskSpace)
+ checkUpdateAtStartup()
+ application.aboutToQuit.connect(stopEngine)
```
+Shared startup functions live in `app/startup.py`: `loadEngine`, `loadPacks`,
+`startEngine`, `bindNotifications`, `checkUpdateAtStartup`, `stopEngine`.
+
### Desktop shutdown
```
-stopApp():
+stopEngine():
taskService.stop() # pause all running/waiting
taskService.flush() # write store to disk
browserService.stop() # close WebSocket server
@@ -492,17 +492,133 @@ Shutdown is identical to desktop.
## Code shape
+### Naming conventions
+
Functions and methods use `camelCase`. Classes use `PascalCase`. Constants use
`UPPER_SNAKE_CASE`. Object-private attributes and class-internal helpers start
with `_`. Module-level classes and constants do not add `_`.
+Function names combine one verb from the verb vocabulary above with one
+concrete business noun: `taskService.add(task)`,
+`featureService.parse(options)`, `browserService.stop()`. No verbs outside the
+vocabulary appear in app-owned code — if none of the existing verbs fit, the
+function's responsibility is unclear and the seam is wrong, not the vocabulary.
+
+### Word choice
+
+Names use short, common English words that a first-time reader understands
+without a dictionary: `task`, `step`, `name`, `folder`, `speed`, `draft`.
+Rare or academic words are replaced with plain equivalents: `sanitize` →
+`toSafeFilename`, `normalize` → `toProxySite`, `orchestrate` → `run`.
+When a shorter word means the same thing, the shorter word wins: `add` over
+`insert`, `stop` over `terminate`.
+
+Names describe what the actor owns in the domain, not its structural role.
+Actors are named `taskService`, `speedMeter`, `categoryService` — generic
+structural nouns like `manager`, `controller`, `coordinator`, `provider`,
+`repository`, `facade`, `pipeline`, and `context` are absent because each
+actor is named for the business concept it owns. A new noun represents a real
+domain concept; introducing a noun just to make an awkward helper sound nicer
+is a sign the helper belongs on an existing actor.
+
+A forced long name (`removeTaskRecordAndDeleteFiles`) is a sign the function
+owns two responsibilities. Better seams make names short:
+`taskService.delete(task, shouldDeleteFiles)`.
+
+### Comments
+
+Code carries no comments and no docstrings by default. A well-named function
+with typed parameters explains what it does; comments that restate the code
+compete for the reader's attention without adding information. Comments earn
+their place only when they explain **why** — a hidden constraint, a workaround
+for a specific bug, or behavior that would surprise a reader. If removing a
+comment would not confuse a future reader, the comment is not written.
+
+### Inlining
+
+This codebase prefers inlined code over shallow extractions. A function earns
+extraction when it is called from two or more sites, or when it exceeds ~15
+lines with a single clear duty and a return type that is a single named type
+(not a tuple or dict). One-line wrappers, single-caller helpers under 5 lines,
+and trivial delegations are inlined at their call site. Three similar lines of
+obvious code are kept inline rather than extracted into a function whose
+interface is as complex as its body — the extraction adds a jump without
+reducing knowledge for the caller.
+
+### Deepening
+
+"Deeper module" in this project means less call-site knowledge, not more
+architecture nouns. Refactors in this codebase have been subtractive:
+workflows moved behind existing actors before new actors were created;
+reach-through plumbing deleted; new modules introduced only when the same
+business rule was already spreading across multiple callers. New seams were
+earned by deleting knowledge from call sites, not by drawing boxes.
+
+### Four-phase `__init__`
+
QWidget and QDialog subclasses follow a four-phase `__init__`:
-`_initWidget()` → `_initLayout()` → `_bind()`. Signals are connected in
-`_bind`, after all widgets exist — so `_initWidget` can set initial state
-without triggering slots.
-Services are signal-driven actors with private state. They do not hold View
-references. Views connect to service signals and call service methods directly.
+```python
+def __init__(self, parent=None):
+ super().__init__(parent)
+ self._initWidget()
+ self._initLayout()
+ self._bind()
+```
+
+Each phase has a single responsibility:
+
+`_initWidget()` — create and configure child widgets. Set initial property
+values, default text, icons, size policies. No signal connections — the widgets
+are inert at this point, so setting state does not trigger any reactions.
+
+`_initLayout()` — assemble layouts and add widgets. Only layout code: margins,
+spacing, stretch factors, addWidget/addLayout calls.
+
+`_bind()` — connect signals to slots. All widgets exist and are laid out, so
+it is safe to connect. Slots triggered here can safely reference any sibling
+widget.
+
+This separation exists because Qt signals fire immediately on connect if the
+current widget state already matches the signal condition. Connecting in
+`_initWidget` before sibling widgets exist leads to slot calls that reference
+widgets not yet created.
+
+### Responsibility layers
+
+```
+View (app/view/)
+ Renders state and collects user intent.
+ Calls service methods: taskService.pause(task), taskService.delete(task, ...).
+ Connects to service signals for refresh: taskService.taskAdded → _onTaskAdded.
+ Does not own task lifecycle, persist records, run downloads, or manage the
+ event loop.
+
+Service (app/services/)
+ Signal-driven actors with private state. The single source of truth for the
+ state they own. Emit signals when state changes.
+ Do not import from app/view/. Do not hold QWidget references. Do not show
+ dialogs or InfoBars.
+
+FeaturePack (features/*_pack/)
+ Declare capabilities (parsers, cards, runtimes). Provide pack-specific
+ business logic (download step implementation, protocol handling).
+ Do not import from app/view/ except for card classes returned by taskCard /
+ draftCard / optionCards. Do not reach into Task Service internals.
+
+Platform (app/platform/)
+ OS-specific adapters: file association, run at login, hidden subprocess,
+ notifications, Android keepalive. Thin wrappers with no business logic.
+
+Entry (Ghost-Downloader-3.py, android/main.py)
+ Composition root. Creates services and views, binds signals, defines
+ shutdown order. Platform-specific wiring lives here, not in services or views.
+```
+
+When a View method grows long because it orchestrates multiple service calls,
+the orchestration belongs behind a service method — the View calls one verb,
+the service owns the ordering. This is how `taskService.redownload(task)`
+replaced multi-step card code that called stop + delete files + reset + start.
## Task execution
@@ -513,7 +629,7 @@ skips COMPLETED, yields remaining while `self.status == RUNNING`. No protocol
overrides `Task.run()` — it is the universal error boundary:
- `TaskError` → `step.setError(StepError(str(e), e.params))` — user-facing
-- `Exception` → `step.setError(StepError(repr(e)))` — unexpected, debuggable
+- `Exception` → `step.setError(StepError("Unexpected error: {detail}"))` — generic wrapper, detail from `str(e)`
- `CancelledError` → passes through (BaseException in Python 3.11+), no error set
Steps raise `TaskError` for known business errors. Steps that use `TaskGroup`
diff --git a/android/BUILD.md b/android/BUILD.md
new file mode 100644
index 00000000..d3f4fd17
--- /dev/null
+++ b/android/BUILD.md
@@ -0,0 +1,110 @@
+# Android Build Notes
+
+Build and packaging knowledge for the PySide6 + python-for-android (p4a)
+Android target. Minimum supported version: Android 10 (API 29).
+
+## Docker CI
+
+The build runs in a custom Docker image with p4a, Android SDK/NDK, and Rust
+toolchain pre-installed. The Dockerfile lives in the CI workflow.
+
+### p4a template patches
+
+p4a copies template files (AndroidManifest, themes, resources) from
+`/opt/p4a/pythonforandroid/bootstraps/qt/build/` into the dist. **Patches to
+these templates are applied in the Docker image layer**, not in the
+buildozer cache volume — the cache gets overwritten on every dist assembly.
+
+Template patches are `sed` commands at the end of the Dockerfile, after the
+p4a install step. Three critical PythonActivity patches:
+
+**Back-key → moveTaskToBack**: without this patch, pressing back calls
+`onDestroy` + `killProcess`, killing downloads. The patch overrides
+`onKeyDown` to call `moveTaskToBack(false)` instead.
+
+**onActivityResult → super()**: without this patch, `QFileDialog` on Android
+deadlocks — `PythonActivity` never propagates the result to Qt.
+
+**onNewIntent → setIntent(intent)**: without this patch, re-entry via share
+intent returns the stale intent from the first launch (`sharedText()` sees
+old data).
+
+### Recipes
+
+Custom p4a recipes live in `android/recipes/`:
+
+- `jh2` — HTTP/2 with Rust extension (requires Rust toolchain in the image)
+- `wreq` — TLS fingerprint HTTP client (Rust + btls-sys)
+- `gd3ffmpeg` — pre-built FFmpeg binaries into `nativeLibraryDir`
+
+## APK size
+
+The base APK was reduced from ~159 MB to ~62 MB via:
+
+- Pruning unused Python stdlib modules (`android/patches/gd3_prune.py`)
+- Pre-built minimal FFmpeg instead of full build (~30 MB → ~6 MB)
+- N_m3u8DL-RE excluded from APK (runtime hot-install via linker64, see
+ ADR 0005)
+
+## Signing
+
+Release keystore: `~/.gd3-android/release.jks`
+- Alias: `ghostdownloader`
+- First public release uses this keystore — Android binds the signing
+ key permanently to the package name (see ADR 0006)
+
+CI secrets (GitHub Actions):
+- `ANDROID_KEYSTORE_BASE64` — base64-encoded `.jks`
+- `ANDROID_KEYSTORE_PASSWORD` — keystore + key password (same)
+- `ANDROID_KEY_ALIAS` — `ghostdownloader`
+
+## Runtime patches (`patches.py`)
+
+`app/view/mobile/patches.py` applies monkey-patches for PySide6-on-Android
+quirks. All patches are called from `app/view/mobile/__init__.py` during
+`setupAndroid()`.
+
+| Patch | What it fixes |
+|---|---|
+| `patchMenus` | RoundMenu reparented as child widget — second top-level window crashes EGL (ADR 0004) |
+| `patchIconRendering` | QSvgPlugin returns blank pixmaps → SvgIconEngine renders via QSvgRenderer |
+| `patchFileDialogs` | QFileDialog `content://` URIs → real filesystem paths via ContentResolver |
+| `patchDialogWidth` | MessageBoxBase limited to screen width on narrow devices |
+| `patchOptionCardLayout` | Horizontal setting/option cards reflowed to vertical |
+| `patchGroupTouch` | CollapsibleSettingCardGroup header tap toggles collapse (touch-friendly) |
+
+## Storage permission
+
+Android 11+ (API 30): `MANAGE_EXTERNAL_STORAGE` — broad file access, required
+for downloading to arbitrary folders. Android 10 and below:
+`WRITE_EXTERNAL_STORAGE`. `PermissionBanner` in `MobileMainWindow` blocks
+task creation until storage is granted.
+
+## StrictMode file URI
+
+Android 7+ (API 24) blocks `file://` URIs in intents by default.
+`_relaxFileUriPolicy()` in `app/platform/android.py` disables StrictMode's
+`FileUriExposedException` check so `Uri.fromFile` intents (open file, reveal
+folder) work.
+
+## Device debugging pitfalls
+
+### Screen-off pauses the Qt event loop
+
+When the screen turns off, Android pauses the activity. The Qt event loop
+stops processing — downloads freeze. The `KeepAlive` foreground service +
+`PARTIAL_WAKE_LOCK` keeps the CPU running, but the Qt event loop only
+resumes when the screen is on or the activity is in the foreground.
+
+### Insets double-inset
+
+Android system bars insets can be applied twice if both the activity theme
+and Qt handle them. Fix: let the activity theme handle status bar color,
+do not apply insets in Qt layout code.
+
+### Desktop preview via grab()
+
+For fast UI iteration without deploying to a real device, use
+`widget.grab().save("preview.png")` to render the widget off-screen on
+desktop. This works because `MobileMainWindow` is a plain `QWidget` with
+no Android-specific rendering.
diff --git a/docs/adr/0004-android-popup-must-be-child-widget.md b/docs/adr/0004-android-popup-must-be-child-widget.md
new file mode 100644
index 00000000..b0c936b6
--- /dev/null
+++ b/docs/adr/0004-android-popup-must-be-child-widget.md
@@ -0,0 +1,22 @@
+# Android popups must be child widgets, not top-level windows
+
+PySide6 on Android with EGL: any second top-level window (`Qt.Popup`,
+`Qt.Dialog` without a parent, or a detached `RoundMenu`) triggers the
+`AndroidDeadlockProtector` in the surfaceflinger path. The EGL surface is
+single-threaded; a second surface creation deadlocks against the first.
+The result is a crash on devices with slow GL drivers — device-specific, not
+reproducible in the emulator.
+
+All popup-like widgets (RoundMenu, Flyout, TeachingTip) must be reparented
+as child widgets of the main window. `patches.py` does this globally via
+`patchMenus()`. A side-effect: `WA_DeleteOnClose` menus crash on focus
+teardown during destruction (`SIGSEGV` via `libsigchain`). Fix:
+`host.setFocus()` before the menu hides.
+
+## Considered Options
+
+- **Use QDialog with parent** — rejected: QDialog still creates a separate
+ window handle on Android. Only embedding as a plain child widget avoids the
+ second surface.
+- **Disable hardware acceleration** — rejected: unacceptable rendering
+ performance.
diff --git a/docs/adr/0005-android-native-binary-exec-via-linker64.md b/docs/adr/0005-android-native-binary-exec-via-linker64.md
new file mode 100644
index 00000000..6136ddfd
--- /dev/null
+++ b/docs/adr/0005-android-native-binary-exec-via-linker64.md
@@ -0,0 +1,22 @@
+# Android native binary execution via linker64, ffmpeg stays pre-bundled
+
+Android 10+ enforces W^X: files outside the APK's `nativeLibraryDir` cannot
+be executed. Downloaded binaries (N_m3u8DL-RE) must be invoked through
+`/system/bin/linker64 <path>` to bypass SELinux exec restrictions. This has
+been validated on-device.
+
+FFmpeg must stay pre-bundled in `nativeLibraryDir` (via the `gd3ffmpeg` p4a
+recipe). N_m3u8DL-RE internally exec's ffmpeg as a child process and does not
+use our linker64 wrapper — if ffmpeg is outside `nativeLibraryDir`,
+N_m3u8DL-RE's internal exec fails silently.
+
+Only N_m3u8DL-RE can be runtime hot-installed (~20 MB APK savings).
+Implementation is deferred.
+
+## Considered Options
+
+- **Bundle all binaries in nativeLibraryDir** — rejected as the long-term
+ goal: N_m3u8DL-RE is ~20 MB and updates independently of the app. Bundling
+ it inflates the APK and requires a full app update for runtime upgrades.
+- **Use app_process instead of linker64** — rejected: `app_process` requires
+ Java class loading, not suitable for plain native executables.
diff --git a/docs/adr/0006-android-package-name.md b/docs/adr/0006-android-package-name.md
new file mode 100644
index 00000000..58892642
--- /dev/null
+++ b/docs/adr/0006-android-package-name.md
@@ -0,0 +1,11 @@
+# Android package name: `io.github.xiaoyouchr.ghostdownloader`
+
+The Android package name is `io.github.xiaoyouchr.ghostdownloader` with
+display name "Ghost Downloader". The package name and display title are
+decoupled via environment variables (`GD3_PKG_DOMAIN`, `GD3_PKG_NAME`,
+`GD3_APP_TITLE`) in the build system — p4a's `--package` flag does not
+tolerate spaces, but the display title needs them.
+
+Once users install the app, the package name can never change — Android
+identifies apps by package name, and a change would force a full uninstall
+(losing user data and download history).