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#onUserStarted(Context, Intent, UserHandle)} is called.
41  */
42 public final class DeviceAdminUserStartedEvent extends Event {
43 
44     private static final long serialVersionUID = 1;
45 
46     /** Begins a query for {@link DeviceAdminUserStartedEvent} events. */
queryPackage(String packageName)47     public static DeviceAdminUserStartedEventQuery queryPackage(String packageName) {
48         return new DeviceAdminUserStartedEventQuery(packageName);
49     }
50 
51     /** {@link EventLogsQuery} for {@link DeviceAdminUserStartedEvent}. */
52     public static final class DeviceAdminUserStartedEventQuery
53             extends EventLogsQuery<DeviceAdminUserStartedEvent,
54             DeviceAdminUserStartedEventQuery> {
55 
56         private static final long serialVersionUID = 1;
57 
58         DeviceAdminReceiverQueryHelper<DeviceAdminUserStartedEventQuery> mDeviceAdminReceiver =
59                 new DeviceAdminReceiverQueryHelper<>(this);
60         IntentQueryHelper<DeviceAdminUserStartedEventQuery> mIntent =
61                 new IntentQueryHelper<>(this);
62         UserHandleQueryHelper<DeviceAdminUserStartedEventQuery> mStartedUser =
63                 new UserHandleQueryHelper<>(this);
64 
DeviceAdminUserStartedEventQuery(String packageName)65         private DeviceAdminUserStartedEventQuery(String packageName) {
66             super(DeviceAdminUserStartedEvent.class, packageName);
67         }
68 
69         /**
70          * Queries {@link Intent} passed into
71          * {@link DeviceAdminReceiver#onUserStarted(Context, Intent, UserHandle)}.
72          */
73         @CheckResult
whereIntent()74         public IntentQueryHelper<DeviceAdminUserStartedEventQuery> whereIntent() {
75             return mIntent;
76         }
77 
78         /** Queries {@link DeviceAdminReceiver}. */
79         @CheckResult
whereDeviceAdminReceiver()80         public DeviceAdminReceiverQuery<DeviceAdminUserStartedEventQuery> whereDeviceAdminReceiver() {
81             return mDeviceAdminReceiver;
82         }
83 
84         /** Queries {@link UserHandle} passed into
85          * {@link DeviceAdminReceiver#onUserStarted(Context, Intent, UserHandle)}.
86          */
87         @CheckResult
whereStartedUser()88         public UserHandleQuery<DeviceAdminUserStartedEventQuery> whereStartedUser() {
89             return mStartedUser;
90         }
91 
92         @Override
filter(DeviceAdminUserStartedEvent event)93         protected boolean filter(DeviceAdminUserStartedEvent event) {
94             if (!mIntent.matches(event.mIntent)) {
95                 return false;
96             }
97             if (!mDeviceAdminReceiver.matches(event.mDeviceAdminReceiver)) {
98                 return false;
99             }
100             if (!mStartedUser.matches(event.mStartedUser)) {
101                 return false;
102             }
103             return true;
104         }
105 
106         @Override
describeQuery(String fieldName)107         public String describeQuery(String fieldName) {
108             return toStringBuilder(DeviceAdminUserStartedEvent.class, this)
109                     .field("intent", mIntent)
110                     .field("deviceAdminReceiver", mDeviceAdminReceiver)
111                     .field("startedUser", mStartedUser)
112                     .toString();
113         }
114     }
115 
116     /** Begins logging a {@link DeviceAdminUserStartedEvent}. */
logger( DeviceAdminReceiver deviceAdminReceiver, Context context, Intent intent, UserHandle startedUser)117     public static DeviceAdminUserStartedEventLogger logger(
118             DeviceAdminReceiver deviceAdminReceiver, Context context, Intent intent, UserHandle startedUser) {
119         return new DeviceAdminUserStartedEventLogger(deviceAdminReceiver, context, intent, startedUser);
120     }
121 
122     /** {@link EventLogger} for {@link DeviceAdminUserStartedEvent}. */
123     public static final class DeviceAdminUserStartedEventLogger
124             extends EventLogger<DeviceAdminUserStartedEvent> {
DeviceAdminUserStartedEventLogger( DeviceAdminReceiver deviceAdminReceiver, Context context, Intent intent, UserHandle startedUser)125         private DeviceAdminUserStartedEventLogger(
126                 DeviceAdminReceiver deviceAdminReceiver, Context context, Intent intent, UserHandle startedUser) {
127             super(context, new DeviceAdminUserStartedEvent());
128             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
129             mEvent.mStartedUser = new SerializableParcelWrapper<>(startedUser);
130             setDeviceAdminReceiver(deviceAdminReceiver);
131         }
132 
133         /** Sets the {@link DeviceAdminReceiver} which received this event. */
134         @CanIgnoreReturnValue
setDeviceAdminReceiver( DeviceAdminReceiver deviceAdminReceiver)135         public DeviceAdminUserStartedEventLogger 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 DeviceAdminUserStartedEventLogger 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 DeviceAdminUserStartedEventLogger 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 DeviceAdminUserStartedEventLogger setIntent(Intent intent) {
158             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
159             return this;
160         }
161 
162         /** Sets the {@link UserHandle}. */
setStartedUser(UserHandle startedUser)163         public DeviceAdminUserStartedEventLogger setStartedUser(UserHandle startedUser) {
164             mEvent.mStartedUser = new SerializableParcelWrapper<>(startedUser);
165             return this;
166         }
167     }
168 
169     protected SerializableParcelWrapper<Intent> mIntent;
170     protected SerializableParcelWrapper<UserHandle> mStartedUser;
171     protected DeviceAdminReceiverInfo mDeviceAdminReceiver;
172 
173     /**
174      * The {@link Intent} passed into
175      * {@link DeviceAdminReceiver#onUserStarted(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#onUserStarted(Context, Intent, UserHandle)}.
187      */
startedUser()188     public UserHandle startedUser() {
189         if (mStartedUser == null) {
190             return null;
191         }
192         return mStartedUser.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 "DeviceAdminUserStartedEvent{"
203                 + " intent=" + intent()
204                 + ", startedUser=" + startedUser()
205                 + ", deviceAdminReceiver=" + mDeviceAdminReceiver
206                 + ", packageName='" + mPackageName + "'"
207                 + ", timestamp=" + mTimestamp
208                 + "}";
209     }
210 }
211