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