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