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