blob: 2cec7f0fc3237debdc5647dc543d0c5c58231c15 [file] [log] [blame]
Jason Monka80bfb52017-11-16 17:15:37 -05001/*
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 */
16package android.app;
17
18import android.annotation.NonNull;
19import android.annotation.Nullable;
20import android.content.BroadcastReceiver;
21import android.content.ContentProvider;
22import android.content.Intent;
David Brazdilfd6dcc22018-10-25 14:07:51 +010023import android.content.pm.ApplicationInfo;
Jason Monka80bfb52017-11-16 17:15:37 -050024
25/**
26 * Interface used to control the instantiation of manifest elements.
27 *
28 * @see #instantiateApplication
29 * @see #instantiateActivity
David Brazdil33bd3432019-02-27 20:15:18 +000030 * @see #instantiateClassLoader
Jason Monka80bfb52017-11-16 17:15:37 -050031 * @see #instantiateService
32 * @see #instantiateReceiver
33 * @see #instantiateProvider
34 */
35public class AppComponentFactory {
36
37 /**
David Brazdilfd6dcc22018-10-25 14:07:51 +010038 * 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 Brazdil33bd3432019-02-27 20:15:18 +000043 * @param aInfo Information about the application being loaded.
David Brazdilfd6dcc22018-10-25 14:07:51 +010044 */
David Brazdil33bd3432019-02-27 20:15:18 +000045 public @NonNull ClassLoader instantiateClassLoader(@NonNull ClassLoader cl,
46 @NonNull ApplicationInfo aInfo) {
David Brazdilfd6dcc22018-10-25 14:07:51 +010047 return cl;
48 }
49
50 /**
Jason Monka80bfb52017-11-16 17:15:37 -050051 * 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 Monkc1386962018-03-28 17:46:03 -040054 * <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 Monka80bfb52017-11-16 17:15:37 -050058 *
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 Monkc1386962018-03-28 17:46:03 -040072 * <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 Monka80bfb52017-11-16 17:15:37 -050076 *
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 Monkc1386962018-03-28 17:46:03 -0400106 * <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 Monka80bfb52017-11-16 17:15:37 -0500110 *
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 Monkc1386962018-03-28 17:46:03 -0400125 * <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 Monka80bfb52017-11-16 17:15:37 -0500129 *
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 Monkc1386962018-03-28 17:46:03 -0400142 public static final AppComponentFactory DEFAULT = new AppComponentFactory();
Jason Monka80bfb52017-11-16 17:15:37 -0500143}