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