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

Commit 20fbef22 authored by Alan Stokes's avatar Alan Stokes
Browse files

Refactor getPackagesForSharedUid.

Replace gstSharedUserIdForPackage() and getPackagesForSharedUserId()
with a function to combine the two,
getSharedUserPackagesForPackage(). This satisfies all the existing use
cases with reasonably clean semantics, and avoids the need to
special-case the situation where the package doesn't have a shared
UID.

Clarify that the result is never null, and remove some redundant null
checks.

Fixes: 146418551
Test: atest PackageManagerTests
Change-Id: I8f34c9071434f908893437a5c6c89defaa5fd576
parent 85d68cd3
Loading
Loading
Loading
Loading
+5 −9
Original line number Diff line number Diff line
@@ -646,16 +646,12 @@ public abstract class PackageManagerInternal {
    public abstract SparseArray<String> getAppsWithSharedUserIds();

    /**
     * Get the value of attribute android:sharedUserId for the given packageName if specified,
     * otherwise {@code null}.
     * Get all packages which share the same userId as the specified package, or an empty array
     * if the package does not have a shared userId.
     */
    public abstract String getSharedUserIdForPackage(@NonNull String packageName);

    /**
     * Get all packages which specified the given sharedUserId as android:sharedUserId attribute
     * or an empty array if no package specified it.
     */
    public abstract String[] getPackagesForSharedUserId(@NonNull String sharedUserId, int userId);
    @NonNull
    public abstract String[] getSharedUserPackagesForPackage(@NonNull String packageName,
            int userId);

    /**
     * Return if device is currently in a "core" boot environment, typically
+2 −3
Original line number Diff line number Diff line
@@ -1918,9 +1918,8 @@ public final class ProcessList {
                // Get all packages belongs to the same shared uid. sharedPackages is empty array
                // if it doesn't have shared uid.
                final PackageManagerInternal pmInt = mService.getPackageManagerInternalLocked();
                final String sharedUserId = pmInt.getSharedUserIdForPackage(app.info.packageName);
                final String[] sharedPackages = pmInt.getPackagesForSharedUserId(sharedUserId,
                        app.userId);
                final String[] sharedPackages = pmInt.getSharedUserPackagesForPackage(
                        app.info.packageName, app.userId);
                pkgDataInfoMap = getPackageAppDataInfoMap(pmInt, sharedPackages.length == 0
                        ? new String[]{app.info.packageName} : sharedPackages, uid);
            } else {
+20 −38
Original line number Diff line number Diff line
@@ -249,7 +249,6 @@ import android.os.storage.VolumeInfo;
import android.os.storage.VolumeRecord;
import android.permission.IPermissionManager;
import android.provider.DeviceConfig;
import android.provider.MediaStore;
import android.provider.Settings.Global;
import android.provider.Settings.Secure;
import android.security.KeyStore;
@@ -23063,16 +23062,10 @@ public class PackageManagerService extends IPackageManager.Stub
        }
        @Override
        public String getSharedUserIdForPackage(String packageName) {
            synchronized (mLock) {
                return getSharedUserIdForPackageLocked(packageName);
            }
        }
        @Override
        public String[] getPackagesForSharedUserId(String sharedUserId, int userId) {
        @NonNull
        public String[] getSharedUserPackagesForPackage(String packageName, int userId) {
            synchronized (mLock) {
                return getPackagesForSharedUserIdLocked(sharedUserId, userId);
                return getSharedUserPackagesForPackageLocked(packageName, userId);
            }
        }
@@ -23305,21 +23298,16 @@ public class PackageManagerService extends IPackageManager.Stub
    }
    @GuardedBy("mLock")
    private String getSharedUserIdForPackageLocked(String packageName) {
        final PackageSetting ps = mSettings.mPackages.get(packageName);
        return (ps != null && ps.isSharedUser()) ? ps.sharedUser.name : null;
    }
    @GuardedBy("mLock")
    private String[] getPackagesForSharedUserIdLocked(String sharedUserId, int userId) {
        try {
            final SharedUserSetting sus = mSettings.getSharedUserLPw(
                    sharedUserId, 0, 0, false);
            if (sus == null) {
    @NonNull
    private String[] getSharedUserPackagesForPackageLocked(String packageName, int userId) {
        final PackageSetting packageSetting = mSettings.mPackages.get(packageName);
        if (packageSetting == null || !packageSetting.isSharedUser()) {
            return EmptyArray.STRING;
        }
            String[] res = new String[sus.packages.size()];
            final Iterator<PackageSetting> it = sus.packages.iterator();
        ArraySet<PackageSetting> packages = packageSetting.sharedUser.packages;
        String[] res = new String[packages.size()];
        final Iterator<PackageSetting> it = packages.iterator();
        int i = 0;
        while (it.hasNext()) {
            PackageSetting ps = it.next();
@@ -23328,13 +23316,7 @@ public class PackageManagerService extends IPackageManager.Stub
            }
        }
        res = ArrayUtils.trimToSize(res, i);
            if (res != null) {
                return res;
            }
        } catch (PackageManagerException e) {
            // Should not happen
        }
        return EmptyArray.STRING;
        return res != null ? res : EmptyArray.STRING;
    }
    @Override
+4 −8
Original line number Diff line number Diff line
@@ -1579,15 +1579,11 @@ public class PermissionManagerService extends IPermissionManager.Stub {
            }

            // If shared user we just reset the state to which only this app contributed.
            final String sharedUserId =
                    mPackageManagerInt.getSharedUserIdForPackage(pkg.getPackageName());
            final String[] pkgNames =
                    mPackageManagerInt.getPackagesForSharedUserId(sharedUserId, userId);
            if (pkgNames != null && pkgNames.length > 0) {
            final String[] pkgNames = mPackageManagerInt.getSharedUserPackagesForPackage(
                    pkg.getPackageName(), userId);
            if (pkgNames.length > 0) {
                boolean used = false;
                final int packageCount = pkgNames.length;
                for (int j = 0; j < packageCount; j++) {
                    final String sharedPkgName = pkgNames[j];
                for (String sharedPkgName : pkgNames) {
                    final AndroidPackage sharedPkg =
                            mPackageManagerInt.getPackage(sharedPkgName);
                    if (sharedPkg != null && !sharedPkg.getPackageName().equals(packageName)
+8 −9
Original line number Diff line number Diff line
@@ -353,9 +353,9 @@ public final class PermissionPolicyService extends SystemService {
        final PermissionToOpSynchroniser synchroniser = new PermissionToOpSynchroniser(
                getUserContext(getContext(), UserHandle.of(userId)));
        synchroniser.addPackage(pkg.packageName);
        final String[] sharedPkgNames = packageManagerInternal.getPackagesForSharedUserId(
                pkg.sharedUserId, userId);
        if (sharedPkgNames != null) {
        final String[] sharedPkgNames = packageManagerInternal.getSharedUserPackagesForPackage(
                pkg.packageName, userId);

        for (String sharedPkgName : sharedPkgNames) {
            final AndroidPackage sharedPkg = packageManagerInternal
                    .getPackage(sharedPkgName);
@@ -363,7 +363,6 @@ public final class PermissionPolicyService extends SystemService {
                synchroniser.addPackage(sharedPkg.getPackageName());
            }
        }
        }
        synchroniser.syncPackages();
    }