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

Commit 965ae263 authored by Alex Stetson's avatar Alex Stetson Committed by Android (Google) Code Review
Browse files

Merge "Get keyguard state from UserManager lock state for bg users" into main

parents bbc48c34 eb05c18c
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -89,6 +89,7 @@ import com.android.internal.widget.LockSettingsInternal;
import com.android.internal.widget.LockSettingsStateListener;
import com.android.server.LocalServices;
import com.android.server.SystemService;
import com.android.server.pm.UserManagerInternal;
import com.android.server.servicewatcher.CurrentUserServiceSupplier;
import com.android.server.servicewatcher.ServiceWatcher;
import com.android.server.utils.Slogf;
@@ -170,6 +171,7 @@ public class TrustManagerService extends SystemService {
    private final ActivityManager mActivityManager;
    private FingerprintManager mFingerprintManager;
    private FaceManager mFaceManager;
    private UserManagerInternal mUserManagerInternal;

    private enum TrustState {
        // UNTRUSTED means that TrustManagerService is currently *not* giving permission for the
@@ -1064,6 +1066,8 @@ public class TrustManagerService extends SystemService {
                    Log.w(TAG, "Unable to check keyguard lock state", e);
                }
                currentUserIsUnlocked = unlockedUser == id;
            } else if (isVisibleBackgroundUser(id)) {
                showingKeyguard = !mUserManager.isUserUnlocked(id);
            }
            final boolean deviceLocked = secure && showingKeyguard && !trusted
                    && !biometricAuthenticated;
@@ -1095,6 +1099,16 @@ public class TrustManagerService extends SystemService {
        }
    }

    private boolean isVisibleBackgroundUser(int userId) {
        if (!mUserManager.isVisibleBackgroundUsersSupported()) {
            return false;
        }
        if (mUserManagerInternal == null) {
            mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
        }
        return mUserManagerInternal.isVisibleBackgroundFullUser(userId);
    }

    private void notifyTrustAgentsOfDeviceLockState(int userId, boolean isLocked) {
        for (int i = 0; i < mActiveAgents.size(); i++) {
            AgentInfo agent = mActiveAgents.valueAt(i);
+52 −0
Original line number Diff line number Diff line
@@ -100,6 +100,7 @@ import com.android.modules.utils.testing.ExtendedMockitoRule;
import com.android.server.LocalServices;
import com.android.server.SystemService;
import com.android.server.SystemServiceManager;
import com.android.server.pm.UserManagerInternal;

import org.junit.After;
import org.junit.Before;
@@ -145,6 +146,7 @@ public class TrustManagerServiceTest {

    private static final String URI_SCHEME_PACKAGE = "package";
    private static final int TEST_USER_ID = 50;
    private static final int TEST_VISIBLE_BACKGROUND_USER_ID = 51;
    private static final UserInfo TEST_USER =
            new UserInfo(TEST_USER_ID, "user", UserInfo.FLAG_FULL);
    private static final int PARENT_USER_ID = 60;
@@ -170,6 +172,7 @@ public class TrustManagerServiceTest {
    private @Mock KeyStoreAuthorization mKeyStoreAuthorization;
    private @Mock LockPatternUtils mLockPatternUtils;
    private @Mock LockSettingsInternal mLockSettingsInternal;
    private @Mock UserManagerInternal mUserManagerInternal;
    private @Mock PackageManager mPackageManager;
    private @Mock UserManager mUserManager;
    private @Mock IWindowManager mWindowManager;
@@ -224,6 +227,7 @@ public class TrustManagerServiceTest {
        when(mUserManager.getAliveUsers()).thenReturn(List.of(TEST_USER));
        when(mUserManager.getEnabledProfileIds(TEST_USER_ID)).thenReturn(new int[0]);
        when(mUserManager.getUserInfo(TEST_USER_ID)).thenReturn(TEST_USER);
        when(mUserManager.isVisibleBackgroundUsersSupported()).thenReturn(false);

        when(mWindowManager.isKeyguardLocked()).thenReturn(true);

@@ -593,6 +597,54 @@ public class TrustManagerServiceTest {
        verify(mTrustListener, never()).onTrustManagedChanged(anyBoolean(), anyInt());
    }

    @Test
    public void testDeviceLocked_visibleBackgroundUser_userLocked() throws RemoteException {
        setupVisibleBackgroundUser(/* visible= */ true, /* unlocked= */ false);
        mService.waitForIdle();
        mTrustManager.reportEnabledTrustAgentsChanged(TEST_VISIBLE_BACKGROUND_USER_ID);
        mService.waitForIdle();
        assertThat(mService.isDeviceLockedInner(TEST_VISIBLE_BACKGROUND_USER_ID)).isTrue();
    }

    @Test
    public void testDeviceLocked_visibleBackgroundUser_userUnlocked() throws RemoteException {
        setupVisibleBackgroundUser(/* visible= */ true, /* unlocked= */ true);
        mService.waitForIdle();
        mTrustManager.reportEnabledTrustAgentsChanged(TEST_VISIBLE_BACKGROUND_USER_ID);
        mService.waitForIdle();
        assertThat(mService.isDeviceLockedInner(TEST_VISIBLE_BACKGROUND_USER_ID)).isFalse();
    }

    @Test
    public void testDeviceLocked_invisibleBackgroundUser_userUnlocked() throws RemoteException {
        setupVisibleBackgroundUser(/* visible= */ false, /* unlocked= */ true);
        mService.waitForIdle();
        mTrustManager.reportEnabledTrustAgentsChanged(TEST_VISIBLE_BACKGROUND_USER_ID);
        mService.waitForIdle();
        assertThat(mService.isDeviceLockedInner(TEST_VISIBLE_BACKGROUND_USER_ID)).isTrue();
    }

    private void setupVisibleBackgroundUser(boolean visible, boolean unlocked) {
        UserInfo info = new UserInfo(TEST_VISIBLE_BACKGROUND_USER_ID, "visible bg user",
                UserInfo.FLAG_FULL);

        when(mActivityManager.isUserRunning(TEST_VISIBLE_BACKGROUND_USER_ID)).thenReturn(true);

        when(mLockPatternUtils.isSecure(TEST_VISIBLE_BACKGROUND_USER_ID)).thenReturn(true);

        when(mUserManager.getAliveUsers()).thenReturn(List.of(TEST_USER, info));
        when(mUserManager.getEnabledProfileIds(TEST_VISIBLE_BACKGROUND_USER_ID)).thenReturn(
                new int[0]);
        when(mUserManager.getUserInfo(TEST_VISIBLE_BACKGROUND_USER_ID)).thenReturn(info);
        when(mUserManager.isUserUnlocked(TEST_VISIBLE_BACKGROUND_USER_ID)).thenReturn(unlocked);
        when(mUserManager.isVisibleBackgroundUsersSupported()).thenReturn(true);

        LocalServices.removeServiceForTest(UserManagerInternal.class);
        LocalServices.addService(UserManagerInternal.class, mUserManagerInternal);
        when(mUserManagerInternal.isVisibleBackgroundFullUser(
                TEST_VISIBLE_BACKGROUND_USER_ID)).thenReturn(visible);
    }

    private void setUpRenewableTrust(ITrustAgentService trustAgent) throws RemoteException {
        ITrustAgentServiceCallback callback = getCallback(trustAgent);
        callback.setManagingTrust(true);