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.net.Uri;
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.IntegerQuery;
33 import com.android.queryable.queries.IntegerQueryHelper;
34 import com.android.queryable.queries.IntentQueryHelper;
35 import com.android.queryable.queries.StringQuery;
36 import com.android.queryable.queries.StringQueryHelper;
37 import com.android.queryable.queries.UriQuery;
38 import com.android.queryable.queries.UriQueryHelper;
39 import com.android.queryable.util.SerializableParcelWrapper;
40 
41 import com.google.errorprone.annotations.CanIgnoreReturnValue;
42 
43 /**
44  * Event logged when
45  * {@link DeviceAdminReceiver#onChoosePrivateKeyAlias(Context, Intent, int, Uri, String)} is called.
46  */
47 public final class DeviceAdminChoosePrivateKeyAliasEvent extends Event {
48 
49     private static final long serialVersionUID = 1;
50 
51     /** Begins a query for {@link DeviceAdminChoosePrivateKeyAliasEvent} events. */
queryPackage(String packageName)52     public static DeviceAdminChoosePrivateKeyAliasEventQuery queryPackage(String packageName) {
53         return new DeviceAdminChoosePrivateKeyAliasEventQuery(packageName);
54     }
55 
56     /** {@link EventLogsQuery} for {@link DeviceAdminChoosePrivateKeyAliasEvent}. */
57     public static final class DeviceAdminChoosePrivateKeyAliasEventQuery
58             extends EventLogsQuery<DeviceAdminChoosePrivateKeyAliasEvent,
59             DeviceAdminChoosePrivateKeyAliasEventQuery> {
60 
61         private static final long serialVersionUID = 1;
62 
63         DeviceAdminReceiverQueryHelper<DeviceAdminChoosePrivateKeyAliasEventQuery> mDeviceAdminReceiver =
64                 new DeviceAdminReceiverQueryHelper<>(this);
65         IntentQueryHelper<DeviceAdminChoosePrivateKeyAliasEventQuery> mIntent =
66                 new IntentQueryHelper<>(this);
67         IntegerQueryHelper<DeviceAdminChoosePrivateKeyAliasEventQuery> mUid =
68                 new IntegerQueryHelper<>(this);
69         UriQueryHelper<DeviceAdminChoosePrivateKeyAliasEventQuery> mUri =
70                 new UriQueryHelper<>(this);
71         StringQueryHelper<DeviceAdminChoosePrivateKeyAliasEventQuery> mAlias =
72                 new StringQueryHelper<>(this);
73 
DeviceAdminChoosePrivateKeyAliasEventQuery(String packageName)74         private DeviceAdminChoosePrivateKeyAliasEventQuery(String packageName) {
75             super(DeviceAdminChoosePrivateKeyAliasEvent.class, packageName);
76         }
77 
78         /**
79          * Queries {@link Intent} passed into
80          * {@link DeviceAdminReceiver#onChoosePrivateKeyAlias(Context, Intent, int, Uri, String).
81          */
82         @CheckResult
whereIntent()83         public IntentQueryHelper<DeviceAdminChoosePrivateKeyAliasEventQuery> whereIntent() {
84             return mIntent;
85         }
86 
87         /** Queries {@link DeviceAdminReceiver}. */
88         @CheckResult
whereDeviceAdminReceiver()89         public DeviceAdminReceiverQuery<DeviceAdminChoosePrivateKeyAliasEventQuery> whereDeviceAdminReceiver() {
90             return mDeviceAdminReceiver;
91         }
92 
93         /** Query {@code uid}. */
94         @CheckResult
whereUid()95         public IntegerQuery<DeviceAdminChoosePrivateKeyAliasEventQuery> whereUid() {
96             return mUid;
97         }
98 
99         /** Queries {@link Uri}. */
100         @CheckResult
whereUri()101         public UriQuery<DeviceAdminChoosePrivateKeyAliasEventQuery> whereUri() {
102             return mUri;
103         }
104 
105         /** Query {@code alias}. */
106         @CheckResult
whereAlias()107         public StringQuery<DeviceAdminChoosePrivateKeyAliasEventQuery> whereAlias() {
108             return mAlias;
109         }
110 
111         @Override
filter(DeviceAdminChoosePrivateKeyAliasEvent event)112         protected boolean filter(DeviceAdminChoosePrivateKeyAliasEvent event) {
113             if (!mIntent.matches(event.mIntent)) {
114                 return false;
115             }
116             if (!mDeviceAdminReceiver.matches(event.mDeviceAdminReceiver)) {
117                 return false;
118             }
119             if (!mUid.matches(event.mUid)) {
120                 return false;
121             }
122             if (!mUri.matches(event.mUri)) {
123                 return false;
124             }
125             if (!mAlias.matches(event.mAlias)) {
126                 return false;
127             }
128             return true;
129         }
130 
131         @Override
describeQuery(String fieldName)132         public String describeQuery(String fieldName) {
133             return toStringBuilder(DeviceAdminChoosePrivateKeyAliasEvent.class, this)
134                     .field("intent", mIntent)
135                     .field("deviceAdminReceiver", mDeviceAdminReceiver)
136                     .field("uid", mUid)
137                     .field("uri", mUri)
138                     .field("alias", mAlias)
139                     .toString();
140         }
141     }
142 
143     /** Begins logging a {@link DeviceAdminChoosePrivateKeyAliasEvent}. */
logger( DeviceAdminReceiver deviceAdminReceiver, Context context, Intent intent, int uid, Uri uri, String alias)144     public static DeviceAdminChoosePrivateKeyAliasEventLogger logger(
145             DeviceAdminReceiver deviceAdminReceiver, Context context,
146             Intent intent, int uid, Uri uri, String alias) {
147         return new DeviceAdminChoosePrivateKeyAliasEventLogger(
148                 deviceAdminReceiver, context, intent, uid, uri, alias);
149     }
150 
151     /** {@link EventLogger} for {@link DeviceAdminChoosePrivateKeyAliasEvent}. */
152     public static final class DeviceAdminChoosePrivateKeyAliasEventLogger
153             extends EventLogger<DeviceAdminChoosePrivateKeyAliasEvent> {
DeviceAdminChoosePrivateKeyAliasEventLogger( DeviceAdminReceiver deviceAdminReceiver, Context context, Intent intent, int uid, Uri uri, String alias)154         private DeviceAdminChoosePrivateKeyAliasEventLogger(
155                 DeviceAdminReceiver deviceAdminReceiver, Context context, Intent intent,
156                 int uid, Uri uri, String alias) {
157             super(context, new DeviceAdminChoosePrivateKeyAliasEvent());
158             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
159             mEvent.mUid = uid;
160             mEvent.mUri = new SerializableParcelWrapper<>(uri);
161             mEvent.mAlias = alias;
162             setDeviceAdminReceiver(deviceAdminReceiver);
163         }
164 
165         /** Sets the {@link DeviceAdminReceiver} which received this event. */
166         @CanIgnoreReturnValue
setDeviceAdminReceiver( DeviceAdminReceiver deviceAdminReceiver)167         public DeviceAdminChoosePrivateKeyAliasEventLogger setDeviceAdminReceiver(
168                 DeviceAdminReceiver deviceAdminReceiver) {
169             mEvent.mDeviceAdminReceiver = new DeviceAdminReceiverInfo(deviceAdminReceiver);
170             return this;
171         }
172 
173         /** Sets the {@link DeviceAdminReceiver} which received this event. */
setDeviceAdminReceiver( Class<? extends DeviceAdminReceiver> deviceAdminReceiverClass)174         public DeviceAdminChoosePrivateKeyAliasEventLogger setDeviceAdminReceiver(
175                 Class<? extends DeviceAdminReceiver> deviceAdminReceiverClass) {
176             mEvent.mDeviceAdminReceiver = new DeviceAdminReceiverInfo(deviceAdminReceiverClass);
177             return this;
178         }
179 
180         /** Sets the {@link DeviceAdminReceiver} which received this event. */
181         @CanIgnoreReturnValue
setDeviceAdminReceiver( String deviceAdminReceiverClassName)182         public DeviceAdminChoosePrivateKeyAliasEventLogger setDeviceAdminReceiver(
183                 String deviceAdminReceiverClassName) {
184             mEvent.mDeviceAdminReceiver = new DeviceAdminReceiverInfo(deviceAdminReceiverClassName);
185             return this;
186         }
187 
188         /** Sets the {@link Intent} which was received. */
setIntent(Intent intent)189         public DeviceAdminChoosePrivateKeyAliasEventLogger setIntent(Intent intent) {
190             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
191             return this;
192         }
193 
194         /** Sets the {@code uid} which was received. */
setUid(int uid)195         public DeviceAdminChoosePrivateKeyAliasEventLogger setUid(int uid) {
196             mEvent.mUid = uid;
197             return this;
198         }
199 
200         /** Sets the {@link Uri} which was received. */
setUri(Uri uri)201         public DeviceAdminChoosePrivateKeyAliasEventLogger setUri(Uri uri) {
202             mEvent.mUri = new SerializableParcelWrapper<>(uri);
203             return this;
204         }
205 
206         /** Sets the {@code alias} which was received. */
setAlias(String alias)207         public DeviceAdminChoosePrivateKeyAliasEventLogger setAlias(String alias) {
208             mEvent.mAlias = alias;
209             return this;
210         }
211     }
212 
213     protected SerializableParcelWrapper<Intent> mIntent;
214     protected DeviceAdminReceiverInfo mDeviceAdminReceiver;
215     protected int mUid;
216     protected SerializableParcelWrapper<Uri> mUri;
217     protected String mAlias;
218 
219     /**
220      * The {@link Intent} passed into
221      * {@link DeviceAdminReceiver#onChoosePrivateKeyAlias(Context, Intent, int, Uri, String)
222      */
intent()223     public Intent intent() {
224         if (mIntent == null) {
225             return null;
226         }
227         return mIntent.get();
228     }
229 
230     /** Information about the {@link DeviceAdminReceiver} which received the intent. */
deviceAdminReceiver()231     public DeviceAdminReceiverInfo deviceAdminReceiver() {
232         return mDeviceAdminReceiver;
233     }
234 
235     /**
236      * The {@code uid} passed into
237      * {@link DeviceAdminReceiver#onChoosePrivateKeyAlias(Context, Intent, int, Uri, String)
238      */
uid()239     public int uid() {
240         return mUid;
241     }
242 
243     /**
244      * The {@link Uri} passed into
245      * {@link DeviceAdminReceiver#onChoosePrivateKeyAlias(Context, Intent, int, Uri, String)
246      */
uri()247     public Uri uri() {
248         if (mUri == null) {
249             return null;
250         }
251         return mUri.get();
252     }
253 
254     /**
255      * The {@code alias} passed into
256      * {@link DeviceAdminReceiver#onChoosePrivateKeyAlias(Context, Intent, int, Uri, String)
257      */
alias()258     public String alias() {
259         return mAlias;
260     }
261 
262     @Override
toString()263     public String toString() {
264         return "DeviceAdminChoosePrivateKeyAliasEvent{"
265                 + " intent=" + intent()
266                 + ", uid=" + mUid
267                 + ", uri=" + uri()
268                 + ", alias=" + mAlias
269                 + ", deviceAdminReceiver=" + mDeviceAdminReceiver
270                 + ", packageName='" + mPackageName + "'"
271                 + ", timestamp=" + mTimestamp
272                 + "}";
273     }
274 }
275