Android中打电话的数据流程分析
1.所有流程的起点是从拨号后按下拨号键开始,此步的代码在/android sourcecode/packages/Contacts/src/com/android/contacts/目录的TwelveKeyDialer.java文件中,相关代码如下:
代码如下:
dialButtonPressed() {
.........
final String number = mDigits.getText().toString();
startActivity(newDialNumberIntent(number));
mDigits.getText().clear();
finish();
}
代码中newDialNumberIntent()方法定义如下:
代码如下:
private Intent newDialNumberIntent(String number) {
final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, Uri.fromParts("tel", number, null));
.............
}
从newDialNumberIntent的定义可以看出,当拨号键按下以后,TwelveKeyDial会启动一个特定的组件,该组件的ACTION为:ACTION_CALL_PRIVILEGED,经过查找,该ACTION启动的组件是目下:/android sourcecode/packeges/Phone/的一个文件,在该文件下的AndroidMenifest.xml中可以查到:“ACTION_CALL_PRIVILEGED”启动的Activity的名字是:PrivilegedOutgoingCallBroadcast,但是我们到/android sourcecode/packeges/Phone/src/....目下并找不到该文件,因为该文件在AndroidMenifest.xml中标记有点特殊:
<activity-alias />,这个标签的意思是这个Activity是另一个Activity的别名,真实的Activity在标签中用“android:targetActivity = OutgoingCallBroadcast”标出,所以 “ACTION_CALL_PRIVILEGED”启动的 PrivilegedOutgoingCallBroadcast 所对应的真实“身份”是 “OutgoingCallBroadcast”。
2.这个时候电话的数据已经流到OutgoingCallBroadcast.java中了。
在OutgoingCallBroadcast.java的onCreate()方法中有:
代码如下:
<PRE class=java name="code">protected void onCreate(Bundle icicle) {
.......
Intent intent = getIntent();
........
String action = intent.getAction();
.......
final boolean emergencyNum = (number != null) && PhoneNumUtils.isEmergencyNumber(number);//判断号码是否是紧急号码
.......
if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
action = emergencyNum ? Intent.ACTION_CALL_EMERGENCY : Intent.ACTION_CALL;
intent.setAction(action);
}
.......
intent.setClass(this, InCallScreen.class);
startActivity(intent);
}</PRE><P></P>
<PRE></PRE>
在这个方法中,判断如果所收到的ACTION是“ACTION_CALL_PRIVILEGED”,那么根据所输入的号码是否是紧急号码进行转换,如果是紧急号码,则ACTION = Intent.ACTION_CALL_EMERGENCY,否则ACTION = Intent.ACTION_CALL,并启动转换Activity :InCallScreen.java
<P></P>
<P>3. InCallScreen.java依然在目录/packeges/Phone/src/com/android/phone下。</P>
<P>InCallScreen的onCreate中调用initInCallScreen初始化打电话界面,并调用registerForPhoneStates注册电话状态监听.<BR>
</P>
<P>在onNewIntent()方法中有:</P>
<P><PRE class=java name="code">protected void onNewIntent(Intent intent) {
..........
String action = intent.getAction();
..........
else if (action.equals(Intent.ACTION_CALL) || action.equals(Intent.ACTION_CALL_EMERGENCY)) {
..........
InCallInitStatus status = placeCall(intent);
}
}
//placeCall
private InCallInitStatus placeCall(Intent intent) {
..............
int callStatus = PhoneUtils.placeCall(........);
}</PRE>InCallScreen.java中的placeCall方法调用PhoneUtils.java文件的placeCall方法。<BR>
<P></P>
<P>4. PhoneUtils.java依然在目录/packeges/Phone/src/com/android/phone下。</P>
<P><PRE class=java name="code">public static int placeCall(...) {
Connection connection;
connection = PhoneApp.getInstance().mCM.dial(phone, numberToDial);
}</PRE>继续追踪,在PhoneApp.java中发现,mCM是CallManager.java类的一个对象,而CallManager.java是属于frameworks层的,所以,这个时候数据流已经进入frameworks.<P></P>
<P>5. 进入/frameworks/base/telephony/java/com/android/internal/telephony目录。</P>
<P>在CallManager.java的dial()方法中,有:</P>
<P><PRE class=java name="code"><SPAN style="BACKGROUND-COLOR: rgb(255,255,255); FONT-FAMILY: Arial, Verdana, sans-serif; WHITE-SPACE: normal"></SPAN><PRE class=java name="code">public Connection dial(Phone phone, String dialNumber) throws CallStateException {
Phone basePhone = getPhoneBase(phone);
Connection result;
<SPAN style="COLOR: #3333ff">result = basePhone.dial(dialString);</SPAN>
........
}
private static Phone getPhoneBase(Phone phone) {
if (phone instanceof PhoneProxy) {
<SPAN style="COLOR: #ff0000">return phone.getForegroundCall().getPhone();</SPAN>
}
return phone;
}</PRE><P></P>
<PRE></PRE>
<PRE class=java name="code"><SPAN style="BACKGROUND-COLOR: rgb(255,255,255); FONT-FAMILY: Arial, Verdana, sans-serif; WHITE-SPACE: normal">继续追踪会发现:</SPAN></PRE><PRE class=java name="code"><SPAN style="BACKGROUND-COLOR: rgb(255,255,255); FONT-FAMILY: Arial, Verdana, sans-serif; WHITE-SPACE: normal">PhoneBase.java抽象类实现了接口Phone.java,而GSMPhone.java又实现了抽象类PhoneBase,所以:</SPAN></PRE>
<P></P>
<P>上述代码中:phone.getForegroundCall()实际相当于GSMPhone对象执行了getForegroundCall()方法。</P>
<P>6. 继续追踪GSMPhone.java,该类位于/frameworks/base/telephony/java/com/android/internal/telephony/gsm/下。</P>
<P><PRE class=java name="code">GSMPhone.java:
GsmCallTracker mCT;
public GsmCall getForegroundCall() {
return mCT.foregroundCall;
}</PRE>可以看出getForegroundCall()函数继续调用GsmCallTracker.java的foregroundCall属性。<P></P>
<P>7.GsmCallTracker.java位于/frameworks/base/telephony/java/com/android/internal/telephony/gsm/下.</P>
<P><PRE class=cpp name="code">GsmCallTracker.java:
GSMCall foregroundCall = new GSMCall(this);</PRE><P></P>
<P>打开GSMCall.java,找到getPhone()方法,发现:</P>
<P><PRE class=java name="code">GSMCallTracker owner;
public Phone getPhone() {
return owner.phone;
}</PRE><P></P>
<P>而在GSMCallTracker.java中有如下声明:</P>
<P><PRE class=cpp name="code">GSMPhone phone;</PRE><P></P>
<P><SPAN style="COLOR: #ff0000">到此,我们得出一下结论:第5部分标记红色的代码所返回的就是GSMPhone的对象,</SPAN><SPAN style="COLOR: #3333ff">进一步可以得出,第5部分蓝色标记的代码即是调用了GSMPhone对象的dial方法。</SPAN></P>
<P>8. 在GSMPhone.java中:</P>
<P><PRE class=java name="code">GSMCallTracker mCT;
public Connection dial(String dialString) throws CallStateException {
return dial(dialString, null);
}
public Connection dial(String dialString, UUSInfo uusInfo) throws CallStateException {
.......
mCT.dial(.......);
}</PRE><P></P>
<P>继续调用GSMCallTracker.java中的dial()方法:</P>
<P><PRE class=cpp name="code">GSMCallTracker.java:
GSMCallTracker(GSMPhone phone) {
cm = phone.mCM;
}
Connection dial(String dialString, int clirMode, UUSInfo uusInfo) {
<SPAN style="COLOR: #ff0000">cm.dial(........);</SPAN>
}</PRE>追踪mCM,发现 :<P></P>
<P>public CommandsInterface mCM;</P>
<P>所以GSMCallTracker持有CommandsInterface对象,即RIL.Java类的对象,所以"cm.dial(....)"即是调用RIL类对象的dial()方法。</P>
<P>9. RIL.java</P>
<P>BOSS出现。</P>
<P>RIL对象负责把客户端的通话请求按照一定的格式发送给"rild"socket,至此,请求过程完毕。</P>
</PRE>