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