Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package android.app; |
| 17 | |
| 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.Nullable; |
| 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.ContentProvider; |
| 22 | import android.content.Intent; |
David Brazdil | fd6dcc2 | 2018-10-25 14:07:51 +0100 | [diff] [blame] | 23 | import android.content.pm.ApplicationInfo; |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Interface used to control the instantiation of manifest elements. |
| 27 | * |
| 28 | * @see #instantiateApplication |
| 29 | * @see #instantiateActivity |
| 30 | * @see #instantiateService |
| 31 | * @see #instantiateReceiver |
| 32 | * @see #instantiateProvider |
| 33 | */ |
| 34 | public class AppComponentFactory { |
| 35 | |
| 36 | /** |
David Brazdil | fd6dcc2 | 2018-10-25 14:07:51 +0100 | [diff] [blame] | 37 | * Allows application to override the creation of the default class loader. |
| 38 | * This can be used to perform things such as dependency injection or setting up |
| 39 | * a custom class loader hierarchy. |
| 40 | * |
| 41 | * @param cl The default classloader instantiated by platform. |
| 42 | */ |
| 43 | public @NonNull ClassLoader instantiateClassLoader(@NonNull ClassLoader cl) { |
| 44 | return cl; |
| 45 | } |
| 46 | |
| 47 | /** |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 48 | * Allows application to override the creation of the application object. This can be used to |
| 49 | * perform things such as dependency injection or class loader changes to these |
| 50 | * classes. |
Jason Monk | c138696 | 2018-03-28 17:46:03 -0400 | [diff] [blame] | 51 | * <p> |
| 52 | * This method is only intended to provide a hook for instantiation. It does not provide |
| 53 | * earlier access to the Application object. The returned object will not be initialized |
| 54 | * as a Context yet and should not be used to interact with other android APIs. |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 55 | * |
| 56 | * @param cl The default classloader to use for instantiation. |
| 57 | * @param className The class to be instantiated. |
| 58 | */ |
| 59 | public @NonNull Application instantiateApplication(@NonNull ClassLoader cl, |
| 60 | @NonNull String className) |
| 61 | throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
| 62 | return (Application) cl.loadClass(className).newInstance(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Allows application to override the creation of activities. This can be used to |
| 67 | * perform things such as dependency injection or class loader changes to these |
| 68 | * classes. |
Jason Monk | c138696 | 2018-03-28 17:46:03 -0400 | [diff] [blame] | 69 | * <p> |
| 70 | * This method is only intended to provide a hook for instantiation. It does not provide |
| 71 | * earlier access to the Activity object. The returned object will not be initialized |
| 72 | * as a Context yet and should not be used to interact with other android APIs. |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 73 | * |
| 74 | * @param cl The default classloader to use for instantiation. |
| 75 | * @param className The class to be instantiated. |
| 76 | * @param intent Intent creating the class. |
| 77 | */ |
| 78 | public @NonNull Activity instantiateActivity(@NonNull ClassLoader cl, @NonNull String className, |
| 79 | @Nullable Intent intent) |
| 80 | throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
| 81 | return (Activity) cl.loadClass(className).newInstance(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Allows application to override the creation of receivers. This can be used to |
| 86 | * perform things such as dependency injection or class loader changes to these |
| 87 | * classes. |
| 88 | * |
| 89 | * @param cl The default classloader to use for instantiation. |
| 90 | * @param className The class to be instantiated. |
| 91 | * @param intent Intent creating the class. |
| 92 | */ |
| 93 | public @NonNull BroadcastReceiver instantiateReceiver(@NonNull ClassLoader cl, |
| 94 | @NonNull String className, @Nullable Intent intent) |
| 95 | throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
| 96 | return (BroadcastReceiver) cl.loadClass(className).newInstance(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Allows application to override the creation of services. This can be used to |
| 101 | * perform things such as dependency injection or class loader changes to these |
| 102 | * classes. |
Jason Monk | c138696 | 2018-03-28 17:46:03 -0400 | [diff] [blame] | 103 | * <p> |
| 104 | * This method is only intended to provide a hook for instantiation. It does not provide |
| 105 | * earlier access to the Service object. The returned object will not be initialized |
| 106 | * as a Context yet and should not be used to interact with other android APIs. |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 107 | * |
| 108 | * @param cl The default classloader to use for instantiation. |
| 109 | * @param className The class to be instantiated. |
| 110 | * @param intent Intent creating the class. |
| 111 | */ |
| 112 | public @NonNull Service instantiateService(@NonNull ClassLoader cl, |
| 113 | @NonNull String className, @Nullable Intent intent) |
| 114 | throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
| 115 | return (Service) cl.loadClass(className).newInstance(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Allows application to override the creation of providers. This can be used to |
| 120 | * perform things such as dependency injection or class loader changes to these |
| 121 | * classes. |
Jason Monk | c138696 | 2018-03-28 17:46:03 -0400 | [diff] [blame] | 122 | * <p> |
| 123 | * This method is only intended to provide a hook for instantiation. It does not provide |
| 124 | * earlier access to the ContentProvider object. The returned object will not be initialized |
| 125 | * with a Context yet and should not be used to interact with other android APIs. |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 126 | * |
| 127 | * @param cl The default classloader to use for instantiation. |
| 128 | * @param className The class to be instantiated. |
| 129 | */ |
| 130 | public @NonNull ContentProvider instantiateProvider(@NonNull ClassLoader cl, |
| 131 | @NonNull String className) |
| 132 | throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
| 133 | return (ContentProvider) cl.loadClass(className).newInstance(); |
| 134 | } |
| 135 | |
David Brazdil | fd6dcc2 | 2018-10-25 14:07:51 +0100 | [diff] [blame] | 136 | private ApplicationInfo mApplicationInfo = null; |
| 137 | |
| 138 | void setApplicationInfo(ApplicationInfo info) { |
| 139 | mApplicationInfo = info; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns the ApplicationInfo associated with this package. |
| 144 | */ |
| 145 | public ApplicationInfo getApplicationInfo() { |
| 146 | return mApplicationInfo; |
| 147 | } |
| 148 | |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 149 | /** |
| 150 | * @hide |
| 151 | */ |
Jason Monk | c138696 | 2018-03-28 17:46:03 -0400 | [diff] [blame] | 152 | public static final AppComponentFactory DEFAULT = new AppComponentFactory(); |
Jason Monk | a80bfb5 | 2017-11-16 17:15:37 -0500 | [diff] [blame] | 153 | } |