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.content.BroadcastReceiver;
22 import android.content.Intent;
23 import android.util.Log;
24 
25 /**
26  * An {@link AppComponentFactory} which redirects invalid class names to premade EventLib classes.
27  */
28 public final class EventLibAppComponentFactory extends AppComponentFactory {
29 
30     private static final String LOG_TAG = "EventLibACF";
31 
32     @Override
instantiateActivity(ClassLoader classLoader, String className, Intent intent)33     public Activity instantiateActivity(ClassLoader classLoader, String className, Intent intent)
34             throws InstantiationException, IllegalAccessException, ClassNotFoundException {
35 
36         try {
37             return super.instantiateActivity(classLoader, className, intent);
38         } catch (ClassNotFoundException e) {
39             Log.d(LOG_TAG,
40                     "Activity class (" + className + ") not found, routing to EventLibActivity");
41             EventLibActivity activity =
42                     (EventLibActivity) super.instantiateActivity(
43                             classLoader, EventLibActivity.class.getName(), intent);
44             activity.setOverrideActivityClassName(className);
45             return activity;
46         }
47     }
48 
49     @Override
instantiateReceiver(ClassLoader classLoader, String className, Intent intent)50     public BroadcastReceiver instantiateReceiver(ClassLoader classLoader, String className,
51             Intent intent)
52             throws InstantiationException, IllegalAccessException, ClassNotFoundException {
53         try {
54             return super.instantiateReceiver(classLoader, className, intent);
55         } catch (ClassNotFoundException e) {
56             Log.d(LOG_TAG, "Broadcast Receiver class (" + className
57                     + ") not found, routing to EventLibBroadcastReceiver");
58 
59             EventLibBroadcastReceiver receiver = (EventLibBroadcastReceiver)
60                     super.instantiateReceiver(
61                             classLoader, EventLibBroadcastReceiver.class.getName(), intent);
62             receiver.setOverrideBroadcastReceiverClassName(className);
63             return receiver;
64         }
65     }
66 }
67