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 
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.DelegatedAdminReceiverInfo;
29 import com.android.queryable.queries.DelegatedAdminReceiverQuery;
30 import com.android.queryable.queries.DelegatedAdminReceiverQueryHelper;
31 import com.android.queryable.queries.IntentQueryHelper;
32 import com.android.queryable.util.SerializableParcelWrapper;
33 
34 import com.google.errorprone.annotations.CanIgnoreReturnValue;
35 
36 /**
37  * Event logged when {@link DelegatedAdminReceiver#onSecurityLogsAvailable(Context, Intent)}
38  * is called.
39  */
40 public final class DelegatedAdminSecurityLogsAvailableEvent extends Event {
41 
42     private static final long serialVersionUID = 1;
43     protected SerializableParcelWrapper<Intent> mIntent;
44     protected DelegatedAdminReceiverInfo mDelegatedAdminReceiver;
45 
46     /** Begins a query for {@link DelegatedAdminSecurityLogsAvailableEvent} events. */
queryPackage(String packageName)47     public static DelegatedAdminSecurityLogsAvailableEventQuery queryPackage(String packageName) {
48         return new DelegatedAdminSecurityLogsAvailableEventQuery(packageName);
49     }
50 
51     /** Begins logging a {@link DelegatedAdminSecurityLogsAvailableEvent}. */
logger( DelegatedAdminReceiver delegatedAdminReceiver, Context context, Intent intent)52     public static DelegatedAdminSecurityLogsAvailableEventLogger logger(
53             DelegatedAdminReceiver delegatedAdminReceiver, Context context, Intent intent) {
54         return new DelegatedAdminSecurityLogsAvailableEventLogger(delegatedAdminReceiver, context,
55                 intent);
56     }
57 
58     /**
59      * The {@link Intent} passed into
60      * {@link DelegatedAdminReceiver#onSecurityLogsAvailable(Context, Intent)}.
61      */
intent()62     public Intent intent() {
63         if (mIntent == null) {
64             return null;
65         }
66         return mIntent.get();
67     }
68 
69     /** Information about the {@link DelegatedAdminReceiver} which received the intent. */
delegatedAdminReceiver()70     public DelegatedAdminReceiverInfo delegatedAdminReceiver() {
71         return mDelegatedAdminReceiver;
72     }
73 
74     @Override
toString()75     public String toString() {
76         return "DelegatedAdminSecurityLogsAvailableEvent{"
77                 + " intent=" + intent()
78                 + ", delegatedAdminReceiver=" + mDelegatedAdminReceiver
79                 + ", packageName='" + mPackageName + "'"
80                 + ", timestamp=" + mTimestamp
81                 + "}";
82     }
83 
84     /** {@link EventLogsQuery} for {@link DelegatedAdminSecurityLogsAvailableEvent}. */
85     public static final class DelegatedAdminSecurityLogsAvailableEventQuery
86             extends EventLogsQuery<DelegatedAdminSecurityLogsAvailableEvent,
87             DelegatedAdminSecurityLogsAvailableEventQuery> {
88 
89         private static final long serialVersionUID = 1;
90 
91         DelegatedAdminReceiverQueryHelper<DelegatedAdminSecurityLogsAvailableEventQuery>
92                 mDelegatedAdminReceiver =
93                 new DelegatedAdminReceiverQueryHelper<>(this);
94         IntentQueryHelper<DelegatedAdminSecurityLogsAvailableEventQuery> mIntent =
95                 new IntentQueryHelper<>(this);
96 
DelegatedAdminSecurityLogsAvailableEventQuery(String packageName)97         private DelegatedAdminSecurityLogsAvailableEventQuery(String packageName) {
98             super(DelegatedAdminSecurityLogsAvailableEvent.class, packageName);
99         }
100 
101         /**
102          * Queries {@link Intent} passed into
103          * {@link DelegatedAdminReceiver#onSecurityLogsAvailable(Context, Intent)}.
104          */
105         @CheckResult
whereIntent()106         public IntentQueryHelper<DelegatedAdminSecurityLogsAvailableEventQuery> whereIntent() {
107             return mIntent;
108         }
109 
110         /** Queries {@link DelegatedAdminReceiver}. */
111         @CheckResult
whereDelegatedAdminReceiver()112         public DelegatedAdminReceiverQuery<DelegatedAdminSecurityLogsAvailableEventQuery> whereDelegatedAdminReceiver() {
113             return mDelegatedAdminReceiver;
114         }
115 
116         @Override
filter(DelegatedAdminSecurityLogsAvailableEvent event)117         protected boolean filter(DelegatedAdminSecurityLogsAvailableEvent event) {
118             if (!mIntent.matches(event.mIntent)) {
119                 return false;
120             }
121             return mDelegatedAdminReceiver.matches(event.mDelegatedAdminReceiver);
122         }
123 
124         @Override
describeQuery(String fieldName)125         public String describeQuery(String fieldName) {
126             return toStringBuilder(DelegatedAdminSecurityLogsAvailableEvent.class, this)
127                     .field("intent", mIntent)
128                     .field("delegatedAdminReceiver", mDelegatedAdminReceiver)
129                     .toString();
130         }
131     }
132 
133     /** {@link EventLogger} for {@link DelegatedAdminSecurityLogsAvailableEvent}. */
134     public static final class DelegatedAdminSecurityLogsAvailableEventLogger
135             extends EventLogger<DelegatedAdminSecurityLogsAvailableEvent> {
DelegatedAdminSecurityLogsAvailableEventLogger( DelegatedAdminReceiver delegatedAdminReceiver, Context context, Intent intent)136         private DelegatedAdminSecurityLogsAvailableEventLogger(
137                 DelegatedAdminReceiver delegatedAdminReceiver, Context context, Intent intent) {
138             super(context, new DelegatedAdminSecurityLogsAvailableEvent());
139             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
140             setDelegatedAdminReceiver(delegatedAdminReceiver);
141         }
142 
143         /** Sets the {@link DelegatedAdminReceiver} which received this event. */
144         @CanIgnoreReturnValue
setDelegatedAdminReceiver( DelegatedAdminReceiver delegatedAdminReceiver)145         public DelegatedAdminSecurityLogsAvailableEventLogger setDelegatedAdminReceiver(
146                 DelegatedAdminReceiver delegatedAdminReceiver) {
147             mEvent.mDelegatedAdminReceiver = new DelegatedAdminReceiverInfo(delegatedAdminReceiver);
148             return this;
149         }
150 
151         /** Sets the {@link DelegatedAdminReceiver} which received this event. */
setDelegatedAdminReceiver( Class<? extends DelegatedAdminReceiver> delegatedAdminReceiverClass)152         public DelegatedAdminSecurityLogsAvailableEventLogger setDelegatedAdminReceiver(
153                 Class<? extends DelegatedAdminReceiver> delegatedAdminReceiverClass) {
154             mEvent.mDelegatedAdminReceiver = new DelegatedAdminReceiverInfo(
155                     delegatedAdminReceiverClass);
156             return this;
157         }
158 
159         /** Sets the {@link DelegatedAdminReceiver} which received this event. */
160         @CanIgnoreReturnValue
setDelegatedAdminReceiver( String delegatedAdminReceiverClassName)161         public DelegatedAdminSecurityLogsAvailableEventLogger setDelegatedAdminReceiver(
162                 String delegatedAdminReceiverClassName) {
163             mEvent.mDelegatedAdminReceiver = new DelegatedAdminReceiverInfo(
164                     delegatedAdminReceiverClassName);
165             return this;
166         }
167 
168         /** Sets the {@link Intent} which was received. */
setIntent(Intent intent)169         public DelegatedAdminSecurityLogsAvailableEventLogger setIntent(Intent intent) {
170             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
171             return this;
172         }
173     }
174 }
175