首页 > 编程语言 > Android 系统服务TelecomService启动过程原理分析
2020
10-08

Android 系统服务TelecomService启动过程原理分析

由于一直负责的是Android Telephony部分的开发工作,对于通信过程的上层部分Telecom服务以及UI都没有认真研究过。最近恰好碰到一个通话方面的问题,涉及到了Telecom部分,因而就花时间仔细研究了下相关的代码。这里做一个简单的总结。这篇文章,主要以下两个部分的内容:

  • 什么是Telecom服务?其作用是什么?
  • Telecom模块的启动与初始化过程;

接下来一篇文章,主要以实际通话过程为例,分析下telephony收到来电后如何将电话信息发送到Telecom模块以及Telecom是如何处理来电。

什么是Telecom服务

Telecom是Android的一个系统服务,其主要作用是管理Android系统当前的通话,如来电显示,接听电话,挂断电话等功能,在Telephony模块与上层UI之间起到了一个桥梁的作用。比如,Telephony有接收到新的来电时,首先会告知Telecom,然后由Telecom服务通知上层应用来电信息,并显示来电界面。

Telecom服务对外提供了一个接口类TelecomManager,通过其提供的接口,客户端可以查询通话状态,发送通话请求以及添加通话链接等。

从Telecom进程对应的AndroidManifest.xml文件来看,Telecom进程的用户ID跟系统进程用户ID相同,是系统的核心服务。那么,其中android:process="system"这个属性值表示什么意思了?查看官方文档,这个表示Telecom将启动在进程system中,这样可以跟其他进程进行资源共享了(对于Android这个全局进程,就是SystemServer所在的进程)。

android:process

By setting this attribute to a process name that's shared with another application, you can arrange for components of both applications to run in the same process — but only if the two applications also share a user ID and be signed with the same certificate.

If the name assigned to this attribute begins with a colon (‘:'), a new process, private to the application, is created when it's needed. If the process name begins with a lowercase character, a global process of that name is created. A global process can be shared with other applications, reducing resource usage.

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
   package="com.android.server.telecom"
   android:versionCode="1"
   android:versionName="1.0.0"
   coreApp="true"
   android:sharedUserId="android.uid.system">

   <application android:label="@string/telecommAppLabel"
    android:icon="@mipmap/ic_launcher_phone"
    android:allowBackup="false"
    android:supportsRtl="true"
    android:process="system"
    android:usesCleartextTraffic="false"
    android:defaultToDeviceProtectedStorage="true"
    android:directBootAware="true">
   ....
   // 包含TelecomService
   <service android:name=".components.TelecomService"
     android:singleUser="true"
     android:process="system">
    <intent-filter>
     <action android:name="android.telecom.ITelecomService" />
    </intent-filter>
   </service>
  ....
  </application>
 </manifest>

代码路径:

/android/applications/sources/services/Telecomm/
/android/frameworks/base/telecomm/

了解了什么是Telecom服务之后,就来看一看Telecom服务是如何启动与初始化的。

Telecom进程的启动与初始化

在SystemServer进程初始化完成启动完系统的核心服务如ActivityManagerService后,就会加载系统其它服务,这其中就包含了一个与Telecom服务启动相关的系统服务专门用于加载Telecom:

 private void startOtherServices() {
  ....
  //启动TelecomLoaderService系统服务,用于加载Telecom
  mSystemServiceManager.startService(TelecomLoaderService.class);
  // 启动telephony注册服务,用于注册监听telephony状态的接口
  telephonyRegistry = new TelephonyRegistry(context);
  ServiceManager.addService("telephony.registry", telephonyRegistry);
 }

调用系统服务管家SystemServiceManager的接口startService创建新的服务,并注册到系统中,最后调用onStart()启动服务。

 public class SystemServiceManager {

  @SuppressWarnings("unchecked")
  public SystemService startService(String className) {
   final Class<SystemService> serviceClass;
   try {
    serviceClass = (Class<SystemService>)Class.forName(className);
   } catch (ClassNotFoundException ex) {
    ....
   }
   return startService(serviceClass);
  }

  // 服务的class文件来创建新的服务对象(服务必须继承SystemService)
  @SuppressWarnings("unchecked")
  public <T extends SystemService> T startService(Class<T> serviceClass) {
   try {
    final String name = serviceClass.getName();
    Slog.i(TAG, "Starting " + name);
    Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);

    // Create the service.
    if (!SystemService.class.isAssignableFrom(serviceClass)) {
     throw new RuntimeException("Failed to create " + name
       + ": service must extend " + SystemService.class.getName());
    }
    final T service;
    try {
     Constructor<T> constructor = serviceClass.getConstructor(Context.class);
     service = constructor.newInstance(mContext);
    } catch (InstantiationException ex) {
     throw new RuntimeException("Failed to create service " + name
       + ": service could not be instantiated", ex);
    } 
    ....
    // Register it.
    mServices.add(service);

    // Start it.
    try {
     service.onStart();
    } catch (RuntimeException ex) {
     throw new RuntimeException("Failed to start service " + name
       + ": onStart threw an exception", ex);
    }
    return service;
   } finally {
    Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
   }
  }
 }

