您的位置:首页 > 移动开发

Android7.0 Messaging源码分析(2) - Application 创建篇

2016-11-16 10:22 316 查看
Messaging 源码位于 packages/apps/Messaging 目录下,application类名是BugleApplication,

在 onCreate 方法里做了两件事,

1 FactoryImpl.register(getApplicationContext(), this);

2 Thread.setDefaultUncaughtExceptionHandler(this);

来看看FactoryImpl.register(getApplicationContext(), this);做了什么事情,

public static Factory register(final Context applicationContext, final BugleApplication application) {
// This only gets called once (from BugleApplication.onCreate), but its not called in tests.
Assert.isTrue(!sRegistered);
Assert.isNull(Factory.get());

final FactoryImpl factory = new FactoryImpl();
Factory.setInstance(factory);
sRegistered = true;

// At this point Factory is published. Services can now get initialized and depend on Factory.get().
factory.mApplication = application;
factory.mApplicationContext = applicationContext;
factory.mMemoryCacheManager = new MemoryCacheManager(); // 创建缓缓管理器
factory.mMediaCacheManager = new BugleMediaCacheManager(); // 创建媒体缓存管理器
factory.mMediaResourceManager = new MediaResourceManager(); // 创建媒体资源管理器
factory.mBugleGservices = new BugleGservicesImpl(applicationContext); // 创建Gservices实现类
factory.mBugleApplicationPrefs = new BugleApplicationPrefs(applicationContext); // 创建 application prefs
factory.mDataModel = new DataModelImpl(applicationContext); // 创建数据模型
factory.mBugleWidgetPrefs = new BugleWidgetPrefs(applicationContext); // 创建 widget prefs
factory.mUIIntents = new UIIntentsImpl(); // 创建UI intent 实现类
factory.mContactContentObserver = new ContactContentObserver();
factory.mMediaUtil = new MediaUtilImpl(); // 创建媒体工具类
factory.mSubscriptionPrefs = new SparseArray<BugleSubscriptionPrefs>();
factory.mCarrierConfigValuesLoader = new BugleCarrierConfigValuesLoader(applicationContext); // 创建运营商配置加载类

Assert.initializeGservices(factory.mBugleGservices);
LogUtil.initializeGservices(factory.mBugleGservices);

// 如果已经获取了权限,就调用 onRequiredPermissionsAcquired 方法
if (OsUtil.hasRequiredPermissions()) {
factory.onRequiredPermissionsAcquired();
}

return factory;
}


前面创建了一大堆管理类,工具类;后面如果已获取了权限就调用了 onRequiredPermissionsAcquired 方法,没有获取权限就不调用。从调用关系看,没有获取权限时会展示 PermissionCheckActivity 页面,用户授予权限后会调用onRequiredPermissionsAcquired 方法;

onRequiredPermissionsAcquired 方法如下:

@Override
public void onRequiredPermissionsAcquired() {
if (sInitialized) {
return;
}
sInitialized = true;

mApplication.initializeSync(this);

final Thread asyncInitialization = new Thread() {
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
mApplication.initializeAsync(FactoryImpl.this);
}
};
asyncInitialization.start();
}


它运行了一个同步方法,一个异步方法。分别如下,

100    // Called by the "real" factory from FactoryImpl.register() (i.e. not run in tests)
101    public void initializeSync(final Factory factory) {
102        Trace.beginSection("app.initializeSync");
103        final Context context = factory.getApplicationContext();
104        final BugleGservices bugleGservices = factory.getBugleGservices();
105        final BuglePrefs buglePrefs = factory.getApplicationPrefs();
106        final DataModel dataModel = factory.getDataModel();
107        final CarrierConfigValuesLoader carrierConfigValuesLoader =
108                factory.getCarrierConfigValuesLoader();
109
110        maybeStartProfiling();
111
112        BugleApplication.updateAppConfig(context); // 更新 app 配置信息
113
114        // Initialize MMS lib 初始化 MMS 类库
115        initMmsLib(context, bugleGservices, carrierConfigValuesLoader);
116        // Initialize APN database 初始化 APN 数据库
117        ApnDatabase.initializeAppContext(context);
118        // Fixup messages in flight if we crashed and send any pending 修复发送时应用程序崩溃的消息,发送所有待发送的消息
119        dataModel.onApplicationCreated();
120        // Register carrier config change receiver 注册运营商配置改变监听器
121        if (OsUtil.isAtLeastM()) {
122            registerCarrierConfigChangeReceiver(context);
123        }
124
125        Trace.endSection();
126    }


这个同步方法做了一些比较重要的事情,比如初始化 MMS 类库,APN数据库等,还处理了待发送和发送失败的消息。

来看异步方法,

164    // Called from thread started in FactoryImpl.register() (i.e. not run in tests)
165    public void initializeAsync(final Factory factory) {
166        // Handle shared prefs upgrade & Load MMS Configuration
167        Trace.beginSection("app.initializeAsync");
168        maybeHandleSharedPrefsUpgrade(factory); // 处理 pref 升级
169        MmsConfig.load(); // 加载 Mms 配置
170        Trace.endSection();
171    }


异步方法做了处理 pref 和 Mms 配置。

这两步完成 Application 创建流程就走完了。

总结

Messaging application创建过程主要做了下面几件事,

创建MemoryCacheManager,MediaResourceManager,MediaUtilImpl 等管理类,工具类。

初始化 MMS 类库,配置项。

处理应用崩溃时处在发送中以及待发送的信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android messaging