整个数据连接过程主要靠DataConnection及其对应cdma和gsm的子类
数据连接包含几种状态,构造函数里面必须添加这几种状态,并且指定一个初始状态,如下代码
293 addState(mDefaultState);
294 addState(mInactiveState, mDefaultState);
295 addState(mActivatingState, mDefaultState);
296 addState(mActiveState, mDefaultState);
297 addState(mDisconnectingState, mDefaultState);
298 addState(mDisconnectingBadDnsState, mDefaultState);
299 setInitialState(mInactiveState);
设置初始化状态为mInactiveState,意思是未激活的连接
private DcInactiveState mInactiveState = new DcInactiveState();
所有的状态类都是继承自HierarchicalState,就像
private class DcInactiveState extends HierarchicalState
状态之间可以相互转换,可以接收消息,进行连接断开等操作
需要进行连接之时,调用
public void connect(Message onCompletedMsg, ApnSetting apn);或者
public void connect(Message onCompletedMsg),前者指定apn设置,后者为null
两个方法都是向当前状态发送消息
sendMessage(obtainMessage(EVENT_CONNECT, new ConnectionParams(apn, onCompletedMsg)));或者
sendMessage(obtainMessage(EVENT_CONNECT, new ConnectionParams(null, onCompletedMsg)));
mInactiveState收到消息之后
554 case EVENT_CONNECT:
555 if (DBG) log("DcInactiveState msg.what=EVENT_CONNECT");
556 ConnectionParams cp = (ConnectionParams) msg.obj;
557 cp.tag = mTag;
558 onConnect(cp);
559 transitionTo(mActivatingState);
560 retVal = true;
561 break;
调用onConnect(cp),这是个abstract函数,在GsmDataConnection和CdmaDataConnection里面得到了实现,在CdmaDataConnection的onConnect(cp)里面,调用RIL.java里面的方法:
98 Message msg = obtainMessage(EVENT_SETUP_DATA_CONNECTION_DONE, cp);
99 msg.obj = cp;
101 phone.mCM.setupDataCall(Integer.toString(RILConstants.SETUP_DATA_TECH_CDMA),
102 Integer.toString(dataProfile), cp.apn.apn, cp.apn.user,
103 cp.apn.password, Integer.toString(RILConstants.SETUP_DATA_AUTH_PAP_CHAP), msg);
这样,把请求发送到RIL层之后,转换到mActivatingState状态,接下来就是等待RIL层回应了
跟上一篇博客学习RIL那样,会形成一个异步结果,绑定到消息里面发上来
AsyncResult.forMessage(rr.mResult, ret, null);
rr.mResult.sendToTarget();
而上面的mActivationState状态会收到
589 case EVENT_SETUP_DATA_CONNECTION_DONE:
590 if (DBG) log("DcActivatingState msg.what=EVENT_SETUP_DATA_CONNECTION_DONE");
591
592 ar = (AsyncResult) msg.obj;
593 cp = (ConnectionParams) ar.userObj;
594
595 SetupResult result = onSetupConnectionCompleted(ar);
596 switch (result) {
597 case SUCCESS:
598 // All is well
599 mActiveState.setEnterNotificationParams(cp, FailCause.NONE);
600 transitionTo(mActiveState);
601 break;
602 case ERR_BadCommand:
603 // Vendor ril rejected the command and didn't connect.
604 // Transition to inactive but send notifications after
605 // we've entered the mInactive state.
606 mInactiveState.setEnterNotificationParams(cp, result.mFailCause);
607 transitionTo(mInactiveState);
608 break;
609 case ERR_BadDns:
610 // Connection succeeded but DNS info is bad so disconnect
611 EventLog.writeEvent(EventLogTags.PDP_BAD_DNS_ADDRESS, dnsServers[0]);
612 tearDownData(cp);
613 transitionTo(mDisconnectingBadDnsState);
614 break;
615 case ERR_Other:
616 // Request the failure cause and process in this state
617 phone.mCM.getLastDataCallFailCause(
618 obtainMessage(EVENT_GET_LAST_FAIL_DONE, cp));
619 break;
620 case ERR_Stale:
621 // Request is stale, ignore.
622 break;
623 default:
624 throw new RuntimeException("Unkown SetupResult, should not happen");
625 }
626 retVal = true;
627 break;