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

Commit 5c32f1c1 authored by Tony Mak's avatar Tony Mak Committed by android-build-merger
Browse files

Merge "Pipe the user restriction to the admin dialog" into pi-dev am: c76929eb

am: 45f39972

Change-Id: I9aefd00b88469de40b81ec18493db10050ae7990
parents ce357b9a 45f39972
Loading
Loading
Loading
Loading
+66 −32
Original line number Original line Diff line number Diff line
@@ -46,6 +46,7 @@ import android.widget.TextView;
import com.android.internal.widget.LockPatternUtils;
import com.android.internal.widget.LockPatternUtils;


import java.util.List;
import java.util.List;
import java.util.Objects;


/**
/**
 * Utility class to host methods usable in adding a restricted padlock icon and showing admin
 * Utility class to host methods usable in adding a restricted padlock icon and showing admin
@@ -90,29 +91,29 @@ public class RestrictedLockUtils {
            // Restriction is not enforced.
            // Restriction is not enforced.
            return null;
            return null;
        } else if (enforcingUsers.size() > 1) {
        } else if (enforcingUsers.size() > 1) {
            return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
            return EnforcedAdmin.createDefaultEnforcedAdminWithRestriction(userRestriction);
        }
        }


        final int restrictionSource = enforcingUsers.get(0).getUserRestrictionSource();
        final int restrictionSource = enforcingUsers.get(0).getUserRestrictionSource();
        final int adminUserId = enforcingUsers.get(0).getUserHandle().getIdentifier();
        final int adminUserId = enforcingUsers.get(0).getUserHandle().getIdentifier();

        if (restrictionSource == UserManager.RESTRICTION_SOURCE_PROFILE_OWNER) {
        if (restrictionSource == UserManager.RESTRICTION_SOURCE_PROFILE_OWNER) {
            // Check if it is a profile owner of the user under consideration.
            // Check if it is a profile owner of the user under consideration.
            if (adminUserId == userId) {
            if (adminUserId == userId) {
                return getProfileOwner(context, adminUserId);
                return getProfileOwner(context, userRestriction, adminUserId);
            } else {
            } else {
                // Check if it is a profile owner of a managed profile of the current user.
                // Check if it is a profile owner of a managed profile of the current user.
                // Otherwise it is in a separate user and we return a default EnforcedAdmin.
                // Otherwise it is in a separate user and we return a default EnforcedAdmin.
                final UserInfo parentUser = um.getProfileParent(adminUserId);
                final UserInfo parentUser = um.getProfileParent(adminUserId);
                return (parentUser != null && parentUser.id == userId)
                return (parentUser != null && parentUser.id == userId)
                        ? getProfileOwner(context, adminUserId)
                        ? getProfileOwner(context, userRestriction, adminUserId)
                        : EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
                        : EnforcedAdmin.createDefaultEnforcedAdminWithRestriction(userRestriction);
            }
            }
        } else if (restrictionSource == UserManager.RESTRICTION_SOURCE_DEVICE_OWNER) {
        } else if (restrictionSource == UserManager.RESTRICTION_SOURCE_DEVICE_OWNER) {
            // When the restriction is enforced by device owner, return the device owner admin only
            // When the restriction is enforced by device owner, return the device owner admin only
            // if the admin is for the {@param userId} otherwise return a default EnforcedAdmin.
            // if the admin is for the {@param userId} otherwise return a default EnforcedAdmin.
            return adminUserId == userId
            return adminUserId == userId
                    ? getDeviceOwner(context) : EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
                    ? getDeviceOwner(context, userRestriction)
                    : EnforcedAdmin.createDefaultEnforcedAdminWithRestriction(userRestriction);
        }
        }


        // If the restriction is enforced by system then return null.
        // If the restriction is enforced by system then return null.
@@ -406,7 +407,6 @@ public class RestrictedLockUtils {
     * or {@code null} if no quality requirements are set. If the requirements are set by
     * or {@code null} if no quality requirements are set. If the requirements are set by
     * multiple device admins, then the admin component will be set to {@code null} and userId to
     * multiple device admins, then the admin component will be set to {@code null} and userId to
     * {@link UserHandle#USER_NULL}.
     * {@link UserHandle#USER_NULL}.
     *
     */
     */
    public static EnforcedAdmin checkIfPasswordQualityIsSet(Context context, int userId) {
    public static EnforcedAdmin checkIfPasswordQualityIsSet(Context context, int userId) {
        final LockSettingCheck check =
        final LockSettingCheck check =
@@ -518,6 +518,11 @@ public class RestrictedLockUtils {
    }
    }


    public static EnforcedAdmin getProfileOrDeviceOwner(Context context, int userId) {
    public static EnforcedAdmin getProfileOrDeviceOwner(Context context, int userId) {
        return getProfileOrDeviceOwner(context, null, userId);
    }

    public static EnforcedAdmin getProfileOrDeviceOwner(
            Context context, String enforcedRestriction, int userId) {
        if (userId == UserHandle.USER_NULL) {
        if (userId == UserHandle.USER_NULL) {
            return null;
            return null;
        }
        }
@@ -528,18 +533,22 @@ public class RestrictedLockUtils {
        }
        }
        ComponentName adminComponent = dpm.getProfileOwnerAsUser(userId);
        ComponentName adminComponent = dpm.getProfileOwnerAsUser(userId);
        if (adminComponent != null) {
        if (adminComponent != null) {
            return new EnforcedAdmin(adminComponent, userId);
            return new EnforcedAdmin(adminComponent, enforcedRestriction, userId);
        }
        }
        if (dpm.getDeviceOwnerUserId() == userId) {
        if (dpm.getDeviceOwnerUserId() == userId) {
            adminComponent = dpm.getDeviceOwnerComponentOnAnyUser();
            adminComponent = dpm.getDeviceOwnerComponentOnAnyUser();
            if (adminComponent != null) {
            if (adminComponent != null) {
                return new EnforcedAdmin(adminComponent, userId);
                return new EnforcedAdmin(adminComponent, enforcedRestriction, userId);
            }
            }
        }
        }
        return null;
        return null;
    }
    }


    public static EnforcedAdmin getDeviceOwner(Context context) {
    public static EnforcedAdmin getDeviceOwner(Context context) {
        return getDeviceOwner(context, null);
    }

    private static EnforcedAdmin getDeviceOwner(Context context, String enforcedRestriction) {
        final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
        final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
                Context.DEVICE_POLICY_SERVICE);
                Context.DEVICE_POLICY_SERVICE);
        if (dpm == null) {
        if (dpm == null) {
@@ -547,12 +556,18 @@ public class RestrictedLockUtils {
        }
        }
        ComponentName adminComponent = dpm.getDeviceOwnerComponentOnAnyUser();
        ComponentName adminComponent = dpm.getDeviceOwnerComponentOnAnyUser();
        if (adminComponent != null) {
        if (adminComponent != null) {
            return new EnforcedAdmin(adminComponent, dpm.getDeviceOwnerUserId());
            return new EnforcedAdmin(
                    adminComponent, enforcedRestriction, dpm.getDeviceOwnerUserId());
        }
        }
        return null;
        return null;
    }
    }


    private static EnforcedAdmin getProfileOwner(Context context, int userId) {
    private static EnforcedAdmin getProfileOwner(Context context, int userId) {
        return getProfileOwner(context, null, userId);
    }

    private static EnforcedAdmin getProfileOwner(
            Context context, String enforcedRestriction, int userId) {
        if (userId == UserHandle.USER_NULL) {
        if (userId == UserHandle.USER_NULL) {
            return null;
            return null;
        }
        }
@@ -563,7 +578,7 @@ public class RestrictedLockUtils {
        }
        }
        ComponentName adminComponent = dpm.getProfileOwnerAsUser(userId);
        ComponentName adminComponent = dpm.getProfileOwnerAsUser(userId);
        if (adminComponent != null) {
        if (adminComponent != null) {
            return new EnforcedAdmin(adminComponent, userId);
            return new EnforcedAdmin(adminComponent, enforcedRestriction, userId);
        }
        }
        return null;
        return null;
    }
    }
