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