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