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

Commit ddb73088 authored by Alexander Roederer's avatar Alexander Roederer Committed by Android (Google) Code Review
Browse files

Merge "Add notif ids to NotificationChannelClassification" into main

parents 0f4dd2c7 3d9a30ab
Loading
Loading
Loading
Loading
+16 −8
Original line number Diff line number Diff line
@@ -361,6 +361,7 @@ import com.android.server.lights.LightsManager;
import com.android.server.notification.GroupHelper.NotificationAttributes;
import com.android.server.notification.ManagedServices.ManagedServiceInfo;
import com.android.server.notification.ManagedServices.UserProfiles;
import com.android.server.notification.NotificationRecordLogger.NotificationReportedEvent;
import com.android.server.notification.toast.CustomToastRecord;
import com.android.server.notification.toast.TextToastRecord;
import com.android.server.notification.toast.ToastRecord;
@@ -1934,10 +1935,20 @@ public class NotificationManagerService extends SystemService {
                hasSensitiveContent, lifespanMs);
    }
    protected void logClassificationChannelAdjustmentReceived(boolean hasPosted, boolean isAlerting,
                                                              int classification, int lifespanMs) {
    protected void logClassificationChannelAdjustmentReceived(NotificationRecord r,
                                                              boolean hasPosted,
                                                              int classification) {
        // Note that this value of isAlerting does not fully indicate whether a notif
        // would make a sound or HUN on device; it is an approximation for metrics.
        boolean isAlerting = r.getChannel().getImportance() >= IMPORTANCE_DEFAULT;
        int instanceId = r.getSbn().getInstanceId() == null
                ? 0 : r.getSbn().getInstanceId().getId();
        FrameworkStatsLog.write(FrameworkStatsLog.NOTIFICATION_CHANNEL_CLASSIFICATION,
                hasPosted, isAlerting, classification, lifespanMs);
                hasPosted, isAlerting, classification,
                r.getLifespanMs(System.currentTimeMillis()),
                NotificationReportedEvent.NOTIFICATION_ADJUSTED.getId(),
                instanceId, r.getUid());
    }
    protected final BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() {
@@ -7052,11 +7063,8 @@ public class NotificationManagerService extends SystemService {
                    int classification = adjustments.getInt(KEY_TYPE);
                    // swap app provided type with the real thing
                    adjustments.putParcelable(KEY_TYPE, newChannel);
                    // Note that this value of isAlerting does not fully indicate whether a notif
                    // would make a sound or HUN on device; it is an approximation for metrics.
                    boolean isAlerting = r.getChannel().getImportance() >= IMPORTANCE_DEFAULT;
                    logClassificationChannelAdjustmentReceived(isPosted, isAlerting, classification,
                            r.getLifespanMs(System.currentTimeMillis()));
                    logClassificationChannelAdjustmentReceived(r, isPosted, classification);
                }
            }
            r.addAdjustment(adjustment);
+16 −7
Original line number Diff line number Diff line
@@ -311,6 +311,7 @@ import androidx.test.filters.SmallTest;
import com.android.internal.R;
import com.android.internal.config.sysui.TestableFlagResolver;
import com.android.internal.logging.InstanceId;
import com.android.internal.logging.InstanceIdSequence;
import com.android.internal.logging.InstanceIdSequenceFake;
import com.android.internal.messages.nano.SystemMessageProto;
@@ -7637,7 +7638,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                mTestNotificationChannel, 1, null, true));
        when(r1.getLifespanMs(anyLong())).thenReturn(234);
        r1.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId());
        InstanceId instanceId1 = mNotificationInstanceIdSequence.newInstanceId();
        r1.getSbn().setInstanceId(instanceId1);
        // Enqueues the notification to be posted, so hasPosted will be false.
        mService.addEnqueuedNotification(r1);
@@ -7648,8 +7650,10 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                r1.getSbn().getPackageName(), r1.getKey(), signals, "",
                r1.getUser().getIdentifier());
        mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment1);
        assertTrue(mService.checkLastClassificationChannelLog(false /*hasPosted*/,
                true /*isAlerting*/, 3 /*TYPE_NEWS*/, 234));
        assertTrue(mService.checkLastClassificationChannelLog(false /*=hasPosted*/,
                true /*=isAlerting*/, Adjustment.TYPE_NEWS, 234,
                NOTIFICATION_ADJUSTED.getId(),
                instanceId1.getId(), r1.getUid()));
        // Set up notifications that will be adjusted
        // This notification starts on a low importance channel, so isAlerting is false.
