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 21 import androidx.annotation.CheckResult; 22 23 import com.android.eventlib.Event; 24 import com.android.eventlib.EventLogger; 25 import com.android.eventlib.EventLogsQuery; 26 import com.android.queryable.info.ServiceInfo; 27 import com.android.queryable.queries.ServiceQuery; 28 import com.android.queryable.queries.ServiceQueryHelper; 29 30 /** 31 * Event logged when {@link Service#onLowMemory()} 32 */ 33 public class ServiceLowMemoryEvent extends Event { 34 35 private static final long serialVersionUID = 1; 36 37 /** Begins a query for {@link ServiceLowMemoryEvent} events. */ queryPackage( String packageName)38 public static ServiceLowMemoryEvent.ServiceLowMemoryEventQuery queryPackage( 39 String packageName) { 40 return new ServiceLowMemoryEvent.ServiceLowMemoryEventQuery(packageName); 41 } 42 43 /** {@link EventLogsQuery} for {@link ServiceLowMemoryEvent}. */ 44 public static final class ServiceLowMemoryEventQuery 45 extends EventLogsQuery<ServiceLowMemoryEvent, 46 ServiceLowMemoryEvent.ServiceLowMemoryEventQuery> { 47 48 private static final long serialVersionUID = 1; 49 50 ServiceQueryHelper<ServiceLowMemoryEvent.ServiceLowMemoryEventQuery> mService = 51 new ServiceQueryHelper<>(this); 52 ServiceLowMemoryEventQuery(String packageName)53 private ServiceLowMemoryEventQuery(String packageName) { 54 super(ServiceLowMemoryEvent.class, packageName); 55 } 56 57 /** Query {@link Service}. */ 58 @CheckResult whereService()59 public ServiceQuery<ServiceLowMemoryEvent.ServiceLowMemoryEventQuery> whereService() { 60 return mService; 61 } 62 63 @Override filter(ServiceLowMemoryEvent event)64 protected boolean filter(ServiceLowMemoryEvent event) { 65 if (!mService.matches(event.mService)) { 66 return false; 67 } 68 return true; 69 } 70 71 @Override describeQuery(String fieldName)72 public String describeQuery(String fieldName) { 73 return toStringBuilder(ServiceLowMemoryEvent.class, this) 74 .field("service", mService) 75 .toString(); 76 } 77 } 78 79 80 /** Begins logging a {@link ServiceLowMemoryEvent}. */ logger(Service service, String serviceName)81 public static ServiceLowMemoryEvent.ServiceLowMemoryEventLogger logger(Service service, 82 String serviceName) { 83 return new ServiceLowMemoryEvent.ServiceLowMemoryEventLogger(service, serviceName); 84 } 85 86 /** {@link EventLogger} for {@link ServiceLowMemoryEvent}. */ 87 public static final class ServiceLowMemoryEventLogger extends 88 EventLogger<ServiceLowMemoryEvent> { ServiceLowMemoryEventLogger(Service service, String serviceName)89 private ServiceLowMemoryEventLogger(Service service, 90 String serviceName) { 91 super(service, new ServiceLowMemoryEvent()); 92 setService(serviceName); 93 } 94 95 /** Sets the {@link Service} which received this event. */ setService( String serviceName)96 public ServiceLowMemoryEvent.ServiceLowMemoryEventLogger setService( 97 String serviceName) { 98 mEvent.mService = ServiceInfo.builder() 99 .serviceClass(serviceName) 100 .build(); 101 return this; 102 } 103 104 } 105 106 protected ServiceInfo mService; 107 108 /** Information about the {@link Service} which received the intent. */ service()109 public ServiceInfo service() { 110 return mService; 111 } 112 113 @Override toString()114 public String toString() { 115 return "ServiceLowMemoryEvent{" 116 + ", service=" + mService 117 + ", packageName='" + mPackageName + "'" 118 + ", timestamp=" + mTimestamp 119 + "}"; 120 } 121 } 122