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

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

Merge changes I97dc5419,I19a2a14b into main

* changes:
  Schedule bg stopping: full instead of !profile
  Schedule bg user stopping: flag off configuration
parents 91380375 77af797d
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -287,7 +287,14 @@ flag {
flag {
    name: "schedule_stop_of_background_user"
    namespace: "multiuser"
    description: "Schedule background users to be stopped at a future point."
    description: "Schedule background users to be stopped at a future point. Infrastructure only."
    bug: "330351042"
}

flag {
    name: "schedule_stop_of_background_user_by_default"
    namespace: "multiuser"
    description: "Activates the background-stopping schedule of inactive users based on the time specified in the configuration."
    bug: "330351042"
}

+5 −2
Original line number Diff line number Diff line
@@ -477,7 +477,10 @@ class UserController implements Handler.Callback {
            mUserSwitchUiEnabled = userSwitchUiEnabled;
            mMaxRunningUsers = maxRunningUsers;
            mDelayUserDataLocking = delayUserDataLocking;
            if (android.multiuser.Flags.scheduleStopOfBackgroundUserByDefault()) {
                // If flag is off, default value of -1 disables scheduling (but not infrastructure).
                mBackgroundUserScheduledStopTimeSecs = backgroundUserScheduledStopTimeSecs;
            }
            mInitialized = true;
        }
    }
@@ -2161,7 +2164,7 @@ class UserController implements Handler.Callback {
            // of mUserLru, so we need to ensure that the foreground user isn't displaced.
            addUserToUserLru(mCurrentUserId);
        }
        if (userStartMode == USER_START_MODE_BACKGROUND && !userInfo.isProfile()) {
        if (userStartMode == USER_START_MODE_BACKGROUND && userInfo.isFull()) {
            scheduleStopOfBackgroundUser(userId);
        }
        t.traceEnd();
+28 −17
Original line number Diff line number Diff line
@@ -19,8 +19,8 @@ package com.android.server.am;
import static android.Manifest.permission.INTERACT_ACROSS_PROFILES;
import static android.Manifest.permission.INTERACT_ACROSS_USERS;
import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
import static android.app.ActivityManager.STOP_USER_ON_SWITCH_TRUE;
import static android.app.ActivityManager.STOP_USER_ON_SWITCH_FALSE;
import static android.app.ActivityManager.STOP_USER_ON_SWITCH_TRUE;
import static android.app.ActivityManagerInternal.ALLOW_FULL_ONLY;
import static android.app.ActivityManagerInternal.ALLOW_NON_FULL;
import static android.app.ActivityManagerInternal.ALLOW_NON_FULL_IN_PROFILE;
@@ -57,10 +57,10 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doCallRealMethod;
@@ -156,7 +156,7 @@ public class UserControllerTest {
    private static final int NONEXIST_USER_ID = 2;
    private static final int TEST_PRE_CREATED_USER_ID = 103;

    private static final int NO_USERINFO_FLAGS = 0;
    private static final int DEFAULT_USER_FLAGS = UserInfo.FLAG_FULL;

    private static final String TAG = UserControllerTest.class.getSimpleName();

@@ -225,8 +225,8 @@ public class UserControllerTest {

            mUserController = new UserController(mInjector);
            mUserController.setAllowUserUnlocking(true);
            setUpUser(TEST_USER_ID, NO_USERINFO_FLAGS);
            setUpUser(TEST_PRE_CREATED_USER_ID, NO_USERINFO_FLAGS, /* preCreated= */ true, null);
            setUpUser(TEST_USER_ID, DEFAULT_USER_FLAGS);
            setUpUser(TEST_PRE_CREATED_USER_ID, DEFAULT_USER_FLAGS, /* preCreated= */ true, null);
            mInjector.mRelevantUser = null;
        });
    }
