Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConnectionService implementation for OutgoingCall

I am trying to implement ConnectionService, PhoneAccount and PhoneAccountHandle inorder to get the disconnectCause for the disconnection of my outgoing call(E.g. User rejected the call, unreachable, etc.) So far I have been able to initiate a call using customPhoneAccount but that phonecall never goes through and hence I am unable to get the response.

Here is the code which I have written uptill now:
register() is the method which is being called from my activity to register the phoneAccount:

public void register() {
    TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);
    PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
            new ComponentName(getPackageName(),
                    MyConnectionService.class.getName()), "myConnectionServiceId");
    PhoneAccount.Builder builder = PhoneAccount.builder(phoneAccountHandle, "CustomAccount");
    builder.setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_CONNECTION_MANAGER);
    PhoneAccount phoneAccount = builder.build();
    manager.registerPhoneAccount(phoneAccount);
}

call()method which initiates the call:

 public void call() {
    TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);
    PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
            new ComponentName(getPackageName(),
                    MyConnectionService.class.getName()), "myConnectionServiceId");
    Bundle test = new Bundle();
    test.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
    manager.placeCall(Uri.parse("tel:" + "1212312312"), test);
}

Here is MyConnectionService:

public class MyConnectionService extends ConnectionService {

public static final String TAG = MyConnectionService.class.getName();
@Override
public int onStartCommand(Intent intent,int flags, int startId) {
    Log.d(TAG, "On Start");
    return super.onStartCommand(intent, flags, startId);
}

@Override
public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
    Connection connection = super.onCreateOutgoingConnection(connectionManagerPhoneAccount, request);
    Log.d(TAG, connection.getDisconnectCause().getReason());
    return connection;
}

@Override
public void onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
    if (request != null) {
        Log.d(TAG, request.toString());
    }
    super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount, request);
}

I have tried with different capabilities along with all options mentioned on other threads related to ConnectionService but couldn't get the desired result, can anybody help me in this ?

like image 737
Saurabh7474 Avatar asked Jun 27 '17 11:06

Saurabh7474


1 Answers

A few things:

  1. You should not use the PhoneAccount.CAPABILITY_CONNECTION_MANAGER, that has a special purpose which is not applicable to your use case and which requires special permissions.

  2. Your onCreateOutgoingConnection method should not call the super version; that method is an blank method in the base ConnectionService class. Telecom calls your override of onCreateOutgoingConnection so that you can provide a new instance of Connection.

Take a look at the Android Open Source TestConnectionService class; it implements a simple ConnectionService: https://android.googlesource.com/platform/packages/services/Telecomm/+/master/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java

like image 56
TGunn Avatar answered Nov 08 '22 06:11

TGunn