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