summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--services/core/java/com/android/server/pm/BackgroundUserSoundNotifier.java15
-rw-r--r--services/tests/mockingservicestests/src/com/android/server/pm/BackgroundUserSoundNotifierTest.java34
2 files changed, 44 insertions, 5 deletions
diff --git a/services/core/java/com/android/server/pm/BackgroundUserSoundNotifier.java b/services/core/java/com/android/server/pm/BackgroundUserSoundNotifier.java
index d1d6ed0f1f99..77572e018dda 100644
--- a/services/core/java/com/android/server/pm/BackgroundUserSoundNotifier.java
+++ b/services/core/java/com/android/server/pm/BackgroundUserSoundNotifier.java
@@ -179,13 +179,17 @@ public class BackgroundUserSoundNotifier {
final String action = intent.getAction().substring(actionIndex);
Log.d(LOG_TAG, "Action requested: " + action + ", by userId "
+ ActivityManager.getCurrentUser() + " for alarm on user "
- + UserHandle.getUserHandleForUid(clientUid));
+ + UserHandle.getUserHandleForUid(clientUid).getIdentifier());
}
if (ACTION_MUTE_SOUND.equals(intent.getAction())) {
muteAlarmSounds(clientUid);
} else if (ACTION_SWITCH_USER.equals(intent.getAction())) {
- activityManager.switchUser(UserHandle.getUserId(clientUid));
+ int userId = UserHandle.getUserId(clientUid);
+ if (mUserManager.isProfile(userId)) {
+ userId = mUserManager.getProfileParent(userId).id;
+ }
+ activityManager.switchUser(userId);
}
if (Flags.multipleAlarmNotificationsSupport()) {
mNotificationClientUids.remove(clientUid);
@@ -237,11 +241,12 @@ public class BackgroundUserSoundNotifier {
UserHandle.of(ActivityManager.getCurrentUser()), 0);
final int userId = UserHandle.getUserId(afi.getClientUid());
final int usage = afi.getAttributes().getUsage();
- UserInfo userInfo = mUserManager.getUserInfo(userId);
-
+ UserInfo userInfo = mUserManager.isProfile(userId) ? mUserManager.getProfileParent(userId) :
+ mUserManager.getUserInfo(userId);
+ ActivityManager activityManager = foregroundContext.getSystemService(ActivityManager.class);
// Only show notification if the sound is coming from background user and the notification
// for this UID is not already shown.
- if (userInfo != null && userId != foregroundContext.getUserId()
+ if (userInfo != null && !activityManager.isProfileForeground(userInfo.getUserHandle())
&& !isNotificationShown(afi.getClientUid())) {
//TODO: b/349138482 - Add handling of cases when usage == USAGE_NOTIFICATION_RINGTONE
if (usage == USAGE_ALARM) {
diff --git a/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundUserSoundNotifierTest.java b/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundUserSoundNotifierTest.java
index bf946a1258fd..3d0c63780ef3 100644
--- a/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundUserSoundNotifierTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundUserSoundNotifierTest.java
@@ -103,6 +103,7 @@ public class BackgroundUserSoundNotifierTest {
assumeTrue(UserManager.supportsMultipleUsers());
AudioAttributes aa = new AudioAttributes.Builder().setUsage(USAGE_ALARM).build();
UserInfo user = createUser("User", UserManager.USER_TYPE_FULL_SECONDARY, 0);
+
final int fgUserId = mSpiedContext.getUserId();
final int bgUserUid = user.id * 100000;
doReturn(UserHandle.of(fgUserId)).when(mSpiedContext).getUser();
@@ -209,6 +210,28 @@ public class BackgroundUserSoundNotifierTest {
eq(UserHandle.of(fgUserId)));
}
+ @Test
+ public void testOnAudioFocusGrant_alarmOnProfileOfForegroundUser_foregroundUserNotNotified() {
+ assumeTrue(UserManager.supportsMultipleUsers());
+ final int fgUserId = mSpiedContext.getUserId();
+ UserInfo fgUserProfile = createProfileForUser("Background profile",
+ UserManager.USER_TYPE_PROFILE_MANAGED, fgUserId, null);
+ assumeTrue("Cannot add a profile", fgUserProfile != null);
+ int fgUserProfileUid = fgUserProfile.id * 100_000;
+
+ AudioAttributes aa = new AudioAttributes.Builder().setUsage(USAGE_ALARM).build();
+ AudioFocusInfo afi = new AudioFocusInfo(aa, fgUserProfileUid, "", "",
+ AudioManager.AUDIOFOCUS_GAIN, 0, 0, Build.VERSION.SDK_INT);
+
+ mBackgroundUserSoundNotifier.getAudioPolicyFocusListener()
+ .onAudioFocusGrant(afi, AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
+
+ verify(mNotificationManager, never())
+ .notifyAsUser(eq(BackgroundUserSoundNotifier.class.getSimpleName()),
+ eq(afi.getClientUid()), any(Notification.class),
+ eq(UserHandle.of(fgUserId)));
+ }
+
@Test
public void testCreateNotification_UserSwitcherEnabled_bothActionsAvailable() {
@@ -327,6 +350,17 @@ public class BackgroundUserSoundNotifierTest {
}
return user;
}
+
+ private UserInfo createProfileForUser(String name, String userType, int userHandle,
+ String[] disallowedPackages) {
+ UserInfo profile = mUserManager.createProfileForUser(
+ name, userType, 0, userHandle, disallowedPackages);
+ if (profile != null) {
+ mUsersToRemove.add(profile.id);
+ }
+ return profile;
+ }
+
private void removeUser(int userId) {
mUserManager.removeUser(userId);
}