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.res.Configuration;
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.ServiceQuery;
29 import com.android.queryable.queries.ServiceQueryHelper;
30 
31 import com.google.errorprone.annotations.CanIgnoreReturnValue;
32 
33 /**
34  * Event logged when {@link Service#onConfigurationChanged(Configuration)}
35  */
36 public class ServiceConfigurationChangedEvent extends Event {
37 
38     private static final long serialVersionUID = 1;
39 
40     /** Begins a query for {@link ServiceConfigurationChangedEvent} events. */
41     public static ServiceConfigurationChangedEvent.ServiceConfigurationChangedEventQuery
queryPackage(String packageName)42             queryPackage(String packageName) {
43         return new ServiceConfigurationChangedEvent
44                 .ServiceConfigurationChangedEventQuery(packageName);
45     }
46 
47     /** {@link EventLogsQuery} for {@link ServiceConfigurationChangedEvent}. */
48     public static final class ServiceConfigurationChangedEventQuery
49             extends EventLogsQuery<ServiceConfigurationChangedEvent,
50             ServiceConfigurationChangedEvent.ServiceConfigurationChangedEventQuery> {
51 
52         private static final long serialVersionUID = 1;
53 
54         ServiceQueryHelper<ServiceConfigurationChangedEvent.ServiceConfigurationChangedEventQuery>
55                 mService = new ServiceQueryHelper<>(this);
56 
ServiceConfigurationChangedEventQuery(String packageName)57         private ServiceConfigurationChangedEventQuery(String packageName) {
58             super(ServiceConfigurationChangedEvent.class, packageName);
59         }
60 
61         /** Query {@link Service}. */
62         @CheckResult
whereService()63         public ServiceQuery<ServiceConfigurationChangedEventQuery> whereService() {
64             return mService;
65         }
66 
67         @Override
filter(ServiceConfigurationChangedEvent event)68         protected boolean filter(ServiceConfigurationChangedEvent event) {
69             if (!mService.matches(event.mService)) {
70                 return false;
71             }
72             return true;
73         }
74 
75         @Override
describeQuery(String fieldName)76         public String describeQuery(String fieldName) {
77             return toStringBuilder(ServiceConfigurationChangedEvent.class, this)
78                     .field("service", mService)
79                     .toString();
80         }
81     }
82 
83 
84     /** Begins logging a {@link ServiceConfigurationChangedEvent}. */
logger( Service service, String serviceName, Configuration configuration)85     public static ServiceConfigurationChangedEvent.ServiceConfigurationChangedEventLogger logger(
86             Service service, String serviceName,
87             Configuration configuration) {
88         return new ServiceConfigurationChangedEvent.ServiceConfigurationChangedEventLogger(
89                 service, serviceName, configuration);
90     }
91 
92     /** {@link EventLogger} for {@link ServiceConfigurationChangedEvent}. */
93     public static final class ServiceConfigurationChangedEventLogger extends
94             EventLogger<ServiceConfigurationChangedEvent> {
95 
ServiceConfigurationChangedEventLogger(Service service, String serviceName, Configuration configuration)96         private ServiceConfigurationChangedEventLogger(Service service,
97                 String serviceName,
98                 Configuration configuration) {
99             super(service, new ServiceConfigurationChangedEvent());
100             mEvent.mConfiguration = configuration;
101             setService(serviceName);
102         }
103 
104         /** Sets the {@link Service} which received this event. */
105         @CanIgnoreReturnValue
setService( String serviceName)106         public ServiceConfigurationChangedEvent.ServiceConfigurationChangedEventLogger setService(
107                 String serviceName) {
108             mEvent.mService = ServiceInfo.builder()
109                     .serviceClass(serviceName)
110                     .build();
111             return this;
112         }
113 
114         /** Sets the {@link Configuration} */
115         @CanIgnoreReturnValue
116         public ServiceConfigurationChangedEvent.ServiceConfigurationChangedEventLogger
setConfiguration(Configuration configuration)117                 setConfiguration(Configuration configuration) {
118             mEvent.mConfiguration = configuration;
119             return this;
120         }
121 
122     }
123 
124     protected ServiceInfo mService;
125     protected Configuration mConfiguration;
126 
127     /**
128      * The {@link Configuration} passed into {@link Service#onConfigurationChanged(Configuration)}.
129      */
configuration()130     public Configuration configuration() {
131         return mConfiguration;
132     }
133 
134     /** Information about the {@link Service} which received the configuration. */
service()135     public ServiceInfo service() {
136         return mService;
137     }
138 
139     @Override
toString()140     public String toString() {
141         return "ServiceConfigurationChangedEvent{"
142                 + " configuration=" + configuration()
143                 + ", service=" + mService
144                 + ", packageName='" + mPackageName + "'"
145                 + ", timestamp=" + mTimestamp
146                 + "}";
147     }
148 }
149