2.4 services Telephony
ConnectionService的createConnection调用流程图如下,
方法代码如下,
1,首先根据电话的状态创建一个连接,
Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request): isIncoming ? onCreateIncomingConnection(callManagerAccount, request): onCreateOutgoingConnection(callManagerAccount, request);
2,完成之后,回调ConnectionServiceAdapter的handleCreateConnectionComplete方法,
mAdapter.handleCreateConnectionComplete(callId, request,•••
其实,步骤2去电流程也会调用。
TelephonyConnectionService的onCreateIncomingConnection方法如下,
Connection connection = createConnectionFor(phone, originalConnection, false /* isOutgoing */,
request.getAccountHandle());
if (connection == null) {
return Connection.createCanceledConnection();
} else {
return connection;
}
调用createConnectionFor构造一个连接,然后返回该连接。connection是指GsmConnection对象。
ConnectionServiceAdapter的handleCreateConnectionComplete方法如下,
void handleCreateConnectionComplete(String id, ConnectionRequest request,
ParcelableConnection connection) {
for (IConnectionServiceAdapter adapter : mAdapters) {
try {
adapter.handleCreateConnectionComplete(id, request, connection);
} catch (RemoteException e) {
}
}
}
此处的IconnectionServiceAdapter对象是binder,指向ConnectionServiceWrapper的内部类Adapter, Adapter定义如下,
private final class Adapter extends IConnectionServiceAdapter.Stub {
Adapter的handleCreateConnectionComplete方法如下,
ConnectionServiceWrapper.this.handleCreateConnectionComplete(callId, request, connection);
直接调用ConnectionServiceWrapper的handleCreateConnectionComplete方法。
绕了这么一大圈,成功创建GsmConnection连接之后又回到了services telecom。
2.5 services telecom
ConnectionServiceWrapper的handleCreateConnectionComplete方法调用流程图如下,
ConnectionServiceWrapper的handleCreateConnectionSuccess方法如下,
if (mPendingResponses.containsKey(callId)) {
mPendingResponses.remove(callId).handleCreateConnectionSuccess(mCallIdMapper, connection);
}
调用CreateConnectionProcessor的handleCreateConnectionSuccess方法,
mResponse.handleCreateConnectionSuccess(idMapper, connection);
mResponse = null;
调用Call的handleCreateConnectionSuccess方法,该方法的逻辑如下,
if (mIsUnknown) {
for (Listener l : mListeners) {
l.onSuccessfulUnknownCall(this, getStateFromConnectionState(connection.getState()));
}
} else if (mIsIncoming) {
mDirectToVoicemailQueryPending = true;
mHandler.postDelayed(mDirectToVoicemailRunnable, Timeouts.getDirectToVoicemailMillis(
mContext.getContentResolver()));
} else {
for (Listener l : mListeners) {
l.onSuccessfulOutgoingCall(this, getStateFromConnectionState(connection.getState()));
}
}
如果是未知call,回调监听器的onSuccessfulUnknownCall方法;
如果是来电,回调监听器的onSuccessfulIncomingCall方法;
如果是去电,回调监听器的onSuccessfulOutgoingCall方法;
CallsManager的addCall方法如下,
private void addCall(Call call) {
Trace.beginSection("addCall");
Log.v(this, "addCall(%s)", call);
call.addListener(this);
mCalls.add(call);
// TODO: Update mForegroundCall prior to invoking
// onCallAdded for calls which immediately take the foreground (like the first call).
for (CallsManagerListener listener : mListeners) {
if (Log.SYSTRACE_DEBUG) {
Trace.beginSection(listener.getClass().toString() + " addCall");
}
listener.onCallAdded(call);//调用监听器的onCallAdded方法
if (Log.SYSTRACE_DEBUG) {
Trace.endSection();
}
}
updateCallsManagerState();//更新状态
Trace.endSection();
}
来电之后,一般会做2件事情,
1,铃声响起
2,启动来电界面。
调用Ringer的onCallAdded方法响铃;
调用InCallController的onCallAdded方法启动来电界面。
这2两个部分在后面的章节详细论述。
小结:
1,RIL和opt telephony phone进程利用通知机制进行通信,当电话的状态发生变化时,会查询当前的call。
2, servicesTelephony 到 services telecom通过binder服务进行通信。
3,然后services telecom 和 services Telephony之间建立一个通话连接。
4,最后services telecom通过监听回调的方式响铃和启动来电界面。