android_tools/adb/
adb_shell.rs

1use crate::error::*;
2use std::{
3    path::{Path, PathBuf},
4    process::Command,
5};
6
7#[derive(Clone, Default)]
8pub struct Shell {
9    pwd: bool,
10    netstat: bool,
11    service_list: bool,
12    ps: bool,
13    wm_size: Option<String>,
14    ls: bool,
15    ls_s: bool,
16    ls_r: bool,
17    install: Option<PathBuf>,
18    install_r: Option<PathBuf>,
19    uninstall: Option<String>,
20    permissions_groups: bool,
21    list_permissions_g_r: bool,
22    dump: Option<String>,
23    path: Option<PathBuf>,
24}
25
26impl Shell {
27    pub fn new() -> Self {
28        Self {
29            ..Default::default()
30        }
31    }
32
33    /// Print current working directory
34    pub fn pwd(&mut self, pwd: bool) -> &mut Self {
35        self.pwd = pwd;
36        self
37    }
38
39    /// List TCP connectivity
40    pub fn netstat(&mut self, netstat: bool) -> &mut Self {
41        self.netstat = netstat;
42        self
43    }
44
45    /// List all services
46    pub fn service_list(&mut self, service_list: bool) -> &mut Self {
47        self.service_list = service_list;
48        self
49    }
50
51    /// Print process status
52    pub fn ps(&mut self, ps: bool) -> &mut Self {
53        self.ps = ps;
54        self
55    }
56
57    /// Displays the current screen resolution
58    pub fn wm_size(&mut self, wm_size: String) -> &mut Self {
59        self.wm_size = Some(wm_size);
60        self
61    }
62
63    /// List directory contents
64    pub fn ls(&mut self, ls: bool) -> &mut Self {
65        self.ls = ls;
66        self
67    }
68
69    /// Print size of each file
70    pub fn ls_s(&mut self, ls_s: bool) -> &mut Self {
71        self.ls_s = ls_s;
72        self
73    }
74
75    /// Install app or install app from phone path
76    pub fn install(&mut self, install: &Path) -> &mut Self {
77        self.install = Some(install.to_owned());
78        self
79    }
80
81    /// Install app from phone path
82    pub fn install_r(&mut self, install_r: &Path) -> &mut Self {
83        self.install_r = Some(install_r.to_owned());
84        self
85    }
86
87    /// Remove the app
88    pub fn uninstall(&mut self, uninstall: String) -> &mut Self {
89        self.uninstall = Some(uninstall);
90        self
91    }
92
93    /// List permission groups definitions
94    pub fn permissions_groups(&mut self, permissions_groups: bool) -> &mut Self {
95        self.permissions_groups = permissions_groups;
96        self
97    }
98
99    /// List permissions details
100    pub fn list_permissions_g_r(&mut self, list_permissions_g_r: bool) -> &mut Self {
101        self.list_permissions_g_r = list_permissions_g_r;
102        self
103    }
104
105    /// List info on one package
106    pub fn dump(&mut self, dump: String) -> &mut Self {
107        self.dump = Some(dump);
108        self
109    }
110
111    /// Path to the apk file
112    pub fn path(&mut self, path: &Path) -> &mut Self {
113        self.path = Some(path.to_owned());
114        self
115    }
116
117    pub fn run(&self) -> Result<()> {
118        let mut shell = Command::new("adb");
119        shell.arg("shell");
120        if self.pwd {
121            shell.arg("pwd");
122        }
123        if self.netstat {
124            shell.arg("netstat");
125        }
126        if self.service_list {
127            shell.arg("service list");
128        }
129        if self.ps {
130            shell.arg("ps");
131        }
132        if let Some(wm_size) = &self.wm_size {
133            shell.arg("wm size").arg(wm_size);
134        }
135        if self.ls {
136            shell.arg("ls");
137        }
138        if self.ls_s {
139            shell.arg("ls -s");
140        }
141        if self.ls_r {
142            shell.arg("ls -R");
143        }
144        if let Some(install) = &self.install {
145            shell.arg("install").arg(install);
146        }
147        if let Some(install_r) = &self.install_r {
148            shell.arg("install -r").arg(install_r);
149        }
150        if let Some(uninstall) = &self.uninstall {
151            shell.arg("uninstall").arg(uninstall);
152        }
153        if self.permissions_groups {
154            shell.arg("permissions groups");
155        }
156        if self.list_permissions_g_r {
157            shell.arg("list permissions -g -r");
158        }
159        if let Some(dump) = &self.dump {
160            shell.arg("dump").arg(dump);
161        }
162        if let Some(path) = &self.path {
163            shell.arg("path").arg(path);
164        }
165        shell.output_err(true)?;
166        Ok(())
167    }
168}