@@ -626,6 +641,7 @@ public class RestrictedLockUtils {
                && isCurrentUserOrProfile(context, admin.userId)) {
                && isCurrentUserOrProfile(context, admin.userId)) {
            targetUserId = admin.userId;
            targetUserId = admin.userId;
        }
        }
        intent.putExtra(DevicePolicyManager.EXTRA_RESTRICTION, admin.enforcedRestriction);
        context.startActivityAsUser(intent, new UserHandle(targetUserId));
        context.startActivityAsUser(intent, new UserHandle(targetUserId));
    }
    }


@@ -700,53 +716,71 @@ public class RestrictedLockUtils {
    }
    }


    public static class EnforcedAdmin {
    public static class EnforcedAdmin {
        @Nullable
        public ComponentName component = null;
        public ComponentName component = null;
        /**
         * The restriction enforced by admin. It could be any user restriction or policy like
         * {@link DevicePolicyManager#POLICY_DISABLE_CAMERA}.
         */
        @Nullable
        public String enforcedRestriction = null;
        public int userId = UserHandle.USER_NULL;
        public int userId = UserHandle.USER_NULL;


        // We use this to represent the case where a policy is enforced by multiple admins.
        // We use this to represent the case where a policy is enforced by multiple admins.
        public final static EnforcedAdmin MULTIPLE_ENFORCED_ADMIN = new EnforcedAdmin();
        public final static EnforcedAdmin MULTIPLE_ENFORCED_ADMIN = new EnforcedAdmin();


        public static EnforcedAdmin createDefaultEnforcedAdminWithRestriction(
                String enforcedRestriction) {
            EnforcedAdmin enforcedAdmin = new EnforcedAdmin();
            enforcedAdmin.enforcedRestriction = enforcedRestriction;
            return enforcedAdmin;
        }

        public EnforcedAdmin(ComponentName component, int userId) {
        public EnforcedAdmin(ComponentName component, int userId) {
            this.component = component;
            this.component = component;
            this.userId = userId;
            this.userId = userId;
        }
        }


        public EnforcedAdmin(ComponentName component, String enforcedRestriction, int userId) {
            this.component = component;
            this.enforcedRestriction = enforcedRestriction;
            this.userId = userId;
        }

        public EnforcedAdmin(EnforcedAdmin other) {
        public EnforcedAdmin(EnforcedAdmin other) {
            if (other == null) {
            if (other == null) {
                throw new IllegalArgumentException();
                throw new IllegalArgumentException();
            }
            }
            this.component = other.component;
            this.component = other.component;
            this.enforcedRestriction = other.enforcedRestriction;
            this.userId = other.userId;
            this.userId = other.userId;
        }
        }


        public EnforcedAdmin() {}
        public EnforcedAdmin() {
        }


        @Override
        @Override
        public boolean equals(Object object) {
        public boolean equals(Object o) {
            if (object == this) return true;
            if (this == o) return true;
            if (!(object instanceof EnforcedAdmin)) return false;
            if (o == null || getClass() != o.getClass()) return false;
            EnforcedAdmin other = (EnforcedAdmin) object;
            EnforcedAdmin that = (EnforcedAdmin) o;
            if (userId != other.userId) {
            return userId == that.userId &&
                return false;
                    Objects.equals(component, that.component) &&
            }
                    Objects.equals(enforcedRestriction, that.enforcedRestriction);
            if ((component == null && other.component == null) ||
                    (component != null && component.equals(other.component))) {
                return true;
            }
            return false;
        }
        }


        @Override
        @Override
        public String toString() {
        public int hashCode() {
            return "EnforcedAdmin{component=" + component + ",userId=" + userId + "}";
            return Objects.hash(component, enforcedRestriction, userId);
        }
        }


        public void copyTo(EnforcedAdmin other) {
        @Override
            if (other == null) {
        public String toString() {
                throw new IllegalArgumentException();
            return "EnforcedAdmin{" +
            }
                    "component=" + component +
            other.component = component;
                    ", enforcedRestriction='" + enforcedRestriction +
            other.userId = userId;
                    ", userId=" + userId +
                    '}';
        }
        }
    }
    }


