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

Commit cfd53d2c authored by Beverly's avatar Beverly Committed by Beverly Tai
Browse files

Always update userId when updating bypassDnd

The information was sometimes stale causing tests to fail.

Test: atest AudioManagerTest
Test: atest PreferencesHelperTest
Fixes: 209498360
Change-Id: I13d87cf0a4871ddc06632748082ed995ca25131e
parent d7c5ca76
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -1775,7 +1775,6 @@ public class NotificationManagerService extends SystemService {
                    mConditionProviders.onUserSwitched(userId);
                    mListeners.onUserSwitched(userId);
                    mZenModeHelper.onUserSwitched(userId);
                    mPreferencesHelper.onUserSwitched(userId);
                }
                // assistant is the only thing that cares about managed profiles specifically
                mAssistants.onUserSwitched(userId);
@@ -1805,7 +1804,6 @@ public class NotificationManagerService extends SystemService {
                    mConditionProviders.onUserUnlocked(userId);
                    mListeners.onUserUnlocked(userId);
                    mZenModeHelper.onUserUnlocked(userId);
                    mPreferencesHelper.onUserUnlocked(userId);
                }
            }
        }
+10 −20
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ParceledListSlice;
import android.metrics.LogMaker;
import android.os.Binder;
import android.os.Build;
import android.os.UserHandle;
import android.provider.Settings;
@@ -190,8 +191,6 @@ public class PreferencesHelper implements RankingConfig {

    private Map<String, List<String>> mOemLockedApps = new HashMap();

    private int mCurrentUserId = UserHandle.USER_NULL;

    public PreferencesHelper(Context context, PackageManager pm, RankingHandler rankingHandler,
            ZenModeHelper zenHelper, PermissionHelper permHelper,
            NotificationChannelLogger notificationChannelLogger,
@@ -215,7 +214,6 @@ public class PreferencesHelper implements RankingConfig {
        updateBadgingEnabled();
        updateBubblesEnabled();
        updateMediaNotificationFilteringEnabled();
        mCurrentUserId = ActivityManager.getCurrentUser();
        syncChannelsBypassingDnd();
    }

@@ -1762,12 +1760,13 @@ public class PreferencesHelper implements RankingConfig {
    private void updateChannelsBypassingDnd() {
        ArraySet<Pair<String, Integer>> candidatePkgs = new ArraySet<>();

        final int currentUserId = getCurrentUser();
        synchronized (mPackagePreferences) {
            final int numPackagePreferences = mPackagePreferences.size();
            for (int i = 0; i < numPackagePreferences; i++) {
                final PackagePreferences r = mPackagePreferences.valueAt(i);
                // Package isn't associated with the current userId
                if (mCurrentUserId != UserHandle.getUserId(r.uid)) {
                if (currentUserId != UserHandle.getUserId(r.uid)) {
                    continue;
                }

@@ -1804,6 +1803,13 @@ public class PreferencesHelper implements RankingConfig {
        }
    }

    private int getCurrentUser() {
        final long identity = Binder.clearCallingIdentity();
        int currentUserId = ActivityManager.getCurrentUser();
        Binder.restoreCallingIdentity(identity);
        return currentUserId;
    }

    private boolean channelIsLiveLocked(PackagePreferences pkgPref, NotificationChannel channel) {
        // Channel is in a group that's blocked
        if (isGroupBlocked(pkgPref.pkg, pkgPref.uid, channel.getGroup())) {
@@ -2509,22 +2515,6 @@ public class PreferencesHelper implements RankingConfig {
        return packageChannels;
    }

    /**
     * Called when user switches
     */
    public void onUserSwitched(int userId) {
        mCurrentUserId = userId;
        syncChannelsBypassingDnd();
    }

    /**
     * Called when user is unlocked
     */
    public void onUserUnlocked(int userId) {
        mCurrentUserId = userId;
        syncChannelsBypassingDnd();
    }

    public void onUserRemoved(int userId) {
        synchronized (mPackagePreferences) {
            int N = mPackagePreferences.size();
+0 −18
Original line number Diff line number Diff line
@@ -1800,24 +1800,6 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        assertEquals(0, mHelper.getBlockedChannelCount("pkg2", UID_O));
    }

    @Test
    public void testUpdateChannelsBypassingDnd_onUserSwitch_onUserUnlocked() throws Exception {
        int user = USER.getIdentifier();
        NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
        NotificationChannel channel1 = new NotificationChannel("id1", "name1",
                NotificationManager.IMPORTANCE_MAX);
        channel1.setBypassDnd(true);
        channel1.setGroup(ncg.getId());

        // channel is associated with a group, then group is deleted
        mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg,  /* fromTargetApp */ true);
        mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true);
        mHelper.deleteNotificationChannelGroup(PKG_N_MR1, user, ncg.getId());

        mHelper.onUserSwitched(user);
        mHelper.onUserUnlocked(user);
    }

    @Test
    public void testGetChannelsBypassingDndCount_noChannelsBypassing() throws Exception {
        assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1,