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 import com.google.errorprone.annotations.CanIgnoreReturnValue; 35 36 /** 37 * Event logged when {@link Service#onUnbind(Intent)} 38 */ 39 public class ServiceUnboundEvent extends Event { 40 41 private static final long serialVersionUID = 1; 42 43 /** Begins a query for {@link ServiceUnboundEvent} events. */ queryPackage(String packageName)44 public static ServiceUnboundEventQuery queryPackage(String packageName) { 45 return new ServiceUnboundEventQuery(packageName); 46 } 47 48 /** {@link EventLogsQuery} for {@link ServiceUnboundEvent}. */ 49 public static final class ServiceUnboundEventQuery 50 extends EventLogsQuery<ServiceUnboundEvent, 51 ServiceUnboundEvent.ServiceUnboundEventQuery> { 52 53 private static final long serialVersionUID = 1; 54 55 ServiceQueryHelper<ServiceUnboundEvent.ServiceUnboundEventQuery> mService = 56 new ServiceQueryHelper<>(this); 57 IntentQueryHelper<ServiceUnboundEvent.ServiceUnboundEventQuery> mIntent = 58 new IntentQueryHelper<>(this); 59 ServiceUnboundEventQuery(String packageName)60 private ServiceUnboundEventQuery(String packageName) { 61 super(ServiceUnboundEvent.class, packageName); 62 } 63 64 /** 65 * Query {@link Intent} passed into {@link Service#onUnbind(Intent)}. 66 */ 67 @CheckResult whereIntent()68 public IntentQuery<ServiceUnboundEventQuery> whereIntent() { 69 return mIntent; 70 } 71 72 /** Query {@link Service}. */ 73 @CheckResult whereService()74 public ServiceQuery<ServiceUnboundEventQuery> whereService() { 75 return mService; 76 } 77 78 @Override filter(ServiceUnboundEvent event)79 protected boolean filter(ServiceUnboundEvent event) { 80 if (!mIntent.matches(event.mIntent)) { 81 return false; 82 } 83 if (!mService.matches(event.mService)) { 84 return false; 85 } 86 return true; 87 } 88 89 @Override describeQuery(String fieldName)90 public String describeQuery(String fieldName) { 91 return toStringBuilder(ServiceUnboundEvent.class, this) 92 .field("intent", mIntent) 93 .field("service", mService) 94 .toString(); 95 } 96 } 97 98 99 /** Begins logging a {@link ServiceUnboundEvent}. */ logger(Service service, String serviceName, Intent intent)100 public static ServiceUnboundEventLogger logger(Service service, 101 String serviceName, Intent intent) { 102 return new ServiceUnboundEventLogger(service, serviceName, intent); 103 } 104 105 /** {@link EventLogger} for {@link ServiceUnboundEvent}. */ 106 public static final class ServiceUnboundEventLogger extends EventLogger<ServiceUnboundEvent> { 107 108 // TODO(b/214187100) Use ServiceInfo here instead of a String to identify the service. ServiceUnboundEventLogger(Service service, String serviceName, Intent intent)109 private ServiceUnboundEventLogger(Service service, 110 String serviceName, 111 Intent intent) { 112 super(service, new ServiceUnboundEvent()); 113 mEvent.mIntent = new SerializableParcelWrapper<>(intent); 114 setService(serviceName); 115 } 116 117 /** Sets the {@link Service} which received this event. */ 118 @CanIgnoreReturnValue setService(String serviceName)119 public ServiceUnboundEventLogger setService(String serviceName) { 120 mEvent.mService = ServiceInfo.builder() 121 .serviceClass(serviceName) 122 .build(); 123 return this; 124 } 125 126 /** Sets the {@link Intent} that was used to bind to the service. */ 127 @CanIgnoreReturnValue setIntent(Intent intent)128 public ServiceUnboundEventLogger setIntent(Intent intent) { 129 mEvent.mIntent = new SerializableParcelWrapper<>(intent); 130 return this; 131 } 132 133 } 134 135 protected ServiceInfo mService; 136 protected SerializableParcelWrapper<Intent> mIntent; 137 138 /** 139 * The {@link Intent} passed into {@link Service#onUnbind(Intent)}. 140 */ intent()141 public Intent intent() { 142 if (mIntent == null) { 143 return null; 144 } 145 return mIntent.get(); 146 } 147 148 /** Information about the {@link Service} which received the intent. */ service()149 public ServiceInfo service() { 150 return mService; 151 } 152 153 @Override toString()154 public String toString() { 155 return "ServiceUnboundEvent{" 156 + " intent=" + intent() 157 + ", service=" + mService 158 + ", packageName='" + mPackageName + "'" 159 + ", timestamp=" + mTimestamp 160 + "}"; 161 } 162 } 163