Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ADR-0005: Android background model: connectedDevice FGS and CompanionDeviceManager

  • Status: accepted
  • Date: 2026-11-08
  • Deciders: maintainer, after the November 2026 research round
  • Tags: android, reliability

Context

Tether on the phone must keep a TLS connection open to the desktop for hours with the screen off, deliver notifications within a second, and survive Doze, app standby buckets, OEM “sleeping apps” killers and reboots. Android 14/15 constrain how a non-system app may do that:

  • Foreground services need a declared type. dataSync is capped at 6 hours per 24 on Android 15; specialUse is accepted but is a flag for review; the connectedDevice type exists precisely for apps that maintain a connection to an external device and requires FOREGROUND_SERVICE_CONNECTED_DEVICE plus at least one device-connection runtime/normal permission (we hold CHANGE_WIFI_MULTICAST_STATE, which qualifies).
  • Doze suspends network access except in maintenance windows unless the app is on the battery-optimisation allowlist. The allowlist dialog (ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) is a permission Google Play restricts to companion-style apps; Tether is sideloaded (ADR-0008) and is a companion app, so the exemption is both available and legitimate.
  • Notification access (NotificationListenerService) is what we need for notification sync. On Android 13+ sideloaded apps hit the “restricted setting” gate: the toggle is disabled until the user goes to App info → Allow restricted settings. The listener is also unbound whenever the OS kills the process.
  • Android 15 OTP redaction: notifications that look like one-time codes are redacted for untrusted listeners. The RECEIVE_SENSITIVE_NOTIFICATIONS permission is role/signature-gated, but apps holding a CompanionDeviceManager association are exempt, exactly because they are expected to forward notifications to a companion device.
  • CompanionDeviceManager (CDM) also gives: eligibility to start foreground services and run in the background when the companion is present (REQUEST_COMPANION_RUN_IN_BACKGROUND, REQUEST_COMPANION_USE_DATA_IN_BACKGROUND), startObservingDevicePresence/CompanionDeviceService callbacks, and a system-level “this app is associated with that device” record that OEM battery killers (Samsung’s “sleeping apps”, in our testing) treat more gently. Since Android 13 an association can be self-managed (AssociationRequest.Builder.setSelfManaged(true), REQUEST_COMPANION_SELF_MANAGED): the app does the discovery itself and the system only shows a confirmation dialog with the display name we pass. That fits our mDNS/TLS discovery; the system’s own Bluetooth/Wi-Fi scan dialog would not find a TLS peer.
  • OEM killers: Samsung One UI puts apps to sleep after days of non-use (Battery → Background usage limits) unless they are listed under Never sleeping apps; Xiaomi needs Autostart. No API toggles these; onboarding must guide.

Phone Link (Link to Windows) uses the same primitives: a persistent foreground service, CDM association and the battery exemption, and OEM whitelisting where it ships as a system app.

Decision

  • TetherService is a foreground service of type connectedDevice with a persistent, low-importance notification (Connected to <PC>). START_STICKY, restarted on BOOT_COMPLETED and MY_PACKAGE_REPLACED; reconnect driven by ConnectivityManager.NetworkCallback and the session’s backoff.
  • CDM self-managed association is created right after the first successful pairing (the display name is the desktop’s Hello.name), one association per paired desktop, removed on unpair. The app declares REQUEST_COMPANION_SELF_MANAGED, REQUEST_COMPANION_RUN_IN_BACKGROUND, REQUEST_COMPANION_USE_DATA_IN_BACKGROUND, and uses CompanionDeviceService presence callbacks as an additional reconnect trigger.
  • Battery optimisation exemption is requested in onboarding with an explanation; the app shows a persistent warning if it is not granted.
  • Notification access is requested in onboarding with the restricted-settings detection and instructions; NotificationListenerService is kept minimal and forwards to the service.
  • Samsung/Xiaomi guidance is a dedicated onboarding step shown only on those manufacturers (Build.MANUFACTURER), with deep links where they exist.
  • No wake locks except a partial wake lock held for the duration of a file or blob transfer. Keepalive relies on the TCP socket plus the exemption; the 15 s ping interval is tolerated by Doze maintenance windows when the exemption is missing (degraded: latency up to minutes, reconnect on screen-on).
  • No FCM/Play Services for wake-ups (ADR-0008, ADR-0010).

Consequences

Positive

  • Honest use of the platform’s intended mechanism for exactly this use case; no tricks that an OS update is likely to break.
  • CDM exempts us from OTP redaction without any shell-level grant, so 2FA codes reach the desktop in the normal tier.
  • The association also improves survival on Samsung in our tests and gives a system UI entry the user recognises.

Negative / risks

  • Onboarding is long: notifications permission, notification access (+ restricted settings), battery exemption, CDM dialog, OEM step. Mitigation: a step-by-step wizard with status checks, and docs/how-to/set-up-android-permissions.md.
  • The persistent notification is visible forever. Mitigation: minimal importance, no sound, user can hide the channel (Android then shows a generic “running in background” summary).
  • If the user declines the battery exemption, delivery latency with the screen off degrades. Accepted; the app shows the state.
  • CDM self-managed dialogs are system-styled and vary by OEM; a user tapping “Don’t allow” loses the exemptions silently. Mitigation: detect missing association and offer to redo it.
  • connectedDevice requires the companion-connection permission prerequisite; if Google tightens the rule to Bluetooth/USB-only permissions, we fall back to specialUse (sideload-only, so no store review).

Alternatives considered

  • WorkManager / JobScheduler polling: 15-minute minimum and no persistent socket. Useless for notifications.
  • FCM push to wake the phone: requires Play Services and a cloud project; contradicts ADR-0008/0010.
  • dataSync or specialUse FGS type: dataSync is time-capped; specialUse is the fallback, not the first choice.
  • AccessibilityService to stay alive: abuse of an accessibility API, triggers the same restricted-settings gate and worse warnings. Rejected.
  • Permanent wake lock: battery drain users would uninstall over. Rejected.
  • Skip CDM and rely on the shell helper (ADR-0007) to grant RECEIVE_SENSITIVE_NOTIFICATIONS: works only in the enhanced tier; OTPs are the single most wanted notification type and must work in the normal tier.

Amendment 2026-11-08: self-managed CDM is privileged on recent builds

Field finding during Milestone 1 bring-up on a Galaxy A55 (Android 16, One UI 8.5): pm list permissions -f reports android.permission.REQUEST_COMPANION_SELF_MANAGED with protectionLevel: signature|privileged, and CompanionDeviceManager.associate() with setSelfManaged(true) throws SecurityException: Application does not hold android.permission.REQUEST_COMPANION_SELF_MANAGED. A sideloaded app therefore cannot create a self-managed association on such builds (Link to Windows can because OEMs preinstall it as a privileged app).

Consequences:

  • The onboarding step detects the missing grant and skips itself with an explanation.
  • The CDM benefits (Android 15+ OTP-notification visibility, background eligibility) are not available in the normal tier on these builds. OTP visibility moves to the enhanced tier (cmd appops set org.tether.android RECEIVE_SENSITIVE_NOTIFICATIONS allow via the adb shell helper, Milestone 4).
  • Milestone 3 will attempt a regular (non-self-managed) association using a BluetoothDeviceFilter that matches the desktop’s Bluetooth adapter, since the PC must be Bluetooth-paired for calls anyway. That path uses only normal permissions.
  • Background reliability in Milestone 1 rests on the connectedDevice foreground service plus the battery-optimisation exemption; verified to survive forced Doze (dumpsys deviceidle force-idle) with notifications still delivered.