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