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

Commit 3d5d80ca authored by Peter Kalauskas's avatar Peter Kalauskas
Browse files

Introduce config for auto-created guest users

Create a new frameworks config: config_guestUserAutoCreated.

If true,

 - Device should always have a guest user available.

 - Instead of showing "Add guest", the UI will show "Guest", and instead
   of "Remove guest", there will be an option to "Reset guest"

 - Guest user is always created on boot if it does not already exist

 - New guest user is created any time current guest user is removed

For now, these behaviors will be handled by System UI and Settings.

In addition, a few changes were made to System UI to simplify the code.
These changes may also affect devices running without the new config:

 - Move GuestResumeSessionReceiver.wipeGuestSession() to
   UserSwitcherController

 - New method: UserSwitcherController.createGuest()

 - Introduce dependency from KeyguardViewMediator to
   UserSwitcherController

 - Introduce dependency from GuestResumeSessionReceiver to
   UserSwitcherController

Bug: 188542158
Test: With config_guestUserAutoCreated=true, remove all guest users
      using adb (`adb shell cmd user list -v --all` to find the user
      ids, then remove each guest user with `adb shell pm remove-user
      <id>`), reboot device, check that there is a new guest on boot.
Test: With config_guestUserAutoCreated=true, reboot device when there is
      already a guest user, confirm that guest user was not wiped after
      reboot is completed.
Test: With config_guestUserAutoCreated=true, switch to guest user,
      select "Reset guest" from QS tile, select "Reset". Phone should
      switch back to last active user, and QS tile should now show
      "Guest" instead of "Add guest". Run `adb shell cmd user list -v
      --all` to confirm guest has a new user id.
Test: With config_guestUserAutoCreated=false, confirm that "Add guest"
      and "Remove guest" features remain unchanged
Change-Id: Ib1c8dd0a0796d95681167f51e12e3f4be21345af
Merged-In: Ib1c8dd0a0796d95681167f51e12e3f4be21345af
parent 255565b2
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -3529,6 +3529,12 @@
    <!-- If true, all guest users created on the device will be ephemeral. -->
    <bool name="config_guestUserEphemeral">false</bool>

    <!-- Whether device should always have a guest user available. If true, guest user will be
         created on boot, and a new guest user will be created in the background anytime the current
         guest user is removed. Instead of showing "Add guest" and "Remove guest", the UI will show
         "Guest" and "Reset guest". -->
    <bool name="config_guestUserAutoCreated">false</bool>

    <!-- Enforce strong auth on boot. Setting this to false represents a security risk and should
         not be ordinarily done. The only case in which this might be permissible is in a car head
         unit where there are hardware mechanisms to protect the device (physical keys) and not
+1 −0
Original line number Diff line number Diff line
@@ -402,6 +402,7 @@
  <java-symbol type="bool" name="config_supportsSystemDecorsOnSecondaryDisplays" />
  <java-symbol type="bool" name="config_supportsInsecureLockScreen" />
  <java-symbol type="bool" name="config_guestUserEphemeral" />
  <java-symbol type="bool" name="config_guestUserAutoCreated" />
  <java-symbol type="bool" name="config_localDisplaysMirrorContent" />
  <java-symbol type="array" name="config_localPrivateDisplayPorts" />
  <java-symbol type="integer" name="config_defaultDisplayDefaultColorMode" />
+2 −0
Original line number Diff line number Diff line
@@ -1422,6 +1422,8 @@
    <string name="guest_new_guest">Add guest</string>
    <!-- Label for exiting and removing the guest session in the user switcher [CHAR LIMIT=35] -->
    <string name="guest_exit_guest">Remove guest</string>
    <!-- Label for resetting guest session in the user switcher, which will remove all data from the current guest session [CHAR LIMIT=35] -->
    <string name="guest_reset_guest">Reset guest</string>
    <!-- Name for the guest user [CHAR LIMIT=35] -->
    <string name="guest_nickname">Guest</string>

+6 −0
Original line number Diff line number Diff line
@@ -1148,12 +1148,18 @@
    <!-- Title of the confirmation dialog when exiting guest session [CHAR LIMIT=NONE] -->
    <string name="guest_exit_guest_dialog_title">Remove guest?</string>

    <!-- Title of the confirmation dialog when resetting guest session [CHAR LIMIT=NONE] -->
    <string name="guest_reset_guest_dialog_title">Reset guest?</string>

    <!-- Message of the confirmation dialog when exiting guest session [CHAR LIMIT=NONE] -->
    <string name="guest_exit_guest_dialog_message">All apps and data in this session will be deleted.</string>

    <!-- Label for button in confirmation dialog when exiting guest session [CHAR LIMIT=35] -->
    <string name="guest_exit_guest_dialog_remove">Remove</string>

    <!-- Label for button in confirmation dialog when resetting guest session [CHAR LIMIT=35] -->
    <string name="guest_reset_guest_dialog_remove">Reset</string>

    <!-- Title of the notification when resuming an existing guest session [CHAR LIMIT=NONE] -->
    <string name="guest_wipe_session_title">Welcome back, guest!</string>

+12 −54
Original line number Diff line number Diff line
@@ -27,15 +27,14 @@ import android.content.IntentFilter;
import android.content.pm.UserInfo;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManagerGlobal;