创建TelecomLoaderService系统服务,将系统默认的SMS应用,拨号应用以及SIM通话管理应用(不知道这个什么鬼)告知PackageManagerService(PMS),以便在适当的时候可以找到应用。

 public class TelecomLoaderService extends SystemService {
  ...
  public TelecomLoaderService(Context context) {
   super(context);
   mContext = context;
   registerDefaultAppProviders();
  }

  @Override
  public void onStart() {
  }

  private void registerDefaultAppProviders() {
   final PackageManagerInternal packageManagerInternal = LocalServices.getService(
     PackageManagerInternal.class);

   // Set a callback for the package manager to query the default sms app.
   packageManagerInternal.setSmsAppPackagesProvider(
     new PackageManagerInternal.PackagesProvider() {
    @Override
    public String[] getPackages(int userId) {
     synchronized (mLock) {
     ....
     ComponentName smsComponent = SmsApplication.getDefaultSmsApplication(
       mContext, true);
     if (smsComponent != null) {
      return new String[]{smsComponent.getPackageName()};
     }
     return null;
    }
   });

   // Set a callback for the package manager to query the default dialer app.
   packageManagerInternal.setDialerAppPackagesProvider(
     new PackageManagerInternal.PackagesProvider() {
    @Override
    public String[] getPackages(int userId) {
     synchronized (mLock) {
     ....
     String packageName = DefaultDialerManager.getDefaultDialerApplication(mContext);
     if (packageName != null) {
      return new String[]{packageName};
     }
     return null;
    }
   });

   // Set a callback for the package manager to query the default sim call manager.
   packageManagerInternal.setSimCallManagerPackagesProvider(
     new PackageManagerInternal.PackagesProvider() {
    @Override
    public String[] getPackages(int userId) {
     synchronized (mLock) {
     ....
     TelecomManager telecomManager =
      (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
     PhoneAccountHandle phoneAccount = telecomManager.getSimCallManager(userId);
     if (phoneAccount != null) {
      return new String[]{phoneAccount.getComponentName().getPackageName()};
     }
     return null;
    }
   });
  }
 }

到目前,好像Telecom服务并没启动,那么究竟Telecom服务在哪里启动的了?仔细看TelecomLoaderService的源代码,其中有一个onBootPhase的函数,用于SystemServer告知系统服务目前系统启动所处的阶段。这里可以看到,等(ActivityManagerService)AMS启动完成以后,就可以开始连接Telecom服务了:

  • 首先,注册默认应用(SMS/Dialer etc)通知对象,以便这些应用发送变更(如下载了一个第三方的SMS应用时,可以通知系统这一变化);
  • 接着,注册运营商配置变化的广播接收器,如果配置有变化时,系统会收到通知;
  • 绑定TelecomService,并将其注册到系统中。
 public class TelecomLoaderService extends SystemService {

  private static final ComponentName SERVICE_COMPONENT = new ComponentName(
    "com.android.server.telecom",
    "com.android.server.telecom.components.TelecomService");

  private static final String SERVICE_ACTION = "com.android.ITelecomService";

  // 当前系统启动的阶段
  @Override
  public void onBootPhase(int phase) {
   if (phase == PHASE_ACTIVITY_MANAGER_READY) {
    registerDefaultAppNotifier();
    registerCarrierConfigChangedReceiver();
    connectToTelecom();
   }
  }

  //绑定Telecom服务
  private void connectToTelecom() {
   synchronized (mLock) {
    if (mServiceConnection != null) {
     // TODO: Is unbinding worth doing or wait for system to rebind?
     mContext.unbindService(mServiceConnection);
     mServiceConnection = null;
    }

    TelecomServiceConnection serviceConnection = new TelecomServiceConnection();
    Intent intent = new Intent(SERVICE_ACTION);
    intent.setComponent(SERVICE_COMPONENT);
    int flags = Context.BIND_IMPORTANT | Context.BIND_FOREGROUND_SERVICE
      | Context.BIND_AUTO_CREATE;

    // Bind to Telecom and register the service
    if (mContext.bindServiceAsUser(intent, serviceConnection, flags, UserHandle.SYSTEM)) {
     mServiceConnection = serviceConnection;
    }
   }
  }
 }

服务绑定:https://developer.android.com/guide/components/bound-services.html

将服务添加到ServiceManager中,如果Telecom服务连接中断时,则重新连接:

 public class TelecomLoaderService extends SystemService {

  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);
      //添加Telecom服务
      ServiceManager.addService(Context.TELECOM_SERVICE, service);
      ....
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
     connectToTelecom();
    }
   }
 }

绑定服务时,调用TelecomService的onBind接口,对整个Telecom系统进行初始化,并返回一个IBinder接口:

 /**
  * Implementation of the ITelecom interface.
  */
 public class TelecomService extends Service implements TelecomSystem.Component {

  @Override
  public IBinder onBind(Intent intent) {
   // 初始化整个Telecom系统
   initializeTelecomSystem(this);
   //返回IBinder接口
   synchronized (getTelecomSystem().getLock()) {
    return getTelecomSystem().getTelecomServiceImpl().getBinder();
   }
  }

 }

Telecom系统初始化,主要工作是新建一个TelecomSystem的类,在这个类中,会对整个Telecom服务的相关类都初始化:

 static void initializeTelecomSystem(Context context) {
   if (TelecomSystem.getInstance() == null) {

    final NotificationManager notificationManager =
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // 用于获取联系人
    contactInfoHelper = new ContactInfoHelper(context);
    // 新建一个单例模式的对象
    TelecomSystem.setInstance(new TelecomSystem(....));
   }
   ....
  }
 }

