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

Commit e44dde31 authored by jovanak's avatar jovanak
Browse files

Cleans up user info creation in UserGridRecyclerView.

UserGridRecyclerView shouldn't be creating UserInfo objects, especially not fake
ones that only serve as placeholder for a String. Guest and Add User buttons in
the user switcher don't correspond to real users, and as such, shouldn't be tied
to UserInfo objects.

This can also lead to bugs because consumers of UserRecord assume UserInfo
corresponds to a real user.

Instead we pass null in these cases, and make UserInfo nullable.

Bug: 144993580
Test: manual
Change-Id: I1c772564c8e9ae65699943f6a2f8c1d2c75f013b
parent 5aa92326
Loading
Loading
Loading
Loading
+10 −11
Original line number Diff line number Diff line
@@ -131,10 +131,7 @@ public class FullscreenUserSwitcher {
    private void showDialogForInitialUser() {
        int initialUser = mCarUserManagerHelper.getInitialUser();
        UserInfo initialUserInfo = mUserManager.getUserInfo(initialUser);
        mSelectedUser = new UserRecord(initialUserInfo,
                /* isStartGuestSession= */ false,
                /* isAddUser= */ false,
                /* isForeground= */ true);
        mSelectedUser = new UserRecord(initialUserInfo, UserRecord.FOREGROUND_USER);

        // If the initial user has screen lock and trusted device, display the unlock dialog on the
        // keyguard.
@@ -180,6 +177,7 @@ public class FullscreenUserSwitcher {
     */
    private void onUserSelected(UserGridRecyclerView.UserRecord record) {
        mSelectedUser = record;
        if (record.mInfo != null) {
            if (hasScreenLock(record.mInfo.id) && hasTrustedDevice(record.mInfo.id)) {
                mUnlockDialogHelper.showUnlockDialog(record.mInfo.id, mOnHideListener);
                return;
@@ -187,6 +185,7 @@ public class FullscreenUserSwitcher {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "no trusted device enrolled for uid: " + record.mInfo.id);
            }
        }
        dismissUserSwitcher();
    }

@@ -195,7 +194,7 @@ public class FullscreenUserSwitcher {
            Log.e(TAG, "Request to dismiss user switcher, but no user selected");
            return;
        }
        if (mSelectedUser.mIsForeground) {
        if (mSelectedUser.mType == UserRecord.FOREGROUND_USER) {
            hide();
            mStatusBar.dismissKeyguard();
            return;
+76 −50
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static android.content.DialogInterface.BUTTON_POSITIVE;
import static android.os.UserManager.DISALLOW_ADD_USER;
import static android.os.UserManager.SWITCHABILITY_STATUS_OK;

import android.annotation.IntDef;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.AlertDialog;
@@ -54,6 +55,8 @@ import com.android.internal.util.UserIcons;
import com.android.systemui.R;
import com.android.systemui.statusbar.phone.SystemUIDialog;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@@ -142,8 +145,8 @@ public class UserGridRecyclerView extends RecyclerView {
            }

            boolean isForeground = fgUserId == userInfo.id;
            UserRecord record = new UserRecord(userInfo, false /* isStartGuestSession */,
                    false /* isAddUser */, isForeground);
            UserRecord record = new UserRecord(userInfo,
                    isForeground ? UserRecord.FOREGROUND_USER : UserRecord.BACKGROUND_USER);
            userRecords.add(record);
        }

@@ -160,27 +163,21 @@ public class UserGridRecyclerView extends RecyclerView {

    private UserRecord createForegroundUserRecord() {
        return new UserRecord(mUserManager.getUserInfo(ActivityManager.getCurrentUser()),
                false /* isStartGuestSession */, false /* isAddUser */, true /* isForeground */);
                UserRecord.FOREGROUND_USER);
    }

    /**
     * Create guest user record
     */
    private UserRecord createStartGuestUserRecord() {
        UserInfo userInfo = new UserInfo();
        userInfo.name = mContext.getString(R.string.start_guest_session);
        return new UserRecord(userInfo, true /* isStartGuestSession */, false /* isAddUser */,
                false /* isForeground */);
        return new UserRecord(null /* userInfo */, UserRecord.START_GUEST);
    }

    /**
     * Create add user record
     */
    private UserRecord createAddUserRecord() {
        UserInfo userInfo = new UserInfo();
        userInfo.name = mContext.getString(R.string.car_add_user);
        return new UserRecord(userInfo, false /* isStartGuestSession */,
                true /* isAddUser */, false /* isForeground */);
        return new UserRecord(null /* userInfo */, UserRecord.ADD_USER);
    }

    public void setUserSelectionListener(UserSelectionListener userSelectionListener) {
@@ -258,35 +255,36 @@ public class UserGridRecyclerView extends RecyclerView {
            UserRecord userRecord = mUsers.get(position);
            RoundedBitmapDrawable circleIcon = getCircularUserRecordIcon(userRecord);
            holder.mUserAvatarImageView.setImageDrawable(circleIcon);
            holder.mUserNameTextView.setText(userRecord.mInfo.name);
            holder.mUserNameTextView.setText(getUserRecordName(userRecord));

            holder.mView.setOnClickListener(v -> {
                if (userRecord == null) {
                    return;
                }

                if (userRecord.mIsStartGuestSession) {
                switch (userRecord.mType) {
                    case UserRecord.START_GUEST:
                        notifyUserSelected(userRecord);
                        UserInfo guest = createNewOrFindExistingGuest(mContext);
                        if (guest != null) {
                            mCarUserManagerHelper.switchToUser(guest);
                        }
                    return;
                }

                        break;
                    case UserRecord.ADD_USER:
                        // If the user wants to add a user, show dialog to confirm adding a user
                if (userRecord.mIsAddUser) {
                        // Disable button so it cannot be clicked multiple times
                        mAddUserView = holder.mView;
                        mAddUserView.setEnabled(false);
                        mAddUserRecord = userRecord;

                        handleAddUserClicked();
                    return;
                }
                // If the user doesn't want to be a guest or add a user, switch to the user selected
                        break;
                    default:
                        // If the user doesn't want to be a guest or add a user, switch to the user
                        // selected
                        notifyUserSelected(userRecord);
                        mCarUserManagerHelper.switchToUser(userRecord.mInfo);
                }
            });

        }
@@ -372,19 +370,44 @@ public class UserGridRecyclerView extends RecyclerView {
        private RoundedBitmapDrawable getCircularUserRecordIcon(UserRecord userRecord) {
            Resources resources = mContext.getResources();
            RoundedBitmapDrawable circleIcon;
            if (userRecord.mIsStartGuestSession) {
            switch (userRecord.mType) {
                case UserRecord.START_GUEST:
                    circleIcon = mUserIconProvider.getRoundedGuestDefaultIcon(resources);
            } else if (userRecord.mIsAddUser) {
                circleIcon = RoundedBitmapDrawableFactory.create(mRes, UserIcons.convertToBitmap(
                        mContext.getDrawable(R.drawable.car_add_circle_round)));
                circleIcon.setCircular(true);
            } else {
                    break;
                case UserRecord.ADD_USER:
                    circleIcon = getCircularAddUserIcon();
                    break;
                default:
                    circleIcon = mUserIconProvider.getRoundedUserIcon(userRecord.mInfo, mContext);
                    break;
            }
            return circleIcon;
        }

        private RoundedBitmapDrawable getCircularAddUserIcon() {
            RoundedBitmapDrawable circleIcon =
                    RoundedBitmapDrawableFactory.create(mRes, UserIcons.convertToBitmap(
                    mContext.getDrawable(R.drawable.car_add_circle_round)));
            circleIcon.setCircular(true);
            return circleIcon;
        }

        private String getUserRecordName(UserRecord userRecord) {
            String recordName;
            switch (userRecord.mType) {
                case UserRecord.START_GUEST:
                    recordName = mContext.getString(R.string.start_guest_session);
                    break;
                case UserRecord.ADD_USER:
                    recordName = mContext.getString(R.string.car_add_user);
                    break;
                default:
                    recordName = userRecord.mInfo.name;
                    break;
            }
            return recordName;
        }

        /**
         * Finds the existing Guest user, or creates one if it doesn't exist.
         * @param context App context
@@ -468,18 +491,21 @@ public class UserGridRecyclerView extends RecyclerView {
     * guest profile, add user profile, or the foreground user.
     */
    public static final class UserRecord {

        public final UserInfo mInfo;
        public final boolean mIsStartGuestSession;
        public final boolean mIsAddUser;
        public final boolean mIsForeground;
        public final @UserRecordType int mType;

        public static final int START_GUEST = 0;
        public static final int ADD_USER = 1;
        public static final int FOREGROUND_USER = 2;
        public static final int BACKGROUND_USER = 3;

        @IntDef({START_GUEST, ADD_USER, FOREGROUND_USER, BACKGROUND_USER})
        @Retention(RetentionPolicy.SOURCE)
        public @interface UserRecordType{}

        public UserRecord(UserInfo userInfo, boolean isStartGuestSession, boolean isAddUser,
                boolean isForeground) {
        public UserRecord(@Nullable UserInfo userInfo, @UserRecordType int recordType) {
            mInfo = userInfo;
            mIsStartGuestSession = isStartGuestSession;
            mIsAddUser = isAddUser;
            mIsForeground = isForeground;
            mType = recordType;
        }
    }