Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 撥號流程全解析:從 DialpadFragment 一路追進 RIL 與 modem

編輯:關於 Android 程式設計

絕大多數情況下,使用者是透過 dialer 輸入號碼並按下撥號鍵完成通話,所以我們就從 dialer 這個入口開始往下追。

DialpadFragment:撥號盤的起點

DialpadFragment 就是撥號界面的 Fragment。當使用者點擊撥號按鈕時,會回呼它的 onClick:

public void onClick(View view) {
    switch (view.getId()) {
        case R.id.dialpad_floating_action_button:
            view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
            handleDialButtonPressed();
            break;
    }
}

handleDialButtonPressed 的核心邏輯是組出一個 Intent 並啟動它:

final Intent intent = CallUtil.getCallIntent(number);
if (!isDigitsShown) {
    // 必須是會議通話時,補上這個 extra
    intent.putExtra(EXTRA_DIAL_CONFERENCE_URI, true);
}
intent.putExtra(ADD_PARTICIPANT_KEY, mAddParticipant && isPhoneInUse());
DialerUtils.startActivityWithErrorToast(getActivity(), intent);
hideAndClearDialpad(false);

CallUtil#getCallIntent:Intent.ACTION_CALL

這裡組出的 Intent 帶的 action 是 Intent.ACTION_CALL,它對應到 /packages/services/Telephony 模組中的 OutgoingCallBroadcaster,而這是一個 Activity。

public static Intent getCallIntent(String number) {
    return getCallIntent(getCallUri(number));
}
public static Intent getCallIntent(Uri uri) {
    return new Intent(Intent.ACTION_CALL, uri);
}

OutgoingCallBroadcaster:權限與號碼的守門人

進入這個 Activity 後,onCreate 會把 Intent 交給 processIntent 處理。processIntent 主要負責三種 action:CALL(一般對外語音通話)、CALL_PRIVILEGED(內建應用如聯絡人、語音撥號、藍牙所發出)與 CALL_EMERGENCY(來自鎖定畫面可達的緊急撥號器)。

對於 tel: URI 的電話,流程是 OutgoingCallReceiver → SipCallOptionHandler → InCallScreen;對於 sip: URI 的網路電話,則跳過 NEW_OUTGOING_CALL 廣播,直接走 SipCallOptionHandler → InCallScreen;voicemail: URI 的語音信箱處理方式與電話類似。

processIntent 做了幾件關鍵的事:先確認裝置具備語音能力,否則直接返回;接著從 Intent 取出號碼並清除分隔符號;然後透過 AppOpsManager 檢查發出 ACTION_CALL 的 uid 與 package 是否具有 OP_CALL_PHONE 權限,沒有就 finish 掉。若為緊急號碼或號碼合法,callNow 為 true,會直接跳到 InCallScreen,不發送 NEW_OUTGOING_CALL 廣播;否則組出一個 ACTION_NEW_OUTGOING_CALL 的有序廣播,交由 OutgoingCallReceiver 攔截處理,並掛上逾時訊息作為保險。

CallController:真正下撥號指令的地方

public void placeCall(Intent intent) {
    // 只接受 CALL / CALL_EMERGENCY / CALL_PRIVILEGED 三種 action
    Phone phone = mApp.mCM.getDefaultPhone();
    if (TelephonyCapabilities.supportsOtasp(phone)) {
        checkForOtaspCall(intent);
    }
    CallStatusCode status = placeCallInternal(intent);
    switch (status) {
        case SUCCESS:
        case EXITED_ECM:
            break;
        default:
            handleOutgoingCallError(status);
            break;
    }
}

無論成功與否,最後都會顯示 InCallScreen,並依錯誤碼給出對應提示。placeCallInternal 則把實際工作交給 PhoneUtils.placeCall,由它與 RIL 建立連線、設定音頻模式與免持狀態。

CallManager#dial 與 Phone 代理

CallManager 會先檢查當前狀態是否允許撥號,若不允许則嘗試當作 in-call MMI 指令處理,否則拋出 CallStateException。而 basePhone 多半是 Phone 的代理類 PhoneProxy,會依 mActivePhone 判斷真正的實作,可能是 GSMPhone、CDMAPhone 或 SipPhone。

以 GSMPhone#dialInternal 為例

protected Connection dialInternal(String dialString, UUSInfo uusInfo, int videoState, Bundle intentExtras) {
    String newDialString = PhoneNumberUtils.stripSeparators(dialString);
    if (handleInCallMmiCommands(newDialString)) {
        return null;
    }
    String networkPortion = PhoneNumberUtils.extractNetworkPortionAlt(newDialString);
    GsmMmiCode mmi = GsmMmiCode.newFromDialString(networkPortion, this, mUiccApplication.get());
    if (mmi == null) {
        return mCT.dial(newDialString, uusInfo, intentExtras);
    } else if (mmi.isTemporaryModeCLIR()) {
        return mCT.dial(mmi.mDialingNumber, mmi.getCLIRMode(), uusInfo, intentExtras);
    }
    mPendingMMIs.add(mmi);
    mMmiRegistrants.notifyRegistrants(new AsyncResult(null, mmi, null));
    mmi.processCode();
    return null;
}

也就是說,號碼會先被判定是 MMI 指令(如 *#06#)還是真正的電話號碼,兩條路之後都由 GsmCallTracker#dial 接手。

RIL#dial:走到盡頭

public void dial(String address, int clirMode, UUSInfo uusInfo, Message result) {
    RILRequest rr = RILRequest.obtain(RIL_REQUEST_DIAL, result);
    rr.mParcel.writeString(address);
    rr.mParcel.writeInt(clirMode);
    // ... 寫入 UUS 資訊
    send(rr);
}

最終仍然是透過 send 方法把 EVENT_SEND 訊息丟給自己處理——有意思的是,這裡居然和簡訊的發送走到同一條路上,之後就交給 modem 去執行真正的動作。

流程總結

  1. DialpadFragment#onClick
  2. DialpadFragment#handleDialButtonPressed
  3. OutgoingCallBroadcaster#processIntent
  4. CallController#placeCall
  5. CallController#placeCallInternal
  6. PhoneUtils#placeCall
  7. CallManager#dial
  8. PhoneProxy#dial
  9. 以 GSMPhone 為例:GSMPhone#dialInternal
  10. RIL#dial
  11. RIL#send,發送 msg = mSender.obtainMessage(EVENT_SEND, rr) 給 RILSender 處理
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved