Skip to content

Commit

Permalink
perf: Optimize *system_packages and improve `list_all_system_packag…
Browse files Browse the repository at this point in the history
…es` API
  • Loading branch information
Rudxain committed Dec 8, 2024
1 parent dc1ce19 commit ac056dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
29 changes: 19 additions & 10 deletions src/core/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,23 @@ pub fn user_flag(user_id: Option<&User>) -> String {
.unwrap_or_default()
}

pub const PACK_URI_SCHEME: &str = "package:";
#[expect(clippy::cast_possible_truncation, reason = "")]
pub const PACK_URI_LEN: u8 = PACK_URI_SCHEME.len() as u8;

/// Installed and uninstalled packages.
///
/// If `device_serial` is empty, it lets ADB choose the default device.
pub fn list_all_system_packages(device_serial: &str, user_id: Option<&User>) -> String {
pub fn list_all_system_packages(device_serial: &str, user_id: Option<&User>) -> Vec<String> {
let action = format!("{PM_LIST_PACKS} -s -u{}", user_flag(user_id));

adb_cmd(true, device_serial, &action)
.unwrap_or_default()
.replace("package:", "")
match adb_cmd(true, device_serial, &action) {
Ok(s) => s
.lines()
.map(|ln| String::from(&ln[PACK_URI_LEN as usize..]))
.collect(),
_ => vec![],
}
}

/// If `device_serial` is empty, it lets ADB choose the default device.
Expand All @@ -190,12 +198,13 @@ pub fn hashset_system_packages(
_ => return HashSet::default(), // You probably don't need to use this function for anything else
};

adb_cmd(true, device_serial, &action)
.unwrap_or_default()
.replace("package:", "")
.lines()
.map(String::from)
.collect()
match adb_cmd(true, device_serial, &action) {
Ok(s) => s
.lines()
.map(|ln| String::from(&ln[PACK_URI_LEN as usize..]))
.collect(),
_ => HashSet::default(),
}
}

// Minimum information for processing adb commands
Expand Down
23 changes: 12 additions & 11 deletions src/core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::gui::widgets::package_row::PackageRow;
use chrono::offset::Utc;
use chrono::{DateTime, Local};
use csv::Writer;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{fmt, fs};

Expand All @@ -24,18 +24,20 @@ pub fn fetch_packages(
device_serial: &str,
user_id: Option<&User>,
) -> Vec<PackageRow> {
let all_system_packages = list_all_system_packages(device_serial, user_id);
let enabled_system_packages =
hashset_system_packages(PackageState::Enabled, device_serial, user_id);
let disabled_system_packages =
let all_sys_packs = list_all_system_packages(device_serial, user_id);
let enabled_sys_packs = hashset_system_packages(PackageState::Enabled, device_serial, user_id);
let disabled_sys_packs =
hashset_system_packages(PackageState::Disabled, device_serial, user_id);
let mut description;
let mut uad_list;
let mut state;
let mut removal;
let mut user_package: Vec<PackageRow> = Vec::new();
// This assumes `for` iter-count is **exact**:
// there are no `continue`s, `break`s, or `return`s.
let mut user_package: Vec<PackageRow> = Vec::with_capacity(all_sys_packs.len());

for p_name in all_system_packages.lines() {
for pack_name in all_sys_packs {
let p_name = &pack_name;
state = PackageState::Uninstalled;
description = "[No description]: CONTRIBUTION WELCOMED";
uad_list = UadList::Unlisted;
Expand All @@ -49,9 +51,9 @@ pub fn fetch_packages(
removal = package.removal;
}

if enabled_system_packages.contains(p_name) {
if enabled_sys_packs.contains(p_name) {
state = PackageState::Enabled;
} else if disabled_system_packages.contains(p_name) {
} else if disabled_sys_packs.contains(p_name) {
state = PackageState::Disabled;
}

Expand All @@ -74,7 +76,7 @@ pub fn string_to_theme(theme: &str) -> Theme {
}
}

pub fn setup_uad_dir(dir: &PathBuf) -> PathBuf {
pub fn setup_uad_dir(dir: &Path) -> PathBuf {
let dir = dir.join("uad");
if let Err(e) = fs::create_dir_all(&dir) {
error!("Can't create directory: {dir:?}");
Expand Down Expand Up @@ -105,7 +107,6 @@ pub fn open_url(dir: PathBuf) {
}

#[rustfmt::skip]
#[allow(clippy::option_if_let_else)]
pub fn last_modified_date(file: PathBuf) -> DateTime<Utc> {
fs::metadata(file).map_or_else(|_| Utc::now(), |metadata| match metadata.modified() {
Ok(time) => time.into(),
Expand Down

0 comments on commit ac056dd

Please sign in to comment.