Made call redirection service prototype work

This commit is contained in:
Sylvain Berfini 2025-11-18 09:20:46 +01:00
parent baa79d9982
commit 1669113352
4 changed files with 105 additions and 2 deletions

View file

@ -217,6 +217,15 @@
</intent-filter> </intent-filter>
</service> </service>
<service
android:name=".telecom.TelecomRedirectionService"
android:exported="true"
android:permission="android.permission.BIND_CALL_REDIRECTION_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallRedirectionService"/>
</intent-filter>
</service>
<!-- Receivers --> <!-- Receivers -->
<receiver android:name=".core.CorePushReceiver" <receiver android:name=".core.CorePushReceiver"

View file

@ -20,6 +20,7 @@
package org.linphone.activities.main package org.linphone.activities.main
import android.app.Dialog import android.app.Dialog
import android.app.role.RoleManager
import android.content.ComponentCallbacks2 import android.content.ComponentCallbacks2
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@ -260,6 +261,8 @@ class MainActivity : GenericActivity(), SnackBarActivity, NavController.OnDestin
Log.d("[Main Activity] Found post create intent") Log.d("[Main Activity] Found post create intent")
handleIntentParams(intent) handleIntentParams(intent)
} }
requestCallRedirectionRole()
} }
override fun onDestroy() { override fun onDestroy() {
@ -737,4 +740,17 @@ class MainActivity : GenericActivity(), SnackBarActivity, NavController.OnDestin
dialog.show() dialog.show()
authenticationRequiredDialog = dialog authenticationRequiredDialog = dialog
} }
private fun requestCallRedirectionRole() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
val roleManager: RoleManager = getSystemService(Context.ROLE_SERVICE) as RoleManager
// Check if the app needs to register call redirection role.
val shouldRequestRole = roleManager.isRoleAvailable(RoleManager.ROLE_CALL_REDIRECTION) &&
!roleManager.isRoleHeld(RoleManager.ROLE_CALL_REDIRECTION)
if (shouldRequestRole) {
val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_REDIRECTION)
startActivityForResult(intent, 123456)
}
}
}
} }

View file

@ -104,9 +104,18 @@ class TelecomConnectionService : ConnectionService() {
request: ConnectionRequest request: ConnectionRequest
): Connection { ): Connection {
if (coreContext.core.callsNb == 0) { if (coreContext.core.callsNb == 0) {
val uri = request.address
val address = coreContext.core.interpretUrl(uri.toString(), true)
if (address != null) {
Log.i(
"[Telecom Connection Service] Starting call to [${address.asStringUriOnly()}]"
)
coreContext.startCall(address = address)
} else {
Log.w("[Telecom Connection Service] No call in Core, aborting outgoing connection!") Log.w("[Telecom Connection Service] No call in Core, aborting outgoing connection!")
return Connection.createCanceledConnection() return Connection.createCanceledConnection()
} }
}
val accountHandle = request.accountHandle val accountHandle = request.accountHandle
val componentName = ComponentName(applicationContext, this.javaClass) val componentName = ComponentName(applicationContext, this.javaClass)

View file

@ -0,0 +1,69 @@
/*
* Copyright (c) 2010-2025 Belledonne Communications SARL.
*
* This file is part of linphone-android
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.linphone.telecom
import android.net.Uri
import android.os.Build
import android.telecom.CallRedirectionService
import android.telecom.PhoneAccountHandle
import androidx.annotation.RequiresApi
import androidx.core.net.toUri
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.core.tools.Log
@RequiresApi(Build.VERSION_CODES.Q)
class TelecomRedirectionService : CallRedirectionService() {
companion object {
private const val TAG = "[Telecom Redirection Service]"
}
override fun onPlaceCall(
handle: Uri,
initialPhoneAccount: PhoneAccountHandle,
allowInteractiveResponse: Boolean
) {
Log.i("$TAG onPlaceCall called with URI [$handle]")
if (handle.scheme != "tel") {
placeCallUnmodified()
return
}
val number = handle.toString().substring("tel:".length)
Log.i("$TAG Extracted number [$number] from tel: URI")
if (TelecomHelper.exists()) {
val telecomHelper = TelecomHelper.get()
val phoneAccount = telecomHelper.findExistingAccount(coreContext.context)
val handle = phoneAccount?.accountHandle
if (handle != null) {
redirectCall(
"sip:sylvain@sip.linphone.org".toUri(),
handle,
allowInteractiveResponse
)
} else {
placeCallUnmodified()
}
} else {
placeCallUnmodified()
}
}
}