From cd5b302903e26e2bd2a8c87ce60879ca40d10387 Mon Sep 17 00:00:00 2001 From: Thulasi Ram Date: Sun, 18 Feb 2024 20:45:57 +0530 Subject: [PATCH 1/4] fix: refactor cmd and handle --- src-tauri/src/adb_cmd.rs | 120 ++++++++++++++++--------------- src-tauri/src/devices.rs | 9 ++- src-tauri/src/packages.rs | 145 ++++++++++++++++++++++---------------- src-tauri/src/users.rs | 2 +- 4 files changed, 156 insertions(+), 120 deletions(-) diff --git a/src-tauri/src/adb_cmd.rs b/src-tauri/src/adb_cmd.rs index 2d813ab..ac356e3 100644 --- a/src-tauri/src/adb_cmd.rs +++ b/src-tauri/src/adb_cmd.rs @@ -5,8 +5,21 @@ use std::os::windows::process::CommandExt; use log::info; -pub trait ADBCommand { +pub trait ADBCommand: Sized { fn execute(&self) -> Result; + + fn arg>(self, arg: S) -> Self; + fn args(self, args: I) -> Self + where + I: IntoIterator, + S: AsRef, + { + let mut s1 = self; + for arg in args { + s1 = s1.arg(arg); + } + s1 + } } #[derive(Debug, thiserror::Error)] @@ -15,65 +28,45 @@ pub enum ADBError { Unknown(String), } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct ADBRaw { - adb_path: String, - sub_commands: Vec, + cmd_str: String, + argsv: Vec, } impl ADBRaw { - pub fn new(adb_path: String, value: Vec) -> Self { - Self { - adb_path: adb_path, - sub_commands: value, - } - } -} - -#[derive(Debug, Clone)] -pub struct ADBShell { - adb_path: String, - sub_commands: Vec, - device_id: String, -} - -impl ADBShell { pub fn new(adb_path: String) -> Self { + let mut cmd_str = "adb"; + if !adb_path.is_empty() { + cmd_str = adb_path.as_str(); + } Self { - adb_path: adb_path, - sub_commands: vec![], - device_id: String::from(""), + cmd_str: cmd_str.to_string(), + argsv: vec![], } } - - pub fn for_device(mut self, device_id: String) -> Self { - self.device_id = device_id; - return self; - } - - pub fn with_commands(mut self, sub_commands: &[&str]) -> Self { - self.sub_commands = sub_commands.iter().map(|s| String::from(*s)).collect(); - return self; - } + } impl ADBCommand for ADBRaw { - fn execute(&self) -> Result { + fn arg>(self, arg: S) -> Self { + // https://users.rust-lang.org/t/best-way-to-clone-and-append-a-single-element/68675/2 - let mut cmd_str = "adb"; - if !self.adb_path.is_empty() { - cmd_str = self.adb_path.as_str(); - } + let mut s1 = self; + s1.argsv.push(arg.as_ref().to_owned()); + return s1; + } - let mut command = Command::new(cmd_str); + fn execute(&self) -> Result { + let mut command = Command::new(self.cmd_str.to_owned()); + command.args(self.argsv.to_vec()); - // https://stackoverflow.com/a/38186733/6323666 - let args = self - .sub_commands - .iter() - .map(|s| s.as_str()) - .collect::>(); - command.args(args); + // let args = self + // .sub_commands + // .iter() + // .map(|s| s.as_str()) + // .collect::>(); + // command.args(args); info!("command {:?}", command); @@ -107,17 +100,32 @@ impl ADBCommand for ADBRaw { } } -impl ADBCommand for ADBShell { - fn execute(&self) -> Result { - let mut sub_commands_with_shell: Vec = vec![String::from("shell")]; +#[derive(Debug, Clone)] +pub struct ADBShell { + adb_raw: ADBRaw, +} - if !String::is_empty(&self.device_id.to_owned()) { - sub_commands_with_shell.insert(0, String::from("-s")); - sub_commands_with_shell.insert(1, self.device_id.to_owned()); - } +impl ADBShell { + pub fn new(adb_path: String) -> Self { + let adbr = ADBRaw::new(adb_path).arg("shell"); + Self { adb_raw: adbr } + } + + pub fn for_device(self, device_id: String) -> Self { + let mut s1 = self; + s1.adb_raw = s1.adb_raw.args(vec!["-s", &device_id]); + return s1; + } +} - sub_commands_with_shell.extend(self.sub_commands.to_owned()); - let adb_raw = ADBRaw::new(self.adb_path.to_owned(), sub_commands_with_shell); - return adb_raw.execute(); +impl ADBCommand for ADBShell { + fn arg>(self, arg: S) -> Self { + let mut s1 = self; + s1.adb_raw = s1.adb_raw.arg(arg.as_ref()); + return s1; + } + + fn execute(&self) -> Result { + return self.adb_raw.execute(); } } diff --git a/src-tauri/src/devices.rs b/src-tauri/src/devices.rs index 8949e13..65ab939 100644 --- a/src-tauri/src/devices.rs +++ b/src-tauri/src/devices.rs @@ -61,7 +61,9 @@ pub struct ADBTerminalImpl { impl ADBTerminalImpl { pub fn list_devices(&self) -> Result> { - let res = ADBRaw::new(self.adb_path.to_owned(), vec![String::from("devices")]).execute(); + let res = ADBRaw::new(self.adb_path.to_owned()) + .arg("devices") + .execute(); match res { Err(e) => { return Err(e.into()); @@ -101,8 +103,9 @@ impl ADBTerminalImpl { return Err(e); } Ok(d) => { - let shell_cmd: ADBShell = ADBShell::new(self.adb_path.to_owned()).for_device(d.id.to_owned()); - let res = shell_cmd.with_commands(&["getprop"]).execute(); + let shell_cmd: ADBShell = + ADBShell::new(self.adb_path.to_owned()).for_device(d.id.to_owned()); + let res = shell_cmd.arg("getprop").execute(); match res { Err(e) => { return Err(e.into()); diff --git a/src-tauri/src/packages.rs b/src-tauri/src/packages.rs index 24361be..c17beac 100644 --- a/src-tauri/src/packages.rs +++ b/src-tauri/src/packages.rs @@ -1,10 +1,10 @@ use crate::adb_cmd::{ADBCommand, ADBShell}; use anyhow::{anyhow, Error, Result}; use lazy_static::lazy_static; +use regex::Regex; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, HashSet}; use std::{fmt::Display, str::FromStr}; -use regex::Regex; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] pub enum PackageState { @@ -110,14 +110,17 @@ const LIST_ENABLED_PACKAGES: &str = "pm list packages -e"; const LIST_DISABLED_PACKAGES: &str = "pm list packages -d"; const LIST_UNINSTALLED_DISABLED_PACKAGES: &str = "pm list packages -u -d"; -pub struct ADBTerminalImpl { - adb_path: String, +pub struct ADBTerminalImpl +where + F: ADBCommand + ?Sized, +{ + adb_command: F, } -impl ADBTerminalImpl { +impl ADBTerminalImpl { pub fn new(adb_path: String) -> Self { Self { - adb_path: String::from(adb_path), + adb_command: ADBShell::new(adb_path), } } } @@ -137,9 +140,9 @@ lazy_static! { static ref PACKAGE_PARSE_REGEX: Regex = Regex::new(r"(.*)\.apk\=(.*)").unwrap(); } -impl ADBTerminalImpl { +impl ADBTerminalImpl { pub fn list_packages(&self, device_id: String, user_id: String) -> Result> { - let shell_cmd = ADBShell::new(self.adb_path.to_owned()).for_device(device_id.to_owned()); + let shell_cmd = self.adb_command.clone().for_device(device_id.to_owned()); let ( mut all_pkg, @@ -165,7 +168,6 @@ impl ADBTerminalImpl { ); let ( - cmd_all_pkg, cmd_enabled_pkg, cmd_disabled_pkg, cmd_system_pkg, @@ -174,45 +176,21 @@ impl ADBTerminalImpl { ) = ( shell_cmd .clone() - .with_commands(&[LIST_ALL_PACKAGES_INCLUDING_UNINSTALLED]), + .args(&[LIST_ENABLED_PACKAGES, "--user", &user_id]), shell_cmd .clone() - .with_commands(&[LIST_ENABLED_PACKAGES, "--user", &user_id]), + .args(&[LIST_DISABLED_PACKAGES, "--user", &user_id]), shell_cmd .clone() - .with_commands(&[LIST_DISABLED_PACKAGES, "--user", &user_id]), + .args(&[LIST_SYSTEM_PACKAGES, "--user", &user_id]), shell_cmd .clone() - .with_commands(&[LIST_SYSTEM_PACKAGES, "--user", &user_id]), + .args(&[LIST_THIRD_PARTY_PACKAGES, "--user", &user_id]), shell_cmd .clone() - .with_commands(&[LIST_THIRD_PARTY_PACKAGES, "--user", &user_id]), - shell_cmd.clone().with_commands(&[ - LIST_UNINSTALLED_DISABLED_PACKAGES, - "--user", - &user_id, - ]), + .args(&[LIST_UNINSTALLED_DISABLED_PACKAGES, "--user", &user_id]), ); - fn callback_all_pkg( - container: &mut HashMap, - ) -> impl FnMut(String) -> Result<()> + '_ { - let parser = |s: String| -> Result<()> { - let ot = s.replace("package:", ""); - for l in ot.lines() { - let caps = PACKAGE_PARSE_REGEX.captures(l).unwrap(); - container.insert( - caps.get(2).unwrap().as_str().to_string(), - PackageAttribs { - package_path: caps.get(1).unwrap().as_str().to_string(), - }, - ); - } - return Ok(()); - }; - return parser; - } - fn callback(container: &mut HashSet) -> impl FnMut(String) -> Result<()> + '_ { let parser = |s: String| -> Result<()> { let ot = s.replace("package:", ""); @@ -224,7 +202,7 @@ impl ADBTerminalImpl { return parser; } - let res = Self::_execute_and_parse(cmd_all_pkg, callback_all_pkg(&mut all_pkg)) + let res = Self::execute_list_all_with_fallback(shell_cmd, user_id.to_owned(), &mut all_pkg) .and_then(|_| Self::_execute_and_parse(cmd_enabled_pkg, callback(&mut enabled_pkg))) .and_then(|_| Self::_execute_and_parse(cmd_disabled_pkg, callback(&mut disabled_pkg))) .and_then(|_| Self::_execute_and_parse(cmd_system_pkg, callback(&mut sys_pkg))) @@ -283,25 +261,18 @@ impl ADBTerminalImpl { pkg: String, clear_pkg: bool, ) -> Result<()> { - let shell_cmd: ADBShell = - ADBShell::new(self.adb_path.to_owned()).for_device(device_id.to_owned()); + let shell_cmd: ADBShell = self.adb_command.clone().for_device(device_id); let (cmd_disable_pkg, cmd_fstop_pkg, cmd_clear_pkg) = ( - shell_cmd.clone().with_commands(&[ - "pm disable-user", - "--user", - &user_id, - &pkg.to_owned(), - ]), - shell_cmd.clone().with_commands(&[ - "am force-stop", - "--user", - &user_id, - &pkg.to_owned(), - ]), shell_cmd .clone() - .with_commands(&["pm clear", "--user", &user_id, &pkg.to_owned()]), + .args(["pm disable-user", "--user", &user_id, &pkg.to_owned()]), + shell_cmd + .clone() + .args(["am force-stop", "--user", &user_id, &pkg.to_owned()]), + shell_cmd + .clone() + .args(&["pm clear", "--user", &user_id, &pkg.to_owned()]), ); Self::_execute_and_parse(cmd_disable_pkg, |s| { @@ -335,11 +306,9 @@ impl ADBTerminalImpl { } pub fn enable_package(&self, device_id: String, user_id: String, pkg: String) -> Result<()> { - let shell_cmd: ADBShell = - ADBShell::new(self.adb_path.to_owned()).for_device(device_id.to_owned()); + let shell_cmd: ADBShell = self.adb_command.clone().for_device(device_id); - let cmd_enable_pkg = - shell_cmd.with_commands(&["pm enable", "--user", &user_id, &pkg.to_owned()]); + let cmd_enable_pkg = shell_cmd.args(["pm enable", "--user", &user_id, &pkg.to_owned()]); Self::_execute_and_parse(cmd_enable_pkg, |s| { if s.contains(&format!("Package {} new state: enabled", pkg.to_owned())) { @@ -352,10 +321,9 @@ impl ADBTerminalImpl { } pub fn install_package(&self, device_id: String, user_id: String, pkg: String) -> Result<()> { - let shell_cmd: ADBShell = - ADBShell::new(self.adb_path.to_owned()).for_device(device_id.to_owned()); + let shell_cmd: ADBShell = self.adb_command.clone().for_device(device_id); - let cmd_enable_pkg = shell_cmd.with_commands(&[ + let cmd_enable_pkg = shell_cmd.args([ "cmd package install-existing", "--user", &user_id, @@ -372,6 +340,63 @@ impl ADBTerminalImpl { return Ok(()); } + fn execute_list_all_with_fallback( + shell_cmd: ADBShell, + user_id: String, + mut container: &mut HashMap, + ) -> Result<()> { + let (cmd_all_pkg, cmd_all_package_user0_fallback, cmd_all_package_user_fallback) = ( + shell_cmd + .clone() + .args([LIST_ALL_PACKAGES_INCLUDING_UNINSTALLED]), + shell_cmd + .clone() + .args([LIST_ALL_PACKAGES_INCLUDING_UNINSTALLED, "--user", "0"]), + shell_cmd + .clone() + .args([LIST_ALL_PACKAGES_INCLUDING_UNINSTALLED, "--user", &user_id]), + ); + + fn callback( + container: &mut HashMap, + ) -> impl FnMut(String) -> Result<()> + '_ { + let parser = |s: String| -> Result<()> { + let ot = s.replace("package:", ""); + for l in ot.lines() { + let caps = PACKAGE_PARSE_REGEX.captures(l).unwrap(); + container.insert( + caps.get(2).unwrap().as_str().to_string(), + PackageAttribs { + package_path: caps.get(1).unwrap().as_str().to_string(), + }, + ); + } + return Ok(()); + }; + return parser; + } + + return Self::_execute_and_parse(cmd_all_pkg, callback(&mut container)) + .or_else(|e| { + if !e + .to_string() + .contains("Shell does not have permission to access user") + { + return Err(e); + } + Self::_execute_and_parse(cmd_all_package_user0_fallback, callback(&mut container)) + }) + .or_else(|e| { + if !e + .to_string() + .contains("Shell does not have permission to access user") + { + return Err(e); + } + Self::_execute_and_parse(cmd_all_package_user_fallback, callback(&mut container)) + }); + } + fn _execute_and_parse( cmd: ADBShell, mut parser: impl FnMut(String) -> Result<()>, diff --git a/src-tauri/src/users.rs b/src-tauri/src/users.rs index d5f56f7..c525459 100644 --- a/src-tauri/src/users.rs +++ b/src-tauri/src/users.rs @@ -34,7 +34,7 @@ impl ADBTerminalImpl { let shell_cmd: ADBShell = ADBShell::new(self.adb_path.to_owned()).for_device(device_id.to_owned()); - let res = shell_cmd.with_commands(&["pm list users "]).execute(); + let res = shell_cmd.args(&["pm list users "]).execute(); match res { Err(e) => { return Err(e.into()); From dab322b2fec8a8ba3bab5d66e4ff5900cb31ac48 Mon Sep 17 00:00:00 2001 From: Thulasi Ram Date: Sun, 18 Feb 2024 20:46:10 +0530 Subject: [PATCH 2/4] fix: remove full screen --- src-tauri/tauri.conf.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 752fa89..cc00332 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -106,7 +106,7 @@ }, "windows": [ { - "fullscreen": true, + "fullscreen": false, "height": 600, "resizable": true, "title": "Simple Android Debloater", From e93667f9bf6d11086a9b564c31c43eeb096e7f6d Mon Sep 17 00:00:00 2001 From: Thulasi Ram Date: Mon, 19 Feb 2024 00:45:23 +0530 Subject: [PATCH 3/4] fix: move devices to a helper util rather than interface --- src-tauri/src/adb_cmd.rs | 41 +++++++++++++++++----- src-tauri/src/devices.rs | 4 +-- src-tauri/src/packages.rs | 73 ++++++++++++++++++++------------------- src-tauri/src/users.rs | 8 +++-- 4 files changed, 77 insertions(+), 49 deletions(-) diff --git a/src-tauri/src/adb_cmd.rs b/src-tauri/src/adb_cmd.rs index ac356e3..e8787b0 100644 --- a/src-tauri/src/adb_cmd.rs +++ b/src-tauri/src/adb_cmd.rs @@ -1,4 +1,4 @@ -use std::process::Command; +use std::{process::Command, rc::Rc}; #[cfg(target_os = "windows")] use std::os::windows::process::CommandExt; @@ -7,7 +7,6 @@ use log::info; pub trait ADBCommand: Sized { fn execute(&self) -> Result; - fn arg>(self, arg: S) -> Self; fn args(self, args: I) -> Self where @@ -20,6 +19,20 @@ pub trait ADBCommand: Sized { } s1 } + + fn arg_prepend>(self, arg: S) -> Self; + fn args_prepend(self, args: I) -> Self + where + I: IntoIterator, + S: AsRef, + { + let mut s1 = self; + for arg in args { + s1 = s1.arg_prepend(arg); + } + s1 + } + } #[derive(Debug, thiserror::Error)] @@ -57,6 +70,12 @@ impl ADBCommand for ADBRaw { return s1; } + fn arg_prepend>(self, arg: S) -> Self { + let mut s1 = self; + s1.argsv.insert(0, arg.as_ref().to_owned()); + return s1; + } + fn execute(&self) -> Result { let mut command = Command::new(self.cmd_str.to_owned()); command.args(self.argsv.to_vec()); @@ -110,12 +129,6 @@ impl ADBShell { let adbr = ADBRaw::new(adb_path).arg("shell"); Self { adb_raw: adbr } } - - pub fn for_device(self, device_id: String) -> Self { - let mut s1 = self; - s1.adb_raw = s1.adb_raw.args(vec!["-s", &device_id]); - return s1; - } } impl ADBCommand for ADBShell { @@ -125,7 +138,19 @@ impl ADBCommand for ADBShell { return s1; } + fn arg_prepend>(self, arg: S) -> Self { + let mut s1 = self; + s1.adb_raw = s1.adb_raw.arg_prepend(arg.as_ref()); + return s1; + } + fn execute(&self) -> Result { return self.adb_raw.execute(); } } + + +pub fn for_device<'a, T: ADBCommand + Clone>(abdc: &'a T, device_id: String) -> T { + // ideally its -s but we send in reverse so prepend works properly + return abdc.clone().args_prepend(vec!["-s", &device_id].into_iter().rev()) +} \ No newline at end of file diff --git a/src-tauri/src/devices.rs b/src-tauri/src/devices.rs index 65ab939..99726cf 100644 --- a/src-tauri/src/devices.rs +++ b/src-tauri/src/devices.rs @@ -1,4 +1,4 @@ -use crate::adb_cmd::{ADBCommand, ADBRaw, ADBShell}; +use crate::adb_cmd::{self, ADBCommand, ADBRaw, ADBShell}; use anyhow::{anyhow, Error, Result}; use core::result::Result::Ok; use serde::{Deserialize, Serialize}; @@ -104,7 +104,7 @@ impl ADBTerminalImpl { } Ok(d) => { let shell_cmd: ADBShell = - ADBShell::new(self.adb_path.to_owned()).for_device(d.id.to_owned()); + adb_cmd::for_device(&ADBShell::new(self.adb_path.to_owned()), d.id.to_owned()); let res = shell_cmd.arg("getprop").execute(); match res { Err(e) => { diff --git a/src-tauri/src/packages.rs b/src-tauri/src/packages.rs index c17beac..7dc19a2 100644 --- a/src-tauri/src/packages.rs +++ b/src-tauri/src/packages.rs @@ -1,4 +1,4 @@ -use crate::adb_cmd::{ADBCommand, ADBShell}; +use crate::adb_cmd::{self, ADBCommand, ADBShell}; use anyhow::{anyhow, Error, Result}; use lazy_static::lazy_static; use regex::Regex; @@ -112,7 +112,7 @@ const LIST_UNINSTALLED_DISABLED_PACKAGES: &str = "pm list packages -u -d"; pub struct ADBTerminalImpl where - F: ADBCommand + ?Sized, + F: ADBCommand + ?Sized, // TODO: Factory with closures? { adb_command: F, } @@ -142,7 +142,7 @@ lazy_static! { impl ADBTerminalImpl { pub fn list_packages(&self, device_id: String, user_id: String) -> Result> { - let shell_cmd = self.adb_command.clone().for_device(device_id.to_owned()); + let shell_cmd = adb_cmd::for_device(&self.adb_command.clone(), device_id.to_owned()); let ( mut all_pkg, @@ -202,17 +202,22 @@ impl ADBTerminalImpl { return parser; } - let res = Self::execute_list_all_with_fallback(shell_cmd, user_id.to_owned(), &mut all_pkg) - .and_then(|_| Self::_execute_and_parse(cmd_enabled_pkg, callback(&mut enabled_pkg))) - .and_then(|_| Self::_execute_and_parse(cmd_disabled_pkg, callback(&mut disabled_pkg))) - .and_then(|_| Self::_execute_and_parse(cmd_system_pkg, callback(&mut sys_pkg))) - .and_then(|_| Self::_execute_and_parse(cmd_tpp_pkg, callback(&mut tpp_pkg))) - .and_then(|_| { - Self::_execute_and_parse( - cmd_uninstalled_disabled_pkg, - callback(&mut uninstalled_disabled_pkg), - ) - }); + let res = + Self::execute_list_all_with_fallback(&shell_cmd, user_id.to_owned(), &mut all_pkg) + .and_then(|_| { + Self::_execute_and_parse(&cmd_enabled_pkg, callback(&mut enabled_pkg)) + }) + .and_then(|_| { + Self::_execute_and_parse(&cmd_disabled_pkg, callback(&mut disabled_pkg)) + }) + .and_then(|_| Self::_execute_and_parse(&cmd_system_pkg, callback(&mut sys_pkg))) + .and_then(|_| Self::_execute_and_parse(&cmd_tpp_pkg, callback(&mut tpp_pkg))) + .and_then(|_| { + Self::_execute_and_parse( + &cmd_uninstalled_disabled_pkg, + callback(&mut uninstalled_disabled_pkg), + ) + }); match res { Err(e) => { @@ -261,7 +266,7 @@ impl ADBTerminalImpl { pkg: String, clear_pkg: bool, ) -> Result<()> { - let shell_cmd: ADBShell = self.adb_command.clone().for_device(device_id); + let shell_cmd = adb_cmd::for_device(&self.adb_command.clone(), device_id.to_owned()); let (cmd_disable_pkg, cmd_fstop_pkg, cmd_clear_pkg) = ( shell_cmd @@ -275,7 +280,7 @@ impl ADBTerminalImpl { .args(&["pm clear", "--user", &user_id, &pkg.to_owned()]), ); - Self::_execute_and_parse(cmd_disable_pkg, |s| { + Self::_execute_and_parse(&cmd_disable_pkg, |s| { if s.contains(&format!( "Package {} new state: disabled-user", pkg.to_owned() @@ -285,7 +290,7 @@ impl ADBTerminalImpl { return Err(anyhow!(s)); }) .and_then(|_| { - Self::_execute_and_parse(cmd_fstop_pkg, |s| { + Self::_execute_and_parse(&cmd_fstop_pkg, |s| { if s.is_empty() { return Ok(()); } @@ -294,7 +299,7 @@ impl ADBTerminalImpl { })?; if clear_pkg { - Self::_execute_and_parse(cmd_clear_pkg, |s| { + Self::_execute_and_parse(&cmd_clear_pkg, |s| { if s.eq("Success") { return Ok(()); } @@ -306,11 +311,11 @@ impl ADBTerminalImpl { } pub fn enable_package(&self, device_id: String, user_id: String, pkg: String) -> Result<()> { - let shell_cmd: ADBShell = self.adb_command.clone().for_device(device_id); + let shell_cmd = adb_cmd::for_device(&self.adb_command.clone(), device_id.to_owned()); let cmd_enable_pkg = shell_cmd.args(["pm enable", "--user", &user_id, &pkg.to_owned()]); - Self::_execute_and_parse(cmd_enable_pkg, |s| { + Self::_execute_and_parse(&cmd_enable_pkg, |s| { if s.contains(&format!("Package {} new state: enabled", pkg.to_owned())) { return Ok(()); } @@ -321,7 +326,7 @@ impl ADBTerminalImpl { } pub fn install_package(&self, device_id: String, user_id: String, pkg: String) -> Result<()> { - let shell_cmd: ADBShell = self.adb_command.clone().for_device(device_id); + let shell_cmd = adb_cmd::for_device(&self.adb_command.clone(), device_id.to_owned()); let cmd_enable_pkg = shell_cmd.args([ "cmd package install-existing", @@ -330,7 +335,7 @@ impl ADBTerminalImpl { &pkg.to_owned(), ]); - Self::_execute_and_parse(cmd_enable_pkg, |s| { + Self::_execute_and_parse(&cmd_enable_pkg, |s| { if s.contains(&format!("Package {} new state: enabled", pkg.to_owned())) { return Ok(()); } @@ -340,20 +345,16 @@ impl ADBTerminalImpl { return Ok(()); } - fn execute_list_all_with_fallback( - shell_cmd: ADBShell, + fn execute_list_all_with_fallback<'a, T: ADBCommand + Clone>( + cmd: &'a T, user_id: String, mut container: &mut HashMap, ) -> Result<()> { let (cmd_all_pkg, cmd_all_package_user0_fallback, cmd_all_package_user_fallback) = ( - shell_cmd - .clone() - .args([LIST_ALL_PACKAGES_INCLUDING_UNINSTALLED]), - shell_cmd - .clone() + cmd.clone().args([LIST_ALL_PACKAGES_INCLUDING_UNINSTALLED]), + cmd.clone() .args([LIST_ALL_PACKAGES_INCLUDING_UNINSTALLED, "--user", "0"]), - shell_cmd - .clone() + cmd.clone() .args([LIST_ALL_PACKAGES_INCLUDING_UNINSTALLED, "--user", &user_id]), ); @@ -376,7 +377,7 @@ impl ADBTerminalImpl { return parser; } - return Self::_execute_and_parse(cmd_all_pkg, callback(&mut container)) + return Self::_execute_and_parse(&cmd_all_pkg, callback(&mut container)) .or_else(|e| { if !e .to_string() @@ -384,7 +385,7 @@ impl ADBTerminalImpl { { return Err(e); } - Self::_execute_and_parse(cmd_all_package_user0_fallback, callback(&mut container)) + Self::_execute_and_parse(&cmd_all_package_user0_fallback, callback(&mut container)) }) .or_else(|e| { if !e @@ -393,12 +394,12 @@ impl ADBTerminalImpl { { return Err(e); } - Self::_execute_and_parse(cmd_all_package_user_fallback, callback(&mut container)) + Self::_execute_and_parse(&cmd_all_package_user_fallback, callback(&mut container)) }); } - fn _execute_and_parse( - cmd: ADBShell, + fn _execute_and_parse<'a, T: ADBCommand>( + cmd: &'a T, mut parser: impl FnMut(String) -> Result<()>, ) -> Result<()> { let res = cmd.execute(); diff --git a/src-tauri/src/users.rs b/src-tauri/src/users.rs index c525459..46f8e84 100644 --- a/src-tauri/src/users.rs +++ b/src-tauri/src/users.rs @@ -1,4 +1,4 @@ -use crate::adb_cmd::{ADBCommand, ADBShell}; +use crate::adb_cmd::{self, ADBCommand, ADBShell}; use anyhow::{anyhow, Result}; use lazy_static::lazy_static; use regex::Regex; @@ -31,8 +31,10 @@ lazy_static! { impl ADBTerminalImpl { pub fn list_users(&self, device_id: String) -> Result> { - let shell_cmd: ADBShell = - ADBShell::new(self.adb_path.to_owned()).for_device(device_id.to_owned()); + let shell_cmd: ADBShell = adb_cmd::for_device( + &ADBShell::new(self.adb_path.to_owned()), + device_id.to_owned(), + ); let res = shell_cmd.args(&["pm list users "]).execute(); match res { From ff64d5022606b3140c276f1903c54324824002d1 Mon Sep 17 00:00:00 2001 From: Thulasi Ram Date: Mon, 19 Feb 2024 00:51:14 +0530 Subject: [PATCH 4/4] fix: optimize imports --- src-tauri/src/adb_cmd.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src-tauri/src/adb_cmd.rs b/src-tauri/src/adb_cmd.rs index e8787b0..99e3c22 100644 --- a/src-tauri/src/adb_cmd.rs +++ b/src-tauri/src/adb_cmd.rs @@ -1,4 +1,4 @@ -use std::{process::Command, rc::Rc}; +use std::process::Command; #[cfg(target_os = "windows")] use std::os::windows::process::CommandExt; @@ -32,7 +32,6 @@ pub trait ADBCommand: Sized { } s1 } - } #[derive(Debug, thiserror::Error)] @@ -58,7 +57,6 @@ impl ADBRaw { argsv: vec![], } } - } impl ADBCommand for ADBRaw { @@ -149,8 +147,9 @@ impl ADBCommand for ADBShell { } } - pub fn for_device<'a, T: ADBCommand + Clone>(abdc: &'a T, device_id: String) -> T { // ideally its -s but we send in reverse so prepend works properly - return abdc.clone().args_prepend(vec!["-s", &device_id].into_iter().rev()) -} \ No newline at end of file + return abdc + .clone() + .args_prepend(vec!["-s", &device_id].into_iter().rev()); +}