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

Commit dcf26f19 authored by Matías Hernández's avatar Matías Hernández
Browse files

Return default notification policy when calling from a user with no ZenModeConfig

Although this should be super rare, it can happen more often (with modes_multiuser) if we never got an onUserSwitched event for that user. Most callsites don't expect to get a null Policy, so even though null is technically allowed, returning _something_ is safer.

Bug: 323163267
Test: atest ZenModeHelperTest
Flag: android.app.modes_multiuser
Change-Id: Ie9ec86037f79bbc0bfe87679b7791979fdc2532d
parent 63034454
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -1941,9 +1941,19 @@ public class ZenModeHelper {
    @Nullable
    public Policy getNotificationPolicy(UserHandle user) {
        synchronized (mConfigLock) {
            if (Flags.modesMultiuser()) {
                // Return a fallback (default) policy for users without a zen config.
                // Note that zen updates (setPolicy, setFilter) won't be applied, so this is mostly
                // about preventing NPEs for careless callers.
                ZenModeConfig config = getConfigLocked(user);
                return config != null
                        ? getNotificationPolicy(config)
                        : getNotificationPolicy(mDefaultConfig);
            } else {
                return getNotificationPolicy(getConfigLocked(user));
            }
        }
    }

    @Nullable
    private static Policy getNotificationPolicy(@Nullable ZenModeConfig config) {
+14 −0
Original line number Diff line number Diff line
@@ -7363,6 +7363,20 @@ public class ZenModeHelperTest extends UiServiceTestCase {
        verify(callback, never()).onZenModeChanged();
    }

    @Test
    @EnableFlags(FLAG_MODES_MULTIUSER)
    public void getNotificationPolicy_fromUserWithoutZenConfig_returnsDefaultPolicy() {
        // Set a custom policy for the current user to double check we return a default one below.
        mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, new Policy(0, 0, 0), ORIGIN_SYSTEM,
                SYSTEM_UID);

        Policy ghostPolicy = mZenModeHelper.getNotificationPolicy(UserHandle.of(5552368));

        assertThat(ghostPolicy).isNotNull();
        assertThat(ZenAdapters.notificationPolicyToZenPolicy(ghostPolicy))
                .isEqualTo(mZenModeHelper.getDefaultZenPolicy());
    }

    private static void addZenRule(ZenModeConfig config, String id, String ownerPkg, int zenMode,
            @Nullable ZenPolicy zenPolicy) {
        ZenRule rule = new ZenRule();