ConnectionService not started from TelecomManager#placeCall

731 Views Asked by At

I need to intercept the events of a outgoing call made by the device framework.
Following the android guide, i'm stopped at point 3 The telecom subsystem binds to your app's ConnectionService implementation., that is i have come to this point:

Call flow

val telecomManager :TelecomManager= getSystemService(
        TELECOM_SERVICE
    ) as TelecomManager
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CALL_PHONE),
            2333)
    } else {
        try {
            val phoneAccountHandle = PhoneAccountHandle(ComponentName(
                applicationContext,
                MyConnectionService::class.java
            ), "ID999")
            telecomManager.registerPhoneAccount(PhoneAccount.builder(
                phoneAccountHandle,
                "label"
            ).setCapabilities(PhoneAccount.CAPABILITY_CONNECTION_MANAGER) .build())
            val extras = Bundle()
            extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle)
            telecomManager.placeCall(Uri.parse("tel:$phoneNumber"), extras)
        } catch (e: SecurityException) {
            e.printStackTrace()
        }
    }

ConnectionService

class MyConnectionService : ConnectionService() {
private val TAG = "mycnnser"
override fun onCreate() {
    super.onCreate()
    Log.d(TAG, "onCreate: ")
}

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
    Log.d(TAG, "onStartCommand: ")
    return super.onStartCommand(intent, flags, startId)
}

override fun onCreateIncomingConnection(connectionManagerPhoneAccount: PhoneAccountHandle, request: ConnectionRequest): Connection {
    Log.d(TAG, "onCreateIncomingConnection: ")
    return super.onCreateIncomingConnection(connectionManagerPhoneAccount, request)
}

override fun onCreateIncomingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle, request: ConnectionRequest) {
    Log.d(TAG, "onCreateIncomingConnectionFailed: ")
    super.onCreateIncomingConnectionFailed(connectionManagerPhoneAccount, request)
}

override fun onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle, request: ConnectionRequest) {
    Log.d(TAG, "onCreateOutgoingConnectionFailed: ")
    super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount, request)
}

override fun onCreateOutgoingConnection(connectionManagerPhoneAccount: PhoneAccountHandle, request: ConnectionRequest): Connection {
    Log.d(TAG, "onCreateOutgoingConnection: ")
    return super.onCreateOutgoingConnection(connectionManagerPhoneAccount, request)
}

override fun onCreateOutgoingHandoverConnection(fromPhoneAccountHandle: PhoneAccountHandle, request: ConnectionRequest): Connection {
    Log.d(TAG, "onCreateOutgoingHandoverConnection: ")
    return super.onCreateOutgoingHandoverConnection(fromPhoneAccountHandle, request)
}

override fun onCreateIncomingHandoverConnection(fromPhoneAccountHandle: PhoneAccountHandle, request: ConnectionRequest): Connection {
    Log.d(TAG, "onCreateIncomingHandoverConnection: ")
    return super.onCreateIncomingHandoverConnection(fromPhoneAccountHandle, request)
}

override fun onHandoverFailed(request: ConnectionRequest, error: Int) {
    super.onHandoverFailed(request, error)
    Log.d(TAG, "onHandoverFailed: ")
}

override fun onConference(connection1: Connection, connection2: Connection) {
    super.onConference(connection1, connection2)
    Log.d(TAG, "onConference: ")
}

override fun onRemoteConferenceAdded(conference: RemoteConference) {
    super.onRemoteConferenceAdded(conference)
    Log.d(TAG, "onRemoteConferenceAdded: ")
}

override fun onRemoteExistingConnectionAdded(connection: RemoteConnection) {
    super.onRemoteExistingConnectionAdded(connection)
    Log.d(TAG, "onRemoteExistingConnectionAdded: ")
}

override fun onConnectionServiceFocusLost() {
    super.onConnectionServiceFocusLost()
    Log.d(TAG, "onConnectionServiceFocusLost: ")
}

override fun onConnectionServiceFocusGained() {
    super.onConnectionServiceFocusGained()
    Log.d(TAG, "onConnectionServiceFocusGained: ")
}}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.thorny.myapplication">

  <uses-permission android:name="android.permission.MANAGE_OWN_CALLS"/>
  <uses-permission android:name="android.permission.READ_CALL_LOG"/>
  <uses-permission android:name="android.permission.READ_PHONE_STATE"
      android:maxSdkVersion="29"/>
  <uses-permission android:name="android.permissions.READ_PHONE_NUMBERS"/>
  <uses-permission
      android:name="android.permission.CALL_PHONE" />

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.MyApplication">
    <activity android:name=".MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <service android:name=".MyConnectionService"
        android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
      <intent-filter>
        <action android:name="android.telecom.ConnectionService" />
      </intent-filter>
    </service>
  </application>

</manifest>

Problem: the call starts through the device framework but all the service logs are never triggered. My Android Version is 10. Thanks

0

There are 0 best solutions below