summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWill Brockman <brockman@google.com>2019-06-14 14:55:31 -0400
committerWill Brockman <brockman@google.com>2019-06-14 19:10:02 +0000
commitc22772c36ae49a9284cc0465469293dbbaa93a6e (patch)
tree1cc998790c5f2bf3a693eb95302b8867d4cfbb84
parent7fc7f9ce2211e161fc8f009302ded5a78daedd17 (diff)
Log usage of addPerson() and setStyle() in notifications.
Added Tron logging to StatusBarNotification.getLogMaker() so it will be present in most logs about the notification. Change-Id: I720706d37c663f2018bdfe2153ad180970166c90 Test: atest android.service.notification.StatusBarNotificationTest Bug: 135180518
-rw-r--r--core/java/android/service/notification/StatusBarNotification.java68
-rw-r--r--core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java22
-rw-r--r--proto/src/metrics_constants/metrics_constants.proto9
3 files changed, 78 insertions, 21 deletions
diff --git a/core/java/android/service/notification/StatusBarNotification.java b/core/java/android/service/notification/StatusBarNotification.java
index 8512a0bd8088..905c7811e457 100644
--- a/core/java/android/service/notification/StatusBarNotification.java
+++ b/core/java/android/service/notification/StatusBarNotification.java
@@ -20,6 +20,7 @@ import android.annotation.NonNull;
import android.annotation.UnsupportedAppUsage;
import android.app.Notification;
import android.app.NotificationManager;
+import android.app.Person;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
@@ -32,6 +33,8 @@ import android.os.UserHandle;
import com.android.internal.logging.nano.MetricsProto;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import java.util.ArrayList;
+
/**
* Class encapsulating a Notification. Sent by the NotificationManagerService to clients including
* the status bar and any {@link android.service.notification.NotificationListenerService}s.
@@ -166,6 +169,7 @@ public class StatusBarNotification implements Parcelable {
/**
* Returns true if application asked that this notification be part of a group.
+ *
* @hide
*/
public boolean isAppGroup() {
@@ -203,18 +207,16 @@ public class StatusBarNotification implements Parcelable {
return 0;
}
- public static final @android.annotation.NonNull Parcelable.Creator<StatusBarNotification> CREATOR
- = new Parcelable.Creator<StatusBarNotification>()
- {
- public StatusBarNotification createFromParcel(Parcel parcel)
- {
- return new StatusBarNotification(parcel);
- }
+ public static final @android.annotation.NonNull
+ Parcelable.Creator<StatusBarNotification> CREATOR =
+ new Parcelable.Creator<StatusBarNotification>() {
+ public StatusBarNotification createFromParcel(Parcel parcel) {
+ return new StatusBarNotification(parcel);
+ }
- public StatusBarNotification[] newArray(int size)
- {
- return new StatusBarNotification[size];
- }
+ public StatusBarNotification[] newArray(int size) {
+ return new StatusBarNotification[size];
+ }
};
/**
@@ -243,14 +245,16 @@ public class StatusBarNotification implements Parcelable {
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}.
*/
public boolean isOngoing() {
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
* {@link Notification#FLAG_NO_CLEAR}.
*/
@@ -274,13 +278,15 @@ public class StatusBarNotification implements Parcelable {
return pkg;
}
- /** The id supplied to {@link android.app.NotificationManager#notify(int,Notification)}. */
+ /** The id supplied to {@link android.app.NotificationManager#notify(int, Notification)}. */
public int getId() {
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() {
return tag;
}
@@ -307,8 +313,10 @@ public class StatusBarNotification implements Parcelable {
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() {
return notification;
}
@@ -320,7 +328,8 @@ public class StatusBarNotification implements Parcelable {
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}.
*/
public long getPostTime() {
@@ -343,6 +352,7 @@ public class StatusBarNotification implements Parcelable {
/**
* The ID passed to setGroup(), or the override, or null.
+ *
* @hide
*/
public String getGroup() {
@@ -398,10 +408,11 @@ public class StatusBarNotification implements Parcelable {
/**
* Returns a LogMaker that contains all basic information of the notification.
+ *
* @hide
*/
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_TAG, getTag())
.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID, getChannelIdLogTag())
@@ -410,6 +421,21 @@ public class StatusBarNotification implements Parcelable {
getNotification().isGroupSummary() ? 1 : 0)
.addTaggedData(MetricsProto.MetricsEvent.FIELD_NOTIFICATION_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() {
@@ -433,6 +459,6 @@ public class StatusBarNotification implements Parcelable {
}
String hash = Integer.toHexString(logTag.hashCode());
return logTag.substring(0, MAX_LOG_TAG_LENGTH - hash.length() - 1) + "-"
- + hash;
+ + hash;
}
}
diff --git a/core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java b/core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java
index 0f32a827377e..6161108d4d70 100644
--- a/core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java
+++ b/core/tests/coretests/src/android/service/notification/StatusBarNotificationTest.java
@@ -24,6 +24,7 @@ import static org.mockito.Mockito.when;
import android.app.ActivityManager;
import android.app.Notification;
+import android.app.Person;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
@@ -87,6 +88,9 @@ public class StatusBarNotificationTest {
assertEquals(0,
logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY));
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
@@ -159,6 +163,24 @@ public class StatusBarNotificationTest {
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) {
return getNotification(pkg, getNotificationBuilder(group, channelId));
}
diff --git a/proto/src/metrics_constants/metrics_constants.proto b/proto/src/metrics_constants/metrics_constants.proto
index 0f0e6f9fb446..a2bc0d4e2f6c 100644
--- a/proto/src/metrics_constants/metrics_constants.proto
+++ b/proto/src/metrics_constants/metrics_constants.proto
@@ -7388,6 +7388,15 @@ message MetricsEvent {
// CATEGORY: NOTIFICATION
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 ----
// Add new aosp constants above this line.
// END OF AOSP CONSTANTS