How do you extend ConnectionService?

2.7k views Asked by At

I want to implement a simple app that will allow me to filter incoming phone calls. I've tried to use BroadcastReceiver to detect calls, and then use TelecomManager to end calls, but TelecomManager's endCall() requires system privileges.

I'm now trying to follow this guide to implement a calling app, which involves implementing the ConnectionService class and using that to create/handle "Connections". Connections represent a phone call and have a setDisconnected() function which I intend to use to filter calls.

The issue is that it seems like my ConnectionService class isn't binding properly to the TelecomManager and its functions aren't getting called because the log statement doesn't happen. How can I make it bind properly?

My implementation of ConnectionService:

public class CallHandler extends ConnectionService {

    @Override
    public Connection onCreateOutgoingConnection (PhoneAccountHandle connectionManagerPhoneAccount,
                                                  final ConnectionRequest request) {
        return null;
    }

    @Override
    public void onCreateOutgoingConnectionFailed (PhoneAccountHandle connectionManagerPhoneAccount,
                                                  final ConnectionRequest request) {
    }

    @Override
    public void onCreateIncomingConnectionFailed (PhoneAccountHandle connectionManagerPhoneAccount,
                                                  final ConnectionRequest request) {

    }

    @Override
    public Connection onCreateIncomingConnection (PhoneAccountHandle connectionManagerPhoneAccount,
                                                  final ConnectionRequest request) {
        Log.i("~~~~INFO", "onCreateIncomingConnection was called!!!!");
        return null;
    }


    public class TestConnection extends Connection {
        @Override
        public void onAnswer() {

        }

        @Override
        public void onReject() {

        }
    }
}

Manifest:

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

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

    <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/AppTheme">
        <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=".CallHandler"
            android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
            <intent-filter>
                <action android:name="android.telecom.ConnectionService" />
            </intent-filter>
        </service>

    </application>

</manifest>

The code inside main's onCreate to register CallHandler:

private void register() {
    Log.i("Info:", "Register Phone account called");
    TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);
    PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
            new ComponentName(getPackageName(),
                    CallHandler.class.getName()), "CallHandlerId");
    PhoneAccount.Builder builder = PhoneAccount.builder(phoneAccountHandle, "CustomAccount");
    builder.setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED);
    PhoneAccount phoneAccount = builder.build();

    manager.registerPhoneAccount(phoneAccount);
}
1

There are 1 answers

0
Jose Riballo On

If you want to reject some incoming calls then you shouldn't use PhoneAccount.CAPABILITY_SELF_MANAGED because it should only be used to manage your own calls. You need PhoneAccount.CAPABILITY_CALL_PROVIDER, PhoneAccount.CAPABILITY_CONNECTION_MANAGER on your phoneAccount and set it as the default phoneAccount on your system. I did id two years ago, I may forget something. Please, let me know your progress.

Enjoy coding.