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