diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java index 2a6d6ed7d3be1..7845200f4bf7c 100644 --- a/core/java/android/os/UserManager.java +++ b/core/java/android/os/UserManager.java @@ -4102,12 +4102,25 @@ public class UserManager { } /** - * Returns true if the user switcher should be shown, this will be if device supports multi-user - * and there are at least 2 users available that are not managed profiles. - * @hide + * Returns true if the user switcher should be shown. + * I.e., returns whether the user switcher is enabled and there is something actionable to show. + * * @return true if user switcher should be shown. + * @hide */ public boolean isUserSwitcherEnabled() { + return isUserSwitcherEnabled(false); + } + + /** + * Returns true if the user switcher should be shown. + * + * @param showEvenIfNotActionable value to return if the feature is enabled but there is nothing + * actionable for the user to do anyway + * @return true if user switcher should be shown. + * @hide + */ + public boolean isUserSwitcherEnabled(boolean showEvenIfNotActionable) { if (!supportsMultipleUsers()) { return false; } @@ -4118,15 +4131,26 @@ public class UserManager { if (isDeviceInDemoMode(mContext)) { return false; } - // If user disabled this feature, don't show switcher - final boolean userSwitcherEnabled = Settings.Global.getInt(mContext.getContentResolver(), - Settings.Global.USER_SWITCHER_ENABLED, 1) != 0; - if (!userSwitcherEnabled) { + // Check the Settings.Global.USER_SWITCHER_ENABLED that the user can toggle on/off. + final boolean userSwitcherSettingOn = Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.USER_SWITCHER_ENABLED, + Resources.getSystem().getBoolean(R.bool.config_showUserSwitcherByDefault) ? 1 : 0) + != 0; + if (!userSwitcherSettingOn) { return false; } - List users = getUsers(true); + + // The feature is enabled. But is it worth showing? + return showEvenIfNotActionable + || areThereUsersToWhichToSwitch() // There are switchable users. + || !hasUserRestriction(UserManager.DISALLOW_ADD_USER); // New users can be added. + } + + /** Returns whether there are any users (other than the current user) to which to switch. */ + private boolean areThereUsersToWhichToSwitch() { + final List users = getUsers(true); if (users == null) { - return false; + return false; } int switchableUserCount = 0; for (UserInfo user : users) { @@ -4134,9 +4158,7 @@ public class UserManager { ++switchableUserCount; } } - final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class) - .getGuestUserDisabled(null); - return switchableUserCount > 1 || guestEnabled; + return switchableUserCount > 1; } /** diff --git a/core/res/res/values-sw600dp/config.xml b/core/res/res/values-sw600dp/config.xml index 368e307f9ff56..34b6a54be4931 100644 --- a/core/res/res/values-sw600dp/config.xml +++ b/core/res/res/values-sw600dp/config.xml @@ -48,5 +48,8 @@ true + + + true diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml index 2dc3996e3d7bb..9959de8aa6452 100644 --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -4444,6 +4444,9 @@ false + + false + false diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index e01a90fdb9d36..29dbe5a95c8cd 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -4007,6 +4007,9 @@ + + + diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/MultiUserSwitch.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/MultiUserSwitch.java index dcc31075a2aca..67b7e979f62d2 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/MultiUserSwitch.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/MultiUserSwitch.java @@ -18,10 +18,8 @@ package com.android.systemui.statusbar.phone; import static com.android.systemui.DejankUtils.whitelistIpcs; -import android.app.admin.DevicePolicyManager; import android.content.Context; import android.os.UserManager; -import android.provider.Settings; import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; @@ -97,33 +95,9 @@ public class MultiUserSwitch extends FrameLayout implements View.OnClickListener } public boolean isMultiUserEnabled() { - // Short-circuiting from UserManager. Needs to be extracted because of SystemUI boolean flag - // qs_show_user_switcher_for_single_user - // TODO(b/138661450) Move IPC calls to background - return whitelistIpcs(() -> { - // The default in UserManager is to show the switcher. We want to not show it unless the - // user explicitly requests it in Settings - final boolean userSwitcherEnabled = Settings.Global.getInt( - mContext.getContentResolver(), - Settings.Global.USER_SWITCHER_ENABLED, 0) != 0; - - if (!userSwitcherEnabled - || !UserManager.supportsMultipleUsers() - || UserManager.isDeviceInDemoMode(mContext) - || mUserManager.hasUserRestriction(UserManager.DISALLOW_USER_SWITCH)) { - return false; - } - - final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class) - .getGuestUserDisabled(null); - return mUserSwitcherController.getSwitchableUserCount() > 1 - // If we cannot add guests even if they are enabled, do not show - || (guestEnabled && !mUserManager.hasUserRestriction( - UserManager.DISALLOW_ADD_USER)) - || mContext.getResources().getBoolean( - R.bool.qs_show_user_switcher_for_single_user); - }); + return whitelistIpcs(() -> mUserManager.isUserSwitcherEnabled( + mContext.getResources().getBoolean(R.bool.qs_show_user_switcher_for_single_user))); } private void registerListener() { @@ -175,7 +149,7 @@ public class MultiUserSwitch extends FrameLayout implements View.OnClickListener private void refreshContentDescription() { String currentUser = null; // TODO(b/138661450) - if (whitelistIpcs(mUserManager::isUserSwitcherEnabled) + if (whitelistIpcs(() -> mUserManager.isUserSwitcherEnabled()) && mUserSwitcherController != null) { currentUser = mUserSwitcherController.getCurrentUserName(mContext); } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java index 412962cc797ab..db00770f0638d 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java @@ -409,18 +409,6 @@ public class UserSwitcherController implements Dumpable { Log.e(TAG, "Couldn't switch to user, id=" + userId); } - public int getSwitchableUserCount() { - int count = 0; - final int N = mUsers.size(); - for (int i = 0; i < N; ++i) { - UserRecord record = mUsers.get(i); - if (record.info != null && record.info.supportsSwitchToByUser()) { - count++; - } - } - return count; - } - protected void switchToUserId(int id) { try { pauseRefreshUsers(); diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java index 29428a27e6da0..b0e3ecb6d17b2 100644 --- a/services/core/java/com/android/server/pm/UserManagerService.java +++ b/services/core/java/com/android/server/pm/UserManagerService.java @@ -3507,7 +3507,7 @@ public class UserManagerService extends IUserManager.Stub { Slog.w(LOG_TAG, "could not start pre-created user " + userId, e); } } else { - dispatchUserAddedIntent(userInfo); + dispatchUserAdded(userInfo); } } finally { @@ -3568,7 +3568,7 @@ public class UserManagerService extends IUserManager.Stub { // Could not read the existing permissions, re-grant them. mPm.onNewUserCreated(preCreatedUser.id); } - dispatchUserAddedIntent(preCreatedUser); + dispatchUserAdded(preCreatedUser); return preCreatedUser; } @@ -3600,7 +3600,7 @@ public class UserManagerService extends IUserManager.Stub { return (now > EPOCH_PLUS_30_YEARS) ? now : 0; } - private void dispatchUserAddedIntent(@NonNull UserInfo userInfo) { + private void dispatchUserAdded(@NonNull UserInfo userInfo) { Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id); // Also, add the UserHandle for mainline modules which can't use the @hide @@ -3610,6 +3610,15 @@ public class UserManagerService extends IUserManager.Stub { android.Manifest.permission.MANAGE_USERS); MetricsLogger.count(mContext, userInfo.isGuest() ? TRON_GUEST_CREATED : (userInfo.isDemo() ? TRON_DEMO_CREATED : TRON_USER_CREATED), 1); + + if (!userInfo.isProfile()) { + // If the user switch hasn't been explicitly toggled on or off by the user, turn it on. + if (android.provider.Settings.Global.getString(mContext.getContentResolver(), + android.provider.Settings.Global.USER_SWITCHER_ENABLED) == null) { + android.provider.Settings.Global.putInt(mContext.getContentResolver(), + android.provider.Settings.Global.USER_SWITCHER_ENABLED, 1); + } + } } /**