Files
tsunagi/crates/tsunagi-android/src/android/display.rs
T
Aleksandr Bogomiakov 8c4c2fa34e Fold network cards and show which peer carries exit traffic
Start the tray and Android overviews with each network folded under its
bold name. Keep the selected exit, its host name and its sent and received
byte totals visible. Put VIP beside Copy secret and Show devices.

Default to bytes and make the unit switch update the desktop traffic
column, totals, rates and plots together. Open the devices window at
1000 points wide and bound the network and per-peer plots. Use both sides
of the phone detail view for transport, latency, version and traffic.

Attach counters to the current network and exit peer. Count off-mesh IP
bytes after carrier acceptance and replies after local interface delivery.
Changing the selection starts new totals. Ordinary mesh traffic to the
same peer and rejected packets stay out of the exit counters.

Advertise the agent release as an optional bounded trailer on an
authenticated control v8 announcement. Older readers can ignore it and
older peers display an unknown version. Release labels live for the
session and do not participate in authentication or wire negotiation.
The new status fields require local IPC v25, so update the daemon, CLI
and tray together. Existing mesh identities and databases need no migration.

On Android, request system interface consent on the first app launch.
Respect an explicit stop or declined consent across launches. Start
Tsunagi Agent re-enables automatic startup.

Checked formatting and strict workspace Clippy on macOS arm64. The full
locked workspace suite passed 588 tests with four opt-in tests ignored.
Built the tray and Android debug APKs. Both instrumentation tests passed
on an Android 37 arm64 emulator, including app startup, stop and restart.
Checked the first-run permission prompt and the rendered phone and desktop
layouts. Native Linux and Windows tests were not run on this host.
2026-11-25 14:33:50 +02:00

268 lines
8.5 KiB
Rust

