AppComponentFactory.java revision c138696489e2e3657e0b80b17cb691b4bbf038ac
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 */
16package android.app;
17
18import android.annotation.NonNull;
19import android.annotation.Nullable;
20import android.content.BroadcastReceiver;
21import android.content.ContentProvider;
22import android.content.Intent;
23
24/**
25 * Interface used to control the instantiation of manifest elements.
26 *
27 * @see #instantiateApplication
28 * @see #instantiateActivity
29 * @see #instantiateService
30 * @see #instantiateReceiver
31 * @see #instantiateProvider
32 */
33public class AppComponentFactory {
34
35    /**
36     * Allows application to override the creation of the application object. This can be used to
37     * perform things such as dependency injection or class loader changes to these
38     * classes.
39     * <p>
40     * This method is only intended to provide a hook for instantiation. It does not provide
41     * earlier access to the Application object. The returned object will not be initialized
42     * as a Context yet and should not be used to interact with other android APIs.
43     *
44     * @param cl        The default classloader to use for instantiation.
45     * @param className The class to be instantiated.
46     */
47    public @NonNull Application instantiateApplication(@NonNull ClassLoader cl,
48            @NonNull String className)
49            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
50        return (Application) cl.loadClass(className).newInstance();
51    }
52
53    /**
54     * Allows application to override the creation of activities. This can be used to
55     * perform things such as dependency injection or class loader changes to these
56     * classes.
57     * <p>
58     * This method is only intended to provide a hook for instantiation. It does not provide
59     * earlier access to the Activity object. The returned object will not be initialized
60     * as a Context yet and should not be used to interact with other android APIs.
61     *
62     * @param cl        The default classloader to use for instantiation.
63     * @param className The class to be instantiated.
64     * @param intent    Intent creating the class.
65     */
66    public @NonNull Activity instantiateActivity(@NonNull ClassLoader cl, @NonNull String className,
67            @Nullable Intent intent)
68            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
69        return (Activity) cl.loadClass(className).newInstance();
70    }
71
72    /**
73     * Allows application to override the creation of receivers. This can be used to
74     * perform things such as dependency injection or class loader changes to these
75     * classes.
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 BroadcastReceiver instantiateReceiver(@NonNull ClassLoader cl,
82            @NonNull String className, @Nullable Intent intent)
83            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
84        return (BroadcastReceiver) cl.loadClass(className).newInstance();
85    }
86
87    /**
88     * Allows application to override the creation of services. This can be used to
89     * perform things such as dependency injection or class loader changes to these
90     * classes.
91     * <p>
92     * This method is only intended to provide a hook for instantiation. It does not provide
93     * earlier access to the Service object. The returned object will not be initialized
94     * as a Context yet and should not be used to interact with other android APIs.
95     *
96     * @param cl        The default classloader to use for instantiation.
97     * @param className The class to be instantiated.
98     * @param intent    Intent creating the class.
99     */
100    public @NonNull Service instantiateService(@NonNull ClassLoader cl,
101            @NonNull String className, @Nullable Intent intent)
102            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
103        return (Service) cl.loadClass(className).newInstance();
104    }
105
106    /**
107     * Allows application to override the creation of providers. This can be used to
108     * perform things such as dependency injection or class loader changes to these
109     * classes.
110     * <p>
111     * This method is only intended to provide a hook for instantiation. It does not provide
112     * earlier access to the ContentProvider object. The returned object will not be initialized
113     * with a Context yet and should not be used to interact with other android APIs.
114     *
115     * @param cl        The default classloader to use for instantiation.
116     * @param className The class to be instantiated.
117     */
118    public @NonNull ContentProvider instantiateProvider(@NonNull ClassLoader cl,
119            @NonNull String className)
120            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
121        return (ContentProvider) cl.loadClass(className).newInstance();
122    }
123
124    /**
125     * @hide
126     */
127    public static final AppComponentFactory DEFAULT = new AppComponentFactory();
128}
129