MIDI inputs list with per-source switches; reconnect dead virtual sources (v0.29.0)

- Setup -> MIDI Sync lists every MIDI source that can send to Sizzle with a
  switch and its state (Listening / Not connected / Off) plus Reconnect.
  Off-choices persist by a stable MidiSourceKey (Android ids change).
- When the app behind a virtual source dies/restarts, Android keeps the
  device and our connection, drops the service binding and sends no callback,
  so Sizzle went deaf to e.g. a restarted Mikro Seq. A worker thread now
  probes open virtual sources every 3 s (openOutputPort is null from a dead
  server) and reopens dead ones; resume re-scans; zero-open port status is
  also treated as dead.
- Verified on an Android 15 emulator: force-stop + restart Mikro Seq ->
  reconnected within ~3 s, notes flowing with both apps in background.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-12 22:32:04 +01:00
co-authored by Claude Opus 5.5
parent fb9a219e27
commit df64af8c9b
12 changed files with 424 additions and 29 deletions
+22 -1
View File
@@ -206,7 +206,8 @@ cc.tiredsysadmin.android.sizzle
│ ├── GamepadInput.kt KeyEvent/MotionEvent -> InputAction (rebindable)
│ ├── MidiByteParser.kt MIDI byte stream -> messages (running status, split packets, realtime, SysEx)
│ ├── MidiClock.kt MIDI clock send / follow
│ └── MidiInput.kt Opens MIDI sources (USB, BLE, other apps' virtual ports) -> MidiSoundModule / CC bindings
│ ├── MidiSourceKey.kt Stable key + display name for a MIDI source (persisted on/off choice)
│ └── MidiInput.kt Lists/opens MIDI sources (USB, BLE, other apps' virtual ports), reconnects dead ones -> MidiSoundModule / CC bindings
│
├── audio/ The real-time sound engine (own thread)
│ ├── Transport.kt TransportState snapshot published to the UI
@@ -347,6 +348,26 @@ foreground on pause, and its notification manager re-derives that state from
`InputRouter` as `NoteOn(…, sounded = true)`, which the ViewModel records but
does not play again. With no UI collecting, the router drops them.
**Which sources, and staying connected (0.29.0).** `MidiInput` keeps every
device that can send to us in `sources` (a `StateFlow<List<MidiSource>>` the
Setup screen lists with a switch each). Sources are listened to unless switched
off; the off-set is persisted as `MidiSourceKey`s (type + maker/product/name +
publishing service or Bluetooth address), because Android's device id changes on
every reconnect. The trap this solves: when the app behind a *virtual* source
dies and restarts (crash, update, force stop), Android keeps the device, keeps
our connection record, drops its service binding, and tells clients nothing:
no `onDeviceRemoved`, no `onDeviceStatusChanged` (checked on Android 15:
`dumpsys midi` shows `Status: null … mServiceConnection: null`). Our
`MidiOutputPort` just goes silent. So a worker thread probes each open virtual
source every 3 s with `MidiDevice.openOutputPort(0)` (null from a dead server;
a live one's probe port is closed at once) and reopens dead ones through
`MidiManager.openDevice`, which makes Android rebind the app's MIDI service.
`refresh()` runs on every Activity resume and the Setup "Reconnect" button closes
and reopens everything. A status report of zero open output ports while we hold
one is treated as dead too (events within 1 s of our own open/close are ignored).
USB and Bluetooth sources are not probed: Android reports their removal.
`MidiInput` logs connects/reconnects under the `SizzleMidi` tag.
**Verifying.** With the setting on and the app backgrounded, `adb shell dumpsys
activity processes` must not show `isFrozen=true` for the Sizzle process, and
another app's notes must keep sounding. Opening a virtual port binds the other