+46 −0
Original line number Original line Diff line number Diff line
@@ -32,6 +32,7 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.UserManager;


import org.junit.Before;
import org.junit.Before;
@@ -42,6 +43,7 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoAnnotations;


import java.util.Arrays;
import java.util.Arrays;
import java.util.Collections;


@RunWith(SettingsLibRobolectricTestRunner.class)
@RunWith(SettingsLibRobolectricTestRunner.class)
public class RestrictedLockUtilsTest {
public class RestrictedLockUtilsTest {
@@ -76,6 +78,42 @@ public class RestrictedLockUtilsTest {
        RestrictedLockUtils.sProxy = mProxy;
        RestrictedLockUtils.sProxy = mProxy;
    }
    }


    @Test
    public void checkIfRestrictionEnforced_deviceOwner() {
        UserManager.EnforcingUser enforcingUser = new UserManager.EnforcingUser(mUserId,
                UserManager.RESTRICTION_SOURCE_DEVICE_OWNER);
        final String userRestriction = UserManager.DISALLOW_UNINSTALL_APPS;
        when(mUserManager.getUserRestrictionSources(userRestriction,
                UserHandle.of(mUserId))).
                thenReturn(Collections.singletonList(enforcingUser));
        setUpDeviceOwner(mAdmin1);

        EnforcedAdmin enforcedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(mContext,
                userRestriction, mUserId);

        assertThat(enforcedAdmin).isNotNull();
        assertThat(enforcedAdmin.enforcedRestriction).isEqualTo(userRestriction);
        assertThat(enforcedAdmin.component).isEqualTo(mAdmin1);
    }

