| 1 | # Usage of Dagger in the Shell library |
| 2 | |
| 3 | --- |
| 4 | |
| 5 | ## Dependencies |
| 6 | |
| 7 | Dagger is not required to use the Shell library, but it has a lot of obvious benefits: |
| 8 | |
| 9 | - Not having to worry about how to instantiate all the dependencies of a class, especially as |
| 10 | dependencies evolve (ie. product controller depends on base controller) |
| 11 | - Can create boundaries within the same app to encourage better code modularity |
| 12 | |
| 13 | As such, the Shell also tries to provide some reasonable out-of-the-box modules for use with Dagger. |
| 14 | |
| 15 | ## Modules |
| 16 | |
| 17 | All the Dagger related code in the Shell can be found in the `com.android.wm.shell.dagger` package, |
| 18 | this is intentional as it keeps the "magic" in a single location. The explicit nature of how |
| 19 | components in the shell are provided is as a result a bit more verbose, but it makes it easy for |
| 20 | developers to jump into a few select files and understand how different components are provided |
| 21 | (especially as products override components). |
| 22 | |
| 23 | The module dependency tree looks a bit like: |
| 24 | - [WMShellConcurrencyModule](frameworks/base/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellConcurrencyModule.java) |
| 25 | (provides threading-related components) |
| 26 | - [WMShellBaseModule](frameworks/base/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellBaseModule.java) |
| 27 | (provides components that are likely common to all products, ie. DisplayController, |
| 28 | Transactions, etc.) |
| 29 | - [WMShellModule](frameworks/base/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellModule.java) |
| 30 | (phone/tablet specific components only) |
| 31 | - [TvPipModule](frameworks/base/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/TvPipModule.java) |
| 32 | (PIP specific components for TV) |
| 33 | - [TvWMShellModule](frameworks/base/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/TvWMShellModule.java) |
| 34 | (TV specific components only) |
| 35 | - etc. |
| 36 | |
| 37 | Ideally features could be abstracted out into their own modules and included as needed by each |
| 38 | product. |
| 39 | |
| 40 | ## Overriding base components |
| 41 | |
| 42 | In some rare cases, there are base components that can change behavior depending on which |
| 43 | product it runs on. If there are hooks that can be added to the component, that is the |
| 44 | preferable approach. |
| 45 | |
| 46 | The alternative is to use the [@DynamicOverride](frameworks/base/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/DynamicOverride.java) |
| 47 | annotation to allow the product module to provide an implementation that the base module can |
| 48 | reference. This is most useful if the existence of the entire component is controlled by the |
| 49 | product and the override implementation is optional (there is a default implementation). More |
| 50 | details can be found in the class's javadoc. |