1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.notification;
18 
19 import android.annotation.Nullable;
20 
21 import com.android.internal.logging.InstanceId;
22 import com.android.internal.logging.UiEventLogger;
23 import com.android.internal.logging.UiEventLoggerImpl;
24 import com.android.internal.util.FrameworkStatsLog;
25 
26 /**
27  * Standard implementation of NotificationRecordLogger interface.
28  * @hide
29  */
30 public class NotificationRecordLoggerImpl implements NotificationRecordLogger {
31 
32     private UiEventLogger mUiEventLogger = new UiEventLoggerImpl();
33 
34     @Override
maybeLogNotificationPosted(NotificationRecord r, NotificationRecord old, int position, int buzzBeepBlink, InstanceId groupId)35     public void maybeLogNotificationPosted(NotificationRecord r, NotificationRecord old,
36             int position, int buzzBeepBlink,
37             InstanceId groupId) {
38         NotificationRecordPair p = new NotificationRecordPair(r, old);
39         if (!p.shouldLogReported(buzzBeepBlink)) {
40             return;
41         }
42         writeNotificationReportedAtom(p, NotificationReportedEvent.fromRecordPair(p),
43                 position, buzzBeepBlink, groupId);
44     }
45 
46     @Override
logNotificationAdjusted(@ullable NotificationRecord r, int position, int buzzBeepBlink, InstanceId groupId)47     public void logNotificationAdjusted(@Nullable NotificationRecord r,
48             int position, int buzzBeepBlink,
49             InstanceId groupId) {
50         NotificationRecordPair p = new NotificationRecordPair(r, null);
51         writeNotificationReportedAtom(p, NotificationReportedEvent.NOTIFICATION_ADJUSTED,
52                 position, buzzBeepBlink, groupId);
53     }
54 
writeNotificationReportedAtom(NotificationRecordPair p, NotificationReportedEvent eventType, int position, int buzzBeepBlink, InstanceId groupId)55     private void writeNotificationReportedAtom(NotificationRecordPair p,
56             NotificationReportedEvent eventType, int position, int buzzBeepBlink,
57             InstanceId groupId) {
58         FrameworkStatsLog.write(FrameworkStatsLog.NOTIFICATION_REPORTED,
59                 /* int32 event_id = 1 */ eventType.getId(),
60                 /* int32 uid = 2 */ p.r.getUid(),
61                 /* string package_name = 3 */ p.r.getSbn().getPackageName(),
62                 /* int32 instance_id = 4 */ p.getInstanceId(),
63                 /* int32 notification_id_hash = 5 */ p.getNotificationIdHash(),
64                 /* int32 channel_id_hash = 6 */ p.getChannelIdHash(),
65                 /* string group_id_hash = 7 */ p.getGroupIdHash(),
66                 /* int32 group_instance_id = 8 */ (groupId == null) ? 0 : groupId.getId(),
67                 /* bool is_group_summary = 9 */ p.r.getSbn().getNotification().isGroupSummary(),
68                 /* string category = 10 */ p.r.getSbn().getNotification().category,
69                 /* int32 style = 11 */ p.getStyle(),
70                 /* int32 num_people = 12 */ p.getNumPeople(),
71                 /* int32 position = 13 */ position,
72                 /* android.stats.sysui.NotificationImportance importance = 14 */
73                 NotificationRecordLogger.getLoggingImportance(p.r),
74                 /* int32 alerting = 15 */ buzzBeepBlink,
75                 /* NotificationImportanceExplanation importance_source = 16 */
76                 p.r.getImportanceExplanationCode(),
77                 /* android.stats.sysui.NotificationImportance importance_initial = 17 */
78                 p.r.getInitialImportance(),
79                 /* NotificationImportanceExplanation importance_initial_source = 18 */
80                 p.r.getInitialImportanceExplanationCode(),
81                 /* android.stats.sysui.NotificationImportance importance_asst = 19 */
82                 p.r.getAssistantImportance(),
83                 /* int32 assistant_hash = 20 */ p.getAssistantHash(),
84                 /* float assistant_ranking_score = 21 */ p.r.getRankingScore()
85         );
86     }
87 
88     @Override
log(UiEventLogger.UiEventEnum event, NotificationRecord r)89     public void log(UiEventLogger.UiEventEnum event, NotificationRecord r) {
90         if (r == null) {
91             return;
92         }
93         mUiEventLogger.logWithInstanceId(event, r.getUid(), r.getSbn().getPackageName(),
94                 r.getSbn().getInstanceId());
95     }
96 
97     @Override
log(UiEventLogger.UiEventEnum event)98     public void log(UiEventLogger.UiEventEnum event) {
99         mUiEventLogger.log(event);
100     }
101 }
102