    @Test
    public void checkIfRestrictionEnforced_profileOwner() {
        UserManager.EnforcingUser enforcingUser = new UserManager.EnforcingUser(mUserId,
                UserManager.RESTRICTION_SOURCE_PROFILE_OWNER);
        final String userRestriction = UserManager.DISALLOW_UNINSTALL_APPS;
        when(mUserManager.getUserRestrictionSources(userRestriction,
                UserHandle.of(mUserId))).
                thenReturn(Collections.singletonList(enforcingUser));
        setUpProfileOwner(mAdmin1, mUserId);

        EnforcedAdmin enforcedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(mContext,
                userRestriction, mUserId);

        assertThat(enforcedAdmin).isNotNull();
        assertThat(enforcedAdmin.enforcedRestriction).isEqualTo(userRestriction);
        assertThat(enforcedAdmin.component).isEqualTo(mAdmin1);
    }

    @Test
    @Test
    public void checkIfDevicePolicyServiceDisabled_noEnforceAdminForManagedProfile() {
    public void checkIfDevicePolicyServiceDisabled_noEnforceAdminForManagedProfile() {
        when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(null);
        when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(null);
@@ -263,4 +301,12 @@ public class RestrictedLockUtilsTest {
        when(mDevicePolicyManager.getActiveAdminsAsUser(userId))
        when(mDevicePolicyManager.getActiveAdminsAsUser(userId))
                .thenReturn(Arrays.asList(activeAdmins));
                .thenReturn(Arrays.asList(activeAdmins));
    }
    }

    private void setUpDeviceOwner(ComponentName admin) {
        when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(admin);
    }

    private void setUpProfileOwner(ComponentName admin, int userId) {
        when(mDevicePolicyManager.getProfileOwnerAsUser(userId)).thenReturn(admin);
    }
}
}