Android5.1拨号过程中牵涉到通话连接的建立过程,这个过程十分重要,因为通话所有的操作都是建立在这个连接connection之上,下面就建立连接的过程做出详细分析:
一.Telecomm 层的层的连接管理者--ConnectionServiceWrapper.java
1.这里从建立连接开始分析,我们知道,之前的文章分析过创建连接的入口,这里就不做赘述了,我们从Call.java(packages/services/Telecomm)中的方法startCreateConnection()开始,在这里最后会去调用CreateConnectionProcessor.java类的 attemptNextPhoneAccount()方法:
service.createConnection(mCall, newResponse(service));
这里就会去调用ConnectionServiceWrapper.java(packages/services/Telecomm)类的createConnection方法,好了,事情到了这一步就开始了真正的创建连接的过程,我们进入到ConnectionServiceWrapper.java类中查看createConnection()方法;
2.进入createConnection()方法:
BindCallback callback = new BindCallback(){
@Override
public void onSuccess() {
String callId = mCallIdMapper.getCallId(call);
mPendingResponses.put(callId, response);
GatewayInfo gatewayInfo = call.getGatewayInfo();
Bundle extras = call.getExtras();
if (gatewayInfo != null &&gatewayInfo.getGatewayProviderPackageName() != null &&
gatewayInfo.getOriginalAddress() !=null) {
extras = (Bundle) extras.clone();
extras.putString(
TelecomManager.GATEWAY_PROVIDER_PACKAGE,
gatewayInfo.getGatewayProviderPackageName());
extras.putParcelable(
TelecomManager.GATEWAY_ORIGINAL_ADDRESS,
gatewayInfo.getOriginalAddress());
}
mServiceInterface.createConnection(
call.getConnectionManagerPhoneAccount(),
callId,
newConnectionRequest(
call.getTargetPhoneAccount(),
call.getHandle(),
extras,
call.getVideoState()),
call.isIncoming(),
call.isUnknown());
}
} catch (RemoteException e) {
Log.e(this, e, "Failure to createConnection -- %s",getComponentName());
mPendingResponses.remove(callId).handleCreateConnectionFailure(newDisconnectCause(DisconnectCause.ERROR, e.toString()));
}
}
@Override
public void onFailure() {
Log.e(this, new Exception(), "Failure to call %s",getComponentName());
response.handleCreateConnectionFailure(newDisconnectCause(DisconnectCause.ERROR));
}
};
mBinder.bind(callback);
我们先看这个 BindCallback,这个类是什么,有什么作用呢?这个就要先看ConnectionServiceWrapper的定义了,看其定义即知此类继承于ServiceBinder类:
interface BindCallback {
voidonSuccess();
voidonFailure();
}
可见BindCallback是一个接口,定义了两个方法,在这里实现了这个类,这个我们后面要用到,暂时不看;我们直接看最后一行:
mBinder.bind(callback);
这个mBinder是什么实例呢,我们看其声明:
private Binder mBinder = new Binder();
继承自ServiceBinder类,自然就继承了其中的内部类和方法,我们追寻到ServiceBinder类中:
final class Binder {
/**
*Performs an asynchronous bind to the service (only if not already bound) andexecutes the
*specified callback.
* @paramcallback The callback to notify of the binding's success or failure.
*/
voidbind(BindCallback callback) {
ThreadUtil.checkOnMainThread();
Log.d(ServiceBinder.this, "bind()");
// Reset any abort request if we're asked to bind again.
clearAbort();
if (!mCallbacks.isEmpty()) {
// Binding already in progress, append to the list of callbacks and bailout.
mCallbacks.add(callback);
return; }
mCallbacks.add(callback);
if (mServiceConnection == null) {
Intent serviceIntent = newIntent(mServiceAction).setComponent(mComponentName);
ServiceConnection connection = new ServiceBinderConnection();
Log.d(ServiceBinder.this, "Binding to service with intent:%s", serviceIntent);
final boolean binding;
if (mUserHandle != null) {
binding = mContext.bindServiceAsUser(serviceIntent,connection,
Context.BIND_AUTO_CREATE,mUserHandle);
} else {
binding = mContext.bindService(serviceIntent,connection,
Context.BIND_AUTO_CREATE);
}
if (!binding) {
handleFailedConnection();
return; }
} else {
Log.d(ServiceBinder.this, "Service is already bound.");
Preconditions.checkNotNull(mBinder);
handleSuccessfulConnection();
} } }
private final classServiceBinderConnection implements ServiceConnection {
@Override
public voidonServiceConnected(ComponentName componentName, IBinder binder) {
ThreadUtil.checkOnMainThread();
Log.i(this, "Service bound %s", componentName);
// Unbind request was queued so unbind immediately.
if (mIsBindingAborted) {
clearAbort();
logServiceDisconnected("onServiceConnected");
mContext.unbindService(this);
handleFailedConnection();
return; }
mServiceConnection = this;
setBinder(binder);
handleSuccessfulConnection();
}
@Override
public voidonServiceDisconnected(ComponentName componentName) {
logServiceDisconnected("onServiceDisconnected");
mServiceConnection = null;
clearAbort();
handleServiceDisconnected();
}}
这个类的定义在这里,先看到mCallbacks.add(callback)就明白了为什么要在ConnectionServiceWrapper的 createConnection方法中要去实例化 BindCallback了然后再使用 mBinder.bind(callback)将其当成参数传到bind方法中,原来是回调,好的,继续,我们看到其中的bindServiceAsUser这个方法我们就知道又是一个用到binder机制的结构了,那么这个connection是如何建立的呢,下面我们就此再展开分析。