Android 系統(tǒng)服務TelecomService啟動過程原理分析
由于一直負責的是Android Telephony部分的開發(fā)工作,對于通信過程的上層部分Telecom服務以及UI都沒有認真研究過。最近恰好碰到一個通話方面的問題,涉及到了Telecom部分,因而就花時間仔細研究了下相關的代碼。這里做一個簡單的總結。這篇文章,主要以下兩個部分的內(nèi)容:
- 什么是Telecom服務?其作用是什么?
- Telecom模塊的啟動與初始化過程;
接下來一篇文章,主要以實際通話過程為例,分析下telephony收到來電后如何將電話信息發(fā)送到Telecom模塊以及Telecom是如何處理來電。
什么是Telecom服務
Telecom是Android的一個系統(tǒng)服務,其主要作用是管理Android系統(tǒng)當前的通話,如來電顯示,接聽電話,掛斷電話等功能,在Telephony模塊與上層UI之間起到了一個橋梁的作用。比如,Telephony有接收到新的來電時,首先會告知Telecom,然后由Telecom服務通知上層應用來電信息,并顯示來電界面。
Telecom服務對外提供了一個接口類TelecomManager,通過其提供的接口,客戶端可以查詢通話狀態(tài),發(fā)送通話請求以及添加通話鏈接等。
從Telecom進程對應的AndroidManifest.xml文件來看,Telecom進程的用戶ID跟系統(tǒng)進程用戶ID相同,是系統(tǒng)的核心服務。那么,其中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進程初始化完成啟動完系統(tǒng)的核心服務如ActivityManagerService后,就會加載系統(tǒng)其它服務,這其中就包含了一個與Telecom服務啟動相關的系統(tǒng)服務專門用于加載Telecom:
private void startOtherServices() {
....
//啟動TelecomLoaderService系統(tǒng)服務,用于加載Telecom
mSystemServiceManager.startService(TelecomLoaderService.class);
// 啟動telephony注冊服務,用于注冊監(jiān)聽telephony狀態(tài)的接口
telephonyRegistry = new TelephonyRegistry(context);
ServiceManager.addService("telephony.registry", telephonyRegistry);
}
調(diào)用系統(tǒng)服務管家SystemServiceManager的接口startService創(chuàng)建新的服務,并注冊到系統(tǒng)中,最后調(diào)用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文件來創(chuàng)建新的服務對象(服務必須繼承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);
}
}
}
創(chuàng)建TelecomLoaderService系統(tǒng)服務,將系統(tǒng)默認的SMS應用,撥號應用以及SIM通話管理應用(不知道這個什么鬼)告知PackageManagerService(PMS),以便在適當?shù)臅r候可以找到應用。
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的函數(shù),用于SystemServer告知系統(tǒng)服務目前系統(tǒng)啟動所處的階段。這里可以看到,等(ActivityManagerService)AMS啟動完成以后,就可以開始連接Telecom服務了:
- 首先,注冊默認應用(SMS/Dialer etc)通知對象,以便這些應用發(fā)送變更(如下載了一個第三方的SMS應用時,可以通知系統(tǒng)這一變化);
- 接著,注冊運營商配置變化的廣播接收器,如果配置有變化時,系統(tǒng)會收到通知;
- 綁定TelecomService,并將其注冊到系統(tǒng)中。
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";
// 當前系統(tǒng)啟動的階段
@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();
}
}
}
綁定服務時,調(diào)用TelecomService的onBind接口,對整個Telecom系統(tǒng)進行初始化,并返回一個IBinder接口:
/**
* Implementation of the ITelecom interface.
*/
public class TelecomService extends Service implements TelecomSystem.Component {
@Override
public IBinder onBind(Intent intent) {
// 初始化整個Telecom系統(tǒng)
initializeTelecomSystem(this);
//返回IBinder接口
synchronized (getTelecomSystem().getLock()) {
return getTelecomSystem().getTelecomServiceImpl().getBinder();
}
}
}
Telecom系統(tǒng)初始化,主要工作是新建一個TelecomSystem的類,在這個類中,會對整個Telecom服務的相關類都初始化:
static void initializeTelecomSystem(Context context) {
if (TelecomSystem.getInstance() == null) {
final NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// 用于獲取聯(lián)系人
contactInfoHelper = new ContactInfoHelper(context);
// 新建一個單例模式的對象
TelecomSystem.setInstance(new TelecomSystem(....));
}
....
}
}
構造一個單例TelecomSystem對象:
public TelecomSystem(
Context context,
/* 用戶未接來電通知類(不包括已接或者拒絕的電話) */
MissedCallNotifierImplFactory missedCallNotifierImplFactory,
/* 查詢來電信息 */
CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
/* 耳機接入狀態(tài)監(jiān)聽 */
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的數(shù)據(jù)庫
TelecomSystemDB.initialize(mContext);
// 創(chuàng)建一個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);
// 創(chuàng)建一個TelecomServiceImpl用于調(diào)用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);
// 執(zhí)行特定的初始化操作
initialize(mContext);
}
}
Android Telephony中的PhoneAccount到底起到個什么作用了?按照源碼中的說明來理解,PhoneAccount表示了不同的接聽或者撥打電話的方式,比如用戶可以通過SIM卡來撥打電話,也可以撥打視頻電話,抑或一個緊急通話,甚至可以通過telephony內(nèi)部的接口來實現(xiàn)撥號,而Android正是通過PhoneAccount來區(qū)分這幾種通話方式的。與之相對應的一個類PhoneAccountHandle則是用于表示哪一個用戶正在使用通話服務。
至此整個Telecom服務就啟動完成了,這樣Telecom服務就可以處理來電或者去電了。在接下來的一篇文章里,將分析下來電是如何在Telecom中傳遞與處理,然后發(fā)送到上層UI界面的。
相關文章
Android 模擬新聞APP顯示界面滑動優(yōu)化實例代碼
所謂滑動優(yōu)化就是滑動時不加載圖片,停止才加載,第一次進入時手動加載。下面通過本文給大家介紹android 模擬新聞app顯示界面滑動優(yōu)化實例代碼,需要的朋友可以參考下2017-03-03
解決Android BitmapFactory的基本使用問題
很多朋友給小編反饋使用方法BitmapFactory.decodeFile轉化Bitmap時報錯,究竟是什么原因?qū)е洛e誤問題呢?今天通過本文給大家介紹下解決Android BitmapFactory的基本使用問題,感興趣的朋友一起看看吧2021-10-10
ubuntu下 AndroidStudio4.1啟動報錯問題的解決
這篇文章主要介紹了ubuntu下 AndroidStudio4.1啟動報錯問題的解決,本文給大家分享個人經(jīng)驗對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
詳解用RxJava實現(xiàn)事件總線(Event Bus)
本篇文章主要介紹了用RxJava實現(xiàn)事件總線(Event Bus),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Android實現(xiàn)文件上傳和下載倒計時功能的圓形進度條
這篇文章主要介紹了Android實現(xiàn)文件上傳和下載倒計時功能的圓形進度條,需要的朋友可以參考下2017-09-09
Android實現(xiàn)recyclerview城市字母索引列表
大家好,本篇文章主要講的是Android實現(xiàn)recyclerview城市字母索引列表,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
Android Studio 3.6中使用視圖綁定替代 findViewById的方法
從 Android Studio 3.6 開始,視圖綁定能夠通過生成綁定對象來替代 findViewById,從而可以幫您簡化代碼、移除 bug,并且從 findViewById 的模版代碼中解脫出來,今天通過本文給大家介紹使用視圖綁定替代 findViewById的方法,感興趣的朋友一起看看吧2020-03-03

