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#onRebind(Intent)} 38 */ 39 public class ServiceReboundEvent extends Event { 40 41 private static final long serialVersionUID = 1; 42 43 /** Begins a query for {@link ServiceReboundEvent} events. */ queryPackage(String packageName)44 public static ServiceReboundEvent.ServiceReboundEventQuery queryPackage(String packageName) { 45 return new ServiceReboundEvent.ServiceReboundEventQuery(packageName); 46 } 47 48 /** {@link EventLogsQuery} for {@link ServiceReboundEvent}. */ 49 public static final class ServiceReboundEventQuery 50 extends EventLogsQuery<ServiceReboundEvent, 51 ServiceReboundEvent.ServiceReboundEventQuery> { 52 53 private static final long serialVersionUID = 1; 54 55 ServiceQueryHelper<ServiceReboundEvent.ServiceReboundEventQuery> mService = 56 new ServiceQueryHelper<>(this); 57 IntentQueryHelper<ServiceReboundEvent.ServiceReboundEventQuery> mIntent = 58 new IntentQueryHelper<>(this); 59 ServiceReboundEventQuery(String packageName)60 private ServiceReboundEventQuery(String packageName) { 61 super(ServiceReboundEvent.class, packageName); 62 } 63 64 /** 65 * Query {@link Intent} passed into {@link Service#onRebind(Intent)}. 66 */ 67 @CheckResult whereIntent()68 public IntentQuery<ServiceReboundEvent.ServiceReboundEventQuery> whereIntent() { 69 return mIntent; 70 } 71 72 /** Query {@link Service}. */ 73 @CheckResult whereService()74 public ServiceQuery<ServiceReboundEvent.ServiceReboundEventQuery> whereService() { 75 return mService; 76 } 77 78 @Override filter(ServiceReboundEvent event)79 protected boolean filter(ServiceReboundEvent 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(ServiceReboundEvent.class, this) 92 .field("intent", mIntent) 93 .field("service", mService) 94 .toString(); 95 } 96 } 97 98 99 /** Begins logging a {@link ServiceReboundEvent}. */ logger(Service service, String serviceName, Intent intent)100 public static ServiceReboundEvent.ServiceReboundEventLogger logger(Service service, 101 String serviceName, Intent intent) { 102 return new ServiceReboundEvent.ServiceReboundEventLogger(service, serviceName, intent); 103 } 104 105 /** {@link EventLogger} for {@link ServiceReboundEvent}. */ 106 public static final class ServiceReboundEventLogger extends EventLogger<ServiceReboundEvent> { 107 ServiceReboundEventLogger(Service service, String serviceName, Intent intent)108 private ServiceReboundEventLogger(Service service, 109 String serviceName, 110 Intent intent) { 111 super(service, new ServiceReboundEvent()); 112 mEvent.mIntent = new SerializableParcelWrapper<>(intent); 113 setService(serviceName); 114 } 115 116 /** Sets the {@link Service} which received this event. */ 117 @CanIgnoreReturnValue setService( String serviceName)118 public ServiceReboundEvent.ServiceReboundEventLogger setService( 119 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 ServiceReboundEvent.ServiceReboundEventLogger 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#onRebind(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 "ServiceReboundEvent{" 156 + " intent=" + intent() 157 + ", service=" + mService 158 + ", packageName='" + mPackageName + "'" 159 + ", timestamp=" + mTimestamp 160 + "}"; 161 } 162 } 163