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