1use crate::device::ADB;
2use crate::error::{ADBResult};
3use log::debug;
4
5impl ADB {
6 pub fn take_screenshot(
8 &self,
9 device_id: &str,
10 output_path: &str,
11 ) -> ADBResult<()> {
12 let device_path = "/sdcard/screenshot.png";
14 self.shell(device_id, &format!("screencap -p {}", device_path))?;
15
16 self.pull(device_id, device_path, output_path, None)?;
18
19 self.shell(device_id, &format!("rm {}", device_path))?;
21
22 Ok(())
23 }
24
25 pub fn record_screen(
33 &self,
34 device_id: &str,
35 output_path: &str,
36 duration_secs: u32,
37 size: Option<&str>,
38 ) -> ADBResult<()> {
39 let device_path = "/sdcard/screen_record.mp4";
41
42 let mut command = format!("screenrecord --time-limit {} ", duration_secs.min(180)); if let Some(resolution) = size {
47 command.push_str(&format!("--size {} ", resolution));
48 }
49
50 command.push_str(device_path);
52
53 self.shell(device_id, &command)?;
55
56 self.pull(device_id, device_path, output_path, None)?;
58
59 self.shell(device_id, &format!("rm {}", device_path))?;
61
62 debug!("屏幕录制已保存到 {}", output_path);
63 Ok(())
64 }
65
66 pub fn capture_logs(
68 &self,
69 device_id: &str,
70 tag: Option<&str>,
71 priority: &str,
72 ) -> ADBResult<String> {
73 let tag_filter = tag.map_or(String::new(), |t| format!(" {}", t));
74 self.shell(
75 device_id,
76 &format!("logcat -d{} *:{}", tag_filter, priority),
77 )
78 }
79
80 pub fn watch_logs(
82 &self,
83 device_id: &str,
84 tag: Option<&str>,
85 priority: &str,
86 ) -> ADBResult<()> {
87 let tag_filter = tag.map_or(String::new(), |t| format!(" {}", t));
88 let command = format!("logcat{} *:{}", tag_filter, priority);
89
90 self.shell_no_wait(device_id, &command)
92 }
93
94 pub fn clear_logs(&self, device_id: &str) -> ADBResult<()> {
96 self.shell(device_id, "logcat -c")?;
97 Ok(())
98 }
99}