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