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.DurationMillisLong; 20 21 import androidx.annotation.Nullable; 22 23 import com.android.internal.logging.InstanceId; 24 import com.android.internal.logging.UiEventLogger; 25 26 import java.util.ArrayList; 27 import java.util.HashMap; 28 import java.util.List; 29 import java.util.Map; 30 31 /** 32 * Fake implementation of NotificationRecordLogger, for testing. 33 */ 34 class NotificationRecordLoggerFake implements NotificationRecordLogger { 35 static class CallRecord extends NotificationRecordPair { 36 public UiEventLogger.UiEventEnum event; 37 38 // The following fields are only relevant to maybeLogNotificationPosted() calls. 39 static final int INVALID = -1; 40 public int position = INVALID, buzzBeepBlink = INVALID; 41 public boolean wasLogged; 42 public InstanceId groupInstanceId; 43 @Nullable @DurationMillisLong public Long postDurationMillisLogged; 44 CallRecord(NotificationRecord r, NotificationRecord old, int position, int buzzBeepBlink, InstanceId groupId)45 CallRecord(NotificationRecord r, NotificationRecord old, int position, 46 int buzzBeepBlink, InstanceId groupId) { 47 super(r, old); 48 this.position = position; 49 this.buzzBeepBlink = buzzBeepBlink; 50 wasLogged = shouldLogReported(buzzBeepBlink); 51 event = wasLogged ? NotificationReportedEvent.fromRecordPair(this) : null; 52 groupInstanceId = groupId; 53 } 54 CallRecord(NotificationRecord r, int position, int buzzBeepBlink, InstanceId groupId)55 CallRecord(NotificationRecord r, int position, int buzzBeepBlink, InstanceId groupId) { 56 super(r, null); 57 this.position = position; 58 this.buzzBeepBlink = buzzBeepBlink; 59 wasLogged = true; 60 event = NotificationReportedEvent.NOTIFICATION_ADJUSTED; 61 groupInstanceId = groupId; 62 } 63 CallRecord(NotificationRecord r, UiEventLogger.UiEventEnum event)64 CallRecord(NotificationRecord r, UiEventLogger.UiEventEnum event) { 65 super(r, null); 66 wasLogged = true; 67 this.event = event; 68 } 69 } 70 private final List<CallRecord> mCalls = new ArrayList<>(); 71 private final Map<NotificationReported, CallRecord> mPendingLogs = new HashMap<>(); 72 numCalls()73 public int numCalls() { 74 return mCalls.size(); 75 } 76 getCalls()77 List<CallRecord> getCalls() { 78 return mCalls; 79 } 80 getPendingLogs()81 List<NotificationReported> getPendingLogs() { 82 return new ArrayList<>(mPendingLogs.keySet()); 83 } 84 get(int index)85 CallRecord get(int index) { 86 return mCalls.get(index); 87 } event(int index)88 UiEventLogger.UiEventEnum event(int index) { 89 return mCalls.get(index).event; 90 } 91 92 @Nullable 93 @Override prepareToLogNotificationPosted(@ullable NotificationRecord r, @Nullable NotificationRecord old, int position, int buzzBeepBlink, InstanceId groupId)94 public NotificationReported prepareToLogNotificationPosted(@Nullable NotificationRecord r, 95 @Nullable NotificationRecord old, int position, int buzzBeepBlink, InstanceId groupId) { 96 NotificationReported nr = NotificationRecordLogger.super.prepareToLogNotificationPosted(r, 97 old, position, buzzBeepBlink, groupId); 98 CallRecord callRecord = new CallRecord(r, old, position, buzzBeepBlink, groupId); 99 callRecord.wasLogged = false; 100 mCalls.add(callRecord); 101 if (nr != null) { 102 mPendingLogs.put(nr, callRecord); 103 } 104 return nr; 105 } 106 107 @Override logNotificationPosted(NotificationReported nr)108 public void logNotificationPosted(NotificationReported nr) { 109 CallRecord callRecord = mPendingLogs.get(nr); 110 if (callRecord == null) { 111 throw new IllegalStateException( 112 "Didn't find corresponding CallRecord in mPreparedCalls. Did you call " 113 + "logNotificationPosted() twice!?"); 114 } 115 mPendingLogs.remove(nr); 116 callRecord.wasLogged = true; 117 callRecord.postDurationMillisLogged = nr.post_duration_millis; 118 } 119 120 @Override logNotificationAdjusted(NotificationRecord r, int position, int buzzBeepBlink, InstanceId groupId)121 public void logNotificationAdjusted(NotificationRecord r, int position, int buzzBeepBlink, 122 InstanceId groupId) { 123 mCalls.add(new CallRecord(r, position, buzzBeepBlink, groupId)); 124 } 125 126 @Override log(UiEventLogger.UiEventEnum event, NotificationRecord r)127 public void log(UiEventLogger.UiEventEnum event, NotificationRecord r) { 128 mCalls.add(new CallRecord(r, event)); 129 } 130 131 @Override log(UiEventLogger.UiEventEnum event)132 public void log(UiEventLogger.UiEventEnum event) { 133 mCalls.add(new CallRecord(null, event)); 134 } 135 } 136