彩信APN切换流程(framework)



TransactionService之前的流程比较简单不在赘述,现在主要理清下从TransactionService到framework的流程。



首先会走到TransactionService的onStartCommand,在此函数中会对当前网络状态进行判断,然后调用到launchTransaction,如果此时网络不可用,会弹出toast,否则就向自己的handler发送EVENT_TRANSACTION_REQUEST。

Handler收到EVENT_TRANSACTION_REQUEST消息后,根据不同的transactionType对data进行不同的处理,我们用的是SEND_TRANSACTION,所以创建了transaction = newSendTransaction,紧接着调用processTransaction,开始进行transaction。

这里就到了关键的函数----beginMmsConnectivity,会根据此函数的返回值来进行下一步的操作,如果是APN_REQUEST_STARTED,则会把当前的MMS放到等待队列里,直到对应的APNready之后才会发送。

beginMmsConnectivity 会调到connectivityservice的startUsingNetworkFeature,下面重点看下这个函数。



    public intstartUsingNetworkFeature(int networkType, Stringfeature,              // networkType这里我们是0,也就是TYPE_MOBILE,feature 是 enablemms1

            IBinderbinder) {



…….

       if(networkType == ConnectivityManager.TYPE_MOBILE) {

           //if type is MMS,continue setup data call

           Slog.d(TAG, "if type is MMS,continue setup data call");

           if (!getMobileDataEnabledByPhoneId(getPhoneIdByFeature(feature)) &&

                   !(feature.indexOf(Phone.FEATURE_ENABLE_MMS)!=-1)) {

           //if (!getMobileDataEnabledByPhoneId(getPhoneIdByFeature(feature))) {

               if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");

               return Phone.APN_TYPE_NOT_AVAILABLE;

           }   if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DM)) {

               usedNetworkType = ConnectivityManager.TYPE_MOBILE_DM;

           } else if (TextUtils.equals(feature.substring(0,Phone.FEATURE_ENABLE_MMS.length()), Phone.FEATURE_ENABLE_MMS)){       //这里判断feature的前几位是不是enablemms

               skipAvailableCheck = true;

               usedNetworkType =ConnectivityManager.getMmsTypeByPhoneId(getPhoneIdByFeature(feature));// 将usedNetworkType赋值为11,即TYPE_MOBILE_DM+1

                                    /* yuqing :if send sms with sim2 ,usedNetworkType=TYPE_MOBILE_DM+1=11 */

           } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {

               usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;

           } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {

               usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;

           } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {

               usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;

           }

       }

       NetworkStateTracker network = mNetTrackers[usedNetworkType]; //根据usedNetworkType来选取对应的NetworkStateTracker

        if(DBG) Slog.d(TAG, "network = " + network);

       if (network != null) {

           if (usedNetworkType != networkType){              //传入的是0,现在NetworkStateTracker是11

               Integer currentPid = new Integer(getCallingPid());



               NetworkStateTracker radio = mNetTrackers[networkType];

               NetworkInfo ni = network.getNetworkInfo();



               // TODO: using serviceState to check is better for Msms

               if (!skipAvailableCheck && ni.isAvailable() == false) {

                   if (DBG) Slog.d(TAG, "special network not available");

                   return Phone.APN_TYPE_NOT_AVAILABLE;

               }              synchronized(this) {

                   mFeatureUsers.add(f);

                   if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {

                       // this gets used for per-pid dns whenconnected

                       mNetRequestersPids[usedNetworkType].add(currentPid);

                   }

               }

               mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,

                       f), getRestoreDefaultNetworkDelay());



               if (DBG) Slog.d(TAG,"ni.isConnectedOrConnecting()="+ni.isConnectedOrConnecting()); //当卡2的数据连接还没起来的时候,这里是disconnect

               if ((ni.isConnectedOrConnecting() == true) &&

                       !network.isTeardownRequested()) {

                   if (ni.isConnected() == true) {

                       // add the pid-specific dns

                       handleDnsConfigurationChange(networkType);

                       if (DBG) Slog.d(TAG, "special network already active");

                       return Phone.APN_ALREADY_ACTIVE;

                   }

                   if (DBG) Slog.d(TAG, "special network already connecting");

                   return Phone.APN_REQUEST_STARTED;

               }



               // check if the radio in play can make another contact

               // assume if cannot fornow               if (!isAlreadyConnecting) {

                   if (DBG) Slog.d(TAG, "reconnecting to special network");

                   if(!network.reconnect()){                // 重要函数,实际调用的是MobileDataStateTracker.reconnect(),下面分析

                                             

                       //if the mms apn is not available, remove the request from mMmsFeatureRequest

                       //if not remove it, the othe mms send request will be pending.

                       if (isMmsFeature(feature) && mMmsFeatureRequest.size() > 0) {

                           mMmsFeatureRequest.remove(0);

                       }

                       return Phone.APN_TYPE_NOT_AVAILABLE;

                   }

               } else {

                   if (DBG) Slog.d(TAG, "already reconnecting to special network");

               }

               return Phone.APN_REQUEST_STARTED;

           } else{               synchronized(this) {

                   mFeatureUsers.add(f);

               }

               mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,

                       f), getRestoreDefaultNetworkDelay());



               return network.startUsingNetworkFeature(feature,

                       getCallingPid(), getCallingUid());

           }

       }

       return Phone.APN_TYPE_NOT_AVAILABLE;

}



