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

Commit 0582bb68 authored by Alejandro Nijamkin's avatar Alejandro Nijamkin
Browse files

Cleans up UserSwitcherController API.

* Removes unused method and classes
* Makes externally-unused methods non-public

Test: Built and ran System UI, manually interacted with user switcher UI
through the lock-screen and full-screen experiences
Bug: 246631653

Change-Id: I0d6a8e4ffc899f358533486d8c4b0d38de6e5519
parent 488a69bf
Loading
Loading
Loading
Loading
+0 −62
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.statusbar;

import android.content.Context;
import android.content.DialogInterface;

import com.android.systemui.R;
import com.android.systemui.statusbar.phone.SystemUIDialog;
import com.android.systemui.statusbar.policy.UserSwitcherController;

public class UserUtil {
    public static void deleteUserWithPrompt(Context context, int userId,
                                            UserSwitcherController userSwitcherController) {
        new RemoveUserDialog(context, userId, userSwitcherController).show();
    }

    private final static class RemoveUserDialog extends SystemUIDialog implements
            DialogInterface.OnClickListener {

        private final int mUserId;
        private final UserSwitcherController mUserSwitcherController;

        public RemoveUserDialog(Context context, int userId,
                                UserSwitcherController userSwitcherController) {
            super(context);
            setTitle(R.string.user_remove_user_title);
            setMessage(context.getString(R.string.user_remove_user_message));
            setButton(DialogInterface.BUTTON_NEUTRAL,
                    context.getString(android.R.string.cancel), this);
            setButton(DialogInterface.BUTTON_POSITIVE,
                    context.getString(R.string.user_remove_user_remove), this);
            setCanceledOnTouchOutside(false);
            mUserId = userId;
            mUserSwitcherController = userSwitcherController;
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == BUTTON_NEUTRAL) {
                cancel();
            } else {
                dismiss();
                mUserSwitcherController.removeUserId(mUserId);
            }
        }
    }
}
+17 −35
Original line number Diff line number Diff line
@@ -158,7 +158,6 @@ public class UserSwitcherController implements Dumpable {
    @VisibleForTesting
    Dialog mAddUserDialog;
    private int mLastNonGuestUser = UserHandle.USER_SYSTEM;
    private boolean mResumeUserOnGuestLogout = true;
    private boolean mSimpleUserSwitcher;
    // When false, there won't be any visual affordance to add a new user from the keyguard even if
    // the user is unlocked
@@ -431,38 +430,41 @@ public class UserSwitcherController implements Dumpable {
        });
    }

    boolean systemCanCreateUsers() {
    private boolean systemCanCreateUsers() {
        return !mUserManager.hasBaseUserRestriction(
                UserManager.DISALLOW_ADD_USER, UserHandle.SYSTEM);
    }

    boolean currentUserCanCreateUsers() {
    private boolean currentUserCanCreateUsers() {
        UserInfo currentUser = mUserTracker.getUserInfo();
        return currentUser != null
                && (currentUser.isAdmin() || mUserTracker.getUserId() == UserHandle.USER_SYSTEM)
                && systemCanCreateUsers();
    }

    boolean anyoneCanCreateUsers() {
    private boolean anyoneCanCreateUsers() {
        return systemCanCreateUsers() && mAddUsersFromLockScreen.getValue();
    }

    @VisibleForTesting
    boolean canCreateGuest(boolean hasExistingGuest) {
        return mUserSwitcherEnabled
                && (currentUserCanCreateUsers() || anyoneCanCreateUsers())
                && !hasExistingGuest;
    }

    @VisibleForTesting
    boolean canCreateUser() {
        return mUserSwitcherEnabled
                && (currentUserCanCreateUsers() || anyoneCanCreateUsers())
                && mUserManager.canAddMoreUsers(UserManager.USER_TYPE_FULL_SECONDARY);
    }

    boolean createIsRestricted() {
    private boolean createIsRestricted() {
        return !mAddUsersFromLockScreen.getValue();
    }

    @VisibleForTesting
    boolean canCreateSupervisedUser() {
        return !TextUtils.isEmpty(mCreateSupervisedUserPackage) && canCreateUser();
    }
@@ -489,30 +491,14 @@ public class UserSwitcherController implements Dumpable {
        return mSimpleUserSwitcher;
    }

    public void setResumeUserOnGuestLogout(boolean resume) {
        mResumeUserOnGuestLogout = resume;
    }

    /**
     * Returns whether the current user is a system user.
     */
    public boolean isSystemUser() {
    @VisibleForTesting
    boolean isSystemUser() {
        return mUserTracker.getUserId() == UserHandle.USER_SYSTEM;
    }

    public void removeUserId(int userId) {
        if (userId == UserHandle.USER_SYSTEM) {
            Log.w(TAG, "User " + userId + " could not removed.");
            return;
        }
        if (mUserTracker.getUserId() == userId) {
            switchToUserId(UserHandle.USER_SYSTEM);
        }
        if (mUserManager.removeUser(userId)) {
            refreshUsers(UserHandle.USER_NULL);
        }
    }

    /**
     * @return UserRecord for the current user
     */
@@ -604,7 +590,7 @@ public class UserSwitcherController implements Dumpable {
        switchToUserId(id);
    }

    protected void switchToUserId(int id) {
    private void switchToUserId(int id) {
        try {
            if (mView != null) {
                mInteractionJankMonitor.begin(InteractionJankMonitor.Configuration.Builder
@@ -621,7 +607,7 @@ public class UserSwitcherController implements Dumpable {

    private void showExitGuestDialog(int id, boolean isGuestEphemeral, DialogShower dialogShower) {
        int newId = UserHandle.USER_SYSTEM;
        if (mResumeUserOnGuestLogout && mLastNonGuestUser != UserHandle.USER_SYSTEM) {
        if (mLastNonGuestUser != UserHandle.USER_SYSTEM) {
            UserInfo info = mUserManager.getUserInfo(mLastNonGuestUser);
            if (info != null && info.isEnabled() && info.supportsSwitchToByUser()) {
                newId = info.id;
@@ -818,12 +804,10 @@ public class UserSwitcherController implements Dumpable {
        refreshUsers(UserHandle.USER_ALL);
    }

    @VisibleForTesting
    public void addAdapter(WeakReference<BaseUserAdapter> adapter) {
        mAdapters.add(adapter);
    }

    @VisibleForTesting
    public ArrayList<UserRecord> getUsers() {
        return mUsers;
    }
@@ -921,7 +905,7 @@ public class UserSwitcherController implements Dumpable {
        int newUserId = UserHandle.USER_SYSTEM;
        if (targetUserId == UserHandle.USER_NULL) {
            // when target user is not specified switch to last non guest user
            if (mResumeUserOnGuestLogout && mLastNonGuestUser != UserHandle.USER_SYSTEM) {
            if (mLastNonGuestUser != UserHandle.USER_SYSTEM) {
                UserInfo info = mUserManager.getUserInfo(mLastNonGuestUser);
                if (info != null && info.isEnabled() && info.supportsSwitchToByUser()) {
                    newUserId = info.id;
@@ -1014,7 +998,7 @@ public class UserSwitcherController implements Dumpable {
     * @return The multi-user user ID of the newly created guest user, or
     * {@link UserHandle#USER_NULL} if the guest couldn't be created.
     */
    public @UserIdInt int createGuest() {
    private @UserIdInt int createGuest() {
        UserInfo guest;
        try {
            guest = mUserManager.createGuest(mContext);
@@ -1036,9 +1020,9 @@ public class UserSwitcherController implements Dumpable {
        mView = view;
    }

    @VisibleForTesting
    public KeyguardStateController getKeyguardStateController() {
        return mKeyguardStateController;
    /** Returns {@code true} if the keyguard is showing; {@code false} otherwise */
    public boolean isKeyguardShowing() {
        return mKeyguardStateController.isShowing();
    }

    /**
@@ -1059,11 +1043,9 @@ public class UserSwitcherController implements Dumpable {
    public static abstract class BaseUserAdapter extends BaseAdapter {

        final UserSwitcherController mController;
        private final KeyguardStateController mKeyguardStateController;

        protected BaseUserAdapter(UserSwitcherController controller) {
            mController = controller;
            mKeyguardStateController = controller.getKeyguardStateController();
            controller.addAdapter(new WeakReference<>(this));
        }

@@ -1081,7 +1063,7 @@ public class UserSwitcherController implements Dumpable {
        }

        private int countUsers(boolean includeGuest) {
            boolean keyguardShowing = mKeyguardStateController.isShowing();
            boolean keyguardShowing = mController.isKeyguardShowing();
            final int userSize = getUsers().size();
            int count = 0;
            for (int i = 0; i < userSize; i++) {
+1 −3
Original line number Diff line number Diff line
@@ -116,9 +116,7 @@ public class KeyguardSecurityContainerTest extends SysuiTestCase {
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        when(mUserSwitcherController.getCurrentUserName()).thenReturn("Test User");
        when(mUserSwitcherController.getKeyguardStateController())
                .thenReturn(mKeyguardStateController);
        when(mKeyguardStateController.isShowing()).thenReturn(true);
        when(mUserSwitcherController.isKeyguardShowing()).thenReturn(true);

        mScreenWidth = getUiDevice().getDisplayWidth();
        mFakeMeasureSpec = View
+2 −3
Original line number Diff line number Diff line
@@ -38,9 +38,9 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations

@SmallTest
@@ -102,8 +102,7 @@ class KeyguardQsUserSwitchControllerTest : SysuiTestCase() {

        ViewUtils.attachView(view)
        testableLooper.processAllMessages()
        `when`(userSwitcherController.keyguardStateController).thenReturn(keyguardStateController)
        `when`(userSwitcherController.keyguardStateController.isShowing).thenReturn(true)
        `when`(userSwitcherController.isKeyguardShowing).thenReturn(true)
        `when`(keyguardStateController.isShowing).thenReturn(true)
        `when`(keyguardStateController.isKeyguardGoingAway).thenReturn(false)
        keyguardQsUserSwitchController.init()