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

Commit 9cbfc9e2 authored by Fyodor Kupolov's avatar Fyodor Kupolov
Browse files

Added DISALLOW_RUN_IN_BACKGROUND user restriction

It forces the user to stop instead of going into the background. Also
changed behavior of stopUser method. Now it also attempts to stop related
users along with the specified userId.

Based on ag/807976, with the only difference that it's now a user restriction.

Bug: 24579258
Bug: 24708668
Change-Id: I357298908816fc58feeed83b7e9979fc33d25da6
parent 6449a956
Loading
Loading
Loading
Loading
+14 −3
Original line number Original line Diff line number Diff line
@@ -154,7 +154,7 @@ public class Am extends BaseCommand {
                "       am switch-user <USER_ID>\n" +
                "       am switch-user <USER_ID>\n" +
                "       am start-user <USER_ID>\n" +
                "       am start-user <USER_ID>\n" +
                "       am unlock-user <USER_ID> [TOKEN_HEX]\n" +
                "       am unlock-user <USER_ID> [TOKEN_HEX]\n" +
                "       am stop-user [-w] <USER_ID>\n" +
                "       am stop-user [-w] [-f] <USER_ID>\n" +
                "       am stack start <DISPLAY_ID> <INTENT>\n" +
                "       am stack start <DISPLAY_ID> <INTENT>\n" +
                "       am stack movetask <TASK_ID> <STACK_ID> [true|false]\n" +
                "       am stack movetask <TASK_ID> <STACK_ID> [true|false]\n" +
                "       am stack resize <STACK_ID> <LEFT,TOP,RIGHT,BOTTOM>\n" +
                "       am stack resize <STACK_ID> <LEFT,TOP,RIGHT,BOTTOM>\n" +
@@ -290,6 +290,7 @@ public class Am extends BaseCommand {
                "am stop-user: stop execution of USER_ID, not allowing it to run any\n" +
                "am stop-user: stop execution of USER_ID, not allowing it to run any\n" +
                "  code until a later explicit start or switch to it.\n" +
                "  code until a later explicit start or switch to it.\n" +
                "  -w: wait for stop-user to complete.\n" +
                "  -w: wait for stop-user to complete.\n" +
                "  -f: force stop even if there are related users that cannot be stopped.\n" +
                "\n" +
                "\n" +
                "am stack start: start a new activity on <DISPLAY_ID> using <INTENT>.\n" +
                "am stack start: start a new activity on <DISPLAY_ID> using <INTENT>.\n" +
                "\n" +
                "\n" +
@@ -1131,10 +1132,13 @@ public class Am extends BaseCommand {


    private void runStopUser() throws Exception {
    private void runStopUser() throws Exception {
        boolean wait = false;
        boolean wait = false;
        String opt = null;
        boolean force = false;
        String opt;
        while ((opt = nextOption()) != null) {
        while ((opt = nextOption()) != null) {
            if ("-w".equals(opt)) {
            if ("-w".equals(opt)) {
                wait = true;
                wait = true;
            } else if ("-f".equals(opt)) {
                force = true;
            } else {
            } else {
                System.err.println("Error: unknown option: " + opt);
                System.err.println("Error: unknown option: " + opt);
                return;
                return;
@@ -1143,7 +1147,7 @@ public class Am extends BaseCommand {
        int user = Integer.parseInt(nextArgRequired());
        int user = Integer.parseInt(nextArgRequired());
        StopUserCallback callback = wait ? new StopUserCallback() : null;
        StopUserCallback callback = wait ? new StopUserCallback() : null;


        int res = mAm.stopUser(user, callback);
        int res = mAm.stopUser(user, force, callback);
        if (res != ActivityManager.USER_OP_SUCCESS) {
        if (res != ActivityManager.USER_OP_SUCCESS) {
            String txt = "";
            String txt = "";
            switch (res) {
            switch (res) {
@@ -1153,6 +1157,13 @@ public class Am extends BaseCommand {
                case ActivityManager.USER_OP_UNKNOWN_USER:
                case ActivityManager.USER_OP_UNKNOWN_USER:
                    txt = " (Unknown user " + user + ")";
                    txt = " (Unknown user " + user + ")";
                    break;
                    break;
                case ActivityManager.USER_OP_ERROR_IS_SYSTEM:
                    txt = " (System user cannot be stopped)";
                    break;
                case ActivityManager.USER_OP_ERROR_RELATED_USERS_CANNOT_STOP:
                    txt = " (Can't stop user " + user
                            + " - one of its related users can't be stopped)";
                    break;
            }
            }
            System.err.println("Switch failed: " + res + txt);
            System.err.println("Switch failed: " + res + txt);
        } else if (callback != null) {
        } else if (callback != null) {
+6 −0
Original line number Original line Diff line number Diff line
@@ -265,6 +265,12 @@ public class ActivityManager {
    /** @hide User operation call: given user id is the current user, can't be stopped. */
    /** @hide User operation call: given user id is the current user, can't be stopped. */
    public static final int USER_OP_IS_CURRENT = -2;
    public static final int USER_OP_IS_CURRENT = -2;


    /** @hide User operation call: system user can't be stopped. */
    public static final int USER_OP_ERROR_IS_SYSTEM = -3;

    /** @hide User operation call: one of related users cannot be stopped. */
    public static final int USER_OP_ERROR_RELATED_USERS_CANNOT_STOP = -4;

    /** @hide Process does not exist. */
    /** @hide Process does not exist. */
    public static final int PROCESS_STATE_NONEXISTENT = -1;
    public static final int PROCESS_STATE_NONEXISTENT = -1;


+5 −2
Original line number Original line Diff line number Diff line
@@ -1981,9 +1981,10 @@ public abstract class ActivityManagerNative extends Binder implements IActivityM
        case STOP_USER_TRANSACTION: {
        case STOP_USER_TRANSACTION: {
            data.enforceInterface(IActivityManager.descriptor);
            data.enforceInterface(IActivityManager.descriptor);
            int userid = data.readInt();
            int userid = data.readInt();
            boolean force = data.readInt() != 0;
            IStopUserCallback callback = IStopUserCallback.Stub.asInterface(
            IStopUserCallback callback = IStopUserCallback.Stub.asInterface(
                    data.readStrongBinder());
                    data.readStrongBinder());
            int result = stopUser(userid, callback);
            int result = stopUser(userid, force, callback);
            reply.writeNoException();
            reply.writeNoException();
            reply.writeInt(result);
            reply.writeInt(result);
            return true;
            return true;
@@ -5287,11 +5288,13 @@ class ActivityManagerProxy implements IActivityManager
        return result;
        return result;
    }
    }


    public int stopUser(int userid, IStopUserCallback callback) throws RemoteException {
    public int stopUser(int userid, boolean force, IStopUserCallback callback)
            throws RemoteException {
        Parcel data = Parcel.obtain();
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        data.writeInterfaceToken(IActivityManager.descriptor);
        data.writeInterfaceToken(IActivityManager.descriptor);
        data.writeInt(userid);
        data.writeInt(userid);
        data.writeInt(force ? 1 : 0);
        data.writeStrongInterface(callback);
        data.writeStrongInterface(callback);
        mRemote.transact(STOP_USER_TRANSACTION, data, reply, 0);
        mRemote.transact(STOP_USER_TRANSACTION, data, reply, 0);
        reply.readException();
        reply.readException();
+1 −1
Original line number Original line Diff line number Diff line
@@ -391,7 +391,7 @@ public interface IActivityManager extends IInterface {
    public boolean switchUser(int userid) throws RemoteException;
    public boolean switchUser(int userid) throws RemoteException;
    public boolean startUserInBackground(int userid) throws RemoteException;
    public boolean startUserInBackground(int userid) throws RemoteException;
    public boolean unlockUser(int userid, byte[] token) throws RemoteException;
    public boolean unlockUser(int userid, byte[] token) throws RemoteException;
    public int stopUser(int userid, IStopUserCallback callback) throws RemoteException;
    public int stopUser(int userid, boolean force, IStopUserCallback callback) throws RemoteException;
    public UserInfo getCurrentUser() throws RemoteException;
    public UserInfo getCurrentUser() throws RemoteException;
    public boolean isUserRunning(int userid, int flags) throws RemoteException;
    public boolean isUserRunning(int userid, int flags) throws RemoteException;
    public int[] getRunningUserIds() throws RemoteException;
    public int[] getRunningUserIds() throws RemoteException;
+13 −0
Original line number Original line Diff line number Diff line
@@ -486,6 +486,19 @@ public class UserManager {
     */
     */
    public static final String DISALLOW_RECORD_AUDIO = "no_record_audio";
    public static final String DISALLOW_RECORD_AUDIO = "no_record_audio";


    /**
     * Specifies if a user is not allowed to run in the background and should be stopped during
     * user switch. The default value is <code>false</code>.
     *
     * <p>This restriction can be set by device owners and profile owners.
     *
     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
     * @see #getUserRestrictions()
     * @hide
     */
    public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";

    /**
    /**
     * Specifies if a user is not allowed to use the camera.
     * Specifies if a user is not allowed to use the camera.
     *
     *
Loading