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

Commit 8646e2c0 authored by Mady Mellor's avatar Mady Mellor
Browse files

Add hasSentValidBubble to NotificationManagerService

If apps have conversations but not bubbles, we still show bubble settings
for them which is confusing for users. This change exposes whether an app
has sent a notification with valid bubble metadata, this allows us to hide
the setting until a bubble is actually sent.

If the app directs the user to bubble settings via the public intent,
they are still available.

Test: atest PreferencesHelper
Bug: 178387292
Change-Id: I7d3d92df49e7f4ed1d2c05cbba5f6f7786789d32
parent cb2fb682
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ interface INotificationManager
    boolean isInInvalidMsgState(String pkg, int uid);
    boolean hasUserDemotedInvalidMsgApp(String pkg, int uid);
    void setInvalidMsgAppDemoted(String pkg, int uid, boolean isDemoted);
    boolean hasSentValidBubble(String pkg, int uid);
    void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled);
    /**
     * Updates the notification's enabled state. Additionally locks importance for all of the
+13 −0
Original line number Diff line number Diff line
@@ -3094,6 +3094,13 @@ public class NotificationManagerService extends SystemService {
                if (mPreferencesHelper.setValidMessageSent(
                        r.getSbn().getPackageName(), r.getUid())) {
                    handleSavePolicyFile();
                } else if (r.getNotification().getBubbleMetadata() != null) {
                    // If bubble metadata is present it is valid (if invalid it's removed
                    // via BubbleExtractor).
                    if (mPreferencesHelper.setValidBubbleSent(
                            r.getSbn().getPackageName(), r.getUid())) {
                        handleSavePolicyFile();
                    }
                }
            } else {
                if (mPreferencesHelper.setInvalidMessageSent(
@@ -3596,6 +3603,12 @@ public class NotificationManagerService extends SystemService {
            handleSavePolicyFile();
        }

        @Override
        public boolean hasSentValidBubble(String pkg, int uid) {
            checkCallerIsSystem();
            return mPreferencesHelper.hasSentValidBubble(pkg, uid);
        }

        @Override
        public void setNotificationDelegate(String callingPkg, String delegate) {
            checkCallerIsSameApp(callingPkg);
+19 −1
Original line number Diff line number Diff line
@@ -808,6 +808,23 @@ public class PreferencesHelper implements RankingConfig {
        }
    }

    /** Sets whether this package has sent a notification with valid bubble metadata. */
    public boolean setValidBubbleSent(String packageName, int uid) {
        synchronized (mPackagePreferences) {
            PackagePreferences r = getOrCreatePackagePreferencesLocked(packageName, uid);
            boolean valueChanged = !r.hasSentValidBubble;
            r.hasSentValidBubble = true;
            return valueChanged;
        }
    }

    boolean hasSentValidBubble(String packageName, int uid) {
        synchronized (mPackagePreferences) {
            PackagePreferences r = getOrCreatePackagePreferencesLocked(packageName, uid);
            return r.hasSentValidBubble;
        }
    }

    @Override
    public boolean isGroupBlocked(String packageName, int uid, String groupId) {
        if (groupId == null) {
@@ -2813,8 +2830,9 @@ public class PreferencesHelper implements RankingConfig {

        boolean hasSentInvalidMessage = false;
        boolean hasSentValidMessage = false;
        // notE: only valid while hasSentMessage is false and hasSentInvalidMessage is true
        // note: only valid while hasSentMessage is false and hasSentInvalidMessage is true
        boolean userDemotedMsgApp = false;
        boolean hasSentValidBubble = false;

        Delegate delegate = null;
        ArrayMap<String, NotificationChannel> channels = new ArrayMap<>();
+14 −0
Original line number Diff line number Diff line
@@ -542,6 +542,7 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        mHelper.setInvalidMessageSent(PKG_P, UID_P);
        mHelper.setValidMessageSent(PKG_P, UID_P);
        mHelper.setInvalidMsgAppDemoted(PKG_P, UID_P, true);
        mHelper.setValidBubbleSent(PKG_P, UID_P);

        mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_NONE);

@@ -561,6 +562,7 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        assertFalse(mHelper.hasSentInvalidMsg(PKG_N_MR1, UID_N_MR1));
        assertTrue(mHelper.hasSentValidMsg(PKG_P, UID_P));
        assertTrue(mHelper.didUserEverDemoteInvalidMsgApp(PKG_P, UID_P));
        assertTrue(mHelper.hasSentValidBubble(PKG_P, UID_P));
        assertEquals(channel1,
                mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false));
        compareChannels(channel2,
@@ -4926,6 +4928,18 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        assertFalse(mHelper.hasUserDemotedInvalidMsgApp(PKG_P, UID_P));
    }

    @Test
    public void testValidBubbleSent() {
        // create package preferences
        mHelper.canShowBadge(PKG_P, UID_P);
        // false by default
        assertFalse(mHelper.hasSentValidBubble(PKG_P, UID_P));

        // set something valid was sent
        mHelper.setValidBubbleSent(PKG_P, UID_P);
        assertTrue(mHelper.hasSentValidBubble(PKG_P, UID_P));
    }

    @Test
    public void testPullPackageChannelPreferencesStats() {
        String channelId = "parent";