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

Commit d6d8ee0a authored by Vinicius Juliao's avatar Vinicius Juliao
Browse files

Add UserManager restriction to the intent data that is sent after closing Parent Needed dialog

Test: com.android.settingslib.RestrictedLockUtilsTest,com.android.settingslib.RestrictedPreferenceHelperTest
Bug: 291287687
Fixes: 291287687
Change-Id: I35d9856c5bd5d53438f9e661260c28b8b7f144f3
parent 5321c545
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -125,7 +125,8 @@ public class RestrictedLockUtilsInternal extends RestrictedLockUtils {
            return null;
        }

        final EnforcedAdmin admin = getProfileOrDeviceOwner(context, enforcingUser.getUserHandle());
        final EnforcedAdmin admin =
                getProfileOrDeviceOwner(context, userRestriction, enforcingUser.getUserHandle());
        if (admin != null) {
            return admin;
        }
+11 −4
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.settingslib;

import static android.app.admin.DevicePolicyResources.Strings.Settings.CONTROLLED_BY_ADMIN_SUMMARY;

import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

import android.app.admin.DevicePolicyManager;
@@ -32,6 +31,7 @@ import android.util.TypedValue;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;

@@ -48,7 +48,8 @@ public class RestrictedPreferenceHelper {
    int uid;

    private boolean mDisabledByAdmin;
    private EnforcedAdmin mEnforcedAdmin;
    @VisibleForTesting
    EnforcedAdmin mEnforcedAdmin;
    private String mAttrUserRestriction = null;
    private boolean mDisabledSummary = false;

@@ -193,8 +194,14 @@ public class RestrictedPreferenceHelper {
     * @return true if the disabled state was changed.
     */
    public boolean setDisabledByAdmin(EnforcedAdmin admin) {
        final boolean disabled = (admin != null ? true : false);
        mEnforcedAdmin = admin;
        boolean disabled = false;
        mEnforcedAdmin = null;
        if (admin != null) {
            disabled = true;
            // Copy the received instance to prevent pass be reference being overwritten.
            mEnforcedAdmin = new EnforcedAdmin(admin);
        }

        boolean changed = false;
        if (mDisabledByAdmin != disabled) {
            mDisabledByAdmin = disabled;
+22 −10
Original line number Diff line number Diff line
@@ -21,11 +21,8 @@ import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_FEATURES_NO
import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT;
import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_REMOTE_INPUT;
import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS;

import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
@@ -78,6 +75,8 @@ public class RestrictedLockUtilsTest {

        when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
                .thenReturn(mDevicePolicyManager);
        when(mContext.getSystemService(DevicePolicyManager.class))
                .thenReturn(mDevicePolicyManager);
        when(mContext.getSystemService(Context.USER_SERVICE))
                .thenReturn(mUserManager);
        when(mContext.getPackageManager())
@@ -87,14 +86,20 @@ public class RestrictedLockUtilsTest {
    }

    @Test
    public void checkIfRestrictionEnforced_deviceOwner() {
    public void checkIfRestrictionEnforced_deviceOwner()
            throws PackageManager.NameNotFoundException {
        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);

        when(mContext.createPackageContextAsUser(any(), eq(0),
                eq(UserHandle.of(mUserId))))
                .thenReturn(mContext);

        setUpDeviceOwner(mAdmin1, mUserId);

        EnforcedAdmin enforcedAdmin = RestrictedLockUtilsInternal
                .checkIfRestrictionEnforced(mContext, userRestriction, mUserId);
@@ -105,14 +110,20 @@ public class RestrictedLockUtilsTest {
    }

    @Test
    public void checkIfRestrictionEnforced_profileOwner() {
    public void checkIfRestrictionEnforced_profileOwner()
            throws PackageManager.NameNotFoundException {
        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);

        when(mContext.createPackageContextAsUser(any(), eq(0),
                eq(UserHandle.of(mUserId))))
                .thenReturn(mContext);

        setUpProfileOwner(mAdmin1);

        EnforcedAdmin enforcedAdmin = RestrictedLockUtilsInternal
                .checkIfRestrictionEnforced(mContext, userRestriction, mUserId);
@@ -326,11 +337,12 @@ public class RestrictedLockUtilsTest {
                .thenReturn(Arrays.asList(activeAdmins));
    }

    private void setUpDeviceOwner(ComponentName admin) {
    private void setUpDeviceOwner(ComponentName admin, int userId) {
        when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(admin);
        when(mDevicePolicyManager.getDeviceOwnerUser()).thenReturn(UserHandle.of(userId));
    }

    private void setUpProfileOwner(ComponentName admin, int userId) {
        when(mDevicePolicyManager.getProfileOwnerAsUser(userId)).thenReturn(admin);
    private void setUpProfileOwner(ComponentName admin) {
        when(mDevicePolicyManager.getProfileOwner()).thenReturn(admin);
    }
}
+23 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settingslib;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doReturn;
@@ -119,4 +120,26 @@ public class RestrictedPreferenceHelperTest {

        verify(mRestrictedTopLevelPreference, never()).setEnabled(false);
    }

    /**
     * Tests if the instance of {@link RestrictedLockUtils.EnforcedAdmin} is received by
     * {@link RestrictedPreferenceHelper#setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin)} as a
     * copy or as a reference.
     */
    @Test
    public void setDisabledByAdmin_disablePreference_receivedEnforcedAdminIsNotAReference() {
        RestrictedLockUtils.EnforcedAdmin enforcedAdmin =
                new RestrictedLockUtils.EnforcedAdmin(/* component */ null,
                        /* enforcedRestriction */ "some_restriction",
                        /* userHandle */ null);

        mHelper.setDisabledByAdmin(enforcedAdmin);

        // If `setDisabledByAdmin` stored `enforcedAdmin` as a reference, then the following
        // assignment would be propagated.
        enforcedAdmin.enforcedRestriction = null;
        assertThat(mHelper.mEnforcedAdmin.enforcedRestriction).isEqualTo("some_restriction");

        assertThat(mHelper.isDisabledByAdmin()).isTrue();
    }
}