1 /* 2 * Copyright (C) 2023 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.contentsuggestions.ClassificationsRequest; 20 import android.app.contentsuggestions.ContentSuggestionsManager; 21 import android.app.contentsuggestions.SelectionsRequest; 22 import android.content.ComponentName; 23 import android.content.Intent; 24 import android.content.res.Configuration; 25 import android.graphics.Bitmap; 26 import android.os.Bundle; 27 import android.service.contentsuggestions.ContentSuggestionsService; 28 29 import com.android.eventlib.events.services.ServiceBoundEvent; 30 import com.android.eventlib.events.services.ServiceConfigurationChangedEvent; 31 import com.android.eventlib.events.services.ServiceCreatedEvent; 32 import com.android.eventlib.events.services.ServiceDestroyedEvent; 33 import com.android.eventlib.events.services.ServiceLowMemoryEvent; 34 import com.android.eventlib.events.services.ServiceMemoryTrimmedEvent; 35 import com.android.eventlib.events.services.ServiceReboundEvent; 36 import com.android.eventlib.events.services.ServiceStartedEvent; 37 import com.android.eventlib.events.services.ServiceTaskRemovedEvent; 38 import com.android.eventlib.events.services.ServiceUnboundEvent; 39 40 /** 41 * A {@link ContentSuggestionsService} which logs events. 42 * 43 * <p>Note that this does not support {@link ServiceBoundEvent}. 44 */ 45 public class EventLibContentSuggestionsService extends ContentSuggestionsService { 46 47 private String mOverrideServiceClassName; 48 setOverrideServiceClassName(String overrideServiceClassName)49 public void setOverrideServiceClassName(String overrideServiceClassName) { 50 mOverrideServiceClassName = overrideServiceClassName; 51 } 52 53 /** 54 * Gets the class name of this service. 55 * 56 * <p>If the class name has been overridden, that will be returned instead. 57 */ getClassName()58 public String getClassName() { 59 if (mOverrideServiceClassName != null) { 60 return mOverrideServiceClassName; 61 } 62 63 return EventLibService.class.getName(); 64 } 65 getComponentName()66 public ComponentName getComponentName() { 67 return new ComponentName(getApplication().getPackageName(), getClassName()); 68 } 69 70 @Override onProcessContextImage(int taskId, Bitmap contextImage, Bundle extras)71 public void onProcessContextImage(int taskId, Bitmap contextImage, Bundle extras) { 72 // TODO(279397270): Fill in 73 } 74 75 @Override onSuggestContentSelections(SelectionsRequest request, ContentSuggestionsManager.SelectionsCallback callback)76 public void onSuggestContentSelections(SelectionsRequest request, 77 ContentSuggestionsManager.SelectionsCallback callback) { 78 // TODO(279397270): Fill in 79 } 80 81 @Override onClassifyContentSelections(ClassificationsRequest request, ContentSuggestionsManager.ClassificationsCallback callback)82 public void onClassifyContentSelections(ClassificationsRequest request, 83 ContentSuggestionsManager.ClassificationsCallback callback) { 84 // TODO(279397270): Fill in 85 } 86 87 @Override onNotifyInteraction(String requestId, Bundle interaction)88 public void onNotifyInteraction(String requestId, Bundle interaction) { 89 // TODO(279397270): Fill in 90 } 91 92 @Override onCreate()93 public void onCreate() { 94 ServiceCreatedEvent.logger(this, getClassName()).log(); 95 super.onCreate(); 96 } 97 98 @Override onStartCommand(Intent intent, int flags, int startId)99 public int onStartCommand(Intent intent, int flags, int startId) { 100 ServiceStartedEvent.logger(this, getClassName(), intent, flags, startId).log(); 101 return super.onStartCommand(intent, flags, startId); 102 } 103 104 @Override onDestroy()105 public void onDestroy() { 106 ServiceDestroyedEvent.logger(this, getClassName()).log(); 107 super.onDestroy(); 108 } 109 110 @Override onConfigurationChanged(Configuration newConfig)111 public void onConfigurationChanged(Configuration newConfig) { 112 ServiceConfigurationChangedEvent.logger(this, getClassName(), newConfig).log(); 113 super.onConfigurationChanged(newConfig); 114 } 115 116 @Override onLowMemory()117 public void onLowMemory() { 118 ServiceLowMemoryEvent.logger(this, getClassName()).log(); 119 super.onLowMemory(); 120 } 121 122 @Override onTrimMemory(int level)123 public void onTrimMemory(int level) { 124 ServiceMemoryTrimmedEvent.logger(this, getClassName(), level).log(); 125 super.onTrimMemory(level); 126 } 127 128 // Cannot override onBind due to final 129 130 @Override onUnbind(Intent intent)131 public boolean onUnbind(Intent intent) { 132 ServiceUnboundEvent.logger(this, getClassName(), intent).log(); 133 return super.onUnbind(intent); 134 } 135 136 @Override onRebind(Intent intent)137 public void onRebind(Intent intent) { 138 ServiceReboundEvent.logger(this, getClassName(), intent).log(); 139 super.onRebind(intent); 140 } 141 142 @Override onTaskRemoved(Intent rootIntent)143 public void onTaskRemoved(Intent rootIntent) { 144 ServiceTaskRemovedEvent.logger(this, getClassName(), rootIntent).log(); 145 super.onTaskRemoved(rootIntent); 146 } 147 } 148