欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 系統(tǒng)服務(wù)TelecomService啟動過程原理分析

 更新時間:2020年07月22日 11:32:28   作者:Jason_Wang  
這篇文章主要介紹了Android 系統(tǒng)服務(wù)TelecomService啟動過程原理分析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

由于一直負責(zé)的是Android Telephony部分的開發(fā)工作,對于通信過程的上層部分Telecom服務(wù)以及UI都沒有認(rèn)真研究過。最近恰好碰到一個通話方面的問題,涉及到了Telecom部分,因而就花時間仔細研究了下相關(guān)的代碼。這里做一個簡單的總結(jié)。這篇文章,主要以下兩個部分的內(nèi)容:

  • 什么是Telecom服務(wù)?其作用是什么?
  • Telecom模塊的啟動與初始化過程;

接下來一篇文章,主要以實際通話過程為例,分析下telephony收到來電后如何將電話信息發(fā)送到Telecom模塊以及Telecom是如何處理來電。

什么是Telecom服務(wù)

Telecom是Android的一個系統(tǒng)服務(wù),其主要作用是管理Android系統(tǒng)當(dāng)前的通話,如來電顯示,接聽電話,掛斷電話等功能,在Telephony模塊與上層UI之間起到了一個橋梁的作用。比如,Telephony有接收到新的來電時,首先會告知Telecom,然后由Telecom服務(wù)通知上層應(yīng)用來電信息,并顯示來電界面。

Telecom服務(wù)對外提供了一個接口類TelecomManager,通過其提供的接口,客戶端可以查詢通話狀態(tài),發(fā)送通話請求以及添加通話鏈接等。

從Telecom進程對應(yīng)的AndroidManifest.xml文件來看,Telecom進程的用戶ID跟系統(tǒng)進程用戶ID相同,是系統(tǒng)的核心服務(wù)。那么,其中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服務(wù)之后,就來看一看Telecom服務(wù)是如何啟動與初始化的。

Telecom進程的啟動與初始化

在SystemServer進程初始化完成啟動完系統(tǒng)的核心服務(wù)如ActivityManagerService后,就會加載系統(tǒng)其它服務(wù),這其中就包含了一個與Telecom服務(wù)啟動相關(guān)的系統(tǒng)服務(wù)專門用于加載Telecom:

 private void startOtherServices() {
  ....
  //啟動TelecomLoaderService系統(tǒng)服務(wù),用于加載Telecom
  mSystemServiceManager.startService(TelecomLoaderService.class);
  // 啟動telephony注冊服務(wù),用于注冊監(jiān)聽telephony狀態(tài)的接口
  telephonyRegistry = new TelephonyRegistry(context);
  ServiceManager.addService("telephony.registry", telephonyRegistry);
 }

調(diào)用系統(tǒng)服務(wù)管家SystemServiceManager的接口startService創(chuàng)建新的服務(wù),并注冊到系統(tǒng)中,最后調(diào)用onStart()啟動服務(wù)。

 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);
  }

  // 服務(wù)的class文件來創(chuàng)建新的服務(wù)對象(服務(wù)必須繼承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)服務(wù),將系統(tǒng)默認(rèn)的SMS應(yīng)用,撥號應(yīng)用以及SIM通話管理應(yīng)用(不知道這個什么鬼)告知PackageManagerService(PMS),以便在適當(dāng)?shù)臅r候可以找到應(yīng)用。

 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服務(wù)并沒啟動,那么究竟Telecom服務(wù)在哪里啟動的了?仔細看TelecomLoaderService的源代碼,其中有一個onBootPhase的函數(shù),用于SystemServer告知系統(tǒng)服務(wù)目前系統(tǒng)啟動所處的階段。這里可以看到,等(ActivityManagerService)AMS啟動完成以后,就可以開始連接Telecom服務(wù)了:

  • 首先,注冊默認(rèn)應(yīng)用(SMS/Dialer etc)通知對象,以便這些應(yīng)用發(fā)送變更(如下載了一個第三方的SMS應(yīng)用時,可以通知系統(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";

  // 當(dāng)前系統(tǒng)啟動的階段
  @Override
  public void onBootPhase(int phase) {
   if (phase == PHASE_ACTIVITY_MANAGER_READY) {
    registerDefaultAppNotifier();
    registerCarrierConfigChangedReceiver();
    connectToTelecom();
   }
  }

  //綁定Telecom服務(wù)
  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;
    }
   }
  }
 }

服務(wù)綁定:https://developer.android.com/guide/components/bound-services.html

將服務(wù)添加到ServiceManager中,如果Telecom服務(wù)連接中斷時,則重新連接:

 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服務(wù)
      ServiceManager.addService(Context.TELECOM_SERVICE, service);
      ....
    }

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

綁定服務(wù)時,調(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服務(wù)的相關(guān)類都初始化:

 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(....));
   }
   ....
  }
 }

構(gòu)造一個單例TelecomSystem對象:

 public TelecomSystem(
    Context context,
    /* 用戶未接來電通知類(不包括已接或者拒絕的電話) */
    MissedCallNotifierImplFactory missedCallNotifierImplFactory,
    /* 查詢來電信息 */
    CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
    /* 耳機接入狀態(tài)監(jiān)聽 */
    HeadsetMediaButtonFactory headsetMediaButtonFactory,
    /* 距離傳感器管理 */
    ProximitySensorManagerFactory proximitySensorManagerFactory,
    /* 通話時電話管理 */
    InCallWakeLockControllerFactory inCallWakeLockControllerFactory,
    /* 音頻服務(wù)管理 */
    AudioServiceFactory audioServiceFactory,
    /* 藍牙設(shè)備管理 */
    BluetoothPhoneServiceImplFactory bluetoothPhoneServiceImplFactory,
    BluetoothVoIPServiceImplFactory bluetoothVoIPServiceImplFactory,
    /* 查詢所有超時信息 */
    Timeouts.Adapter timeoutsAdapter,
    /* 響鈴播放 */
    AsyncRingtonePlayer asyncRingtonePlayer,
    /* 電話號碼幫助類 */
    PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
    /* 通話時阻斷通知 */
    InterruptionFilterProxy interruptionFilterProxy) {
   mContext = context.getApplicationContext();
   // 初始化telecom相關(guān)的feature
   TelecomFeature.makeFeature(mContext);
   // 初始化telecom的數(shù)據(jù)庫
   TelecomSystemDB.initialize(mContext);
   // 創(chuàng)建一個PhoneAccount注冊管理類
   mPhoneAccountRegistrar = new PhoneAccountRegistrar(mContext);
   ....
   // 初始化通話管家,正是它負責(zé)與上層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);
   .... 
   // 所有來電與去電的處理中轉(zhuǎn)站
   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ū)分這幾種通話方式的。與之相對應(yīng)的一個類PhoneAccountHandle則是用于表示哪一個用戶正在使用通話服務(wù)。

至此整個Telecom服務(wù)就啟動完成了,這樣Telecom服務(wù)就可以處理來電或者去電了。在接下來的一篇文章里,將分析下來電是如何在Telecom中傳遞與處理,然后發(fā)送到上層UI界面的。

到此這篇關(guān)于Android 系統(tǒng)服務(wù)TelecomService啟動過程原理分析的文章就介紹到這了,更多相關(guān)Android 系統(tǒng)服務(wù)TelecomService啟動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論