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