Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 1 | # Dagger 2 in SystemUI |
| 2 | *Dagger 2 is a dependency injection framework that compiles annotations to code |
| 3 | to create dependencies without reflection* |
| 4 | |
| 5 | ## Recommended reading |
| 6 | |
| 7 | Go read about Dagger 2. |
| 8 | |
Jason Monk | 55fd968 | 2018-12-27 07:32:10 -0500 | [diff] [blame^] | 9 | - [User's guide](https://google.github.io/dagger/users-guide) |
| 10 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 11 | TODO: Add some links. |
| 12 | |
| 13 | ## State of the world |
| 14 | |
| 15 | Dagger 2 has been turned on for SystemUI and a early first pass has been taken |
| 16 | for converting everything in Dependency.java to use Dagger. Since a lot of |
| 17 | SystemUI depends on Dependency, stubs have been added to Dependency to proxy |
| 18 | any gets through to the instances provided by dagger, this will allow migration |
| 19 | of SystemUI through a number of CLs. |
| 20 | |
| 21 | ### How it works in SystemUI |
| 22 | |
| 23 | For the classes that we're using in Dependency and are switching to dagger, the |
| 24 | equivalent dagger version is using @Singleton and only having one instance. |
| 25 | To have the single instance span all of SystemUI and be easily accessible for |
| 26 | other components, there is a single root Component that exists that generates |
| 27 | these. The component lives in SystemUIFactory and is called SystemUIRootComponent. |
| 28 | |
| 29 | ```java |
| 30 | @Singleton |
| 31 | @Component(modules = {SystemUIFactory.class, DependencyProvider.class, ContextHolder.class}) |
| 32 | public interface SystemUIRootComponent { |
| 33 | @Singleton |
| 34 | Dependency.DependencyInjector createDependency(); |
| 35 | } |
| 36 | ``` |
| 37 | |
| 38 | The root modules are what provides the global singleton dependencies across |
| 39 | SystemUI. ContextHolder is just a wrapper that provides a context. |
| 40 | SystemUIFactory @Provide dependencies that need to be overridden by SystemUI |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 41 | variants (like other form factors). DependencyBinder creates the mapping from |
| 42 | interfaces to implementation classes. DependencyProvider provides or binds any |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 43 | remaining depedencies required. |
| 44 | |
| 45 | ### Adding injection to a new SystemUI object |
| 46 | |
| 47 | Anything that depends on any @Singleton provider from SystemUIRootComponent |
| 48 | should be declared as a Subcomponent of the root component, this requires |
| 49 | declaring your own interface for generating your own modules or just the |
| 50 | object you need injected. The subcomponent also needs to be added to |
| 51 | SystemUIRootComponent in SystemUIFactory so it can be acquired. |
| 52 | |
| 53 | ```java |
| 54 | public interface SystemUIRootComponent { |
| 55 | + @Singleton |
| 56 | + Dependency.DependencyInjector createDependency(); |
| 57 | } |
| 58 | |
| 59 | public class Dependency extends SystemUI { |
| 60 | ... |
| 61 | + @Subcomponent |
| 62 | + public interface DependencyInjector { |
| 63 | + Dependency createSystemUI(); |
| 64 | + } |
| 65 | } |
| 66 | ``` |
| 67 | |
| 68 | For objects that extend SystemUI and require injection, you can define an |
| 69 | injector that creates the injected object for you. This other class should |
| 70 | be referenced in @string/config_systemUIServiceComponents. |
| 71 | |
| 72 | ```java |
| 73 | public static class DependencyCreator implements Injector { |
| 74 | @Override |
| 75 | public SystemUI apply(Context context) { |
| 76 | return SystemUIFactory.getInstance().getRootComponent() |
| 77 | .createDependency() |
| 78 | .createSystemUI(); |
| 79 | } |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | ### Adding a new injectable object |
| 84 | |
| 85 | First tag the constructor with @Inject. Also tag it with @Singleton if only one |
| 86 | instance should be created. |
| 87 | |
| 88 | ```java |
| 89 | @Singleton |
| 90 | public class SomethingController { |
| 91 | @Inject |
| 92 | public SomethingController(Context context, |
| 93 | @Named(MAIN_HANDLER_NAME) Handler mainHandler) { |
| 94 | // context and mainHandler will be automatically populated. |
| 95 | } |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | If you have an interface class and an implementation class, dagger needs to know |
| 100 | how to map it. The simplest way to do this is to add a provides method to |
| 101 | DependencyProvider. |
| 102 | |
| 103 | ```java |
| 104 | public class DependencyProvider { |
| 105 | ... |
| 106 | @Singleton |
| 107 | @Provide |
| 108 | public SomethingController provideSomethingController(Context context, |
| 109 | @Named(MAIN_HANDLER_NAME) Handler mainHandler) { |
| 110 | return new SomethingControllerImpl(context, mainHandler); |
| 111 | } |
| 112 | } |
| 113 | ``` |
| 114 | |
| 115 | If you need to access this from Dependency#get, then add an adapter to Dependency |
| 116 | that maps to the instance provided by Dagger. The changes should be similar |
| 117 | to the following diff. |
| 118 | |
| 119 | ```java |
| 120 | public class Dependency { |
| 121 | ... |
| 122 | @Inject Lazy<SomethingController> mSomethingController; |
| 123 | ... |
| 124 | public void start() { |
| 125 | ... |
| 126 | mProviders.put(SomethingController.class, mSomethingController::get); |
| 127 | } |
| 128 | } |
| 129 | ``` |
| 130 | |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 131 | ### Using injection with Fragments |
| 132 | |
| 133 | Fragments are created as part of the FragmentManager, so they need to be |
| 134 | setup so the manager knows how to create them. To do that, add a method |
| 135 | to com.android.systemui.fragments.FragmentService$FragmentCreator that |
| 136 | returns your fragment class. Thats all thats required, once the method |
| 137 | exists, FragmentService will automatically pick it up and use injection |
| 138 | whenever your fragment needs to be created. |
| 139 | |
| 140 | ```java |
| 141 | public interface FragmentCreator { |
| 142 | + NavigationBarFragment createNavigationBar(); |
| 143 | } |
| 144 | ``` |
| 145 | |
| 146 | If you need to create your fragment (i.e. for the add or replace transaction), |
| 147 | then the FragmentHostManager can do this for you. |
| 148 | |
| 149 | ```java |
| 150 | FragmentHostManager.get(view).create(NavigationBarFragment.class); |
| 151 | ``` |
| 152 | |
Jason Monk | ea54e8a | 2018-12-20 10:01:48 -0500 | [diff] [blame] | 153 | ### Using injection with Views |
| 154 | |
| 155 | Generally, you shouldn't need to inject for a view, as the view should |
| 156 | be relatively self contained and logic that requires injection should be |
| 157 | moved to a higher level construct such as a Fragment or a top-level SystemUI |
| 158 | component, see above for how to do injection for both of which. |
| 159 | |
| 160 | Still here? Yeah, ok, sysui has a lot of pre-existing views that contain a |
| 161 | lot of code that could benefit from injection and will need to be migrated |
| 162 | off from Dependency#get uses. Similar to how fragments are injected, the view |
| 163 | needs to be added to the interface |
| 164 | com.android.systemui.util.InjectionInflationController$ViewInstanceCreator. |
| 165 | |
| 166 | ```java |
| 167 | public interface ViewInstanceCreator { |
| 168 | + QuickStatusBarHeader createQsHeader(); |
| 169 | } |
| 170 | ``` |
| 171 | |
| 172 | Presumably you need to inflate that view from XML (otherwise why do you |
| 173 | need anything special? see earlier sections about generic injection). To obtain |
| 174 | an inflater that supports injected objects, call InjectionInflationController#injectable, |
| 175 | which will wrap the inflater it is passed in one that can create injected |
| 176 | objects when needed. |
| 177 | |
| 178 | ```java |
| 179 | @Override |
| 180 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, |
| 181 | Bundle savedInstanceState) { |
| 182 | return mInjectionInflater.injectable(inflater).inflate(R.layout.my_layout, container, false); |
| 183 | } |
| 184 | ``` |
| 185 | |
| 186 | There is one other important thing to note about injecting with views. SysUI |
| 187 | already has a Context in its global dagger component, so if you simply inject |
| 188 | a Context, you will not get the one that the view should have with proper |
| 189 | theming. Because of this, always ensure to tag views that have @Inject with |
| 190 | the @Named view context. |
| 191 | |
| 192 | ```java |
| 193 | public CustomView(@Named(VIEW_CONTEXT) Context themedViewContext, AttributeSet attrs, |
| 194 | OtherCustomDependency something) { |
| 195 | ... |
| 196 | } |
| 197 | ``` |
| 198 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 199 | ## TODO List |
| 200 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 201 | - Eliminate usages of Dependency#get |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 202 | - Add links in above TODO |