1 /*
2  * Copyright (C) 2021 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.eventlib.events.deviceadminreceivers;
18 
19 import android.app.admin.DelegatedAdminReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 
23 import androidx.annotation.CheckResult;
24 
25 import com.android.eventlib.Event;
26 import com.android.eventlib.EventLogger;
27 import com.android.eventlib.EventLogsQuery;
28 import com.android.queryable.info.DelegatedAdminReceiverInfo;
29 import com.android.queryable.queries.DelegatedAdminReceiverQuery;
30 import com.android.queryable.queries.DelegatedAdminReceiverQueryHelper;
31 import com.android.queryable.queries.IntegerQueryHelper;
32 import com.android.queryable.queries.IntentQueryHelper;
33 import com.android.queryable.queries.LongQueryHelper;
34 import com.android.queryable.util.SerializableParcelWrapper;
35 
36 import com.google.errorprone.annotations.CanIgnoreReturnValue;
37 
38 /**
39  * Event logged when {@link DelegatedAdminReceiver#onNetworkLogsAvailable(Context, Intent, long,
40  * int)}
41  * is called.
42  */
43 public final class DelegatedAdminNetworkLogsAvailableEvent extends Event {
44 
45     private static final long serialVersionUID = 1;
46     protected SerializableParcelWrapper<Intent> mIntent;
47     protected DelegatedAdminReceiverInfo mDelegatedAdminReceiver;
48     protected long mBatchToken;
49     protected int mNetworkLogsCount;
50 
51     /** Begins a query for {@link DelegatedAdminNetworkLogsAvailableEvent} events. */
queryPackage(String packageName)52     public static DelegatedAdminNetworkLogsAvailableEventQuery queryPackage(String packageName) {
53         return new DelegatedAdminNetworkLogsAvailableEventQuery(packageName);
54     }
55 
56     /** Begins logging a {@link DelegatedAdminNetworkLogsAvailableEvent}. */
logger( DelegatedAdminReceiver delegatedAdminReceiver, Context context, Intent intent, long batchToken, int networkLogsCount)57     public static DelegatedAdminNetworkLogsAvailableEventLogger logger(
58             DelegatedAdminReceiver delegatedAdminReceiver, Context context, Intent intent,
59             long batchToken, int networkLogsCount) {
60         return new DelegatedAdminNetworkLogsAvailableEventLogger(
61                 delegatedAdminReceiver, context, intent, batchToken, networkLogsCount);
62     }
63 
64     /**
65      * The {@link Intent} passed into
66      * {@link DelegatedAdminReceiver#onNetworkLogsAvailable(Context, Intent, long, int)}.
67      */
intent()68     public Intent intent() {
69         if (mIntent == null) {
70             return null;
71         }
72         return mIntent.get();
73     }
74 
75     /** Information about the {@link DelegatedAdminReceiver} which received the intent. */
delegatedAdminReceiver()76     public DelegatedAdminReceiverInfo delegatedAdminReceiver() {
77         return mDelegatedAdminReceiver;
78     }
79 
80     /**
81      * The {@code batchToken} passed into
82      * {@link DelegatedAdminReceiver#onNetworkLogsAvailable(Context, Intent, long, int)}.
83      */
batchToken()84     public long batchToken() {
85         return mBatchToken;
86     }
87 
88     /**
89      * The {@code networkLogsCount} passed into
90      * {@link DelegatedAdminReceiver#onNetworkLogsAvailable(Context, Intent, long, int)}.
91      */
networkLogsCount()92     public int networkLogsCount() {
93         return mNetworkLogsCount;
94     }
95 
96     @Override
toString()97     public String toString() {
98         return "DelegatedAdminNetworkLogsAvailableEvent{"
99                 + " intent=" + intent()
100                 + ", batchToken=" + mBatchToken
101                 + ", networkLogsCount=" + mNetworkLogsCount
102                 + ", delegatedAdminReceiver=" + mDelegatedAdminReceiver
103                 + ", packageName='" + mPackageName + "'"
104                 + ", timestamp=" + mTimestamp
105                 + "}";
106     }
107 
108     /** {@link EventLogsQuery} for {@link DelegatedAdminNetworkLogsAvailableEvent}. */
109     public static final class DelegatedAdminNetworkLogsAvailableEventQuery
110             extends EventLogsQuery<DelegatedAdminNetworkLogsAvailableEvent,
111             DelegatedAdminNetworkLogsAvailableEventQuery> {
112 
113         private static final long serialVersionUID = 1;
114 
115         DelegatedAdminReceiverQueryHelper<DelegatedAdminNetworkLogsAvailableEventQuery>
116                 mDelegatedAdminReceiver =
117                 new DelegatedAdminReceiverQueryHelper<>(this);
118         IntentQueryHelper<DelegatedAdminNetworkLogsAvailableEventQuery> mIntent =
119                 new IntentQueryHelper<>(this);
120         LongQueryHelper<DelegatedAdminNetworkLogsAvailableEventQuery> mBatchToken =
121                 new LongQueryHelper<>(this);
122         IntegerQueryHelper<DelegatedAdminNetworkLogsAvailableEventQuery> mNetworkLogsCount =
123                 new IntegerQueryHelper<>(this);
124 
DelegatedAdminNetworkLogsAvailableEventQuery(String packageName)125         private DelegatedAdminNetworkLogsAvailableEventQuery(String packageName) {
126             super(DelegatedAdminNetworkLogsAvailableEvent.class, packageName);
127         }
128 
129         /**
130          * Queries {@link Intent} passed into
131          * {@link DelegatedAdminReceiver#onNetworkLogsAvailable(Context, Intent, long, int)}.
132          */
133         @CheckResult
whereIntent()134         public IntentQueryHelper<DelegatedAdminNetworkLogsAvailableEventQuery> whereIntent() {
135             return mIntent;
136         }
137 
138         /** Queries {@link DelegatedAdminReceiver}. */
139         @CheckResult
whereDelegatedAdminReceiver()140         public DelegatedAdminReceiverQuery<DelegatedAdminNetworkLogsAvailableEventQuery> whereDelegatedAdminReceiver() {
141             return mDelegatedAdminReceiver;
142         }
143 
144         /**
145          * Query {@code batchToken} passed into
146          * {@link DelegatedAdminReceiver#onNetworkLogsAvailable(Context, Intent, long, int)}.
147          */
148         @CheckResult
whereBatchToken()149         public LongQueryHelper<DelegatedAdminNetworkLogsAvailableEventQuery> whereBatchToken() {
150             return mBatchToken;
151         }
152 
153         /**
154          * Query {@code networkLogsCount} passed into
155          * {@link DelegatedAdminReceiver#onNetworkLogsAvailable(Context, Intent, long, int)}.
156          */
157         @CheckResult
whereNetworkLogsCount()158         public IntegerQueryHelper<DelegatedAdminNetworkLogsAvailableEventQuery> whereNetworkLogsCount() {
159             return mNetworkLogsCount;
160         }
161 
162         @Override
filter(DelegatedAdminNetworkLogsAvailableEvent event)163         protected boolean filter(DelegatedAdminNetworkLogsAvailableEvent event) {
164             if (!mIntent.matches(event.mIntent)) {
165                 return false;
166             }
167             if (!mDelegatedAdminReceiver.matches(event.mDelegatedAdminReceiver)) {
168                 return false;
169             }
170             if (!mBatchToken.matches(event.mBatchToken)) {
171                 return false;
172             }
173             return mNetworkLogsCount.matches(event.mNetworkLogsCount);
174         }
175 
176         @Override
describeQuery(String fieldName)177         public String describeQuery(String fieldName) {
178             return toStringBuilder(DelegatedAdminNetworkLogsAvailableEvent.class, this)
179                     .field("intent", mIntent)
180                     .field("delegatedAdminReceiver", mDelegatedAdminReceiver)
181                     .field("batchToken", mBatchToken)
182                     .field("networkLogsCount", mNetworkLogsCount)
183                     .toString();
184         }
185     }
186 
187     /** {@link EventLogger} for {@link DelegatedAdminNetworkLogsAvailableEvent}. */
188     public static final class DelegatedAdminNetworkLogsAvailableEventLogger
189             extends EventLogger<DelegatedAdminNetworkLogsAvailableEvent> {
DelegatedAdminNetworkLogsAvailableEventLogger( DelegatedAdminReceiver delegatedAdminReceiver, Context context, Intent intent, long batchToken, int networkLogsCount)190         private DelegatedAdminNetworkLogsAvailableEventLogger(
191                 DelegatedAdminReceiver delegatedAdminReceiver, Context context, Intent intent,
192                 long batchToken, int networkLogsCount) {
193             super(context, new DelegatedAdminNetworkLogsAvailableEvent());
194             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
195             mEvent.mBatchToken = batchToken;
196             mEvent.mNetworkLogsCount = networkLogsCount;
197             setDelegatedAdminReceiver(delegatedAdminReceiver);
198         }
199 
200         /** Sets the {@link DelegatedAdminReceiver} which received this event. */
201         @CanIgnoreReturnValue
setDelegatedAdminReceiver( DelegatedAdminReceiver delegatedAdminReceiver)202         public DelegatedAdminNetworkLogsAvailableEventLogger setDelegatedAdminReceiver(
203                 DelegatedAdminReceiver delegatedAdminReceiver) {
204             mEvent.mDelegatedAdminReceiver = new DelegatedAdminReceiverInfo(delegatedAdminReceiver);
205             return this;
206         }
207 
208         /** Sets the {@link DelegatedAdminReceiver} which received this event. */
setDelegatedAdminReceiver( Class<? extends DelegatedAdminReceiver> delegatedAdminReceiverClass)209         public DelegatedAdminNetworkLogsAvailableEventLogger setDelegatedAdminReceiver(
210                 Class<? extends DelegatedAdminReceiver> delegatedAdminReceiverClass) {
211             mEvent.mDelegatedAdminReceiver = new DelegatedAdminReceiverInfo(
212                     delegatedAdminReceiverClass);
213             return this;
214         }
215 
216         /** Sets the {@link DelegatedAdminReceiver} which received this event. */
217         @CanIgnoreReturnValue
setDelegatedAdminReceiver( String delegatedAdminReceiverClassName)218         public DelegatedAdminNetworkLogsAvailableEventLogger setDelegatedAdminReceiver(
219                 String delegatedAdminReceiverClassName) {
220             mEvent.mDelegatedAdminReceiver = new DelegatedAdminReceiverInfo(
221                     delegatedAdminReceiverClassName);
222             return this;
223         }
224 
225         /** Sets the {@link Intent} which was received. */
setIntent(Intent intent)226         public DelegatedAdminNetworkLogsAvailableEventLogger setIntent(Intent intent) {
227             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
228             return this;
229         }
230 
231         /** Sets the {@code batchToken} which was received. */
setBatchToken(long batchToken)232         public DelegatedAdminNetworkLogsAvailableEventLogger setBatchToken(long batchToken) {
233             mEvent.mBatchToken = batchToken;
234             return this;
235         }
236 
237         /** Sets the {@code networkLogsCount} which was received. */
setNetworkLogsCount( int networkLogsCount)238         public DelegatedAdminNetworkLogsAvailableEventLogger setNetworkLogsCount(
239                 int networkLogsCount) {
240             mEvent.mNetworkLogsCount = networkLogsCount;
241             return this;
242         }
243     }
244 }
245