import com.android.internal.logging.UiEventLogger;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.qs.QSUserSwitcherEvent;
import com.android.systemui.statusbar.phone.SystemUIDialog;
import com.android.systemui.statusbar.policy.UserSwitcherController;

/**
 * Manages notification when a guest session is resumed.
@@ -47,9 +46,12 @@ public class GuestResumeSessionReceiver extends BroadcastReceiver {
    private static final String SETTING_GUEST_HAS_LOGGED_IN = "systemui.guest_has_logged_in";

    private Dialog mNewSessionDialog;
    private final UserSwitcherController mUserSwitcherController;
    private final UiEventLogger mUiEventLogger;

    public GuestResumeSessionReceiver(UiEventLogger uiEventLogger) {
    public GuestResumeSessionReceiver(UserSwitcherController userSwitcherController,
            UiEventLogger uiEventLogger) {
        mUserSwitcherController = userSwitcherController;
        mUiEventLogger = uiEventLogger;
    }

@@ -90,7 +92,8 @@ public class GuestResumeSessionReceiver extends BroadcastReceiver {
            int notFirstLogin = Settings.System.getIntForUser(
                    cr, SETTING_GUEST_HAS_LOGGED_IN, 0, userId);
            if (notFirstLogin != 0) {
                mNewSessionDialog = new ResetSessionDialog(context, mUiEventLogger, userId);
                mNewSessionDialog = new ResetSessionDialog(context, mUserSwitcherController,
                        mUiEventLogger, userId);
                mNewSessionDialog.show();
            } else {
                Settings.System.putIntForUser(
@@ -99,54 +102,6 @@ public class GuestResumeSessionReceiver extends BroadcastReceiver {
        }
    }

    /**
     * Wipes the guest session.
     *
     * The guest must be the current user and its id must be {@param userId}.
     */
    private static void wipeGuestSession(Context context, int userId) {
        UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
        UserInfo currentUser;
        try {
            currentUser = ActivityManager.getService().getCurrentUser();
        } catch (RemoteException e) {
            Log.e(TAG, "Couldn't wipe session because ActivityManager is dead");
            return;
        }
        if (currentUser.id != userId) {
            Log.w(TAG, "User requesting to start a new session (" + userId + ")"
                    + " is not current user (" + currentUser.id + ")");
            return;
        }
        if (!currentUser.isGuest()) {
            Log.w(TAG, "User requesting to start a new session (" + userId + ")"
                    + " is not a guest");
            return;
        }

        boolean marked = userManager.markGuestForDeletion(currentUser.id);
        if (!marked) {
            Log.w(TAG, "Couldn't mark the guest for deletion for user " + userId);
            return;
        }
        UserInfo newGuest = userManager.createGuest(context, currentUser.name);

        try {
            if (newGuest == null) {
                Log.e(TAG, "Could not create new guest, switching back to system user");
                ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
                userManager.removeUser(currentUser.id);
                WindowManagerGlobal.getWindowManagerService().lockNow(null /* options */);
                return;
            }
            ActivityManager.getService().switchUser(newGuest.id);
            userManager.removeUser(currentUser.id);
        } catch (RemoteException e) {
            Log.e(TAG, "Couldn't wipe session because ActivityManager or WindowManager is dead");
            return;
        }
    }

    private void cancelDialog() {
        if (mNewSessionDialog != null && mNewSessionDialog.isShowing()) {
            mNewSessionDialog.cancel();
@@ -160,10 +115,12 @@ public class GuestResumeSessionReceiver extends BroadcastReceiver {
        private static final int BUTTON_WIPE = BUTTON_NEGATIVE;
        private static final int BUTTON_DONTWIPE = BUTTON_POSITIVE;

        private final UserSwitcherController mUserSwitcherController;
        private final UiEventLogger mUiEventLogger;
        private final int mUserId;

        ResetSessionDialog(Context context, UiEventLogger uiEventLogger, int userId) {
        ResetSessionDialog(Context context, UserSwitcherController userSwitcherController,
                UiEventLogger uiEventLogger, int userId) {
            super(context);

            setTitle(context.getString(R.string.guest_wipe_session_title));
@@ -175,6 +132,7 @@ public class GuestResumeSessionReceiver extends BroadcastReceiver {
            setButton(BUTTON_DONTWIPE,
                    context.getString(R.string.guest_wipe_session_dontwipe), this);

            mUserSwitcherController = userSwitcherController;
            mUiEventLogger = uiEventLogger;
            mUserId = userId;
        }
@@ -183,7 +141,7 @@ public class GuestResumeSessionReceiver extends BroadcastReceiver {
        public void onClick(DialogInterface dialog, int which) {
            if (which == BUTTON_WIPE) {
                mUiEventLogger.log(QSUserSwitcherEvent.QS_USER_GUEST_WIPE);
                wipeGuestSession(getContext(), mUserId);
                mUserSwitcherController.removeGuestUser(mUserId, UserHandle.USER_NULL);
                dismiss();
            } else if (which == BUTTON_DONTWIPE) {
                mUiEventLogger.log(QSUserSwitcherEvent.QS_USER_GUEST_CONTINUE);
Loading