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