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