SystemUIService啟動-Android13
- 1、SystemUIService啟動
- 2、其他SystemUI services啟動
- 2.1 Dagger依賴注入
- 2.2 Recents為例
1、SystemUIService啟動
SystemUI啟動,及其SystemUIService啟動
<!-- SystemUi service component --><string name="config_systemUIServiceComponent" translatable="false">com.android.systemui/com.android.systemui.SystemUIService</string>
2、其他SystemUI services啟動
2.1 Dagger依賴注入
frameworks/base/packages/SystemUI/src/com/android/systemui/dagger/SystemUICoreStartableModule.kt
- 注解驅動: Dagger基于Java注解進行配置。
@Component
,@Module
和@Inject
是三個核心注解,它們定義了依賴關系的結構。
@Component
定義了一個接口,表示一組相關對象的集合,以及它們之間的依賴關系。
@Module
用于封裝提供者方法,這些方法可以生成或提供依賴項。
@Inject
告訴Dagger需要自動注入某個字段或構造函數。- 代碼生成: 在編譯時,Dagger會根據上述注解生成對應的類和方法,這些生成的代碼負責實際的對象創建和依賴注入。這種靜態類型和編譯時檢查避免了運行時錯誤,并提高了性能。
Dagger Android Injection: 簡化Android應用的依賴注入
用 Dagger2 在 Android 中實現依賴注入
2.2 Recents為例
frameworks/base/packages/SystemUI/README.md
frameworks/base/packages/SystemUI/src/com/android/systemui/Dependency.java
frameworks/base/packages/SystemUI/src/com/android/systemui/dagger/SystemUICoreStartableModule.kt
/** Inject into Recents. */@Binds@IntoMap@ClassKey(Recents::class)abstract fun bindRecents(sysui: Recents): CoreStartable
frameworks/base/packages/SystemUI/src/com/android/systemui/dagger/ReferenceSystemUIModule.java
frameworks/base/packages/SystemUI/src/com/android/systemui/recents/RecentsModule.java
/*** @return The {@link RecentsImplementation} from the config.*/@Providespublic static RecentsImplementation provideRecentsImpl(Context context,ContextComponentHelper componentHelper) {final String clsName = context.getString(R.string.config_recentsComponent);if (clsName == null || clsName.length() == 0) {throw new RuntimeException("No recents component configured", null);}RecentsImplementation impl = componentHelper.resolveRecents(clsName);if (impl == null) {Class<?> cls = null;try {cls = context.getClassLoader().loadClass(clsName);} catch (Throwable t) {throw new RuntimeException("Error loading recents component: " + clsName, t);}try {impl = (RecentsImplementation) cls.newInstance();} catch (Throwable t) {throw new RuntimeException("Error creating recents component: " + clsName, t);}}return impl;}
<string name="config_recentsComponent" translatable="false">com.android.systemui.recents.OverviewProxyRecentsImpl</string>
使用了dagger
的 @IntoMap
注入相關類。只要是 繼承 CoreStartable
類的都將會被注入。