• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.bedstead.testapp;
18 
19 import android.accounts.AccountManager;
20 import android.app.Activity;
21 import android.app.AppComponentFactory;
22 import android.app.NotificationManager;
23 import android.app.Service;
24 import android.app.admin.DevicePolicyManager;
25 import android.bluetooth.BluetoothAdapter;
26 import android.bluetooth.BluetoothManager;
27 import android.content.BroadcastReceiver;
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.RestrictionsManager;
32 import android.content.pm.CrossProfileApps;
33 import android.content.pm.LauncherApps;
34 import android.content.pm.PackageManager;
35 import android.net.wifi.WifiManager;
36 import android.os.HardwarePropertiesManager;
37 import android.os.UserManager;
38 import android.security.KeyChain;
39 import android.telecom.TelecomManager;
40 import android.telephony.SmsManager;
41 import android.util.Log;
42 
43 import com.android.bedstead.testapp.processor.annotations.FrameworkClass;
44 import com.android.bedstead.testapp.processor.annotations.TestAppReceiver;
45 import com.android.eventlib.premade.EventLibService;
46 
47 /**
48  * An {@link AppComponentFactory} which redirects invalid class names to premade TestApp classes.
49  */
50 @TestAppReceiver(
51         frameworkClasses = {
52                 @FrameworkClass(frameworkClass = DevicePolicyManager.class, constructor = "context.getSystemService(android.app.admin.DevicePolicyManager.class)"),
53                 @FrameworkClass(frameworkClass = HardwarePropertiesManager.class, constructor = "context.getSystemService(android.os.HardwarePropertiesManager.class)"),
54                 @FrameworkClass(frameworkClass = UserManager.class, constructor = "context.getSystemService(android.os.UserManager.class)"),
55                 @FrameworkClass(frameworkClass = WifiManager.class, constructor = "context.getSystemService(android.net.wifi.WifiManager.class)"),
56                 @FrameworkClass(frameworkClass = PackageManager.class, constructor = "context.getPackageManager()"),
57                 @FrameworkClass(frameworkClass = CrossProfileApps.class, constructor = "context.getSystemService(android.content.pm.CrossProfileApps.class)"),
58                 @FrameworkClass(frameworkClass = LauncherApps.class, constructor = "context.getSystemService(android.content.pm.LauncherApps.class)"),
59                 @FrameworkClass(frameworkClass = AccountManager.class, constructor = "context.getSystemService(android.accounts.AccountManager.class)"),
60                 @FrameworkClass(frameworkClass = Context.class, constructor = "context"),
61                 @FrameworkClass(frameworkClass = ContentResolver.class, constructor = "context.getContentResolver()"),
62                 @FrameworkClass(frameworkClass = BluetoothManager.class, constructor = "context.getSystemService(android.bluetooth.BluetoothManager.class)"),
63                 @FrameworkClass(frameworkClass = BluetoothAdapter.class, constructor = "context.getSystemService(android.bluetooth.BluetoothManager.class).getAdapter()"),
64                 @FrameworkClass(frameworkClass = KeyChain.class, constructor = "null"), // KeyChain can not be instantiated - all calls are static
65                 @FrameworkClass(frameworkClass = NotificationManager.class, constructor =
66                         "context.getSystemService(android.app.NotificationManager.class)"),
67                 @FrameworkClass(frameworkClass = TelecomManager.class, constructor =
68                         "context.getSystemService(android.telecom.TelecomManager.class)"),
69                 @FrameworkClass(frameworkClass = RestrictionsManager.class, constructor =
70                         "context.getSystemService(android.content.RestrictionsManager.class)"),
71                 @FrameworkClass(frameworkClass = SmsManager.class, constructor =
72                         "context.getSystemService(android.telephony.SmsManager.class)")
73         }
74 )
75 public final class TestAppAppComponentFactory extends AppComponentFactory {
76 
77     private static final String LOG_TAG = "TestAppACF";
78 
79     @Override
instantiateActivity(ClassLoader classLoader, String className, Intent intent)80     public Activity instantiateActivity(ClassLoader classLoader, String className, Intent intent)
81             throws InstantiationException, IllegalAccessException, ClassNotFoundException {
82         Log.e(LOG_TAG, "Initiating activity for class "
83                 + className + " and intent " + intent);
84         try {
85             return super.instantiateActivity(classLoader, className, intent);
86         } catch (ClassNotFoundException e) {
87             Log.d(LOG_TAG,
88                     "Activity class (" + className + ") not found, routing to TestAppActivity");
89             BaseTestAppActivity activity =
90                     (BaseTestAppActivity) super.instantiateActivity(
91                             classLoader, BaseTestAppActivity.class.getName(), intent);
92             activity.setOverrideActivityClassName(className);
93             return activity;
94         }
95     }
96 
97     @Override
instantiateReceiver(ClassLoader classLoader, String className, Intent intent)98     public BroadcastReceiver instantiateReceiver(ClassLoader classLoader, String className,
99             Intent intent)
100             throws InstantiationException, IllegalAccessException, ClassNotFoundException {
101         Log.e(LOG_TAG, "Initiating receiver for class "
102                 + className + " and intent " + intent);
103         try {
104             return super.instantiateReceiver(classLoader, className, intent);
105         } catch (ClassNotFoundException e) {
106             if (className.endsWith("DeviceAdminReceiver")) {
107                 Log.d(LOG_TAG, "Broadcast Receiver class (" + className
108                         + ") not found, routing to TestAppDeviceAdminReceiver");
109                 BaseTestAppDeviceAdminReceiver receiver = (BaseTestAppDeviceAdminReceiver)
110                         super.instantiateReceiver(
111                                 classLoader, BaseTestAppDeviceAdminReceiver.class.getName(),
112                                 intent);
113                 receiver.setOverrideDeviceAdminReceiverClassName(className);
114                 return receiver;
115             } else if (className.endsWith("DelegatedAdminReceiver")) {
116                 Log.d(LOG_TAG, "Broadcast Receiver class (" + className
117                         + ") not found, routing to TestAppDelegatedAdminReceiver");
118                 BaseTestAppDelegatedAdminReceiver receiver = (BaseTestAppDelegatedAdminReceiver)
119                         super.instantiateReceiver(
120                                 classLoader, BaseTestAppDelegatedAdminReceiver.class.getName(),
121                                 intent);
122                 receiver.setOverrideDelegatedAdminReceiverClassName(className);
123                 return receiver;
124             }
125 
126             Log.d(LOG_TAG, "Broadcast Receiver class (" + className
127                     + ") not found, routing to TestAppBroadcastReceiver");
128             BaseTestAppBroadcastReceiver receiver = (BaseTestAppBroadcastReceiver)
129                     super.instantiateReceiver(
130                             classLoader, BaseTestAppBroadcastReceiver.class.getName(), intent);
131             receiver.setOverrideBroadcastReceiverClassName(className);
132             return receiver;
133         }
134     }
135 
136     @Override
instantiateService(ClassLoader classLoader, String className, Intent intent)137     public Service instantiateService(ClassLoader classLoader, String className, Intent intent)
138             throws InstantiationException, IllegalAccessException, ClassNotFoundException {
139         Log.e(LOG_TAG, "Initiating service for class "
140                 + className + " and intent " + intent);
141         try {
142             return super.instantiateService(classLoader, className, intent);
143         } catch (ClassNotFoundException e) {
144             if (className.endsWith("AccountAuthenticatorService")) {
145                 Log.d(LOG_TAG, "Service class (" + className
146                         + ") not found, routing to TestAppAccountAuthenticatorService");
147                 return super.instantiateService(
148                         classLoader,
149                         TestAppAccountAuthenticatorService.class.getName(),
150                         intent);
151             } else if (className.endsWith("ContentSuggestionsService")) {
152                 Log.d(LOG_TAG, "Service class (" + className
153                         + ") not found, routing to BaseTestAppContentSuggestionsService");
154                 BaseTestAppContentSuggestionsService service =
155                         (BaseTestAppContentSuggestionsService) super.instantiateService(
156                         classLoader,
157                         BaseTestAppContentSuggestionsService.class.getName(),
158                         intent);
159                 service.setOverrideServiceClassName(className);
160                 return service;
161             }
162 
163             if (className.endsWith("CredentialProviderService")) {
164                 Log.d(LOG_TAG, "Service class (" + className
165                         + ") not found, routing to BaseTestAppCredentialProviderService");
166                 return super.instantiateService(
167                         classLoader,
168                         BaseTestAppCredentialProviderService.class.getName(),
169                         intent);
170             }
171 
172             Log.d(LOG_TAG,
173                     "Service class (" + className + ") not found, routing to EventLibService");
174             EventLibService service =
175                     (EventLibService) super.instantiateService(
176                             classLoader, EventLibService.class.getName(), intent);
177             service.setOverrideServiceClassName(className);
178             return service;
179         }
180     }
181 }
182