android_tools/adb/
adb_shell_am.rs1use crate::error::*;
2use std::{
3 path::{Path, PathBuf},
4 process::Command,
5};
6
7use super::ScreenCompatibilityMode;
8
9#[derive(Clone, Default)]
10pub struct AdbShellAm {
11 d: bool,
12 w: bool,
13 p: Option<PathBuf>,
14 r: bool,
15 s: bool,
16 n: bool,
17 user: Option<String>,
18 opengl_trace: bool,
19 start_profiler: Option<PathBuf>,
20 start: bool,
21 startservice: bool,
22 force_stop: Option<PathBuf>,
23 kill: Option<PathBuf>,
24 kill_all: bool,
25 broadcast: Option<String>,
26 instrument: bool,
27 no_window_animation: bool,
28 profile_start: Option<PathBuf>,
29 profile_stop: bool,
30 dumpheap: Option<PathBuf>,
31 set_debug_app: Option<PathBuf>,
32 persistent: bool,
33 clear_debug_app: bool,
34 monitor: bool,
35 gdb: bool,
36 screen_compat: Option<ScreenCompatibilityMode>,
37 display_size: Option<String>,
38 display_density: Option<String>,
39 to_uri: bool,
40 to_intent_uri: bool,
41}
42
43impl AdbShellAm {
44 pub fn new() -> Self {
45 Self {
46 ..Default::default()
47 }
48 }
49
50 pub fn d(&mut self, d: bool) -> &mut Self {
51 self.d = d;
52 self
53 }
54
55 pub fn w(&mut self, w: bool) -> &mut Self {
56 self.w = w;
57 self
58 }
59
60 pub fn p(&mut self, p: &Path) -> &mut Self {
61 self.p = Some(p.to_owned());
62 self
63 }
64
65 pub fn r(&mut self, r: bool) -> &mut Self {
66 self.r = r;
67 self
68 }
69
70 pub fn s(&mut self, s: bool) -> &mut Self {
71 self.s = s;
72 self
73 }
74
75 pub fn n(&mut self, n: bool) -> &mut Self {
76 self.n = n;
77 self
78 }
79
80 pub fn user(&mut self, user: String) -> &mut Self {
81 self.user = Some(user);
82 self
83 }
84
85 pub fn opengl_trace(&mut self, opengl_trace: bool) -> &mut Self {
86 self.opengl_trace = opengl_trace;
87 self
88 }
89
90 pub fn start_profiler(&mut self, start_profiler: &Path) -> &mut Self {
91 self.start_profiler = Some(start_profiler.to_owned());
92 self
93 }
94
95 pub fn start(&mut self, start: bool) -> &mut Self {
96 self.start = start;
97 self
98 }
99
100 pub fn startservice(&mut self, startservice: bool) -> &mut Self {
101 self.startservice = startservice;
102 self
103 }
104
105 pub fn force_stop(&mut self, force_stop: &Path) -> &mut Self {
106 self.force_stop = Some(force_stop.to_owned());
107 self
108 }
109
110 pub fn kill(&mut self, kill: &Path) -> &mut Self {
111 self.kill = Some(kill.to_owned());
112 self
113 }
114
115 pub fn kill_all(&mut self, kill_all: bool) -> &mut Self {
116 self.kill_all = kill_all;
117 self
118 }
119
120 pub fn broadcast(&mut self, broadcast: String) -> &mut Self {
121 self.broadcast = Some(broadcast);
122 self
123 }
124
125 pub fn instrument(&mut self, instrument: bool) -> &mut Self {
126 self.instrument = instrument;
127 self
128 }
129
130 pub fn no_window_animation(&mut self, no_window_animation: bool) -> &mut Self {
131 self.no_window_animation = no_window_animation;
132 self
133 }
134
135 pub fn profile_start(&mut self, profile_start: &Path) -> &mut Self {
136 self.profile_start = Some(profile_start.to_owned());
137 self
138 }
139
140 pub fn profile_stop(&mut self, profile_stop: bool) -> &mut Self {
141 self.profile_stop = profile_stop;
142 self
143 }
144
145 pub fn dumpheap(&mut self, dumpheap: &Path) -> &mut Self {
146 self.dumpheap = Some(dumpheap.to_owned());
147 self
148 }
149
150 pub fn set_debug_app(&mut self, set_debug_app: &Path) -> &mut Self {
151 self.set_debug_app = Some(set_debug_app.to_owned());
152 self
153 }
154
155 pub fn persistent(&mut self, persistent: bool) -> &mut Self {
156 self.persistent = persistent;
157 self
158 }
159
160 pub fn clear_debug_app(&mut self, clear_debug_app: bool) -> &mut Self {
161 self.clear_debug_app = clear_debug_app;
162 self
163 }
164
165 pub fn monitor(&mut self, monitor: bool) -> &mut Self {
166 self.monitor = monitor;
167 self
168 }
169
170 pub fn gdb(&mut self, gdb: bool) -> &mut Self {
171 self.gdb = gdb;
172 self
173 }
174
175 pub fn screen_compat(&mut self, screen_compat: ScreenCompatibilityMode) -> &mut Self {
176 self.screen_compat = Some(screen_compat);
177 self
178 }
179
180 pub fn display_size(&mut self, display_size: String) -> &mut Self {
181 self.display_size = Some(display_size);
182 self
183 }
184
185 pub fn display_density(&mut self, display_density: String) -> &mut Self {
186 self.display_density = Some(display_density);
187 self
188 }
189
190 pub fn to_uri(&mut self, to_uri: bool) -> &mut Self {
191 self.to_uri = to_uri;
192 self
193 }
194
195 pub fn to_intent_uri(&mut self, to_intent_uri: bool) -> &mut Self {
196 self.to_intent_uri = to_intent_uri;
197 self
198 }
199
200 pub fn run(&self) -> Result<()> {
201 let mut am = Command::new("adb");
202 am.arg("shell");
203 am.arg("am");
204 if self.d {
205 am.arg("-D");
206 }
207 if self.w {
208 am.arg("-W");
209 }
210 if let Some(p) = &self.p {
211 am.arg("-P").arg(p);
212 }
213 if self.r {
214 am.arg("-R");
215 }
216 if self.s {
217 am.arg("-S");
218 }
219 if self.n {
220 am.arg("-n");
221 }
222 if let Some(user) = &self.user {
223 am.arg("-user").arg(user);
224 }
225 if self.opengl_trace {
226 am.arg("--opengl-trace");
227 }
228 if let Some(start_profiler) = &self.start_profiler {
229 am.arg("--start-profiler").arg(start_profiler);
230 }
231 if self.start {
232 am.arg("start");
233 }
234 if self.startservice {
235 am.arg("startservice");
236 }
237 if let Some(force_stop) = &self.force_stop {
238 am.arg("force-stop").arg(force_stop);
239 }
240 if let Some(kill) = &self.kill {
241 am.arg("kill").arg(kill);
242 }
243 if self.kill_all {
244 am.arg("kill-all");
245 }
246 if let Some(broadcast) = &self.broadcast {
247 am.arg("broadcast").arg(broadcast);
248 }
249 if self.instrument {
250 am.arg("instrument");
251 }
252 if self.no_window_animation {
253 am.arg("--no-window-animation");
254 }
255 if let Some(profile_start) = &self.profile_start {
256 am.arg("profile start").arg(profile_start);
257 }
258 if self.profile_stop {
259 am.arg("profile stop");
260 }
261 if let Some(dumpheap) = &self.dumpheap {
262 am.arg("dumpheap").arg(dumpheap);
263 }
264 if let Some(set_debug_app) = &self.set_debug_app {
265 am.arg("set-debug-app").arg(set_debug_app);
266 }
267 if self.persistent {
268 am.arg("--persistent");
269 }
270 if self.clear_debug_app {
271 am.arg("clear-debug-app");
272 }
273 if self.monitor {
274 am.arg("monitor");
275 }
276 if self.gdb {
277 am.arg("gdb");
278 }
279 if self.gdb {
280 am.arg("--gdb");
281 }
282 if let Some(screen_compat) = &self.screen_compat {
283 am.arg("screen-compat").arg(screen_compat.to_string());
284 }
285 if let Some(display_size) = &self.display_size {
286 am.arg("display-size").arg(display_size);
287 }
288 if let Some(display_density) = &self.display_density {
289 am.arg("display-density").arg(display_density);
290 }
291 if self.to_uri {
292 am.arg("to-uri");
293 }
294 if self.to_intent_uri {
295 am.arg("to-intent-uri");
296 }
297 am.output_err(true)?;
298 Ok(())
299 }
300}