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.premade; 18 19 import android.app.Service; 20 import android.content.ComponentName; 21 import android.content.Intent; 22 import android.content.res.Configuration; 23 import android.os.Binder; 24 import android.os.IBinder; 25 26 import com.android.eventlib.events.services.ServiceBoundEvent; 27 import com.android.eventlib.events.services.ServiceConfigurationChangedEvent; 28 import com.android.eventlib.events.services.ServiceCreatedEvent; 29 import com.android.eventlib.events.services.ServiceDestroyedEvent; 30 import com.android.eventlib.events.services.ServiceLowMemoryEvent; 31 import com.android.eventlib.events.services.ServiceMemoryTrimmedEvent; 32 import com.android.eventlib.events.services.ServiceReboundEvent; 33 import com.android.eventlib.events.services.ServiceStartedEvent; 34 import com.android.eventlib.events.services.ServiceTaskRemovedEvent; 35 import com.android.eventlib.events.services.ServiceUnboundEvent; 36 37 import com.google.errorprone.annotations.CanIgnoreReturnValue; 38 39 /** 40 * An {@link Service} which logs events for all lifecycle events. 41 */ 42 public class EventLibService extends Service { 43 44 private String mOverrideServiceClassName; 45 private final IBinder mBinder = new Binder(); 46 setOverrideServiceClassName(String overrideServiceClassName)47 public void setOverrideServiceClassName(String overrideServiceClassName) { 48 mOverrideServiceClassName = overrideServiceClassName; 49 } 50 51 /** 52 * Gets the class name of this service. 53 * 54 * <p>If the class name has been overridden, that will be returned instead. 55 */ getClassName()56 public String getClassName() { 57 if (mOverrideServiceClassName != null) { 58 return mOverrideServiceClassName; 59 } 60 61 return EventLibService.class.getName(); 62 } 63 getComponentName()64 public ComponentName getComponentName() { 65 return new ComponentName(getApplication().getPackageName(), getClassName()); 66 } 67 68 @Override onCreate()69 public void onCreate() { 70 ServiceCreatedEvent.logger(this, getClassName()).log(); 71 } 72 73 @Override onStartCommand(Intent intent, int flags, int startId)74 public int onStartCommand(Intent intent, int flags, int startId) { 75 ServiceStartedEvent.logger(this, getClassName(), intent, flags, startId).log(); 76 return super.onStartCommand(intent, flags, startId); 77 } 78 79 @Override onDestroy()80 public void onDestroy() { 81 ServiceDestroyedEvent.logger(this, getClassName()).log(); 82 } 83 84 @Override onConfigurationChanged(Configuration newConfig)85 public void onConfigurationChanged(Configuration newConfig) { 86 ServiceConfigurationChangedEvent.logger(this, getClassName(), newConfig).log(); 87 } 88 89 @Override onLowMemory()90 public void onLowMemory() { 91 ServiceLowMemoryEvent.logger(this, getClassName()).log(); 92 } 93 94 @Override onTrimMemory(int level)95 public void onTrimMemory(int level) { 96 ServiceMemoryTrimmedEvent.logger(this, getClassName(), level).log(); 97 } 98 99 @Override 100 @CanIgnoreReturnValue onBind(Intent intent)101 public IBinder onBind(Intent intent) { 102 ServiceBoundEvent.logger(this, getClassName(), intent).log(); 103 return mBinder; 104 } 105 106 @Override onUnbind(Intent intent)107 public boolean onUnbind(Intent intent) { 108 ServiceUnboundEvent.logger(this, getClassName(), intent).log(); 109 return super.onUnbind(intent); 110 } 111 112 @Override onRebind(Intent intent)113 public void onRebind(Intent intent) { 114 ServiceReboundEvent.logger(this, getClassName(), intent).log(); 115 } 116 117 @Override onTaskRemoved(Intent rootIntent)118 public void onTaskRemoved(Intent rootIntent) { 119 ServiceTaskRemovedEvent.logger(this, getClassName(), rootIntent).log(); 120 } 121 } 122