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.Activity; 20 import android.app.AppComponentFactory; 21 import android.app.Service; 22 import android.content.BroadcastReceiver; 23 import android.content.Intent; 24 import android.util.Log; 25 26 /** 27 * An {@link AppComponentFactory} which redirects invalid class names to premade EventLib classes. 28 */ 29 public final class EventLibAppComponentFactory extends AppComponentFactory { 30 31 private static final String LOG_TAG = "EventLibACF"; 32 33 @Override instantiateActivity(ClassLoader classLoader, String className, Intent intent)34 public Activity instantiateActivity(ClassLoader classLoader, String className, Intent intent) 35 throws InstantiationException, IllegalAccessException, ClassNotFoundException { 36 37 try { 38 return super.instantiateActivity(classLoader, className, intent); 39 } catch (ClassNotFoundException e) { 40 Log.d(LOG_TAG, 41 "Activity class (" + className + ") not found, routing to EventLibActivity"); 42 EventLibActivity activity = 43 (EventLibActivity) super.instantiateActivity( 44 classLoader, EventLibActivity.class.getName(), intent); 45 activity.setOverrideActivityClassName(className); 46 return activity; 47 } 48 } 49 50 @Override instantiateReceiver(ClassLoader classLoader, String className, Intent intent)51 public BroadcastReceiver instantiateReceiver(ClassLoader classLoader, String className, 52 Intent intent) 53 throws InstantiationException, IllegalAccessException, ClassNotFoundException { 54 try { 55 return super.instantiateReceiver(classLoader, className, intent); 56 } catch (ClassNotFoundException e) { 57 if (className.endsWith("DeviceAdminReceiver")) { 58 Log.d(LOG_TAG, "Broadcast Receiver class (" + className 59 + ") not found, routing to TestAppDeviceAdminReceiver"); 60 EventLibDeviceAdminReceiver receiver = (EventLibDeviceAdminReceiver) 61 super.instantiateReceiver( 62 classLoader, EventLibDeviceAdminReceiver.class.getName(), 63 intent); 64 receiver.setOverrideDeviceAdminReceiverClassName(className); 65 return receiver; 66 } else if (className.endsWith("DelegatedAdminReceiver")) { 67 Log.d(LOG_TAG, "Broadcast Receiver class (" + className 68 + ") not found, routing to EventLibDelegatedAdminReceiver"); 69 EventLibDelegatedAdminReceiver receiver = (EventLibDelegatedAdminReceiver) 70 super.instantiateReceiver( 71 classLoader, EventLibDelegatedAdminReceiver.class.getName(), 72 intent); 73 receiver.setOverrideDelegatedAdminReceiverClassName(className); 74 return receiver; 75 } 76 77 Log.d(LOG_TAG, "Broadcast Receiver class (" + className 78 + ") not found, routing to EventLibBroadcastReceiver"); 79 80 EventLibBroadcastReceiver receiver = (EventLibBroadcastReceiver) 81 super.instantiateReceiver( 82 classLoader, EventLibBroadcastReceiver.class.getName(), intent); 83 receiver.setOverrideBroadcastReceiverClassName(className); 84 return receiver; 85 } 86 } 87 88 @Override instantiateService(ClassLoader classLoader, String className, Intent intent)89 public Service instantiateService(ClassLoader classLoader, String className, Intent intent) 90 throws InstantiationException, IllegalAccessException, ClassNotFoundException { 91 try { 92 return super.instantiateService(classLoader, className, intent); 93 } catch (ClassNotFoundException e) { 94 if (className.endsWith("ContentSuggestionsService")) { 95 Log.d(LOG_TAG, "Service class (" + className 96 + ") not found, routing to EventLibContentSuggestionsService"); 97 98 EventLibContentSuggestionsService service = (EventLibContentSuggestionsService) 99 super.instantiateService(classLoader, 100 EventLibContentSuggestionsService.class.getName(), intent); 101 service.setOverrideServiceClassName(className); 102 return service; 103 } 104 105 Log.d(LOG_TAG, "Service class (" + className 106 + ") not found, routing to EventLibService"); 107 108 EventLibService service = (EventLibService) 109 super.instantiateService( 110 classLoader, EventLibService.class.getName(), intent); 111 service.setOverrideServiceClassName(className); 112 return service; 113 } 114 } 115 } 116