@@ -7659,7 +7663,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                mLowImportanceNotificationChannel, 1, null, true));
        when(r2.getLifespanMs(anyLong())).thenReturn(345);
        r2.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId());
        InstanceId instanceId2 = mNotificationInstanceIdSequence.newInstanceId();
        r2.getSbn().setInstanceId(instanceId2);
        // Adds the notification as already posted, so hasPosted will be true.
        mService.addNotification(r2);
        // The signal is removed when used so it has to be readded.
@@ -7669,15 +7674,19 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                r2.getUser().getIdentifier());
        mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment2);
        assertTrue(mService.checkLastClassificationChannelLog(true /*hasPosted*/,
                false /*isAlerting*/, 3 /*TYPE_NEWS*/, 345)); // currently failing
                false /*isAlerting*/, Adjustment.TYPE_NEWS, 345,
                NOTIFICATION_ADJUSTED.getId(),
                instanceId2.getId() /*instance_id*/, r2.getUid()));
        signals.putInt(Adjustment.KEY_TYPE, Adjustment.TYPE_PROMOTION);
        Adjustment adjustment3 = new Adjustment(
                r2.getSbn().getPackageName(), r2.getKey(), signals, "",
                r2.getUser().getIdentifier());
        mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment3);
        assertTrue(mService.checkLastClassificationChannelLog(true /*hasPosted*/,
                false /*isAlerting*/, 1 /*TYPE_PROMOTION*/, 345));
        assertTrue(mService.checkLastClassificationChannelLog(true /*=hasPosted*/,
                false /*=isAlerting*/, Adjustment.TYPE_PROMOTION, 345,
                NOTIFICATION_ADJUSTED.getId(),
                instanceId2.getId() /*instance_id*/, r2.getUid()));
    }
    @Test
+27 −4
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.notification;

import static android.app.NotificationManager.IMPORTANCE_DEFAULT;

import android.companion.ICompanionDeviceManager;
import android.content.ComponentName;
import android.content.Context;
@@ -25,6 +27,7 @@ import androidx.annotation.Nullable;

import com.android.internal.logging.InstanceIdSequence;
import com.android.server.notification.ManagedServices.ManagedServiceInfo;
import com.android.server.notification.NotificationRecordLogger.NotificationReportedEvent;

import java.util.HashSet;
import java.util.Set;
@@ -57,6 +60,9 @@ public class TestableNotificationManagerService extends NotificationManagerServi
        public boolean isAlerting;
        public long classification;
        public long lifetime;
        public long eventId;
        public long instanceId;
        public long uid;
    }
    public ClassificationChannelLog  lastClassificationChannelLog = null;

@@ -221,20 +227,34 @@ public class TestableNotificationManagerService extends NotificationManagerServi
    }

    @Override
    protected void logClassificationChannelAdjustmentReceived(boolean hasPosted, boolean isAlerting,
                                                              int classification, int lifetimeMs) {
    protected void logClassificationChannelAdjustmentReceived(NotificationRecord r,
                                                              boolean hasPosted,
                                                              int classification) {

        boolean isAlerting = r.getChannel().getImportance() >= IMPORTANCE_DEFAULT;
        int instanceId = r.getSbn().getInstanceId() == null
                ? 0 : r.getSbn().getInstanceId().getId();
        int lifetimeMs = r.getLifespanMs(System.currentTimeMillis());
        int uid = r.getUid();

        lastClassificationChannelLog = new ClassificationChannelLog();
        lastClassificationChannelLog.hasPosted = hasPosted;
        lastClassificationChannelLog.isAlerting = isAlerting;
        lastClassificationChannelLog.classification = classification;
        lastClassificationChannelLog.lifetime = lifetimeMs;
        lastClassificationChannelLog.eventId =
                NotificationReportedEvent.NOTIFICATION_ADJUSTED.getId();
        lastClassificationChannelLog.instanceId = instanceId;
        lastClassificationChannelLog.uid = uid;
    }

    /**
     * Returns true if the last recorded classification channel log has all the values specified.
     */
    public boolean checkLastClassificationChannelLog(boolean hasPosted, boolean isAlerting,
                                                     int classification, int lifetime) {
                                                     int classification, int lifetime,
                                                     int eventId, int instanceId,
                                                     int uid) {
        if (lastClassificationChannelLog == null) {
            return false;
        }
@@ -242,6 +262,9 @@ public class TestableNotificationManagerService extends NotificationManagerServi
        return hasPosted == lastClassificationChannelLog.hasPosted
                && isAlerting == lastClassificationChannelLog.isAlerting
                && classification == lastClassificationChannelLog.classification
                && lifetime == lastClassificationChannelLog.lifetime;
                && lifetime == lastClassificationChannelLog.lifetime
                && eventId == lastClassificationChannelLog.eventId
                && instanceId == lastClassificationChannelLog.instanceId
                && uid == lastClassificationChannelLog.uid;
    }
}