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

Commit 6a895fce authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Prevent conversations with bots from being in the people section." into sc-dev

parents 969a1a86 db3398f6
Loading
Loading
Loading
Loading
+26 −3
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.app.ActivityManager;
import android.app.IActivityManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.Person;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.Context;
@@ -556,7 +557,7 @@ public final class NotificationRecord {
        pw.println(prefix + "bigContentView=" + formatRemoteViews(notification.bigContentView));
        pw.println(prefix + "headsUpContentView="
                + formatRemoteViews(notification.headsUpContentView));
        pw.print(prefix + String.format("color=0x%08x", notification.color));
        pw.println(prefix + String.format("color=0x%08x", notification.color));
        pw.println(prefix + "timeout="
                + TimeUtils.formatForLogging(notification.getTimeoutAfter()));
        if (notification.actions != null && notification.actions.length > 0) {
@@ -1439,7 +1440,7 @@ public final class NotificationRecord {

        if (mTargetSdkVersion >= Build.VERSION_CODES.R
                && Notification.MessagingStyle.class.equals(notification.getNotificationStyle())
            && mShortcutInfo == null) {
                && (mShortcutInfo == null || isOnlyBots(mShortcutInfo.getPersons()))) {
            return false;
        }
        if (mHasSentValidMsg && mShortcutInfo == null) {
@@ -1448,6 +1449,28 @@ public final class NotificationRecord {
        return true;
    }

    /**
     * Determines if the {@link ShortcutInfo#getPersons()} array includes only bots, for the purpose
     * of excluding that shortcut from the "conversations" section of the notification shade.  If
     * the shortcut has no people, this returns false to allow the conversation into the shade, and
     * if there is any non-bot person we allow it as well.  Otherwise, this is only bots and will
     * not count as a conversation.
     */
    private boolean isOnlyBots(Person[] persons) {
        // Return false if there are no persons at all
        if (persons == null || persons.length == 0) {
            return false;
        }
        // Return false if there are any non-bot persons
        for (Person person : persons) {
            if (!person.isBot()) {
                return false;
            }
        }
        // Return true otherwise
        return true;
    }

    StatusBarNotification getSbn() {
        return sbn;
    }
+42 −0
Original line number Diff line number Diff line
@@ -1133,6 +1133,48 @@ public class NotificationRecordTest extends UiServiceTestCase {
        assertEquals(FLAG_FILTER_TYPE_CONVERSATIONS, record.getNotificationType());
    }

    @Test
    public void testIsConversation_shortcutHasOneBot_targetsR() {
        StatusBarNotification sbn = getMessagingStyleNotification(PKG_R);
        NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
        ShortcutInfo shortcutMock = mock(ShortcutInfo.class);
        when(shortcutMock.getPersons()).thenReturn(new Person[]{
                new Person.Builder().setName("Bot").setBot(true).build()
        });
        record.setShortcutInfo(shortcutMock);

        assertFalse(record.isConversation());
    }

    @Test
    public void testIsConversation_shortcutHasOnePerson_targetsR() {
        StatusBarNotification sbn = getMessagingStyleNotification(PKG_R);
        NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
        ShortcutInfo shortcutMock = mock(ShortcutInfo.class);
        when(shortcutMock.getPersons()).thenReturn(new Person[]{
                new Person.Builder().setName("Person").setBot(false).build()
        });
        record.setShortcutInfo(shortcutMock);

        assertTrue(record.isConversation());
        assertEquals(FLAG_FILTER_TYPE_CONVERSATIONS, record.getNotificationType());
    }

    @Test
    public void testIsConversation_shortcutHasOneBotOnePerson_targetsR() {
        StatusBarNotification sbn = getMessagingStyleNotification(PKG_R);
        NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
        ShortcutInfo shortcutMock = mock(ShortcutInfo.class);
        when(shortcutMock.getPersons()).thenReturn(new Person[]{
                new Person.Builder().setName("Bot").setBot(true).build(),
                new Person.Builder().setName("Person").setBot(false).build()
        });
        record.setShortcutInfo(shortcutMock);

        assertTrue(record.isConversation());
        assertEquals(FLAG_FILTER_TYPE_CONVERSATIONS, record.getNotificationType());
    }

    @Test
    public void testIsConversation_noShortcut() {
        StatusBarNotification sbn = getMessagingStyleNotification();