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