android 6.0的TELECOM_SERVICE的由来

起因

android 5.0之前是,Phone进程对外的服务都是通过TelephonyManager来实现的。现在多出来了个TelecomManager来给应用调用电话相关的功能。所以要看看,这个TelecomManager是何方神圣。

使用举例

  • Dialer中,拨打电话的时候,最后会调用TelecomManager的placeCall函数。
    final TelecomManager tm = (TelecomManager)context.getSystemService(Context.TELECOM_SERVICE);
    tm.placeCall(intent.getData(), intent.getExtras());
    发现是通过getSystemService来获得的。所以看一下是怎么注册到Context里面去的,是在frameworks/base/core/java/android/app/SystemServiceRegistry.java里面:
    registerService(Context.TELECOM_SERVICE, TelecomManager.class,
    new CachedServiceFetcher<TelecomManager>() {
    @Override
    public TelecomManager createService(ContextImpl ctx) {
    return new TelecomManager(ctx.getOuterContext());
    }});
    这样,应用通过getSystemService接口就可以方便使用TelecomManager的实例了。

  • 回到placeCall这个函数:
    public void placeCall(Uri address, Bundle extras) {
    ITelecomService service = getTelecomService();
    if (service != null) {
    if (address == null) {
    Log.w(TAG, "Cannot place call to empty address.");
    }
    try {
    service.placeCall(address, extras == null ? new Bundle() : extras,
    mContext.getOpPackageName());
    } catch (RemoteException e) {
    Log.e(TAG, "Error calling ITelecomService#placeCall", e);
    }
    }
    }
    发现是调用ITelecomService这个service
    private ITelecomService getTelecomService() {
    return ITelecomService.Stub.asInterface(ServiceManager.getService(Context.TELECOM_SERVICE));
    }
    又是通过Context.TELECOM_SERVICE,发现和之前Dialer用统一个关键词来获得service,但是注意,这个两个不同的方式获得,所以对应的service也不一样。
    后面这个是通过ServiceManager来找到系统的服务,然后转成对应的服务端引用。说明必然有个地方把这个
    ITelecomService加入到ServiceManager中。所以,搜索代码:
    在frameworks/base/services/core/java/com/android/server/telecom/TelecomLoaderService.java的内部类TelecomServiceConnection的onServiceConnected中添加
    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);
    ServiceManager.addService(Context.TELECOM_SERVICE, service);
    ....
    即,通过bindService连接了某个service,然后把通过binder返回的service添加到ServiceManager中。
    在TelecomLoaderService中可以看到,是绑定了TelecomService(packages/services/Telecomm/src/com/android/server/telecom/components/TelecomService.java),看TelecomService的onBind函数
    @Override
    public IBinder onBind(Intent intent) {
    Log.d(this, "onBind");
    initializeTelecomSystem(this);
    synchronized (getTelecomSystem().getLock()) {
    return getTelecomSystem().getTelecomServiceImpl().getBinder();
    }
    }
    当bindService的命令来的时候,做了两件事,一个是初始化TelecomSystem,一个是返回TelecomServiceImpl中的binder实例(实现了ITelecomService的stub端)。

  1. initializeTelecomSystem里面生成了TelecomSystem对象,在TelecomSystem的构造函数里面,创建了CallsManager/TelecomServiceImpl/CallIntentProcessor/PhoneAccountRegistrar/MissedCallNotifier等重要的telecom实例。
  2. getTelecomSystem().getTelecomServiceImpl().getBinder():实际就是得到了packages/services/Telecomm/src/com/android/server/telecom/TelecomServiceImpl.java中的mBinderImpl
    private final ITelecomService.Stub mBinderImpl = new ITelecomService.Stub() {
    @Override
    public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme,
    String callingPackage) {
    synchronized (mLock) {
    if (!canReadPhoneState(callingPackage, "getDefaultOutgoingPhoneAccount")) {
    return null;
    }
    ....
  • 即,最后TelecomManager调用TelecomServiceImpl的placeCall函数完成打电话操作。
    /**
    * @see android.telecom.TelecomManager#placeCall
    */
    @Override
    public void placeCall(Uri handle, Bundle extras, String callingPackage) {
    enforceCallingPackage(callingPackage);
    if (!canCallPhone(callingPackage, "placeCall")) {
    throw new SecurityException("Package " + callingPackage
    + " is not allowed to place phone calls");
    }

          // Note: we can still get here for the default/system dialer, even if the Phone
          // permission is turned off. This is because the default/system dialer is always
          // allowed to attempt to place a call (regardless of permission state), in case
          // it turns out to be an emergency call. If the permission is denied and the
          // call is being made to a non-emergency number, the call will be denied later on
          // by {@link UserCallIntentProcessor}.
    
          final boolean hasCallAppOp = mAppOpsManager.noteOp(AppOpsManager.OP_CALL_PHONE,
                  Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
    
          final boolean hasCallPermission = mContext.checkCallingPermission(CALL_PHONE) ==
                  PackageManager.PERMISSION_GRANTED;
    
          synchronized (mLock) {
              final UserHandle userHandle = Binder.getCallingUserHandle();
              long token = Binder.clearCallingIdentity();
              try {
                  final Intent intent = new Intent(Intent.ACTION_CALL, handle);
                  intent.putExtras(extras);
                  new UserCallIntentProcessor(mContext, userHandle).processIntent(intent,
                          callingPackage, hasCallAppOp && hasCallPermission);
              } finally {
                  Binder.restoreCallingIdentity(token);
              }
          }
      }
    

问题:什么时候完成发生的启动TelecomService的?

回答:系统启动时SystemServer(frameworks/base/services/java/com/android/server/SystemServer.java)会启动TelecomLoaderService。
mSystemServiceManager.startService(TelecomLoaderService.class);
当系统回调onBootPhase的时候,就会调用connectToTelecom函数去启动TelecomService。
@Override
public void onBootPhase(int phase) {
if (phase == PHASE_ACTIVITY_MANAGER_READY) {
registerDefaultAppNotifier();
registerCarrierConfigChangedReceiver();
connectToTelecom();
}
}

感受:

Google为了弄个用户用的TelecomManager居然搞了这么大的一圈。其实,这就是分层的设计理念。不同层次的调用对应不同的方法。
另外,通过分析也知道了CallsManager等相关函数的创建点。总之,收获满满。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • ```java /* * Copyright (C) 2006 The Android Open Source P...
    mrganer阅读 1,082评论 0 50
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 1:InputChannel提供函数创建底层的Pipe对象 2: 1)客户端需要新建窗口 2)new ViewRo...
    自由人是工程师阅读 5,096评论 0 18
  • 七长篇小说《百年炉火》第十七章 2015-02-18 12:5835 十七 张八老汉这几天一直睡不好,一件事情纳在...
    294af09a07b2阅读 412评论 0 0
  • 一、搓揉脚趾的作用 ①可以促使我们身体尽快恢复健康状态,并且启到保健的作用 ②帮助食物促进吸收 二、搓洗腋窝 ①经...
    金雪娃娃阅读 229评论 1 2