• 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.IntegerQueryHelper;
29 import com.android.queryable.queries.IntentQuery;
30 import com.android.queryable.queries.IntentQueryHelper;
31 import com.android.queryable.queries.ServiceQuery;
32 import com.android.queryable.queries.ServiceQueryHelper;
33 import com.android.queryable.util.SerializableParcelWrapper;
34 
35 /**
36  * Event logged when {@link Service#onStartCommand(Intent, int, int)}
37  */
38 public class ServiceStartedEvent extends Event {
39 
40     private static final long serialVersionUID = 1;
41 
42     /** Begins a query for {@link ServiceStartedEvent} events. */
queryPackage(String packageName)43     public static ServiceStartedEventQuery queryPackage(String packageName) {
44         return new ServiceStartedEventQuery(packageName);
45     }
46 
47     /** {@link EventLogsQuery} for {@link ServiceStartedEvent}. */
48     public static final class ServiceStartedEventQuery extends EventLogsQuery<ServiceStartedEvent,
49             ServiceStartedEventQuery> {
50 
51         private static final long serialVersionUID = 1;
52 
53         ServiceQueryHelper<ServiceStartedEventQuery> mService = new ServiceQueryHelper<>(this);
54         IntentQueryHelper<ServiceStartedEventQuery> mIntent = new IntentQueryHelper<>(this);
55         IntegerQueryHelper<ServiceStartedEventQuery> mFlags = new IntegerQueryHelper<>(this);
56         IntegerQueryHelper<ServiceStartedEventQuery> mStartId = new IntegerQueryHelper<>(this);
57 
ServiceStartedEventQuery(String packageName)58         private ServiceStartedEventQuery(String packageName) {
59             super(ServiceStartedEvent.class, packageName);
60         }
61 
62         /** Query {@link Service}. */
63         @CheckResult
whereService()64         public ServiceQuery<ServiceStartedEventQuery> whereService() {
65             return mService;
66         }
67 
68         /**
69          * Query {@link Intent} passed into {@link Service#onBind(Intent)}.
70          */
71         @CheckResult
whereIntent()72         public IntentQuery<ServiceStartedEventQuery> whereIntent() {
73             return mIntent;
74         }
75 
76         /** Query {@link Service}. */
77         @CheckResult
whereFlags()78         public IntegerQueryHelper<ServiceStartedEventQuery> whereFlags() {
79             return mFlags;
80         }
81 
82         /** Query {@link Service}. */
83         @CheckResult
whereStartId()84         public IntegerQueryHelper<ServiceStartedEventQuery> whereStartId() {
85             return mStartId;
86         }
87 
88         @Override
filter(ServiceStartedEvent event)89         protected boolean filter(ServiceStartedEvent event) {
90             if (!mFlags.matches(event.mFlags)) {
91                 return false;
92             }
93             if (!mStartId.matches(event.mStartId)) {
94                 return false;
95             }
96             if (!mIntent.matches(event.mIntent)) {
97                 return false;
98             }
99             if (!mService.matches(event.mService)) {
100                 return false;
101             }
102             return true;
103         }
104 
105         @Override
describeQuery(String fieldName)106         public String describeQuery(String fieldName) {
107             return toStringBuilder(ServiceStartedEvent.class, this)
108                     .field("flags", mFlags)
109                     .field("startId", mStartId)
110                     .field("intent", mIntent)
111                     .field("service", mService)
112                     .toString();
113         }
114     }
115 
116 
117     /** Begins logging a {@link ServiceStartedEvent}. */
logger(Service service, String serviceName, Intent intent, int flags, int startId)118     public static ServiceStartedEventLogger logger(Service service,
119             String serviceName, Intent intent, int flags, int startId) {
120         return new ServiceStartedEventLogger(service, serviceName, intent, flags, startId);
121     }
122 
123     /** {@link EventLogger} for {@link ServiceStartedEvent}. */
124     public static final class ServiceStartedEventLogger extends EventLogger<ServiceStartedEvent> {
125 
126         // TODO(b/214187100) Use ServiceInfo here instead of a String to identify the service.
ServiceStartedEventLogger(Service service, String serviceName, Intent intent, int flags, int startId)127         private ServiceStartedEventLogger(Service service,
128                 String serviceName, Intent intent, int flags, int startId) {
129             super(service, new ServiceStartedEvent());
130             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
131             mEvent.mFlags = flags;
132             mEvent.mStartId = startId;
133             setService(serviceName);
134         }
135 
136         /** Sets the {@link Service} which received this event. */
setService(String serviceName)137         public ServiceStartedEventLogger setService(String serviceName) {
138             mEvent.mService = ServiceInfo.builder()
139                     .serviceClass(serviceName)
140                     .build();
141             return this;
142         }
143 
144         /** Sets the {@link Intent} supplied to {@link android.content.Context#startService}. */
setIntent(Intent intent)145         public ServiceStartedEventLogger setIntent(Intent intent) {
146             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
147             return this;
148         }
149 
150         /** Sets the flags used for the start request of this service. */
setFlags(int flags)151         public ServiceStartedEventLogger setFlags(int flags) {
152             mEvent.mFlags = flags;
153             return this;
154         }
155 
156         /** Sets the startId. */
setStartId(int startId)157         public ServiceStartedEventLogger setStartId(int startId) {
158             mEvent.mStartId = startId;
159             return this;
160         }
161 
162     }
163 
164     protected ServiceInfo mService;
165     protected SerializableParcelWrapper<Intent> mIntent;
166     protected int mFlags;
167     protected int mStartId;
168 
169     /**
170      * The {@link Intent} passed into {@link Service#onStartCommand(Intent, int, int)}.
171      */
intent()172     public Intent intent() {
173         if (mIntent == null) {
174             return null;
175         }
176         return mIntent.get();
177     }
178 
179     /**
180      * The flags passed into {@link Service#onStartCommand(Intent, int, int)}.
181      */
flags()182     public int flags() {
183         return mFlags;
184     }
185 
186     /**
187      * The startId passed into {@link Service#onStartCommand(Intent, int, int)}.
188      */
startId()189     public int startId() {
190         return mStartId;
191     }
192 
193     /** Information about the {@link Service} which received the intent. */
service()194     public ServiceInfo service() {
195         return mService;
196     }
197 
198     @Override
toString()199     public String toString() {
200         return "ServiceStartedEvent{"
201                 + ", service=" + mService
202                 + ", flags=" + mFlags
203                 + ", startId=" + mStartId
204                 + ", packageName='" + mPackageName + "'"
205                 + ", timestamp=" + mTimestamp
206                 + "}";
207     }
208 }
209