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

Commit 0bf6e391 authored by Adam Bookatz's avatar Adam Bookatz Committed by Android (Google) Code Review
Browse files

Merge "Communal Profile Notifications" into main

parents 032cc572 69aea7f3
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -5079,6 +5079,32 @@ public class UserManager {
        }
    }

    /**
     * Returns list of the profiles of the given user, including userId itself, as well as the
     * communal profile, if there is one.
     *
     * <p>Note that this returns both enabled and not enabled profiles.
     * <p>Note that this includes all profile types (not including Restricted profiles).
     *
     * @hide
     */
    @FlaggedApi(android.multiuser.Flags.FLAG_SUPPORT_COMMUNAL_PROFILE)
    @RequiresPermission(anyOf = {
            Manifest.permission.MANAGE_USERS,
            Manifest.permission.CREATE_USERS,
            Manifest.permission.QUERY_USERS})
    public List<UserInfo> getProfilesIncludingCommunal(@UserIdInt int userId) {
        final List<UserInfo> profiles = getProfiles(userId);
        final UserHandle communalProfile = getCommunalProfile();
        if (communalProfile != null) {
            final UserInfo communalInfo = getUserInfo(communalProfile.getIdentifier());
            if (communalInfo != null) {
                profiles.add(communalInfo);
            }
        }
        return profiles;
    }

    /**
     * Checks if the 2 provided user handles belong to the same profile group.
     *
+6 −1
Original line number Diff line number Diff line
@@ -250,6 +250,7 @@ public class NotificationLockscreenUserManagerImpl implements
    private final Handler mMainHandler;
    private final Handler mBackgroundHandler;
    private final Executor mBackgroundExecutor;
    /** The current user and its profiles (possibly including a communal profile). */
    protected final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<>();
    protected final SparseArray<UserInfo> mCurrentManagedProfiles = new SparseArray<>();

