android4.1 phone模块流程(转)

2019-04-14 18:59发布

去电流程(到framework层):
com.android.phone.OutgoingCallBroadcaster.onCreate(Bundle)
com.android.phone.OutgoingCallBroadcaster.processIntent(Intent)
com.android.phone.CallController.placeCall(Intent)、com.android.phone.OutgoingCallBroadcaster.OutgoingCallReceiver.OutgoingCallReceiver()
com.android.phone.CallController.placeCallInternal(Intent)、com.android.phone.PhoneApp.displayCallScreen()
com.android.phone.PhoneUtils.placeCall(Context, Phone, String, Uri, boolean, Uri, int)
com.android.internal.telephony.CallManager.dial(Phone, String, CallDetails)[connection = app.mCM.dial(phone, numberToDial, callDetails);]
com.android.internal.telephony.Phone.dial(String, CallDetails)[Phone basePhone = getPhoneBase(phone);result = basePhone.dial(dialString, callDetails);]
com.android.internal.telephony.PhoneBase.dial(String, CallDetails)com.android.internal.telephony.PhoneProxy.dial(String, CallDetails)
{
 1 com.android.internal.telephony.cdma.CDMAPhone.dial(String)com.android.internal.telephony.cdma.CDMALTEImsPhone.dial(String)
 2 com.android.internal.telephony.gsm.GSMPhone.dial(String)
 3 com.android.internal.telephony.ims.RilImsPhone.dial(String)
 4 com.android.internal.telephony.sip.SipPhone.dial(String)
 5 com.android.internal.telephony.PhoneProxy.dial(String)[这个dial又有可能是前述5种dial之一,下面以GSMPhone的dial为例]
}
com.android.internal.telephony.CallTracker.dial(String, UUSInfo)
com.android.internal.telephony.gsm.GsmCallTracker.dial(String, UUSInfo)
com.android.internal.telephony.CommandsInterface.dial(String, int, UUSInfo, Message)
com.android.internal.telephony.RIL.dial(String, int, UUSInfo, Message)
com.android.internal.telephony.RIL.dial(String, int, UUSInfo, CallDetails, Message)
com.android.internal.telephony.RIL.send(RILRequest)
[
 msg = mSender.obtainMessage(EVENT_SEND, rr);交给RILSender对象mSender的handleMessage()处理:
 LocalSocket s;
 s.getOutputStream().write(dataLength);
 s.getOutputStream().write(data);
]

 RIL.java文件结构:
 class RILRequest {
    private RILRequest() {
        /*Retrieves a new RILRequest instance from the pool.*/
        static RILRequest obtain(int request, Message result) {
        }
        /*Returns a RILRequest instance to the pool.*/
        void release() {
        }
    }
 }
 public final class RIL extends BaseCommands implements CommandsInterface {
    static final String SOCKET_NAME_RIL = "rild";
    static final String SOCKET_NAME_RIL1 = "rild1";
    class RILSender extends Handler implements Runnable {
            handleMessage(Message msg) {
        }
    }
    class RILReceiver implements Runnable {
        byte[] buffer;
        RILReceiver() {
        }
        run() {
            LocalSocket s = null;
                    LocalSocketAddress l;
            if (mInstanceId == null || mInstanceId == 0 || multiRild == false) {
                            rilSocket = SOCKET_NAME_RIL;
                    } else {
                            rilSocket = SOCKET_NAME_RIL1;
                    }
            try {
                            s = new LocalSocket();
                            l = new LocalSocketAddress(rilSocket,
                                LocalSocketAddress.Namespace.RESERVED);
                            s.connect(l);
                    } catch (IOException ex){
            }
        }    
    }
 }

 因为在 RIL.java 文件中有 static final String SOCKET_NAME_RIL = "rild"; 
 run()函数中有: 
                    s = new LocalSocket(); 
                    l = new LocalSocketAddress(SOCKET_NAME_RIL, 
                            LocalSocketAddress.Namespace.RESERVED); 
                    s.connect(l); 
                    ... 
                    mSocket = s;}
所以可以确定s.getOutputStream().write(data) 是往 rild 的socket写数据。 
到这里,frameworks 请求结束,通过socket转到RIL层处理 dial请求。

