Prevent conversations with bots from being in the people section.

Fixes: 179786734
Test: manual w/ notify2
Test: atest NotificationRecordTest
Change-Id: I9d20daa7bccd8a05cdbeef5baf260b2333ed162f
This commit is contained in:
Jeff DeCew
2021-05-03 11:51:20 -04:00
parent b8eac19403
commit db3398f623
2 changed files with 68 additions and 3 deletions

View File

@@ -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) {
@@ -1438,8 +1439,8 @@ public final class NotificationRecord {
}
if (mTargetSdkVersion >= Build.VERSION_CODES.R
&& Notification.MessagingStyle.class.equals(notification.getNotificationStyle())
&& mShortcutInfo == null) {
&& Notification.MessagingStyle.class.equals(notification.getNotificationStyle())
&& (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;
}

View File

@@ -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();