前面在DcTracker初始化过程中注册了大量监听器,其中有两个监听器可以触发APN的创建过程:1、SIM载入完毕;2、APN改变。其中SIM载入完毕的注册是EVENT_RECORDS_LOADED事件,APN改变是注册了一个ApnChangeObserver类来监听。
当SIM载入完毕时,将会触发onRecordsLoaded()
private void onRecordsLoaded() {
logi("onRecordsLoaded: createAllApnList");
mAutoAttachOnCreationConfig = mPhone.getContext().getResources()
.getBoolean(com.android.internal.R.bool.config_auto_attach_data_on_creation);
createAllApnList();
setInitialAttachApn();
if (mPhone.mCi.getRadioState().isOn()) {
if (DBG) log("onRecordsLoaded: notifying data availability");
notifyOffApnsOfAvailability(Phone.REASON_SIM_LOADED);
}
setupDataOnConnectableApns(Phone.REASON_SIM_LOADED);
}
当APN改变时,用户指定APN,将会触发onApnChanged()
/**
* Handles changes to the APN database.
*/
private void onApnChanged() {
DctConstants.State overallState = getOverallState();
boolean isDisconnected = (overallState == DctConstants.State.IDLE ||
overallState == DctConstants.State.FAILED);
createAllApnList();
setInitialAttachApn();
cleanUpConnectionsOnUpdatedApns(!isDisconnected);
// FIXME: See bug 17426028 maybe no conditional is needed.
if (mPhone.getSubId() == SubscriptionManager.getDefaultDataSubId()) {
setupDataOnConnectableApns(Phone.REASON_APN_CHANGED);
}
}
两个事件都会调用createAllApnList()和setInitialAttachApn(),分别是初始化当前卡可用的所有APN和设置默认联网用的APN,首先看createAllApnList()
private void createAllApnList() {
mAllApnSettings = new ArrayList<ApnSetting>();
IccRecords r = mIccRecords.get();
String operator = (r != null) ? r.getOperatorNumeric() : "";
if (operator != null) {
String selection = "numeric = '" + operator + "'";
Cursor cursor = mPhone.getContext().getContentResolver().query(
Telephony.Carriers.CONTENT_URI, null, selection, null, null);
if (cursor != null) {
if (cursor.getCount() > 0) {
mAllApnSettings = createApnList(cursor);
}
cursor.close();
}
}
addEmergencyApnSetting(); // 添加emergencyApnSettings
dedupeApnSettings(); // 去除重复的APN
if (mAllApnSettings.isEmpty()) {
mPreferredApn = null;
} else {
mPreferredApn = getPreferredApn();
if (mPreferredApn != null && !mPreferredApn.numeric.equals(operator)) {
mPreferredApn = null;
setPreferredApn(-1);
}
}
setDataProfilesAsNeeded();
}
这里面主要做了三件事
1、创建一个APN的列表,其中包含:当前SIM对应的APN、紧急APN;
APN的来源是telephony.db数据库中表carries,数据库的的参数是更新于apns-conf.xml文件
2、合并相同的APN;
在apns-conf.xml中的APN有可能又重复的
3、寻找一个当前Prefer的APN参数;
用户使用一张卡时,可能手动选择过使用的APN,因此,当用户再次插拔卡后,getPreferredApn用于找出用户之前选择的APN
这个mPreferredApn会在buildWaitingApns中被检查
如果mPreferredApn满足用户数据链接的需求,那么buildWaitingApns就直接会返回mPreferredApn用于联网使用的APN
如果mPreferredApn已经不能满足用户数据链接的需求时,才会从mAllApnSettings(createAllApnList)中去找合适的APN用于联网
这就是为什么setupData中getNextWaitingApn要去拿list的第0个APN联网,因为当mPreferredApn有效时,只会有一个APN存在apnContext中
以上就是APN的初始化和设置默认值