Listen to the notification posted events from NotificationListenerService instead

UsageStats NOTIFICATION_INTERRUPTION events are only created if a
conversation has its own conversation-specific notification channel
created. Switch to NotificationListenerService so that we could get the notification posted events from all conversations.

Bug: 146522621
Test: atest com.android.server.people.data.DataManagerTest
Test: atest com.android.server.people.data.UsageStatsQueryHelperTest
Change-Id: Iaa120567891144a952707deb5cea1321aa28341c
This commit is contained in:
Danning Chen
2020-02-14 10:36:10 -08:00
parent 94580df0a8
commit 7af601e8ed
4 changed files with 31 additions and 39 deletions

View File

@@ -639,6 +639,14 @@ public class DataManager {
/** Listener for the notifications and their settings changes. */
private class NotificationListener extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
EventHistoryImpl eventHistory = getEventHistoryIfEligible(sbn);
if (eventHistory != null) {
eventHistory.addEvent(new Event(sbn.getPostTime(), Event.TYPE_NOTIFICATION_POSTED));
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap,
int reason) {

View File

@@ -80,10 +80,6 @@ class UsageStatsQueryHelper {
addEventByShortcutId(packageData, e.getShortcutId(),
new Event(e.getTimeStamp(), Event.TYPE_SHORTCUT_INVOCATION));
break;
case UsageEvents.Event.NOTIFICATION_INTERRUPTION:
addEventByNotificationChannelId(packageData, e.getNotificationChannelId(),
new Event(e.getTimeStamp(), Event.TYPE_NOTIFICATION_POSTED));
break;
case UsageEvents.Event.LOCUS_ID_SET:
onInAppConversationEnded(packageData, e);
LocusId locusId = e.getLocusId() != null ? new LocusId(e.getLocusId()) : null;
@@ -142,17 +138,4 @@ class UsageStatsQueryHelper {
EventStore.CATEGORY_LOCUS_ID_BASED, locusId.getId());
eventHistory.addEvent(event);
}
private void addEventByNotificationChannelId(PackageData packageData,
String notificationChannelId, Event event) {
ConversationInfo conversationInfo =
packageData.getConversationStore().getConversationByNotificationChannelId(
notificationChannelId);
if (conversationInfo == null) {
return;
}
EventHistoryImpl eventHistory = packageData.getEventStore().getOrCreateEventHistory(
EventStore.CATEGORY_SHORTCUT_BASED, conversationInfo.getShortcutId());
eventHistory.addEvent(event);
}
}

View File

@@ -184,6 +184,7 @@ public final class DataManagerTest {
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);
mNotificationChannel = new NotificationChannel(
@@ -324,6 +325,28 @@ public final class DataManagerTest {
assertEquals(newPhoneNumber, conversations.get(0).getContactPhoneNumber());
}
@Test
public void testNotificationPosted() {
mDataManager.onUserUnlocked(USER_ID_PRIMARY);
ShortcutInfo shortcut = buildShortcutInfo(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID,
buildPerson());
mDataManager.onShortcutAddedOrUpdated(shortcut);
NotificationListenerService listenerService =
mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY);
listenerService.onNotificationPosted(mStatusBarNotification);
List<Range<Long>> activeNotificationOpenTimeSlots = new ArrayList<>();
mDataManager.forAllPackages(packageData ->
activeNotificationOpenTimeSlots.addAll(
packageData.getEventHistory(TEST_SHORTCUT_ID)
.getEventIndex(Event.TYPE_NOTIFICATION_POSTED)
.getActiveTimeSlots()));
assertEquals(1, activeNotificationOpenTimeSlots.size());
}
@Test
public void testNotificationOpened() {
mDataManager.onUserUnlocked(USER_ID_PRIMARY);

View File

@@ -59,7 +59,6 @@ public final class UsageStatsQueryHelperTest {
private static final String PKG_NAME = "pkg";
private static final String ACTIVITY_NAME = "TestActivity";
private static final String SHORTCUT_ID = "abc";
private static final String NOTIFICATION_CHANNEL_ID = "test : abc";
private static final LocusId LOCUS_ID_1 = new LocusId("locus_1");
private static final LocusId LOCUS_ID_2 = new LocusId("locus_2");
@@ -83,7 +82,6 @@ public final class UsageStatsQueryHelperTest {
scheduledExecutorService, testDir, helper);
mPackageData.mConversationStore.mConversationInfo = new ConversationInfo.Builder()
.setShortcutId(SHORTCUT_ID)
.setNotificationChannelId(NOTIFICATION_CHANNEL_ID)
.setLocusId(LOCUS_ID_1)
.build();
@@ -113,19 +111,6 @@ public final class UsageStatsQueryHelperTest {
assertEquals(expectedEvent, events.get(0));
}
@Test
public void testQueryNotificationInterruptionEvent() {
addUsageEvents(createNotificationInterruptionEvent(100L));
assertTrue(mHelper.querySince(50L));
assertEquals(100L, mHelper.getLastEventTimestamp());
Event expectedEvent = new Event(100L, Event.TYPE_NOTIFICATION_POSTED);
List<Event> events = mPackageData.mEventStore.mShortcutEventHistory.queryEvents(
Event.ALL_EVENT_TYPES, 0L, Long.MAX_VALUE);
assertEquals(1, events.size());
assertEquals(expectedEvent, events.get(0));
}
@Test
public void testInAppConversationSwitch() {
addUsageEvents(
@@ -203,13 +188,6 @@ public final class UsageStatsQueryHelperTest {
return e;
}
private static UsageEvents.Event createNotificationInterruptionEvent(long timestamp) {
UsageEvents.Event e = createUsageEvent(UsageEvents.Event.NOTIFICATION_INTERRUPTION,
timestamp);
e.mNotificationChannelId = NOTIFICATION_CHANNEL_ID;
return e;
}
private static UsageEvents.Event createLocusIdSetEvent(long timestamp, String locusId) {
UsageEvents.Event e = createUsageEvent(UsageEvents.Event.LOCUS_ID_SET, timestamp);
e.mClass = ACTIVITY_NAME;