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 import com.google.errorprone.annotations.CanIgnoreReturnValue;
35 
36 /**
37  * Event logged when {@link Service#onTaskRemoved(Intent)}
38  */
39 public class ServiceTaskRemovedEvent extends Event {
40 
41     private static final long serialVersionUID = 1;
42 
43     /** Begins a query for {@link ServiceTaskRemovedEvent} events. */
queryPackage( String packageName)44     public static ServiceTaskRemovedEvent.ServiceTaskRemovedEventQuery queryPackage(
45             String packageName) {
46         return new ServiceTaskRemovedEvent.ServiceTaskRemovedEventQuery(packageName);
47     }
48 
49     /** {@link EventLogsQuery} for {@link ServiceTaskRemovedEvent}. */
50     public static final class ServiceTaskRemovedEventQuery
51             extends EventLogsQuery<ServiceTaskRemovedEvent,
52             ServiceTaskRemovedEvent.ServiceTaskRemovedEventQuery> {
53 
54         private static final long serialVersionUID = 1;
55 
56         ServiceQueryHelper<ServiceTaskRemovedEvent.ServiceTaskRemovedEventQuery> mService =
57                 new ServiceQueryHelper<>(this);
58         IntentQueryHelper<ServiceTaskRemovedEvent.ServiceTaskRemovedEventQuery> mIntent =
59                 new IntentQueryHelper<>(this);
60 
ServiceTaskRemovedEventQuery(String packageName)61         private ServiceTaskRemovedEventQuery(String packageName) {
62             super(ServiceTaskRemovedEvent.class, packageName);
63         }
64 
65         /**
66          * Query {@link Intent} passed into {@link Service#onTaskRemoved(Intent)}.
67          */
68         @CheckResult
whereIntent()69         public IntentQuery<ServiceTaskRemovedEvent.ServiceTaskRemovedEventQuery> whereIntent() {
70             return mIntent;
71         }
72 
73         /** Query {@link Service}. */
74         @CheckResult
whereService()75         public ServiceQuery<ServiceTaskRemovedEvent.ServiceTaskRemovedEventQuery> whereService() {
76             return mService;
77         }
78 
79         @Override
filter(ServiceTaskRemovedEvent event)80         protected boolean filter(ServiceTaskRemovedEvent event) {
81             if (!mIntent.matches(event.mIntent)) {
82                 return false;
83             }
84             if (!mService.matches(event.mService)) {
85                 return false;
86             }
87             return true;
88         }
89 
90         @Override
describeQuery(String fieldName)91         public String describeQuery(String fieldName) {
92             return toStringBuilder(ServiceTaskRemovedEvent.class, this)
93                     .field("intent", mIntent)
94                     .field("service", mService)
95                     .toString();
96         }
97     }
98 
99 
100     /** Begins logging a {@link ServiceTaskRemovedEvent}. */
logger(Service service, String serviceName, Intent intent)101     public static ServiceTaskRemovedEvent.ServiceTaskRemovedEventLogger logger(Service service,
102             String serviceName, Intent intent) {
103         return new ServiceTaskRemovedEvent.ServiceTaskRemovedEventLogger(service, serviceName,
104                 intent);
105     }
106 
107     /** {@link EventLogger} for {@link ServiceTaskRemovedEvent}. */
108     public static final class ServiceTaskRemovedEventLogger extends
109             EventLogger<ServiceTaskRemovedEvent> {
110 
ServiceTaskRemovedEventLogger(Service service, String serviceName, Intent intent)111         private ServiceTaskRemovedEventLogger(Service service,
112                 String serviceName,
113                 Intent intent) {
114             super(service, new ServiceTaskRemovedEvent());
115             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
116             setService(serviceName);
117         }
118 
119         /** Sets the {@link Service} which received this event. */
120         @CanIgnoreReturnValue
setService( String serviceName)121         public ServiceTaskRemovedEvent.ServiceTaskRemovedEventLogger setService(
122                 String serviceName) {
123             mEvent.mService = ServiceInfo.builder()
124                     .serviceClass(serviceName)
125                     .build();
126             return this;
127         }
128 
129         /** Sets the {@link Intent} that was used to bind to the service. */
130         @CanIgnoreReturnValue
setIntent(Intent intent)131         public ServiceTaskRemovedEvent.ServiceTaskRemovedEventLogger setIntent(Intent intent) {
132             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
133             return this;
134         }
135 
136     }
137 
138     protected ServiceInfo mService;
139     protected SerializableParcelWrapper<Intent> mIntent;
140 
141     /**
142      * The {@link Intent} passed into {@link Service#onTaskRemoved(Intent)}.
143      */
intent()144     public Intent intent() {
145         if (mIntent == null) {
146             return null;
147         }
148         return mIntent.get();
149     }
150 
151     /** Information about the {@link Service} which received the intent. */
service()152     public ServiceInfo service() {
153         return mService;
154     }
155 
156     @Override
toString()157     public String toString() {
158         return "ServiceTaskRemovedEvent{"
159                 + " intent=" + intent()
160                 + ", service=" + mService
161                 + ", packageName='" + mPackageName + "'"
162                 + ", timestamp=" + mTimestamp
163                 + "}";
164     }
165 }
166