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

Commit d4e5ca03 authored by Kholoud Mohamed's avatar Kholoud Mohamed
Browse files

Give precedence to protectedPackages set locally

Bug: 258442697
Test: android.devicepolicy.cts.UserControlDisabledPackagesTest
Change-Id: I8f6646f01a9810fb0b680f5099f40414d01c14ee
parent 1be7bd6a
Loading
Loading
Loading
Loading
+7 −2
Original line number Original line Diff line number Diff line
@@ -375,10 +375,15 @@ public abstract class PackageManagerInternal {
            int deviceOwnerUserId, String deviceOwner, SparseArray<String> profileOwners);
            int deviceOwnerUserId, String deviceOwner, SparseArray<String> profileOwners);


    /**
    /**
     * Marks packages as protected for a given user or all users in case of USER_ALL.
     * Marks packages as protected for a given user or all users in case of USER_ALL. Setting
     * {@code packageNames} to {@code null} means unset all existing protected packages for the
     * given user.
     *
     * <p> Note that setting it if set for a specific user, it takes precedence over the packages
     * set globally using USER_ALL.
     */
     */
    public abstract void setOwnerProtectedPackages(
    public abstract void setOwnerProtectedPackages(
            @UserIdInt int userId, @NonNull List<String> packageNames);
            @UserIdInt int userId, @Nullable List<String> packageNames);


    /**
    /**
     * Returns {@code true} if a given package can't be wiped. Otherwise, returns {@code false}.
     * Returns {@code true} if a given package can't be wiped. Otherwise, returns {@code false}.
+1 −1
Original line number Original line Diff line number Diff line
@@ -354,7 +354,7 @@ abstract class PackageManagerInternalBase extends PackageManagerInternal {
    @Override
    @Override
    @Deprecated
    @Deprecated
    public final void setOwnerProtectedPackages(
    public final void setOwnerProtectedPackages(
            @UserIdInt int userId, @NonNull List<String> packageNames) {
            @UserIdInt int userId, @Nullable List<String> packageNames) {
        getProtectedPackages().setOwnerProtectedPackages(userId, packageNames);
        getProtectedPackages().setOwnerProtectedPackages(userId, packageNames);
    }
    }


+11 −6
Original line number Original line Diff line number Diff line
@@ -16,7 +16,6 @@


package com.android.server.pm;
package com.android.server.pm;


import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.annotation.UserIdInt;
import android.content.Context;
import android.content.Context;
@@ -81,8 +80,8 @@ public class ProtectedPackages {


    /** Sets packages protected by a device or profile owner. */
    /** Sets packages protected by a device or profile owner. */
    public synchronized void setOwnerProtectedPackages(
    public synchronized void setOwnerProtectedPackages(
            @UserIdInt int userId, @NonNull List<String> packageNames) {
            @UserIdInt int userId, @Nullable List<String> packageNames) {
        if (packageNames.isEmpty()) {
        if (packageNames == null) {
            mOwnerProtectedPackages.remove(userId);
            mOwnerProtectedPackages.remove(userId);
        } else {
        } else {
            mOwnerProtectedPackages.put(userId, new ArraySet<>(packageNames));
            mOwnerProtectedPackages.put(userId, new ArraySet<>(packageNames));
@@ -134,15 +133,21 @@ public class ProtectedPackages {
     */
     */
    private synchronized boolean isOwnerProtectedPackage(
    private synchronized boolean isOwnerProtectedPackage(
            @UserIdInt int userId, String packageName) {
            @UserIdInt int userId, String packageName) {
        return isPackageProtectedForUser(UserHandle.USER_ALL, packageName)
        return hasProtectedPackages(userId)
                || isPackageProtectedForUser(userId, packageName);
                ? isPackageProtectedForUser(userId, packageName)
                : isPackageProtectedForUser(UserHandle.USER_ALL, packageName);
    }
    }


    private synchronized boolean isPackageProtectedForUser(int userId, String packageName) {
    private synchronized boolean isPackageProtectedForUser(
            @UserIdInt int userId, String packageName) {
        int userIdx = mOwnerProtectedPackages.indexOfKey(userId);
        int userIdx = mOwnerProtectedPackages.indexOfKey(userId);
        return userIdx >= 0 && mOwnerProtectedPackages.valueAt(userIdx).contains(packageName);
        return userIdx >= 0 && mOwnerProtectedPackages.valueAt(userIdx).contains(packageName);
    }
    }


    private synchronized boolean hasProtectedPackages(@UserIdInt int userId) {
        return mOwnerProtectedPackages.indexOfKey(userId) >= 0;
    }

    /**
    /**
     * Returns {@code true} if a given package's state is protected. Otherwise, returns
     * Returns {@code true} if a given package's state is protected. Otherwise, returns
     * {@code false}.
     * {@code false}.