Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4ddac7d6 authored by William Loh's avatar William Loh
Browse files

Default to all users in pm list packages shell command

Change the default user from USER_SYSTEM to USER_ALL users as it is more
intuitive on multi-user form factors. If a -U option is given then all uids
are listed for USER_ALL. Ex:
    $ adb shell pm list packages -U com.android.systemui
    package:com.android.systemui uid:10145,1110145

Bug: 146343680
Test: existing unit tests still pass
Change-Id: I814c5c327752ae1e14882c284461b2edff710dae
parent ad9a2371
Loading
Loading
Loading
Loading
+86 −65
Original line number Diff line number Diff line
@@ -128,6 +128,7 @@ import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -829,7 +830,7 @@ class PackageManagerShellCommand extends ShellCommand {
        boolean showVersionCode = false;
        boolean listApexOnly = false;
        int uid = -1;
        int userId = UserHandle.USER_SYSTEM;
        int defaultUserId = UserHandle.USER_ALL;
        try {
            String opt;
            while ((opt = getNextOption()) != null) {
@@ -873,7 +874,7 @@ class PackageManagerShellCommand extends ShellCommand {
                        listApexOnly = true;
                        break;
                    case "--user":
                        userId = UserHandle.parseUserArg(getNextArgRequired());
                        defaultUserId = UserHandle.parseUserArg(getNextArgRequired());
                        break;
                    case "--uid":
                        showUid = true;
@@ -891,22 +892,29 @@ class PackageManagerShellCommand extends ShellCommand {

        final String filter = getNextArg();

        if (userId == UserHandle.USER_ALL) {
            getFlags |= PackageManager.MATCH_KNOWN_PACKAGES;
        int[] userIds = {defaultUserId};
        if (defaultUserId == UserHandle.USER_ALL) {
            final UserManagerInternal umi = LocalServices.getService(UserManagerInternal.class);
            userIds = umi.getUserIds();
        }
        if (showSdks) {
            getFlags |= PackageManager.MATCH_STATIC_SHARED_AND_SDK_LIBRARIES;
        }

        // Build a map of packages to a list of corresponding uids. Keys are strings containing
        // the sdk or package name along with optional additional information based on opt.
        final Map<String, List<String>> out = new HashMap<>();
        for (int userId : userIds) {
            final int translatedUserId =
                    translateUserId(userId, UserHandle.USER_SYSTEM, "runListPackages");
        @SuppressWarnings("unchecked")
        final ParceledListSlice<PackageInfo> slice =
            @SuppressWarnings("unchecked") final ParceledListSlice<PackageInfo> slice =
                    mInterface.getInstalledPackages(getFlags, translatedUserId);
            final List<PackageInfo> packages = slice.getList();

            final int count = packages.size();
            for (int p = 0; p < count; p++) {
                final PackageInfo info = packages.get(p);
                final StringBuilder stringBuilder = new StringBuilder();
                if (filter != null && !info.packageName.contains(filter)) {
                    continue;
                }
@@ -915,21 +923,23 @@ class PackageManagerShellCommand extends ShellCommand {
                    continue;
                }

            final boolean isSystem = !isApex &&
                    (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
                final boolean isSystem = !isApex
                        && (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
                final boolean isEnabled = !isApex && info.applicationInfo.enabled;
            if ((listDisabled && isEnabled) ||
                    (listEnabled && !isEnabled) ||
                    (listSystem && !isSystem) ||
                    (listThirdParty && isSystem) ||
                    (listApexOnly && !isApex)) {
                if ((listDisabled && isEnabled)
                        || (listEnabled && !isEnabled)
                        || (listSystem && !isSystem)
                        || (listThirdParty && isSystem)
                        || (listApexOnly && !isApex)) {
                    continue;
                }

                String name = null;
                if (showSdks) {
                    final ParceledListSlice<SharedLibraryInfo> libsSlice =
                        mInterface.getDeclaredSharedLibraries(info.packageName, getFlags, userId);
                            mInterface.getDeclaredSharedLibraries(
                                info.packageName, getFlags, userId
                            );
                    if (libsSlice == null) {
                        continue;
                    }
@@ -948,27 +958,38 @@ class PackageManagerShellCommand extends ShellCommand {
                    name = info.packageName;
                }

            pw.print(prefix);
                stringBuilder.append(prefix);
                if (showSourceDir) {
                pw.print(info.applicationInfo.sourceDir);
                pw.print("=");
                    stringBuilder.append(info.applicationInfo.sourceDir);
                    stringBuilder.append("=");
                }
            pw.print(name);
                stringBuilder.append(name);
                if (showVersionCode) {
                pw.print(" versionCode:");
                    stringBuilder.append(" versionCode:");
                    if (info.applicationInfo != null) {
                    pw.print(info.applicationInfo.longVersionCode);
                        stringBuilder.append(info.applicationInfo.longVersionCode);
                    } else {
                    pw.print(info.getLongVersionCode());
                        stringBuilder.append(info.getLongVersionCode());
                    }
                }
                if (listInstaller) {
                pw.print("  installer=");
                pw.print(mInterface.getInstallerPackageName(info.packageName));
                    stringBuilder.append("  installer=");
                    stringBuilder.append(mInterface.getInstallerPackageName(info.packageName));
                }
                List<String> uids = out.computeIfAbsent(
                        stringBuilder.toString(), k -> new ArrayList<>()
                );
                if (showUid && !isApex) {
                    uids.add(String.valueOf(info.applicationInfo.uid));
                }
            }
        }
        for (Map.Entry<String, List<String>> entry : out.entrySet()) {
            pw.print(entry.getKey());
            List<String> uids = entry.getValue();
            if (!uids.isEmpty()) {
                pw.print(" uid:");
                pw.print(info.applicationInfo.uid);
                pw.print(String.join(",", uids));
            }
            pw.println();
        }