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.broadcastreceivers;
18 
19 import android.content.BroadcastReceiver;
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.BroadcastReceiverInfo;
29 import com.android.queryable.queries.BroadcastReceiverQuery;
30 import com.android.queryable.queries.BroadcastReceiverQueryHelper;
31 import com.android.queryable.queries.IntegerQuery;
32 import com.android.queryable.queries.IntegerQueryHelper;
33 import com.android.queryable.queries.IntentQuery;
34 import com.android.queryable.queries.IntentQueryHelper;
35 import com.android.queryable.util.SerializableParcelWrapper;
36 
37 import com.google.errorprone.annotations.CanIgnoreReturnValue;
38 
39 /**
40  * Event logged when {@link BroadcastReceiver#onReceive(Context, Intent)} is called.
41  */
42 public final class BroadcastReceivedEvent extends Event {
43 
44     private static final long serialVersionUID = 1;
45 
46     /** Begins a query for {@link BroadcastReceivedEvent} events. */
queryPackage(String packageName)47     public static BroadcastReceivedEventQuery queryPackage(String packageName) {
48         return new BroadcastReceivedEventQuery(packageName);
49     }
50 
51     /** {@link EventLogsQuery} for {@link BroadcastReceivedEvent}. */
52     public static final class BroadcastReceivedEventQuery
53             extends EventLogsQuery<BroadcastReceivedEvent, BroadcastReceivedEventQuery> {
54 
55         private static final long serialVersionUID = 1;
56 
57         BroadcastReceiverQueryHelper<BroadcastReceivedEventQuery> mBroadcastReceiver =
58                 new BroadcastReceiverQueryHelper<>(this);
59         IntentQueryHelper<BroadcastReceivedEventQuery> mIntent = new IntentQueryHelper<>(this);
60         IntegerQueryHelper<BroadcastReceivedEventQuery> mResultCode = new IntegerQueryHelper<>(
61                 this);
62 
BroadcastReceivedEventQuery(String packageName)63         private BroadcastReceivedEventQuery(String packageName) {
64             super(BroadcastReceivedEvent.class, packageName);
65         }
66 
67         /**
68          * Query {@link Intent} passed into {@link BroadcastReceiver#onReceive(Context, Intent)}.
69          */
70         @CheckResult
whereIntent()71         public IntentQuery<BroadcastReceivedEventQuery> whereIntent() {
72             return mIntent;
73         }
74 
75         /**
76          * Query result code received via {@link BroadcastReceiver#getResultCode()} in {@link
77          * BroadcastReceiver#onReceive(Context, Intent)}.
78          */
79         @CheckResult
whereResultCode()80         public IntegerQuery<BroadcastReceivedEventQuery> whereResultCode() {
81             return mResultCode;
82         }
83 
84         /** Query {@link BroadcastReceiver}. */
85         @CheckResult
whereBroadcastReceiver()86         public BroadcastReceiverQuery<BroadcastReceivedEventQuery> whereBroadcastReceiver() {
87             return mBroadcastReceiver;
88         }
89 
90         @Override
filter(BroadcastReceivedEvent event)91         protected boolean filter(BroadcastReceivedEvent event) {
92             if (!mIntent.matches(event.mIntent)) {
93                 return false;
94             }
95             if (!mBroadcastReceiver.matches(event.mBroadcastReceiver)) {
96                 return false;
97             }
98             if (!mResultCode.matches(event.mResultCode)) {
99                 return false;
100             }
101             return true;
102         }
103 
104         @Override
describeQuery(String fieldName)105         public String describeQuery(String fieldName) {
106             return toStringBuilder(BroadcastReceivedEvent.class, this)
107                     .field("intent", mIntent)
108                     .field("broadcastReceiver", mBroadcastReceiver)
109                     .toString();
110         }
111     }
112 
113     /** Begins logging a {@link BroadcastReceivedEvent}. */
logger( BroadcastReceiver broadcastReceiver, Context context, Intent intent, int resultCode)114     public static BroadcastReceivedEventLogger logger(
115             BroadcastReceiver broadcastReceiver, Context context, Intent intent, int resultCode) {
116         return new BroadcastReceivedEventLogger(broadcastReceiver, context, intent, resultCode);
117     }
118 
119     /** {@link EventLogger} for {@link BroadcastReceivedEvent}. */
120     public static final class BroadcastReceivedEventLogger
121             extends EventLogger<BroadcastReceivedEvent> {
BroadcastReceivedEventLogger( BroadcastReceiver broadcastReceiver, Context context, Intent intent, int resultCode)122         private BroadcastReceivedEventLogger(
123                 BroadcastReceiver broadcastReceiver, Context context, Intent intent,
124                 int resultCode) {
125             super(context, new BroadcastReceivedEvent());
126             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
127             mEvent.mResultCode = resultCode;
128             setBroadcastReceiver(broadcastReceiver);
129         }
130 
131         /** Sets the {@link BroadcastReceiver} which received this event. */
132         @CanIgnoreReturnValue
setBroadcastReceiver( BroadcastReceiver broadcastReceiver)133         public BroadcastReceivedEventLogger setBroadcastReceiver(
134                 BroadcastReceiver broadcastReceiver) {
135             mEvent.mBroadcastReceiver = new BroadcastReceiverInfo(broadcastReceiver);
136             return this;
137         }
138 
139         /** Sets the {@link BroadcastReceiver} which received this event. */
setBroadcastReceiver( Class<? extends BroadcastReceiver> broadcastReceiverClass)140         public BroadcastReceivedEventLogger setBroadcastReceiver(
141                 Class<? extends BroadcastReceiver> broadcastReceiverClass) {
142             mEvent.mBroadcastReceiver = new BroadcastReceiverInfo(broadcastReceiverClass);
143             return this;
144         }
145 
146         /** Sets the {@link BroadcastReceiver} which received this event. */
147         @CanIgnoreReturnValue
setBroadcastReceiver( String broadcastReceiverClassName)148         public BroadcastReceivedEventLogger setBroadcastReceiver(
149                 String broadcastReceiverClassName) {
150             mEvent.mBroadcastReceiver = new BroadcastReceiverInfo(broadcastReceiverClassName);
151             return this;
152         }
153 
154         /** Sets the {@link Intent} which was received. */
155         @CanIgnoreReturnValue
setIntent(Intent intent)156         public BroadcastReceivedEventLogger setIntent(Intent intent) {
157             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
158             return this;
159         }
160     }
161 
162     protected SerializableParcelWrapper<Intent> mIntent;
163     protected BroadcastReceiverInfo mBroadcastReceiver;
164     protected int mResultCode;
165 
166     /**
167      * The {@link Intent} passed into {@link BroadcastReceiver#onReceive(Context, Intent)}.
168      */
intent()169     public Intent intent() {
170         if (mIntent == null) {
171             return null;
172         }
173         return mIntent.get();
174     }
175 
176     /** Information about the {@link BroadcastReceiver} which received the intent. */
broadcastReceiver()177     public BroadcastReceiverInfo broadcastReceiver() {
178         return mBroadcastReceiver;
179     }
180 
181     @Override
toString()182     public String toString() {
183         return "BroadcastReceivedEvent{"
184                 + " intent=" + intent()
185                 + " resultCode=" + mResultCode
186                 + ", broadcastReceiver=" + mBroadcastReceiver
187                 + ", packageName='" + mPackageName + "'"
188                 + ", timestamp=" + mTimestamp
189                 + "}";
190     }
191 }
192