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