android 6.0的TELECOM_SERVICE的由来

起因

android 5.0之前是,Phone进程对外的服务都是通过TelephonyManager来实现的。现在多出来了个TelecomManager来给应用调用电话相关的功能。所以要看看,这个TelecomManager是何方神圣。

使用举例

  • Dialer中,拨打电话的时候,最后会调用TelecomManager的placeCall函数。
    final TelecomManager tm = (TelecomManager)context.getSystemService(Context.TELECOM_SERVICE);
    tm.placeCall(intent.getData(), intent.getExtras());
    发现是通过getSystemService来获得的。所以看一下是怎么注册到Context里面去的,是在frameworks/base/core/java/android/app/SystemServiceRegistry.java里面:
    registerService(Context.TELECOM_SERVICE, TelecomManager.class,
    new CachedServiceFetcher() {
    @Override
    public TelecomManager createService(ContextImpl ctx) {
    return new TelecomManager(ctx.getOuterContext());
    }});
    这样,应用通过getSystemService接口就可以方便使用TelecomManager的实例了。

  • 回到placeCall这个函数:
    public void placeCall(Uri address, Bundle extras) {
    ITelecomService service = getTelecomService();
    if (service != null) {
    if (address == null) {
    Log.w(TAG, "Cannot place call to empty address.");
    }
    try {
    service.placeCall(address, extras == null ? new Bundle() : extras,
    mContext.getOpPackageName());
    } catch (RemoteException e) {
    Log.e(TAG, "Error calling ITelecomService#placeCall", e);
    }
    }
    }
    发现是调用ITelecomService这个service
    private ITelecomService getTelecomService() {
    return ITelecomService.Stub.asInterface(ServiceManager.getService(Context.TELECOM_SERVICE));
    }
    又是通过Context.TELECOM_SERVICE,发现和之前Dialer用统一个关键词来获得service,但是注意,这个两个不同的方式获得,所以对应的service也不一样。
    后面这个是通过ServiceManager来找到系统的服务,然后转成对应的服务端引用。说明必然有个地方把这个
    ITelecomService加入到ServiceManager中。所以,搜索代码:
    在frameworks/base/services/core/java/com/android/server/telecom/TelecomLoaderService.java的内部类TelecomServiceConnection的onServiceConnected中添加
    private class TelecomServiceConnection implements ServiceConnection {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    // Normally, we would listen for death here, but since telecom runs in the same process
    // as this loader (process="system") thats redundant here.
    try {
    service.linkToDeath(new IBinder.DeathRecipient() {
    @Override
    public void binderDied() {
    connectToTelecom();
    }
    }, 0);
    SmsApplication.getDefaultMmsApplication(mContext, false);
    ServiceManager.addService(Context.TELECOM_SERVICE, service);
    ....
    即,通过bindService连接了某个service,然后把通过binder返回的service添加到ServiceManager中。
    在TelecomLoaderService中可以看到,是绑定了TelecomService(packages/services/Telecomm/src/com/android/server/telecom/components/TelecomService.java),看TelecomService的onBind函数
    @Override
    public IBinder onBind(Intent intent) {
    Log.d(this, "onBind");
    initializeTelecomSystem(this);
    synchronized (getTelecomSystem().getLock()) {
    return getTelecomSystem().getTelecomServiceImpl().getBinder();
    }
    }
    当bindService的命令来的时候,做了两件事,一个是初始化TelecomSystem,一个是返回TelecomServiceImpl中的binder实例(实现了ITelecomService的stub端)。

  1. initializeTelecomSystem里面生成了TelecomSystem对象,在TelecomSystem的构造函数里面,创建了CallsManager/TelecomServiceImpl/CallIntentProcessor/PhoneAccountRegistrar/MissedCallNotifier等重要的telecom实例。
  2. getTelecomSystem().getTelecomServiceImpl().getBinder():实际就是得到了packages/services/Telecomm/src/com/android/server/telecom/TelecomServiceImpl.java中的mBinderImpl
    private final ITelecomService.Stub mBinderImpl = new ITelecomService.Stub() {
    @Override
    public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme,
    String callingPackage) {
    synchronized (mLock) {
    if (!canReadPhoneState(callingPackage, "getDefaultOutgoingPhoneAccount")) {
    return null;
    }
    ....
  • 即,最后TelecomManager调用TelecomServiceImpl的placeCall函数完成打电话操作。
    /**
    * @see android.telecom.TelecomManager#placeCall
    */
    @Override
    public void placeCall(Uri handle, Bundle extras, String callingPackage) {
    enforceCallingPackage(callingPackage);
    if (!canCallPhone(callingPackage, "placeCall")) {
    throw new SecurityException("Package " + callingPackage
    + " is not allowed to place phone calls");
    }

          // Note: we can still get here for the default/system dialer, even if the Phone
          // permission is turned off. This is because the default/system dialer is always
          // allowed to attempt to place a call (regardless of permission state), in case
          // it turns out to be an emergency call. If the permission is denied and the
          // call is being made to a non-emergency number, the call will be denied later on
          // by {@link UserCallIntentProcessor}.
    
          final boolean hasCallAppOp = mAppOpsManager.noteOp(AppOpsManager.OP_CALL_PHONE,
                  Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
    
          final boolean hasCallPermission = mContext.checkCallingPermission(CALL_PHONE) ==
                  PackageManager.PERMISSION_GRANTED;
    
          synchronized (mLock) {
              final UserHandle userHandle = Binder.getCallingUserHandle();
              long token = Binder.clearCallingIdentity();
              try {
                  final Intent intent = new Intent(Intent.ACTION_CALL, handle);
                  intent.putExtras(extras);
                  new UserCallIntentProcessor(mContext, userHandle).processIntent(intent,
                          callingPackage, hasCallAppOp && hasCallPermission);
              } finally {
                  Binder.restoreCallingIdentity(token);
              }
          }
      }
    

问题:什么时候完成发生的启动TelecomService的?

回答:系统启动时SystemServer(frameworks/base/services/java/com/android/server/SystemServer.java)会启动TelecomLoaderService。
mSystemServiceManager.startService(TelecomLoaderService.class);
当系统回调onBootPhase的时候,就会调用connectToTelecom函数去启动TelecomService。
@Override
public void onBootPhase(int phase) {
if (phase == PHASE_ACTIVITY_MANAGER_READY) {
registerDefaultAppNotifier();
registerCarrierConfigChangedReceiver();
connectToTelecom();
}
}

感受:

Google为了弄个用户用的TelecomManager居然搞了这么大的一圈。其实,这就是分层的设计理念。不同层次的调用对应不同的方法。
另外,通过分析也知道了CallsManager等相关函数的创建点。总之,收获满满。

你可能感兴趣的:(android 6.0的TELECOM_SERVICE的由来)