MobileDataStateTracker.reconnect()中会调用setEnableApn (mApnType,true),此时的apntype是已经转换好了的, mApnType =networkTypeToApnType(netType);

代码如下,我们在创建的时候,选择的是第11个即mNetTrackers[usedNetworkType],也就是说netype =11,但是经过这个函数转换后神奇的变成了MMS,不知是何用意,暂且先不管。

    public static StringnetworkTypeToApnType(int netType) {

       switch(netType) {

           case ConnectivityManager.TYPE_MOBILE:

               return Phone.APN_TYPE_DEFAULT;  // TODO - use just one of these

           case ConnectivityManager.TYPE_MOBILE_MMS:

               return Phone.APN_TYPE_MMS;

           case ConnectivityManager.TYPE_MOBILE_SUPL:

               return Phone.APN_TYPE_SUPL;

           case ConnectivityManager.TYPE_MOBILE_DUN:

               return Phone.APN_TYPE_DUN;

           case ConnectivityManager.TYPE_MOBILE_HIPRI:

               return Phone.APN_TYPE_HIPRI;

           case ConnectivityManager.TYPE_MOBILE_DM:

               return Phone.APN_TYPE_DM;          

default:

                    // Msms MMS netType @zha

               if (ConnectivityManager.isNetworkTypeValid(netType)) {

                   return Phone.APN_TYPE_MMS;

                    } else {

                   Log.e(TAG, "Error mapping networkType " + netType + " toapnType.");

                   return null;

                    }

       }

    }

setEnableApn调用了mPhoneService.enableApnType(apnType),即DataConnectionTracker.enableApnType,返回值就是此函数中给出的,此函数中又调用了apnTypeToId来转换ID,

转换为APN_MMS_ID,紧接着调用setEnabled,发送EVENT_ENABLE_NEW_APN给自己的handler,回调到onEnableApn--àonEnableNewApn()

调用GsmDataConnectionTracker的onEnableNewApn,这里会调用       cleanUpConnection(true, Phone.REASON_APN_SWITCHED);disconnect当前APN,

停止netstate的poll,也就是为什么我们看到E的图标没有了。

调用MsmsGsmDataConnectionTracker的enableApnType,到MsmsGsmDataConnectionTrackerProxy.onEnableNewApn,此函数就是最终gprsattach的地方。



public staticvoid onEnableNewApn(int phoneId) {

       synchronized (sInstance) {

           log("onEnableNewApn(" + phoneId + ") activePhoneId:" +sActivePhoneId);

           if (phoneId == INVALID_PHONE_ID){                       //传入PHONEID不正确

               log("phoneId is invalid,onEnableNewApn out!!!");

               return;

           }

           sRequestConnectPhoneId = phoneId;

           sRequestPhoneIdBeforeVoiceCallEnd = sRequestConnectPhoneId;

           if (sActivePhoneId != INVALID_PHONE_ID) {

               if(sRequestConnectPhoneId!=sActivePhoneId){             //当前APN存在,且phoneid和请求值不等,即双卡之间切换时

                 

                    if(sTracker[sActivePhoneId].isAllPdpDisconnectDone()) { //PDP连接全部断开时,switchphone E图标消失,否则强制断开连接。

                       checkAndSwitchPhone(sActivePhoneId, null);

                   } else {

                       sTracker[sActivePhoneId].cleanupConnections(true,

                       Phone.REASON_DATA_DISABLED);

                   }

               }else{

                   sTracker[sActivePhoneId].onEnableNewApnInternal();

               }

           } else {   //当前activephone不存在,

                if(sTracker[sRequestConnectPhoneId].getCurrentGprsState() ==ServiceState.STATE_IN_SERVICE) { //如果GPRS inservice,发起数据连接

                   sTracker[sRequestConnectPhoneId].trySetupData(Phone.REASON_APN_SWITCHED);

                   sActivePhoneId = sRequestConnectPhoneId;

               } else {

                   sTracker[sRequestConnectPhoneId].mGsmPhone.mCM.setGprsAttach(null);//notinservice ,则发起gprsattach

                   sActivePhoneId = sRequestConnectPhoneId;

              }

           }

           if(phoneId == sActivePhoneId){ //如果传入值和当前active值相同

               // do not need to switch phone, so we initialize sRequestConnectPhoneId.

               sRequestConnectPhoneId=INVALID_PHONE_ID;

           }

           log("onEnableNewApn out");

       }

}

如果这次setapnenable没有成功(返回APN_REQUEST_STARTED),TransactionService会将当前的Transaction放入pending列表,

并侦听EVENT_DATA_STATE_CHANGED事件,在合适的时候重新发起,调用processPendingTransaction处理pending的Transaction。

你可能感兴趣的:(framework)