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