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

Commit 17e92915 authored by Hai Zhang's avatar Hai Zhang
Browse files

Move role holder qualification check into Role.

Previously the checks about ApplicationInfo was done in
RoleControllerServiceImpl, so move it into Role to make sure we do all
the necessary checks every time.

Bug: 110557011
Test: build
Change-Id: I7d106c2f4f209b23e5b5d95697d975cc75ee10ee
parent 9da04eab
Loading
Loading
Loading
Loading
+38 −8
Original line number Diff line number Diff line
@@ -192,7 +192,7 @@ public class Role {
     * @param packageName the package name of the application to get confirmation message for
     * @param context the {@code Context} to retrieve system services
     *
     * @return the confirmation message, or {@code null} if no confirmation is needed.
     * @return the confirmation message, or {@code null} if no confirmation is needed
     */
    @Nullable
    public CharSequence getConfirmationMessage(@NonNull String packageName,
@@ -205,7 +205,7 @@ public class Role {

    /**
     * Check whether a package is qualified for this role, i.e. whether it contains all the required
     * components.
     * components (plus meeting some other general restrictions).
     *
     * @param packageName the package name to check for
     * @param context the {@code Context} to retrieve system services
@@ -213,9 +213,11 @@ public class Role {
     * @return whether the package is qualified for a role
     */
    public boolean isPackageQualified(@NonNull String packageName, @NonNull Context context) {
        if (Objects.equals(packageName, PACKAGE_NAME_ANDROID_SYSTEM)) {
        if (!isPackageQualifiedWithoutCheckingRequiredComponentsAsUser(packageName,
                Process.myUserHandle(), context)) {
            return false;
        }

        int requiredComponentsSize = mRequiredComponents.size();
        for (int i = 0; i < requiredComponentsSize; i++) {
            RequiredComponent requiredComponent = mRequiredComponents.get(i);
@@ -225,17 +227,18 @@ public class Role {
                return false;
            }
        }

        return true;
    }

    /**
     * Get the list of packages that are qualified for this role, i.e. packages containing all the
     * required components.
     * required components (plus meeting some other general restrictions).
     *
     * @param user the user to get the qualifying packages.
     * @param context the {@code Context} to retrieve system services
     *
     * @return the set of packages that are qualified for this role
     * @return the list of packages that are qualified for this role
     */
    @NonNull
    public List<String> getQualifyingPackagesAsUser(@NonNull UserHandle user,
@@ -256,9 +259,6 @@ public class Role {
                ComponentName componentName = qualifyingComponents.get(qualifyingComponentsIndex);

                String packageName = componentName.getPackageName();
                if (Objects.equals(packageName, PACKAGE_NAME_ANDROID_SYSTEM)) {
                    continue;
                }
                Integer componentCount = packageComponentCountMap.get(packageName);
                packageComponentCountMap.put(packageName, componentCount == null ? 1
                        : componentCount + 1);
@@ -269,16 +269,46 @@ public class Role {
        int packageComponentCountMapSize = packageComponentCountMap.size();
        for (int i = 0; i < packageComponentCountMapSize; i++) {
            int componentCount = packageComponentCountMap.valueAt(i);

            if (componentCount != requiredComponentsSize) {
                continue;
            }
            String packageName = packageComponentCountMap.keyAt(i);
            if (!isPackageQualifiedWithoutCheckingRequiredComponentsAsUser(packageName, user,
                    context)) {
                continue;
            }
            qualifyingPackages.add(packageName);
        }

        return qualifyingPackages;
    }

    private boolean isPackageQualifiedWithoutCheckingRequiredComponentsAsUser(
            @NonNull String packageName, @NonNull UserHandle user, @NonNull Context context) {
        if (Objects.equals(packageName, PACKAGE_NAME_ANDROID_SYSTEM)) {
            return false;
        }

        ApplicationInfo applicationInfo = PackageUtils.getApplicationInfoAsUser(packageName, user,
                context);
        if (applicationInfo == null) {
            Log.w(LOG_TAG, "Cannot get ApplicationInfo for package: " + packageName + ", user: "
                    + user.getIdentifier());
            return false;
        }

        // TODO: STOPSHIP: Check for disabled packages?

        // TODO: STOPSHIP: Add and check PackageManager.getSharedLibraryInfo().

        if (applicationInfo.isInstantApp()) {
            return false;
        }

        return true;
    }

    /**
     * Grant this role to an application.
     *
+0 −14
Original line number Diff line number Diff line
@@ -167,20 +167,6 @@ public class RoleControllerServiceImpl extends RoleControllerService {
            callback.onFailure();
            return;
        }

        ApplicationInfo applicationInfo = PackageUtils.getApplicationInfo(packageName, this);
        if (applicationInfo == null) {
            Log.e(LOG_TAG, "Cannot get ApplicationInfo for package: " + packageName);
            callback.onFailure();
            return;
        }
        // TODO: STOPSHIP: Check for disabled packages?
        // TODO: STOPSHIP: Add and check PackageManager.getSharedLibraryInfo().
        if (applicationInfo.isInstantApp()) {
            Log.e(LOG_TAG, "Cannot set Instant App as role holder, package: " + packageName);
            callback.onFailure();
            return;
        }
        if (!role.isPackageQualified(packageName, this)) {
            Log.e(LOG_TAG, "Package does not qualify for the role, package: " + packageName
                    + ", role: " + roleName);