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

Commit f74a11c7 authored by Miriam Polzer's avatar Miriam Polzer
Browse files

Return null instead of crashing when user does not exists

The checkIfKeyguardFeaturesDisabled function was crashing with a
nullptr exception when called on a userId that does not exists.
Return null instead. No user means nothing can be disabled.

Fix: 386971405
Test: atest \
SettingsLibRoboTests:com.android.settingslib.RestrictedLockUtilsTest
Flag: EXEMPT BUG_FIX
Change-Id: Ibc04b2faf8c0480d1f1b6bf4c7ee578b5b126d1a
parent e18535d5
Loading
Loading
Loading
Loading
+17 −8
Original line number Diff line number Diff line
@@ -244,14 +244,23 @@ public class RestrictedLockUtilsInternal extends RestrictedLockUtils {
     */
    public static EnforcedAdmin checkIfKeyguardFeaturesDisabled(Context context,
            int keyguardFeatures, final @UserIdInt int userId) {
        final LockSettingCheck check = (dpm, admin, checkUser) -> {
        UserInfo userInfo = UserManager.get(context).getUserInfo(userId);
        if (userInfo == null) {
            Log.w(LOG_TAG, "User " + userId + " does not exist");
            return null;
        }

        final LockSettingCheck check =
                (dpm, admin, checkUser) -> {
                    int effectiveFeatures = dpm.getKeyguardDisabledFeatures(admin, checkUser);
                    if (checkUser != userId) {
                        // This case is reached when {@code checkUser} is a managed profile and
                        // {@code userId} is the parent user.
                        effectiveFeatures &= PROFILE_KEYGUARD_FEATURES_AFFECT_OWNER;
                    }
                    return (effectiveFeatures & keyguardFeatures) != KEYGUARD_DISABLE_FEATURES_NONE;
                };
        if (UserManager.get(context).getUserInfo(userId).isManagedProfile()) {
        if (userInfo.isManagedProfile()) {
            DevicePolicyManager dpm =
                    (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
            return findEnforcedAdmin(dpm.getActiveAdminsAsUser(userId), dpm, userId, check);
+10 −0
Original line number Diff line number Diff line
@@ -458,6 +458,16 @@ public class RestrictedLockUtilsTest {
        assertThat(intentCaptor.getValue().getExtra(EXTRA_RESTRICTION)).isNull();
    }

    /** See b/386971405. Ensure that the code does not crash when the user is not found. */
    @Test
    public void checkIfKeyguardFeaturesDisabled_returnsNull_whenUserDoesNotExist() {
        when(mUserManager.getUserInfo(mUserId)).thenReturn(null);
        assertThat(
                        RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled(
                                mContext, KEYGUARD_DISABLE_FINGERPRINT, mUserId))
                .isNull();
    }

    private UserInfo setUpUser(int userId, ComponentName[] admins) {
        UserInfo userInfo = new UserInfo(userId, "primary", 0);
        when(mUserManager.getUserInfo(userId)).thenReturn(userInfo);