feat(android): keep the onion published through Doze with a foreground service

WireViewModel owned the single Wire node and called shutdown() from
onCleared(), so a backgrounded node died with the UI. Node lifetime is now
arbitrated by a pure policy (NodeLifetime) with two anchors — UI and
keep-alive — and one process-wide owner (NodeHost) that is the only caller
of flush()/shutdown() in the app.

KeepAliveService holds the process in the foreground behind an ongoing
notification from the "keep-alive" channel, started on unlock and stopped
by Lock or the notification action, so peers can still reach the device
while the screen is off.

- foregroundServiceType="specialUse": dataSync is documented as a finite
  transfer (upload/download/backup) and this is an always-on node
- POST_NOTIFICATIONS requested when the node becomes reachable; denial
  keeps the service running with no visible notice instead of failing
- re-unlock with a node the service kept alive adopts it rather than
  reopening the store the live node still holds
- 9 device-free unit tests for the start/stop/re-unlock state machine
This commit is contained in:
apk-dev 2026-09-12 16:34:40 -04:00
commit e923e06891
No known key found for this signature in database
11 changed files with 814 additions and 87 deletions

View file

@ -4,10 +4,33 @@
<!--
INTERNET is the only permission requested at first launch.
No camera (invites are typed/pasted, not scanned), no contacts, no
storage. Tor runs in-process; there is no foreground service and no
keep-alive notification in v1, so the node only lives while the app does.
storage.
Tor runs in-process, and the node is kept published across Doze and
backgrounding by KeepAliveService: a foreground service with an ongoing
notification, started on unlock, stopped by Lock or by the notification's
Stop action. Its anchors and the node's lifetime are arbitrated in
NodeHost/NodeLifetime; the service never owns the node itself.
-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<!--
specialUse, not dataSync. dataSync is documented as "data transfer
operations" (upload/download, backup/restore, import/export) and the
official alternatives guide points at WorkManager or a user-initiated
transfer job — all of which assume a transfer with an end. A node holding
an inbound onion service open and draining a retry queue has no end and no
progress to report, so specialUse ("valid foreground service use cases
that aren't covered by the other types") is the honest declaration. It
costs a Play Console justification; dataSync would cost a lie.
-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<!-- API 33+. Without it the keep-alive still runs, but its notification is
hidden from the drawer (the user sees the service in the Task Manager
instead). Denial is handled, not fatal: see WireViewModel.keepAliveNote. -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:allowBackup="false"
@ -30,6 +53,21 @@
</intent-filter>
</activity>
<!--
Not exported: only this app starts it, from a user-visible unlock.
stopWithTask is left at its default (false) on purpose — swiping the
task away must not take the onion offline; the activity is destroyed,
NodeHost flushes the outbox, and the onion stays published.
-->
<service
android:name=".keepalive.KeepAliveService"
android:exported="false"
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="peer-to-peer messaging node: keeps an inbound onion service published and an outbox retry queue running so peers can deliver messages while the app is backgrounded; there is no user-initiated transfer to bound the work" />
</service>
</application>
</manifest>