Update logic about parent and convo channels

To handle the case where an app sends a notification directly
to a custom channel, and to properly show priority markings
on conversation tiles

Test: atest; manually ensure convo tiles are marked as priority
Fixes: 184709662
Change-Id: I8f9b6169943da5670bc09fd16bbe692fd43d2cf6
This commit is contained in:
Julia Reynolds
2021-06-08 15:11:09 -04:00
parent 894c5d3d73
commit 99a5975af8
5 changed files with 210 additions and 97 deletions

View File

@@ -36,8 +36,8 @@ public final class ConversationChannel implements Parcelable {
private ShortcutInfo mShortcutInfo;
private int mUid;
private NotificationChannel mParentNotificationChannel;
private NotificationChannelGroup mParentNotificationChannelGroup;
private NotificationChannel mNotificationChannel;
private NotificationChannelGroup mNotificationChannelGroup;
private long mLastEventTimestamp;
private boolean mHasActiveNotifications;
private boolean mHasBirthdayToday;
@@ -61,8 +61,8 @@ public final class ConversationChannel implements Parcelable {
boolean hasActiveNotifications) {
mShortcutInfo = shortcutInfo;
mUid = uid;
mParentNotificationChannel = parentNotificationChannel;
mParentNotificationChannelGroup = parentNotificationChannelGroup;
mNotificationChannel = parentNotificationChannel;
mNotificationChannelGroup = parentNotificationChannelGroup;
mLastEventTimestamp = lastEventTimestamp;
mHasActiveNotifications = hasActiveNotifications;
}
@@ -74,8 +74,8 @@ public final class ConversationChannel implements Parcelable {
List<ConversationStatus> statuses) {
mShortcutInfo = shortcutInfo;
mUid = uid;
mParentNotificationChannel = parentNotificationChannel;
mParentNotificationChannelGroup = parentNotificationChannelGroup;
mNotificationChannel = parentNotificationChannel;
mNotificationChannelGroup = parentNotificationChannelGroup;
mLastEventTimestamp = lastEventTimestamp;
mHasActiveNotifications = hasActiveNotifications;
mHasBirthdayToday = hasBirthdayToday;
@@ -85,8 +85,8 @@ public final class ConversationChannel implements Parcelable {
public ConversationChannel(Parcel in) {
mShortcutInfo = in.readParcelable(ShortcutInfo.class.getClassLoader());
mUid = in.readInt();
mParentNotificationChannel = in.readParcelable(NotificationChannel.class.getClassLoader());
mParentNotificationChannelGroup =
mNotificationChannel = in.readParcelable(NotificationChannel.class.getClassLoader());
mNotificationChannelGroup =
in.readParcelable(NotificationChannelGroup.class.getClassLoader());
mLastEventTimestamp = in.readLong();
mHasActiveNotifications = in.readBoolean();
@@ -104,8 +104,8 @@ public final class ConversationChannel implements Parcelable {
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(mShortcutInfo, flags);
dest.writeInt(mUid);
dest.writeParcelable(mParentNotificationChannel, flags);
dest.writeParcelable(mParentNotificationChannelGroup, flags);
dest.writeParcelable(mNotificationChannel, flags);
dest.writeParcelable(mNotificationChannelGroup, flags);
dest.writeLong(mLastEventTimestamp);
dest.writeBoolean(mHasActiveNotifications);
dest.writeBoolean(mHasBirthdayToday);
@@ -120,12 +120,12 @@ public final class ConversationChannel implements Parcelable {
return mUid;
}
public NotificationChannel getParentNotificationChannel() {
return mParentNotificationChannel;
public NotificationChannel getNotificationChannel() {
return mNotificationChannel;
}
public NotificationChannelGroup getParentNotificationChannelGroup() {
return mParentNotificationChannelGroup;
public NotificationChannelGroup getNotificationChannelGroup() {
return mNotificationChannelGroup;
}
public long getLastEventTimestamp() {
@@ -149,4 +149,18 @@ public final class ConversationChannel implements Parcelable {
public @Nullable List<ConversationStatus> getStatuses() {
return mStatuses;
}
@Override
public String toString() {
return "ConversationChannel{" +
"mShortcutInfo=" + mShortcutInfo +
", mUid=" + mUid +
", mNotificationChannel=" + mNotificationChannel +
", mNotificationChannelGroup=" + mNotificationChannelGroup +
", mLastEventTimestamp=" + mLastEventTimestamp +
", mHasActiveNotifications=" + mHasActiveNotifications +
", mHasBirthdayToday=" + mHasBirthdayToday +
", mStatuses=" + mStatuses +
'}';
}
}

View File

@@ -307,10 +307,10 @@ public class PeopleSpaceTile implements Parcelable {
mContactUri = getContactUri(info);
mStatuses = channel.getStatuses();
mLastInteractionTimestamp = channel.getLastEventTimestamp();
mIsImportantConversation = channel.getParentNotificationChannel() != null
&& channel.getParentNotificationChannel().isImportantConversation();
mCanBypassDnd = channel.getParentNotificationChannel() != null
&& channel.getParentNotificationChannel().canBypassDnd();
mIsImportantConversation = channel.getNotificationChannel() != null
&& channel.getNotificationChannel().isImportantConversation();
mCanBypassDnd = channel.getNotificationChannel() != null
&& channel.getNotificationChannel().canBypassDnd();
mNotificationPolicyState = SHOW_CONVERSATIONS;
}

View File

@@ -675,7 +675,7 @@ public class PeopleSpaceWidgetManager {
updatedTile.setUserIcon(icon);
}
if (DEBUG) Log.d(TAG, "Statuses: " + conversation.getStatuses().toString());
NotificationChannel channel = conversation.getParentNotificationChannel();
NotificationChannel channel = conversation.getNotificationChannel();
if (channel != null) {
if (DEBUG) Log.d(TAG, "Important:" + channel.isImportantConversation());
updatedTile.setIsImportantConversation(channel.isImportantConversation());

View File

@@ -254,6 +254,18 @@ public class DataManager {
return null;
}
ConversationInfo getConversationInfo(String packageName, int userId, String shortcutId) {
UserData userData = getUnlockedUserData(userId);
if (userData != null) {
PackageData packageData = userData.getPackageData(packageName);
// App may have been uninstalled.
if (packageData != null) {
return packageData.getConversationInfo(shortcutId);
}
}
return null;
}
@Nullable
private ConversationChannel getConversationChannel(String packageName, int userId,
String shortcutId, ConversationInfo conversationInfo) {
@@ -277,7 +289,7 @@ public class DataManager {
int uid = mPackageManagerInternal.getPackageUid(packageName, 0, userId);
NotificationChannel parentChannel =
mNotificationManagerInternal.getNotificationChannel(packageName, uid,
conversationInfo.getParentNotificationChannelId());
conversationInfo.getNotificationChannelId());
NotificationChannelGroup parentChannelGroup = null;
if (parentChannel != null) {
parentChannelGroup =
@@ -302,7 +314,7 @@ public class DataManager {
String shortcutId = conversationInfo.getShortcutId();
ConversationChannel channel = getConversationChannel(packageData.getPackageName(),
packageData.getUserId(), shortcutId, conversationInfo);
if (channel == null || channel.getParentNotificationChannel() == null) {
if (channel == null || channel.getNotificationChannel() == null) {
return;
}
conversationChannels.add(channel);
@@ -791,8 +803,8 @@ public class DataManager {
private boolean isCachedRecentConversation(ConversationInfo conversationInfo) {
return conversationInfo.isShortcutCachedForNotification()
&& conversationInfo.getNotificationChannelId() == null
&& conversationInfo.getParentNotificationChannelId() != null
&& Objects.equals(conversationInfo.getNotificationChannelId(),
conversationInfo.getParentNotificationChannelId())
&& conversationInfo.getLastEventTimestamp() > 0L;
}
@@ -910,7 +922,7 @@ public class DataManager {
}
@VisibleForTesting
NotificationListenerService getNotificationListenerServiceForTesting(@UserIdInt int userId) {
NotificationListener getNotificationListenerServiceForTesting(@UserIdInt int userId) {
return mNotificationListeners.get(userId);
}
@@ -1132,7 +1144,7 @@ public class DataManager {
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
public void onNotificationPosted(StatusBarNotification sbn, RankingMap map) {
if (sbn.getUser().getIdentifier() != mUserId) {
return;
}
@@ -1145,16 +1157,22 @@ public class DataManager {
});
if (packageData != null) {
Ranking rank = new Ranking();
map.getRanking(sbn.getKey(), rank);
ConversationInfo conversationInfo = packageData.getConversationInfo(shortcutId);
if (conversationInfo == null) {
return;
}
if (DEBUG) Log.d(TAG, "Last event from notification: " + sbn.getPostTime());
ConversationInfo updated = new ConversationInfo.Builder(conversationInfo)
ConversationInfo.Builder updated = new ConversationInfo.Builder(conversationInfo)
.setLastEventTimestamp(sbn.getPostTime())
.setParentNotificationChannelId(sbn.getNotification().getChannelId())
.build();
packageData.getConversationStore().addOrUpdate(updated);
.setNotificationChannelId(rank.getChannel().getId());
if (!TextUtils.isEmpty(rank.getChannel().getParentChannelId())) {
updated.setParentNotificationChannelId(rank.getChannel().getParentChannelId());
} else {
updated.setParentNotificationChannelId(sbn.getNotification().getChannelId());
}
packageData.getConversationStore().addOrUpdate(updated.build());
EventHistoryImpl eventHistory = packageData.getEventStore().getOrCreateEventHistory(
EventStore.CATEGORY_SHORTCUT_BASED, shortcutId);

View File

@@ -134,6 +134,8 @@ public final class DataManagerTest {
private static final String NOTIFICATION_CHANNEL_ID = "test : sc";
private static final String PARENT_NOTIFICATION_CHANNEL_ID = "test";
private static final long MILLIS_PER_MINUTE = 1000L * 60L;
private static final String GENERIC_KEY = "key";
private static final String CUSTOM_KEY = "custom";
@Mock
private Context mContext;
@@ -158,7 +160,11 @@ public final class DataManagerTest {
@Mock
private JobScheduler mJobScheduler;
@Mock
private StatusBarNotification mStatusBarNotification;
private StatusBarNotification mGenericSbn;
@Mock
private StatusBarNotification mConvoSbn;
@Mock
private NotificationListenerService.RankingMap mRankingMap;
@Mock
private Notification mNotification;
@Mock
@@ -202,8 +208,6 @@ public final class DataManagerTest {
mParentNotificationChannel = new NotificationChannel(
PARENT_NOTIFICATION_CHANNEL_ID, "test channel",
NotificationManager.IMPORTANCE_DEFAULT);
when(mNotificationManagerInternal.getNotificationChannel(anyString(), anyInt(),
anyString())).thenReturn(mParentNotificationChannel);
when(mContext.getContentResolver()).thenReturn(mContentResolver);
when(mContext.getMainLooper()).thenReturn(Looper.getMainLooper());
@@ -246,16 +250,26 @@ public final class DataManagerTest {
when(mPackageManager.getPackageUidAsUser(TEST_PKG_NAME, USER_ID_PRIMARY))
.thenReturn(TEST_PKG_UID);
when(mStatusBarNotification.getNotification()).thenReturn(mNotification);
when(mStatusBarNotification.getPackageName()).thenReturn(TEST_PKG_NAME);
when(mStatusBarNotification.getUser()).thenReturn(UserHandle.of(USER_ID_PRIMARY));
when(mStatusBarNotification.getPostTime()).thenReturn(System.currentTimeMillis());
when(mNotification.getShortcutId()).thenReturn(TEST_SHORTCUT_ID);
when(mNotification.getChannelId()).thenReturn(PARENT_NOTIFICATION_CHANNEL_ID);
mNotificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "test channel", NotificationManager.IMPORTANCE_DEFAULT);
mNotificationChannel.setConversationId("test", TEST_SHORTCUT_ID);
mNotificationChannel.setConversationId(PARENT_NOTIFICATION_CHANNEL_ID, TEST_SHORTCUT_ID);
when(mNotificationManagerInternal.getNotificationChannel(anyString(), anyInt(),
eq(mNotificationChannel.getId()))).thenReturn(mNotificationChannel);
when(mNotificationManagerInternal.getNotificationChannel(anyString(), anyInt(),
eq(mParentNotificationChannel.getId()))).thenReturn(mParentNotificationChannel);
when(mGenericSbn.getKey()).thenReturn(GENERIC_KEY);
when(mGenericSbn.getNotification()).thenReturn(mNotification);
when(mGenericSbn.getPackageName()).thenReturn(TEST_PKG_NAME);
when(mGenericSbn.getUser()).thenReturn(UserHandle.of(USER_ID_PRIMARY));
when(mGenericSbn.getPostTime()).thenReturn(System.currentTimeMillis());
when(mConvoSbn.getKey()).thenReturn(CUSTOM_KEY);
when(mConvoSbn.getNotification()).thenReturn(mNotification);
when(mConvoSbn.getPackageName()).thenReturn(TEST_PKG_NAME);
when(mConvoSbn.getUser()).thenReturn(UserHandle.of(USER_ID_PRIMARY));
when(mConvoSbn.getPostTime()).thenReturn(System.currentTimeMillis());
when(mNotification.getShortcutId()).thenReturn(TEST_SHORTCUT_ID);
mCancellationSignal = new CancellationSignal();
@@ -449,10 +463,7 @@ public final class DataManagerTest {
buildPerson());
mDataManager.addOrUpdateConversationInfo(shortcut);
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
List<Range<Long>> activeNotificationOpenTimeSlots = getActiveSlotsForTestShortcut(
Event.NOTIFICATION_EVENT_TYPES);
@@ -471,7 +482,7 @@ public final class DataManagerTest {
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationRemoved(mStatusBarNotification, null,
listenerService.onNotificationRemoved(mGenericSbn, null,
NotificationListenerService.REASON_CLICK);
List<Range<Long>> activeNotificationOpenTimeSlots = getActiveSlotsForTestShortcut(
@@ -495,20 +506,20 @@ public final class DataManagerTest {
shortcut.setCached(ShortcutInfo.FLAG_CACHED_NOTIFICATIONS);
mDataManager.addOrUpdateConversationInfo(shortcut);
when(mNotification.getShortcutId()).thenReturn(shortcutId);
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
}
// Post another notification for the last conversation.
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
// Removing one of the two notifications does not un-cache the shortcut.
listenerService.onNotificationRemoved(mStatusBarNotification, null,
listenerService.onNotificationRemoved(mGenericSbn, null,
NotificationListenerService.REASON_CANCEL);
verify(mShortcutServiceInternal, never()).uncacheShortcuts(
anyInt(), any(), anyString(), any(), anyInt(), anyInt());
// Removing the second notification un-caches the shortcut.
listenerService.onNotificationRemoved(mStatusBarNotification, null,
listenerService.onNotificationRemoved(mGenericSbn, null,
NotificationListenerService.REASON_CANCEL_ALL);
verify(mShortcutServiceInternal).uncacheShortcuts(
anyInt(), any(), eq(TEST_PKG_NAME), anyList(), eq(USER_ID_PRIMARY),
@@ -526,7 +537,7 @@ public final class DataManagerTest {
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
shortcut.setCached(ShortcutInfo.FLAG_CACHED_NOTIFICATIONS);
mDataManager.addOrUpdateConversationInfo(shortcut);
@@ -626,19 +637,13 @@ public final class DataManagerTest {
buildPerson());
mDataManager.addOrUpdateConversationInfo(shortcut);
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
sendConvoNotification();
shortcut.setCached(ShortcutInfo.FLAG_CACHED_NOTIFICATIONS);
mDataManager.addOrUpdateConversationInfo(shortcut);
assertThat(mDataManager.getConversation(TEST_PKG_NAME, USER_ID_PRIMARY,
TEST_SHORTCUT_ID)).isNotNull();
listenerService.onNotificationChannelModified(TEST_PKG_NAME, UserHandle.of(USER_ID_PRIMARY),
mNotificationChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED);
ConversationChannel result = mDataManager.getConversation(TEST_PKG_NAME, USER_ID_PRIMARY,
TEST_SHORTCUT_ID);
assertThat(result).isNotNull();
@@ -661,9 +666,7 @@ public final class DataManagerTest {
assertThat(mDataManager.getConversation(TEST_PKG_NAME, USER_ID_PRIMARY,
TEST_SHORTCUT_ID + "1")).isNull();
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
sendConvoNotification();
ConversationStatus cs = new ConversationStatus.Builder("id", ACTIVITY_ANNIVERSARY).build();
mDataManager.addOrUpdateStatus(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID, cs);
@@ -673,14 +676,50 @@ public final class DataManagerTest {
assertEquals(shortcut.getId(), result.getShortcutInfo().getId());
assertEquals(1, result.getShortcutInfo().getPersons().length);
assertEquals(CONTACT_URI, result.getShortcutInfo().getPersons()[0].getUri());
assertEquals(mNotificationChannel.getId(), result.getNotificationChannel().getId());
assertEquals(mParentNotificationChannel.getId(),
result.getParentNotificationChannel().getId());
assertEquals(mStatusBarNotification.getPostTime(), result.getLastEventTimestamp());
result.getNotificationChannel().getParentChannelId());
assertEquals(mConvoSbn.getPostTime(), result.getLastEventTimestamp());
assertTrue(result.hasActiveNotifications());
assertFalse(result.hasBirthdayToday());
assertThat(result.getStatuses()).containsExactly(cs);
}
@Test
public void testOnNotificationChannelModified() {
mDataManager.onUserUnlocked(USER_ID_PRIMARY);
assertThat(mDataManager.getConversation(TEST_PKG_NAME, USER_ID_PRIMARY,
TEST_SHORTCUT_ID)).isNull();
ShortcutInfo shortcut = buildShortcutInfo(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID,
buildPerson());
shortcut.setCached(ShortcutInfo.FLAG_PINNED);
mDataManager.addOrUpdateConversationInfo(shortcut);
sendConvoNotification();
ConversationChannel result = mDataManager.getConversation(TEST_PKG_NAME, USER_ID_PRIMARY,
TEST_SHORTCUT_ID);
assertFalse(result.getNotificationChannel().canBubble());
NotificationChannel updated = new NotificationChannel(mNotificationChannel.getId(),
mNotificationChannel.getDescription(), mNotificationChannel.getImportance());
updated.setConversationId(mNotificationChannel.getParentChannelId(),
mNotificationChannel.getConversationId());
updated.setAllowBubbles(true);
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationChannelModified(TEST_PKG_NAME, UserHandle.of(USER_ID_PRIMARY),
updated, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED);
ConversationInfo ci = mDataManager.getConversationInfo(TEST_PKG_NAME, USER_ID_PRIMARY,
TEST_SHORTCUT_ID);
assertThat(ci).isNotNull();
assertEquals(mNotificationChannel.getId(), ci.getNotificationChannelId());
assertEquals(mParentNotificationChannel.getId(), ci.getParentNotificationChannelId());
assertTrue(ci.isBubbled());
}
@Test
public void testGetConversation_demoted() {
mDataManager.onUserUnlocked(USER_ID_PRIMARY);
@@ -713,11 +752,8 @@ public final class DataManagerTest {
shortcut.setCached(ShortcutInfo.FLAG_PINNED);
mDataManager.addOrUpdateConversationInfo(shortcut);
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
ConversationChannel result = mDataManager.getConversation(TEST_PKG_NAME, USER_ID_PRIMARY,
TEST_SHORTCUT_ID);
sendGenericNotification();
mDataManager.getConversation(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID);
verify(mShortcutServiceInternal).getShortcuts(
anyInt(), anyString(), anyLong(), anyString(), anyList(), any(), any(),
@@ -1085,7 +1121,7 @@ public final class DataManagerTest {
shortcut.setCached(ShortcutInfo.FLAG_CACHED_NOTIFICATIONS);
mDataManager.addOrUpdateConversationInfo(shortcut);
when(mNotification.getShortcutId()).thenReturn(shortcutId);
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
}
mDataManager.pruneDataForUser(USER_ID_PRIMARY, mCancellationSignal);
@@ -1107,9 +1143,9 @@ public final class DataManagerTest {
shortcut.setCached(ShortcutInfo.FLAG_CACHED_NOTIFICATIONS);
mDataManager.addOrUpdateConversationInfo(shortcut);
when(mNotification.getShortcutId()).thenReturn(shortcutId);
when(mStatusBarNotification.getPostTime()).thenReturn(100L + i);
listenerService.onNotificationPosted(mStatusBarNotification);
listenerService.onNotificationRemoved(mStatusBarNotification, null,
when(mGenericSbn.getPostTime()).thenReturn(100L + i);
sendGenericNotification();
listenerService.onNotificationRemoved(mGenericSbn, null,
NotificationListenerService.REASON_CANCEL);
}
@@ -1162,9 +1198,7 @@ public final class DataManagerTest {
shortcut.setCached(ShortcutInfo.FLAG_CACHED_NOTIFICATIONS);
mDataManager.addOrUpdateConversationInfo(shortcut);
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
List<ConversationChannel> result = mDataManager.getRecentConversations(USER_ID_PRIMARY);
assertEquals(1, result.size());
@@ -1172,8 +1206,9 @@ public final class DataManagerTest {
assertEquals(1, result.get(0).getShortcutInfo().getPersons().length);
assertEquals(CONTACT_URI, result.get(0).getShortcutInfo().getPersons()[0].getUri());
assertEquals(mParentNotificationChannel.getId(),
result.get(0).getParentNotificationChannel().getId());
assertEquals(mStatusBarNotification.getPostTime(), result.get(0).getLastEventTimestamp());
result.get(0).getNotificationChannel().getId());
assertEquals(null, result.get(0).getNotificationChannel().getParentChannelId());
assertEquals(mGenericSbn.getPostTime(), result.get(0).getLastEventTimestamp());
assertTrue(result.get(0).hasActiveNotifications());
}
@@ -1186,11 +1221,9 @@ public final class DataManagerTest {
shortcut.setCached(ShortcutInfo.FLAG_CACHED_NOTIFICATIONS);
mDataManager.addOrUpdateConversationInfo(shortcut);
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
List<ConversationChannel> result = mDataManager.getRecentConversations(USER_ID_PRIMARY);
mDataManager.getRecentConversations(USER_ID_PRIMARY);
verify(mShortcutServiceInternal).getShortcuts(
anyInt(), anyString(), anyLong(), anyString(), anyList(), any(), any(),
@@ -1211,8 +1244,8 @@ public final class DataManagerTest {
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
when(mNotification.getShortcutId()).thenReturn(TEST_SHORTCUT_ID);
listenerService.onNotificationPosted(mStatusBarNotification);
listenerService.onNotificationRemoved(mStatusBarNotification, null,
sendGenericNotification();
listenerService.onNotificationRemoved(mGenericSbn, null,
NotificationListenerService.REASON_CLICK);
mDataManager.pruneOldRecentConversations(USER_ID_PRIMARY,
@@ -1231,11 +1264,9 @@ public final class DataManagerTest {
buildPerson());
mDataManager.addOrUpdateConversationInfo(shortcut);
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
assertEquals(mStatusBarNotification.getPostTime(),
assertEquals(mGenericSbn.getPostTime(),
mDataManager.getLastInteraction(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID));
assertEquals(0L,
mDataManager.getLastInteraction("not_test_pkg", USER_ID_PRIMARY, TEST_SHORTCUT_ID));
@@ -1375,9 +1406,7 @@ public final class DataManagerTest {
TEST_SHORTCUT_ID, buildPerson());
mDataManager.addOrUpdateConversationInfo(shortcut);
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
List<ConversationChannel> result = mDataManager.getRecentConversations(USER_ID_PRIMARY);
assertTrue(result.isEmpty());
@@ -1395,7 +1424,7 @@ public final class DataManagerTest {
// Post a notification and customize the notification settings.
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
listenerService.onNotificationChannelModified(TEST_PKG_NAME, UserHandle.of(USER_ID_PRIMARY),
mNotificationChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED);
@@ -1414,7 +1443,7 @@ public final class DataManagerTest {
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
// posting updates the last interaction time, so delay before deletion
try {
Thread.sleep(500);
@@ -1422,7 +1451,7 @@ public final class DataManagerTest {
e.printStackTrace();
}
long approxDeletionTime = System.currentTimeMillis();
listenerService.onNotificationRemoved(mStatusBarNotification, null,
listenerService.onNotificationRemoved(mGenericSbn, null,
NotificationListenerService.REASON_CANCEL);
ConversationInfo conversationInfo = mDataManager.getPackage(TEST_PKG_NAME, USER_ID_PRIMARY)
@@ -1442,8 +1471,8 @@ public final class DataManagerTest {
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
listenerService.onNotificationRemoved(mStatusBarNotification, null,
sendGenericNotification();
listenerService.onNotificationRemoved(mGenericSbn, null,
NotificationListenerService.REASON_CANCEL);
mDataManager.removeRecentConversation(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID,
USER_ID_PRIMARY);
@@ -1472,14 +1501,14 @@ public final class DataManagerTest {
// Post a notification and then dismiss it for conversation #1.
when(mNotification.getShortcutId()).thenReturn("1");
listenerService.onNotificationPosted(mStatusBarNotification);
listenerService.onNotificationRemoved(mStatusBarNotification, null,
sendGenericNotification();
listenerService.onNotificationRemoved(mGenericSbn, null,
NotificationListenerService.REASON_CANCEL);
// Post a notification for conversation #2, but don't dismiss it. Its shortcut won't be
// uncached when removeAllRecentConversations() is called.
when(mNotification.getShortcutId()).thenReturn("2");
listenerService.onNotificationPosted(mStatusBarNotification);
sendGenericNotification();
mDataManager.removeAllRecentConversations(USER_ID_PRIMARY);
@@ -1562,6 +1591,58 @@ public final class DataManagerTest {
return (queryFlags & flag) != 0;
}
// "Sends" a notification to a non-customized notification channel - the notification channel
// is something generic like "messages" and the notification has a shortcut id
private void sendGenericNotification() {
when(mNotification.getChannelId()).thenReturn(PARENT_NOTIFICATION_CHANNEL_ID);
doAnswer(invocationOnMock -> {
NotificationListenerService.Ranking ranking = (NotificationListenerService.Ranking)
invocationOnMock.getArguments()[1];
ranking.populate(
(String) invocationOnMock.getArguments()[0],
0,
false,
0,
0,
mParentNotificationChannel.getImportance(),
null, null,
mParentNotificationChannel, null, null, true, 0, false, -1, false, null, null,
false, false, false, null, 0, false);
return true;
}).when(mRankingMap).getRanking(eq(GENERIC_KEY),
any(NotificationListenerService.Ranking.class));
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mGenericSbn, mRankingMap);
}
// "Sends" a notification to a customized notification channel - the notification channel
// is specific to a person, and the channel has a convo id matching the notification's shortcut
// and the channel has a parent channel id
private void sendConvoNotification() {
when(mNotification.getChannelId()).thenReturn(NOTIFICATION_CHANNEL_ID);
doAnswer(invocationOnMock -> {
NotificationListenerService.Ranking ranking = (NotificationListenerService.Ranking)
invocationOnMock.getArguments()[1];
ranking.populate(
(String) invocationOnMock.getArguments()[0],
0,
false,
0,
0,
mNotificationChannel.getImportance(),
null, null,
mNotificationChannel, null, null, true, 0, false, -1, false, null, null, false,
false, false, null, 0, false);
return true;
}).when(mRankingMap).getRanking(eq(CUSTOM_KEY),
any(NotificationListenerService.Ranking.class));
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mConvoSbn, mRankingMap);
}
private class TestContactsQueryHelper extends ContactsQueryHelper {
private Uri mContactUri;