Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationUmaTracker.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationUmaTracker.java b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationUmaTracker.java |
| index bf85281f2e046702ca76fd62ef7e6315521fcf6c..baf653180ea92da5d74fdd9dfbcb3c7a71780337 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationUmaTracker.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationUmaTracker.java |
| @@ -4,22 +4,30 @@ |
| package org.chromium.chrome.browser.notifications; |
| +import android.annotation.TargetApi; |
| +import android.app.NotificationManager; |
| import android.content.SharedPreferences; |
| import android.support.annotation.IntDef; |
| import android.support.v4.app.NotificationManagerCompat; |
| +import org.chromium.base.BuildInfo; |
| import org.chromium.base.ContextUtils; |
| +import org.chromium.base.Log; |
| import org.chromium.base.library_loader.LibraryLoader; |
| import org.chromium.base.metrics.RecordHistogram; |
| import java.lang.annotation.Retention; |
| import java.lang.annotation.RetentionPolicy; |
| +import java.lang.reflect.InvocationTargetException; |
| +import java.lang.reflect.Method; |
| /** |
| * Helper class to make tracking notification UMA stats easier for various features. Having a |
| * single entry point here to make more complex tracking easier to add in the future. |
| */ |
| public class NotificationUmaTracker { |
| + private static final String TAG = "NotifsUMATracker"; |
| + |
| @Retention(RetentionPolicy.SOURCE) |
| @IntDef({DOWNLOAD_FILES, DOWNLOAD_PAGES, CLOSE_INCOGNITO, CONTENT_SUGGESTION, |
| SYSTEM_NOTIFICATION_TYPE_BOUNDARY}) |
| @@ -64,16 +72,41 @@ public class NotificationUmaTracker { |
| * types. Splits the logs by the global enabled state of notifications and also logs the last |
| * notification shown prior to the global notifications state being disabled by the user. |
| * @param type The type of notification that was shown. |
| + * @param channelId |
|
Peter Beverloo
2017/04/20 17:30:29
nit: Id of the channel of the notification that wa
awdf
2017/04/21 13:36:28
Done.
|
| * @see SystemNotificationType |
| */ |
| - public void onNotificationShown(@SystemNotificationType int type) { |
| - if (mNotificationManager.areNotificationsEnabled()) { |
| - saveLastShownNotification(type); |
| - recordHistogram("Mobile.SystemNotification.Shown", type); |
| - } else { |
| + public void onNotificationShown( |
| + @SystemNotificationType int type, @ChannelsInitializer.ChannelId String channelId) { |
| + if (!mNotificationManager.areNotificationsEnabled()) { |
| logPotentialBlockedCause(); |
| recordHistogram("Mobile.SystemNotification.Blocked", type); |
| + return; |
| + } |
| + saveLastShownNotification(type); |
|
Peter Beverloo
2017/04/20 17:30:29
Should this be in the branch of line 89? The last
awdf
2017/04/21 13:36:28
Good point. Fixed.
|
| + if (BuildInfo.isAtLeastO() && isChannelBlocked(channelId)) { |
| + recordHistogram("Mobile.SystemNotification.ChannelBlocked", type); |
| + } else { |
| + recordHistogram("Mobile.SystemNotification.Shown", type); |
| + } |
| + } |
| + |
| + @TargetApi(26) |
| + private boolean isChannelBlocked(@ChannelsInitializer.ChannelId String channelId) { |
| + // Use non-compat notification manager as compat does not have getNotificationChannel (yet). |
| + NotificationManager notificationManager = |
| + ContextUtils.getApplicationContext().getSystemService(NotificationManager.class); |
| + // TODO(crbug.com/707804) Remove the following reflection once compileSdk is bumped to O. |
| + try { |
| + Method getNotificationChannel = notificationManager.getClass().getMethod( |
| + "getNotificationChannel", String.class); |
| + Object channel = getNotificationChannel.invoke(notificationManager, channelId); |
| + Method getImportance = channel.getClass().getMethod("getImportance"); |
| + int importance = (int) getImportance.invoke(channel); |
| + return (importance == NotificationManager.IMPORTANCE_NONE); |
| + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { |
| + Log.e(TAG, "Error checking channel importance:", e); |
| } |
| + return false; |
| } |
| private void saveLastShownNotification(@SystemNotificationType int type) { |