来电流程:电话状态信息和电话状态改变这个信息是分开发送的吗?应该是一起发送的但是状态改变给其他模块是用广播,phone自己是用getState获得。因该是这样,以后再验证。
class RILReceiver implements Runnable {
com.android.internal.telephony.RIL.readRilMessage(InputStream, byte[])[class RILReceiver implements Runnable {}线程读取rild中传来的数据]
com.android.internal.telephony.RIL.processResponse(Parcel)
}
com.android.internal.telephony.RIL.processUnsolicited(Parcel)或者com.android.internal.telephony.RIL.processSolicited(Parcel)
android.os.RegistrantList.notifyRegistrants(AsyncResult)[对于来电response是RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED.
mCallStateRegistrants.notifyRegistrants(new AsyncResult(null, null, null));到此即可通知GsmCallTracker电话状态的改变,继而TelephonyManager可以getCallState]
android.os.RegistrantList.internalNotifyRegistrants(Object, Throwable)
android.os.Registrant.internalNotifyRegistrant(Object, Throwable)
android.os.Handler.sendMessage(Message)[这里的handler发送给下面提到的GsmCallTracker对象。由GsmCallTracker的handleMessage()处理EVENT_CALL_STATE_CHANGE事件,从底层得到Phone.State]
[这个中括号里面的是在读取rild数据前已经准备好的。
com.android.internal.telephony.gsm.GSMPhone.GSMPhone(Context, CommandsInterface, PhoneNotifier, boolean)
com.android.internal.telephony.gsm.GsmCallTracker.GsmCallTracker(GSMPhone)
com.android.internal.telephony.BaseCommands.registerForCallStateChanged(Handler, int, Object)
【cm.registerForCallStateChanged(this, EVENT_CALL_STATE_CHANGE, null);这里三个参数构成了Registrant r = new Registrant (h, what, obj)的三个参数,因为android.os.Registrant.Registrant(Handler h, int what, Object obj)所以,这里的handler是this,即GsmCallTracker对象。GsmCallTracker中的cm.registerForCallStateChanged(this, EVENT_CALL_STATE_CHANGE, null);实际上是com.android.internal.telephony.BaseCommands.registerForCallStateChanged(Handler, int, Object),该函数中的mCallStateRegistrants.add(r);即把(this, EVENT_CALL_STATE_CHANGE, null)三个参数构成的Registrant加在了BaseCommands的RegistrantList中即mCallStateRegistrants。而RIL.java文件中的com.android.internal.telephony.RIL.processUnsolicited(Parcel)方法在response case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED时的执行语句:mCallStateRegistrants.notifyRegistrants(new AsyncResult(null, null, null)),这里的mCallStateRegistrants是继承的BaseCommands类的字段。可知GsmCallTracker得到了电话状态改变的通知。】
android.os.RegistrantList.add(Registrant)[mCallStateRegistrants.add(r);]
]

com.android.internal.telephony.gsm.GsmCallTracker.handleMessage(Message)[case EVENT_CALL_STATE_CHANGE:pollCallsWhenSafe();]
com.android.internal.telephony.CallTracker.pollCallsWhenSafe()
[
 lastRelevantPoll = obtainMessage(EVENT_POLL_CALLS_RESULT);[Message android.os.Handler.obtainMessage(int what)这里obtainMessage直到RIL.java rr.mResult.sendToTarget()才发送]
 cm.getCurrentCalls(lastRelevantPoll);
]
1、com.android.internal.telephony.RIL.getCurrentCalls(Message)
   com.android.internal.telephony.RIL.send(RILRequest)[发送RILRequest:RIL_REQUEST_GET_CURRENT_CALLS]
   com.android.internal.telephony.RIL.RILSender.handleMessage(Message)[case EVENT_SEND:]
   com.android.internal.telephony.RIL.processSolicited(Parcel)[接着RIL调用processSolicited处理RIL_REQUEST_GET_CURRENT_CALLS的返回结果]
   com.android.internal.telephony.RIL.responseCallList(Parcel)
   android.os.AsyncResult.forMessage(Message, Object, Throwable)[AsyncResult.forMessage(rr.mResult, ret, null);这句是保存结果对象ret和结果信息rr.mResult,并返回一个AsyncResult]
   android.os.Message.sendToTarget()[rr.mResult.sendToTarget();信息交给谁处理了呢?lastRelevantPoll是个Message,经RILRequest.obtain(int, Message)变成了rr中的rr.mResult字段——这是个Message类型的。]

