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