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

Commit 50788fe6 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "DISALLOW_RUN_IN_BACKGROUND works for profiles" into main

parents 7be8c274 ffbe9543
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -5672,9 +5672,11 @@ public class ActivityManager {
    }

    /**
     * Return if a given profile is in the foreground.
     * Returns whether the given user, or its parent (if the user is a profile), is in the
     * foreground.
     * @param userHandle UserHandle to check
     * @return Returns the boolean result.
     * @return whether the user is the foreground user or, if it is a profile, whether its parent
     *         is the foreground user
     * @hide
     */
    @RequiresPermission(anyOf = {
+25 −11
Original line number Diff line number Diff line
@@ -576,6 +576,8 @@ class UserController implements Handler.Callback {
            }
            // allowDelayedLocking set here as stopping user is done without any explicit request
            // from outside.
            Slogf.i(TAG, "Too many running users (%d). Attempting to stop user %d",
                    currentlyRunningLru.size(), userId);
            if (stopUsersLU(userId, /* force= */ false, /* allowDelayedLocking= */ true,
                    /* stopUserCallback= */ null, /* keyEvictedCallback= */ null)
                    == USER_OP_SUCCESS) {
@@ -1540,6 +1542,7 @@ class UserController implements Handler.Callback {
        }
        if (userInfo.isGuest() || userInfo.isEphemeral()) {
            // This is a user to be stopped.
            Slogf.i(TAG, "Stopping background guest or ephemeral user " + oldUserId);
            synchronized (mLock) {
                stopUsersLU(oldUserId, /* force= */ true, /* allowDelayedLocking= */ false,
                        null, null);
@@ -2219,6 +2222,10 @@ class UserController implements Handler.Callback {
        mUserSwitchObservers.finishBroadcast();
    }

    /**
     * Possibly stops the given full user (or its profile) when we switch out of it, if dictated
     * by policy.
     */
    private void stopUserOnSwitchIfEnforced(@UserIdInt int oldUserId) {
        // Never stop system user
        if (oldUserId == UserHandle.USER_SYSTEM) {
@@ -2228,20 +2235,27 @@ class UserController implements Handler.Callback {
                hasUserRestriction(UserManager.DISALLOW_RUN_IN_BACKGROUND, oldUserId);
        synchronized (mLock) {
            // If running in background is disabled or mStopUserOnSwitch mode, stop the user.
            boolean disallowRunInBg = hasRestriction || shouldStopUserOnSwitch();
            if (!disallowRunInBg) {
                if (DEBUG_MU) {
                    Slogf.i(TAG, "stopUserOnSwitchIfEnforced() NOT stopping %d and related users",
                            oldUserId);
                }
            if (hasRestriction || shouldStopUserOnSwitch()) {
                Slogf.i(TAG, "Stopping user %d and its profiles on user switch", oldUserId);
                stopUsersLU(oldUserId, /* force= */ false, /* allowDelayedLocking= */ false,
                        null, null);
                return;
            }
            if (DEBUG_MU) {
                Slogf.i(TAG, "stopUserOnSwitchIfEnforced() stopping %d and related users",
                        oldUserId);
        }
            stopUsersLU(oldUserId, /* force= */ false, /* allowDelayedLocking= */ true,
                    null, null);

        // We didn't need to stop the parent, but perhaps one of its profiles needs to be stopped.
        final List<UserInfo> profiles = mInjector.getUserManager().getProfiles(
                oldUserId, /* enabledOnly= */ false);
        final int count = profiles.size();
        for (int i = 0; i < count; i++) {
            final int profileUserId = profiles.get(i).id;
            if (hasUserRestriction(UserManager.DISALLOW_RUN_IN_BACKGROUND, profileUserId)) {
                Slogf.i(TAG, "Stopping profile %d on user switch", profileUserId);
                synchronized (mLock) {
                    stopUsersLU(profileUserId,
                            /* force= */ true, /* allowDelayedLocking= */ false, null, null);
                }
            }
        }
    }

+3 −2
Original line number Diff line number Diff line
@@ -717,8 +717,9 @@ public class UserRestrictionsUtils {
                    break;
                case UserManager.DISALLOW_RUN_IN_BACKGROUND:
                    if (newValue) {
                        int currentUser = ActivityManager.getCurrentUser();
                        if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
                        final ActivityManager am = context.getSystemService(ActivityManager.class);
                        if (!am.isProfileForeground(UserHandle.of(userId))
                                && userId != UserHandle.USER_SYSTEM) {
                            try {
                                ActivityManager.getService().stopUser(userId, false, null);
                            } catch (RemoteException e) {