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

Commit e304aa57 authored by Tetiana Meronyk's avatar Tetiana Meronyk Committed by Android (Google) Code Review
Browse files

Merge "Add check if the profile belongs to foreground user when showing notification." into main

parents b81e388d ba078dd0
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -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) {
+34 −0
Original line number Diff line number Diff line
@@ -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);
    }