@@ -643,14 +643,16 @@ public class UserControllerTest {
    /** Test scheduling stopping of background users after a user-switch. */
    @Test
    public void testScheduleStopOfBackgroundUser_switch() {
        mSetFlagsRule.enableFlags(android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER);
        mSetFlagsRule.enableFlags(
                android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER,
                android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER_BY_DEFAULT);
        assumeFalse(UserManager.isVisibleBackgroundUsersEnabled());

        mUserController.setInitialConfig(/* userSwitchUiEnabled= */ true,
                /* maxRunningUsers= */ 10, /* delayUserDataLocking= */ false,
                /* backgroundUserScheduledStopTimeSecs= */ 2);

        setUpUser(TEST_USER_ID1, NO_USERINFO_FLAGS);
        setUpUser(TEST_USER_ID1, DEFAULT_USER_FLAGS);

        // Switch to TEST_USER_ID from user 0
        int numberOfUserSwitches = 0;
@@ -693,7 +695,9 @@ public class UserControllerTest {
    /** Test scheduling stopping of background users that were started in the background. */
    @Test
    public void testScheduleStopOfBackgroundUser_startInBackground() throws Exception {
        mSetFlagsRule.enableFlags(android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER);
        mSetFlagsRule.enableFlags(
                android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER,
                android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER_BY_DEFAULT);
        assumeFalse(UserManager.isVisibleBackgroundUsersEnabled());

        mUserController.setInitialConfig(/* userSwitchUiEnabled= */ true,
@@ -733,7 +737,9 @@ public class UserControllerTest {
    /** Test scheduling stopping of background users - reschedule if current user is a guest. */
    @Test
    public void testScheduleStopOfBackgroundUser_rescheduleWhenGuest() throws Exception {
        mSetFlagsRule.enableFlags(android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER);
        mSetFlagsRule.enableFlags(
                android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER,
                android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER_BY_DEFAULT);
        assumeFalse(UserManager.isVisibleBackgroundUsersEnabled());

        mUserController.setInitialConfig(/* userSwitchUiEnabled= */ true,
@@ -743,7 +749,7 @@ public class UserControllerTest {
        final int TEST_USER_GUEST = 902;
        setUpUser(TEST_USER_GUEST, UserInfo.FLAG_GUEST);

        setUpUser(TEST_USER_ID2, NO_USERINFO_FLAGS);
        setUpUser(TEST_USER_ID2, DEFAULT_USER_FLAGS);

        // Switch to TEST_USER_ID from user 0
        int numberOfUserSwitches = 0;
@@ -789,7 +795,9 @@ public class UserControllerTest {
    /** Test scheduling stopping of background users - reschedule if user with a scheduled alarm. */
    @Test
    public void testScheduleStopOfBackgroundUser_rescheduleIfAlarm() throws Exception {
        mSetFlagsRule.enableFlags(android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER);
        mSetFlagsRule.enableFlags(
                android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER,
                android.multiuser.Flags.FLAG_SCHEDULE_STOP_OF_BACKGROUND_USER_BY_DEFAULT);
        assumeFalse(UserManager.isVisibleBackgroundUsersEnabled());

        mUserController.setInitialConfig(/* userSwitchUiEnabled= */ true,
@@ -1657,7 +1665,7 @@ public class UserControllerTest {
    }

    private void setUpAndStartUserInBackground(int userId) throws Exception {
        setUpUser(userId, 0);
        setUpUser(userId, DEFAULT_USER_FLAGS);
        mUserController.startUser(userId, USER_START_MODE_BACKGROUND);
        verify(mInjector.mLockPatternUtilsMock, times(1)).unlockUserKeyIfUnsecured(userId);
        mUserStates.put(userId, mUserController.getStartedUserState(userId));
@@ -1732,17 +1740,20 @@ public class UserControllerTest {
        if (userType == null) {
            userType = UserInfo.getDefaultUserType(flags);
        }
        UserInfo userInfo = new UserInfo(userId, "User" + userId, /* iconPath= */ null, flags,
                userType);
        userInfo.preCreated = preCreated;
        when(mInjector.mUserManagerMock.getUserInfo(eq(userId))).thenReturn(userInfo);
        when(mInjector.mUserManagerMock.isPreCreated(userId)).thenReturn(preCreated);

        UserTypeDetails userTypeDetails = UserTypeFactory.getUserTypes().get(userType);
        assertThat(userTypeDetails).isNotNull();
        when(mInjector.mUserManagerInternalMock.getUserProperties(eq(userId)))
                .thenReturn(userTypeDetails.getDefaultUserPropertiesReference());

        flags |= userTypeDetails.getDefaultUserInfoFlags();

        UserInfo userInfo = new UserInfo(userId, "User" + userId, /* iconPath= */ null, flags,
                userType);
        userInfo.preCreated = preCreated;
        when(mInjector.mUserManagerMock.getUserInfo(eq(userId))).thenReturn(userInfo);
        when(mInjector.mUserManagerMock.isPreCreated(userId)).thenReturn(preCreated);

        mUserInfos.put(userId, userInfo);
        when(mInjector.mUserManagerMock.getUsers(anyBoolean()))
                .thenReturn(mUserInfos.values().stream().toList());