Core-Telecom

Core-Telecom 程式庫提供可靠且一致的 API 組合,簡化將呼叫應用程式與 Android 平台整合的程序

如要瞭解實際實作方式,您可以在 GitHub 上找到應用程式範例:

設定 Core-Telecom

androidx.core:core-telecom 依附元件新增至應用程式的 build.gradle 檔案:

dependencies {
    implementation ("androidx.core:core-telecom:1.0.0")
}

AndroidManifest.xml 中宣告 MANAGE_OWN_CALLS 權限:

<uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />

註冊應用程式

使用 CallsManager 將通話應用程式註冊至 Android,即可開始向系統新增通話。註冊時,請指定應用程式的功能 (例如音訊、視訊支援):

val callsManager = CallsManager(context)

val capabilities: @CallsManager.Companion.Capability Int =
    (CallsManager.CAPABILITY_BASELINE or
          CallsManager.CAPABILITY_SUPPORTS_VIDEO_CALLING)

callsManager.registerAppWithTelecom(capabilities)

通話管理

使用 Core-Telecom API 建立及管理通話生命週期。

建立通話

CallAttributesCompat 物件會定義專屬通話的屬性,這些屬性可具有下列特性:

  • displayName:來電者名稱。
  • address:通話地址 (例如電話號碼、會議連結)。
  • direction:傳入或傳出。
  • callType:音訊或視訊。
  • callCapabilities:支援轉移和保留。

以下範例說明如何建立來電:

fun createIncomingCallAttributes(
    callerName: String,
    callerNumber: String,
    isVideoCall: Boolean): CallAttributesCompat {
    val addressUri = Uri.parse("YourAppScheme:$callerNumber")

    // Define capabilities supported by your call.
    val callCapabilities = CallAttributesCompat.CallCapability(
        supportsSetInactive = CallAttributesCompat.SUPPORTS_SET_INACTIVE // Call can be made inactive (implies hold)
    )

    return CallAttributesCompat(
        displayName = callerName,
        address = addressUri,
        direction = CallAttributesCompat.DIRECTION_INCOMING,
        callType = if (isVideoCall) CallAttributesCompat.CALL_TYPE_VIDEO_CALL else CallAttributesCompat.CALL_TYPE_AUDIO_CALL,
        callCapabilitiesCompat = callCapabilities
    )
}

新增通話

請將 callsManager.addCallCallAttributesCompat 和回呼搭配使用,為系統新增呼叫並管理遠端途徑更新。addCall 區塊中的 callControlScope 主要可讓應用程式轉換通話狀態,並接收音訊更新:

try {
    callsManager.addCall(
        INCOMING_CALL_ATTRIBUTES,
        onAnswerCall, // Watch needs to know if it can answer the call.
        onSetCallDisconnected,
        onSetCallActive,
        onSetCallInactive
    ) {
        // The call was successfully added once this scope runs.
        callControlScope = this
    }
}
catch(addCallException: Exception){
   // Handle the addCall failure.
}

接聽來電

CallControlScope 中接聽來電:

when (val result = answer(CallAttributesCompat.CALL_TYPE_AUDIO_CALL)) {
    is CallControlResult.Success -> { /* Call answered */ }
    is CallControlResult.Error -> { /* Handle error */ }
}

拒接來電

CallControlScope 中使用 disconnect() 搭配 DisconnectCause.REJECTED 拒接來電:

disconnect(DisconnectCause(DisconnectCause.REJECTED))

讓外撥通話處於活動狀態

在對方接聽後,將外撥通話設為啟用狀態:

when (val result = setActive()) {
    is CallControlResult.Success -> { /* Call active */ }
    is CallControlResult.Error -> { /* Handle error */ }
}

保留通話

使用 setInactive() 保留通話:

when (val result = setInactive()) {
    is CallControlResult.Success -> { /* Call on hold */ }
    is CallControlResult.Error -> { /* Handle error */ }
}

中斷通話

使用 disconnect()DisconnectCause 中斷通話:

disconnect(DisconnectCause(DisconnectCause.LOCAL))

管理通話音訊端點

使用 CallControlScope 中的 currentCallEndpointavailableEndpointsisMuted Flow 觀察及管理音訊端點

fun observeAudioStateChanges(callControlScope: CallControlScope) {
    with(callControlScope) {
        launch { currentCallEndpoint.collect { /* Update UI */ } }
        launch { availableEndpoints.collect { /* Update UI */ } }
        launch { isMuted.collect { /* Handle mute state */ } }
    }
}

使用 requestEndpointChange() 變更有效音訊裝置:

coroutineScope.launch {
     callControlScope.requestEndpointChange(callEndpoint)
}

前景支援

程式庫會使用 ConnectionService (Android 13 API 級別 33 以下) 或 foregroundtypes (Android 14 API 級別 34 以上) 提供前景支援。

根據前景服務規定,應用程式必須發布通知,讓使用者知道應用程式正在前景執行。

為確保應用程式獲得前景執行優先順序,請在新增與平台的呼叫後建立通知。當應用程式終止呼叫或通知不再有效時,前景優先順序就會移除。

進一步瞭解前景服務

遠端 Surface 支援

遠端裝置 (智慧手錶、藍牙耳機、Android Auto) 可在不直接與手機互動下管理通話。您的應用程式必須實作回呼 lambda (onAnswerCallonSetCallDisconnectedonSetCallActiveonSetCallInactive),並提供給 CallsManager.addCall,以便處理這些裝置啟動的動作。

發生遠端動作時,系統會叫用相應的 lambda。

成功完成 lambda 信號,表示指令已處理。如果無法遵循指令,lambda 應擲回例外狀況。

