Android ConnectionService integration¶
This article explains the steps required to integrate libSoftphone with Android ConnectionService API in SelfManaged mode. Refer to Official Android Documentation for more information on ConnectionService API.
Step 1: Implement ConnectionService¶
LibSoftphone takes care of all the heavy lifting around ConnectionService implementation for you.
However, the host app needs provide a Service derived from cz.acrobits.libsoftphone.telecom.ConnectionService
that ensures that libSoftphone is initialized whenever ConnectionService::onCreate()
is invoked.
Implement your simple ConnectionService as follows:
1public class MyConnectionService extends cz.acrobits.libsoftphone.telecom.ConnectionService 2{ 3 @Override 4 public void onCreate() 5 { 6 super.onCreate(); 7 // start() method should ensure that libSoftphone is initialized and running 8 DemoPhoneApplication.start(); 9 } 10}
Step 2: Add ConnectionService implementation to manifest¶
Add your ConnectionService implementation to manifest:
1<service android:name=".MyConnectionService" 2 android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE" 3 android:exported="true"> 4 <intent-filter> 5 <action android:name="android.telecom.ConnectionService" /> 6 </intent-filter> 7</service>
Step 3: Add permissions to manifest¶
You’ll need to add the following permissions into your app’s manifest:
1 <uses-permission android:name="android.permission.MANAGE_OWN_CALLS"/> 2 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Step 4: Request Phone permissions at runtime¶
Request the Phone permission before using the Telecom API:
1String[] permissions; 2if(Build.VERSION.SDK_INT >= 31) 3 permissions = new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_PHONE_NUMBERS}; 4else 5 permissions = new String[]{Manifest.permission.READ_PHONE_STATE}; 6 7ActivityCompat.requestPermissions(this, permissions, PHONE_PERMISSION_REQUEST_CODE);
Step 5: Register your PhoneAccount¶
Register your app’s Telecom API PhoneAccount on app start:
1Context context = AndroidUtil.getApplicationContext(); 2// create some account icon, e.g. from app icon 3Icon icon = Icon.createWithResource(context, R.mipmap.icon); 4String accountName = "MyConnectionServiceAccount"; 5ComponentName serviceComponent = new ComponentName(context, MyConnectionService.class); 6TelecomUtil.registerCallingAccount(serviceComponent, true);
Step 6: Make sure Self Managed Call Integration is enabled¶
The value of the callIntegrationMode
preference key needs to be set to bestEffort
.
The bestEffort
mode will attempt to use the Self Managed ConnectionService when possible:
On APIs below 28, it will not use ConnectionService at all due to compatibility reasons.
On APIs 28+, it will it will attempt to establish a ConnectionService; and in case of failure: * If an outgoing call fails to establish a ConnectionService, it will still go through without ConnectionService integration. * If an incoming call fails to establish a ConnectionService, it will be automatically rejected.