Files
VoelinClient/crates/voelin-android/src/foreground.rs
T
Claude e050148a32 Rename the project to Voelin; packages hold only the app, tsgw is a Linux server package
The product is Voelin (app id io.github.faumaray.Voelin), everywhere:
- crates tsc-* -> voelin-* (and voelin-fuzz), the desktop binary
  tsc-desktop -> voelin, the development CLI tsctl -> voelinctl;
- the Android app: applicationId, Kotlin package io.github.faumaray.voelin,
  VoelinApp, libvoelin_android.so (JNI class names follow);
- TSC_* environment variables -> VOELIN_*, Gradle properties tsc.* ->
  voelin.*, signing secrets VOELIN_SIGNING_*;
- the data/config/cache/state folder tsc -> voelin, the keyring service,
  thread names, the dev compose project;
- desktop file, metainfo, icon, Flatpak manifest, NSIS installer, docs.
The gateway keeps its name: bin tsgw (crate voelin-gateway), TSGW_* env
vars, the tsgw.v1+json protocol, the tsgw image. The window title and the
About page show "Voelin <version>" from voelin_platform::APP_NAME.

Packages (scripts/package.sh): the app only (voelin, voelin.exe, APKs,
Flatpak). voelinctl is a development tool and in no package. tsgw is for
servers and now its own Linux package, tsgw_<v>_amd64.deb and
tsgw-<v>-linux-x86_64.tar.gz, with a sandboxed systemd unit
(packaging/linux/tsgw.service; /etc/tsgw config, /var/lib/tsgw state) and
dependencies of its own (no GUI libraries). docs/gateway-admin.md has an
Install section.

Fixes:
- package-windows: makensis on Windows found no file through "D:/a/..."
  paths; pass Windows paths (cygpath -w).
- StartupWMClass is the app id, which the app sets as its X11 WM_CLASS
  (it was the binary name, so desktops could not match the window).
- Gradle wrapper download timeout 10 s -> 60 s (slow proxies).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
2026-09-26 21:11:17 +00:00

133 lines
4.0 KiB
Rust

//! When the app needs its voice foreground service.
//!
//! Android keeps the microphone and the process of a backgrounded app only
//! while a foreground service with a visible notification runs. The service
//! runs while any session has a voice connection (connecting or connected);
//! its notification names the servers and offers Mute and Disconnect.
use std::collections::BTreeMap;
use voelin_core::{Event, SessionId, VoiceState};
/// What the voice notification shows.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VoiceNotice {
pub text: String,
/// The microphone is muted in every voice session (the action unmutes).
pub muted: bool,
}
#[derive(Default)]
pub struct Foreground {
sessions: BTreeMap<SessionId, Session>,
}
#[derive(Default)]
struct Session {
name: Option<String>,
voice: VoiceState,
muted: bool,
}
impl Foreground {
/// Apply `event`. Returns the new notification when it changed:
/// `Some(None)` means the service should stop.
pub fn update(&mut self, event: &Event) -> Option<Option<VoiceNotice>> {
let before = self.notice();
match event {
Event::State { session, state } => {
let s = self.sessions.entry(*session).or_default();
s.voice = state.voice;
s.muted = state.input_muted;
}
Event::ServerInfo { session, name, .. } => {
self.sessions.entry(*session).or_default().name = Some(name.clone());
}
_ => return None,
}
let after = self.notice();
(after != before).then_some(after)
}
fn active(&self) -> impl Iterator<Item = (&SessionId, &Session)> {
self.sessions.iter().filter(|(_, s)| s.voice != VoiceState::Disconnected)
}
/// Sessions with a voice connection (for the notification's actions).
pub fn voice_sessions(&self) -> Vec<SessionId> {
self.active().map(|(id, _)| *id).collect()
}
/// The notification, `None` without voice.
pub fn notice(&self) -> Option<VoiceNotice> {
let active: Vec<&Session> = self.active().map(|(_, s)| s).collect();
if active.is_empty() {
return None;
}
let names: Vec<&str> =
active.iter().map(|s| s.name.as_deref().unwrap_or("a server")).collect();
let names = names.join(", ");
let text = if active.iter().all(|s| s.voice == VoiceState::Connecting) {
format!("Connecting to {names}")
} else {
format!("In voice on {names}")
};
Some(VoiceNotice { text, muted: active.iter().all(|s| s.muted) })
}
}
#[cfg(test)]
mod tests {
use voelin_core::SessionState;
use voelin_model::{Capabilities, ServerFlavor};
use super::*;
fn voice(session: SessionId, voice: VoiceState, input_muted: bool) -> Event {
Event::State {
session,
state: SessionState { voice, input_muted, ..SessionState::default() },
}
}
fn notice(text: &str, muted: bool) -> Option<Option<VoiceNotice>> {
Some(Some(VoiceNotice { text: text.into(), muted }))
}
#[test]
fn service_follows_voice_connections() {
let mut fg = Foreground::default();
assert_eq!(fg.update(&voice(1, VoiceState::Disconnected, false)), None);
assert_eq!(
fg.update(&voice(1, VoiceState::Connecting, false)),
notice("Connecting to a server", false)
);
let info = Event::ServerInfo {
session: 1,
name: "Home".into(),
flavor: ServerFlavor::Unknown(String::new()),
capabilities: Capabilities::default(),
};
assert_eq!(fg.update(&info), notice("Connecting to Home", false));
assert_eq!(
fg.update(&voice(1, VoiceState::Connected, false)),
notice("In voice on Home", false)
);
assert_eq!(fg.update(&voice(1, VoiceState::Connected, false)), None, "no change");
assert_eq!(
fg.update(&voice(1, VoiceState::Connected, true)),
notice("In voice on Home", true)
);
// A second session that is not muted: the action mutes both.
assert_eq!(
fg.update(&voice(2, VoiceState::Connecting, false)),
notice("In voice on Home, a server", false)
);
assert_eq!(fg.voice_sessions(), [1, 2]);
assert_eq!(fg.update(&Event::Error { session: 1, message: "x".into() }), None);
fg.update(&voice(2, VoiceState::Disconnected, false));
assert_eq!(fg.update(&voice(1, VoiceState::Disconnected, true)), Some(None));
assert!(fg.voice_sessions().is_empty());
}
}