Android TeleComm 初探

  安卓在5.0之后在TeleService和IncallUI之间多加了一层TeleComm,个人感觉是不想让IncallUI和Phone进程联系过于紧密,也便于其他通话方式的添加(不仅仅局限于通过电信运营商拨打传统电话),好了话不多说,开始分析,本文基于安卓7.1分析.

1.首先SystemServer在startOtherServices函数里会拉起TelecomLoaderService

mSystemServiceManager.startService(TelecomLoaderService.class);

2.然后TelecomLoaderService回去连接TelecomService

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


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

  此处要注意,绑定服务后TelecomLoaderService会把返回的binder对象进行ServiceManager.addService的操作

ServiceManager.addService(Context.TELECOM_SERVICE, service);

  所以我们通过context.getSystemService()获取到的TelecomManager对象实质调用的是该处绑定的服务在onBind中返回的Binder对象,该服务的类名为:com.android.server.telecom.components.TelecomService.

3.进入TelecomService的onBind方法

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(this, "onBind");
        initializeTelecomSystem(this);
        synchronized (getTelecomSystem().getLock()) {
            return getTelecomSystem().getTelecomServiceImpl().getBinder();
        }
    }
static void initializeTelecomSystem(Context context) {
        if (TelecomSystem.getInstance() == null) {
            final NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            TelecomSystem.setInstance(
                    new TelecomSystem(
                            context,
                            new MissedCallNotifierImpl.MissedCallNotifierImplFactory() {
                                @Override
                                public MissedCallNotifierImpl makeMissedCallNotifierImpl(
                                        Context context,
                                        PhoneAccountRegistrar phoneAccountRegistrar,
                                        PhoneNumberUtilsAdapter phoneNumberUtilsAdapter) {
                                    return new MissedCallNotifierImpl(context,
                                            phoneAccountRegistrar, phoneNumberUtilsAdapter);
                                }
                            },
                            new CallerInfoAsyncQueryFactory() {
                                @Override
                                public CallerInfoAsyncQuery startQuery(int token, Context context,
                                        String number,
                                        CallerInfoAsyncQuery.OnQueryCompleteListener listener,
                                        Object cookie) {
                                    Log.i(TelecomSystem.getInstance(),
                                            "CallerInfoAsyncQuery.startQuery number=%s cookie=%s",
                                            Log.pii(number), cookie);
                                    return CallerInfoAsyncQuery.startQuery(
                                            token, context, number, listener, cookie);
                                }
                            },
                            new HeadsetMediaButtonFactory() {
                                @Override
                                public HeadsetMediaButton create(
                                        Context context,
                                        CallsManager callsManager,
                                        TelecomSystem.SyncRoot lock) {
                                    return new HeadsetMediaButton(context, callsManager, lock);
                                }
                            },
                            new ProximitySensorManagerFactory() {
                                @Override
                                public ProximitySensorManager create(
                                        Context context,
                                        CallsManager callsManager) {
                                    return new ProximitySensorManager(
                                            new TelecomWakeLock(
                                                    context,
                                                    PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK,
                                                    ProximitySensorManager.class.getSimpleName()),
                                            callsManager);
                                }
                            },
                            new InCallWakeLockControllerFactory() {
                                @Override
                                public InCallWakeLockController create(Context context,
                                        CallsManager callsManager) {
                                    return new InCallWakeLockController(
                                            new TelecomWakeLock(context,
                                                    PowerManager.FULL_WAKE_LOCK,
                                                    InCallWakeLockController.class.getSimpleName()),
                                            callsManager);
                                }
                            },
                            new CallAudioManager.AudioServiceFactory() {
                                @Override
                                public IAudioService getAudioService() {
                                    return IAudioService.Stub.asInterface(
                                            ServiceManager.getService(Context.AUDIO_SERVICE));
                                }
                            },
                            new BluetoothPhoneServiceImpl.BluetoothPhoneServiceImplFactory() {
                                @Override
                                public BluetoothPhoneServiceImpl makeBluetoothPhoneServiceImpl(
                                        Context context, TelecomSystem.SyncRoot lock,
                                        CallsManager callsManager,
                                        PhoneAccountRegistrar phoneAccountRegistrar) {
                                    return new BluetoothPhoneServiceImpl(context, lock,
                                            callsManager, new BluetoothAdapterProxy(),
                                            phoneAccountRegistrar);
                                }
                            },
                            new Timeouts.Adapter(),
                            new AsyncRingtonePlayer(),
                            new ViceNotifier() {
                                @Override
                                public ViceNotificationImpl create(Context context,
                                        CallsManager callsManager) {
                                    return new ViceNotificationImpl(
                                            context.getApplicationContext(), callsManager);
                                }
                            },
                            new PhoneNumberUtilsAdapterImpl(),
                            new InterruptionFilterProxy() {
                                @Override
                                public void setInterruptionFilter(int interruptionFilter) {
                                    notificationManager.setInterruptionFilter(interruptionFilter);
                                }

                                @Override
                                public int getCurrentInterruptionFilter() {
                                    return notificationManager.getCurrentInterruptionFilter();
                                }
                            }
                    ));
        }
        if (BluetoothAdapter.getDefaultAdapter() != null) {
            context.startService(new Intent(context, BluetoothPhoneService.class));
        }
    }

  可以看出该处主要初始化了一个TelecomSystem的对象,然后在onBind方法中返回了getTelecomSystem().getTelecomServiceImpl().getBinder().

