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

Commit a8d48c07 authored by Yuri Lin's avatar Yuri Lin
Browse files

Log whether a notification has a summary

This may be with the NOTIFICATION_POSTED event (seems to be what I see in practice with the tester) but also can be posted afterwards with an adjustment, as summaries are already included in the diff that merits logging.

Flag: android.app.nm_summarization_ui
Bug: 409751981
Test: NotificationManagerServiceTest, atom tester
Change-Id: Id088c62835da98d1d49e2a0ebfdebc81b2ce7f5a
parent 2b81818d
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.notification;

import static android.app.Flags.nmSummarization;
import static android.app.Flags.nmSummarizationUi;
import static android.service.notification.NotificationListenerService.REASON_ASSISTANT_CANCEL;
import static android.service.notification.NotificationListenerService.REASON_CANCEL;
import static android.service.notification.NotificationListenerService.REASON_CLEAR_DATA;
@@ -31,6 +33,7 @@ import android.os.Bundle;
import android.service.notification.Adjustment;
import android.service.notification.NotificationListenerService;
import android.service.notification.NotificationStats;
import android.text.TextUtils;
import android.util.Log;

import com.android.internal.logging.InstanceId;
@@ -531,6 +534,7 @@ interface NotificationRecordLogger {
        final int age_in_minutes;
        final boolean is_promoted_ongoing;
        final boolean has_promotable_characteristics;
        final boolean has_summary;
        @DurationMillisLong long post_duration_millis; // Not final; calculated at the end.

        NotificationReported(NotificationRecordPair p,
@@ -572,6 +576,8 @@ interface NotificationRecordLogger {
                    p.r.getSbn().getPostTime(), notification.getWhen());
            this.is_promoted_ongoing = notification.isPromotedOngoing();
            this.has_promotable_characteristics = notification.hasPromotableCharacteristics();
            this.has_summary = (nmSummarization() || nmSummarizationUi())
                    ? !TextUtils.isEmpty(p.r.getSummarization()) : false;
        }
    }

+2 −1
Original line number Diff line number Diff line
@@ -80,7 +80,8 @@ class NotificationRecordLoggerImpl implements NotificationRecordLogger {
                notificationReported.is_locked,
                notificationReported.age_in_minutes,
                notificationReported.is_promoted_ongoing,
                notificationReported.has_promotable_characteristics);
                notificationReported.has_promotable_characteristics,
                notificationReported.has_summary);
    }

    @Override
+52 −0
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@ import static android.app.ActivityTaskManager.INVALID_TASK_ID;
import static android.app.Flags.FLAG_API_RICH_ONGOING;
import static android.app.Flags.FLAG_API_RICH_ONGOING_PERMISSION;
import static android.app.Flags.FLAG_NM_SUMMARIZATION;
import static android.app.Flags.FLAG_NM_SUMMARIZATION_UI;
import static android.app.Flags.FLAG_OPT_IN_RICH_ONGOING;
import static android.app.Flags.FLAG_UI_RICH_ONGOING;
import static android.app.Notification.EXTRA_ALLOW_DURING_SETUP;
import static android.app.Notification.EXTRA_PICTURE;
@@ -7831,6 +7833,56 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                instanceId2.getId() /*instance_id*/, r2.getUid()));
    }
    @Test
    @EnableFlags({FLAG_NM_SUMMARIZATION, FLAG_NM_SUMMARIZATION_UI})
    public void testSummarizationAdjustmentsLogged() throws RemoteException {
        NotificationManagerService.WorkerHandler handler = mock(
                NotificationManagerService.WorkerHandler.class);
        mService.setHandler(handler);
        when(mAssistants.isSameUser(any(), anyInt())).thenReturn(true);
        when(mAssistants.isServiceTokenValidLocked(any())).thenReturn(true);
        when(mAssistants.isAdjustmentAllowedForPackage(anyInt(), anyString(),
                anyString())).thenReturn(true);
        // Set up two notifications, one of which will be adjusted
        final NotificationRecord r1 = generateNotificationRecord(
                mTestNotificationChannel, 1, null, true);
        r1.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId());
        mService.addNotification(r1);
        final NotificationRecord r2 = generateNotificationRecord(
                mTestNotificationChannel, 2, null, true);
        r2.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId());
        mService.addNotification(r2);
        Bundle signals1 = new Bundle();
        signals1.putCharSequence(Adjustment.KEY_SUMMARIZATION, "hello");
        Adjustment adjustment1 = new Adjustment(r1.getSbn().getPackageName(), r1.getKey(), signals1,
                "", r1.getUser().getIdentifier());
        mBinderService.applyAdjustmentFromAssistant(null, adjustment1);
        waitForIdle();
        // Actually apply the adjustment & recalculate importance when run
        doAnswer(invocationOnMock -> {
            ((NotificationRecord) invocationOnMock.getArguments()[0])
                    .applyAdjustments();
            ((NotificationRecord) invocationOnMock.getArguments()[0])
                    .calculateImportance();
            return null;
        }).when(mRankingHelper).extractSignals(any(NotificationRecord.class));
        // Now make sure that when the sort happens, we actually log the changes.
        mService.handleRankingSort();
        // first verify we got the actual summarization in
        assertThat(r1.getSummarization()).isEqualTo("hello");
        // Expect one log only, for r1's adjustment
        assertThat(mNotificationRecordLogger.numCalls()).isEqualTo(1);
        assertThat(mNotificationRecordLogger.event(0)).isEqualTo(NOTIFICATION_ADJUSTED);
        assertThat(mNotificationRecordLogger.get(0).getInstanceId()).isEqualTo(
                r1.getSbn().getInstanceId().getId());
    }
    @Test
    public void testAdjustmentToImportanceNone_cancelsNotification() throws Exception {
        NotificationManagerService.WorkerHandler handler = mock(