正確實作可確保在不同裝置上流暢控制通話。使用各種遠端表面進行徹底測試。

來電額外資訊

除了管理通話狀態和通話音訊路徑,這個程式庫還支援通話擴充功能,這是應用程式可實作的選用功能,可在 Android Auto 等遠端介面提供更豐富的通話體驗。這些功能包括會議室、通話靜音和其他通話圖示。當應用程式實作擴充功能時,應用程式提供的資訊會與所有已連結裝置同步,這些裝置也支援在 UI 中顯示這些擴充功能。也就是說,使用者也可以在遠端裝置上使用這些功能。

建立含有額外資訊的通話

建立通話時,您可以改用 CallManager#addCallWithExtensions 來讓應用程式存取名為 ExtensionInitializationScope 的不同範圍,而非使用 CallManager#addCall 建立通話。這個範圍可讓應用程式初始化所支援的選用擴充功能組合。此外,這個範圍還提供額外方法 onCall,在擴充功能交換和初始化完成後,會將 CallControlScope 傳回至應用程式。

scope.launch {
    mCallsManager.addCallWithExtensions(
        attributes,
        onAnswer,
        onDisconnect,
        onSetActive,
        onSetInactive
    ) {
        // Initialize extension-specific code...

        // After the call has been initialized, perform in-call actions
        onCall {
            // Example: process call state updates
            callStateFlow.onEach { newState ->
                // handle call state updates and notify telecom
            }.launchIn(this)

            // Use initialized extensions...
        }
    }
}

支援通話參與者

如果您的應用程式支援會議或群組通話的通話參與者,請使用 addParticipantExtension 宣告支援此擴充功能,並在參與者變更時使用相關 API 更新遠端介面。

mCallsManager.addCallWithExtensions(...) {
        // Initialize extensions...

        // Notifies Jetpack that this app supports the participant
        // extension and provides the initial participants state in the call.
        val participantExtension = addParticipantExtension(
            initialParticipants,
            initialActiveParticipant
        )

        // After the call has been initialized, perform in-call control actions
        onCall {
            // other in-call control and extension actions...

            // Example: update remote surfaces when the call participants change
            participantsFlow.onEach { newParticipants ->
                participantExtension.updateParticipants(newParticipants)
            }.launchIn(this)
        }
    }

除了通知遠端介面通話中的參與者,您也可以使用 ParticipantExtension#updateActiveParticipant 更新實際參與者。

系統也支援與通話參與者相關的選用動作。應用程式可使用 ParticipantExtension#addRaiseHandSupport 支援參與者在通話中舉手,並查看其他參與者是否也舉手。

mCallsManager.addCallWithExtensions(...) {
        // Initialize extensions...

        // Notifies Jetpack that this app supports the participant
        // extension and provides the initial list of participants in the call.
        val participantExtension = addParticipantExtension(initialParticipants)
        // Notifies Jetpack that this app supports the notion of participants
        // being able to raise and lower their hands.
        val raiseHandState = participantExtension.addRaiseHandSupport(
                initialRaisedHands
            ) { onHandRaisedStateChanged ->
                // handle this user's raised hand state changed updates from
                // remote surfaces.
            }

        // After the call has been initialized, perform in-call control actions
        onCall {
            // other in-call control and extension actions...

            // Example: update remote surfaces when the call participants change
            participantsFlow.onEach { newParticipants ->
                participantExtension.updateParticipants(newParticipants)
            }.launchIn(this)
            // notify remote surfaces of which of the participants have their
            // hands raised
            raisedHandsFlow.onEach { newRaisedHands ->
                raiseHandState.updateRaisedHands(newRaisedHands)
            }.launchIn(this)
        }
    }

支援來電靜音功能

使用者可以要求應用程式將通話的傳出音訊靜音,而無須實際將裝置的麥克風靜音。這項功能會針對個別通話進行管理,因此 Jetpack 可在 VoIP 通話處於啟用狀態時,處理管理當前行動通話全域靜音狀態的複雜問題。這可讓多通話情況下靜音傳出音訊的錯誤率降低,同時也能提供實用的功能,例如在使用者講話時,但未察覺通話靜音功能已啟用時,顯示「你在說話嗎?」等提示。

mCallsManager.addCallWithExtensions(...) {
        // Initialize extensions...

        // Add support for locally silencing the call's outgoing audio and
        // register a handler for when the user changes the call silence state
        // from a remote surface.
        val callSilenceExtension = addLocalCallSilenceExtension(
            initialCallSilenceState = false
        ) { newCallSilenceStateRequest ->
            // handle the user's request to enable/disable call silence from
            // a remote surface
        }

        // After the call has been initialized, perform in-call control actions
        onCall {
            // other in-call control and extension actions...

            // When the call's call silence state changes, update remote
            // surfaces of the new state.
            callSilenceState.onEach { isSilenced ->
                callSilenceExtension.updateIsLocallySilenced(isSilenced)
            }.launchIn(this)
        }
    }

支援通話圖示

通話圖示可讓應用程式指定代表通話的自訂圖示,以便在通話期間顯示在遠端介面上。這個圖示也會在通話期間更新。

mCallsManager.addCallWithExtensions(...) {
        // Initialize extensions...

        // Add support for a custom call icon to be displayed during the
        // lifetime of the call.
        val callIconExtension = addCallIconExtension(
            initialCallIconUri = initialUri
        )

        // After the call has been initialized, perform in-call control actions
        onCall {
            // other in-call control and extension actions...

            // When the call's icon changes, update remote surfaces by providing
            // the new URI.
            callIconUri.onEach { newIconUri ->
                callIconExtension.updateCallIconUri(newIconUri)
            }.launchIn(this)
        }
    }