Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 029190c1 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Log usage of addPerson() and setStyle() in notifications." into qt-dev

parents b7abebde c22772c3
Loading
Loading
Loading
Loading
+47 −21
Original line number Diff line number Diff line
@@ -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,16 +207,14 @@ 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)
        {
    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)
        {
            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}.
     */
@@ -279,8 +283,10 @@ public class StatusBarNotification implements Parcelable {
        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() {
+22 −0
Original line number Diff line number Diff line
@@ -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));
    }
+9 −0
Original line number Diff line number Diff line
@@ -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