亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Android 系統服務TelecomService啟動過程原理分析

瀏覽:2日期:2022-09-23 11:03:55

由于一直負責的是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啟動內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網! $.get('https://blog.csdn.net/wang2119/article/uvc/58164251');
標簽: Android
相關文章:
主站蜘蛛池模板: 大陆黄色网 | 国产精品久久久久9999高清 | 永久精品免费影院在线观看网站 | 亚洲 欧美 手机 在线观看 | 国产露脸对白91精品 | 欧美a欧美1级 | 免费a视频在线观看 | 新一级毛片国语版 | 在线免费观看亚洲视频 | 色婷婷综合激情 | 日本高清免费毛片久久看 | 黄色一及| 日韩精品观看 | 久久综合精品国产一区二区三区无 | 亚洲黄网在线观看 | 黄色一级毛片免费看 | 国产精品黄页网站在线播放免费 | 韩国一级毛片视频免费观看 | 亚洲另类视频 | 精品美女视频在线观看2023 | 久久综合一本 | 8x8x国产| 日韩中文字幕网 | 国产精品第一区在线观看 | 9966久久精品免费看国产 | 成人污片 | 日韩电影久久久被窝网 | 特黄特色网站 | 黄色特一级片 | 大学生一级毛片免费看真人 | 中文字幕久久综合伊人 | 日本一级淫一片免费 | 欧美日韩黄色片 | 国产日产欧美精品一区二区三区 | 免费黄色一级毛片 | 国产精品无卡无在线播放 | 欧美成人午夜影院 | 成人福利在线免费观看 | 玖玖国产在线观看 | 在线成人精品国产区免费 | 中文字幕在线观看日韩 |