android 系统数据业务---打开

2数据业务打开

DcTracker的onTrySetupData调用流程图如下,

android 系统数据业务---打开_第1张图片

onTrySetupData方法如下,

protected boolean onTrySetupData(String reason) {
   if (DBG) log("onTrySetupData: reason=" + reason);
   setupDataOnConnectableApns(reason);//利用可连接的APN进行拨号
   return true;
}

setupDataOnConnectableApns方法如下,

private void setupDataOnConnectableApns(String reason) {
    setupDataOnConnectableApns(reason, RetryFailures.ALWAYS);
}

这里RetryFailures.ALWAYS表示连网失败话,会一直重试。

trySetupData主要根据当前终端的运行状态,判断框架是否应该继续拨号。

setupData主要逻辑如下,

1,首先根据一些列的条件进行判断,然后创建一个DataConnection对象,

if (dcac == null) {
   dcac = createDataConnection();
}

2,创建成功后,更新状态,

apnContext.setDataConnectionAc(dcac);
apnContext.setApnSetting(apnSetting);
apnContext.setState(DctConstants.State.CONNECTING);
mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());

Message msg = obtainMessage();
msg.what = DctConstants.EVENT_DATA_SETUP_COMPLETE;
msg.obj = apnContext;
//调用DcAsyncChannel的bringUp函数进行拨号上网
dcac.bringUp(apnContext, getInitialMaxRetry(), profileId, radioTech, mAutoAttachOnCreation,
                msg);

首先调用createDataConnection方法构造DataConnection和DcAsyncChannel对象,

然后调用调用DcAsyncChannel的bringUp函数进行拨号上网。

2.1 createDataConnection

createDataConnection的调用流程图如下,

android 系统数据业务---打开_第2张图片

createDataConnection方法如下,

private DcAsyncChannel createDataConnection() {
   if (DBG) log("createDataConnection E");
      //每个DataConnection有唯一的id号
int id = mUniqueIdGenerator.getAndIncrement();
    DataConnection conn = DataConnection.makeDataConnection(mPhone, id,
                      this, mDcTesterFailBringUpAll, mDcc);
mDataConnections.put(id, conn);
 //创建DcAsyncChannel
DcAsyncChannel dcac = new DcAsyncChannel(conn, LOG_TAG);
//完成dctracker与dataconnection之间handler的绑定
    int status = dcac.fullyConnectSync(mPhone.getContext(), this, conn.getHandler());
    if (status == AsyncChannel.STATUS_SUCCESSFUL) {
       mDataConnectionAcHashMap.put(dcac.getDataConnectionIdSync(), dcac);
    } else {
       loge("createDataConnection: Could not connect to dcac=" + dcac + " status=" + status);
     }
     if (DBG) log("createDataConnection() X id=" + id + " dc=" + conn);
     return dcac;
}

其实就是创建了2个对象, DataConnection和DcAsyncChannel,然后将2个对象通过Handler绑定,主要是为了通信。

1,创建DataConnection

DataConnection的makeDataConnection方法如下,

static DataConnection makeDataConnection(PhoneBase phone, int id,
      DcTrackerBase dct, DcTesterFailBringUpAll failBringUpAll, DcController dcc) {
   DataConnection dc = new DataConnection(phone,
                "DC-" + mInstanceNumber.incrementAndGet(), id, dct, failBringUpAll, dcc);
   dc.start();
   if (DBG) dc.log("Made " + dc.getName());
   return dc;
}

首先构造DataConnection对象, DataConnection其实是一个状态机, DataConnection类的定义如下,

public final class DataConnection extends StateMachine {

这个状态机有7种状态,初始状态为

addState(mDefaultState);//默认状态
addState(mInactiveState, mDefaultState);//未激活状态
addState(mActivatingState, mDefaultState);//正在激活状态
addState(mRetryingState, mDefaultState);//尝试激活状态
addState(mActiveState, mDefaultState);//已激活状态
addState(mDisconnectingState, mDefaultState);//正在断开状态
addState(mDisconnectingErrorCreatingConnection, mDefaultState);//错误状态
setInitialState(mInactiveState);//初始状态

状态机构造完成之后,就启动状态机。

2,创建DcAsyncChannel对象,并绑定

DcAsyncChannel定义如下,

public class DcAsyncChannel extends AsyncChannel {

DcAsyncChannel继承于AsyncChannel,其详细原理请见····

2.2拨号过程

DcAsyncChannel的bringUp方法直接发送消息,

sendMessage(DataConnection.EVENT_CONNECT,
           new ConnectionParams(apnContext, initialMaxRetry, profileId,
                            rilRadioTechnology, retryWhenSSChange, onCompletedMsg));

直接回调DataConnection状态机的processMessage方法, DataConnection有6个状态,调用哪个的

processMessage方法呢?上个小结中说过,初始状态为DcInactiveState,因此直接调用DcInactiveState

的processMessage方法,对EVENT_CONNECT消息的处理如下,

ConnectionParams cp = (ConnectionParams) msg.obj;//获取消息
if (initConnection(cp)) {
     onConnect(mConnectionParams);//进行拨号
     transitionTo(mActivatingState);//进入ActivatingState状态
} else {
    •••
    notifyConnectCompleted(cp, DcFailCause.UNACCEPTABLE_NETWORK_PARAMETER,
                                false); //通知DcTracker拨号失败
}
retVal = HANDLED;
break;

首先调用onConnect进行拨号,然后调用transitionTo方法进入ActivatingState状态。

onConnect方法主要逻辑如下,

Message msg = obtainMessage(EVENT_SETUP_DATA_CONNECTION_DONE, cp);
msg.obj = cp; // 构建Message对象
•••
//利用DcAsyncChannel传输的ConnectionParams信息调用RIL的setupDataCall进行拨号
mPhone.mCi.setupDataCall(Integer.toString(cp.mRilRat + 2), Integer.toString(cp.mProfileId),
                mApnSetting.apn, mApnSetting.user, mApnSetting.password,
                Integer.toString(authType), protocol, msg);

RIL的setupDataCall方法如下,

setupDataCall(String radioTechnology, String profile, String apn,
            String user, String password, String authType, String protocol,
            Message result) {
        //构造RILRequest对象
        RILRequest rr
                = RILRequest.obtain(RIL_REQUEST_SETUP_DATA_CALL, result);
          //封装消息
rr.mParcel.writeInt(7);
        rr.mParcel.writeString(radioTechnology);
        rr.mParcel.writeString(profile);
        rr.mParcel.writeString(apn);
        rr.mParcel.writeString(user);
        rr.mParcel.writeString(password);
        rr.mParcel.writeString(authType);
        rr.mParcel.writeString(protocol);
        if (RILJ_LOGD) riljLog(rr.serialString() + "> "
                + requestToString(rr.mRequest) + " " + radioTechnology + " "
                + profile + " " + apn + " " + user + " "
                + password + " " + authType + " " + protocol);
        send(rr); //调用send方法发送消息
    }

具体的RIL机制在此就不论述了,本文的重点不在于RIL机制,

只需要知道的是当收到RIL_REQUEST_SETUP_DATA_CALL消息时, 将向DataConnection发送

EVENT_SETUP_DATA_CONNECTION_DONE的消息.

你可能感兴趣的:(---【数据业务】)