diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java index a3bca4c8976ec..6c86446552549 100644 --- a/services/core/java/com/android/server/pm/UserManagerService.java +++ b/services/core/java/com/android/server/pm/UserManagerService.java @@ -1365,15 +1365,20 @@ public class UserManagerService extends IUserManager.Stub { @Override public void setUserEnabled(@UserIdInt int userId) { checkManageUsersPermission("enable user"); + UserInfo info; + boolean wasUserDisabled = false; synchronized (mPackagesLock) { - UserInfo info; synchronized (mUsersLock) { info = getUserInfoLU(userId); + if (info != null && !info.isEnabled()) { + wasUserDisabled = true; + info.flags ^= UserInfo.FLAG_DISABLED; + writeUserLP(getUserDataLU(info.id)); + } } - if (info != null && !info.isEnabled()) { - info.flags ^= UserInfo.FLAG_DISABLED; - writeUserLP(getUserDataLU(info.id)); - } + } + if (wasUserDisabled && info != null && info.isProfile()) { + sendProfileAddedBroadcast(info.profileGroupId, info.id); } } @@ -4759,7 +4764,9 @@ public class UserManagerService extends IUserManager.Stub { MetricsLogger.count(mContext, userInfo.isGuest() ? TRON_GUEST_CREATED : (userInfo.isDemo() ? TRON_DEMO_CREATED : TRON_USER_CREATED), 1); - if (!userInfo.isProfile()) { + if (userInfo.isProfile()) { + sendProfileAddedBroadcast(userInfo.profileGroupId, userInfo.id); + } else { // 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) { @@ -5357,6 +5364,17 @@ public class UserManagerService extends IUserManager.Stub { } } + /** + * Send {@link Intent#ACTION_PROFILE_ADDED} broadcast when a user of type + * {@link UserInfo#isProfile()} is added. This broadcast is sent only to dynamic receivers + * created with {@link Context#registerReceiver}. + */ + private void sendProfileAddedBroadcast(int parentUserId, int addedUserId) { + sendProfileBroadcast( + new Intent(Intent.ACTION_PROFILE_ADDED), + parentUserId, addedUserId); + } + /** * Send {@link Intent#ACTION_PROFILE_REMOVED} broadcast when a user of type * {@link UserInfo#isProfile()} is removed. Additionally sends @@ -5375,12 +5393,12 @@ public class UserManagerService extends IUserManager.Stub { if (Objects.equals(userType, UserManager.USER_TYPE_PROFILE_MANAGED)) { sendManagedProfileRemovedBroadcast(parentUserId, removedUserId); } - sendProfileBroadcastToRegisteredReceivers( + sendProfileBroadcast( new Intent(Intent.ACTION_PROFILE_REMOVED), parentUserId, removedUserId); } - private void sendProfileBroadcastToRegisteredReceivers(Intent intent, + private void sendProfileBroadcast(Intent intent, int parentUserId, int userId) { final UserHandle parentHandle = UserHandle.of(parentUserId); intent.putExtra(Intent.EXTRA_USER, UserHandle.of(userId)); @@ -5391,7 +5409,7 @@ public class UserManagerService extends IUserManager.Stub { private void sendManagedProfileRemovedBroadcast(int parentUserId, int removedUserId) { Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED); - managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId)); + managedProfileIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(removedUserId)); managedProfileIntent.putExtra(Intent.EXTRA_USER_HANDLE, removedUserId); final UserHandle parentHandle = UserHandle.of(parentUserId); getDevicePolicyManagerInternal().broadcastIntentToManifestReceivers(