Loading core/java/android/service/notification/StatusBarNotification.java +47 −21 Original line number Original line Diff line number Diff line Loading @@ -20,6 +20,7 @@ import android.annotation.NonNull; import android.annotation.UnsupportedAppUsage; import android.annotation.UnsupportedAppUsage; import android.app.Notification; import android.app.Notification; import android.app.NotificationManager; import android.app.NotificationManager; import android.app.Person; import android.content.Context; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager; Loading @@ -32,6 +33,8 @@ import android.os.UserHandle; import com.android.internal.logging.nano.MetricsProto; import com.android.internal.logging.nano.MetricsProto; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import java.util.ArrayList; /** /** * Class encapsulating a Notification. Sent by the NotificationManagerService to clients including * Class encapsulating a Notification. Sent by the NotificationManagerService to clients including * the status bar and any {@link android.service.notification.NotificationListenerService}s. * the status bar and any {@link android.service.notification.NotificationListenerService}s. Loading Loading @@ -166,6 +169,7 @@ public class StatusBarNotification implements Parcelable { /** /** * Returns true if application asked that this notification be part of a group. * Returns true if application asked that this notification be part of a group. * * @hide * @hide */ */ public boolean isAppGroup() { public boolean isAppGroup() { Loading Loading @@ -203,16 +207,14 @@ public class StatusBarNotification implements Parcelable { return 0; return 0; } } public static final @android.annotation.NonNull Parcelable.Creator<StatusBarNotification> CREATOR public static final @android.annotation.NonNull = new Parcelable.Creator<StatusBarNotification>() Parcelable.Creator<StatusBarNotification> CREATOR = { new Parcelable.Creator<StatusBarNotification>() { public StatusBarNotification createFromParcel(Parcel parcel) public StatusBarNotification createFromParcel(Parcel parcel) { { return new StatusBarNotification(parcel); return new StatusBarNotification(parcel); } } public StatusBarNotification[] newArray(int size) public StatusBarNotification[] newArray(int size) { { return new StatusBarNotification[size]; return new StatusBarNotification[size]; } } }; }; Loading Loading @@ -243,14 +245,16 @@ public class StatusBarNotification implements Parcelable { this.key, this.notification); this.key, this.notification); } } /** Convenience method to check the notification's flags for /** * Convenience method to check the notification's flags for * {@link Notification#FLAG_ONGOING_EVENT}. * {@link Notification#FLAG_ONGOING_EVENT}. */ */ public boolean isOngoing() { public boolean isOngoing() { return (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0; return (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0; } } /** Convenience method to check the notification's flags for /** * Convenience method to check the notification's flags for * either {@link Notification#FLAG_ONGOING_EVENT} or * either {@link Notification#FLAG_ONGOING_EVENT} or * {@link Notification#FLAG_NO_CLEAR}. * {@link Notification#FLAG_NO_CLEAR}. */ */ Loading Loading @@ -279,8 +283,10 @@ public class StatusBarNotification implements Parcelable { return id; return id; } } /** The tag supplied to {@link android.app.NotificationManager#notify(int,Notification)}, /** * or null if no tag was specified. */ * The tag supplied to {@link android.app.NotificationManager#notify(int, Notification)}, * or null if no tag was specified. */ public String getTag() { public String getTag() { return tag; return tag; } } Loading @@ -307,8 +313,10 @@ public class StatusBarNotification implements Parcelable { return initialPid; return initialPid; } } /** The {@link android.app.Notification} supplied to /** * {@link android.app.NotificationManager#notify(int,Notification)}. */ * The {@link android.app.Notification} supplied to * {@link android.app.NotificationManager#notify(int, Notification)}. */ public Notification getNotification() { public Notification getNotification() { return notification; return notification; } } Loading @@ -320,7 +328,8 @@ public class StatusBarNotification implements Parcelable { return user; return user; } } /** The time (in {@link System#currentTimeMillis} time) the notification was posted, /** * The time (in {@link System#currentTimeMillis} time) the notification was posted, * which may be different than {@link android.app.Notification#when}. * which may be different than {@link android.app.Notification#when}. */ */ public long getPostTime() { public long getPostTime() { Loading @@ -343,6 +352,7 @@ public class StatusBarNotification implements Parcelable { /** /** * The ID passed to setGroup(), or the override, or null. * The ID passed to setGroup(), or the override, or null. * * @hide * @hide */ */ public String getGroup() { public String getGroup() { Loading Loading @@ -398,10 +408,11 @@ public class StatusBarNotification implements Parcelable { /** /** * Returns a LogMaker that contains all basic information of the notification. * Returns a LogMaker that contains all basic information of the notification. * * @hide * @hide */ */ public LogMaker getLogMaker() { public LogMaker getLogMaker() { return new LogMaker(MetricsEvent.VIEW_UNKNOWN).setPackageName(getPackageName()) LogMaker logMaker = new LogMaker(MetricsEvent.VIEW_UNKNOWN).setPackageName(getPackageName()) .addTaggedData(MetricsEvent.NOTIFICATION_ID, getId()) .addTaggedData(MetricsEvent.NOTIFICATION_ID, getId()) .addTaggedData(MetricsEvent.NOTIFICATION_TAG, getTag()) .addTaggedData(MetricsEvent.NOTIFICATION_TAG, getTag()) .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID, getChannelIdLogTag()) .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID, getChannelIdLogTag()) Loading @@ -410,6 +421,21 @@ public class StatusBarNotification implements Parcelable { getNotification().isGroupSummary() ? 1 : 0) getNotification().isGroupSummary() ? 1 : 0) .addTaggedData(MetricsProto.MetricsEvent.FIELD_NOTIFICATION_CATEGORY, .addTaggedData(MetricsProto.MetricsEvent.FIELD_NOTIFICATION_CATEGORY, getNotification().category); getNotification().category); if (getNotification().extras != null) { // Log the style used, if present. We only log the hash here, as notification log // events are frequent, while there are few styles (hence low chance of collisions). String template = getNotification().extras.getString(Notification.EXTRA_TEMPLATE); if (template != null && !template.isEmpty()) { logMaker.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_STYLE, template.hashCode()); } ArrayList<Person> people = getNotification().extras.getParcelableArrayList( Notification.EXTRA_PEOPLE_LIST); if (people != null && !people.isEmpty()) { logMaker.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_PEOPLE, people.size()); } } return logMaker; } } private String getGroupLogTag() { private String getGroupLogTag() { Loading core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java +22 −0 Original line number Original line Diff line number Diff line Loading @@ -24,6 +24,7 @@ import static org.mockito.Mockito.when; import android.app.ActivityManager; import android.app.ActivityManager; import android.app.Notification; import android.app.Notification; import android.app.Person; import android.content.Context; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager; Loading Loading @@ -87,6 +88,9 @@ public class StatusBarNotificationTest { assertEquals(0, assertEquals(0, logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY)); logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY)); assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CATEGORY)); assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CATEGORY)); assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_STYLE)); assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_PEOPLE)); } } /** Verify that modifying the returned logMaker won't leave stale data behind for /** Verify that modifying the returned logMaker won't leave stale data behind for Loading Loading @@ -159,6 +163,24 @@ public class StatusBarNotificationTest { sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID)); sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID)); } } @Test public void testLogMakerWithPerson() { Notification.Builder builder = getNotificationBuilder(GROUP_ID_1, CHANNEL_ID) .addPerson(new Person.Builder().build()); final LogMaker logMaker = getNotification(PKG, builder).getLogMaker(); assertEquals(1, logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_PEOPLE)); } @Test public void testLogMakerWithStyle() { Notification.Builder builder = getNotificationBuilder(GROUP_ID_1, CHANNEL_ID) .setStyle(new Notification.MessagingStyle(new Person.Builder().build())); final LogMaker logMaker = getNotification(PKG, builder).getLogMaker(); assertEquals("android.app.Notification$MessagingStyle".hashCode(), logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_STYLE)); } private StatusBarNotification getNotification(String pkg, String group, String channelId) { private StatusBarNotification getNotification(String pkg, String group, String channelId) { return getNotification(pkg, getNotificationBuilder(group, channelId)); return getNotification(pkg, getNotificationBuilder(group, channelId)); } } Loading proto/src/metrics_constants/metrics_constants.proto +9 −0 Original line number Original line Diff line number Diff line Loading @@ -7388,6 +7388,15 @@ message MetricsEvent { // CATEGORY: NOTIFICATION // CATEGORY: NOTIFICATION MEDIA_NOTIFICATION_SEEKBAR = 1743; MEDIA_NOTIFICATION_SEEKBAR = 1743; // Custom tag for StatusBarNotification. Length of // Notification.extras[EXTRA_PEOPLE_LIST], set by addPerson(). FIELD_NOTIFICATION_PEOPLE = 1744; // Custom tag for StatusBarNotification. The Java hashcode of // Notification.extras[EXTRA_TEMPLATE], which is a string like // android.app.Notification$MessagingStyle, set by setStyle(). FIELD_NOTIFICATION_STYLE = 1745; // ---- End Q Constants, all Q constants go above this line ---- // ---- End Q Constants, all Q constants go above this line ---- // Add new aosp constants above this line. // Add new aosp constants above this line. // END OF AOSP CONSTANTS // END OF AOSP CONSTANTS Loading Loading
core/java/android/service/notification/StatusBarNotification.java +47 −21 Original line number Original line Diff line number Diff line Loading @@ -20,6 +20,7 @@ import android.annotation.NonNull; import android.annotation.UnsupportedAppUsage; import android.annotation.UnsupportedAppUsage; import android.app.Notification; import android.app.Notification; import android.app.NotificationManager; import android.app.NotificationManager; import android.app.Person; import android.content.Context; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager; Loading @@ -32,6 +33,8 @@ import android.os.UserHandle; import com.android.internal.logging.nano.MetricsProto; import com.android.internal.logging.nano.MetricsProto; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import java.util.ArrayList; /** /** * Class encapsulating a Notification. Sent by the NotificationManagerService to clients including * Class encapsulating a Notification. Sent by the NotificationManagerService to clients including * the status bar and any {@link android.service.notification.NotificationListenerService}s. * the status bar and any {@link android.service.notification.NotificationListenerService}s. Loading Loading @@ -166,6 +169,7 @@ public class StatusBarNotification implements Parcelable { /** /** * Returns true if application asked that this notification be part of a group. * Returns true if application asked that this notification be part of a group. * * @hide * @hide */ */ public boolean isAppGroup() { public boolean isAppGroup() { Loading Loading @@ -203,16 +207,14 @@ public class StatusBarNotification implements Parcelable { return 0; return 0; } } public static final @android.annotation.NonNull Parcelable.Creator<StatusBarNotification> CREATOR public static final @android.annotation.NonNull = new Parcelable.Creator<StatusBarNotification>() Parcelable.Creator<StatusBarNotification> CREATOR = { new Parcelable.Creator<StatusBarNotification>() { public StatusBarNotification createFromParcel(Parcel parcel) public StatusBarNotification createFromParcel(Parcel parcel) { { return new StatusBarNotification(parcel); return new StatusBarNotification(parcel); } } public StatusBarNotification[] newArray(int size) public StatusBarNotification[] newArray(int size) { { return new StatusBarNotification[size]; return new StatusBarNotification[size]; } } }; }; Loading Loading @@ -243,14 +245,16 @@ public class StatusBarNotification implements Parcelable { this.key, this.notification); this.key, this.notification); } } /** Convenience method to check the notification's flags for /** * Convenience method to check the notification's flags for * {@link Notification#FLAG_ONGOING_EVENT}. * {@link Notification#FLAG_ONGOING_EVENT}. */ */ public boolean isOngoing() { public boolean isOngoing() { return (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0; return (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0; } } /** Convenience method to check the notification's flags for /** * Convenience method to check the notification's flags for * either {@link Notification#FLAG_ONGOING_EVENT} or * either {@link Notification#FLAG_ONGOING_EVENT} or * {@link Notification#FLAG_NO_CLEAR}. * {@link Notification#FLAG_NO_CLEAR}. */ */ Loading Loading @@ -279,8 +283,10 @@ public class StatusBarNotification implements Parcelable { return id; return id; } } /** The tag supplied to {@link android.app.NotificationManager#notify(int,Notification)}, /** * or null if no tag was specified. */ * The tag supplied to {@link android.app.NotificationManager#notify(int, Notification)}, * or null if no tag was specified. */ public String getTag() { public String getTag() { return tag; return tag; } } Loading @@ -307,8 +313,10 @@ public class StatusBarNotification implements Parcelable { return initialPid; return initialPid; } } /** The {@link android.app.Notification} supplied to /** * {@link android.app.NotificationManager#notify(int,Notification)}. */ * The {@link android.app.Notification} supplied to * {@link android.app.NotificationManager#notify(int, Notification)}. */ public Notification getNotification() { public Notification getNotification() { return notification; return notification; } } Loading @@ -320,7 +328,8 @@ public class StatusBarNotification implements Parcelable { return user; return user; } } /** The time (in {@link System#currentTimeMillis} time) the notification was posted, /** * The time (in {@link System#currentTimeMillis} time) the notification was posted, * which may be different than {@link android.app.Notification#when}. * which may be different than {@link android.app.Notification#when}. */ */ public long getPostTime() { public long getPostTime() { Loading @@ -343,6 +352,7 @@ public class StatusBarNotification implements Parcelable { /** /** * The ID passed to setGroup(), or the override, or null. * The ID passed to setGroup(), or the override, or null. * * @hide * @hide */ */ public String getGroup() { public String getGroup() { Loading Loading @@ -398,10 +408,11 @@ public class StatusBarNotification implements Parcelable { /** /** * Returns a LogMaker that contains all basic information of the notification. * Returns a LogMaker that contains all basic information of the notification. * * @hide * @hide */ */ public LogMaker getLogMaker() { public LogMaker getLogMaker() { return new LogMaker(MetricsEvent.VIEW_UNKNOWN).setPackageName(getPackageName()) LogMaker logMaker = new LogMaker(MetricsEvent.VIEW_UNKNOWN).setPackageName(getPackageName()) .addTaggedData(MetricsEvent.NOTIFICATION_ID, getId()) .addTaggedData(MetricsEvent.NOTIFICATION_ID, getId()) .addTaggedData(MetricsEvent.NOTIFICATION_TAG, getTag()) .addTaggedData(MetricsEvent.NOTIFICATION_TAG, getTag()) .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID, getChannelIdLogTag()) .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID, getChannelIdLogTag()) Loading @@ -410,6 +421,21 @@ public class StatusBarNotification implements Parcelable { getNotification().isGroupSummary() ? 1 : 0) getNotification().isGroupSummary() ? 1 : 0) .addTaggedData(MetricsProto.MetricsEvent.FIELD_NOTIFICATION_CATEGORY, .addTaggedData(MetricsProto.MetricsEvent.FIELD_NOTIFICATION_CATEGORY, getNotification().category); getNotification().category); if (getNotification().extras != null) { // Log the style used, if present. We only log the hash here, as notification log // events are frequent, while there are few styles (hence low chance of collisions). String template = getNotification().extras.getString(Notification.EXTRA_TEMPLATE); if (template != null && !template.isEmpty()) { logMaker.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_STYLE, template.hashCode()); } ArrayList<Person> people = getNotification().extras.getParcelableArrayList( Notification.EXTRA_PEOPLE_LIST); if (people != null && !people.isEmpty()) { logMaker.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_PEOPLE, people.size()); } } return logMaker; } } private String getGroupLogTag() { private String getGroupLogTag() { Loading
core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java +22 −0 Original line number Original line Diff line number Diff line Loading @@ -24,6 +24,7 @@ import static org.mockito.Mockito.when; import android.app.ActivityManager; import android.app.ActivityManager; import android.app.Notification; import android.app.Notification; import android.app.Person; import android.content.Context; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager; Loading Loading @@ -87,6 +88,9 @@ public class StatusBarNotificationTest { assertEquals(0, assertEquals(0, logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY)); logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY)); assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CATEGORY)); assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CATEGORY)); assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_STYLE)); assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_PEOPLE)); } } /** Verify that modifying the returned logMaker won't leave stale data behind for /** Verify that modifying the returned logMaker won't leave stale data behind for Loading Loading @@ -159,6 +163,24 @@ public class StatusBarNotificationTest { sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID)); sbn.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID)); } } @Test public void testLogMakerWithPerson() { Notification.Builder builder = getNotificationBuilder(GROUP_ID_1, CHANNEL_ID) .addPerson(new Person.Builder().build()); final LogMaker logMaker = getNotification(PKG, builder).getLogMaker(); assertEquals(1, logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_PEOPLE)); } @Test public void testLogMakerWithStyle() { Notification.Builder builder = getNotificationBuilder(GROUP_ID_1, CHANNEL_ID) .setStyle(new Notification.MessagingStyle(new Person.Builder().build())); final LogMaker logMaker = getNotification(PKG, builder).getLogMaker(); assertEquals("android.app.Notification$MessagingStyle".hashCode(), logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_STYLE)); } private StatusBarNotification getNotification(String pkg, String group, String channelId) { private StatusBarNotification getNotification(String pkg, String group, String channelId) { return getNotification(pkg, getNotificationBuilder(group, channelId)); return getNotification(pkg, getNotificationBuilder(group, channelId)); } } Loading
proto/src/metrics_constants/metrics_constants.proto +9 −0 Original line number Original line Diff line number Diff line Loading @@ -7388,6 +7388,15 @@ message MetricsEvent { // CATEGORY: NOTIFICATION // CATEGORY: NOTIFICATION MEDIA_NOTIFICATION_SEEKBAR = 1743; MEDIA_NOTIFICATION_SEEKBAR = 1743; // Custom tag for StatusBarNotification. Length of // Notification.extras[EXTRA_PEOPLE_LIST], set by addPerson(). FIELD_NOTIFICATION_PEOPLE = 1744; // Custom tag for StatusBarNotification. The Java hashcode of // Notification.extras[EXTRA_TEMPLATE], which is a string like // android.app.Notification$MessagingStyle, set by setStyle(). FIELD_NOTIFICATION_STYLE = 1745; // ---- End Q Constants, all Q constants go above this line ---- // ---- End Q Constants, all Q constants go above this line ---- // Add new aosp constants above this line. // Add new aosp constants above this line. // END OF AOSP CONSTANTS // END OF AOSP CONSTANTS Loading