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