4

I'm building an app that hooks on the stock Dialer (Marshmallow API). My goal is to get incoming and place outgoing calls, while getting a handle on the Connection objects to manipulate the Connection's methods.

I have registered PhoneAccount with the CAPABILITY_CALL_PROVIDER.

PhoneAccount.Builder builder = new PhoneAccount.Builder(phoneAccountHandle, "CustomAccount");
builder.setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER);
PhoneAccount phoneAccount = builder.build();
telecomManager.registerPhoneAccount(phoneAccount);

My account is visible inside the stock Dialer app (Settings-> Calls-> Calling Accounts) and I have enabled it.

I have a Service that monitors Phone State and on CALL_STATE_RINGING it calls TelecomManager's addNewIncomingCall() method.

public void onCallStateChanged(int state, String incomingNumber) {
    if (state == TelephonyManager.CALL_STATE_RINGING) {
        Toast.makeText(getApplicationContext(), "Phone Is Ringing",
                Toast.LENGTH_SHORT).show();
        Bundle extras = new Bundle();
        Uri uri = Uri.fromParts(PhoneAccount.SCHEME_TEL, incomingNumber, null);
        extras.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS, uri);
        extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
        telecomManager.addNewIncomingCall(phoneAccountHandle, extras);
    }
    if (state == TelephonyManager.CALL_STATE_OFFHOOK) {.......}
    ...
} 

My custom Connection Service:

@Override
public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {

    Toast.makeText(getApplicationContext(), "onCreateIncomingConnection called", Toast.LENGTH_SHORT).show();
    Connection incomingCallCannection = createConnection(request);
    incomingCallCannection.setRinging();
    return incomingCallCannection;
}

@Override
public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
    Toast.makeText(getApplicationContext(), "onCreateOutgoingConnection called", Toast.LENGTH_SHORT).show();

    Connection outgoingCallConnection = createConnection(request);
    outgoingCallConnection.setDialing();

    return outgoingCallConnection;
}

private Connection createConnection(ConnectionRequest request) {
    mConnection = new Connection() {
        @Override
        public void onStateChanged(int state) {
            super.onStateChanged(state);
        }

        @Override
        public void onDisconnect() {
            super.onDisconnect();
            mConnection.setDisconnected(new DisconnectCause(DisconnectCause.CANCELED));
            mConnectionsAvailableForConference.clear();
            mConnection.destroy();
        }

        @Override
        public void onSeparate() {
            super.onSeparate();
        }

        @Override
        public void onAbort() {
            super.onAbort();
            mConnection.setDisconnected(new DisconnectCause(DisconnectCause.CANCELED));
            mConnection.destroy();
        }

        @Override
        public void onHold() {
            super.onHold();
        }

        @Override
        public void onAnswer() {
            super.onAnswer();
            mConnection.setActive();
        }

        @Override
        public void onReject() {
            super.onReject();
            mConnection.setDisconnected(new DisconnectCause(DisconnectCause.CANCELED));
            mConnection.destroy();

        }
    };
    mConnection.setAddress(request.getAddress(), TelecomManager.PRESENTATION_ALLOWED);
    mConnection.setExtras(request.getExtras());
    return mConnection;
}

Now, both ConnectionService's callback methods get called on incoming and outgoing calls respectively. The problem is, when I go to the Dialer and place an outgoing call (using my PhoneAccount) I get the dialing screen (inCallUI ?), with the right caller info being shown (contact name, tel # etc..), but the line doesn't ring in my earpiece and the call is not established (the telephone number that should be receiving the call doesn't ring).

I tried returning super.onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) in the callback instead of creating my own Connection object, and I get the same behavior.

TLDR: my app communicates with the Dialer, is able to place a call and show the dialing screen, but the phone line doesn't ring and nothing happens.

niton
  • 8,771
  • 21
  • 32
  • 52
rayiskon
  • 41
  • 3

1 Answers1

0

I have been on this for days finding a solution. But after going through the documentation over again it clearly stated that placing outgoing call with a custom PhoneAccount does not use the phone sim service to make the call, it the app that will handle all the call operation by itself.

CAPABILITY_CALL_PROVIDER: Flag indicating that this PhoneAccount can make phone calls in place of traditional SIM-based telephony calls.

if you need to transfer data during outgoing call you can use the Bundle to send info to the default call app.

you can read more on the documentation here. https://developer.android.com/reference/android/telecom/PhoneAccount https://developer.android.com/guide/topics/connectivity/telecom/selfManaged#outgoing

Thecarisma
  • 1,424
  • 18
  • 19