4.TelecomSystem中初始化了一大堆对象,其中核心为CallsManager,TelecomServiceImpl

public TelecomSystem(
            Context context,
            MissedCallNotifierImplFactory missedCallNotifierImplFactory,
            CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
            HeadsetMediaButtonFactory headsetMediaButtonFactory,
            ProximitySensorManagerFactory proximitySensorManagerFactory,
            InCallWakeLockControllerFactory inCallWakeLockControllerFactory,
            AudioServiceFactory audioServiceFactory,
            BluetoothPhoneServiceImplFactory
                    bluetoothPhoneServiceImplFactory,
            Timeouts.Adapter timeoutsAdapter,
            AsyncRingtonePlayer asyncRingtonePlayer,
            ViceNotifier vicenotifier,
            PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
            InterruptionFilterProxy interruptionFilterProxy) {
        mContext = context.getApplicationContext();
        Log.setContext(mContext);
        Log.initMd5Sum();

        Log.startSession("TS.init");
        mViceNotifier = vicenotifier;
        mPhoneAccountRegistrar = new PhoneAccountRegistrar(mContext);
        mContactsAsyncHelper = new ContactsAsyncHelper(
                new ContactsAsyncHelper.ContentResolverAdapter() {
                    @Override
                    public InputStream openInputStream(Context context, Uri uri)
                            throws FileNotFoundException {
                        return context.getContentResolver().openInputStream(uri);
                    }
                });
        BluetoothManager bluetoothManager = new BluetoothManager(mContext,
                new BluetoothAdapterProxy());
        WiredHeadsetManager wiredHeadsetManager = new WiredHeadsetManager(mContext);
        SystemStateProvider systemStateProvider = new SystemStateProvider(mContext);

        mMissedCallNotifier = missedCallNotifierImplFactory
                .makeMissedCallNotifierImpl(mContext, mPhoneAccountRegistrar,
                        phoneNumberUtilsAdapter);

        DefaultDialerManagerAdapter defaultDialerAdapter =
                new TelecomServiceImpl.DefaultDialerManagerAdapterImpl();

        mCallsManager = new CallsManager(
                mContext,
                mLock,
                mContactsAsyncHelper,
                callerInfoAsyncQueryFactory,
                mMissedCallNotifier,
                mPhoneAccountRegistrar,
                headsetMediaButtonFactory,
                proximitySensorManagerFactory,
                inCallWakeLockControllerFactory,
                audioServiceFactory,
                bluetoothManager,
                wiredHeadsetManager,
                systemStateProvider,
                defaultDialerAdapter,
                timeoutsAdapter,
                asyncRingtonePlayer,
                mViceNotifier,
                phoneNumberUtilsAdapter,
                interruptionFilterProxy);

        mRespondViaSmsManager = new RespondViaSmsManager(mCallsManager, mLock);
        mCallsManager.setRespondViaSmsManager(mRespondViaSmsManager);

        mContext.registerReceiver(mUserSwitchedReceiver, USER_SWITCHED_FILTER);
        mContext.registerReceiver(mUserStartingReceiver, USER_STARTING_FILTER);

        mBluetoothPhoneServiceImpl = bluetoothPhoneServiceImplFactory.makeBluetoothPhoneServiceImpl(
                mContext, mLock, mCallsManager, mPhoneAccountRegistrar);
        mCallIntentProcessor = new CallIntentProcessor(mContext, mCallsManager);
        mTelecomBroadcastIntentProcessor = new TelecomBroadcastIntentProcessor(
                mContext, mCallsManager);

        // Register the receiver for the dialer secret codes, used to enable extended logging.
        mDialerCodeReceiver = new DialerCodeReceiver(mCallsManager);
        mContext.registerReceiver(mDialerCodeReceiver, DIALER_SECRET_CODE_FILTER,
                Manifest.permission.CONTROL_INCALL_EXPERIENCE, null);

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

总结:先把TelecomSystem,CallsManager,TelecomServiceImpl,TelecomService.这些类混个脸熟

你可能感兴趣的:(Android TeleComm 初探)