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