@@ -689,12 +690,16 @@ public class NotificationLockscreenUserManagerImpl implements
        }
    }

    @SuppressLint("MissingPermission")
    private void updateCurrentProfilesCache() {
        synchronized (mLock) {
            mCurrentProfiles.clear();
            mCurrentManagedProfiles.clear();
            if (mUserManager != null) {
                for (UserInfo user : mUserManager.getProfiles(mCurrentUserId)) {
                List<UserInfo> profiles = android.multiuser.Flags.supportCommunalProfile()
                        ? mUserManager.getProfilesIncludingCommunal(mCurrentUserId)
                        : mUserManager.getProfiles(mCurrentUserId);
                for (UserInfo user : profiles) {
                    mCurrentProfiles.put(user.id, user);
                    if (UserManager.USER_TYPE_PROFILE_MANAGED.equals(user.userType)) {
                        mCurrentManagedProfiles.put(user.id, user);
+28 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ import android.os.UserManager;
import android.provider.Settings;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.util.SparseArray;

import androidx.test.filters.SmallTest;

@@ -120,6 +121,7 @@ public class NotificationLockscreenUserManagerMainThreadTest extends SysuiTestCa
    private UserInfo mCurrentUser;
    private UserInfo mSecondaryUser;
    private UserInfo mWorkUser;
    private UserInfo mCommunalUser;
    private FakeSettings mSettings;
    private TestNotificationLockscreenUserManager mLockscreenUserManager;
    private NotificationEntry mCurrentUserNotif;
@@ -142,12 +144,18 @@ public class NotificationLockscreenUserManagerMainThreadTest extends SysuiTestCa
        mSecondaryUser = new UserInfo(currentUserId + 1, "", 0);
        mWorkUser = new UserInfo(currentUserId + 2, "" /* name */, null /* iconPath */, 0,
                UserManager.USER_TYPE_PROFILE_MANAGED);
        mCommunalUser = new UserInfo(currentUserId + 3, "" /* name */, null /* iconPath */, 0,
                UserManager.USER_TYPE_PROFILE_COMMUNAL);

        when(mKeyguardManager.getPrivateNotificationsAllowed()).thenReturn(true);
        when(mUserManager.getProfiles(currentUserId)).thenReturn(Lists.newArrayList(
                mCurrentUser, mWorkUser));
        when(mUserManager.getProfilesIncludingCommunal(currentUserId)).thenReturn(
                Lists.newArrayList(mCurrentUser, mWorkUser, mCommunalUser));
        when(mUserManager.getProfiles(mSecondaryUser.id)).thenReturn(Lists.newArrayList(
                mSecondaryUser));
        when(mUserManager.getProfilesIncludingCommunal(mSecondaryUser.id)).thenReturn(
                Lists.newArrayList(mSecondaryUser, mCommunalUser));
        mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
                Handler.createAsync(Looper.myLooper()));

@@ -177,6 +185,26 @@ public class NotificationLockscreenUserManagerMainThreadTest extends SysuiTestCa
            lockScreenUris, 0);
    }

    @Test
    public void testGetCurrentProfiles() {
        final SparseArray<UserInfo> expectedCurProfiles = new SparseArray<>();
        expectedCurProfiles.put(mCurrentUser.id, mCurrentUser);
        expectedCurProfiles.put(mWorkUser.id, mWorkUser);
        if (android.multiuser.Flags.supportCommunalProfile()) {
            expectedCurProfiles.put(mCommunalUser.id, mCommunalUser);
        }
        assertTrue(mLockscreenUserManager.getCurrentProfiles().contentEquals(expectedCurProfiles));

        mLockscreenUserManager.mUserChangedCallback.onUserChanging(mSecondaryUser.id, mContext);

        final SparseArray<UserInfo> expectedSecProfiles = new SparseArray<>();
        expectedSecProfiles.put(mSecondaryUser.id, mSecondaryUser);
        if (android.multiuser.Flags.supportCommunalProfile()) {
            expectedSecProfiles.put(mCommunalUser.id, mCommunalUser);
        }
        assertTrue(mLockscreenUserManager.getCurrentProfiles().contentEquals(expectedSecProfiles));
    }

    @Test
    public void testLockScreenShowNotificationsFalse() {
        mSettings.putInt(LOCK_SCREEN_SHOW_NOTIFICATIONS, 0);
+37 −5
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ import android.os.UserManager;
import android.provider.Settings;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.util.SparseArray;

import androidx.test.filters.SmallTest;

@@ -136,6 +137,7 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
    private UserInfo mCurrentUser;
    private UserInfo mSecondaryUser;
    private UserInfo mWorkUser;
    private UserInfo mCommunalUser;
    private FakeSettings mSettings;
    private TestNotificationLockscreenUserManager mLockscreenUserManager;
    private NotificationEntry mCurrentUserNotif;
@@ -158,14 +160,20 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
        mSecondaryUser = new UserInfo(currentUserId + 1, "", 0);
        mWorkUser = new UserInfo(currentUserId + 2, "" /* name */, null /* iconPath */, 0,
                UserManager.USER_TYPE_PROFILE_MANAGED);
        mCommunalUser = new UserInfo(currentUserId + 3, "" /* name */, null /* iconPath */, 0,
                UserManager.USER_TYPE_PROFILE_COMMUNAL);

        when(mKeyguardManager.getPrivateNotificationsAllowed()).thenReturn(true);
        when(mUserManager.getProfiles(currentUserId)).thenReturn(Lists.newArrayList(
                mCurrentUser, mWorkUser));
        when(mUserManager.getProfilesIncludingCommunal(currentUserId)).thenReturn(
                Lists.newArrayList(mCurrentUser, mWorkUser, mCommunalUser));
        when(mUserManager.getUsers()).thenReturn(Lists.newArrayList(
                mCurrentUser, mWorkUser, mSecondaryUser));
                mCurrentUser, mWorkUser, mSecondaryUser, mCommunalUser));
        when(mUserManager.getProfiles(mSecondaryUser.id)).thenReturn(Lists.newArrayList(
                mSecondaryUser));
        when(mUserManager.getProfilesIncludingCommunal(mSecondaryUser.id)).thenReturn(
                Lists.newArrayList(mSecondaryUser, mCommunalUser));
        mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
                Handler.createAsync(Looper.myLooper()));

@@ -210,6 +218,26 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
            lockScreenUris, 0);
    }

    @Test
    public void testGetCurrentProfiles() {
        final SparseArray<UserInfo> expectedCurProfiles = new SparseArray<>();
        expectedCurProfiles.put(mCurrentUser.id, mCurrentUser);
        expectedCurProfiles.put(mWorkUser.id, mWorkUser);
        if (android.multiuser.Flags.supportCommunalProfile()) {
            expectedCurProfiles.put(mCommunalUser.id, mCommunalUser);
        }
        assertTrue(mLockscreenUserManager.getCurrentProfiles().contentEquals(expectedCurProfiles));

        mLockscreenUserManager.mUserChangedCallback.onUserChanging(mSecondaryUser.id, mContext);

        final SparseArray<UserInfo> expectedSecProfiles = new SparseArray<>();
        expectedSecProfiles.put(mSecondaryUser.id, mSecondaryUser);
        if (android.multiuser.Flags.supportCommunalProfile()) {
            expectedSecProfiles.put(mCommunalUser.id, mCommunalUser);
        }
        assertTrue(mLockscreenUserManager.getCurrentProfiles().contentEquals(expectedSecProfiles));
    }

    @Test
    public void testLockScreenShowNotificationsFalse() {
        mSettings.putInt(LOCK_SCREEN_SHOW_NOTIFICATIONS, 0);
@@ -713,7 +741,8 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
        assertEquals(0, mLockscreenUserManager.mCurrentProfiles.size());
        mLockscreenUserManager.mCurrentProfiles.append(0, mock(UserInfo.class));
        simulateProfileAvailabilityActions(Intent.ACTION_PROFILE_AVAILABLE);
        assertEquals(2, mLockscreenUserManager.mCurrentProfiles.size());
        int numProfiles = android.multiuser.Flags.supportCommunalProfile() ? 3 : 2;
        assertEquals(numProfiles, mLockscreenUserManager.mCurrentProfiles.size());
    }

    @Test
