From 9356917794db14653b70f2d108c51a366b5ef4fc Mon Sep 17 00:00:00 2001 From: Muhammad Rayhan Hamada Budiman Date: Thu, 19 Nov 2026 09:02:41 +0700 Subject: [PATCH 1/3] feat: list connected devices Read attached devices and emulators from the SDK's adb, show model, transport state, and refresh on navigation. --- README.md | 5 +- src/android.rs | 183 ++++++++++++++++++++++++++++++++++++++++- src/app.rs | 216 +++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 395 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5321697..0f79f62 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,9 @@ [![CI](https://github.com/RayhanHamada/android-cli-gui/actions/workflows/ci.yml/badge.svg)](https://github.com/RayhanHamada/android-cli-gui/actions/workflows/ci.yml) Unofficial GUI for the [Android CLI](https://developer.android.com/tools/agents/android-cli). -Manage Android SDK platforms and build tools without installing Android Studio. Data -comes from the official Android CLI and Android SDK sources. +Manage Android SDK platforms and build tools, inspect connected devices and +emulators, without installing Android Studio. Data comes from the official +Android CLI and Android SDK sources. ## Install diff --git a/src/android.rs b/src/android.rs index aff62e4..c9f8bcc 100644 --- a/src/android.rs +++ b/src/android.rs @@ -66,6 +66,27 @@ impl BuildTool { } } +/// A device reported by `adb devices -l`. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Device { + /// `adb` serial, e.g. `emulator-5554` or a physical device serial. + pub serial: String, + /// Raw adb state: `device`, `unauthorized`, `offline`, etc. + pub state: String, + pub model: Option, + pub product: Option, + pub device: Option, + pub transport_id: Option, + pub usb: Option, + pub is_emulator: bool, +} + +impl Device { + pub fn is_online(&self) -> bool { + self.state == "device" + } +} + /// A point-in-time view of the SDK that the UI renders. #[derive(Clone, Debug, Default)] pub struct SdkSnapshot { @@ -75,8 +96,12 @@ pub struct SdkSnapshot { pub root: Option, pub platforms: Vec, pub build_tools: Vec, - /// Non-fatal CLI or parse failure. + pub devices: Vec, + /// Path to the `adb` binary used for device discovery. + pub adb: Option, + /// Non-fatal CLI, adb, or parse failure. pub error: Option, + pub devices_error: Option, } /// Tracks the running `android` child so shutdown can stop it cleanly. @@ -234,6 +259,116 @@ fn is_executable(path: &std::path::Path) -> bool { path.is_file() } +fn adb_binary_name() -> &'static str { + if cfg!(windows) { "adb.exe" } else { "adb" } +} + +fn find_adb(root: Option<&std::path::Path>) -> Option { + let name = adb_binary_name(); + let mut sdk_roots: Vec = Vec::new(); + if let Some(root) = root { + sdk_roots.push(root.to_path_buf()); + } + for var in ["ANDROID_HOME", "ANDROID_SDK_ROOT"] { + if let Some(root) = env::var_os(var) { + sdk_roots.push(PathBuf::from(root)); + } + } + + for sdk_root in sdk_roots { + let adb = sdk_root.join("platform-tools").join(name); + if is_executable(&adb) { + return Some(adb); + } + } + + search_paths() + .into_iter() + .map(|dir| dir.join(name)) + .find(|path| is_executable(path)) +} + +impl SdkSnapshot { + /// Refresh attached devices from the SDK's `adb devices -l`. + pub fn refresh_devices(&mut self) { + self.adb = None; + self.devices.clear(); + self.devices_error = None; + + let Some(adb) = find_adb(self.root.as_deref()) else { + self.devices_error = Some(format!( + "`{}` was not found. Install Android SDK Platform-Tools with `android sdk install platform-tools`.", + adb_binary_name() + )); + return; + }; + + self.adb = Some(adb.clone()); + let running = RunningCli::default(); + match run(&adb, None, &["devices", "-l"], &running) { + Ok(output) => self.devices = parse_devices(&output), + Err(err) => self.devices_error = Some(err), + } + } +} + +/// Parse `adb devices -l`: +/// +/// ```text +/// List of devices attached +/// emulator-5554 device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64a transport_id:1 +/// R58M12345 unauthorized usb:1-1 transport_id:2 +/// ``` +fn parse_devices(output: &str) -> Vec { + output + .lines() + .filter_map(|line| { + let line = line.trim(); + if line.is_empty() + || line.starts_with("List of devices attached") + || line.starts_with('*') + { + return None; + } + + let mut parts = line.split_whitespace(); + let serial = parts.next()?.to_string(); + let state = parts.next()?.to_string(); + let mut model = None; + let mut product = None; + let mut device = None; + let mut transport_id = None; + let mut usb = None; + + for field in parts { + let Some((key, value)) = field.split_once(':') else { + continue; + }; + match key { + "model" => model = Some(value.to_string()), + "product" => product = Some(value.to_string()), + "device" => device = Some(value.to_string()), + "transport_id" => transport_id = Some(value.to_string()), + "usb" => usb = Some(value.to_string()), + _ => {} + } + } + + let is_emulator = serial.starts_with("emulator-"); + Some(Device { + serial, + state, + model, + product, + device, + transport_id, + usb, + is_emulator, + }) + }) + .collect() +} + pub fn snapshot() -> SdkSnapshot { let Some(cli) = find_cli() else { return SdkSnapshot { @@ -268,6 +403,7 @@ pub fn snapshot() -> SdkSnapshot { } Err(err) => snapshot.error = Some(err), } + snapshot.refresh_devices(); snapshot } @@ -471,8 +607,12 @@ fn run( .map(str::trim) .find(|line| !line.is_empty()) .unwrap_or("no output"); + let program = bin + .file_name() + .and_then(|name| name.to_str()) + .unwrap_or("command"); Err(format!( - "`android` exited {}: {}", + "`{program}` exited {}: {}", output.status, truncate(tail, 200) )) @@ -569,6 +709,45 @@ Available packages: assert!(!tool.matches("35")); } + #[test] + fn parses_adb_devices() { + let output = "\ +List of devices attached +emulator-5554 device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64a transport_id:1 +R58M12345 unauthorized usb:1-1 transport_id:2 +"; + let devices = parse_devices(output); + + assert_eq!(devices.len(), 2); + assert_eq!( + devices[0], + Device { + serial: "emulator-5554".into(), + state: "device".into(), + model: Some("sdk_gphone64_arm64".into()), + product: Some("sdk_gphone64_arm64".into()), + device: Some("emu64a".into()), + transport_id: Some("1".into()), + usb: None, + is_emulator: true, + } + ); + assert!(devices[0].is_online()); + assert_eq!(devices[1].state, "unauthorized"); + assert_eq!(devices[1].usb.as_deref(), Some("1-1")); + assert!(!devices[1].is_online()); + } + + #[test] + fn ignores_adb_noise_and_blank_lines() { + let output = "\ +List of devices attached + +* daemon not running; starting now at tcp:5037 +"; + assert!(parse_devices(output).is_empty()); + } + #[test] fn parses_installed_and_available_platforms() { let output = "\ diff --git a/src/app.rs b/src/app.rs index eb3ff3b..33a4065 100644 --- a/src/app.rs +++ b/src/app.rs @@ -44,7 +44,7 @@ impl Page { Page::BuildTools => { "Android SDK Build-Tools — installed and available, read from the Android CLI." } - Page::Devices => "Connected devices and emulators.", + Page::Devices => "Devices connected through adb, including running emulators.", Page::Emulators => "Android Virtual Devices.", Page::Settings => "SDK root, Java, and CLI configuration.", } @@ -52,10 +52,7 @@ impl Page { fn body(self) -> &'static str { match self { - Page::SdkVersions | Page::BuildTools => "", - Page::Devices => { - "Not implemented yet. Will list connected devices from `android device` and `adb devices -l`." - } + Page::SdkVersions | Page::BuildTools | Page::Devices => "", Page::Emulators => { "Not implemented yet. Will list and manage AVDs with `android emulator`." } @@ -262,6 +259,9 @@ impl App { if this.page != page { this.page = page; this.confirm_remove = None; + if page == Page::Devices { + this.sdk.refresh_devices(); + } cx.notify(); } }); @@ -365,6 +365,7 @@ impl App { match self.page { Page::SdkVersions => self.render_sdk_versions(cx), Page::BuildTools => self.render_build_tools(cx), + Page::Devices => self.render_devices(cx), page => Self::render_placeholder(page, cx), } } @@ -454,6 +455,211 @@ impl App { self.render_package_page(rows, summary, empty_title, empty_detail, false, cx) } + fn render_devices(&mut self, cx: &mut Context) -> AnyElement { + if self.sdk.cli.is_none() { + return self.render_cli_missing(cx); + } + + let border = cx.theme().border; + let muted = cx.theme().muted_foreground; + let total = self.sdk.devices.len(); + let online = self + .sdk + .devices + .iter() + .filter(|device| device.is_online()) + .count(); + let summary = format!("{online} online · {total} attached"); + + let header = div() + .flex() + .items_center() + .gap_4() + .px_6() + .py_2() + .border_b_1() + .border_color(border) + .child( + Label::new("DEVICE") + .text_size(px(10.)) + .text_color(muted) + .flex_1(), + ) + .child( + Label::new("TYPE") + .text_size(px(10.)) + .text_color(muted) + .w(px(90.)), + ) + .child( + Label::new("STATE") + .text_size(px(10.)) + .text_color(muted) + .w(px(110.)), + ) + .child( + Label::new("DETAILS") + .text_size(px(10.)) + .text_color(muted) + .flex_1(), + ); + + let mut content = div().flex().flex_col().w_full().child( + div() + .px_6() + .py_2() + .child(Label::new(summary).text_size(px(11.)).text_color(muted)), + ); + + if let Some(error) = &self.sdk.devices_error { + content = content.child(Self::render_error(error, cx)); + } + content = content.child(header); + + if self.sdk.devices.is_empty() { + let (title, detail) = if self.sdk.devices_error.is_some() { + ( + "Device listing unavailable".to_string(), + "Fix the adb issue above, then click Refresh.".to_string(), + ) + } else { + ( + "No devices connected".to_string(), + "Start an emulator with `android emulator start `, or connect a \ + device with USB debugging enabled and accept the debugging prompt." + .to_string(), + ) + }; + content = content.child( + div() + .flex() + .flex_col() + .gap_1() + .px_6() + .py_8() + .max_w(px(680.)) + .child( + Label::new(title) + .text_size(px(14.)) + .font_weight(FontWeight::SEMIBOLD), + ) + .child( + Label::new(detail) + .text_size(px(12.)) + .text_color(muted) + .whitespace_normal(), + ), + ); + } else { + for (ix, device) in self.sdk.devices.iter().enumerate() { + content = content.child(self.render_device_row(device, ix, cx)); + } + } + + content + .child( + div().px_6().py_4().child( + Label::new( + "Devices and states come from `adb devices -l` using the SDK's platform-tools.", + ) + .text_size(px(11.)) + .text_color(muted), + ), + ) + .into_any_element() + } + + fn render_device_row( + &self, + device: &android::Device, + ix: usize, + cx: &mut Context, + ) -> impl IntoElement { + let border = cx.theme().border; + let muted = cx.theme().muted_foreground; + let state = match device.state.as_str() { + "device" => "Online", + "unauthorized" => "Unauthorized", + "offline" => "Offline", + state => state, + }; + let state_color = if device.is_online() { + cx.theme().success + } else if device.state == "unauthorized" || device.state == "offline" { + cx.theme().danger + } else { + muted + }; + let kind = if device.is_emulator { + "Emulator" + } else { + "Device" + }; + + let mut details = Vec::new(); + if let Some(model) = &device.model { + details.push(model.clone()); + } else if let Some(product) = &device.product { + details.push(product.clone()); + } + if let Some(product) = &device.product { + if device.model.as_deref() != Some(product) { + details.push(format!("product {product}")); + } + } + if let Some(device_name) = &device.device { + details.push(format!("device {device_name}")); + } + if let Some(usb) = &device.usb { + details.push(format!("USB {usb}")); + } + let details = if details.is_empty() { + "—".to_string() + } else { + details.join(" · ") + }; + + div() + .flex() + .items_center() + .gap_4() + .px_6() + .py_2() + .border_b_1() + .border_color(border) + .child( + div().flex_1().min_w(px(0.)).child( + Label::new(device.serial.clone()) + .id(("device-serial", ix)) + .text_size(px(12.)) + .truncate(), + ), + ) + .child( + Label::new(kind) + .id(("device-kind", ix)) + .text_size(px(12.)) + .text_color(muted) + .w(px(90.)), + ) + .child( + Label::new(state) + .id(("device-state", ix)) + .text_size(px(12.)) + .text_color(state_color) + .w(px(110.)), + ) + .child( + div().flex_1().min_w(px(0.)).child( + Label::new(details) + .id(("device-details", ix)) + .text_size(px(12.)) + .text_color(muted) + .truncate(), + ), + ) + } + fn search_query(&self, cx: &mut Context) -> (String, String) { let raw_query = self.search_input.read(cx).value().trim().to_string(); let query = raw_query.to_lowercase(); From 8e8d4f40122c3bba3a8132d583fc9b09f6cfabe1 Mon Sep 17 00:00:00 2001 From: Muhammad Rayhan Hamada Budiman Date: Thu, 19 Nov 2026 09:10:49 +0700 Subject: [PATCH 2/3] feat: list all configured AVDs Read every AVD from `android emulator list --long`, merge running ones with adb details, and clamp fixed-width table parsing. --- src/android.rs | 132 ++++++++++++++++++++++++++++++++- src/app.rs | 198 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 269 insertions(+), 61 deletions(-) diff --git a/src/android.rs b/src/android.rs index c9f8bcc..629adfa 100644 --- a/src/android.rs +++ b/src/android.rs @@ -87,6 +87,22 @@ impl Device { } } +/// A configured Android Virtual Device from `android emulator list --long`. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Emulator { + pub id: String, + pub name: String, + pub api_level: String, + pub status: String, + pub serial: Option, +} + +impl Emulator { + pub fn is_online(&self) -> bool { + self.status.eq_ignore_ascii_case("online") || self.status.eq_ignore_ascii_case("running") + } +} + /// A point-in-time view of the SDK that the UI renders. #[derive(Clone, Debug, Default)] pub struct SdkSnapshot { @@ -97,6 +113,7 @@ pub struct SdkSnapshot { pub platforms: Vec, pub build_tools: Vec, pub devices: Vec, + pub emulators: Vec, /// Path to the `adb` binary used for device discovery. pub adb: Option, /// Non-fatal CLI, adb, or parse failure. @@ -289,17 +306,33 @@ fn find_adb(root: Option<&std::path::Path>) -> Option { } impl SdkSnapshot { - /// Refresh attached devices from the SDK's `adb devices -l`. + /// Refresh configured AVDs and attached devices from the official tooling. pub fn refresh_devices(&mut self) { self.adb = None; self.devices.clear(); + self.emulators.clear(); self.devices_error = None; + let mut errors = Vec::new(); + + if let Some(cli) = self.cli.clone() { + let running = RunningCli::default(); + match run( + &cli, + self.root.as_deref(), + &["emulator", "list", "--long"], + &running, + ) { + Ok(output) => self.emulators = parse_emulators(&output), + Err(err) => errors.push(err), + } + } let Some(adb) = find_adb(self.root.as_deref()) else { - self.devices_error = Some(format!( + errors.push(format!( "`{}` was not found. Install Android SDK Platform-Tools with `android sdk install platform-tools`.", adb_binary_name() )); + self.devices_error = Some(errors.join(" ")); return; }; @@ -307,7 +340,11 @@ impl SdkSnapshot { let running = RunningCli::default(); match run(&adb, None, &["devices", "-l"], &running) { Ok(output) => self.devices = parse_devices(&output), - Err(err) => self.devices_error = Some(err), + Err(err) => errors.push(err), + } + + if !errors.is_empty() { + self.devices_error = Some(errors.join(" ")); } } } @@ -369,6 +406,72 @@ fn parse_devices(output: &str) -> Vec { .collect() } +/// Parse the fixed-width table from `android emulator list --long`: +/// +/// ```text +/// AVD ID AVD Name API Level Status Serial +/// Medium_Phone_API_37.0 Medium Phone API 37.0 android-37.0 Offline +/// Pixel_8 Pixel 8 android-35 Online emulator-5554 +/// ``` +fn parse_emulators(output: &str) -> Vec { + let mut lines = output.lines(); + let Some(header) = lines.find(|line| line.contains("AVD ID") && line.contains("AVD Name")) + else { + return Vec::new(); + }; + + let Some(id_start) = header.find("AVD ID") else { + return Vec::new(); + }; + let Some(name_start) = header.find("AVD Name") else { + return Vec::new(); + }; + let Some(api_start) = header.find("API Level") else { + return Vec::new(); + }; + let Some(status_start) = header.find("Status") else { + return Vec::new(); + }; + let Some(serial_start) = header.find("Serial") else { + return Vec::new(); + }; + + if !(id_start < name_start && name_start < api_start && api_start < status_start) + || status_start >= serial_start + { + return Vec::new(); + } + + let field = |line: &str, start: usize, end: Option| { + let start = start.min(line.len()); + let end = end.unwrap_or(line.len()).min(line.len()); + line.get(start..end).unwrap_or_default().trim().to_string() + }; + + lines + .filter_map(|line| { + if line.trim().is_empty() { + return None; + } + let id = field(line, id_start, Some(name_start)); + if id.is_empty() { + return None; + } + let name = field(line, name_start, Some(api_start)); + let api_level = field(line, api_start, Some(status_start)); + let status = field(line, status_start, Some(serial_start)); + let serial = field(line, serial_start, None); + Some(Emulator { + id, + name, + api_level, + status, + serial: (!serial.is_empty()).then_some(serial), + }) + }) + .collect() +} + pub fn snapshot() -> SdkSnapshot { let Some(cli) = find_cli() else { return SdkSnapshot { @@ -748,6 +851,29 @@ List of devices attached assert!(parse_devices(output).is_empty()); } + #[test] + fn parses_emulator_list_long() { + let header = "AVD ID AVD Name API Level Status Serial"; + let offline = + "Medium_Phone_API_37.0 Medium Phone API 37.0 android-37.0 Offline"; + let online = format!( + "{:<25}{:<30}{:<15}{:<15}{}", + "Pixel_8", "Pixel 8", "android-35", "Online", "emulator-5554" + ); + let output = format!("{header}\n{offline}\n{online}\n"); + + let emulators = parse_emulators(&output); + assert_eq!(emulators.len(), 2); + assert_eq!(emulators[0].id, "Medium_Phone_API_37.0"); + assert_eq!(emulators[0].name, "Medium Phone API 37.0"); + assert_eq!(emulators[0].api_level, "android-37.0"); + assert_eq!(emulators[0].status, "Offline"); + assert_eq!(emulators[0].serial, None); + assert!(!emulators[0].is_online()); + assert_eq!(emulators[1].serial.as_deref(), Some("emulator-5554")); + assert!(emulators[1].is_online()); + } + #[test] fn parses_installed_and_available_platforms() { let output = "\ diff --git a/src/app.rs b/src/app.rs index 33a4065..0ce8f0e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,6 @@ //! Application shell and the SDK Versions screen. -use std::time::Duration; +use std::{collections::HashSet, time::Duration}; use gpui_kit::{ component::{ @@ -159,6 +159,50 @@ struct PackageRow { installed: bool, } +/// A configured AVD or an adb-attached device in the Devices table. +struct DeviceRow { + title: String, + kind: &'static str, + state: String, + details: String, + online: bool, + state_is_error: bool, +} + +fn device_state_label(state: &str) -> String { + match state { + "device" => "Online".to_string(), + "unauthorized" => "Unauthorized".to_string(), + "offline" => "Offline".to_string(), + state => state.to_string(), + } +} + +fn adb_device_details(device: &android::Device) -> String { + let mut details = Vec::new(); + if let Some(model) = &device.model { + details.push(model.clone()); + } else if let Some(product) = &device.product { + details.push(product.clone()); + } + if let Some(product) = &device.product { + if device.model.as_deref() != Some(product) { + details.push(format!("product {product}")); + } + } + if let Some(name) = &device.device { + details.push(format!("device {name}")); + } + if let Some(usb) = &device.usb { + details.push(format!("USB {usb}")); + } + if details.is_empty() { + "—".to_string() + } else { + details.join(" · ") + } +} + impl App { pub fn new(window: &mut Window, cx: &mut Context) -> Self { let search_input = cx.new(|cx| { @@ -460,16 +504,15 @@ impl App { return self.render_cli_missing(cx); } + let rows = self.device_rows(); + let total = rows.len(); + let online = rows.iter().filter(|row| row.online).count(); + let summary = format!( + "{online} online · {total} device{}", + if total == 1 { "" } else { "s" } + ); let border = cx.theme().border; let muted = cx.theme().muted_foreground; - let total = self.sdk.devices.len(); - let online = self - .sdk - .devices - .iter() - .filter(|device| device.is_online()) - .count(); - let summary = format!("{online} online · {total} attached"); let header = div() .flex() @@ -516,17 +559,17 @@ impl App { } content = content.child(header); - if self.sdk.devices.is_empty() { + if rows.is_empty() { let (title, detail) = if self.sdk.devices_error.is_some() { ( "Device listing unavailable".to_string(), - "Fix the adb issue above, then click Refresh.".to_string(), + "Fix the tooling issue above, then click Refresh.".to_string(), ) } else { ( - "No devices connected".to_string(), - "Start an emulator with `android emulator start `, or connect a \ - device with USB debugging enabled and accept the debugging prompt." + "No devices or emulators".to_string(), + "Create an emulator with `android emulator create`, or connect a device \ + with USB debugging enabled and accept the debugging prompt." .to_string(), ) }; @@ -551,8 +594,8 @@ impl App { ), ); } else { - for (ix, device) in self.sdk.devices.iter().enumerate() { - content = content.child(self.render_device_row(device, ix, cx)); + for (ix, row) in rows.iter().enumerate() { + content = content.child(self.render_device_row(row, ix, cx)); } } @@ -560,7 +603,8 @@ impl App { .child( div().px_6().py_4().child( Label::new( - "Devices and states come from `adb devices -l` using the SDK's platform-tools.", + "Configured AVDs come from `android emulator list --long`; attached \ + devices come from `adb devices -l`.", ) .text_size(px(11.)) .text_color(muted), @@ -569,55 +613,93 @@ impl App { .into_any_element() } + fn device_rows(&self) -> Vec { + let mut rows = Vec::new(); + let mut matched = HashSet::new(); + + for emulator in &self.sdk.emulators { + let running = emulator.serial.as_ref().and_then(|serial| { + self.sdk + .devices + .iter() + .find(|device| &device.serial == serial) + }); + if let Some(device) = running { + matched.insert(device.serial.clone()); + } + + let (state, state_is_error) = match running { + Some(device) => ( + device_state_label(&device.state), + device.state == "unauthorized" || device.state == "offline", + ), + None => (emulator.status.clone(), false), + }; + let online = running.map_or_else(|| emulator.is_online(), |device| device.is_online()); + + let mut details = Vec::new(); + if !emulator.name.is_empty() { + details.push(emulator.name.clone()); + } + if !emulator.api_level.is_empty() { + details.push(emulator.api_level.clone()); + } + if let Some(serial) = &emulator.serial { + details.push(format!("serial {serial}")); + } + if let Some(device) = running { + let adb = adb_device_details(device); + if adb != "—" { + details.push(adb); + } + } + + rows.push(DeviceRow { + title: emulator.id.clone(), + kind: "Emulator", + state, + details: details.join(" · "), + online, + state_is_error, + }); + } + + for device in &self.sdk.devices { + if matched.contains(&device.serial) { + continue; + } + rows.push(DeviceRow { + title: device.serial.clone(), + kind: if device.is_emulator { + "Emulator" + } else { + "Device" + }, + state: device_state_label(&device.state), + details: adb_device_details(device), + online: device.is_online(), + state_is_error: device.state == "unauthorized" || device.state == "offline", + }); + } + + rows + } + fn render_device_row( &self, - device: &android::Device, + row: &DeviceRow, ix: usize, cx: &mut Context, ) -> impl IntoElement { let border = cx.theme().border; let muted = cx.theme().muted_foreground; - let state = match device.state.as_str() { - "device" => "Online", - "unauthorized" => "Unauthorized", - "offline" => "Offline", - state => state, - }; - let state_color = if device.is_online() { + let state_color = if row.online { cx.theme().success - } else if device.state == "unauthorized" || device.state == "offline" { + } else if row.state_is_error { cx.theme().danger } else { muted }; - let kind = if device.is_emulator { - "Emulator" - } else { - "Device" - }; - - let mut details = Vec::new(); - if let Some(model) = &device.model { - details.push(model.clone()); - } else if let Some(product) = &device.product { - details.push(product.clone()); - } - if let Some(product) = &device.product { - if device.model.as_deref() != Some(product) { - details.push(format!("product {product}")); - } - } - if let Some(device_name) = &device.device { - details.push(format!("device {device_name}")); - } - if let Some(usb) = &device.usb { - details.push(format!("USB {usb}")); - } - let details = if details.is_empty() { - "—".to_string() - } else { - details.join(" · ") - }; div() .flex() @@ -629,21 +711,21 @@ impl App { .border_color(border) .child( div().flex_1().min_w(px(0.)).child( - Label::new(device.serial.clone()) + Label::new(row.title.clone()) .id(("device-serial", ix)) .text_size(px(12.)) .truncate(), ), ) .child( - Label::new(kind) + Label::new(row.kind) .id(("device-kind", ix)) .text_size(px(12.)) .text_color(muted) .w(px(90.)), ) .child( - Label::new(state) + Label::new(row.state.clone()) .id(("device-state", ix)) .text_size(px(12.)) .text_color(state_color) @@ -651,7 +733,7 @@ impl App { ) .child( div().flex_1().min_w(px(0.)).child( - Label::new(details) + Label::new(row.details.clone()) .id(("device-details", ix)) .text_size(px(12.)) .text_color(muted) From 18b602a9ff26441136c0391091fbf7175c5c7c66 Mon Sep 17 00:00:00 2001 From: Muhammad Rayhan Hamada Budiman Date: Thu, 19 Nov 2026 09:21:17 +0700 Subject: [PATCH 3/3] feat: start and stop emulators Add per-AVD Start and Stop controls, keep row status visible, and refresh the device list after the CLI finishes. --- src/android.rs | 18 ++++++++++ src/app.rs | 93 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 101 insertions(+), 10 deletions(-) diff --git a/src/android.rs b/src/android.rs index 629adfa..cf8932c 100644 --- a/src/android.rs +++ b/src/android.rs @@ -757,6 +757,24 @@ pub fn remove( run(cli, root, &["sdk", "remove", package], running) } +pub fn start_emulator( + cli: &std::path::Path, + root: Option<&std::path::Path>, + device: &str, + running: &RunningCli, +) -> Result { + run(cli, root, &["emulator", "start", device], running) +} + +pub fn stop_emulator( + cli: &std::path::Path, + root: Option<&std::path::Path>, + device: &str, + running: &RunningCli, +) -> Result { + run(cli, root, &["emulator", "stop", device], running) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/app.rs b/src/app.rs index 0ce8f0e..fe27f06 100644 --- a/src/app.rs +++ b/src/app.rs @@ -77,14 +77,18 @@ enum Mutation { Install(String), Update(String), Remove(String), + StartEmulator(String), + StopEmulator(String), } impl Mutation { - fn package(&self) -> &str { + fn target(&self) -> &str { match self { - Mutation::Install(package) | Mutation::Update(package) | Mutation::Remove(package) => { - package - } + Mutation::Install(target) + | Mutation::Update(target) + | Mutation::Remove(target) + | Mutation::StartEmulator(target) + | Mutation::StopEmulator(target) => target, } } @@ -93,6 +97,8 @@ impl Mutation { Mutation::Install(_) => "Installed", Mutation::Update(_) => "Updated", Mutation::Remove(_) => "Removed", + Mutation::StartEmulator(_) => "Started", + Mutation::StopEmulator(_) => "Stopped", } } @@ -101,6 +107,8 @@ impl Mutation { Mutation::Install(_) => "Installing", Mutation::Update(_) => "Updating", Mutation::Remove(_) => "Removing", + Mutation::StartEmulator(_) => "Starting", + Mutation::StopEmulator(_) => "Stopping", } } } @@ -167,6 +175,12 @@ struct DeviceRow { details: String, online: bool, state_is_error: bool, + action: Option, +} + +enum DeviceAction { + Start(String), + Stop(String), } fn device_state_label(state: &str) -> String { @@ -252,8 +266,8 @@ impl App { let root = self.sdk.root.clone(); let cli_process = self.running.clone(); - let package = mutation.package().to_string(); - let running = format!("{} {package}…", mutation.gerund()); + let target = mutation.target().to_string(); + let running = format!("{} {target}…", mutation.gerund()); let verb = mutation.verb(); self.busy = true; self.confirm_remove = None; @@ -271,6 +285,12 @@ impl App { Mutation::Remove(package) => { android::remove(&cli, root.as_deref(), &package, &cli_process) } + Mutation::StartEmulator(device) => { + android::start_emulator(&cli, root.as_deref(), &device, &cli_process) + } + Mutation::StopEmulator(device) => { + android::stop_emulator(&cli, root.as_deref(), &device, &cli_process) + } } }); @@ -280,7 +300,7 @@ impl App { this.busy = false; this.sdk = android::snapshot(); this.status = Some(match outcome { - Ok(_) => Status::success(format!("{verb} {package}.")), + Ok(_) => Status::success(format!("{verb} {target}.")), Err(err) => Status::error(err), }); cx.notify(); @@ -544,8 +564,9 @@ impl App { Label::new("DETAILS") .text_size(px(10.)) .text_color(muted) - .flex_1(), - ); + .w(px(300.)), + ) + .child(div().w(px(90.))); let mut content = div().flex().flex_col().w_full().child( div() @@ -557,6 +578,9 @@ impl App { if let Some(error) = &self.sdk.devices_error { content = content.child(Self::render_error(error, cx)); } + if let Some(status) = &self.status { + content = content.child(self.render_status(status, cx)); + } content = content.child(header); if rows.is_empty() { @@ -661,6 +685,11 @@ impl App { details: details.join(" · "), online, state_is_error, + action: Some(if online { + DeviceAction::Stop(emulator.id.clone()) + } else { + DeviceAction::Start(emulator.id.clone()) + }), }); } @@ -679,6 +708,7 @@ impl App { details: adb_device_details(device), online: device.is_online(), state_is_error: device.state == "unauthorized" || device.state == "offline", + action: None, }); } @@ -732,7 +762,7 @@ impl App { .w(px(110.)), ) .child( - div().flex_1().min_w(px(0.)).child( + div().w(px(300.)).min_w(px(0.)).child( Label::new(row.details.clone()) .id(("device-details", ix)) .text_size(px(12.)) @@ -740,6 +770,49 @@ impl App { .truncate(), ), ) + .child(self.render_device_action(row.action.as_ref(), ix, cx)) + } + + fn render_device_action( + &self, + action: Option<&DeviceAction>, + ix: usize, + cx: &mut Context, + ) -> impl IntoElement { + let cell = div().flex().items_center().w(px(90.)); + let Some(action) = action else { + return cell; + }; + let disabled = self.busy || self.sdk.cli.is_none(); + + match action { + DeviceAction::Start(device) => { + let device = device.clone(); + cell.child( + Button::new(("start-emulator", ix)) + .primary() + .small() + .label("Start") + .disabled(disabled) + .on_click(cx.listener(move |this, _, _, cx| { + this.start_mutation(Mutation::StartEmulator(device.clone()), cx); + })), + ) + } + DeviceAction::Stop(device) => { + let device = device.clone(); + cell.child( + Button::new(("stop-emulator", ix)) + .ghost() + .small() + .label("Stop") + .disabled(disabled) + .on_click(cx.listener(move |this, _, _, cx| { + this.start_mutation(Mutation::StopEmulator(device.clone()), cx); + })), + ) + } + } } fn search_query(&self, cx: &mut Context) -> (String, String) {