• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.services;
18 
19 import android.app.Service;
20 import android.content.Intent;
21 
22 import androidx.annotation.CheckResult;
23 
24 import com.android.eventlib.Event;
25 import com.android.eventlib.EventLogger;
26 import com.android.eventlib.EventLogsQuery;
27 import com.android.queryable.info.ServiceInfo;
28 import com.android.queryable.queries.IntentQuery;
29 import com.android.queryable.queries.IntentQueryHelper;
30 import com.android.queryable.queries.ServiceQuery;
31 import com.android.queryable.queries.ServiceQueryHelper;
32 import com.android.queryable.util.SerializableParcelWrapper;
33 
34 /**
35  * Event logged when {@link Service#onBind(Intent)}
36  */
37 public class ServiceBoundEvent extends Event {
38 
39     private static final long serialVersionUID = 1;
40 
41     /** Begins a query for {@link ServiceBoundEvent} events. */
queryPackage(String packageName)42     public static ServiceBoundEventQuery queryPackage(String packageName) {
43         return new ServiceBoundEventQuery(packageName);
44     }
45 
46     /** {@link EventLogsQuery} for {@link ServiceBoundEvent}. */
47     public static final class ServiceBoundEventQuery
48             extends EventLogsQuery<ServiceBoundEvent, ServiceBoundEventQuery> {
49 
50         private static final long serialVersionUID = 1;
51 
52         ServiceQueryHelper<ServiceBoundEventQuery> mService = new ServiceQueryHelper<>(this);
53         IntentQueryHelper<ServiceBoundEventQuery> mIntent = new IntentQueryHelper<>(this);
54 
ServiceBoundEventQuery(String packageName)55         private ServiceBoundEventQuery(String packageName) {
56             super(ServiceBoundEvent.class, packageName);
57         }
58 
59         /**
60          * Query {@link Intent} passed into {@link Service#onBind(Intent)}.
61          */
62         @CheckResult
whereIntent()63         public IntentQuery<ServiceBoundEventQuery> whereIntent() {
64             return mIntent;
65         }
66 
67         /** Query {@link Service}. */
68         @CheckResult
whereService()69         public ServiceQuery<ServiceBoundEvent.ServiceBoundEventQuery> whereService() {
70             return mService;
71         }
72 
73         @Override
filter(ServiceBoundEvent event)74         protected boolean filter(ServiceBoundEvent event) {
75             if (!mIntent.matches(event.mIntent)) {
76                 return false;
77             }
78             if (!mService.matches(event.mService)) {
79                 return false;
80             }
81             return true;
82         }
83 
84         @Override
describeQuery(String fieldName)85         public String describeQuery(String fieldName) {
86             return toStringBuilder(ServiceBoundEvent.class, this)
87                     .field("intent", mIntent)
88                     .field("service", mService)
89                     .toString();
90         }
91     }
92 
93 
94     /** Begins logging a {@link ServiceBoundEvent}. */
logger(Service service, String serviceName, Intent intent)95     public static ServiceBoundEventLogger logger(Service service,
96             String serviceName, Intent intent) {
97         return new ServiceBoundEventLogger(service, serviceName, intent);
98     }
99 
100     /** {@link EventLogger} for {@link ServiceBoundEvent}. */
101     public static final class ServiceBoundEventLogger extends EventLogger<ServiceBoundEvent> {
102 
103         // TODO(b/214187100) Use ServiceInfo here instead of a String to identify the service.
ServiceBoundEventLogger(Service service, String serviceName, Intent intent)104         private ServiceBoundEventLogger(Service service, String serviceName,
105                 Intent intent) {
106             super(service, new ServiceBoundEvent());
107             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
108             setService(serviceName);
109         }
110 
111         /** Sets the {@link Service} which received this event. */
setService(String serviceName)112         public ServiceBoundEventLogger setService(String serviceName) {
113             mEvent.mService = ServiceInfo.builder()
114                     .serviceClass(serviceName)
115                     .build();
116             return this;
117         }
118 
119         /** Sets the {@link Intent} which was used to bind to the service. */
setIntent(Intent intent)120         public ServiceBoundEventLogger setIntent(Intent intent) {
121             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
122             return this;
123         }
124 
125     }
126 
127     protected ServiceInfo mService;
128     protected SerializableParcelWrapper<Intent> mIntent;
129 
130     /**
131      * The {@link Intent} passed into {@link Service#onBind(Intent)}.
132      */
intent()133     public Intent intent() {
134         if (mIntent == null) {
135             return null;
136         }
137         return mIntent.get();
138     }
139 
140     /** Information about the {@link Service} which received the intent. */
service()141     public ServiceInfo service() {
142         return mService;
143     }
144 
145     @Override
toString()146     public String toString() {
147         return "ServiceBoundEvent{"
148                 + " intent=" + intent()
149                 + ", service=" + mService
150                 + ", packageName='" + mPackageName + "'"
151                 + ", timestamp=" + mTimestamp
152                 + "}";
153     }
154 }
155