@@ -723,7 +752,8 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
        assertEquals(0, mLockscreenUserManager.mCurrentProfiles.size());
        mLockscreenUserManager.mCurrentProfiles.append(0, mock(UserInfo.class));
        simulateProfileAvailabilityActions(Intent.ACTION_PROFILE_UNAVAILABLE);
        assertEquals(2, mLockscreenUserManager.mCurrentProfiles.size());
        int numProfiles = android.multiuser.Flags.supportCommunalProfile() ? 3 : 2;
        assertEquals(numProfiles, mLockscreenUserManager.mCurrentProfiles.size());
    }

    @Test
@@ -735,7 +765,8 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
        assertEquals(0, mLockscreenUserManager.mCurrentManagedProfiles.size());
        mLockscreenUserManager.mCurrentProfiles.append(0, mock(UserInfo.class));
        simulateProfileAvailabilityActions(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
        assertEquals(2, mLockscreenUserManager.mCurrentProfiles.size());
        int numProfiles = android.multiuser.Flags.supportCommunalProfile() ? 3 : 2;
        assertEquals(numProfiles, mLockscreenUserManager.mCurrentProfiles.size());
        assertEquals(1, mLockscreenUserManager.mCurrentManagedProfiles.size());
    }

@@ -748,7 +779,8 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
        assertEquals(0, mLockscreenUserManager.mCurrentManagedProfiles.size());
        mLockscreenUserManager.mCurrentProfiles.append(0, mock(UserInfo.class));
        simulateProfileAvailabilityActions(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
        assertEquals(2, mLockscreenUserManager.mCurrentProfiles.size());
        int numProfiles = android.multiuser.Flags.supportCommunalProfile() ? 3 : 2;
        assertEquals(numProfiles, mLockscreenUserManager.mCurrentProfiles.size());
        assertEquals(1, mLockscreenUserManager.mCurrentManagedProfiles.size());
    }

+30 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertWithMessage;

import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;

import android.annotation.UserIdInt;
@@ -34,6 +35,7 @@ import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.platform.test.annotations.Postsubmit;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.provider.Settings;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;
@@ -266,6 +268,34 @@ public final class UserManagerTest {
        assertWithMessage("Communal profile not visible").that(umCommunal.isUserVisible()).isTrue();
    }

    @Test
    @RequiresFlagsEnabled(android.multiuser.Flags.FLAG_SUPPORT_COMMUNAL_PROFILE)
    public void testGetProfilesIncludingCommunal() throws Exception {
        int mainUserId = mUserManager.getMainUser().getIdentifier();
        final UserInfo otherUser = createUser("TestUser", /* flags= */ 0);
        final UserInfo profile = createProfileForUser("Profile",
                UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId);

        final UserHandle communalProfile = mUserManager.getCommunalProfile();

        final List<UserInfo> mainsActual = mUserManager.getProfilesIncludingCommunal(mainUserId);
        final List<UserInfo> othersActual = mUserManager.getProfilesIncludingCommunal(otherUser.id);

        final List<Integer> mainsExpected = new ArrayList<>();
        mainsExpected.add(mainUserId);
        if (profile != null) mainsExpected.add(profile.id);
        if (communalProfile != null) mainsExpected.add(communalProfile.getIdentifier());
        assertEquals(mainsExpected.stream().sorted().toList(),
                mainsActual.stream().map(ui -> ui.id).sorted().toList());


        final List<Integer> othersExpected = new ArrayList<>();
        othersExpected.add(otherUser.id);
        if (communalProfile != null) othersExpected.add(communalProfile.getIdentifier());
        assertEquals(othersExpected.stream().sorted().toList(),
                othersActual.stream().map(ui -> ui.id).sorted().toList());
    }

    @Test
    public void testPrivateProfile() throws Exception {
        UserHandle mainUser = mUserManager.getMainUser();