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; } } } }
服务绑定:
将服务添加到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 RIL使用详解