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 
21 import androidx.annotation.CheckResult;
22 
23 import com.android.eventlib.Event;
24 import com.android.eventlib.EventLogger;
25 import com.android.eventlib.EventLogsQuery;
26 import com.android.queryable.info.ServiceInfo;
27 import com.android.queryable.queries.ServiceQuery;
28 import com.android.queryable.queries.ServiceQueryHelper;
29 
30 import com.google.errorprone.annotations.CanIgnoreReturnValue;
31 
32 /**
33  * Event logged when {@link Service#onCreate()}
34  */
35 public class ServiceCreatedEvent extends Event {
36 
37     private static final long serialVersionUID = 1;
38 
39     /** Begins a query for {@link ServiceCreatedEvent} events. */
queryPackage(String packageName)40     public static ServiceCreatedEvent.ServiceCreatedEventQuery queryPackage(String packageName) {
41         return new ServiceCreatedEvent.ServiceCreatedEventQuery(packageName);
42     }
43 
44     /** {@link EventLogsQuery} for {@link ServiceCreatedEvent}. */
45     public static final class ServiceCreatedEventQuery
46             extends EventLogsQuery<ServiceCreatedEvent,
47             ServiceCreatedEvent.ServiceCreatedEventQuery> {
48 
49         private static final long serialVersionUID = 1;
50 
51         ServiceQueryHelper<ServiceCreatedEvent.ServiceCreatedEventQuery> mService =
52                 new ServiceQueryHelper<>(this);
53 
ServiceCreatedEventQuery(String packageName)54         private ServiceCreatedEventQuery(String packageName) {
55             super(ServiceCreatedEvent.class, packageName);
56         }
57 
58         /** Query {@link Service}. */
59         @CheckResult
whereService()60         public ServiceQuery<ServiceCreatedEvent.ServiceCreatedEventQuery> whereService() {
61             return mService;
62         }
63 
64         @Override
filter(ServiceCreatedEvent event)65         protected boolean filter(ServiceCreatedEvent event) {
66             if (!mService.matches(event.mService)) {
67                 return false;
68             }
69             return true;
70         }
71 
72         @Override
describeQuery(String fieldName)73         public String describeQuery(String fieldName) {
74             return toStringBuilder(ServiceCreatedEvent.class, this)
75                     .field("service", mService)
76                     .toString();
77         }
78     }
79 
80 
81     /** Begins logging a {@link ServiceCreatedEvent}. */
logger(Service service, String serviceName)82     public static ServiceCreatedEvent.ServiceCreatedEventLogger logger(Service service,
83             String serviceName) {
84         return new ServiceCreatedEvent.ServiceCreatedEventLogger(service, serviceName);
85     }
86 
87     /** {@link EventLogger} for {@link ServiceCreatedEvent}. */
88     public static final class ServiceCreatedEventLogger extends EventLogger<ServiceCreatedEvent> {
89 
90         // TODO(b/214187100) Use ServiceInfo here instead of a String to identify the service.
ServiceCreatedEventLogger(Service service, String serviceName)91         private ServiceCreatedEventLogger(Service service,
92                 String serviceName) {
93             super(service, new ServiceCreatedEvent());
94             setService(serviceName);
95         }
96 
97         /** Sets the {@link Service} which received this event. */
98         @CanIgnoreReturnValue
setService( String serviceName)99         public ServiceCreatedEvent.ServiceCreatedEventLogger setService(
100                 String serviceName) {
101             mEvent.mService = ServiceInfo.builder()
102                     .serviceClass(serviceName)
103                     .build();
104             return this;
105         }
106 
107     }
108 
109     protected ServiceInfo mService;
110 
111     /** Information about the {@link Service} which received the intent. */
service()112     public ServiceInfo service() {
113         return mService;
114     }
115 
116     @Override
toString()117     public String toString() {
118         return "ServiceCreatedEvent{"
119                 + ", service=" + mService
120                 + ", packageName='" + mPackageName + "'"
121                 + ", timestamp=" + mTimestamp
122                 + "}";
123     }
124 }
125