• 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#onRebind(Intent)}
36  */
37 public class ServiceReboundEvent extends Event {
38 
39     private static final long serialVersionUID = 1;
40 
41     /** Begins a query for {@link ServiceReboundEvent} events. */
queryPackage(String packageName)42     public static ServiceReboundEvent.ServiceReboundEventQuery queryPackage(String packageName) {
43         return new ServiceReboundEvent.ServiceReboundEventQuery(packageName);
44     }
45 
46     /** {@link EventLogsQuery} for {@link ServiceReboundEvent}. */
47     public static final class ServiceReboundEventQuery
48             extends EventLogsQuery<ServiceReboundEvent,
49             ServiceReboundEvent.ServiceReboundEventQuery> {
50 
51         private static final long serialVersionUID = 1;
52 
53         ServiceQueryHelper<ServiceReboundEvent.ServiceReboundEventQuery> mService =
54                 new ServiceQueryHelper<>(this);
55         IntentQueryHelper<ServiceReboundEvent.ServiceReboundEventQuery> mIntent =
56                 new IntentQueryHelper<>(this);
57 
ServiceReboundEventQuery(String packageName)58         private ServiceReboundEventQuery(String packageName) {
59             super(ServiceReboundEvent.class, packageName);
60         }
61 
62         /**
63          * Query {@link Intent} passed into {@link Service#onRebind(Intent)}.
64          */
65         @CheckResult
whereIntent()66         public IntentQuery<ServiceReboundEvent.ServiceReboundEventQuery> whereIntent() {
67             return mIntent;
68         }
69 
70         /** Query {@link Service}. */
71         @CheckResult
whereService()72         public ServiceQuery<ServiceReboundEvent.ServiceReboundEventQuery> whereService() {
73             return mService;
74         }
75 
76         @Override
filter(ServiceReboundEvent event)77         protected boolean filter(ServiceReboundEvent 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(ServiceReboundEvent.class, this)
90                     .field("intent", mIntent)
91                     .field("service", mService)
92                     .toString();
93         }
94     }
95 
96 
97     /** Begins logging a {@link ServiceReboundEvent}. */
logger(Service service, String serviceName, Intent intent)98     public static ServiceReboundEvent.ServiceReboundEventLogger logger(Service service,
99             String serviceName, Intent intent) {
100         return new ServiceReboundEvent.ServiceReboundEventLogger(service, serviceName, intent);
101     }
102 
103     /** {@link EventLogger} for {@link ServiceReboundEvent}. */
104     public static final class ServiceReboundEventLogger extends EventLogger<ServiceReboundEvent> {
105 
ServiceReboundEventLogger(Service service, String serviceName, Intent intent)106         private ServiceReboundEventLogger(Service service,
107                 String serviceName,
108                 Intent intent) {
109             super(service, new ServiceReboundEvent());
110             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
111             setService(serviceName);
112         }
113 
114         /** Sets the {@link Service} which received this event. */
setService( String serviceName)115         public ServiceReboundEvent.ServiceReboundEventLogger setService(
116                 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 ServiceReboundEvent.ServiceReboundEventLogger 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#onRebind(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 "ServiceReboundEvent{"
152                 + " intent=" + intent()
153                 + ", service=" + mService
154                 + ", packageName='" + mPackageName + "'"
155                 + ", timestamp=" + mTimestamp
156                 + "}";
157     }
158 }
159