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

Commit 802ba57b authored by Adam Bookatz's avatar Adam Bookatz
Browse files

Re-schedule bg stop on shorter timescale

If a background user was scheduled to stop, but we postponed it, we were
using the default automatic stopping time to decide how long to postpone
it. However, that was no longer ideal because:
* we now also have background user stops due to impending alarms. So
  even if the default scheduling is disabled, we still want to be able
to postpone schedules. Without this cl, we would simply never stop the
background user; now, we will.
* the default value might be a very large value. If we already wanted to
  stop the user, but just couldn't due to some transient issue (with no
evidence that the user is actually useful right now), we shouldn't have
to wait so long when rescheduling.

Bug: 401340391
Bug: 330351042
Test: UserControllerTest
Flag: android.multiuser.schedule_stop_of_background_user
Change-Id: I46d53e5c139dbaab298714c06fdf485ceebb8b77
parent 14788606
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
@@ -240,6 +240,13 @@ class UserController implements Handler.Callback {
     */
    private static final long TIME_BEFORE_USERS_ALARM_TO_AVOID_STOPPING_MS = 60 * 60_000; // 60 mins

    /**
     * If a background user stop is requested, but doing so right now isn't suitable (e.g. because
     * it is currently playing audio) and we have to reschedule that background stop, we should
     * schedule that stop for this many seconds from now.
     */
    private static final int POSTPONEMENT_TIME_FOR_BACKGROUND_USER_STOP_SECS = 30 * 60; // 30 mins

    /**
     * Maximum number of users we allow to be running at a time, including system user.
     *
@@ -2645,6 +2652,16 @@ class UserController implements Handler.Callback {
        scheduleStopOfBackgroundUser(oldUserId, mBackgroundUserScheduledStopTimeSecs);
    }

    /**
     * Possibly schedules the user to be stopped at a future point, even though we had originally
     * wanted to stop it now. For example, perhaps we have to postpone a scheduled user stop for
     * some reason, and therefore are rescheduling it until a later point.
     * This is only intended for full users that are currently in the background.
     */
    private void rescheduleStopOfBackgroundUser(@UserIdInt int oldUserId) {
        scheduleStopOfBackgroundUser(oldUserId, POSTPONEMENT_TIME_FOR_BACKGROUND_USER_STOP_SECS);
    }

    /**
     * Possibly schedules the user to be stopped at after the given number of seconds.
     * This is only intended for full users that are currently in the background.
@@ -2704,13 +2721,13 @@ class UserController implements Handler.Callback {
        if (avoidStoppingUserDueToUpcomingAlarm(userId)) {
            // We want this user running soon for alarm-purposes, so don't stop it now. Reschedule.
            Slogf.d(TAG, "User %d will fire an alarm soon, so reschedule bg stopping", userId);
            scheduleStopOfInactiveBackgroundUser(userId);
            rescheduleStopOfBackgroundUser(userId);
            return;
        }
        if (mInjector.getAudioManagerInternal().isUserPlayingAudio(userId)) {
            // User is audible (even if invisibly, e.g. via an alarm), so don't stop it. Reschedule.
            Slogf.d(TAG, "User %d is playing audio, so reschedule bg stopping", userId);
            scheduleStopOfInactiveBackgroundUser(userId);
            rescheduleStopOfBackgroundUser(userId);
            return;
        }
        synchronized (mLock) {
@@ -2725,7 +2742,7 @@ class UserController implements Handler.Callback {
            final UserInfo currentOrTargetUser = getCurrentUserLU();
            if (currentOrTargetUser != null && currentOrTargetUser.isGuest()) {
                // Don't kill any background users for the sake of a Guest. Just reschedule instead.
                scheduleStopOfInactiveBackgroundUser(userId);
                rescheduleStopOfBackgroundUser(userId);
                return;
            }
            Slogf.i(TAG, "Stopping background user %d due to inactivity", userId);