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

Commit 808497be authored by Kholoud Mohamed's avatar Kholoud Mohamed
Browse files

Migrate setApplicationHidden to the policy engine

Bug: 273496614
Test: btest a.d.c.ApplicationHiddenTest
Change-Id: I57d8730e64d970618ae17fae6e77292f49207f0d
parent d9fc7a57
Loading
Loading
Loading
Loading
+45 −39
Original line number Diff line number Diff line
@@ -771,7 +771,9 @@ final class DevicePolicyEngine {
        Intent intent = new Intent(PolicyUpdateReceiver.ACTION_DEVICE_POLICY_SET_RESULT);
        intent.setPackage(admin.getPackageName());

        List<ResolveInfo> receivers = mContext.getPackageManager().queryBroadcastReceiversAsUser(
        Binder.withCleanCallingIdentity(() -> {
            List<ResolveInfo> receivers =
                    mContext.getPackageManager().queryBroadcastReceiversAsUser(
                            intent,
                            PackageManager.ResolveInfoFlags.of(PackageManager.GET_RECEIVERS),
                            admin.getUserId());
@@ -793,6 +795,7 @@ final class DevicePolicyEngine {
            intent.putExtras(extras);

            maybeSendIntentToAdminReceivers(intent, UserHandle.of(admin.getUserId()), receivers);
        });
    }

    // TODO(b/261430877): Finalise the decision on which admins to send the updates to.
@@ -821,7 +824,9 @@ final class DevicePolicyEngine {
        Intent intent = new Intent(PolicyUpdateReceiver.ACTION_DEVICE_POLICY_CHANGED);
        intent.setPackage(admin.getPackageName());

        List<ResolveInfo> receivers = mContext.getPackageManager().queryBroadcastReceiversAsUser(
        Binder.withCleanCallingIdentity(() -> {
            List<ResolveInfo> receivers =
                    mContext.getPackageManager().queryBroadcastReceiversAsUser(
                            intent,
                            PackageManager.ResolveInfoFlags.of(PackageManager.GET_RECEIVERS),
                            admin.getUserId());
@@ -842,6 +847,7 @@ final class DevicePolicyEngine {

            maybeSendIntentToAdminReceivers(
                    intent, UserHandle.of(admin.getUserId()), receivers);
        });
    }

    private void maybeSendIntentToAdminReceivers(
+25 −2
Original line number Diff line number Diff line
@@ -13544,9 +13544,32 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                Slogf.v(LOG_TAG, "calling pm.setApplicationHiddenSettingAsUser(%s, %b, %d)",
                        packageName, hidden, userId);
            }
            if (isDevicePolicyEngineEnabled()) {
                EnforcingAdmin admin = getEnforcingAdminForCaller(who, callerPackage);
                mDevicePolicyEngine.setLocalPolicy(
                        PolicyDefinition.APPLICATION_HIDDEN(packageName),
                        admin,
                        new BooleanPolicyValue(hidden),
                        userId);
                Boolean resolvedPolicy = mDevicePolicyEngine.getResolvedPolicy(
                        PolicyDefinition.APPLICATION_HIDDEN(packageName), userId);
                result = mInjector.binderWithCleanCallingIdentity(() -> {
                    try {
                        // This is a best effort to continue returning the same value that was
                        // returned before the policy engine migration.
                        return mInjector.getIPackageManager().getPackageInfo(
                                packageName, MATCH_UNINSTALLED_PACKAGES, userId) != null
                                && (mIPackageManager.getApplicationHiddenSettingAsUser(
                                        packageName, userId) == hidden);
                    } catch (RemoteException e) {
                        return false;
                    }
                });
            } else {
                result = mInjector.binderWithCleanCallingIdentity(() -> mIPackageManager
                        .setApplicationHiddenSettingAsUser(packageName, hidden, userId));
            }
        }
        DevicePolicyEventLogger
                .createEvent(DevicePolicyEnums.SET_APPLICATION_HIDDEN)
                .setAdmin(caller.getPackageName())
+33 −2
Original line number Diff line number Diff line
@@ -226,8 +226,7 @@ final class PolicyDefinition<V> {
     * Passing in {@code null} for {@code packageName} will return
     * {@link #GENERIC_APPLICATION_RESTRICTIONS}.
     */
    static PolicyDefinition<Bundle> APPLICATION_RESTRICTIONS(
            String packageName) {
    static PolicyDefinition<Bundle> APPLICATION_RESTRICTIONS(String packageName) {
        if (packageName == null) {
            return GENERIC_APPLICATION_RESTRICTIONS;
        }
@@ -254,6 +253,34 @@ final class PolicyDefinition<V> {
            (Integer value, Context context, Integer userId, PolicyKey policyKey) -> true,
            new IntegerPolicySerializer());

    // This is saved in the static map sPolicyDefinitions so that we're able to reconstruct the
    // actual policy with the correct arguments (i.e. packageName) when reading the policies from
    // xml.
    static PolicyDefinition<Boolean> GENERIC_APPLICATION_HIDDEN =
            new PolicyDefinition<>(
                    new PackagePolicyKey(
                            DevicePolicyIdentifiers.APPLICATION_HIDDEN_POLICY),
                    // TODO(b/276713779): Don't need to take in a resolution mechanism since its
                    //  never used, but might need some refactoring to not always assume a non-null
                    //  mechanism.
                    TRUE_MORE_RESTRICTIVE,
                    POLICY_FLAG_LOCAL_ONLY_POLICY,
                    PolicyEnforcerCallbacks::setApplicationHidden,
                    new BooleanPolicySerializer());

    /**
     * Passing in {@code null} for {@code packageName} will return
     * {@link #GENERIC_APPLICATION_HIDDEN}.
     */
    static PolicyDefinition<Boolean> APPLICATION_HIDDEN(String packageName) {
        if (packageName == null) {
            return GENERIC_APPLICATION_HIDDEN;
        }
        return GENERIC_APPLICATION_HIDDEN.createPolicyDefinition(
                new PackagePolicyKey(
                        DevicePolicyIdentifiers.APPLICATION_HIDDEN_POLICY, packageName));
    }

    private static final Map<String, PolicyDefinition<?>> POLICY_DEFINITIONS = new HashMap<>();
    private static Map<String, Integer> USER_RESTRICTION_FLAGS = new HashMap<>();

@@ -272,6 +299,10 @@ final class PolicyDefinition<V> {
                GENERIC_APPLICATION_RESTRICTIONS);
        POLICY_DEFINITIONS.put(DevicePolicyIdentifiers.RESET_PASSWORD_TOKEN_POLICY,
                RESET_PASSWORD_TOKEN);
        POLICY_DEFINITIONS.put(DevicePolicyIdentifiers.KEYGUARD_DISABLED_FEATURES_POLICY,
                KEYGUARD_DISABLED_FEATURES);
        POLICY_DEFINITIONS.put(DevicePolicyIdentifiers.APPLICATION_HIDDEN_POLICY,
                GENERIC_APPLICATION_HIDDEN);

        // User Restriction Policies
        USER_RESTRICTION_FLAGS.put(UserManager.DISALLOW_MODIFY_ACCOUNTS, /* flags= */ 0);
+20 −4
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ final class PolicyEnforcerCallbacks {
        return Boolean.TRUE.equals(Binder.withCleanCallingIdentity(() -> {
            if (!(policyKey instanceof PackagePermissionPolicyKey)) {
                throw new IllegalArgumentException("policyKey is not of type "
                        + "PermissionGrantStatePolicyKey");
                        + "PermissionGrantStatePolicyKey, passed in policyKey is: " + policyKey);
            }
            PackagePermissionPolicyKey parsedKey = (PackagePermissionPolicyKey) policyKey;
            Objects.requireNonNull(parsedKey.getPermissionName());
@@ -165,7 +165,7 @@ final class PolicyEnforcerCallbacks {
            try {
                if (!(policyKey instanceof IntentFilterPolicyKey)) {
                    throw new IllegalArgumentException("policyKey is not of type "
                            + "IntentFilterPolicyKey");
                            + "IntentFilterPolicyKey, passed in policyKey is: " + policyKey);
                }
                IntentFilterPolicyKey parsedKey =
                        (IntentFilterPolicyKey) policyKey;
@@ -193,7 +193,7 @@ final class PolicyEnforcerCallbacks {
        return Boolean.TRUE.equals(Binder.withCleanCallingIdentity(() -> {
            if (!(policyKey instanceof PackagePolicyKey)) {
                throw new IllegalArgumentException("policyKey is not of type "
                        + "PackagePolicyKey");
                        + "PackagePolicyKey, passed in policyKey is: " + policyKey);
            }
            PackagePolicyKey parsedKey = (PackagePolicyKey) policyKey;
            String packageName = Objects.requireNonNull(parsedKey.getPackageName());
@@ -211,7 +211,7 @@ final class PolicyEnforcerCallbacks {
        return Boolean.TRUE.equals(Binder.withCleanCallingIdentity(() -> {
            if (!(policyKey instanceof UserRestrictionPolicyKey)) {
                throw new IllegalArgumentException("policyKey is not of type "
                        + "UserRestrictionPolicyKey");
                        + "UserRestrictionPolicyKey, passed in policyKey is: " + policyKey);
            }
            UserRestrictionPolicyKey parsedKey =
                    (UserRestrictionPolicyKey) policyKey;
@@ -221,4 +221,20 @@ final class PolicyEnforcerCallbacks {
            return true;
        }));
    }

    static boolean setApplicationHidden(
            @Nullable Boolean hide, @NonNull Context context, int userId,
            @NonNull PolicyKey policyKey) {
        return Boolean.TRUE.equals(Binder.withCleanCallingIdentity(() -> {
            if (!(policyKey instanceof PackagePolicyKey)) {
                throw new IllegalArgumentException("policyKey is not of type "
                        + "PackagePolicyKey, passed in policyKey is: " + policyKey);
            }
            PackagePolicyKey parsedKey = (PackagePolicyKey) policyKey;
            String packageName = Objects.requireNonNull(parsedKey.getPackageName());
            IPackageManager packageManager = AppGlobals.getPackageManager();
            return packageManager.setApplicationHiddenSettingAsUser(
                    packageName, hide != null && hide, userId);
        }));
    }
}