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