blob: ae632915dd2dfa7f02bc580c4af24d5778fa7069 [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
30 * @see #instantiateService
31 * @see #instantiateReceiver
32 * @see #instantiateProvider
33 */
34public class AppComponentFactory {
35
36 /**
David Brazdilfd6dcc22018-10-25 14:07:51 +010037 * 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 Monka80bfb52017-11-16 17:15:37 -050048 * 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 Monkc1386962018-03-28 17:46:03 -040051 * <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 Monka80bfb52017-11-16 17:15:37 -050055 *
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 Monkc1386962018-03-28 17:46:03 -040069 * <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 Monka80bfb52017-11-16 17:15:37 -050073 *
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 Monkc1386962018-03-28 17:46:03 -0400103 * <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 Monka80bfb52017-11-16 17:15:37 -0500107 *
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 Monkc1386962018-03-28 17:46:03 -0400122 * <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 Monka80bfb52017-11-16 17:15:37 -0500126 *
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 Brazdilfd6dcc22018-10-25 14:07:51 +0100136 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 Monka80bfb52017-11-16 17:15:37 -0500149 /**
150 * @hide
151 */
Jason Monkc1386962018-03-28 17:46:03 -0400152 public static final AppComponentFactory DEFAULT = new AppComponentFactory();
Jason Monka80bfb52017-11-16 17:15:37 -0500153}