1 /*
2  * Copyright (C) 2023 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.content.pm.PackageManager;
20 
21 import com.android.os.dnd.DNDPolicyProto;
22 
23 import com.google.protobuf.InvalidProtocolBufferException;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 /**
29  * ZenModeEventLoggerFake extends ZenModeEventLogger for ease of verifying logging output. This
30  * class behaves exactly the same as its parent class except that instead of actually logging, it
31  * stores the full information at time of log whenever something would be logged.
32  */
33 public class ZenModeEventLoggerFake extends ZenModeEventLogger {
34     // A record of the contents of each event we'd log, stored by recording the ChangeState object
35     // at the time of the log.
36     private List<ZenStateChanges> mChanges = new ArrayList<>();
37 
ZenModeEventLoggerFake(PackageManager pm)38     public ZenModeEventLoggerFake(PackageManager pm) {
39         super(pm);
40     }
41 
42     @Override
logChanges()43     void logChanges() {
44         // current change state being logged
45         mChanges.add(mChangeState.copy());
46     }
47 
48     // Reset the state of the logger (remove all changes).
reset()49     public void reset() {
50         mChanges = new ArrayList<>();
51     }
52 
53     // Returns the number of changes logged.
numLoggedChanges()54     public int numLoggedChanges() {
55         return mChanges.size();
56     }
57 
58     // is index i out of range for the set of changes we have
outOfRange(int i)59     private boolean outOfRange(int i) {
60         return i < 0 || i >= mChanges.size();
61     }
62 
63     // Throw an exception if provided index is out of range
checkInRange(int i)64     private void checkInRange(int i) throws IllegalArgumentException {
65         if (outOfRange(i)) {
66             throw new IllegalArgumentException("invalid index for logged event: " + i);
67         }
68     }
69 
70     // Get the UiEvent ID of the i'th logged event.
getEventId(int i)71     public int getEventId(int i) throws IllegalArgumentException {
72         checkInRange(i);
73         return mChanges.get(i).getEventId().getId();
74     }
75 
76     // Get the previous zen mode associated with the change at event i.
getPrevZenMode(int i)77     public int getPrevZenMode(int i) throws IllegalArgumentException {
78         checkInRange(i);
79         return mChanges.get(i).mPrevZenMode;
80     }
81 
82     // Get the new zen mode associated with the change at event i.
getNewZenMode(int i)83     public int getNewZenMode(int i) throws IllegalArgumentException {
84         checkInRange(i);
85         return mChanges.get(i).mNewZenMode;
86     }
87 
88     // Get the changed rule type associated with event i.
getChangedRuleType(int i)89     public int getChangedRuleType(int i) throws IllegalArgumentException {
90         checkInRange(i);
91         return mChanges.get(i).getChangedRuleType();
92     }
93 
getNumRulesActive(int i)94     public int getNumRulesActive(int i) throws IllegalArgumentException {
95         checkInRange(i);
96         return mChanges.get(i).getNumRulesActive();
97     }
98 
getFromSystemOrSystemUi(int i)99     public boolean getFromSystemOrSystemUi(int i) throws IllegalArgumentException {
100         // While this isn't a logged output value, it's still helpful to check in tests.
101         checkInRange(i);
102         return mChanges.get(i).mFromSystemOrSystemUi;
103     }
104 
getIsUserAction(int i)105     public boolean getIsUserAction(int i) throws IllegalArgumentException {
106         checkInRange(i);
107         return mChanges.get(i).getIsUserAction();
108     }
109 
getPackageUid(int i)110     public int getPackageUid(int i) throws IllegalArgumentException {
111         checkInRange(i);
112         return mChanges.get(i).getPackageUid();
113     }
114 
115     // Get the DNDPolicyProto (unmarshaled from bytes) associated with event i.
116     // Note that in creation of the log, we use a notification.proto mirror of DNDPolicyProto,
117     // but here we use the actual logging-side proto to make sure they continue to match.
getPolicyProto(int i)118     public DNDPolicyProto getPolicyProto(int i) throws IllegalArgumentException {
119         checkInRange(i);
120         byte[] policyBytes = mChanges.get(i).getDNDPolicyProto();
121         try {
122             return DNDPolicyProto.parseFrom(policyBytes);
123         } catch (InvalidProtocolBufferException e) {
124             return null; // couldn't turn it into proto
125         }
126     }
127 
getAreChannelsBypassing(int i)128     public boolean getAreChannelsBypassing(int i) throws IllegalArgumentException {
129         checkInRange(i);
130         return mChanges.get(i).getAreChannelsBypassing();
131     }
132 }
133