From 89f8a83b3186a1c8ab76faa0960c44ec0710ca62 Mon Sep 17 00:00:00 2001 From: amaa-99 <-> Date: Tue, 25 Jul 2023 17:40:23 +0200 Subject: [PATCH] * [Issue #3] Bug in the implementation of Api.getPackagesForUser() leading to empty package list. Fixed the error in the syntax of the 'pm list packages' command executed in the Api.getPackagesForUser() leading to an execution error and consequently to an empty package list getting returned. --- .../main/java/dev/ukanth/ufirewall/Api.java | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/dev/ukanth/ufirewall/Api.java b/app/src/main/java/dev/ukanth/ufirewall/Api.java index 91f74ff3..6540a03a 100644 --- a/app/src/main/java/dev/ukanth/ufirewall/Api.java +++ b/app/src/main/java/dev/ukanth/ufirewall/Api.java @@ -1427,7 +1427,7 @@ public static List getApps(Context ctx, GetAppList appList) { } } } - //use pm list packages -f -U --user 10 + int pkgManagerFlags = PackageManager.GET_META_DATA; // it's useless to iterate over uninstalled packages if we don't support multi-profile apps if (G.supportDual()) { @@ -1686,19 +1686,15 @@ private static boolean packagesExistForUserUid(HashMap pkgs, int } public static HashMap getPackagesForUser(List userProfile) { - HashMap listApps = new HashMap<>(); - for(Integer integer: userProfile) { - Shell.Result result = Shell.cmd("pm list packages -U --user " + integer).exec(); - List out = result.getOut(); - Matcher matcher; - for (String item : out) { - matcher = dual_pattern.matcher(item); - if (matcher.find() && matcher.groupCount() > 0) { - String packageName = matcher.group(1); - String packageId = matcher.group(2); - Log.i(TAG, packageId + " " + packageName); - listApps.put(Integer.parseInt(packageId), packageName); - } + HashMap listApps = new HashMap<>(); + PackageManager pkgManager = ctx.getPackageManager(); + for (Integer uid : userProfile) { + List installed = pkgManager.getInstalledApplications(PackageManager.GET_META_DATA); + for (ApplicationInfo appInfo : installed) { + String packageName = appInfo.packageName; + int packageId = appInfo.uid; + Log.i(TAG, packageId + " " + packageName); + listApps.put(packageId, packageName); } } return listApps.size() > 0 ? listApps : null;