2、这里的pollCallsWhenSafe是GsmCallTracker继承的CallTracker的方法,所以EVENT_POLL_CALLS_RESULT事件其实还是由GsmCallTracker的handleMessage()处理[某类的方法中obtain的message由它自己的handler处理,当时这样想的]
  com.android.internal.telephony.gsm.GsmCallTracker.handlePollCalls(AsyncResult)、com.android.internal.telephony.gsm.GsmCallTracker.updatePhoneState()、phone.notifyPreciseCallStateChanged();
    [1
    -->com.android.internal.telephony.gsm.GSMPhone.notifyNewRingingConnection(Connection)[此时在PhoneApp中已经创建CallNotifier,CallNotifier构造方法中有如下方法:]
    -->com.android.phone.CallNotifier.registerForNotifications()[mCM.registerForNewRingingConnection(this, PHONE_NEW_RINGING_CONNECTION, null);this,对应下行的Handler参数,说明,由CallNotifier的handleMessage()处理PHONE_NEW_RINGING_CONNECTION信息]
    -->com.android.internal.telephony.CallManager.registerForNewRingingConnection(Handler, int, Object)
    -->com.android.phone.CallNotifier.handleMessage(Message)
    -->com.android.phone.CallNotifier.onNewRingingConnection(AsyncResult)
    -->com.android.phone.CallNotifier.showIncomingCall()
    -->com.android.phone.NotificationMgr.updateNotificationAndLaunchIncomingCallUi()
    -->com.android.phone.NotificationMgr.updateInCallNotification(boolean)
    -->android.app.PendingIntent.getActivity(Context, int, Intent, int)-----PendingIntent that can be used to launch the InCallScreen.
    PendingIntent android.app.PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
    Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent).
    -->com.android.phone.PhoneApp.createInCallIntent(int)
    -->com.android.phone.PhoneApp.createInCallIntent()
    -->com.android.phone.InCallScreen.internalResolveIntent(Intent)
    ]
    [2
    -->com.android.internal.telephony.gsm.GSMPhone.notifyPhoneStateChanged()
    -->com.android.internal.telephony.PhoneNotifier.notifyPhoneState(Phone)实际上用的是com.android.internal.telephony.DefaultPhoneNotifier.notifyPhoneState(Phone)
    -->com.android.server.TelephonyRegistry.notifyCallState(int, String)
    -->com.android.server.TelephonyRegistry.broadcastCallStateChanged(int, String)
    -->android.content.Context.sendBroadcast(Intent, String)发送的intent如下:
         Intent intent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        intent.putExtra(Phone.STATE_KEY, DefaultPhoneNotifier.convertCallState(state).toString());
        if (!TextUtils.isEmpty(incomingNumber)) {
            intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, incomingNumber);
        }
        mContext.sendBroadcast(intent, android.Manifest.permission.READ_PHONE_STATE);
    ]
电话状态改变是怎么传给上层的呢?TelephonyManager得到的是CallState,DefaultPhoneNotifier.convertCallState(State)把Phone.State转化为了CallState,Phone.State来自CallTracker。CallTracker直接得到状态还是得到状态改变?似乎是状态改变信息.以后再验证。
     /*  /frameworks/base/telephony/java/android/telephony/TelephonyManager.java android.telephony.TelephonyManager
     * This was a {@link android.content.Context#sendStickyBroadcast sticky}
     * broadcast in version 1.0, but it is no longer sticky.
     * Instead, use {@link #getCallState} to synchronously query the current call state.
     */
    android.telephony.TelephonyManager.getCallState()
    com.android.phone.PhoneInterfaceManager.getCallState()
    com.android.internal.telephony.DefaultPhoneNotifier.convertCallState(State)[return DefaultPhoneNotifier.convertCallState(mPhone.getState());]
    Phone的state由哪来的呢?com.android.internal.telephony.Phone.getState()它有好几个实现,以GSMPhone为例,
    com.android.internal.telephony.gsm.GSMPhone.getState()[return mCT.state;mCT应该是指GSMCallTracker它的state字段又哪来的呢?继承的CallTracker的state字段,类型为Phone.state,所以GSMCallTracker只有Phone字段(GSMPhone phone;)即可。GSMCallTracker里面又有个mCM,cm = phone.mCM;cm.registerForCallStateChanged(this, EVENT_CALL_STATE_CHANGE, null);这里的cm不是CallManager类而是实现了CommandsInterface接口的BaseCommands类,实际上用的是com.android.internal.telephony.BaseCommands.registerForCallStateChanged(Handler, int, Object)方法。class RIL extends BaseCommands所以RIL类中的mCallStateRegistrants实际上是继承的BaseCommands类中的字段。又BaseCommands类中有方法
public void registerForCallStateChanged(Handler h, int what, Object obj) {
        Registrant r = new Registrant (h, what, obj);

        mCallStateRegistrants.add(r);
    }
RIL类中有com.android.internal.telephony.RIL.processUnsolicited(Parcel)
switch(response) {
...
   case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED:
    if (RILJ_LOGD) unsljLog(response);
    mCallStateRegistrants.notifyRegistrants(new AsyncResult(null, null, null));
...
}所以,以BaseCommands类为桥梁,GSMCallTracker从RIL类中获得了电话状态改变的通知。这个里面有没有状态信息?只传送状态改变信息?似乎是后者,以后再验证。不是,因为TelephonyManager.getCallState()
    ]