From 7575e053b28eada3ccb76ee245a5ff09f41afeea Mon Sep 17 00:00:00 2001 From: Will Brockman Date: Tue, 16 Jun 2020 15:05:12 -0400 Subject: [PATCH] Statsd log for app notification bans. I accidentally omitted logging for the event of banning/unbanning notifications for the entire app, when I added notification channel ban logging in ag/10450892. Change-Id: Ibf2be1367bf41ad2b431cfcd2fef820ff270fed9 Test: atest PreferencesHelperTest Bug: 156398451 --- .../NotificationChannelLogger.java | 28 +++++++++++++++++-- .../NotificationChannelLoggerImpl.java | 9 ++++++ .../notification/PreferencesHelper.java | 1 + .../NotificationChannelLoggerFake.java | 5 ++++ .../notification/PreferencesHelperTest.java | 8 ++++++ 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/notification/NotificationChannelLogger.java b/services/core/java/com/android/server/notification/NotificationChannelLogger.java index a7b18778f8681..5c127c31d6c2c 100644 --- a/services/core/java/com/android/server/notification/NotificationChannelLogger.java +++ b/services/core/java/com/android/server/notification/NotificationChannelLogger.java @@ -98,6 +98,16 @@ public interface NotificationChannelLogger { channelGroup, uid, pkg, false); } + /** + * Log blocking or unblocking of the entire app's notifications. + * @param uid UID of the app. + * @param pkg Package name of the app. + * @param enabled If true, notifications are now allowed. + */ + default void logAppNotificationsAllowed(int uid, String pkg, boolean enabled) { + logAppEvent(NotificationChannelEvent.getBlocked(enabled), uid, pkg); + } + /** * Low-level interface for logging events, to be implemented. * @param event Event to log. @@ -123,6 +133,13 @@ public interface NotificationChannelLogger { @NonNull NotificationChannelGroup channelGroup, int uid, String pkg, boolean wasBlocked); + /** + * Low-level interface for logging app-as-a-whole events, to be implemented. + * @param uid UID of app. + * @param pkg Package of app. + */ + void logAppEvent(@NonNull NotificationChannelEvent event, int uid, String pkg); + /** * The UiEvent enums that this class can log. */ @@ -144,8 +161,11 @@ public interface NotificationChannelLogger { @UiEvent(doc = "System created a new conversation (sub-channel in a notification channel)") NOTIFICATION_CHANNEL_CONVERSATION_CREATED(272), @UiEvent(doc = "System deleted a new conversation (sub-channel in a notification channel)") - NOTIFICATION_CHANNEL_CONVERSATION_DELETED(274); - + NOTIFICATION_CHANNEL_CONVERSATION_DELETED(274), + @UiEvent(doc = "All notifications for the app were blocked.") + APP_NOTIFICATIONS_BLOCKED(557), + @UiEvent(doc = "Notifications for the app as a whole were unblocked.") + APP_NOTIFICATIONS_UNBLOCKED(558); private final int mId; NotificationChannelEvent(int id) { @@ -178,6 +198,10 @@ public interface NotificationChannelLogger { ? NotificationChannelEvent.NOTIFICATION_CHANNEL_GROUP_CREATED : NotificationChannelEvent.NOTIFICATION_CHANNEL_GROUP_DELETED; } + + public static NotificationChannelEvent getBlocked(boolean enabled) { + return enabled ? APP_NOTIFICATIONS_UNBLOCKED : APP_NOTIFICATIONS_BLOCKED; + } } /** diff --git a/services/core/java/com/android/server/notification/NotificationChannelLoggerImpl.java b/services/core/java/com/android/server/notification/NotificationChannelLoggerImpl.java index 2f7772eec2d25..fd3dd568f6341 100644 --- a/services/core/java/com/android/server/notification/NotificationChannelLoggerImpl.java +++ b/services/core/java/com/android/server/notification/NotificationChannelLoggerImpl.java @@ -19,6 +19,8 @@ package com.android.server.notification; import android.app.NotificationChannel; import android.app.NotificationChannelGroup; +import com.android.internal.logging.UiEventLogger; +import com.android.internal.logging.UiEventLoggerImpl; import com.android.internal.util.FrameworkStatsLog; /** @@ -27,6 +29,8 @@ import com.android.internal.util.FrameworkStatsLog; * should live in the interface so it can be tested. */ public class NotificationChannelLoggerImpl implements NotificationChannelLogger { + UiEventLogger mUiEventLogger = new UiEventLoggerImpl(); + @Override public void logNotificationChannel(NotificationChannelEvent event, NotificationChannel channel, int uid, String pkg, @@ -51,4 +55,9 @@ public class NotificationChannelLoggerImpl implements NotificationChannelLogger /* int old_importance*/ NotificationChannelLogger.getImportance(wasBlocked), /* int importance*/ NotificationChannelLogger.getImportance(channelGroup)); } + + @Override + public void logAppEvent(NotificationChannelEvent event, int uid, String pkg) { + mUiEventLogger.log(event, uid, pkg); + } } diff --git a/services/core/java/com/android/server/notification/PreferencesHelper.java b/services/core/java/com/android/server/notification/PreferencesHelper.java index e472e30977778..afc75572ae4f5 100644 --- a/services/core/java/com/android/server/notification/PreferencesHelper.java +++ b/services/core/java/com/android/server/notification/PreferencesHelper.java @@ -1654,6 +1654,7 @@ public class PreferencesHelper implements RankingConfig { } setImportance(packageName, uid, enabled ? DEFAULT_IMPORTANCE : IMPORTANCE_NONE); + mNotificationChannelLogger.logAppNotificationsAllowed(uid, packageName, enabled); } /** diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationChannelLoggerFake.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationChannelLoggerFake.java index b6ea063ccc146..f609306e44b09 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationChannelLoggerFake.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationChannelLoggerFake.java @@ -51,4 +51,9 @@ public class NotificationChannelLoggerFake implements NotificationChannelLogger NotificationChannelGroup channelGroup, int uid, String pkg, boolean wasBlocked) { mCalls.add(new CallRecord(event)); } + + @Override + public void logAppEvent(NotificationChannelEvent event, int uid, String pkg) { + mCalls.add(new CallRecord(event)); + } } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java index 622a203c52428..2e49929ec0327 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java @@ -2265,6 +2265,14 @@ public class PreferencesHelperTest extends UiServiceTestCase { assertEquals(3, mHelper.getBlockedAppCount(0)); } + @Test + public void testAppBlockedLogging() { + mHelper.setEnabled(PKG_N_MR1, 1020, false); + assertEquals(1, mLogger.getCalls().size()); + assertEquals( + NotificationChannelLogger.NotificationChannelEvent.APP_NOTIFICATIONS_BLOCKED, + mLogger.get(0).event); + } @Test public void testXml_statusBarIcons_default() throws Exception { String preQXml = "\n"