android Telephony学习 --- 第七篇 android7.0 来电(MT)流程

我们先看下7.0来电大体流程:


7.0来电流程.png

Framework

modem接收到来电通知消息后,以AT指令的方式上报RIL层,RIL层通过sokcet将消息发送给RILJ, 上报事件ID: RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED:

  • frameworks/opt/telephony – RIL
    private void processUnsolicited (Parcel p, int type) {
           case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED:
                if (RILJ_LOGD) unsljLog(response);
                mCallStateRegistrants
                    .notifyRegistrants(new AsyncResult(null, null, null));
            break;
  • frameworks/opt/telephony – BaseCommands
    mCallStateRegistrants在BaseCommands中添加的观察者方法,在GsmCdmaCallTracker中注册了registerForCallStateChanged方法:
    @Override
    public void registerForCallStateChanged(Handler h, int what, Object obj) {
        Registrant r = new Registrant (h, what, obj);
        mCallStateRegistrants.add(r);
    }
  • frameworks/opt/telephony – GsmCdmaCallTracker
   public GsmCdmaCallTracker (GsmCdmaPhone phone) {
        mCi = phone.mCi;
        mCi.registerForCallStateChanged(this, EVENT_CALL_STATE_CHANGE, null);

接着找到EVENT_CALL_STATE_CHANGE消息:

            case EVENT_CALL_STATE_CHANGE:
                pollCallsWhenSafe();
            break;
  • frameworks/opt/telephony – CallTracker
    找到GsmCdmaCallTracker父类的方法pollCallsWhenSafe:
    protected void pollCallsWhenSafe() {
        mNeedsPoll = true;
        if (checkNoOperationsPending()) {
            mLastRelevantPoll = obtainMessage(EVENT_POLL_CALLS_RESULT);
            mCi.getCurrentCalls(mLastRelevantPoll);
        }
    }
  • frameworks/opt/telephony – RIL
    找到CommandsInterface mCi,而其RIL implements CommandsInterface实现了getCurrentCalls方法,,携带消息
    EVENT_POLL_CALLS_RESULT:
    @Override
    public void getCurrentCalls (Message result) {
        RILRequest rr = RILRequest.obtain(RIL_REQUEST_GET_CURRENT_CALLS, result);
        if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
        send(rr);
    }
  • frameworks/opt/telephony – GsmCdmaCallTracker
    和之前流程类似,接着进入handlePollCalls方法:
 case EVENT_POLL_CALLS_RESULT:
                Rlog.d(LOG_TAG, "Event EVENT_POLL_CALLS_RESULT Received");
                if (msg == mLastRelevantPoll) {
                    if (DBG_POLL) log(
                            "handle EVENT_POLL_CALL_RESULT: set needsPoll=F");
                    mNeedsPoll = false;
                    mLastRelevantPoll = null;
                    handlePollCalls((AsyncResult)msg.obj);
                }

更新状态,发送call state change通知等:

     if (newRinging != null) {
            mPhone.notifyNewRingingConnection(newRinging);
         }
        updatePhoneState();
        if (hasNonHangupStateChanged || newRinging != null || hasAnyCallDisconnected) {
            mPhone.notifyPreciseCallStateChanged();
        }
  • frameworks/opt/telephony – Phone
    notifyNewRingingConnectionP方法:
    /**
     * Notify registrants of a new ringing Connection.
     * Subclasses of Phone probably want to replace this with a
     * version scoped to their packages
     */
    public void notifyNewRingingConnectionP(Connection cn) {
        if (!mIsVoiceCapable)
            return;
        AsyncResult ar = new AsyncResult(null, cn, null);
        mNewRingingConnectionRegistrants.notifyRegistrants(ar);
    }

Telephony

  • packages/service/Telephony – PstnIncomingCallNotifier
    找到注册registerForNewRingingConnection处,
    消息EVENT_NEW_RINGING_CONNECTION调用handleNewRingingConnection:
         mPhone.registerForNewRingingConnection(mHandler, EVENT_NEW_RINGING_CONNECTION, null);

         case EVENT_NEW_RINGING_CONNECTION:
               handleNewRingingConnection((AsyncResult) msg.obj);
               break;

Framework

  • frameworks/base/telecomm – TelecomManager
    addNewIncomingCall方法
public void addNewIncomingCall(PhoneAccountHandle phoneAccount, Bundle extras) {
        try {
            if (isServiceConnected()) {
                getTelecomService().addNewIncomingCall(
                        phoneAccount, extras == null ? new Bundle() : extras);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException adding a new incoming call: " + phoneAccount, e);
        }
    }

Telecom

  • packages/services/Telecom – TelecomServiceImpl
    找到对应的ITelecomServic aidl接收的地方,查看addNewIncomingCall方法:
  private final ITelecomService.Stub mBinderImpl = new ITelecomService.Stub() {
   @Override
        public void addNewIncomingCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
              Intent intent = new Intent(TelecomManager.ACTION_INCOMING_CALL);
                            intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
                                    phoneAccountHandle);
              intent.putExtra(CallIntentProcessor.KEY_IS_INCOMING_CALL, true);
              if (extras != null) {
                       extras.setDefusable(true);
                                intent.putExtra(TelecomManager.EXTRA_INCOMING_CALL_EXTRAS, extras);
                            }
             mCallIntentProcessorAdapter.processIncomingCallIntent(
                                    mCallsManager, intent); 
  • packages/services/Telecom – CallIntentProcessor
    static void processIncomingCallIntent(CallsManager callsManager, Intent intent) {
        callsManager.processIncomingCallIntent(phoneAccountHandle, clientExtras);
    }
  • packages/services/Telecom – CallsManager
    创建call之后,CreateConnection创建链接,之后的流程和呼出流程类似:
   void processIncomingCallIntent(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
        Call call = new Call(
                getNextCallId(),
                mContext,
                this,
                mLock,
                mConnectionServiceRepository,
                mContactsAsyncHelper,
                mCallerInfoAsyncQueryFactory,
                handle,
                null /* gatewayInfo */,
                null /* connectionManagerPhoneAccount */,
                phoneAccountHandle,
                Call.CALL_DIRECTION_INCOMING /* callDirection */,
                false /* forceAttachToExistingConnection */,
                false /* isConference */
        );
        call.addListener(this);
        call.startCreateConnection(mPhoneAccountRegistrar);
    }
  • packages/services/Telecom – Call
void startCreateConnection(PhoneAccountRegistrar phoneAccountRegistrar) {
    mCreateConnectionProcessor = new CreateConnectionProcessor(this, mRepository, this,
            phoneAccountRegistrar, mContext);
    mCreateConnectionProcessor.process();
}
  • packages/services/Telecom – CreateConnectionProcessor
@VisibleForTesting
public void process() {
    Log.v(this, "process");
    clearTimeout();
    mAttemptRecords = new ArrayList<>();
    if (mCall.getTargetPhoneAccount() != null) {
        mAttemptRecords.add(new CallAttemptRecord(
                mCall.getTargetPhoneAccount(), mCall.getTargetPhoneAccount()));
    }
    adjustAttemptsForConnectionManager();
    adjustAttemptsForEmergency();
    mAttemptRecordIterator = mAttemptRecords.iterator();
    attemptNextPhoneAccount();
}
  • packages/services/Telecom – ConnectionServiceWrapper
mServiceInterface.createConnection(

        call.getConnectionManagerPhoneAccount(),
        callId,
        new ConnectionRequest(
                call.getTargetPhoneAccount(),
                call.getHandle(),
                extras,
                call.getVideoState(),
                callId),
        call.shouldAttachToExistingConnection(),
        call.isUnknown());

Frameworks

  • frameworks/base/telecomm -- ConnectionService
    呼出时是调用onCreateOutgoingConnection,此篇是呼入,需要查看onCreateIncomingConnection
Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request)
        : isIncoming ? onCreateIncomingConnection(callManagerAccount, request)
        : onCreateOutgoingConnection(callManagerAccount, request);

Telecom

  • packages/services/Telecom – Call
 public void handleCreateConnectionSuccess(
    switch (mCallDirection) {
        case CALL_DIRECTION_INCOMING:
            // Listeners (just CallsManager for now) will be responsible for checking whether
            // the call should be blocked.
            for (Listener l : mListeners) {
                l.onSuccessfulIncomingCall(this);
            }
            break;
  • packages/services/Telecom – CallsManager
@Override
public void onSuccessfulIncomingCall(Call incomingCall) {
    Log.d(this, "onSuccessfulIncomingCall");
    List<IncomingCallFilter.CallFilter> filters = new ArrayList<>();
    filters.add(new DirectToVoicemailCallFilter(mCallerInfoLookupHelper));
    filters.add(new AsyncBlockCheckFilter(mContext, new BlockCheckerAdapter()));
    filters.add(new CallScreeningServiceFilter(mContext, this, mPhoneAccountRegistrar,
            mDefaultDialerManagerAdapter,
            new ParcelableCallUtils.Converter(), mLock));
    new IncomingCallFilter(mContext, this, incomingCall, mLock,
            mTimeoutsAdapter, filters).performFiltering();
}
  • packages/services/Telecom – IncomingCallFilter
    主要执行关于拦截来电的,是否是黑名单等信息,此篇不关注此处流程:
public void performFiltering() {
    mHandler.postDelayed(new Runnable("ICF.pFTO") { // performFiltering time-out
        @Override
        public void loggedRun() {
            // synchronized to prevent a race on mResult and to enter into Telecom.
            synchronized (mTelecomLock) {
                if (mIsPending) {
                    Log.i(IncomingCallFilter.this, "Call filtering has timed out.");
                    Log.event(mCall, Log.Events.FILTERING_TIMED_OUT);
                    mListener.onCallFilteringComplete(mCall, mResult);
                    mIsPending = false;
                }
            }
        }
    }.prepare(), mTimeoutsAdapter.getCallScreeningTimeoutMillis(mContext.getContentResolver()));
}
  • packages/services/Telecom – CallsManager
   @Override
   public void onCallFilteringComplete(Call incomingCall, CallFilteringResult result) {
          addCall(incomingCall);
      }
    private void addCall(Call call) {
        for (CallsManagerListener listener : mListeners) {
            listener.onCallAdded(call);}
  • packages/services/Telecom – InCallController
    和呼入篇类似,附相关代码:
@Override
public void onCallAdded(Call call) {
    if (!isBoundToServices()) {
        bindToServices(call);
    } else {
        addCall(call);
        inCallService.addCall(parcelableCall);
}

Frameworks

  • frameworks/base/telecomm – InCallService
    实现aidl方法addCall,找到消息MSG_ADD_CALL:
private final class InCallServiceBinder extends IInCallService.Stub {
    @Override
    public void setInCallAdapter(IInCallAdapter inCallAdapter) {
        mHandler.obtainMessage(MSG_SET_IN_CALL_ADAPTER, inCallAdapter).sendToTarget();
    }
    @Override
    public void addCall(ParcelableCall call) {
        mHandler.obtainMessage(MSG_ADD_CALL, call).sendToTarget();
    }
    case MSG_ADD_CALL:
    mPhone.internalAddCall((ParcelableCall) msg.obj);
    break;
  • frameworks/base/telecomm -- Phone
final void internalAddCall(ParcelableCall parcelableCall) {
    Call call = new Call(this, parcelableCall.getId(), mInCallAdapter,
            parcelableCall.getState());
    mCallByTelecomCallId.put(parcelableCall.getId(), call);
    mCalls.add(call);
    checkCallTree(parcelableCall);
    call.internalUpdate(parcelableCall, mCallByTelecomCallId);
    fireCallAdded(call);
 }
private void fireCallAdded(Call call) {
    for (Listener listener : mListeners) {
        listener.onCallAdded(this, call);
    }
}
  • frameworks/base/telecomm – InCallService
@Override
public void onCallAdded(Phone phone, Call call) {
    InCallService.this.onCallAdded(call);
}

Dialer

  • packages/app/Dialer -- InCallServiceImpl
@Override
public void onCallAdded(Call call) {
   InCallPresenter.getInstance().onCallAdded(call);}

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

推荐阅读更多精彩内容