xref: /aosp_15_r20/external/dagger2/java/dagger/hilt/android/EntryPointAccessors.kt (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2021 The Dagger Authors.
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 
17 package dagger.hilt.android
18 
19 import android.app.Activity
20 import android.content.Context
21 import androidx.fragment.app.Fragment
22 import android.view.View
23 import dagger.hilt.EntryPoints
24 import dagger.hilt.android.internal.Contexts
25 
26 /** Utility functions for dealing with entry points for standard Android components. */
27 object EntryPointAccessors {
28   /**
29    * Returns the entry point interface from an application. The context can be any context derived
30    * from the application context. May only be used with entry point interfaces installed in the
31    * SingletonComponent.
32    */
33   @JvmStatic
fromApplicationnull34   fun <T> fromApplication(context: Context, entryPoint: Class<T>): T =
35     EntryPoints.get(Contexts.getApplication(context.applicationContext), entryPoint)
36 
37   /**
38    * Returns the entry point interface from an application. The context can be any context derived
39    * from the application context. May only be used with entry point interfaces installed in the
40    * SingletonComponent.
41    */
42   inline fun <reified T> fromApplication(context: Context): T =
43     fromApplication(context, T::class.java)
44 
45   /**
46    * Returns the entry point interface from an activity. May only be used with entry point
47    * interfaces installed in the ActivityComponent.
48    */
49   @JvmStatic
50   fun <T> fromActivity(activity: Activity, entryPoint: Class<T>): T =
51     EntryPoints.get(activity, entryPoint)
52 
53   /**
54    * Returns the entry point interface from an activity. May only be used with entry point
55    * interfaces installed in the ActivityComponent.
56    */
57   inline fun <reified T> fromActivity(activity: Activity): T =
58     fromActivity(activity, T::class.java)
59 
60   /**
61    * Returns the entry point interface from a fragment. May only be used with entry point interfaces
62    * installed in the FragmentComponent.
63    */
64   @JvmStatic
65   fun <T> fromFragment(fragment: Fragment, entryPoint: Class<T>): T =
66     EntryPoints.get(fragment, entryPoint)
67 
68   /**
69    * Returns the entry point interface from a fragment. May only be used with entry point interfaces
70    * installed in the FragmentComponent.
71    */
72   inline fun <reified T> fromFragment(fragment: Fragment): T =
73     fromFragment(fragment, T::class.java)
74 
75   /**
76    * Returns the entry point interface from a view. May only be used with entry point interfaces
77    * installed in the ViewComponent or ViewNoFragmentComponent.
78    */
79   @JvmStatic
80   fun <T> fromView(view: View, entryPoint: Class<T>): T = EntryPoints.get(view, entryPoint)
81 
82   /**
83    * Returns the entry point interface from a view. May only be used with entry point interfaces
84    * installed in the ViewComponent or ViewNoFragmentComponent.
85    */
86   inline fun <reified T> fromView(view: View): T =
87     fromView(view, T::class.java)
88 }
89