Prototype to try CallRedirectionService APIs

This commit is contained in:
Sylvain Berfini 2025-08-07 10:40:59 +02:00
parent 3f22a596db
commit dda41fb88e
3 changed files with 111 additions and 0 deletions

View file

@ -42,6 +42,9 @@
<!-- Needed for auto start at boot if keep alive service is enabled -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
<application
android:name=".LinphoneApplication"
android:allowBackup="true"
@ -208,6 +211,15 @@
</intent-filter>
</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 -->
<receiver android:name=".core.CorePushReceiver"

View file

@ -0,0 +1,73 @@
/*
* 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.content.Context
import android.net.Uri
import android.os.Build
import android.telecom.CallRedirectionService
import android.telecom.PhoneAccount
import android.telecom.PhoneAccountHandle
import android.telecom.TelecomManager
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")
val telecomManager: TelecomManager = coreContext.context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
val phoneAccountHandleList: List<PhoneAccountHandle> = telecomManager.selfManagedPhoneAccounts
var found = false
for (phoneAccountHandle in phoneAccountHandleList) {
val phoneAccount: PhoneAccount =
telecomManager.getPhoneAccount(phoneAccountHandle)
Log.i("$TAG handle [$phoneAccountHandle] componentName = ${phoneAccountHandle.componentName}")
if (phoneAccountHandle.componentName.packageName == coreContext.context.packageName) {
Log.i("$TAG PLACING CALL!")
redirectCall("sip:sylvain@sip.linphone.org".toUri(), phoneAccountHandle, allowInteractiveResponse)
found = true
}
}
if (!found) {
Log.i("$TAG No matching phone account handle found :'(")
placeCallUnmodified()
}
}
}

View file

@ -22,6 +22,8 @@ package org.linphone.ui.main
import android.Manifest
import android.annotation.SuppressLint
import android.app.Dialog
import android.app.role.RoleManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
@ -89,6 +91,8 @@ class MainActivity : GenericActivity() {
private const val CHAT_FRAGMENT_ID = 3
private const val MEETINGS_FRAGMENT_ID = 4
private const val REDIRECT_ROLE_REQUEST_CODE = 1
const val ARGUMENTS_CHAT = "Chat"
const val ARGUMENTS_CONVERSATION_ID = "ConversationId"
}
@ -391,6 +395,8 @@ class MainActivity : GenericActivity() {
Log.d("$TAG savedInstanceState is null but intent isn't, handling it")
handleIntent(intent)
}
requestCallRedirectionRole()
}
override fun onPause() {
@ -849,4 +855,24 @@ class MainActivity : GenericActivity() {
}
}
}
private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { isGranted ->
}
private fun requestCallRedirectionRole() {
requestPermissionLauncher.launch(arrayOf(Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_PHONE_NUMBERS))
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, REDIRECT_ROLE_REQUEST_CODE)
}
}
}
}