//! Android owns the GLES surface, keyboard and clipboard. egui owns the widgets.
use crate::view::{Phone, SystemAction};
use egui::{
Event, ImeEvent, Key, Modifiers, PointerButton, Pos2, TouchDeviceId, TouchId, TouchPhase,
};
use egui_glow::{Painter, glow};
use jni::{
JNIEnv,
objects::{JObject, JString, JValue},
sys::{jboolean, jfloat, jint, jlong},
};
use std::{
cell::RefCell,
ffi::{c_char, c_void},
sync::Arc,
time::Instant,
};
#[link(name = "EGL")]
unsafe extern "C" {
fn eglGetProcAddress(name: *const c_char) -> *const c_void;
}
struct Display {
painter: Painter,
ctx: egui::Context,
ui: Phone,
events: Vec<Event>,
primary: Option<i32>,
start: Instant,
ime: i32,
exit_active: bool,
}
thread_local! { static DISPLAY: RefCell<Option<Display>> = const {RefCell::new(None)}; }
fn with_display<T: Default>(f: impl FnOnce(&mut Display) -> T) -> T {
DISPLAY.with_borrow_mut(|d| d.as_mut().map(f).unwrap_or_default())
}
#[unsafe(no_mangle)]
extern "system" fn Java_cy_hexor_tsunagi_Native_surface(mut env: JNIEnv, _: JObject) {
// SAFETY: GLSurfaceView invokes this callback on the GL thread with its ES3 context current.
let gl = unsafe {
glow::Context::from_loader_function_cstr(|name| eglGetProcAddress(name.as_ptr()))
};
let Ok(painter) = Painter::new(Arc::new(gl), "", None, false) else {
let _ = env.throw_new(
"java/lang/IllegalStateException",
"Could not initialise OpenGL ES",
);
return;
};
let ctx = egui::Context::default();
crate::view::configure(&ctx);
DISPLAY.with_borrow_mut(|d| {
// If Android discarded the old EGL context, its GPU resources are already gone.
let ui = d.take().map(|old| old.ui).unwrap_or_default();
*d = Some(Display {
painter,
ctx,
ui,
events: Vec::new(),
primary: None,
start: Instant::now(),
ime: -1,
exit_active: false,
});
});
}
#[unsafe(no_mangle)]
extern "system" fn Java_cy_hexor_tsunagi_Native_draw(
mut env: JNIEnv,
_: JObject,
host: JObject,
w: jint,
h: jint,
scale: jfloat,
service_started: jboolean,
) -> jlong {
with_display(|d| {
let size = egui::vec2(w as f32, h as f32) / scale;
let mut input = egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, size)),
events: std::mem::take(&mut d.events),
time: Some(d.start.elapsed().as_secs_f64()),
..Default::default()
};
if let Some(info) = input.viewports.get_mut(&egui::ViewportId::ROOT) {
info.native_pixels_per_point = Some(scale);
}
let mut action = None;
let state = match super::status() {
crate::service::State::Off if service_started != 0 => crate::service::State::Starting,
state => state,
};
let exit_active = matches!(&state, crate::service::State::Ready(report, _) if report.networks.iter().any(|n| n.active && n.exit.via.is_some()));
if exit_active && !d.exit_active {
let _ = env.call_method(&host, "requestExitNotifications", "()V", &[]);
}
d.exit_active = exit_active;
let output = d.ctx.run(input, |ctx| {
action = d.ui.draw(ctx, &state, super::send).or(action);
});
if let Some(action) = action {
let method = match action {
SystemAction::Start => "startAgent",
SystemAction::Stop => "stopAgent",
SystemAction::PasteSecret => "pasteSecret",
SystemAction::Finish => "closeScreen",
};
let _ = env.call_method(&host, method, "()V", &[]);
}
if let Some(secret) = d.ui.take_copied_secret()
&& let Ok(text) = env.new_string(secret.as_str())
{
let _ = env.call_method(
&host,
"copySecret",
"(Ljava/lang/String;)V",
&[JValue::Object(&text)],
);
}
for command in output.platform_output.commands {
let (method, value) = match command {
egui::OutputCommand::CopyText(text) => ("copyText", text),
egui::OutputCommand::OpenUrl(url) => ("openUrl", url.url),
_ => continue,
};
if let Ok(text) = env.new_string(value) {
let _ = env.call_method(
&host,
method,
"(Ljava/lang/String;)V",
&[JValue::Object(&text)],
);
}
}
let ime = if output.platform_output.ime.is_none() {
0
} else if d.ui.secret_focused(&d.ctx) {
2
} else {
1
};
if ime != d.ime {
d.ime = ime;
let _ = env.call_method(&host, "keyboard", "(I)V", &[JValue::Int(ime)]);
}
let ppp = output.pixels_per_point;
let primitives = d.ctx.tessellate(output.shapes, ppp);
let pixels = [w as u32, h as u32];
d.painter.clear(pixels, [0.1, 0.1, 0.1, 1.0]);
d.painter
.paint_and_update_textures(pixels, ppp, &primitives, &output.textures_delta);
output
.viewport_output
.get(&egui::ViewportId::ROOT)
.map_or(1000, |v| {
v.repaint_delay.as_millis().clamp(16, 1000) as jlong
})
})
}
#[unsafe(no_mangle)]
extern "system" fn Java_cy_hexor_tsunagi_Native_touch(
_: JNIEnv,
_: JObject,
action: jint,
id: jint,
x: jfloat,
y: jfloat,
) {
with_display(|d| {
let pos = egui::pos2(x, y);
let phase = match action {
0 => TouchPhase::Start,
1 => TouchPhase::Move,
2 => TouchPhase::End,
_ => TouchPhase::Cancel,
};
d.events.push(Event::Touch {
device_id: TouchDeviceId(0),
id: TouchId(id as u64),
phase,
pos,
force: None,
});
if phase == TouchPhase::Start && d.primary.is_none() {
d.primary = Some(id);
}
if d.primary != Some(id) {
return;
}
if phase != TouchPhase::Cancel {
d.events.push(Event::PointerMoved(pos));
}
if phase != TouchPhase::Move {
if phase == TouchPhase::Cancel {
d.events.push(Event::PointerGone);
}
d.events.push(Event::PointerButton {
pos,
button: PointerButton::Primary,
pressed: phase == TouchPhase::Start,
modifiers: Modifiers::NONE,
});
}
if matches!(phase, TouchPhase::Cancel | TouchPhase::End) {
d.events.push(Event::PointerGone);
d.primary = None;
}
});
}
#[unsafe(no_mangle)]
extern "system" fn Java_cy_hexor_tsunagi_Native_input(
mut env: JNIEnv,
_: JObject,
kind: jint,
value: JString,
) {
let Ok(text) = env.get_string(&value) else {
return;
};
let text = String::from(text);
with_display(|d| match kind {
0 => d.events.push(Event::Text(text)),
1 => d.events.push(Event::Ime(ImeEvent::Enabled)),
2 => d.events.push(Event::Ime(ImeEvent::Preedit(text))),
3 => {
d.events.push(Event::Ime(ImeEvent::Commit(text)));
d.events.push(Event::Ime(ImeEvent::Disabled));
}
4 => d.ui.paste_secret(text),
5 => d.ui.error(text),
_ => {}
});
}
#[unsafe(no_mangle)]
extern "system" fn Java_cy_hexor_tsunagi_Native_key(_: JNIEnv, _: JObject, key: jint) {
let key = match key {
67 => Key::Backspace,
112 => Key::Delete,
66 => Key::Enter,
21 => Key::ArrowLeft,
22 => Key::ArrowRight,
61 => Key::Tab,
_ => return,
};
with_display(|d| {
for pressed in [true, false] {
d.events.push(Event::Key {
key,
physical_key: None,
pressed,
repeat: false,
modifiers: Modifiers::NONE,
});
}
});
}
#[unsafe(no_mangle)]
extern "system" fn Java_cy_hexor_tsunagi_Native_back(mut env: JNIEnv, _: JObject, host: JObject) {
with_display(|d| {
if d.ui.back() == Some(SystemAction::Finish) {
let _ = env.call_method(&host, "closeScreen", "()V", &[]);
}
});
}