构造一个单例TelecomSystem对象:

 public TelecomSystem(
    Context context,
    /* 用户未接来电通知类(不包括已接或者拒绝的电话) */
    MissedCallNotifierImplFactory missedCallNotifierImplFactory,
    /* 查询来电信息 */
    CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
    /* 耳机接入状态监听 */
    HeadsetMediaButtonFactory headsetMediaButtonFactory,
    /* 距离传感器管理 */
    ProximitySensorManagerFactory proximitySensorManagerFactory,
    /* 通话时电话管理 */
    InCallWakeLockControllerFactory inCallWakeLockControllerFactory,
    /* 音频服务管理 */
    AudioServiceFactory audioServiceFactory,
    /* 蓝牙设备管理 */
    BluetoothPhoneServiceImplFactory bluetoothPhoneServiceImplFactory,
    BluetoothVoIPServiceImplFactory bluetoothVoIPServiceImplFactory,
    /* 查询所有超时信息 */
    Timeouts.Adapter timeoutsAdapter,
    /* 响铃播放 */
    AsyncRingtonePlayer asyncRingtonePlayer,
    /* 电话号码帮助类 */
    PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
    /* 通话时阻断通知 */
    InterruptionFilterProxy interruptionFilterProxy) {
   mContext = context.getApplicationContext();
   // 初始化telecom相关的feature
   TelecomFeature.makeFeature(mContext);
   // 初始化telecom的数据库
   TelecomSystemDB.initialize(mContext);
   // 创建一个PhoneAccount注册管理类
   mPhoneAccountRegistrar = new PhoneAccountRegistrar(mContext);
   ....
   // 初始化通话管家,正是它负责与上层UI的交互
   mCallsManager = new CallsManager(
     mContext, mLock, mContactsAsyncHelper,
     callerInfoAsyncQueryFactory, mMissedCallNotifier,
     mPhoneAccountRegistrar, headsetMediaButtonFactory,
     proximitySensorManagerFactory, inCallWakeLockControllerFactory,
     audioServiceFactory, bluetoothManager,
     wiredHeadsetManager, systemStateProvider,
     defaultDialerAdapter, timeoutsAdapter,AsyncRingtonePlayer,
     phoneNumberUtilsAdapter, interruptionFilterProxy);

   CallsManager.initialize(mCallsManager);
   // 注册需要接收的广播 
   mContext.registerReceiver(mUserSwitchedReceiver, USER_SWITCHED_FILTER);
   mContext.registerReceiver(mUserStartingReceiver, USER_STARTING_FILTER);
   mContext.registerReceiver(mFeatureChangedReceiver, FEATURE_CHANGED_FILTER);
   mContext.registerReceiver(mEmergencyReceiver, EMERGENCY_STATE_CHANGED);
   .... 
   // 所有来电与去电的处理中转站
   mCallIntentProcessor = new CallIntentProcessor(mContext, mCallsManager);
   // 创建一个TelecomServiceImpl用于调用TelecomService的接口
   mTelecomServiceImpl = new TelecomServiceImpl(
     mContext, mCallsManager, mPhoneAccountRegistrar,
     new CallIntentProcessor.AdapterImpl(),
     new UserCallIntentProcessorFactory() {
      @Override
      public UserCallIntentProcessor create(Context context, UserHandle userHandle) {
       return new UserCallIntentProcessor(context, userHandle);
      }
     },
     defaultDialerAdapter,
     new TelecomServiceImpl.SubscriptionManagerAdapterImpl(),
     mLock);
   // 执行特定的初始化操作
   initialize(mContext);
  }
 }

Android Telephony中的PhoneAccount到底起到个什么作用了?按照源码中的说明来理解,PhoneAccount表示了不同的接听或者拨打电话的方式,比如用户可以通过SIM卡来拨打电话,也可以拨打视频电话,抑或一个紧急通话,甚至可以通过telephony内部的接口来实现拨号,而Android正是通过PhoneAccount来区分这几种通话方式的。与之相对应的一个类PhoneAccountHandle则是用于表示哪一个用户正在使用通话服务。

至此整个Telecom服务就启动完成了,这样Telecom服务就可以处理来电或者去电了。在接下来的一篇文章里,将分析下来电是如何在Telecom中传递与处理,然后发送到上层UI界面的。

到此这篇关于Android 系统服务TelecomService启动过程原理分析的文章就介绍到这了,更多相关Android 系统服务TelecomService启动内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