在来电系统级别会发生什么?

时间:2014-01-10 20:44:15

标签: android telephony

我从中下载了主分支的完整源代码 https://android.googlesource.com/platform/frameworks/base/+/master,我试图破解来电时的事件链。

我认为ACTION_ANSWER意图已经启动,但超出此范围却不知道之前或之后会发生什么。

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:6)

让我们先来看看CallNotifier

  

/ ** *用于侦听手机状态变化的手机应用模块   来自电话层的各种其他*事件,并触发任何事件   产生的UI行为*(如启动Ringer和Incoming Call   用户界面,播放通话音,*更新通知,写通话记录   条目等)* /

此处理程序响应的消息之一是:CallStateMonitor.PHONE_NEW_RINGING_CONNECTION

case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
    log("RINGING... (new)");
    onNewRingingConnection((AsyncResult) msg.obj);
    mSilentRingerRequested = false;
    break;

onNewRingingConnection(AsyncResult)最终(通常情况下)调用ringAndNotifyOfIncomingCall(Connection c)

private void ringAndNotifyOfIncomingCall(Connection c) {
    if (PhoneUtils.isRealIncomingCall(c.getState())) {
        mRinger.ring();
    } else {
        if (VDBG) log("- starting call waiting tone...");
            if (mCallWaitingTonePlayer == null) {
                mCallWaitingTonePlayer = new InCallTonePlayer(
                                         InCallTonePlayer.TONE_CALL_WAITING);
                mCallWaitingTonePlayer.start();
            }
    }

    // CallModeler.onNewRingingConnection(Connection)
    mCallModeler.onNewRingingConnection(c);
}

CallModeler.onNewRingingConnection(Connection)Link)通知附加的听众:

for (int i = 0; i < mListeners.size(); ++i) {
    mListeners.get(i).onIncoming(call);
}

这些侦听器实现CallModeler.Listener接口。 CallHandlerServiceProxy就是这样一个倾听者,其onIncoming(Call)回调会引发CallHandlerServiceProxy.processIncoming(Call)

private void processIncoming(Call call) {
    ....
    // ICallHandlerService
    mCallHandlerServiceGuarded.onIncoming(call,
                   RejectWithTextMessageManager.loadCannedResponses());
    ....
}

CallHandlerService定义了ICallHandlerService.Stub成员,其onIncoming(Call, List<String>)方法如下:

@Override
public void onIncoming(Call call, List<String> textResponses) {
    ....
    mMainHandler.sendMessage(mMainHandler.obtainMessage(
                   ON_UPDATE_CALL_WITH_TEXT_RESPONSES, incomingCall));
    ....
}

这就是mMainHandler处理案例ON_UPDATE_CALL_WITH_TEXT_RESPONSES

的方式
case ON_UPDATE_CALL_WITH_TEXT_RESPONSES:
    AbstractMap.SimpleEntry<Call, List<String>> entry
                   = (AbstractMap.SimpleEntry<Call, List<String>>) msg.obj;
    Log.i(TAG, "ON_INCOMING_CALL: " + entry.getKey());

    // CallList
    mCallList.onIncoming(entry.getKey(), entry.getValue());
    break;

CallList会保留一个实施CallList.Listener的侦听器列表,并从onIncomingCall(Call)方法触发CallList.onIncoming(Call, List<String>)个事件。

现在,让我们看一下InCallPresenter

  

/ ** *从CallList获取更新并通知InCallActivity   (UI)*的变化。 *负责开展活动   所有来电*都是新来电和完成活动   断开。 *创建和管理通话中状态并提供   主持人*的听众模式,希望收听   通话状态的变化。 * TODO:这个班级变得更像一个州   机器在这一点上。考虑重命名。 * /

InCallPresenter实现CallList.Listener接口,负责启动InCallActivity,为所有与电话相关的操作提供UI。以下评论(摘自InCallPresenter.startOrFinishUi(InCallState))将上述事件链结合在一起:

/* A new Incoming call means that the user needs to be notified of the
   the call (since it wasn't them who initiated it).  We do this 
   through full  screen notifications and happens indirectly through {@link 
   StatusBarListener}. The process for incoming calls is as follows:

   1) CallList          - Announces existence of new INCOMING call
   2) InCallPresenter   - Gets announcement and calculates that the new 
                          InCallState should be set to INCOMING.
   3) InCallPresenter   - This method is called to see if we need to 
                          start or finish the app given the new state.
   4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter 
                          calls StatusBarNotifier explicitly to issue a 
                          FullScreen Notification that will either start the
                          InCallActivity or show the user a top-level 
                          notification dialog if the user is in 
                          an immersive app. That notification can also start 
                          the InCallActivity.         
   5) InCallActivity    - Main activity starts up and at the end of its 
                          onCreate will call InCallPresenter::setActivity() 
                          to let the presenter know that start-up is complete.
                  [ AND NOW YOU'RE IN THE CALL. voila! ] */

我希望这可以回答你的问题,或者至少可以告诉你在哪里看。随意纠正我忽略/误解的任何事情。

答案 1 :(得分:1)

看看这个Grep code InCallScreen.java

   else if (action.equals(Intent.ACTION_ANSWER)) {
        internalAnswerCall();
        app.setRestoreMuteOnInCallResume(false);
        return InCallInitStatus.SUCCESS;

答案 2 :(得分:1)

希望下面的代码帮助你。

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
         super.onCallStateChanged(state, incomingNumber);
         switch(state){
         case TelephonyManager.CALL_STATE_IDLE:
             //Not in call: Play music

             break;
         case TelephonyManager.CALL_STATE_OFFHOOK:
             //A call is dialing, active or on hold

             break;
         case TelephonyManager.CALL_STATE_RINGING:
             //Incoming call: Pause music

             break;

         }            
    }

谷歌参考是
http://developer.android.com/reference/android/telephony/TelephonyManager.html