Dagger2在SystemUI中的应用,android组件化和插件化

在SystemUIApplication onCreate时回调 ContextAvailableCallback, 构建SystemUIFactory,并对它进行初始化

src/com/android/systemui/SystemUIFactory.java

public static void createFromConfig(Context context, boolean fromTest) {
    if (mFactory != null) {
        return;
    }

    final String clsName = context.getString(R.string.config_systemUIFactoryComponent);
    if (clsName == null || clsName.length() == 0) {
        throw new RuntimeException("No SystemUIFactory component configured");
    }

    try {
        Class<?> cls = null;
        cls = context.getClassLoader().loadClass(clsName);
        // 创建SystemUIFactory实例
        mFactory = (SystemUIFactory) cls.newInstance();
        // 初始化SystemUIFactory
        mFactory.init(context, fromTest);
    } catch (Throwable t) {
        Log.w(TAG, "Error creating SystemUIFactory component: " + clsName, t);
        throw new RuntimeException(t);
    }
}

public void init(Context context, boolean fromTest)
        throws ExecutionException, InterruptedException {
    ...
    // 获取dagger组件
    mRootComponent = buildGlobalRootComponent(context);
    ...

    // 获取systemui的dagger组件
    // And finally, retrieve whatever SysUI needs from WMShell and build SysUI.
    SysUIComponent.Builder builder = mRootComponent.getSysUIComponent();
    mSysUIComponent = builder.build();
    ...

    // 构建Dependency实例并初始化
    // Every other part of our codebase currently relies on Dependency, so we
    // really need to ensure the Dependency gets initialized early on.
    Dependency dependency = mSysUIComponent.createDependency();
    dependency.start();
}

protected GlobalRootComponent buildGlobalRootComponent(Context context) {
    return DaggerGlobalRootComponent.builder()
            .context(context)
            .build();
} 

Dependency组件使用@Lazy标签懒加载: 首先构建LazyDependencyCreator放入mProviders,然后在真正使用dependency时调用createDep

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值