Implemented uninstall command (#86)

* implemented uninstall command
This commit is contained in:
Andreas Tzionis
2025-01-29 21:17:28 +02:00
committed by GitHub
parent dc909ceda6
commit 79d96d4c76
13 changed files with 84 additions and 1 deletions

View File

@@ -36,7 +36,7 @@ impl Drop for ADBTermios {
fn drop(&mut self) {
// Custom drop implementation, restores previous termios structure.
if let Err(e) = tcsetattr(self.fd, TCSANOW, &self.old_termios) {
log::error!("Error while droping ADBTermios: {e}")
log::error!("Error while dropping ADBTermios: {e}")
}
}
}

View File

@@ -133,6 +133,10 @@ fn main() -> Result<()> {
log::info!("Starting installation of APK {}...", path.display());
device.install(&path)?;
}
DeviceCommands::Uninstall { package } => {
log::info!("Uninstalling the package {}...", package);
device.uninstall(&package)?;
}
DeviceCommands::Framebuffer { path } => {
device.framebuffer(&path)?;
log::info!("Successfully dumped framebuffer at path {path}");

View File

@@ -33,6 +33,11 @@ pub enum DeviceCommands {
/// Path to APK file. Extension must be ".apk"
path: PathBuf,
},
/// Uninstall a package from the device
Uninstall {
/// Name of the package to uninstall
package: String,
},
/// Dump framebuffer of device
Framebuffer {
/// Framebuffer image destination path

View File

@@ -41,6 +41,9 @@ pub trait ADBDeviceExt {
/// Install an APK pointed to by `apk_path` on device.
fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()>;
/// Uninstall the package `package` from device.
fn uninstall(&mut self, package: &str) -> Result<()>;
/// Inner method requesting framebuffer from an Android device
fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>>;

View File

@@ -35,6 +35,10 @@ impl<T: ADBMessageTransport> ADBDeviceExt for ADBMessageDevice<T> {
self.install(apk_path)
}
fn uninstall(&mut self, package: &str) -> Result<()> {
self.uninstall(package)
}
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
self.framebuffer_inner()
}

View File

@@ -96,6 +96,11 @@ impl ADBDeviceExt for ADBTcpDevice {
self.inner.install(apk_path)
}
#[inline]
fn uninstall(&mut self, package: &str) -> Result<()> {
self.inner.uninstall(package)
}
#[inline]
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
self.inner.framebuffer_inner()

View File

@@ -273,6 +273,11 @@ impl ADBDeviceExt for ADBUSBDevice {
self.inner.install(apk_path)
}
#[inline]
fn uninstall(&mut self, package: &str) -> Result<()> {
self.inner.uninstall(package)
}
#[inline]
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
self.inner.framebuffer_inner()

View File

@@ -5,3 +5,4 @@ mod push;
mod reboot;
mod shell;
mod stat;
mod uninstall;

View File

@@ -0,0 +1,19 @@
use crate::{device::adb_message_device::ADBMessageDevice, ADBMessageTransport, Result};
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
pub(crate) fn uninstall(&mut self, package_name: &str) -> Result<()> {
self.open_session(format!("exec:cmd package 'uninstall' {}\0", package_name).as_bytes())?;
let final_status = self.get_transport_mut().read_message()?;
match final_status.into_payload().as_slice() {
b"Success\n" => {
log::info!("Package {} successfully uninstalled", package_name);
Ok(())
}
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
d.to_vec(),
)?)),
}
}
}

View File

@@ -20,6 +20,7 @@ pub(crate) enum AdbServerCommand {
MDNSServices,
ServerStatus,
ReconnectOffline,
Uninstall(String),
Install(u64),
// Local commands
ShellCommand(String),
@@ -83,6 +84,9 @@ impl Display for AdbServerCommand {
}
AdbServerCommand::Usb => write!(f, "usb:"),
AdbServerCommand::Install(size) => write!(f, "exec:cmd package 'install' -S {size}"),
AdbServerCommand::Uninstall(package) => {
write!(f, "exec:cmd package 'uninstall' {package}")
}
}
}
}

View File

@@ -115,6 +115,10 @@ impl ADBDeviceExt for ADBServerDevice {
self.install(apk_path)
}
fn uninstall(&mut self, package: &str) -> Result<()> {
self.uninstall(package)
}
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
self.framebuffer_inner()
}

View File

@@ -12,4 +12,5 @@ mod send;
mod stat;
mod tcpip;
mod transport;
mod uninstall;
mod usb;

View File

@@ -0,0 +1,28 @@
use std::io::Read;
use crate::{models::AdbServerCommand, server_device::ADBServerDevice, Result};
impl ADBServerDevice {
/// Uninstall a package from device
pub fn uninstall(&mut self, package_name: &str) -> Result<()> {
let serial: String = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.transport
.send_adb_request(AdbServerCommand::Uninstall(package_name.to_string()))?;
let mut data = [0; 1024];
let read_amount = self.transport.get_raw_connection()?.read(&mut data)?;
match &data[0..read_amount] {
b"Success\n" => {
log::info!("Package {} successfully uninstalled", package_name);
Ok(())
}
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
d.to_vec(),
)?)),
}
}
}