use crate::device::ADB;
use crate::error::{ADBResult};
use log::debug;
impl ADB {
pub fn take_screenshot(
&self,
device_id: &str,
output_path: &str,
) -> ADBResult<()> {
let device_path = "/sdcard/screenshot.png";
self.shell(device_id, &format!("screencap -p {}", device_path))?;
self.pull(device_id, device_path, output_path, None)?;
self.shell(device_id, &format!("rm {}", device_path))?;
Ok(())
}
pub fn record_screen(
&self,
device_id: &str,
output_path: &str,
duration_secs: u32,
size: Option<&str>,
) -> ADBResult<()> {
let device_path = "/sdcard/screen_record.mp4";
let mut command = format!("screenrecord --time-limit {} ", duration_secs.min(180));
if let Some(resolution) = size {
command.push_str(&format!("--size {} ", resolution));
}
command.push_str(device_path);
self.shell(device_id, &command)?;
self.pull(device_id, device_path, output_path, None)?;
self.shell(device_id, &format!("rm {}", device_path))?;
debug!("屏幕录制已保存到 {}", output_path);
Ok(())
}
pub fn capture_logs(
&self,
device_id: &str,
tag: Option<&str>,
priority: &str,
) -> ADBResult<String> {
let tag_filter = tag.map_or(String::new(), |t| format!(" {}", t));
self.shell(
device_id,
&format!("logcat -d{} *:{}", tag_filter, priority),
)
}
pub fn watch_logs(
&self,
device_id: &str,
tag: Option<&str>,
priority: &str,
) -> ADBResult<()> {
let tag_filter = tag.map_or(String::new(), |t| format!(" {}", t));
let command = format!("logcat{} *:{}", tag_filter, priority);
self.shell_no_wait(device_id, &command)
}
pub fn clear_logs(&self, device_id: &str) -> ADBResult<()> {
self.shell(device_id, "logcat -c")?;
Ok(())
}
}